我正在用C做这个大项目,这是我作业的一部分.我的问题是我的程序输出x = -0.00而不是x = 0.00.我试过比较像if(x == – 0.00)x = fabs(x),但我读过它不会像双打那样工作.所以我的问题是有没有其他方法来检查double是否等于负零?
解决方法
您可以使用math.h中的标准宏符号(arg).如果arg为负,则返回非零值,否则返回0.
从man page:
signbit()
is a generic macro which can work on all real floating-
point types. It returns a nonzero value if the value of x has its
sign bit set.This is not the same as x < 0.0,because IEEE 754 floating point
allows zero to be signed. The comparison -0.0 < 0.0 is false,but
signbit(-0.0) will return a nonzero value.NaNs and infinities have a sign bit.
此外,从cppreference.com开始:
This macro detects the sign bit of zeroes,infinities,and NaNs. Along
withcopysign
,this macro is one of the only two portable ways to examine the sign of a NaN.