如何在不损失精度的情况下打印 Perl bignum?
#!/usr/bin/perl
use strict;
use warnings;
my $s = "1234567890.123456789";
{
no bignum; printf "bignum==%s\n", bignum::in_effect() // 0;
my $x = $s;
printf "%29s\n", $x;
printf "%29.9f\n\n", $x;
}
{
use bignum; printf "bignum==%s\n", bignum::in_effect() // 0;
my $x = $s;
printf "%29s\n", $x;
printf "%29.9f\n\n", $x;
}
我的 Perl 的 printf (为 darwin-thread-multi-2level 构建的 ActiveState v5.10.1)使用 %f 转换不尊重超过 1e-6 位的值,即使使用 bignum 时也是如此:
$ t.pl
bignum==0
1234567890.123456789
1234567890.123456717
bignum==1
1234567890.123456789
1234567890.123456717
How can I print my input without loss precision ?
我的真正问题是我需要操纵这个数字(例如,$x/0.000_000_001,或者更糟糕的是,$x/0.000_001_024,我不能用 substr( )函数调用),但是在我到达“有趣”的部分之前,当前的减少已经困扰了我。
#!/usr/bin/perl
use strict;
use warnings;
my $s = "1234567890.123456789";
{
no bignum; printf "bignum==%s\n", bignum::in_effect() // 0;
my $x = $s;
printf "%29s\n", $x;
printf "%29.9f\n\n", $x;
}
{
use bignum; printf "bignum==%s\n", bignum::in_effect() // 0;
my $x = $s;
printf "%29s\n", $x;
printf "%29.9f\n\n", $x;
}
My Perl's printf (ActiveState v5.10.1 built for darwin-thread-multi-2level) using the %f conversion doesn't honor my value past the 1e-6 digit, even when using bignum:
$ t.pl
bignum==0
1234567890.123456789
1234567890.123456717
bignum==1
1234567890.123456789
1234567890.123456717
How can I print my input without losing precision?
My real problem is that I'm going to need to manipulate this number (e.g., $x/0.000_000_001, or, worse, $x/0.000_001_024, which I can't fake with substr() function calls), but the current abatement has stumped me before I can even get to the "fun" part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Perl 的
printf
并不真正执行 bignums。使用 Math::BigFloat 方法之一获取字符串。由于这样做只是复制字符串,因此您必须执行类似操作
才能使
$x
成为 Math::BigFloat。然后类似的事情应该做你想做的事。
Perl's
printf
doesn't really do bignums. Use one of the Math::BigFloat methods for getting a string. Since doingjust copies the string, you'll have to do something like
so that
$x
is a Math::BigFloat. Then something likeshould do what you want.