可以在不丢失重要性的情况下转换为二进制并返回十进制的最重要的十进制数字精度是 6 还是 7.225? [英] Is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance 6 or 7.225?

查看:17
本文介绍了可以在不丢失重要性的情况下转换为二进制并返回十进制的最重要的十进制数字精度是 6 还是 7.225?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过两种不同的浮点数精度公式.

I've come across two different precision formulas for floating-point numbers.

⌊(N-1) log10(2)⌋ = 6 位小数(单精度)

⌊(N-1) log10(2)⌋ = 6 decimal digits (Single-precision)

N log10(2) ≈7.225 个十进制数字(单精度)

N log10(2) ≈ 7.225 decimal digits (Single-precision)

其中 N = 24 有效位(单精度)

Where N = 24 Significant bits (Single-precision)

第一个公式位于IEEE 标准的第 4 页顶部754 for Binary Floating-Point Arithmetic",由 W. Kahan 教授撰写.

The first formula is found at the top of page 4 of "IEEE Standard 754 for Binary Floating-Point Arithmetic" written by, Professor W. Kahan.

第二个公式见维基百科文章单精度浮点格式" 在 IEEE 754 单精度二进制浮点格式:binary32 部分下.

The second formula is found on the Wikipedia article "Single-precision floating-point format" under section IEEE 754 single-precision binary floating-point format: binary32.

对于第一个公式,W. Kahan 教授说

For the first formula, Professor W. Kahan says

如果一个十进制字符串 最多有 6 个 sig.dec. 转换为 Single,然后转换回相同数量的 sig.十二月,那么最终的字符串应该与原始字符串匹配.

If a decimal string with at most 6 sig. dec. is converted to Single and then converted back to the same number of sig. dec., then the final string should match the original.

对于第二个公式,维基百科说

For the second formula, Wikipedia says

...总精度为 24 位(相当于 log10(224) ≈ 7.225 个十进制数字).

...the total precision is 24 bits (equivalent to log10(224) ≈ 7.225 decimal digits).

两个公式的结果(6 位和 7.225 位十进制数字)是不同的,我希望它们是相同的,因为我认为它们都是用来表示可以转换为浮点二进制和然后转换回十进制,并使用与开头相同的有效小数位数.

The results of both formulas (6 and 7.225 decimal digits) are different, and I expected them to be the same because I assumed they both were meant to represent the most significant decimal digits which can be converted to floating-point binary and then converted back to decimal with the same number of significant decimal digits that it started with.

为什么这两个数字不同,可以转换为二进制并返回十进制而不丢失重要性的最重要的十进制数字精度是多少?

推荐答案

可以达到的最高有效十进制数字精度是多少转换为二进制并返回十进制而不丢失重要性?

what is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance?

可以在不丢失重要性的情况下转换为二进制并返回到十进制的最高有效十进制数字精度(对于单精度浮点数或 24 位)是 6 位十进制数字.

The most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance (for single-precision floating-point numbers or 24-bits) is 6 decimal digits.

为什么这两个数字不同...

Why do these two numbers differ...

数字 6 和 7.225 不同,因为它们定义了两个不同的东西.6 是可以往返的最多十进制数字.7.225 是 24 位二进制整数的近似十进制位数精度,因为 24 位二进制整数可以有 7 或 8 位十进制数字,具体取决于其具体值.

The numbers 6 and 7.225 differ, because they define two different things. 6 is the most decimal digits that can be round-tripped. 7.225 is the approximate number of decimal digits precision for a 24-bit binary integer because a 24-bit binary integer can have 7 or 8 decimal digits depending on its specific value.

7.225 是使用特定的二进制整数公式找到的.

7.225 was found using the specific binary integer formula.

dspec = b·log10(2)             (d规范= 特定的十进制数字,b = 位)

dspec = b·log10(2)             (dspec = specific decimal digits, b = bits)

但是,您通常需要知道的是 b 位整数的最小和最大十进制数字.以下公式用于查找特定二进制整数的最小和最大十进制数字(24 位分别为 7 和 8).

However, what you normally need to know, are the minimum and maximum decimal digits for a b-bit integer. The following formulas are used to find the min and max decimal digits (7 and 8 respectively for 24-bits) of a specific binary integer.

dmin = ⌈(b-1)·log10(2)⌉    (d分钟= 最小十进制数字,b = 位,⌈x⌉ = 最小整数 ≥ x)

dmin = ⌈(b-1)·log10(2)⌉    (dmin = min decimal digits, b = bits, ⌈x⌉ = smallest integer ≥ x)

dmax = ⌈b·log10(2)⌉         (dmax= 最大小数位数,b = 位,⌈x⌉ = 最小整数 ≥ x)

