I wish to understand why this part of a larger code that I have written for some iterative process evaluates to nan and how to fix it.
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <cmath>
int main(){
double x = -2.56583e-07;
double a = 3.322;
std::cout << "y = " << pow(x,a) * 0.0<< std::endl;
}
y = nan
Backstory:
I have explicitly written the variables here but x takes +ve or -ve values depending on the magnitude of the previous midpoint(in the sense of a Runge-Kutta problem), the '0.0' multiplier is just to specify whether x is +ve(1) or -ve(0), and 'a' take mainly integer values in the range (0, 2), but may take real values such as 3.322 above in some cases.
I would ideally expect it to return 0.0 (which it does on code-blocks, but I work on Xcode) but it doesn't appear to multiply the complex number by the zero factor.
As an example, I have tried testing a complex variable to see the value of the trialling imaginary part
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <cmath>
#include <complex>
int main(){
double x = -2.56583e-07;
double a = 3.322;
std::complex<double> y = pow(x, a) * 0.0;
std::cout << "y = " << y << std::endl;
}
Thanks, Ganado, but I need the function to evaluate to a double. I would think, taking the power of a complex variable will return a complex, I know I could use the real part but that will turn out to be just a part of my solution. Any ideas how to achieve that?
Oh yeah, I got that. Thanks anyway, I have opted to use the real part instead, my thinking was that since it was a complex value, I could multiply it by zero to get just a real part without calling the real type-function on the complex variable.
You're welcome, but sorry, I feel the need to ask: Programming weirdness aside, what would you expect multiplying a complex number by 0 to do? (a + bi) * 0 would distribute to be a * 0 + b * i * 0 = 0, not a.
PS: std::complex::real() is a simple getter, you'd be doing less computation than multiplying by 0, anyway.
But either way it's a micro-optimization.
Of course, I was expecting a zero result (which I referred loosely to above as "real part"). Hence my bewilderment when I got the nan results. I guess it came down to this rule you mentioned before that
Basic rules to know:
0 * NaN = NaN
0 * Inf = Inf
This is guaranteed on any system that conforms to IEEE-754.
Although I would have expected the compiler to make a distinction between an infinite variable and a complex one which was the case in my calculation here and it shouldn't have evaluated to nan.