简体   繁体   English

确定浮点数的十进制精度

[英]Determining decimal precision of a floating point number

I have been searching the internet for days now and can't seem to find a generally accepted formula for determining the number of decimal digits that we can store in a floating point format without truncation error. 我已经在互联网上搜索了几天,似乎无法找到公认的公式来确定我们可以以浮点格式存储的小数位数而不会出现截断错误。 The answer I came across the most often was: 我最常遇到的答案是:

Decimal precision = log₁₀(2ⁿ), where n = the number of bits in the mantissa. 十进制精度=log₁₀(2ⁿ),其中n =尾数中的位数。

Now, this lines up with everything I've seen with the single point and double point formats discussed in my CS book. 现在,这与我在CS本书中讨论的单点和双点格式所看到的所有内容保持一致。

However, when I went to check this against the examples in my CS book, the numbers didn't line up. 但是,当我对照CS书中的示例进行检查时,数字并没有对齐。

The examples used an 8-bit format with a 4 bit mantissa. 这些示例使用8位格式和4位尾数。 Given the formula above, I should only be able to store a decimal value with precision of up to 1.2 digits. 给定上面的公式,我应该只能存储精度不超过1.2位的十进制值。 However, according to the book (and confirmed by trying other decimal values) I can store a value such as 2.25 in a 4 bit mantissa with out truncation. 但是,根据该书(并尝试使用其他十进制值进行确认),我可以将4位尾数中的2.25之类的值无截断地存储。 0.0625 can be stored as well without error. 0.0625也可以存储而没有错误。 In both of these cases, the decimal digits exceed the 1.2 given from the equation above. 在这两种情况下,十进制数字均超过上式给出的1.2。

I can't help but wonder if there is something I am missing. 我不禁想知道我是否缺少某些东西。 Is there a way to determine decimal precision given a mantissa of any size? 给定任意大小的尾数,是否可以确定小数精度? Does the formula above conflict with mantissa fields below a certain size? 上面的公式是否与一定大小以下的尾数字段冲突?

Any clarity on the matter would be greatly appreciated. 对此事的任何澄清将不胜感激。

Danny D. 丹尼·D

a generally accepted formula for determining the number of decimal digits that we can store in a floating point format without truncation error. 一个通用的公式,用于确定我们可以以浮点格式存储的无小数位数的小数位数。

Research FLT_DIG, DBL_DIG, LDBL_DIG . 研究FLT_DIG, DBL_DIG, LDBL_DIG They are the number of significant decimal digits (as in text ) that can be rounded into a floating-point type and back again for float, double, long double . 它们是有效的小数位数(如文本中所示 ),可以四舍五入为浮点类型,然后再次返回为float, double, long double

With binary bases and to generalize for OP, use 带有二进制基数并通用化为OP,请使用

floor((significant_bits - 1) log10(base))

With base == 2 and a significant_bits == 5 bit signicand (4 explicitly encoded, 1 implied), this is floor(1.204...) or 1 . base == 2并且significant_bits == 5significant_bits == 5位符号(显式编码为4,隐含1),这是floor(1.204...)1

This means with decimal numbers of the form w.000 * 10 power , all combinations of w ( 1 - 9 ) and power (in a min/max range) would "round-trip" from decimal text to OP_s_Floating_point_type to decimal text successfully. 这意味着与表单w.000 * 10 的电力的十进制数,的所有组合w1 - 9 )和power (在最小/最大范围)将“往返”从十进制文本到OP_s_Floating_point_type成功十进制文本。

If one tried to round-trip decimal text of the form w.y00 * 10 power , for some combination of w,y , a failure would occur. 如果尝试对w.y00 * 10 power形式的十进制文本进行往返操作,则对于w,y某些组合,将发生故障。 It does not mean all combinations of that form would fail, just some. 这并不意味着该形式的所有组合都会失败,只是其中的一部分。

Consider trying to take decimal text "9.0", "9.1", "9.2", ... "9.9" and convert those 10 to OP's FP format. 考虑尝试采用十进制文本“ 9.0”,“ 9.1”,“ 9.2”,...“ 9.9”,并将那10转换为OP的FP格式。 In the 9 range, that format only supports encoding 9.0 and 9.5. 在9范围内,该格式仅支持9.0和9.5编码。 So mapping those 10 numbers would result in 2 different FP numbers. 因此,映射这10个数字将导致2个不同的FP数字。 Of course converting those 2 back to decimal form results in only 2 different answers. 当然,将这2个转换回十进制形式只会得到2个不同的答案。 The round-trip failed for 8 of those 10. 这10个中有8个往返失败。

if there is something I am missing. 如果有什么我想念的。
Is there a way to determine decimal precision given a mantissa of any size? 给定任意大小的尾数,是否可以确定小数精度?

The value of 1 derived from the above formula is the worst case and the worst case is the number usually reported for decimal precision . 从上述公式得出的值1是最坏的情况最坏的情况是通常以十进制精度报告的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM