我正在开发一个用Perl编写的会计脚本,想知道在进行小数算术运算时应该采取“正确”的方式。例如,我想确保这样的比较能够正确地工作:
"0.1" + "0.1" + "0.1" == "0.3"
"258.9" * "2000" == "517800"
...
在Python中,我会使用Decimal
类型来处理值,但是在Perl中应该怎么做呢?
注意:现有的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;
但是!这种魔法只适用于数字。如果你试图把字符串加在一起,它不会起作用,魔法来得太晚了。你必须明确地强制它们成为数字。要将字符串转换为数字,可以像这样将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 将保留有效数字。Math::BigFloat 不会。
Math::BigFloat
对我来说似乎可以工作,尽管它仍然是浮点算术。我很惊讶没有核心模块来执行十进制算术。 - Eugene Yarmash0.01
变成了带有指数 2
的 1
。每个任意精度库都必须有其精度的限制,否则您很容易耗尽内存。例如,1/3
。Python 的 Decimal 限制为 30 位。Math::BigFloat 可以达到 40 位。Math::BigFloat 讨论了这个问题。如果您需要更高的精度,请使用 Math::BigRat。它将分数存储为分数。use bigrat; print 1/3
将输出 1/3
。 - Schwernuse 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 -E'say 0.1 + 0.1 + 0.1 == 0.3 ? "equal" : "not equal"'
。 - ikegami