﻿ 语言不可知 – 为什么浮点数不准确？ - 代码日志

#### 语言不可知 – 为什么浮点数不准确？

32-bit "single precision" float: 9.19999980926513671875
64-bit "double precision" float: 9.199999999999999289457264239899814128875732421875

5179139571476070 * 2 -49

9.2可以简单地是92/10，但是如果n被限制为整数值，则10不能被表示为2n。

def float_to_bin_parts(number, bits=64):
if bits == 32:          # single precision
int_pack      = 'I'
float_pack    = 'f'
exponent_bits = 8
mantissa_bits = 23
exponent_bias = 127
elif bits == 64:        # double precision. all python floats are this
int_pack      = 'Q'
float_pack    = 'd'
exponent_bits = 11
mantissa_bits = 52
exponent_bias = 1023
else:
raise ValueError, 'bits argument must be 32 or 64'
bin_iter = iter(bin(struct.unpack(int_pack, struct.pack(float_pack, number))[0])[2:].rjust(bits, '0'))
return [''.join(islice(bin_iter, x)) for x in (1, exponent_bits, mantissa_bits)]

Python的float是一个64位，双精度数字。在其他语言(如C，C，Java和C#)中，双精度具有单独类型double，通常实现为64位。

>>> float_to_bin_parts(9.2)
['0', '10000000010', '0010011001100110011001100110011001100110011001100110']

>标志
>指数
>尾数(也称为显着性或分数)

6.0221413×1023

1.0010011001100110011001100110011001100110011001100110

0.0010011001100110011001100110011001100110011001100110

>符号(第一个分量)：0表示正，1表示负
>指数(中间分量)：减去2(位数) – 1 – 1以获得真实指数
>尾数(最后一个分量)：除以2(位数)，并加1得到真尾数

1.0010011001100110011001100110011001100110011001100110 x 1011

1.1499999999999999 x 23 (inexact!)

9.1999999999999993

9.2

1.0010011001100110011001100110011001100110011001100110 x 1011

10010011001100110011001100110011001100110011001100110 x 1011-110100

5179139571476070 x 23-52

5179139571476070 x 2-49

5179139571476070 / 249

5179139571476070 / 562949953421312

9.1999999999999993

9.5

>>> float_to_bin_parts(9.5)
['0', '10000000010', '0011000000000000000000000000000000000000000000000000']

1.0011 x 1011

10011 x 1011-100

10011 x 10-1

19 x 2-1

19 / 21

19 / 2

9.5