Performing mathematical operations on very large quantities in Perl

I have a case where some values ​​in a data file wrap 64 bits, which makes them very large, for example 18446744073709551608 .

So I need to do a subtraction from 2 ^ 64. I tried this using a simple

 2^64 - 18446744073709551608 

But I think this number is too large and does not receive an actual answer. 8. What do I need to do to perform this substitution.

+4
source share
3 answers

Check out bignum pragma:

 use bignum; print 2**64 - 18446744073709551608; 

This should print correctly 8 .

+10
source

Note that bignum is just a layer that automatically creates all the constant numbers of Math :: BigFloat or Math :: BigInt objects. If you want only this for some numbers, you can specify use bignum; in a limited area, add no bignum; in places or explicitly use Math::BigFloat->new('your constant') (or BigInt) to create specific numbers and the results of any operations associated with them are big.

+4
source
 use bignum ; print 2**64 - 18446744073709551608 ,"\n" ; 

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

0
source

Source: https://habr.com/ru/post/1334578/


All Articles