从Math :: BigFloat转换为内置浮点数

时间:2014-09-27 02:00:06

标签: perl

我想知道如何将$c转换回内置浮点数。我已阅读Math::BigIntMath::BigRatMath::BigFloat的文档。有一些函数可以对数字进行舍入,但我无法弄清楚如何将其强制转换为浮点数,同时保留尽可能高的精度。

use strict;
use warnings;

use Math::BigInt;
use Math::BigRat;

my $a = Math::BigInt::->new(400);
my $b = Math::BigInt::->new(3);

my $c = 2 ** (Math::BigRat::->new($a)/$b);

1 个答案:

答案 0 :(得分:2)

您需要numify()方法:

use strict;
use warnings;

use Math::BigInt;
use Math::BigRat;

my $a = Math::BigInt->new(400);
my $b = Math::BigInt->new(3);

my $c = 2 ** (Math::BigRat->new($a)/$b);

print $c, "\n";
print $c->numify(), "\n";

输出:

13719325343735719921671695695860836966789
1.37193253437357e+40