简体   繁体   English

舍入IEEE 754浮点数

[英]Rounding IEEE 754 Floating Point Numbers

I understand that there are 4 rounding modes supported by IEEE 754 standards I just want to make sure I understand each really well. 我了解IEEE 754标准支持4种舍入模式,我只是想确保自己对每种方法都非常了解。 Are the following examples correct? 以下示例正确吗?

• Rounding towards positive infinity: Does that mean we add one to the LSB no matter what it is (0 or 1)? •向正无穷大舍入:这是否意味着无论LSB是多少(0或1),我们都将LSB加1?

Say we have 1.1010100000000...00 rounding towards positive infinity means we do +1 so we get 1.1010100000000...01 & similarly when we have 1.10101000000..01 and we need to round towards positive infinity we do +1 and we get 1.1010100000...10? 假设我们有1.1010100000000 ... 00四舍五入为正无穷大意味着我们做+1所以我们得到1.1010100000000 ... 01&类似地当我们有1.10101000000..01时我们需要四舍五入为正无穷大我们做+1我们得到1.1010100000 ... 10?

• Rounding towards negative infinity: We add 0 to LSB meaning we do nothing (meaning that it is same as truncating mode) or do we change the LSB to 0 (if 1 we change it to 0 and if 0 we keep it 0)? •向负无穷大舍入:我们向LSB加0意味着什么都不做(意味着它与截断模式相同),还是将LSB更改为0(如果1更改为0,0保留为0)?

• Truncating Mode: Just chop off the GRS bits •截断模式:仅切掉GRS位

• Round towards nearest even : I learned that one from How to perform round to even with floating point numbers and its pretty clear now. •向最接近的偶数取 :我从“ 如何对浮点数取整到偶数”中学到了一个,现在已经很清楚了。

Your question indicates that you are thinking in terms of a practical implementation, but it is easier to think of rounding strategies in simpler, more abstract terms, and then the implementation falls into place naturally. 您的问题表明您在考虑一种实际的实现方式,但是用更简单,更抽象的术语来考虑舍入策略会更容易,然后实现就自然而然地就位了。

You mostly have it right, except for a couple of details. 除了一些细节外,您基本上都是对的。

  • rounding towards +inf means that if the result is not exact , the value closest to +inf out of the two candidates should be chosen. 四舍五入到+ inf表示如果结果不精确 ,则应选择两个候选中最接近+ inf的值。 First, in sign-magnitude, this latter possibility only mean adding one to the significand as computed if the result is positive . 首先,就符号幅度而言,后一种可能性仅意味着如果结果为正,则将其加到有效位数上。 If the result is negative, then “chopping of the GRS bits”, the thing you suggest to do for truncating mode, is correct. 如果结果为负,则建议对截断模式执行的“斩波GRS位”是正确的。 (Thinking about it for two seconds should convince you that it is normal that rounding towards +inf and truncating coincide on negative results.) Second, if the result is exact (which means the GRS bits are all zero in the implementation you are thinking of), the computed bits should be returned as-is, even if the result is positive. (考虑两秒钟,您将确信,对+ inf舍入并在否定结果处截断是正常的。)其次,如果结果正确(这意味着您正在考虑的实现中GRS位全为零) ),即使结果为正,也应按原样返回计算的位。

  • similarly, rounding towards -inf coincides with truncating for the positive results, and corresponds to adding one to the significand of negative results that are not exact. 类似地,向-inf舍入与截断阳性结果相吻合,并且对应于将不精确的阴性结果的含义加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM