pow() and modulus trouble

Newbie Member
7Apr2006,06:49   #1
akidamo's Avatar
Hi I am programming in C,

and when I do a pow function such as num = pow(28,7);

If num is a float or a double, the correct answer is achieved, but if it is an int or a long, num is assigned an incorrect answer.

Which gives me a problem because I then what to do num%33.

Like I said if I do it, when num is an int, the answer is incorrect.
and if I keep num as a double or float, I get the error:
illegal use of floating point in function.....

Is there anyway to overcome this? It is driving me mad!!
Team Leader
7Apr2006,08:04   #2
coderzone's Avatar
Its not the problem with the pow but its just long size is not big enough to hold the data.

long iTemp = (long)pow(28,7);

This should avoid all the errors and warning but the out put is not correct because the actual output is 13492928512 which is more than 2^32.
Newbie Member
7Apr2006,09:07   #3
akidamo's Avatar
ahh thanks anyway, I guess there is nothing I can really do then to get the correct answer.
Newbie Member
7Apr2006,09:44   #4
akidamo's Avatar
actually I found a way to do it ty
Team Leader
7Apr2006,10:44   #5
coderzone's Avatar
Why dont you share that with us.
Newbie Member
7Apr2006,12:01   #6
akidamo's Avatar
Sorry! of course, its a bit long, but I got the answer I needed. Just a bit of maths.

Instead of doing modulus 33...

I used a float, say float num1, and found pow(28,7)
I then divided it by 33,
I then assigned this answer to a long/int, which automatically trunicates it, say long num2
I then again assigned it back to a float, and multiplied this float by 33. say float num3
Then i did num1 - num3
which give me the same answer as pow(28,7)%33 should have given me.

Not the best at explaining!