-2

Hi all i am working for quite a while now with c++, and this is a problem that's bothering me.

I try to evaluate pow(2,30), but the answer comes like this 1.07374e+009, instead of the expected integer form 1073741824. I tried casting it to int or long, but the thing is, then its sometimes truncates it to a lower value. For example, 1073741824 sometimes gets truncated to 1073741823. What should i do? Also if the number is not 2, how should one make sure to get the answer in right format.

Saurabh Kumar
  • 2,088
  • 14
  • 17
  • First is due to how you output it. Define the format in a way you want. Second is due to precision. – Sami Kuhmonen Jul 08 '18 at 18:13
  • 2
    std::pow always produces floats, doubles, or long doubles regardless of input types. – doug Jul 08 '18 at 18:16
  • 3
    Handy reading: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) TL;DR version: The result was likely something like 1073741823.99999999. Casting to an integer lops of the fraction rather than performing any intelligent rounding. – user4581301 Jul 08 '18 at 18:16

2 Answers2

1

If you are sure that always you calculate the power of 2. Its better if you use the left shift operator since it is very efficient compared to pow() function. Have a look this snippet. It works like charm.

#include <iostream>

int main()
{
    long pow_value = (1L << 30); //Equivalent to calculating 2^30
    printf("%ld",pow_value);

    return 0;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
yadhu
  • 1,253
  • 14
  • 25
  • 3
    Right idea, but a good answer needs to explain what went wrong in addition to providing an alternative. – user4581301 Jul 08 '18 at 18:19
  • The behavior which the user is facing is expected from the floating point operations. user is better off using the alternative integer version to calculate powers. – yadhu Jul 08 '18 at 18:26
  • Incidentally, you don't need the parentheses around `1 << 30` (or `1L << 30` with my edit). – Pete Becker Jul 08 '18 at 18:39
  • i got the idea, but what should i do if the number is not two. No one till now has given me the answer to that. :( – Saurabh Kumar Jul 08 '18 at 20:07
  • Thanks for the edit. @PeteBecker This logic not only works for 2, it works for all the power two numbers, 2^30 == 4^28 = 8^27. However if you have any other number, You can use logic in the question [The most efficient way to implement an integer based power function pow(int, int)](https://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int) – yadhu Jul 08 '18 at 20:18
  • @SaurabhKumar — nobody has answered about numbers other than two because you didn’t ask about them. – Pete Becker Jul 08 '18 at 20:28
0

pow() only works with floating point numbers, and when you output a large floating point number, the << operator automatically prints it in scientific notation. Casting it to an integer type won't work because floating point numbers aren't 100% precise and have floating point error.

If you only need powers of 2, you can use bit shifting. a << b is equivalent to a times 2 to the bth power. Shifting something to the left multiplies it by a power of 2 in binary just like how moving the decimal point multiplies a number by powers of 10 in decimal. If you need powers with other bases, you can use something from this post.

eesiraed
  • 4,626
  • 4
  • 16
  • 34