IEEE 754,除以零 [英] IEEE 754, division by zero

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

问题描述

我知道在标准IEEE 754中允许零除.我想知道它是如何用二进制表示的.

I know in standard IEEE 754 division by zero is allowed. I want to know how it's represented in binary.

例如,十进制的0.25是 0 01111101 00000000000000000000000 以二进制形式.相对于5.0/0.0或0.0/0.0,它们具有二进制表示形式吗?它们是相同的吗? 谢谢.

For example, 0.25 in decimal is 0 01111101 00000000000000000000000 in binary. What about 5.0/0.0 or 0.0/0.0 do they have represenation in binary, and are they same? Thanks.

推荐答案

当您将有限数除以零时,将得到一个无穷大,并带有您要除法的数字的符号.所以5.0/0.0是+ inf,但是0.0/0.0返回的是QNaN不定式.

When you divide a finite number by zero you'll get an infinity with the sign of the number you tried to divide. So 5.0/0.0 is +inf but 0.0/0.0 returns something called a QNaN indefinite.

假设我们将负一除以零.因为这会导致预先计算的异常,所以我认为了解发生情况的关键在于英特尔在4.9.1.2节中使用的响应"语言.

Let’s say we are dividing negative one by zero. Because this results in a pre-computed exception I think the key to understanding what happens is in the "response" verbiage Intel uses in section 4.9.1.2

除零异常的屏蔽响应是设置ZE标志,并返回一个以操作数的符号的异或运算的无穷大符号.
The masked response for the divide-by-zero exception is to set the ZE flag and return an infinity signed with the exclusive OR of the sign of the operands.

我希望我没看错.由于零掩码位(位于x87 FPU的控制字中)为1,因此一旦fpu在用于除法的操作数中检测到零,则预计算的异常标志将置1.现在处理器知道要执行以下操作:

I hope I’m reading this right. Since the Zero mask bit (found in the control word of the x87 FPU) is a 1, the pre-computed exception flag becomes set once the fpu detects the zero in the operand used for division. Now the processor knows to do something like this:


    1 sign of operand 1, our -1.0
xor 0 sign of operand 2, the zero 
----------
    1 response

现在有了响应位,我知道我是正无穷大还是负无穷大

Now with that response bit I know whether I have a positive or negative infinity


-inf 1 11111111 00000000000000000000000
-----+-+------+-+---------------------+
     | |      | |                     |
     | +------+ +---------------------+
     |    |               |
     |    v               v
     | exponent        fraction
     |
     v
     sign

如果我改用正1.0并除以零:

If I had a positive 1.0 instead and divided by zero:


    0 sign of operand 1
xor 0 sign of operand 2
-----------
    0 

现在我有 inf 0 11111111 00000000000000000000000

只要分子为正,并且除以零,您将获得相同的正无穷大. 这是我想像的当我运行这样的事情时发生的事情:

As long as the numerator is positive and you're dividing by zero you'll get the same positive infinity. This is what I imagine happening when I run something like this:

int main() {
    SetExceptionMask(exAllArithmeticExceptions);    
    float a = -1;
    float b = a / 0.0f;
    printf("%f\n", b);
}

结果是-inf,看起来像这样1 11111111 00000000000000000000000

The result is -inf which looks like this 1 11111111 00000000000000000000000

QNaN(安静而不是数字")对于调试特别有用,它通过几种不同的方式生成,但是0.0/0.0将返回如下所示的内容:

QNaNs ("quiet not a number") are especially helpful for debugging and are generated through a few different ways but 0.0/0.0 will return something that looks like this:


qnan 0 11111111 10000000000000000000000
-----+-+------+-+---------------------+
                |                     |
                +---------------------+
                         |
                         v
                      fraction

现在,软件可以出于任何目的操纵QNaN分数中的位,通常这似乎是出于诊断目的. 要了解更多信息,我建议观看部分31( https://youtu.be/SsDoUirLkbY )和33(<此英特尔手册阅读的href ="https://youtu.be/3ZxXSUPSFaQ" rel ="nofollow noreferrer"> https://youtu.be/3ZxXSUPSFaQ ).

Now software can manipulate the bits in the fraction of a QNaN for any purpose, usually this seems done for diagnostic purposes. To learn more I recommend watching parts 31(https://youtu.be/SsDoUirLkbY), and 33(https://youtu.be/3ZxXSUPSFaQ) of this Intel Manual reading.

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

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