如何重新present 0.1在浮点运算及十进制 [英] How To Represent 0.1 In Floating Point Arithmetic And Decimal
问题描述
我想了解浮点运算更好,见过几个环节,以什么每台计算机科学家应该知道关于浮点运算
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.1或0.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
我知道浮动是TWP部分(即一个数的功率)
I know about the float being twp parts (ie a number to the power of)
推荐答案
我一直指出人民对哈拉尔德·施密特在线转换,与它的漂亮的照片的维基百科的文章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
符号为正,这是pretty容易。
The sign is positive, that's pretty easy.
该指数是 64 + 32 + 16 + 8 + 2 + 1 = 123 - 127偏压= -4
,所以乘数是 2 -4
或 1/16
。
The exponent is 64+32+16+8+2+1 = 123 - 127 bias = -4
, so the multiplier is 2-4
or 1/16
.
尾数是矮胖。它由 1
的(隐式底座)加(与每个都是值得所有这些位 1 /(2 N )
为 N
开始于 1
键,向右增加), {1/2,1/16,1/32,1/256,1/512,1/4096,8192分之1,1/65536,一十三万一千零七十二分之一,一百〇四万八千五百七十六分之一,二百〇九万七千一百五十二分之一,八百三十八万八千六百〇八分之一}
。
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
,这就是为什么他们说你不能再present 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的例子是基本上容易。这是再presented为:
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 1
是 1/2
或 0.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相当于
1 + 3/10 + 4/100 + 5/1000
或
-1 -2 -3
1 + 3*10 + 4*10 + 5*10
同样,IEEE754重新presentation十进制 0.8125
是:
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
这篇关于如何重新present 0.1在浮点运算及十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!