如何在不损失精度的情况下打印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- 2级),即使使用bignum,使用%f转换也无法兑现超过1e-6位的值:
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?
我的 real 问题是我将需要操纵这个数字(例如,$ x / 0.000_000_001,或更糟糕的是,$ x / 0.000_001_024,我不能用substr()函数调用来伪造该数字) ),但目前的减排措施使我感到困惑,甚至无法进入乐趣部分。
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.
Perl的 printf
并没有真正实现bignums。使用 Math :: BigFloat 方法之一来获取字符串。因为这样做
Perl's printf
doesn't really do bignums. Use one of the Math::BigFloat methods for getting a string. Since doing
my $x = $s;
只复制字符串,就必须做类似的事情
just copies the string, you'll have to do something like
my $x = 0+$s;
,因此 $ x
是一个数学运算: :BigFloat。然后类似
so that $x
is a Math::BigFloat. Then something like
printf "%29s\n", $x->ffround(-9);
应该做你想做的事。