in C++, when I try pow(-0.857487, 1.5)
it returns nan. I found out this is due to an overflow.
Is there any alternative to math.pow() or any other way to calculate this value?
The problem at hand is not any kind of overflow, but that the result of pow(-0.857487, 1.5) = -0.857487 ^ 3/2 = sqrt(-0.857487)^3 = -0.794038... i
(simplified demonstration math) is not a real number.
In general, pow(a,b)
does not exist in the real numbers for negative a
and non-integer b
. For positive a
however, pow(a,b)
always exists and is real, so if you work with real numbers only, you probably want something like pow(abs(a),b)
.
If complex arithmetic is actually what you want, you can use std::complex
:
#include <iostream>
#include <complex>
int main () {
std::complex<double> d = -0.857487;
std::cout << std::pow(d, 1.5);
}
Output: (-1.45862e-16,-0.794038)
. See it live.