Check if the complex number has NaN

I am trying to check whether a number std::complexthat is the result of a Fourier transform (using http://fftw.org/ ) contains NaNboth the real and the imaginary part.

I am using Borland C ++, so I do not have access to std::isnan. I tried to check if the number NaNis by comparing it with myself:

(n.imag() != n.imag())

However, as soon as I call n.imag()or std::imag(n), I get an "invalid floating point operation".

Is there any way to verify the correctness std::complex; if it contains a NaN?

+5
source share
4 answers

g++:

#include<iostream>
#include<cmath>
#include<complex>

int main(){

  double x=sqrt(-1.);
  std::complex<double> c(sqrt(-1.), 2.);

  std::cout<<x<<"\n";
  std::cout<<c<<"\n";

  std::cout<< ( (c!=c) ? "yup" : "nope" )<<"\n";
}
+4

float.h

int _isnan(double d);

nonzero (TRUE), NaN; 0 (FALSE).

int _fpclass(double __d);

, . FLOAT.H(NaN, INF ..)

+1

Is there fpclassify()in math.h? He must return FP_NANfor NaN. Or better yet use it isnan(). If there are no such functions / macros, you can look at the binary representation of your floats or doubles and check manually for NaN. See IEEE-754 Single-Point and Point-to-Point Formats for details.

0
source

All Articles