二进制浮点数表示为十进制数(Binary Floats Represented as Decimal Numbers)

编程入门 行业动态 更新时间:2024-10-25 08:24:28
二进制浮点数表示为十进制数(Binary Floats Represented as Decimal Numbers)

并非所有的小数都可以完全用二进制浮点数表示。

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

有两个原因可能导致实数不能完全表示为浮点数。 最常见的情况是用十进制数字0.1来表示。 尽管它具有有限的十进制表示,但在二进制中它具有无限重复表示。

另一种方式呢? 如果使用足够的数字,是否可以使用十进制数精确表示每个IEEE 754浮点数?

Not all decimal numbers can be represented exactly using binary floats.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

There are two reasons why a real number might not be exactly representable as a floating-point number. The most common situation is illustrated by the decimal number 0.1. Although it has a finite decimal representation, in binary it has an infinite repeating representation.

What about the other way around? Can every single IEEE 754 float be represented exactly using a decimal number, if enough digits are used?

最满意答案

是的,如果使用足够的数字,每个有限的IEEE 754浮点数都可以精确地使用十进制数表示。

精确度的每个附加二进制数字最多需要一个附加的精确十进制数字来表示。

例如:

0.1b -> 0.5 0.01b -> 0.25 0.11b -> 0.75 0.001b -> 0.125

在精确表示点之后,1到2之间的双精度(binary64)数字只需要52个十进制数字:

#include <stdio.h> int main(void) { printf("%.55f\n", 1.1); }

结果:

1.1000000000000000888178419700125232338905334472656250000

在上面的表示结尾显示的四个之后,它全部为零。 1.100000000000000088817841970012523233890533447265625是距离11/10最近的double的确切值。

正如下面的评论所指出的,负指数的每个附加单位也需要一个额外的十进制数字来准确表示。 但是,大幅度的负指数在其十进制表示中具有前导零。 最小的次正常数在点后面有1022 + 52个十进制数字,但这些数字的第一个近似1022 * log 10 (2)将是零。

Yes, every finite IEEE 754 float be represented exactly using a decimal number, if enough digits are used.

Each additional binary digit of precision requires at most one additional decimal digit of precision to represent exactly.

For instance:

0.1b -> 0.5 0.01b -> 0.25 0.11b -> 0.75 0.001b -> 0.125

A double-precision (binary64) number between 1 and 2 requires only 52 decimal digits after the dot to be represented exactly:

#include <stdio.h> int main(void) { printf("%.55f\n", 1.1); }

Result:

1.1000000000000000888178419700125232338905334472656250000

It's all zeroes after the four displayed at the end of the representation above. 1.100000000000000088817841970012523233890533447265625 is the exact value of the double nearest to 11/10.

As pointed out in the comments below, each additional unit of magnitude for a negative exponent also requires one additional decimal digit to represent exactly. But negative exponents of a large magnitude have leading zeroes in their decimal representations. The smallest subnormal number would have 1022 + 52 decimal digits after the dot, but the first nearly 1022*log10(2) of these digits would be zeroes.

更多推荐

本文发布于:2023-08-04 07:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点数   十进制数   Binary   Floats   Decimal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!