MS VS 2013'LDBL_DIG'const。从'float.h'是不正确的? [英] MS VS 2013 'LDBL_DIG' const. From 'float.h' is incorrect?

查看:75
本文介绍了MS VS 2013'LDBL_DIG'const。从'float.h'是不正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自MS VS Community 2013 Update 5的'float.h':

Quote:

#define LDBL_DIG DBL_DIG / *精度的十进制数字* /

引用:

#define DBL_DIG 15 / *精度的十进制数* /

我用 sprintf 测试了它,它显示 17 有意义的数字。



当我在调试模式下运行测试代码时,它还在编辑器('autos'窗口)中显示17位数字('0.14285714285714285')。



I还在MS VS 2015社区更新2 RC中打开了'float.h',它仍然相同,

但是现在它还有一个新的常量:

引用:

#define DBL_DECIMAL_DIG 17 //舍入精度的十进制数字数





我尝试过:



T. ets代码:



const auto oneSeventh = 1.0L / 7.0L;

sprintf(buf,%。* Lf ,30U,oneSeventh); // 0.142857142857142850000000000000

解决方案

双浮点数有53个尾数位( DBL_MANT_DIG )其中MSB是隐式设置的,并且存储了52位。相应的小数位数是

 log10(2 ^ 53)= 53 * log10(2)= 15.95 



将该值向下舍入用作 DBL_DIG 。因此,精确度几乎还有一位十进制数。



DBL_DECIMAL_DIG 指定可以的小数位数四舍五入到双精度数字并再次返回而不更改值。为满足此要求,它必须比16的基准精度高一个。另请参阅< float.h> [ ^ ]。



因此,打印具有17个有效十进制数字的值并将其转换回双精度将产生相同的值。当只打印16位数字时,转换后的值可能不相同。



Microsoft没有 long double的原生支持精度:

long double和double的表示是相同的。但是,long double和double是单独的类型。

因此 LDBL_xxx 定义使用相应的 DBL_xxx 定义


In 'float.h' from MS VS Community 2013 Update 5:

Quote:

#define LDBL_DIG DBL_DIG /* # of decimal digits of precision */

Quote:

#define DBL_DIG 15 /* # of decimal digits of precision */

I tested it using sprintf, it shows 17 meaningful digits.

It also shows 17 digits ('0.14285714285714285') in the editor ('autos' window), when i running test code in the debug mode.

I also opened 'float.h' in the MS VS 2015 Community Update 2 RC, it still same,
but now it has also a new constant:

Quote:

#define DBL_DECIMAL_DIG 17 // # of decimal digits of rounding precision



What I have tried:

Tets code:

const auto oneSeventh = 1.0L / 7.0L;
sprintf(buf, "%.*Lf", 30U, oneSeventh); // 0.142857142857142850000000000000

解决方案

A double floating point number has 53 mantissa bits (DBL_MANT_DIG) where the MSB is implicit set and 52 bits are stored. The corresponding number of decimal digits is

log10(2^53) = 53 * log10(2) = 15.95


Rounding that value down is used as DBL_DIG. So there is nearly one more decimal digit of precision.

DBL_DECIMAL_DIG specifies the number of decimal digits that can be rounded to a double precision number and back again without change to the value. It must be one more than the uprounded precision of 16 to satisfy this requirement. See also the standard at <float.h>[^].

So printing a value with 17 significant decimal digits and converting that back to double precision will result in the same value. When printing only 16 digits, the converted value might be not identical.

Microsoft has no native support for long double precision:

The representation of long double and double is identical. However, long double and double are separate types.

Therefore the LDBL_xxx definitions are using the corresponding DBL_xxx definitions.


这篇关于MS VS 2013'LDBL_DIG'const。从'float.h'是不正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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