如何在浮点算术和十进制中表示0.1 [英] How To Represent 0.1 In Floating Point Arithmetic And Decimal

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

问题描述

我想要更好地了解浮点运算,并且看到了几个链接,每​​个计算机科学家应该知道的浮点算术。



我还是不了解如何将数字 0.1 0.5 存储在浮点数和小数位。



有人可以解释它是如何布置的记忆?



我知道浮动是两部分(即,一个数字某事的力量)。

解决方案

我一直指着人们朝着 Harald Schmidt的在线转换器,以及维基百科IEEE754-1985文章及其漂亮的图片。



对于这两个具体值,你得到(对于0.1):

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

标志是积极的,这很容易。



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



尾数很粗。它由 1 (隐含基数)加上(对于所有这些位,每个值都 1 /(2 n as n 开始于 1 并增加到右边), {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 。 p>

当乘以乘数时,您将获得 0.100000001490116119384765625 ,这就是为什么他们说您不能代表 0.1 完全符合IEEE754浮点数,并为SO提供了很多机会,让人们回答为什么不0.1 + 0.1 + 0.1 == 0.3?类型问题: - )






0.5示例更容易。它表示为:

  s eeeeeeee mmmmmmmmmmmmmmmmmmmm 
0 01111110 00000000000000000000000
/ pre>

这意味着它是隐含的基础, 1 ,加上没有其他添加剂(所有尾数位都为零)。



标志再次是正面的。指数为 64 + 32 + 16 + 8 + 4 + 2 = 126 - 127 bias = -1 。因此乘数是 2 -1 这是 1/2 0.5



所以最终的值是 1 乘以$ $ c> 0.5 或 0.5 。 Voila!






我有时会发现用十进制来更容易地想到它。



数字1.345相当于

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

或:

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






类似地,十进制 0.8125 的I​​EEE754表示是:

  s eeeeeeee mmmmmmmmmmmmmmmmmmmmm 
0 01111110 10100000000000000000000

隐含基数为1,这相当于二进制值:

  01111110-01111111 
1.101 * 2

或:

  -1 
(1 + 1/2 + 1/8)* 2(该位为0以后无1/4)
/ pre>

,成为:

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

然后成为:

  13 / 8 * 1/2 = 0.8125 


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'.

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).

解决方案

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

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.

The exponent is 64+32+16+8+2+1 = 123 - 127 bias = -4, so the multiplier is 2-4 or 1/16.

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}.

When you add all these up, you get 1.60000002384185791015625.

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 :-)


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

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
0 01111110 00000000000000000000000

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

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.

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.

The number 1.345 is equivalent to

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

or:

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


Similarly, the IEEE754 representation for decimal 0.8125 is:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
0 01111110 10100000000000000000000

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

         01111110-01111111
1.101 * 2

or:

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

which becomes:

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

and then becomes:

13/8 * 1/2 = 0.8125

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

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