Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pow(10,5) = 9,999 in C++

Tags:

c++

mingw

pow

Recently i write a block of code:

const int sections = 10;

for(int t= 0; t < 5; t++){
   int i = pow(sections, 5- t -1);  
   cout << i << endl;
}

And the result is wrong:

9999
1000
99
10
1

If i using just this code:

for(int t = 0; t < 5; t++){
    cout << pow(sections,5-t-1) << endl; 
}

The problem doesn't occur anymore:

10000
1000
100
10
1

Does anyone give me an explaination? thanks you very much!

like image 758
Kingfisher Phuoc Avatar asked Mar 14 '12 14:03

Kingfisher Phuoc


People also ask

Why POW is used in C?

pow() function in C The function pow() is used to calculate the power raised to the base value. It takes two arguments. It returns the power raised to the base value. It is declared in “math.

Is POW faster than multiplication C++?

C++11 Performance Tip: Update on When to Use std::pow When not compiling with -ffast-math, direct multiplication was significantly faster than std::pow , around two orders of magnitude faster when comparing x * x * x and code:std::pow(x, 3) .

Why do we use POW in C++?

The pow() function returns the result of the first argument raised to the power of the second argument. This function is defined in the cmath header file.

What data type does the POW function return?

pow() function returns the base to the exponent power, as in base exponent , the base and the exponent are in decimal numeral system.


2 Answers

Due to the representation of floating point values pow(10.0, 5) could be 9999.9999999 or something like this. When you assign that to an integer that got truncated.

EDIT: In case of cout << pow(10.0, 5); it looks like the output is rounded, but I don't have any supporting document right now confirming that.

EDIT 2: The comment made by BoBTFish and this question confirms that when pow(10.0, 5) is used directly in cout that is getting rounded.

like image 68
taskinoor Avatar answered Oct 06 '22 09:10

taskinoor


When used with fractional exponents, pow(x,y) is commonly evaluated as exp(log(x)*y); such a formula would mathematically correct if evaluated with infinite precision, but may in practice result in rounding errors. As others have noted, a value of 9999.999999999 when cast to an integer will yield 9999. Some languages and libraries use such a formulation all the time when using an exponentiation operator with a floating-point exponent; others try to identify when the exponent is an integer and use iterated multiplication when appropriate. Looking up documentation for the pow function, it appears that it's supposed to work when x is negative and y has no fractional part (when x is negative and `y is even, the result should be pow(-x,y); when y is odd, the result should be -pow(-x,y). It would seem logical that when y has no fractional part a library which is going to go through the trouble of dealing with a negative x value should use iterated multiplication, but I don't know of any spec dictating that it must.

In any case, if you are trying to raise an integer to a power, it is almost certainly best to use integer maths for the computation or, if the integer to be raised is a constant or will always be small, simply use a lookup table (raising numbers from 0 to 15 by any power that would fit in a 64-bit integer would require only a 4,096-item table).

like image 43
supercat Avatar answered Oct 06 '22 08:10

supercat