Search code examples
c++cnumerical-methods

std::pow gives a wrong approximation for fractional exponents


Here is what I mean trying to do

 double x=1.1402
 double pow=1/3;
 std::pow(x,pow) -1;

result is 0 but I expect 0.4465

the equation is (1 + x) ^3= 1.1402, find x.


Solution

  • 1/3 is done as integer arithmetic, so you're assigning 0 to pow. Try pow(x, 1.0/3.0);