检查复数是否有 NaN
我正在尝试检查 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这适用于 g++ :
This works on g++ :
来自
float.h
标头如果传入的值为 NaN,则返回一个
非零
值 (TRUE
);否则返回0
(FALSE
)。返回一个整数值,指示其参数的浮点类。可能的值在 FLOAT.H 中定义(NaN、INF 等)
From the
float.h
headerReturns a
nonzero
value (TRUE
) if the value passed in is a NaN; otherwise it returns0
(FALSE
).Returns an integer value that indicates the floating-point class of its argument. The possible values are defined in FLOAT.H (NaN, INF, etc.)
math.h
中有fpclassify()
吗?对于 NaN,它应该返回FP_NAN
。或者更好的是使用isnan()
。如果没有这样的函数/宏,您可以查看浮点数或双精度数的二进制表示形式,并手动检查 NaN。有关详细信息,请参阅 IEEE-754 单精度和双精度格式。Is there
fpclassify()
inmath.h
? It should returnFP_NAN
for NaNs. Or better yet useisnan()
. 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.我发现 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]