gpt4 book ai didi

floating-point - 如何用二进制表示 float 。 IEEE

转载 作者:行者123 更新时间:2023-12-04 02:14:22 24 4
gpt4 key购买 nike

类似于十进制二进制数也可以表示 float 。现在我读到它可以有那种花车

0.5:0.1 , 0.25:0.01 , 0.125:0.001 ...等等。但是,例如,0.1(十进制)如何用二进制表示?

此外,给定一个十进制 float ,如何将其转换为等效的十进制数,(假设它不是那么简单)。

编辑:所以我知道更好的问题是;如何将十进制 float 转换为二进制?现在我知道我们将小数部分相乘,直到它变为零。现在很有可能两个浮点可以具有相同的表示,对吧?

最佳答案

给定多少位?

0.1b:

0.00011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011...

如您所见,这是一个近似值。

Binary                                          Decimal
0.1 == 1/2^1 == 1/2 == 0.5
0.01 == 1/2^2 == 1/4 == 0.25
0.11 == 1/2^1 + 1/2^2 == 1/2 + 1/4 == 3/4 == 0.75

radix point 之后的每一位表示 1/2^(position_after_bit_string)。

postion:   |1|2|3|4|5|6|7|
0.|0|0|0|0|0|0|1|

所以 0.0000001 = 1/2^7 = 0.0078125

伪代码:

decimal_value = 0 
for i, bit in enumerate(binary_string):
if bit == 1
decimal_value += 1/2**i

了解更多信息 Why can't decimal numbers be represented exactly in binary?

关于floating-point - 如何用二进制表示 float 。 IEEE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13663026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com