浮点除零的行为 [英] The behaviour of floating point division by zero

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

问题描述

考虑

  #include< iostream> 
int main()
{
double a = 1.0 / 0;
double b = -1.0 / 0;
double c = 0.0 / 0;
std :: cout<<一个< b< C; //停止编译器优化代码。



$ b我一直认为 a 将是+ Inf, b 将是-Inf,并且 c 将是NaN。但是我也听到一些传言,严格地说,零点浮点除法的行为是未定义的,因此上面的代码不能被认为是可移植的C ++。 (这理论上消除了我的百万行加上代码堆栈的完整性哦。)



谁是正确的?

注意我很满意定义的 ,但是我在这里讨论猫食,恶魔打喷嚏 undefined behavior



如果硬件/编译器正确执行IEEE 754标准,除非提供预期的INF,-INF和NaN,否则......取决于它。

未定义的方式,编译器实现的决定,并且有很多变量,比如硬件体系结构,代码生成效率,编译器开发者的懒惰等等。 b
来源:

C ++标准状态表示除以0.0是 undefined


C ++ Standard 5.6如果/或%的第二个操作数为零,则行为是不确定的



C ++标准18.3.2.4 b
$ b

... static constexpr bool is_iec559;

... 56。当且仅当该类型符合IEC 559标准时才为真。


IEEE754的C ++检测:

标准库包含一个检测IEEE754是否被支持的模板:

lockquote
static constexpr bool is_iec559; / p>



  #include< numeric> 
bool isFloatIeee754 = std :: numeric_limits< float> :: is_iec559();

当然,这种机制假定编译器知道目标执行机器(理论上可能不是如果IEEE754不被支持,该怎么办?



,通常一个除0触发硬件异常,并使应用程序终止。

Consider

#include <iostream>
int main()
{
    double a = 1.0 / 0;
    double b = -1.0 / 0;
    double c = 0.0 / 0;
    std::cout << a << b << c; // to stop compilers from optimising out the code.    
}

I have always thought that a will be +Inf, b will be -Inf, and c will be NaN. But I also hear rumours that strictly speaking the behaviour of floating point division by zero is undefined and therefore the above code cannot considered to be portable C++. (That theoretically obliterates the integrity of my million line plus code stack. Oops.)

Who's correct?

Note I'm happy with implementation defined, but I'm talking about cat-eating, demon-sneezing undefined behaviour here.

解决方案

C++ standard does not force the IEEE 754 standard, because that depends mostly on hardware architecture.

If the hardware/compiler implement correctly the IEEE 754 standard, the division will provide the expected INF, -INF and NaN, otherwise... it depends.

Undefined means, the compiler implementation decides, and there are many variables to that like the hardware architecture, code generation efficiency, compiler developer laziness, etc..

Source:

The C++ standard state that a division by 0.0 is undefined

C++ Standard 5.6.4

... If the second operand of / or % is zero the behavior is undefined

C++ Standard 18.3.2.4

...static constexpr bool is_iec559;

...56. True if and only if the type adheres to IEC 559 standard.217

...57. Meaningful for all floating point types.

C++ detection of IEEE754:

The standard library includes a template to detect if IEEE754 is supported or not:

static constexpr bool is_iec559;

#include <numeric>
bool isFloatIeee754 = std::numeric_limits<float>::is_iec559();

Of course, this mechanism assumes the compiler is aware of the target execution machine (which could theoretically not be the case).

What if IEEE754 is not supported?

It depends, usually a division by 0 trigger a hardware exception and make the application terminate.

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

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