php+math+biginteger,PHP使用Math::BigInt遇到诡异的问题

博客讨论了在Perl中使用Math::BigInt模块处理大整数除法的问题。作者指出,由于Math::BigInt的运算方法会修改原始对象,导致计算错误。解决方案是使用`copy`方法先复制对象,然后再进行运算。修复后的代码成功地计算出了大于给定整数且能被除数整除的最接近数。
摘要由CSDN通过智能技术生成

I need to get the nearest number $new to $orig divisible by $divisor. The $new should be greater than $orig. So I have come with the following formula (hopefully, there is no error in it):

$new = $orig + ($divisor - $orig % $divisor)

Now, the $orig number is an integer and has up to 30 digits. I want to implement this into a Perl function using Math::BigInt, but the output is quite wrong.

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

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

https://metacpan.org/pod/Math::BigInt#Arithmetic-methods : "These methods modify the invocand object and returns it." In other words, bmod, bsub, and badd are like %=, -=, and +=, not like %, -, and +.

So everywhere where you call one of the arithmetic methods, you should copy first, so the object you are currently calling the method on isn't changed:

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

(Also changed your test to do modulus, not division.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值