检查复数是否有 NaN

发布于 2025-01-07 23:09:57 字数 410 浏览 5 评论 0原文

我正在尝试检查 std::complex 数字是否是傅里叶变换的结果(使用 http://fftw.org/)在实部或图像部分包含 NaN

我使用的是 Borland C++,因此无法访问 std::isnan。我尝试通过与自身比较来检查数字是否为 NaN:

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

但是,一旦我调用 n.imag() 或 std:: imag(n),我得到一个“浮点无效运算”。

有什么方法可以验证 std::complex 是否良好;如果它包含 NaN

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2025-01-14 23:09:57

这适用于 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";
}

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";
}
心意如水 2025-01-14 23:09:57

来自 float.h 标头

int _isnan(double d);

如果传入的值为 NaN,则返回一个非零 值 (TRUE);否则返回0 (FALSE)。

int _fpclass(double __d);

返回一个整数值,指示其参数的浮点类。可能的值在 FLOAT.H 中定义(NaN、INF 等)

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.)

无言温柔 2025-01-14 23:09:57

math.h 中有 fpclassify() 吗?对于 NaN,它应该返回 FP_NAN。或者更好的是使用 isnan()。如果没有这样的函数/宏,您可以查看浮点数或双精度数的二进制表示形式,并手动检查 NaN。有关详细信息,请参阅 IEEE-754 单精度和双精度格式。

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.

唠甜嗑 2025-01-14 23:09:57

我发现 Borland 有自己的数学库。因此,如果您想避免浮点错误,请使用 Borlands Math 中的 IsNan。

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/[电子邮件受保护]

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/[email protected]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文