Reciprocal

 

I am not the greatest mathematician but I have some functions I am converting from easy language that use the reciprocal of a number to provide a multiplyer in another function.

double method (int pwr)

{

double frac=0;

frac=1/pwr;

dootherstuffwith frac

}

However when I Print this the expression 1/pwr comes out as zero. I am doing binary progression stuff so all the numbers are of the series 1, 2, 4, 8, 16 etc. I can't see what I am doing wrong. I can't see a math.reciprocal function. Short of doing a pseudo lookup function ;

if(pwr = 2)

return(.5);

if(pwr = 4)

return(.25);

etc etc

any ideas ????

Thanks

Paz

 

Hi. You need to define the '1' as a variable, type double, first - ie.

double method (int pwr)

{

double frac=0,temp;

temp = 1;

frac=temp/pwr;

dootherstuffwith frac

}

 

It would be very helpful if you could explain why that is the case. But in any case, thanks very much.

 
Q0paz:
It would be very helpful if you could explain why that is the case. But in any case, thanks very much.

because when you devide an integer by an integer the value is expressed as an integr with a remainder, not the fractional value.

 

Ahhh ! Thank you.

Reason: