San*_*esí 2 floating-point ieee-754 julia
我最近开始使用Julia,我发现了bits
函数,它返回其数字参数的位串表示.例如:
julia> bits(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
但是,在使用此功能时,我惊讶地发现bits
返回非常不同的位串1.0
和2.0
:
julia> bits(1.0)
"0011111111110000000000000000000000000000000000000000000000000000"
julia> bits(2.0)
"0100000000000000000000000000000000000000000000000000000000000000"
Run Code Online (Sandbox Code Playgroud)
我原以为这两个值是相似的......
那些位是什么意思?我含糊地回忆起有关编码指数的比特(来自我的数值分析类),但我真的不记得了,我没有设法在网上找到一个好的描述......
jub*_*0bs 12
要理解为什么ASCIIString
值bits(1.0)
和bits(2.0)
"如此不同"的原因,你需要知道关于IEEE-754(二进制)浮点数的一点(!).每个这样的双精度数字都存储为64位字,分为三个部分:
可以使用以下公式获得标准化数字(例如1.0
和2.0
)的值:
(-1)^sign_bit x 1.significand x 2^(biased_exponent - bias)
Run Code Online (Sandbox Code Playgroud)
(对于双精度浮点数,偏差值为2 ^ 10 - 1 = 1023)
现在,
1.0 = +1.000 ... x 2 ^(1023 - 偏见)
1023对应于基数2中的(0)1111111111,因此对应的位串是
0 01111111111 0000000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)2.0 = +1.000 ... x 2 ^(1024 - 偏差)
1024对应于base 2中的10000000000,因此对应的位串是
0 10000000000 0000000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)3.0 = +1.100 ... x 2 ^(1024 - 偏差)
所以相应的位串是
0 10000000000 1000000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)等等
总之,您可以2.0
通过递增位串中的偏置指数部分来获得位串1.0
,该位是2的幂,减去1.递增这样的数字会导致其二进制表示的所有位发生变化,增加数字9999(以十进制表示)的相同方式会导致所有数字发生变化.