为什么 Math::BigInt 输出错误?
Why does Math::BigInt has wrong output?
我需要得到最接近 $new
到 $orig
且能被 $divisor
整除的数字。 $new
应该大于 $orig
。所以我得出了以下公式(希望没有错误):
$new = $orig + ($divisor - $orig % $divisor)
现在,$orig
号码是一个整数,最多有 30 位数字。我想使用 Math::BigInt
将其实现到 Perl 函数中,但输出是完全错误的。
use Math::BigInt;
Math::BigInt->accuracy(60);
Math::BigInt->precision(60);
my $orig = Math::BigInt->new('5967920747812842369477355441'); # A
my $divisor = Math::BigInt->new('719'); # B
my $modulo = $orig->bmod($divisor); # A % B = M
my $diff = $divisor->bsub($modulo); # B - M = D
my $new = $orig->badd($diff); # A + D = N
my $test = $new->bdiv($divisor); # N / B = 0
print("orig : $orig\n"); # 10; should be: 5967920747812842369477355441
print("modulo : $modulo\n"); # 10; should be: 648
print("diff : $diff\n"); # 71; should be: 71
print("new : $new\n"); # 10; should be: 5967920747812842369477355512
print("test : $test\n"); # 10; should be: 0
https://metacpan.org/pod/Math::BigInt#Arithmetic-methods : "These methods modify the invocand object and returns it." 换句话说,bmod
、bsub
和 badd
就像 %=
、-=
, 和 +=
, 不像 %
, -
, 和 +
.
所以无论你在哪里调用其中一种算术方法,你都应该先复制,这样你当前调用该方法的对象就不会改变:
use Math::BigInt;
Math::BigInt->accuracy(60);
Math::BigInt->precision(60);
my $orig = Math::BigInt->new('5967920747812842369477355441'); # A
my $divisor = Math::BigInt->new('719'); # B
my $modulo = $orig->copy->bmod($divisor); # A % B = M
my $diff = $divisor->copy->bsub($modulo); # B - M = D
my $new = $orig->copy->badd($diff); # A + D = N
my $test = $new->copy->bmod($divisor); # N % B = 0
print("orig : $orig\n"); # 10; should be: 5967920747812842369477355441
print("modulo : $modulo\n"); # 10; should be: 648
print("diff : $diff\n"); # 71; should be: 71
print("new : $new\n"); # 10; should be: 5967920747812842369477355512
print("test : $test\n"); # 10; should be: 0
(还更改了您的测试以进行取模,而不是除法。)
我需要得到最接近 $new
到 $orig
且能被 $divisor
整除的数字。 $new
应该大于 $orig
。所以我得出了以下公式(希望没有错误):
$new = $orig + ($divisor - $orig % $divisor)
现在,$orig
号码是一个整数,最多有 30 位数字。我想使用 Math::BigInt
将其实现到 Perl 函数中,但输出是完全错误的。
use Math::BigInt;
Math::BigInt->accuracy(60);
Math::BigInt->precision(60);
my $orig = Math::BigInt->new('5967920747812842369477355441'); # A
my $divisor = Math::BigInt->new('719'); # B
my $modulo = $orig->bmod($divisor); # A % B = M
my $diff = $divisor->bsub($modulo); # B - M = D
my $new = $orig->badd($diff); # A + D = N
my $test = $new->bdiv($divisor); # N / B = 0
print("orig : $orig\n"); # 10; should be: 5967920747812842369477355441
print("modulo : $modulo\n"); # 10; should be: 648
print("diff : $diff\n"); # 71; should be: 71
print("new : $new\n"); # 10; should be: 5967920747812842369477355512
print("test : $test\n"); # 10; should be: 0
https://metacpan.org/pod/Math::BigInt#Arithmetic-methods : "These methods modify the invocand object and returns it." 换句话说,bmod
、bsub
和 badd
就像 %=
、-=
, 和 +=
, 不像 %
, -
, 和 +
.
所以无论你在哪里调用其中一种算术方法,你都应该先复制,这样你当前调用该方法的对象就不会改变:
use Math::BigInt;
Math::BigInt->accuracy(60);
Math::BigInt->precision(60);
my $orig = Math::BigInt->new('5967920747812842369477355441'); # A
my $divisor = Math::BigInt->new('719'); # B
my $modulo = $orig->copy->bmod($divisor); # A % B = M
my $diff = $divisor->copy->bsub($modulo); # B - M = D
my $new = $orig->copy->badd($diff); # A + D = N
my $test = $new->copy->bmod($divisor); # N % B = 0
print("orig : $orig\n"); # 10; should be: 5967920747812842369477355441
print("modulo : $modulo\n"); # 10; should be: 648
print("diff : $diff\n"); # 71; should be: 71
print("new : $new\n"); # 10; should be: 5967920747812842369477355512
print("test : $test\n"); # 10; should be: 0
(还更改了您的测试以进行取模,而不是除法。)