To what precision does perl print floating-point numbers?

倖福魔咒の 提交于 2019-12-10 14:33:41

问题


my $num = log(1_000_000) / log(10);
print "num: $num\n";
print "int(num): " . int($num) . "\n";
print "sprintf(num): " . sprintf("%0.16f", $num) . "\n";

produces:

num: 6
int(num): 5
sprintf(num): 5.9999999999999991

To what precision does perl print floating-point numbers?

Using: v5.8.8 built for x86_64-linux-thread-multi


回答1:


When stringifying floating point numbers, whether to print or otherwise, Perl generally uses the value of DBL_DIG or LDBL_DIG from the float.h or limits.h file where it was compiled.

This is typically the precision of the floating point type perl will use rounded down. For instance, if using a typical double type, the precision is 53 bits = 15.95 digits, and Perl will usually stringify with 15 digits of precision.



来源:https://stackoverflow.com/questions/14104372/to-what-precision-does-perl-print-floating-point-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!