定义平方根的条件
define condition for square root
这个宏用于计算sqrt(x)
,
#define SQRT(x) ((x)<=0.0||(x)!=(x)?0.0:sqrt(x))
我不明白第二种情况是什么情况(x)!=(x)
可能是真的?
(x) != (x)
如果 x
是 IEEE 754 Not-a-number (NaN). I.e. this macro tries to coerce the result to 0.0 for arguments values for which square root is not defined. The sqrt(3) Linux man page 则为真 很好地总结了 return 值:
RETURN VALUE
On success, these functions return the square root of x.
If x
is a NaN, a NaN is returned.
If x
is +0 (-0), +0 (-0) is returned.
If x
is positive infinity, positive infinity is returned.
If x
is less than -0, a domain error occurs, and a NaN is returned.
因此,此表达式确保在第一种和第四种情况下 return 将 returned 而不是 NaN,此外,errno
保持不变并且不会发生浮点异常.
(顺便说一句,如果您在宏参数中有副作用,那么使用这个奇怪的宏是不安全的,因此最好将其替换为内联函数或类似函数)
这个宏用于计算sqrt(x)
,
#define SQRT(x) ((x)<=0.0||(x)!=(x)?0.0:sqrt(x))
我不明白第二种情况是什么情况(x)!=(x)
可能是真的?
(x) != (x)
如果 x
是 IEEE 754 Not-a-number (NaN). I.e. this macro tries to coerce the result to 0.0 for arguments values for which square root is not defined. The sqrt(3) Linux man page 则为真 很好地总结了 return 值:
RETURN VALUE
On success, these functions return the square root of x.
If
x
is a NaN, a NaN is returned.If
x
is +0 (-0), +0 (-0) is returned.If
x
is positive infinity, positive infinity is returned.If
x
is less than -0, a domain error occurs, and a NaN is returned.
因此,此表达式确保在第一种和第四种情况下 return 将 returned 而不是 NaN,此外,errno
保持不变并且不会发生浮点异常.
(顺便说一句,如果您在宏参数中有副作用,那么使用这个奇怪的宏是不安全的,因此最好将其替换为内联函数或类似函数)