Performing math operations on very large numbers in Perl

我与影子孤独终老i 提交于 2019-12-19 04:04:22

问题


I have a case where some values in a data file have 64 bit wrap around which makes them very large, like, 18446744073709551608.

So I have to perform a subtraction from 2^64. I tried this using the simple

2^64 - 18446744073709551608

But I guess this number is too large and don't get the actual answer 8. What do I need to do to perform this substraction.


回答1:


Check out the bignum pragma:

use bignum;

print 2**64 - 18446744073709551608;

This should properly print 8.




回答2:


Note that bignum is just a layer that makes all constant numbers automatically Math::BigFloat or Math::BigInt objects. If you only want this for some numbers, you can either specify the use bignum; in a restricted scope, add no bignum; in places, or explicitly use Math::BigFloat->new('your constant') (or BigInt) to make particular numbers and the results of any operations involving them big.




回答3:


use bignum ;

print 2**64 - 18446744073709551608 ,"\n" ;

in perl language , ^ = ** , mod = % good luck !



来源:https://stackoverflow.com/questions/4620054/performing-math-operations-on-very-large-numbers-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!