简体   繁体   English

如何判断数字是否可以精确表示为32位IEEE浮点数?

[英]How to tell if a number is exactly representable as a 32-bit IEEE float?

This is a task from my textbook 这是我教科书上的任务

Given a hypothetical computer with a word length of 32 bits divided the following way: 给定一个假设的计算机,其字长为32位,则按以下方式划分:
1 bit for the sign bit 1位为符号位
9 bit biased exponent 9位有偏指数
23 bit mantissa part 23位尾数部分

Is 2^-1 + 2^-29 a machine number on this computer? 2 ^ -1 + 2 ^ -29是此计算机上的机器编号吗? How might i go about determining this? 我该如何确定呢?

abs((-1) - (-29)) is larger than the mantissa width so no, that would require too many significant figures (mantissa bits). abs((-1) - (-29))大于尾数宽度,因此没有,这将需要过多的有效数字(尾数位)。

If you wrote them out in a non-exponential binary-point notation, like 0.10000...0001 , the gap between the 2 ones would be larger than the mantissa. 如果以非指数二进制点表示法将它们写出,例如0.10000...0001 ,则两个之间的距离将大于尾数。 (It's like a decimal point, but it's binary place values so we call it a binary point.) (这就像一个小数点,但它是二进制位值,因此我们将其称为二进制点。)

ie 2 -29 is less than 1 ulp for 2 -1 . 即2 -29对于2 -1小于1 ulp。 (ULP = unit in the last place = low bit of the mantissa.) (ULP =末尾的单位=尾数的低位。)

Binary floats can represent a fixed number of (binary) "significant figures" equal to the mantissa width, regardless of the exponent. 二进制浮点数可以表示等于尾数宽度的固定数量(二进制)“有效数字”,而不管指数如何。 ( https://en.wikipedia.org/wiki/Significant_figures ) https://en.wikipedia.org/wiki/Significant_figures


Note that the stored width of the mantissa is only 23 bits, but there's an implicit leading 1 (for normalized numbers). 请注意,尾数的存储宽度仅为23位,但是有一个隐式的前导1(用于归一化的数字)。 https://en.wikipedia.org/wiki/Single-precision_floating-point_format https://en.wikipedia.org/wiki/Single-precision_floating-point_format

So 1 + 2 -23 is exactly representable. 因此1 + 2 -23完全可以表示。 ie 1 ulp relative to 1.0 = 2^-23 . 即相对于1.0 = 2^-23 1 ulp。 (Related: https://en.wikipedia.org/wiki/Machine_epsilon like C FLT_EPSILON is half a ulp of 1.0, so 2^-24 for binary32 floats. It's the largest value you can add to 1.0 and still have the result round to 1.0 , ie the largest rounding error you can get when adding to 1.0 ) (相关: https ://en.wikipedia.org/wiki/Machine_epsilon像C FLT_EPSILON一样是1.0的一半,因此,binary32浮点数为2 ^ -24。这是您可以添加到1.0最大值,并且仍然可以得到结果到1.0 ,即添加到1.0时可以得到的最大舍入误差)

Note that many modern documents use "significand" instead of "mantissa" because it's more mathematically correct. 请注意,许多现代文档使用“有效位数”代替“尾数”,因为它在数学上更正确。

Is 2^-1 + 2^-29 a machine number on this computer? 2 ^ -1 + 2 ^ -29是此计算机上的机器编号吗?
How might i go about determining this? 我该如何确定呢?

Assuming the hypothetical format is using base 2 (that was not specified), then 2 -1 and 2 -29 are both exactly representable being small powers of 2. Let us call them a_1 and a_29 and assume a_1 > a_29 > 0 假设假设的格式使用2的底数(未指定),则2 -1和2 -29都是可表示的,都是2的次幂。让我们将它们称为a_1a_29并假设a_1 > a_29 > 0

"1 bit for the sign bit, 9 bit biased exponent, 23 bit mantissa part" is akin to binary32 . “ 1位为符号位,9位偏置指数,23位尾数部分”类似于binary32 Should it follow all those rules (including sub-normals), then any subtraction between different values will not result in 0. This is important for the next step. 如果遵循所有这些规则(包括法线),则不同值之间的任何减法都不会得出0。这对于下一步很重要。

Set sum/difference acc = a_1 +/- a_29 and acc_1 = acc - a_1 . 设置总和/差acc = a_1 +/- a_29acc_1 = acc - a_1 If acc_1 == +/-a_29 , then acc was exactly represented, else acc was not exactly the sum/difference a_1 +/- a_29 , just a rounded result. 如果acc_1 == +/-a_29 ,则acc精确表示,否则acc并不完全是和/差a_1 +/- a_29 ,而是四舍五入的结果。

In the case of 2 -1 and 2 -29 , the difference is not exactly representable as the "23 bit mantissa part" is too narrow to encode exactly a relative difference of 2 -1 - 29 . 在2 -1和2 -29的情况下,不同的是不为“23比特尾数部分”精确表示太窄编码恰好2 -1的相对差- 29。 We would need about "27-28 bit mantissa part" to do so. 为此,我们需要大约“ 27-28位尾数部分”。

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

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