Power (O(log n)) from hackerBlocks

solved the problem , then looked for editoral , found following code

float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temptemp;
else
{
if(y > 0)
return x
temptemp;
else
return (temp
temp)/x;
}
}

in this when y is even (if statement), which case is handeled by if(y<0) and how.

if power i.e. y could be negative, why don’t it considers it in first most if when y is even

Y<0 case is not handled explicitly in the even case because an even number divided by 2 will give the exact number, eg y=-4 or y=4 (y/2=-2/2) but if y is odd, y=-5 (y/2=-3 now temp * temp will give x^-6 so we divide by x to get x^-5) if y=5 (y/2=2 now temp *temp=y^4 so we need to multiply x)