为什么浮点数的有效数字是7或6 [英] Why floating-points number's significant numbers is 7 or 6

查看:244
本文介绍了为什么浮点数的有效数字是7或6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在维基百科日志2 24 = 7.22 .

I see this in Wikipedia log 224 = 7.22.

我不知道为什么要计算2 ^ 24,为什么要取log10……我真的很需要您的帮助.

I have no idea why we should calculate 2^24 and why we should take log10......I really really need your help.

推荐答案

为什么浮点数的有效数字是7或6(?)

why floating-points number's significant numbers is 7 or 6 (?)

考虑一些使用 Pigeonhole原理的想法:

  1. binary32 float可以编码大约2 32 完全相同的数字 .即使我们将自己限制在-10 38 ... +10 38 .任何时候,代码的文本值都类似于0.1f,它将被编码为附近的float,该文本值可能不是完全相同的文本值.问题是:我们可以编码几位数并且仍然保持独特的float?
  2. 对于2的幂数范围,通常线性对2 23 (8,388,608)个值进行编码.
  3. 在[1.0 ... 2.0)范围内,线性地对2 23 (8,388,608)个值进行了编码.
  4. 在[2 33 或8,589,934,592 ... 2 34 或17,179,869,184范围内),同样,2 23 (8,388,608)值是线性编码:彼此相距1024.0.在子范围[9,000,000,000和10,000,000,000]中,大约有976,562个不同的值.
  1. binary32 float can encode about 232 different numbers exactly. The numbers one can write in text like 42.0, 1.0, 3.1415623... are infinite, even if we restrict ourselves to a range like -1038 ... +1038. Any time code has a textual value like 0.1f, it is encoded to a nearby float, which may not be the exact same text value. The question is: how many digits can we code and still maintain distinctive float?
  2. For the various powers-of-2 range, 223 (8,388,608) values are normally linearly encoded.
  3. In the range [1.0 ... 2.0), 223 (8,388,608) values are linearly encoded.
  4. In the range [233 or 8,589,934,592 ... 234 or 17,179,869,184), again, 223 (8,388,608) values are linearly encoded: 1024.0 apart from each other. In the sub range [9,000,000,000 and 10,000,000,000), there are about 976,562 different values.

将其放在一起...

  1. 作为文本,范围[1.000_000 ... 2.000_000),使用1个前导数字和6个尾随数字,有1,000,000个不同的值.对于#3,在相同范围内,存在8,388,608个不同的float,从而允许每个文本值映射到不同的float. 在此范围内,我们可以使用7位数字.

  1. As text, the range [1.000_000 ... 2.000_000), using 1 lead digit and 6 trailings ones, there are 1,000,000 different values. Per #3, In the same range, with 8,388,608 different float exist, allowing each textual value to map to a different float. In this range we can use 7 digits.

作为文本,范围为[9,000,000 * 10 3 和10,000,000 * 10 3 ),使用1个前导数字和6个尾随数字,有1,000,000个不同价值观.每#4,在相同范围内,少于<1,000,000个不同的float值.因此,某些十进制文本值将转换为相同的float. 在此范围内,我们可以使用6位(而不是7位)进行独特的转化.

As text, the range [9,000,000*103 and 10,000,000*103), using 1 lead digit and 6 trailings ones, there are 1,000,000 different values. Per #4, In the same range, there are less than 1,000,000 different float values. Thus some decimal textual values will convert to the same float. In this range we can use 6, not 7, digits for distinctive conversions.

典型的float的最坏情况是 6个有效数字.查找您的 float的限制:

The worse case for typical float is 6 significant digits. To find the limit for your float:

#include <float.h>
printf("FLT_DIG = %d\n", FLT_DIG);  // this commonly prints 6


...不知道为什么我们应该计算2 ^ 24,为什么我们应该取log10

... no idea why we should calculate 2^24 and why we should take log10

2 ^ 24是通用float的概括,它具有24位的 binary 精度,对应于具有7.22 ...位数字的奇妙的 decimal 系统.我们以 log10 来比较 binary float decimal 文本.

2^24 is a generalization as with common float and its 24 bits of binary precision, that corresponds to fanciful decimal system with 7.22... digits. We take log10 to compare the binary float to decimal text.

2 24 == 10 7.22 ...

224 == 107.22...

但我们不应该服用2 24 .让我们研究一下如何从C11dr§5.2.4.2.211中定义FLT_DIG:

Yet we should not take 224. Let us look into how FLT_DIG is defined from C11dr §5.2.4.2.2 11:

小数位数 q ,这样任何带有 q 小数位数的浮点数都可以四舍五入为 p 基数 b 数字,然后再次返回而无需更改为 q 十进制数字,

number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits,

p log 10 b .............如果b是10的幂> ⎣( p − 1)log 10 b ⎦..否则

p log10 b ............. if b is a power of 10
⎣(p − 1) log10 b⎦.. otherwise

注意"log 10 2 24 "与"24 log 10 2"相同.

Notice "log10 224" is same as "24 log10 2".

作为float,这些值在2的幂之间呈线性分布,如#2、3、4所示.

As a float, the values are distributed linearly between powers of 2 as shown in #2,3,4.

作为 text ,值在10的幂之间线性地分布 ,例如[1.000000 ... 9.999999] * 10 some_exponent .

As text, values are distributed linearly between powers of 10 like a 7 significant digit values of [1.000000 ... 9.999999]*10some_exponent.

这两个组的过渡发生在不同的值. 1,2,4,8,16,32 ...与1,10,100,...在确定最坏情况时,我们从24位减去1 来说明未对齐问题.

The transition of these 2 groups happen at different values. 1,2,4,8,16,32... versus 1,10,100, ... In determining the worst case, we subtract 1 from the 24 bits to account for the mis-alignment.

⎣( p − 1)log 10 b ⎦-> floor((24 − 1) log10(2))-> floor(6.923...)-> 6

⎣(p − 1) log10 b⎦ --> floor((24 − 1) log10(2)) --> floor(6.923...) --> 6.

如果我们的float使用10、100或1000为底,而不是非常常见的2,则这两个组的过渡发生在相同的值上,我们将不减去一个.

Had our float used base 10, 100, or 1000, rather than very common 2, the transition of these 2 groups happen at same values and we would not subtract one.

这篇关于为什么浮点数的有效数字是7或6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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