perl打印浮点数的精度是多少? [英] To what precision does perl print floating-point numbers?

查看:170
本文介绍了perl打印浮点数的精度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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";

产生:

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

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

To what precision does perl print floating-point numbers?

使用:针对x86_64-linux-thread-multi构建的v5.8.8

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

推荐答案

在对浮点数进行字符串化(无论是打印还是以其他方式)时,Perl通常使用float.h或limits.h文件中的DBL_DIG或LDBL_DIG的值.被编译.

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将使用四舍五入的浮点类型的精度.例如,如果使用典型的双精度类型,则精度为53位= 15.95位,而Perl通常以15位精度进行字符串化.

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.

这篇关于perl打印浮点数的精度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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