是浮点precision可变还是不变? [英] Is floating point precision mutable or invariant?

查看:237
本文介绍了是浮点precision可变还是不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到的混合答案是否浮点数(即浮动双击长双)有且只有一个precision的价值,或者有precision值可能会有所不同。

I keep getting mixed answers of whether floating point numbers (i.e. float, double, or long double) have one and only one value of precision, or have a precision value which can vary.

一个题目叫浮动与双precision 似乎在暗示浮点precision是绝对的。

One topic called float vs. double precision seems to imply that floating point precision is an absolute.

然而,另一个话题称为float和double 说的差异,

However, another topic called Difference between float and double says,

一般双有 15至16 precision的小数位数

In general a double has 15 to 16 decimal digits of precision

说:

float类型的变量通常具有关于 7显著位数

Variables of type float typically have a precision of about 7 significant digits

类型的变量通常翻番有一个precision 关于 16显著位数

Variables of type double typically have a precision of about 16 significant digits

我不喜欢提及像上面的近似,如果我敏感code,可以容易折断,当我的值是不准确的工作。因此,让我们以正视听。是浮点precision可变或不变,为什么?

I don't like to refer to approximations like the above if I'm working with sensitive code that can break easily when my values are not exact. So let's set the record straight. Is floating point precision mutable or invariant, and why?

推荐答案

在precision是固定的,这就是恰好53位二进制数字作为双precision(或52,如果我们排除隐​​式龙头1)。这涉及到关于15位十进制数

The precision is fixed, which is exactly 53 binary digits for double-precision (or 52 if we exclude the implicit leading 1). This comes out to about 15 decimal digits.

该任择议定书要求我阐述一下为什么有恰好53个二进制数字表示关于15位十进制数。

The OP asked me to elaborate on why having exactly 53 binary digits means "about" 15 decimal digits.

要直观地理解这一点,让我们考虑一个less- precise浮点格式:而不是一个52位尾数像双precision人数,我们只是将使用一个4位尾数。

To understand this intuitively, let's consider a less-precise floating-point format: instead of a 52-bit mantissa like double-precision numbers have, we're just going to use a 4-bit mantissa.

所以,每个数字看起来像:(-1)取值&倍; 2 YYY &倍; 1.xxxx(其中取值为符号位, YYY 是指数,而 1 .XXXX 是尾数)。对于眼前的讨论中,我们将只关注尾数,而不是标志或指数。

So, each number will look like: (-1)s × 2yyy × 1.xxxx (where s is the sign bit, yyy is the exponent, and 1.xxxx is the normalised mantissa). For the immediate discussion, we'll focus only on the mantissa and not the sign or exponent.

下面是一个什么样 1.xxxx 看起来像所有的表XXXX 值(四舍五入所有半至 - 甚至,只是怎么样的默认浮点舍入模式下工作):

Here's a table of what 1.xxxx looks like for all xxxx values (all rounding is half-to-even, just like how the default floating-point rounding mode works):

  xxxx  |  1.xxxx  |  value   |  2dd  |  3dd  
--------+----------+----------+-------+--------
  0000  |  1.0000  |  1.0     |  1.0  |  1.00
  0001  |  1.0001  |  1.0625  |  1.1  |  1.06
  0010  |  1.0010  |  1.125   |  1.1  |  1.12
  0011  |  1.0011  |  1.1875  |  1.2  |  1.19
  0100  |  1.0100  |  1.25    |  1.2  |  1.25
  0101  |  1.0101  |  1.3125  |  1.3  |  1.31
  0110  |  1.0110  |  1.375   |  1.4  |  1.38
  0111  |  1.0111  |  1.4375  |  1.4  |  1.44
  1000  |  1.1000  |  1.5     |  1.5  |  1.50
  1001  |  1.1001  |  1.5625  |  1.6  |  1.56
  1010  |  1.1010  |  1.625   |  1.6  |  1.62
  1011  |  1.1011  |  1.6875  |  1.7  |  1.69
  1100  |  1.1100  |  1.75    |  1.8  |  1.75
  1101  |  1.1101  |  1.8125  |  1.8  |  1.81
  1110  |  1.1110  |  1.875   |  1.9  |  1.88
  1111  |  1.1111  |  1.9375  |  1.9  |  1.94

多少十进制数字,你说提供?可以说图2,在该两个十进制数范围内的每个值被覆盖,尽管不是唯一地;或者你可以说3,它涵盖了所有唯一值,但不要在三个小数位数范围内的所有值提供覆盖。

How many decimal digits do you say that provides? You could say 2, in that each value in the two-decimal-digit range is covered, albeit not uniquely; or you could say 3, which covers all unique values, but do not provide coverage for all values in the three-decimal-digit range.

有关参数的缘故,我们会说这有2个十进制数字:小数precision将是数字在那里这些十进制数字的所有值可以重新presented数

For the sake of argument, we'll say it has 2 decimal digits: the decimal precision will be the number of digits where all values of those decimal digits could be represented.

好吧,那么,这样会发生什么,如果我们减少一半的所有号码(所以我们使用 YYY = -1)?

Okay, then, so what happens if we halve all the numbers (so we're using yyy = -1)?

  xxxx  |  1.xxxx  |  value    |  1dd  |  2dd  
--------+----------+-----------+-------+--------
  0000  |  1.0000  |  0.5      |  0.5  |  0.50
  0001  |  1.0001  |  0.53125  |  0.5  |  0.53
  0010  |  1.0010  |  0.5625   |  0.6  |  0.56
  0011  |  1.0011  |  0.59375  |  0.6  |  0.59
  0100  |  1.0100  |  0.625    |  0.6  |  0.62
  0101  |  1.0101  |  0.65625  |  0.7  |  0.66
  0110  |  1.0110  |  0.6875   |  0.7  |  0.69
  0111  |  1.0111  |  0.71875  |  0.7  |  0.72
  1000  |  1.1000  |  0.75     |  0.8  |  0.75
  1001  |  1.1001  |  0.78125  |  0.8  |  0.78
  1010  |  1.1010  |  0.8125   |  0.8  |  0.81
  1011  |  1.1011  |  0.84375  |  0.8  |  0.84
  1100  |  1.1100  |  0.875    |  0.9  |  0.88
  1101  |  1.1101  |  0.90625  |  0.9  |  0.91
  1110  |  1.1110  |  0.9375   |  0.9  |  0.94
  1111  |  1.1111  |  0.96875  |  1.   |  0.97

根据同样的标准和以前一样,我们现在正在处理1十进制数字。所以你可以看到,根据不同的指数,可以有更多或更少的十进制数字,因为二进制和十进制浮点数字不干净相互映射

同样的道理也适用于双精度型$ P ​​$ pcision浮点数(与52位尾数),只有在你得到或者根据指数15或16位十进制数那样的话。

The same argument applies to double-precision floating point numbers (with the 52-bit mantissa), only in that case you're getting either 15 or 16 decimal digits depending on the exponent.

这篇关于是浮点precision可变还是不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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