Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}

The output is 24.

I'm using GNU/GCC in Code::Blocks.

What is happening?

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

like image 637
exsnake Avatar asked Sep 05 '14 04:09

exsnake


People also ask

Does POW return a double?

pow Example. The method raises a to the power of b and returns the result as double. In other words, a is multiplied by itself b times.

Why POW function is not working in C?

The above error occurs because we have added “math. h” header file, but haven't linked the program to the following math library. Link the program with the above library, so that the call to function pow() is resolved.

What is the return type of pow ()?

The pow() function returns the value x y x^y xy (x raised to the power y) where x and y are two variables of type double. The return type is double.

What is the time complexity of POW?

Time Complexity: O(N) because pow(x,n) is called recursively for each number from 1 to n.


3 Answers

Here is what may be happening here. You should be able to confirm this by looking at your compiler's implementation of the pow function:

Assuming you have the correct #include's, (all the previous answers and comments about this are correct -- don't take the #include files for granted), the prototype for the standard pow function is this:

double pow(double, double);

and you're calling pow like this:

pow(5,2);

The pow function goes through an algorithm (probably using logarithms), thus uses floating point functions and values to compute the power value.

The pow function does not go through a naive "multiply the value of x a total of n times", since it has to also compute pow using fractional exponents, and you can't compute fractional powers that way.

So more than likely, the computation of pow using the parameters 5 and 2 resulted in a slight rounding error. When you assigned to an int, you truncated the fractional value, thus yielding 24.

If you are using integers, you might as well write your own "intpow" or similar function that simply multiplies the value the requisite number of times. The benefits of this are:

  1. You won't get into the situation where you may get subtle rounding errors using pow.

  2. Your intpow function will more than likely run faster than an equivalent call to pow.

like image 81
PaulMcKenzie Avatar answered Sep 19 '22 23:09

PaulMcKenzie


You want int result from a function meant for doubles.

You should perhaps use

ele=(int)(0.5 + pow(n,2)); /*    ^    ^              */ /* casting and rounding   */ 
like image 30
Mohit Jain Avatar answered Sep 17 '22 23:09

Mohit Jain


Floating-point arithmetic is not exact.

Although small values can be added and subtracted exactly, the pow() function normally works by multiplying logarithms, so even if the inputs are both exact, the result is not. Assigning to int always truncates, so if the inexactness is negative, you'll get 24 rather than 25.

The moral of this story is to use integer operations on integers, and be suspicious of <math.h> functions when the actual arguments are to be promoted or truncated. It's unfortunate that GCC doesn't warn unless you add -Wfloat-conversion (it's not in -Wall -Wextra, probably because there are many cases where such conversion is anticipated and wanted).

For integer powers, it's always safer and faster to use multiplication (division if negative) rather than pow() - reserve the latter for where it's needed! Do be aware of the risk of overflow, though.

like image 36
Toby Speight Avatar answered Sep 17 '22 23:09

Toby Speight