Question

I'm trying to check if a std::complex number that is a result of a fourier transform (using http://fftw.org/) contains a NaN in either the real or imag part.

I'm using Borland C++, so I don't have access to std::isnan. I have tried to check if the number is NaN by comparing it to itself:

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

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

Is there any way to validate if a std::complex is good; if it contains a NaN?

Was it helpful?

Solution 4

I found out that Borland has its own math library. So if you want to avoid floating point errors, use IsNan from Borlands Math.

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Math_IsNan@Double.html

OTHER TIPS

This works on 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";
}

From the float.h header

int _isnan(double d);

Returns a nonzero value (TRUE) if the value passed in is a NaN; otherwise it returns 0 (FALSE).

int _fpclass(double __d);

Returns an integer value that indicates the floating-point class of its argument. The possible values are defined in FLOAT.H (NaN, INF, etc.)

Is there fpclassify() in math.h? It should return FP_NAN for NaNs. Or better yet use isnan(). If there are no such functions/macros, you may look at the binary representation of your floats or doubles and check manually for NaNs. See IEEE-754 single and double precision formats for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top