我如何在 Perl 中执行小数运算?
How do I perform decimal arithmetic in Perl?
我正在编写一个用 Perl 编写的会计脚本,我想知道执行十进制算术计算的 'proper' 方法是什么。例如,我想确保像这样的比较工作正常:
"0.1" + "0.1" + "0.1" == "0.3"
"258.9" * "2000" == "517800"
...
在 Python 中,我将使用 Decimal
类型作为值,但我在 Perl 中做什么?
我知道的最好方法是在绝对差异小于公差的情况下进行测试。例如:
perl -e '$x = 0.1 + 0.1 + 0.1; $y = 0.3; $q = abs($x - $y) < 0.0001 ? "EQUAL" : "NOT EQUAL";打印 $q 。 "\n";'
(注意:有 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;
但是! 魔法只适用于 numbers。如果您尝试将字符串加在一起,那是行不通的,魔法来得太晚了。您必须明确强制它们为数字。要对字符串进行数值化,您可以将 0 添加到字符串中,例如 $a += 0
。或者,您可以通过从 0 +
开始强制将方程式作为 bignums 来完成,它会向下级联。
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 将保留有效数字。 Math::BigFloat不会。
以下是您可以如何使用 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
将 $precision 更改为“10”,这是输出:
3 / 7 = 0.4285714286
我正在编写一个用 Perl 编写的会计脚本,我想知道执行十进制算术计算的 'proper' 方法是什么。例如,我想确保像这样的比较工作正常:
"0.1" + "0.1" + "0.1" == "0.3"
"258.9" * "2000" == "517800"
...
在 Python 中,我将使用 Decimal
类型作为值,但我在 Perl 中做什么?
我知道的最好方法是在绝对差异小于公差的情况下进行测试。例如:
perl -e '$x = 0.1 + 0.1 + 0.1; $y = 0.3; $q = abs($x - $y) < 0.0001 ? "EQUAL" : "NOT EQUAL";打印 $q 。 "\n";'
(注意:有 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;
但是! 魔法只适用于 numbers。如果您尝试将字符串加在一起,那是行不通的,魔法来得太晚了。您必须明确强制它们为数字。要对字符串进行数值化,您可以将 0 添加到字符串中,例如 $a += 0
。或者,您可以通过从 0 +
开始强制将方程式作为 bignums 来完成,它会向下级联。
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 将保留有效数字。 Math::BigFloat不会。
以下是您可以如何使用 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
将 $precision 更改为“10”,这是输出:
3 / 7 = 0.4285714286