dmax = ⌈b·log10(2)⌉         (dmax = max decimal digits, b = bits, ⌈x⌉ = smallest integer ≥ x)

要详细了解这些公式是如何得出的,请阅读 二进制整数中的小数位数,由 Rick Regan 编写.

To learn more about how these formulas are derived, read Number of Decimal Digits In a Binary Integer, written by Rick Regan.

这一切都很好,但您可能会问,如果您说 24 位数字的十进制数字跨度是 7 到 8,为什么 6 是往返转换的最多十进制数字?

This is all well and good, but you may ask, why is 6 the most decimal digits for a round-trip conversion if you say that the span of decimal digits for a 24-bit number is 7 to 8?

答案是——因为上述公式仅适用于整数,不适用于浮点数!

The answer is — because the above formulas only work for integers and not floating-point numbers!

每个十进制整数都有一个精确的二进制值.但是,不能对每个十进制浮点数说同样的话.以 .1 为例.二进制中的 .1 是数字 0.000110011001100...,它是重复或循环的二进制.这会产生舍入误差.

Every decimal integer has an exact value in binary. However, the same cannot be said for every decimal floating-point number. Take .1 for example. .1 in binary is the number 0.000110011001100..., which is a repeating or recurring binary. This can produce rounding error.

此外,表示十进制浮点数比表示同等重要的十进制整数需要多一位.这是因为浮点数越接近 0 越精确,离 0 越远越不精确.因此,许多浮点数接近最小值和最大值范围(emin = -126 和 emax = +127 对于单精度)由于舍入误差而损失 1 位精度.要直观地看到这一点,请查看 每个计算机程序员应该做什么了解浮点,第 1 部分,由 Josh Haberman 编写.

Moreover, it takes one more bit to represent a decimal floating-point number than it does to represent a decimal integer of equal significance. This is because floating-point numbers are more precise the closer they are to 0, and less precise the further they are from 0. Because of this, many floating-point numbers near the minimum and maximum value ranges (emin = -126 and emax = +127 for single-precision) lose 1 bit of precision due to rounding error. To see this visually, look at What every computer programmer should know about floating point, part 1, written by Josh Haberman.

此外,至少有 784,757 个正七位十进制数在往返转换后无法保留其原始值.8.589973e9 就是这样一个无法在往返中存活的数字的例子.这是不保留其原始值的最小正数.

Furthermore, there are at least 784,757 positive seven-digit decimal numbers that cannot retain their original value after a round-trip conversion. An example of such a number that cannot survive the round-trip is 8.589973e9. This is the smallest positive number that does not retain its original value.

这是您应该用于浮点数精度的公式,它将为您提供 6 个十进制数字以进行往返转换.

Here's the formula that you should be using for floating-point number precision that will give you 6 decimal digits for round-trip conversion.

dmax = ⌊(b-1)·log10(2)⌋    (d最大= 最大十进制位数,b = 位,⌊x⌋ = 最大整数 ≤ x)

dmax = ⌊(b-1)·log10(2)⌋    (dmax = max decimal digits, b = bits, ⌊x⌋ = largest integer ≤ x)

要了解有关如何推导出此公式的更多信息,请阅读 往返转换所需的位数,同样由 Rick Regan 编写.Rick 出色地展示了参考严格证明的公式推导.

To learn more about how this formula is derived, read Number of Digits Required For Round-Trip Conversions, also written by Rick Regan. Rick does an excellent job showing the formulas derivation with references to rigorous proofs.

因此,您可以建设性地利用上述公式;如果您了解它们的工作原理,则可以将它们应用于任何使用浮点数据类型的编程语言.您只需要知道您的浮点数据类型所具有的有效位数,并且您可以找到它们各自的十进制位数,您可以依靠这些位数在往返转换后不会丢失重要性.

As a result, you can utilize the above formulas in a constructive way; if you understand how they work, you can apply them to any programming language that uses floating-point data types. All you have to know is the number of significant bits that your floating-point data type has, and you can find their respective number of decimal digits that you can count on to have no loss of significance after a round-trip conversion.

2017 年 6 月 18 日更新:我想包含一个指向 Rick Regan 新文章的链接,该文章更详细,我认为比此处提供的任何答案都更好地回答了这个问题.他的文章是《二进制浮点数的十进制精度"并且可以在他的网站 www.exploringbinary.com 上找到.

June 18, 2017 Update: I want to include a link to Rick Regan's new article which goes into more detail and in my opinion better answers this question than any answer provided here. His article is "Decimal Precision of Binary Floating-Point Numbers" and can be found on his website www.exploringbinary.com.

这篇关于可以在不丢失重要性的情况下转换为二进制并返回十进制的最重要的十进制数字精度是 6 还是 7.225?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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