How does Perl store / process very large numbers? Should I use a module instead of the default Perl processing method?

I needed to add 50-digit numbers, so I considered them as β€œstrings” and wrote my own functions to combine them. Subsequently, to hell with this, I tried this:

readFile(shift (@ARGV)); sub readFile { my $file = shift; #contains a bunch of 50-digit numbers my $result = 0; open (my $inFile, $file); while (<$inFile>) { chomp; $result += $_; } print $result; } 

and to my surprise it worked. I do not understand. In any other language I have ever used, you will have to use some special variable for this. Does Perl automatically detect that you have a very large number and process it accordingly? If so, if it is known in advance that they will deal with very large numbers, is there a Perl module that is more efficient than Perl can handle them by default?

Thanks in advance.

+3
source share
1 answer

Perl will handle them correctly, but as double precision floating point values, so your result will not have about fifty precision digits. However, you can use bigint pragma to get transparent handling of a large number: just insert use bigint; into your code. (This obviously won't work as well as floating point math, but it does hit to manipulate the strings on its own.)

+10
source

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


All Articles