Function Evaluating to nan

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


y = (nan,0)


Which again give the same result.

Thank you.
Last edited on
but it doesn't appear to multiply the complex number by the zero factor.

Maybe I'm misunderstanding, but it sounds like you're assuming that <cmath> function pow can return a complex number given real input. It can't.
http://en.cppreference.com/w/cpp/numeric/math/pow

Basic rules to know:
0 * NaN = NaN
0 * Inf = Inf

This is guaranteed on any system that conforms to IEEE-754.


Solution:
Convert it to std::complex before the function call, not after, so that it uses the correct pow overload.
https://en.cppreference.com/w/cpp/numeric/complex/pow

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cmath>
#include <complex>

int main(){
    
    std::complex<double> x = -2.56583e-07;
    std::complex<double> a = 3.322;

    std::cout << "y = " << pow(x, a) << std::endl;
}

y = (-6.76316e-23,-1.08065e-22)
Last edited on
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?
I don't know what you mean.

You're saying you want it to implicitly give the real part of a complex number?
This is not possible, you must do pow(x, a).real() then.

Also, note that only one of the parameters to pow needs to be std::complex. You could keep either the base or the exponent as a double, but not both.
Last edited on
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.

Thanks again!
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.
Last edited on
Dear Ganaldo,

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.
Last edited on
Topic archived. No new replies allowed.