简体   繁体   English

perl打印浮点数的精度是多少?

[英]To what precision does perl print floating-point numbers?

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? perl 打印浮点数的精度是多少?

Using: v5.8.8 built for x86_64-linux-thread-multi 使用:v5.8.8为x86_64-linux-thread-multi构建

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. 在对浮点数进行字符串化时,无论是打印还是其他方式,Perl通常使用float.h中的DBL_DIG或LDBL_DIG值或者编译它们的limits.h文件。

This is typically the precision of the floating point type perl will use rounded down. 这通常是perl将使用向下舍入的浮点类型的精度。 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. 例如,如果使用典型的double类型,精度为53位= 15.95位,Perl通常用15位精度进行字符串化。

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

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