如何在Perl执行数字算术?

问题描述 投票:9回答:3

我正在用Perl编写的脚本,会计,我不知道什么是“正确”的方式来执行数字算术运算。举例来说,我想,以确保像这些比较正常工作:

"0.1" + "0.1" + "0.1" == "0.3"
"258.9" * "2000" == "517800"
...

在Python我使用Decimal类型的值,但我该怎么在Perl呢?

perl decimal
3个回答
8
投票

(注:有Math::Currency但目前破)。

使用Math::BigFloat来表示数字的任意精度的对象。

use Math::BigFloat;

print Math::BigFloat->new(0.1) +
      Math::BigFloat->new(0.1) +
      Math::BigFloat->new(0.1) == Math::BigFloat->new(0.3);

你可以用bignum自动做到这一点?

use bignum;

print 0.1 + 0.1 + 0.1 == 0.3;

但!魔术仅适用于数字。如果您尝试添加字符串连接在一起,它不会工作,魔来得太晚。你必须明确地迫使他们是数字。要numify一个字符串,您可以添加0到字符串,如$a += 0。或者你可以用0 +开始强制公式做的大数,它会向下级联线。

use bignum;

$a = "0.1";
$b = "0.1";
$c = "0.1";
$d = "0.3";

# False
print $a + $b + $c == $d;

# True
print 0 + $a + $b + $c == $d;

两个警告。

首先,这一切是在一个沉重的性能代价。不仅做任意精度数学,但所有的方法和重载魔力。基准测试,看是否这是可以接受的。幸运的bignum只在其范围内,而不是整个程序将升级号码。这也是安全使用bignum的范围之外的这些数字,与他们做任何数学也将升级。

其次,Decimal将保留显著数字。数学:: BigFloat不会。


0
投票

下面是如何使用Math::Decimal除法:

use 5.024003; # use `say'
use Math::Decimal qw( 
                     dec_mul_pow10 
                     dec_neg 
                     dec_rndiv 
                    ); 
my $num_1 = '3'; 
my $num_2 = '7'; 
my $precision = '3'; 

# You want to get three digits after the decimal, 
# so multiply dividend by 1000, divide to get an 
# integer quotient, and then divide that quotient 
# by 1000. 
my $dividend_up_some = dec_mul_pow10( $num_1, $precision ); 
# Rounding by `NEAR_EVN' is "bankers' rounding." 
my $quotient_up_some = 
          dec_rndiv( 'NEAR_EVN', $dividend_up_some, 
                     $num_2 ); 
# Move it back down to get your desired precision after 
# the decimal. 
my $quotient = 
      dec_mul_pow10( $quotient_up_some, 
                     dec_neg( $precision ) ); 
say "$num_1 / $num_2 = $quotient";

运行此程序,这里是输出:

3 / 7 = 0.429

更改$精度为“10”,这里的输出:

3 / 7 = 0.4285714286

-4
投票

我知道最好的办法是如何具有绝对差异小于公差测试。例如:

的perl -e“$ X = 0.1 + 0.1 + 0.1; $ Y = 0.3; $ Q = ABS($ X - $ Y)<0.0001? “平等”:“不等于”;打印$ Q。 “\ n”;”

© www.soinside.com 2019 - 2024. All rights reserved.