如何用浮点数和小数表示 0.1 [英] How To Represent 0.1 In Floating Point Arithmetic And Decimal

查看:47
本文介绍了如何用浮点数和小数表示 0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力更好地理解浮点运算,并看到了一些指向每个计算机科学家应该知道的关于浮点运算的知识"的链接.

I am trying to understand floating point arithmetic better and have seen a few links to 'What Every Computer Scientist Should Know About Floating Point Arithmetic'.

我仍然不明白像 0.10.5 这样的数字是如何以浮点数和小数形式存储的.

I still don't understand how a number like 0.1 or 0.5 is stored in floats and as decimals.

有人可以解释一下它是如何布局的吗?

Can someone please explain how it is laid out is memory?

我知道浮点数是两部分(即,某物的幂的数字).

I know about the float being two parts (i.e., a number to the power of something).

推荐答案

我一直把人们指向 Harald Schmidt 的在线转换器,以及 维基百科 IEEE754-1985 文章 精美的图片.

I've always pointed people towards Harald Schmidt's online converter, along with the Wikipedia IEEE754-1985 article with its nice pictures.

对于这两个特定值,您会得到(对于 0.1):

For those two specific values, you get (for 0.1):

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm    1/n
0 01111011 10011001100110011001101
           |  ||  ||  ||  ||  || +- 8388608
           |  ||  ||  ||  ||  |+--- 2097152
           |  ||  ||  ||  ||  +---- 1048576
           |  ||  ||  ||  |+-------  131072
           |  ||  ||  ||  +--------   65536
           |  ||  ||  |+-----------    8192
           |  ||  ||  +------------    4096
           |  ||  |+---------------     512
           |  ||  +----------------     256
           |  |+-------------------      32
           |  +--------------------      16
           +-----------------------       2

信号是积极的,这很容易.

The sign is positive, that's pretty easy.

指数为64+32+16+8+2+1 = 123 - 127 bias = -4,所以乘数为2-41/16.

尾数很粗.它由 1 (隐式基数)加上(对于所有这些位,每个位都值得 1/(2n) 作为 n1 开始并向右增加),{1/2, 1/16, 1/32, 1/256, 1/512, 1/4096,1/8192、1/65536、1/131072、1/1048576、1/2097152、1/8388608}.

The mantissa is chunky. It consists of 1 (the implicit base) plus (for all those bits with each being worth 1/(2n) as n starts at 1 and increases to the right), {1/2, 1/16, 1/32, 1/256, 1/512, 1/4096, 1/8192, 1/65536, 1/131072, 1/1048576, 1/2097152, 1/8388608}.

当你把所有这些加起来时,你会得到 1.60000002384185791015625.

When you add all these up, you get 1.60000002384185791015625.

当你将它乘以乘数时,你会得到 0.100000001490116119384765625,这就是为什么他们说你不能将 0.1 完全表示为 IEEE754 浮点数,并提供了很多机会所以对于回答 为什么 0.1 + 0.1 + 0.1 == 0.3?" 类型问题的人来说:-)

When you multiply that by the multiplier, you get 0.100000001490116119384765625, which is why they say you cannot represent 0.1 exactly as an IEEE754 float, and provides so much opportunity on SO for people answering "why doesn't 0.1 + 0.1 + 0.1 == 0.3?"-type questions :-)

0.5 的例子要简单得多.它表示为:

The 0.5 example is substantially easier. It's represented as:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
0 01111110 00000000000000000000000

这意味着它是隐式基数,1,加上没有其他添加(所有尾数位都为零).

which means it's the implicit base, 1, plus no other additives (all the mantissa bits are zero).

符号又是正面的.指数为 64+32+16+8+4+2 = 126 - 127 偏差 = -1.因此乘数是 2-11/20.5.

The sign is again positive. The exponent is 64+32+16+8+4+2 = 126 - 127 bias = -1. Hence the multiplier is 2-1 which is 1/2 or 0.5.

所以最终的值是1乘以0.5,或0.5.瞧!

So the final value is 1 multiplied by 0.5, or 0.5. Voila!

我有时发现用小数来理解它更容易.

I've sometimes found it easier to think of it in terms of decimal.

数字 1.345 等价于

The number 1.345 is equivalent to

1 + 3/10   + 4/100 + 5/1000

或:

        -1       -2      -3
1 + 3*10   + 4*10  + 5*10

<小时>

同样,十进制 0.8125 的 IEEE754 表示是:


Similarly, the IEEE754 representation for decimal 0.8125 is:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
0 01111110 10100000000000000000000

隐式基数为 1,相当于二进制:

With the implicit base of 1, that's equivalent to the binary:

         01111110-01111111
1.101 * 2

或:

                     -1
(1   + 1/2 + 1/8) * 2     (no 1/4 since that bit is 0)

变成:

(8/8 + 4/8 + 1/8) * 1/2

然后变成:

13/8 * 1/2 = 0.8125

这篇关于如何用浮点数和小数表示 0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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