1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

pow() and modulus trouble

Discussion in 'C' started by akidamo, Apr 7, 2006.

  1. akidamo

    akidamo New Member

    Joined:
    Apr 7, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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!! :eek:
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    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.
     
  3. akidamo

    akidamo New Member

    Joined:
    Apr 7, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    ahh thanks anyway, I guess there is nothing I can really do then to get the correct answer.
     
  4. akidamo

    akidamo New Member

    Joined:
    Apr 7, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    actually I found a way to do it :) ty
     
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Why dont you share that with us.
     
  6. akidamo

    akidamo New Member

    Joined:
    Apr 7, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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! :)
     

Share This Page