十进制和浮动的精确值为双倍? [英] Precise value of double in decimal and floating?

查看:208
本文介绍了十进制和浮动的精确值为双倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DBL_MAX

  179769313486231570814527423731704356798070567525844996598917476803157260 
780028538760589558632766878171540458953514382464234321326889464182768467
546703537516986049910576551282076245490090389328944075868508455133942304
583236903222948165808559332123348274797826204144723168738177180919299881
250404026184124858368.000000

但如果我这样做:

  double a = 1234567890123456789.0; 
printf(%f,a);




1234567890123456768.000000


这里的精度是17位数。

  double a = 0.1234567890123456789; 
printf(%。20f,a);




0.1234567890123456773


这里浮点后的精度是17位数:

  double a = 1234567890.1234567890123456789 ; 
printf(%。20f,a);

这将生成:


1234567890.12345671653747558594


现在,浮点后的十进制+ 7的精度将为10位, / p>

这是否意味着我只有17位才能获得双精度的精确值?如果是,为什么DBL_MAX的数字超过300位数字?

解决方案

典型的 double 具有15-16个十进制数字的意义 - 与下一个 double 相比。



所有有限的 double 具有如OP的 DBL_MAX = 1797693134862315708 ... 的确切值。浮点数是对数分布的。所以通常有两种不同的 double 在0.5到1.0之间,因为在2到4之间。



意味着OP的第二大数字只匹配 DBL_MAX 到前300位数字的前16位。见下文。







这是否意味着我只有17位才能获得双精度值?如果是,使用DBL_MAX的所有数字的数字是多于300位数字?


double ,因为 binary64 可以正确地表示 / em>约2 ** 64个不同的数字。 0.1234567890123456789 不是其中之一。最近的 double 0.1234567890123456773 ... 注意它匹配至少15 (DBL_DIG) 数位。



通常这意味着 double 确切 $ c>与下一个 double 的相对差异不重要。



1e300 1e-300 范围之一,而不是精确度。






  int main(void){
printf(%。* e\\\
DBL_DECIMAL_DIG + 2,DBL_MAX);
printf(%。* e\\\
,DBL_DECIMAL_DIG + 2,nextafter(DBL_MAX,0));
printf(%。* e\\\
,DBL_DECIMAL_DIG + 2,nextafter(0,1));
}

输出

  1.7976931348623157081e + 308 
1.7976931348623155086e + 308
4.9406564584124654418e-324

考虑使用 printf(%。* e,DBL_DECIMAL_DIG-1,a); 而不是 printf( %.20f,a); 可以更清楚地查看十进制符号意义一个 double 。如果您准备好十六进制符号,请尝试 printf(%a,a);


DBL_MAX is

179769313486231570814527423731704356798070567525844996598917476803157260
780028538760589558632766878171540458953514382464234321326889464182768467
546703537516986049910576551282076245490090389328944075868508455133942304
583236903222948165808559332123348274797826204144723168738177180919299881
250404026184124858368.000000

But if I do:

double a=1234567890123456789.0;
printf("%f",a);

1234567890123456768.000000

Here the precision is 17 digits.

double a=0.1234567890123456789;
printf("%.20f",a);

0.1234567890123456773

Here also the precision after the floating point is 17 digits:

double a=1234567890.1234567890123456789;
printf("%.20f",a);

That will generate:

1234567890.12345671653747558594

Now, the precision will be 10 digits for decimal + 7 after the floating point which makes 17.

Does that mean that I have only 17 digits to get a precise value for double? If yes, why is the number of digits of DBL_MAX over 300 digits?

解决方案

Typical double has 15-16 decimal digits of significance - as compared to the next double.

All finite double have exact values like with OP's DBL_MAX = 1797693134862315708.... Floating point numbers are distributed logarithmically. So typically there are as many different double between 0.5 and 1.0 as there are between 2 and 4.

This distribution means OP's 2nd largest number only matches DBL_MAX to first 16 places out of the 300+ digits. See below.


Does it mean that I have only 17 digits to get a precise value for double ? If yes what is the use of all that number of digits for DBL_MAX more than 300 digit?

double, as binary64 can represent exactly about 2**64 different numbers. 0.1234567890123456789 is not one of them. The closest double is 0.1234567890123456773... Notice it matches to at least 15 (DBL_DIG) digits.

Usually this means the exact value of a double is not as important as its relative difference to the next double.

The value of 1e300 or 1e-300 is one of range, not precision.


int main(void) {
  printf("%.*e\n", DBL_DECIMAL_DIG + 2, DBL_MAX);
  printf("%.*e\n", DBL_DECIMAL_DIG + 2, nextafter(DBL_MAX,0));
  printf("%.*e\n", DBL_DECIMAL_DIG + 2, nextafter(0, 1));
}

Output

1.7976931348623157081e+308
1.7976931348623155086e+308
4.9406564584124654418e-324

Consider using printf("%.*e", DBL_DECIMAL_DIG-1, a); rather than printf("%.20f",a); to get a more clear view of the decimal notation significance of a double. If you are ready for hexadecimal notation, try printf("%a",a);

这篇关于十进制和浮动的精确值为双倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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