Why does pow (n, 2) return 24 when n = 5, with my compiler and OS?

#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int n,i,ele; n=5; ele=pow(n,2); printf("%d",ele); return 0; } 

Output 24 .

I am using GNU / GCC in Code :: Blocks.

What's happening?

I know that the pow function returns a double , but 25 type int, so why does this code print 24 instead of 25 ? If n=4; n=6; n=3; n=2; n=4; n=6; n=3; n=2; the code works, but with five it doesn't work.

+19
c c-standard-library
Sep 05 '14 at 4:08
source share
5 answers

Here is what can happen here. You should be able to confirm this by looking at the implementation of your pow function compiler:

Assuming you have the correct #include, (all the previous answers and comments about this are correct - do not accept #include files as given), the prototype of the standard pow function is the following:

double pow(double, double);

and you call pow like this:

pow(5,2);

The pow function goes through an algorithm (possibly using logarithms), so it uses functions and floating-point values ​​to calculate the power value.

The pow function does not go through the naive "multiply the value of x by the sum of n times", since it must also calculate pow using fractional exponents, and you cannot calculate fractional powers in this way.

Thus, it is more likely that the calculation of pow using parameters 5 and 2 led to a small rounding error. When you assigned int , you truncated the fractional value, giving 24.

If you use integers, you can write your own "intpow" or similar function, which simply multiplies the value as many times as necessary. The advantages of this:

  • You will not encounter a situation where you can get subtle rounding errors with pow .

  • The intpow function is likely to be faster than the equivalent pow call.

+20
Sep 05 '14 at 4:42
source share
β€” -

You want to get the result from a function designed to double.

Perhaps you should use

 ele=(int)(0.5 + pow(n,2)); /* ^ ^ */ /* casting and rounding */ 
+2
Sep 05 '14 at 5:06
source share

If you are not #include <math.h> , then the compiler does not know the types of the pow() arguments, which are like double not int , so you get an undefined result. You get 24, I get 16418.

+1
Sep 05 '14 at 4:19
source share

When you use pow with variables, its result is double . The int assignment truncates it.

Thus, you can avoid this error by assigning the result to pow to double or float .

So basically

This means exp(log(x) * y) , which will produce a result that doesn't exactly match x^y - an almost approximate value as a floating point value. So, for example, 5^2 becomes 24.9999996 or 25.00002

+1
Sep 05 '14 at 4:31
source share

Floating-point arithmetic is not accurate.

Although small values ​​can be added and subtracted exactly, the pow() function usually works by multiplying the logarithms, so even if the inputs are accurate, the result will not be. The int assignment always truncates, so if the inaccuracy is negative, you will get 24, not 25.

The moral of this story is to use integer operations on integers and be suspicious of the <math.h> functions when the actual arguments must be advanced or truncated. Unfortunately, GCC does not warn you if you did not add -Wfloat-conversion (this is not in -Wall -Wextra , perhaps because there are many cases where such a conversion is expected and necessary).

For integer degrees, it is always safer and faster to use multiplication (division, if negative) rather than pow() - reserve the latter for where it is needed! However, keep in mind the risk of overflow.

+1
Dec 15 '16 at 11:28
source share



All Articles