为什么IEEE754单精度浮点数只有7位精度? [英] Why IEEE754 single-precision float has only 7 digit precision?

查看:72
本文介绍了为什么IEEE754单精度浮点数只有7位精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么单精度浮点数有 7 位精度(或双精度 15-16 位精度)?

谁能解释一下我们是如何根据为 float(Sign(32) Exponent(30-23), Fraction (22-0)) 分配的 32 位得出的?

解决方案

有效数的 23 个小数位 (22-0) 出现在内存格式中,但总精度实际上是 24 位,因为我们假设有一个前导 1.这相当于 log10(2^24) ≈ 7.225 十进制数字.

双精度浮点数有 52 位小数,加上前导 1 是 53.因此双精度可以容纳 log10(2^53) ≈ 15.955 个十进制数字,而不是 16.

注意:前导 1 不是符号位.它实际上是 (-1)^sign * 1.ffffffff * 2^(eeee-constant) 但我们不需要在分数中存储前导 1.符号位仍然必须存储

<小时>

有些数字不能表示为 2 的幂之和,例如 1/9:

<预><代码>>>>>双 d = 0.1111111111111111;>>>>System.out.println(d + " " + d*10);0.1111111111111111.1111111111111098

如果财务程序在没有自我更正的情况下一遍又一遍地进行计算,最终会出现差异.

<预><代码>>>>>双 d = 0.1111111111111111;>>>>双和 = 0;>>>>for(int i=0; i<1000000000; i++) {sum+=d;}>>>>System.out.println(sum);111111108.91914201

经过 10 亿次求和后,我们损失了超过 2 美元.

Why does a single-precision floating point number have 7 digit precision (or double 15-16 digits precision)?

Can anyone please explain how we arrive on that based on the 32 bits assigned for float(Sign(32) Exponent(30-23), Fraction (22-0))?

解决方案

23 fraction bits (22-0) of the significand appear in the memory format but the total precision is actually 24 bits since we assume there is a leading 1. This is equivalent to log10(2^24) ≈ 7.225 decimal digits.

Double-precision float has 52 bits in fraction, plus the leading 1 is 53. Therefore a double can hold log10(2^53) ≈ 15.955 decimal digits, not quite 16.

Note: The leading 1 is not a sign bit. It is actually (-1)^sign * 1.ffffffff * 2^(eeee-constant) but we need not store the leading 1 in the fraction. The sign bit must still be stored


There are some numbers that cannot be represented as a sum of powers of 2, such as 1/9:

>>>> double d = 0.111111111111111;
>>>> System.out.println(d + "
" + d*10);
0.111111111111111
1.1111111111111098

If a financial program were to do this calculation over and over without self-correcting, there would eventually be discrepancies.

>>>> double d = 0.111111111111111;
>>>> double sum = 0;
>>>> for(int i=0; i<1000000000; i++) {sum+=d;}
>>>> System.out.println(sum);
111111108.91914201

After 1 billion summations, we are missing over $2.

这篇关于为什么IEEE754单精度浮点数只有7位精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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