负零的平方根 [英] Square root of negative zero

查看:209
本文介绍了负零的平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在g ++版本4.8.0(32位mingw)下,-0.0(二进制表示0x8000000000000000)的平方根是NAN。但是我可以发誓早期的版本(我不知道是哪一个,但是最后一次运行我的完整测试套件)只返回了0.0,这对我来说似乎更加明智。



这是正确的吗?有没有在C ++标准的变化,或者这是一个g + +的变化?

解决方案

这是一个非标准的行为MinGW环境(我也间接地通过一个客户来观察我的工作软件,这个客户意外地失败了)。 code> sqrt(-0。),即 -0。,在编译时正确计算值的优化级别

我最终定义了自己的fixed 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试图(在这个特定的情况下失败)。

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

I ended up defining my own "fixed" sqrt() as:

double mysqrt(double x) {
  if (x == 0.) return x; else return sqrt(x);
}


(*) IEEE 754 defines 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).

这篇关于负零的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