Menu

Problem using pow

hydra_be
2009-04-25
2013-04-11
  • hydra_be

    hydra_be - 2009-04-25

    Hi, i am using the Real library for a little project of mine and i have a question about the pow method.

    In the program I am making it might happen that a 5th root of -0.6 must be calculated using the pow function: (-0.6)^(0.2)

    As the 5th root of 0.6 is 0.90288...
    0.90288... to the power 5 is 0.6
    and -0.90288... to the power 5 is -0.6
    so -0.90288.. is the 5th root of -0.6

    But the pow method returns nan instead of -0.90288... :(.

    Am i doing something wrong here? Any help would be appreciated!

     
    • Roar Lauritzsen

      Roar Lauritzsen - 2009-04-27

      The return of "nan" is intentional. The problem is that the Real library does not know that your number "0.2" equals exactly 1/5, it treats it like any other non-integer number. If one pursued your thought further, one could end up arguing that it should be possible to raise a negative number to any fraction that has an odd denominator, e.g. "(-0.6)^(3425/13)". However, determining that a given number contains a specific fraction is a complex process. It may seem to you that 0.2 is such a "round" number that it should merit special treatment, but in the Real library, and in most other math libraries around the world, it doesn't.

      However, in the Real library, there is an alternative. You can use the nroot function. E.g:
        Real a("-0.6");
        a.nroot(5);  // calculates the fifth root of a
      This would give you the correct answer.

       

Log in to post a comment.