Four rounding modes in the IEEE754 standard

I. Introduction

Recently I IEEE754标准am writing a floating-point adder based on it. One of the requirements is IEEE754标准four rounding modes to be satisfied .

When we normalize the order or right, when the operand with a smaller order is shifted to the right, the low bits of the mantissa part will be lost, which will cause errors. Therefore, we need to adopt one of the four rounding modes to round the mantissa according to the demand to reduce the error.

2. Four rounding modes in the IEEE754 standard

1. Round to nearest

That is, rounding under the decimal system . However, the following situations may also occur:

  • The extra number is 1001, which is greater than 0.5, so the lowest digit is incremented by 1.
  • The surplus number is 0111, which is less than 0.5, so the surplus number is directly discarded.
  • The extra digit is 1000, which happens to be a special case equal to 0.5; then at this time, if the lowest bit is 0, the extra digits are discarded, and the lowest digit is 1, then 1 is carried.

Note that the digits described here refer to binary numbers. Because this is the mantissa, when calculating the relationship between these binary and 0.5, that is, when converting to decimal, we multiply the weight of each digit 2^(-i)and then sum it up.

2. Round towards 0: That is, it is rounded toward the zero point of the number axis, so we can just truncate it directly.

3. Round towards positive infinity: For a positive number, if the extra digits are all 0, it will be truncated directly, and if it is not all 0, then the least significant digit will be incremented by 1; for a negative number, no matter how many extra digits are, it will be truncated directly.

4. Round towards negative infinity: For negative numbers, if the extra digits are all 0, it will be truncated directly, if it is not all 0, then the least significant digit will be incremented by 1; if it is positive, no matter how many extra digits are, it will be truncated directly.

Three, for example

It is required to keep 3 decimal places

1. Round to nearest

对于1.001_1001,舍入处理后为1.010(去掉多余的4位,加0.001)
对于1.001_0111,舍入处理后为1.001(去掉多余的4位)
对于-1.001_1000,舍入处理后为-1.010(去掉多余的4位,加0.001,因为此时最低位为0)
对于-1.010_1000,舍入处理后为-1.010(直接去掉多余的4位,因为此时最低位为0

2. Round towards 0

//正数直接截尾
对于1.001_1001,舍入处理后为1.001(直接去掉多余的4位)
对于1.001_0111,舍入处理后为1.001(直接去掉多余的4位)

//负数直接截尾
对于-1.001_1000,舍入处理后为-1.001(直接去掉多余的4位)
对于-1.010_1000,舍入处理后为-1.010(直接去掉多余的4位)

3. Round towards positive infinity

//正数多余位不全为0进位1
对于1.001_1001,舍入处理后为1.010(去掉多余的4位,加0.001)
对于1.001_0111,舍入处理后为1.010(去掉多余的4位,加0.001//正数多余位全为0直接截尾
对于1.001_0000,舍入处理后为1.001(直接去掉多余的4位)

//负数直接截尾
对于-1.001_1010,舍入处理后为-1.001(直接去掉多余的4位)

4. Round towards negative infinity

//正数直接截尾
对于1.001_1001,舍入处理后为1.001(直接去掉多余的4位)
对于1.001_0111,舍入处理后为1.001(直接去掉多余的4位)

//负数多余位全为0直接截尾
对于-1.001_0000,舍入处理后为-1.001(直接去掉多余的4位)

//负数多余位不全为0进位1
对于-1.001_1010,舍入处理后为-1.010(去掉多余的4位,加0.001

I think there may be errors in my understanding, friends who know can communicate! ! !

Reference link: https://www.cnblogs.com/jva-index/p/13897423.html

Guess you like

Origin blog.csdn.net/qq_39507748/article/details/110219526
Recommended