负零的平方根 [英] Square root of negative zero
问题描述
这是正确的吗?有没有在C ++标准的变化,或者这是一个g + +的变化?
这是一个非标准的行为MinGW环境(我也间接地通过一个客户来观察我的工作软件,这个客户意外地失败了)。 code> sqrt(-0。),即 -0。
,在编译时正确计算值的优化级别
我最终定义了自己的fixed Under g++ version 4.8.0 (32-bit mingw), the square root of -0.0 (binary representation 0x8000000000000000) is NAN. But I could have sworn that earlier versions (I'm not sure which, but the last time I ran my full test suite) returned simply 0.0, which seems more sensible to me. Is this right? Has something in the C++ standard changed, or is this a g++ change? This is a non-standard* behavior of the MinGW environment (that I have also observed, indirectly, through a customer for whom the software I work on was failing unexpectedly). You may have seen the correct result for I ended up defining my own "fixed"
(*) IEEE 754 defines 这篇关于负零的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! sqrt()
as:
$ p $ double mysqrt(double x){
if x == 0.)return x;否则返回sqrt(x);
$ b $(b)(*)IEEE 754将 sqrt(-0。)
定义为 -0。
。 C或C ++编译器不必为浮点运算实现IEEE 754,但是GCC试图(在这个特定的情况下失败)。sqrt(-0.)
, i.e. -0.
, with a higher optimization level where the value was computed correctly at compile-time, or with a previous version of the run-time in which the bug was not present.sqrt()
as:double mysqrt(double x) {
if (x == 0.) return x; else return sqrt(x);
}
sqrt(-0.)
as -0.
. A C or C++ compiler does not have to implement IEEE 754 for floating-point arithmetic, but GCC tries to (and in this particular instance, fails).