Perl: the Markov chain saw PerlMonks

by cpaplham (Initiate)
 on Feb 23, 2013 at 00:08 UTC Need Help??
cpaplham has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to add one hundred 50-digit numbers together. my code is basically:
 my $big_number = 37107287533902102798797998220837590246510135740250 + 46376937677490009712648124896970078050417018260538 + ...; [download] I have tried use bignum; but this didn't work. Whenever I print the value, I get an empty string. I only need about 15 digits of precision. Replies are listed 'Best First'. Re: Adding really big numbers by frozenwithjoy (Priest) on Feb 23, 2013 at 00:15 UTC Which version of Perl are you using? It works just fine for me on 5.16.1. Using bigint, I get values for every integer. Using bignum, I get values for every int and decimal (except for trailing zeroes). Without an extra module, anything over 20 digits defaults to scientific notation with 15 digits of precision. Update: Here is some info on largest positive/negative integers that can be nadled by different setups w/o using modules: Re: maximum value of a scalar I'm using 5.14. I agree, it's strange. Odd. Works fine for me on 5.14.2. What is your operating system? Re: Adding really big numbers by salva (Abbot) on Feb 23, 2013 at 09:33 UTC If you need to do that application wide, you can compile a custom perl that uses long doubles to represent floating point numbers. x86 long doubles have 80 bits and a resolution of 18 digits. Other architectures as SPARC use 128 bits for long doubles. Otherwise, you can use Math::LongDouble or Math::MPFR Re: Adding really big numbers by Khen1950fx (Canon) on Feb 23, 2013 at 01:58 UTC What lib are you using? How are you declaring accuracy and precision? Maybe this'll help: #!/usr/bin/perl -l BEGIN {$|  = 1;
$^W = 1; } use strict; use warnings; use bignum lib => 'FastCalc'; use Memoize; memoize('really_big'); really_big(); sub really_big { bignum->accuracy(15); bignum->precision(15); my$big_number = 3710728753390210279879799822083759024651013574025
+0
+ 46376937677490009712648124896970078050417018260538;
print \$big_number;
}
[download]
by ambrus (Abbot) on Mar 13, 2013 at 21:34 UTC

If you only need about 15 digits of precision, you don't need to do anything special. Perl's built in arithmetic will read the numbers as double-precision floats and add them that way, which gives about 15 digits of precision.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020253]
Front-paged by Arunbear
help
Chatterbox?
 [Corion]: thezip: If you want to confuse your users, use system(1, "that\\command" );, which will make Perl launch it in the background [Corion]: That will keep the console window open even though the user can't type into it anymore [thezip]: So I have a script that generates a log file. After script completion, I want tohave VIM open this logfile. [thezip]: i don't get the command line "back" until I close VIM. No what I want to happen... [thezip]: I currently don't have access to CYGWIN, else I'd just do a tail -f on the logfile. [Corion]: thezip: If you want to open vim and can live with opening a second console window, use start "The results" vim.exe c:\path\to\logfile .log [thezip]: Ooops... I lied. I guess Cygwin is back. I'll just do a tail -f instead. Better. Sorry for the noise.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2017-03-27 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (321 votes). Check out past polls.