Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pow() function changes behaviour when put inside a function

Tags:

c++

math

gnu

I have been programming in C++ for a while now. I have seen previously that power function gives wrong answer for bigger powers due to precision issues but today while solving coding problems I saw that under the same type of parameters, pow() function gave different values when put inside a function vs when evaluated directly.

 #include <iostream>
 #include <math.h>
 using namespace std;

 long long n,d;

 long long power(long long x)
 {
    return pow(100,x);
 }

 long long powersecond(long long x)
 {
    return pow(100,(int)x);
 }

 int main()
 {
    n = 68; d = 2;
    cout << n*power(d) <<endl;        // outputs 679932
    cout << n*pow(100,d) <<endl;      // outputs 680000
    cout << n*powersecond(d) <<endl;  // outputs 679932
    cout << n*pow(100,(int)d) <<endl; // outputs 680000
    return 0;
 }

Notice that the answer doesn't change even after converting x to integer in powersecond() function.The answer is still 679932 even if d is int instead of long long int. The compiler I used is gnu gcc compiler in VS Code.

like image 531
Sudheer Tripathi Avatar asked May 21 '20 19:05

Sudheer Tripathi


People also ask

What does POW function return in C?

Return Values for pow() Function in C 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.

How does POW function work?

Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function. pow() is function to get the power of a number, but we have to use #include<math.

Why POW function is not working in C?

On some online compilers, the following error may occur. 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.


1 Answers

The problem is that the output of pow is a floating point double. In your custom function you convert that output to long long, which will truncate if the value returned by pow is slightly low instead of slightly high. See Is floating point math broken?. When you call pow directly the value is kept as a double even after the multiplication, and output rounding gives you a more accurate result.

You expect the value returned by pow(100,2) to be 10000, but instead it might be 9999.99999999999 because of the way floating point works. When converted to integer, that becomes 9999; multiplied by 68, you have 679932.

On the other hand, 9999.99999999999 multiplied by 68 becomes 679999.999999999. That's close enough to 680000 that the output function << will round it for you. You can get a more exact figure if you apply output formatting.

like image 190
Mark Ransom Avatar answered Dec 03 '22 05:12

Mark Ransom