Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > processing large numbers/values/figures

Reply
Thread Tools

processing large numbers/values/figures

 
 
Sherm Pendley
Guest
Posts: n/a
 
      07-10-2006
Lukas Ruf <(E-Mail Removed)> writes:

> How can I deal with large numbers in Perl?


Have a look at the Math::BigInt, Math::BigFloat, and Math::BigRat modules,
depending on the kind of numbers you're working with. All three are core.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
 
Reply With Quote
 
 
 
 
Lukas Ruf
Guest
Posts: n/a
 
      07-10-2006
Dear all,

for a large number of files, I must accumulate numbers found
therein. For this accumulation, I have been running into
number-overruns.

As a beginner with Perl, I have been able to 'split()' lines
into arrays of numeric-strings. However, when adding fields
together, number-overruns do happen.

After searching the web for a while (maybe with the wrong
keywords) without success, I kindly ask this mailing list
for help:

How can I deal with large numbers in Perl?

- adding
- subtracting
- multiplying
- dividing
- printing

+ large in the range up to 10*2^^40 (10 Tera).
+ most important: adding and printing to ASCII

Thanks in advance for any help!

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-10-2006
Lukas Ruf <(E-Mail Removed)> wrote:
> Dear all,
>
> for a large number of files, I must accumulate numbers found
> therein. For this accumulation, I have been running into
> number-overruns.


I don't get overruns until 10**307. Are you sure it is an overrun
rather than just a loss of precision?

>
> As a beginner with Perl, I have been able to 'split()' lines
> into arrays of numeric-strings. However, when adding fields
> together, number-overruns do happen.
>
> After searching the web for a while (maybe with the wrong
> keywords) without success, I kindly ask this mailing list
> for help:
>
> How can I deal with large numbers in Perl?
>
> - adding
> - subtracting
> - multiplying
> - dividing
> - printing
>
> + large in the range up to 10*2^^40 (10 Tera).


On my 32 bit machine, I get unit-level precision up to over 1000*2**40.

> + most important: adding and printing to ASCII


Aside from the bignum and other modules previously mentioned, have you
looked at using printf with a higher precision format? Maybe you are
losing precision at the print stage rather than the internal
representation.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Lukas Ruf
Guest
Posts: n/a
 
      07-10-2006
> A. Sinan Unur [Mon, 10 Jul 2006 18:30:19 GMT]:
>
> Lukas Ruf <(E-Mail Removed)> wrote in news:slrneb55hc.kbb.ruf@pc-
> 4082.ethz.ch:
>
> > How can I deal with large numbers in Perl?
> >
> > - adding
> > - subtracting
> > - multiplying
> > - dividing
> > - printing
> >
> > + large in the range up to 10*2^^40 (10 Tera).
> > + most important: adding and printing to ASCII

>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use bignum;
>
> my $x = 10*(2**40);
> print 123.55 * $x, "\n";
>


thanks very much!

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
Lukas Ruf
Guest
Posts: n/a
 
      07-10-2006
> Sherm Pendley [Mon, 10 Jul 2006 14:09:10 -0400]:
>
> Lukas Ruf <(E-Mail Removed)> writes:
>
> > How can I deal with large numbers in Perl?

>
> Have a look at the Math::BigInt, Math::BigFloat, and Math::BigRat
> modules, depending on the kind of numbers you're working with. All
> three are core.
>


thank you very much!

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
Lukas Ruf
Guest
Posts: n/a
 
      07-11-2006
> http://www.velocityreviews.com/forums/(E-Mail Removed) [10 Jul 2006 20:21:24 GMT]:
>
> Lukas Ruf <(E-Mail Removed)> wrote:
> > Dear all,
> >
> > for a large number of files, I must accumulate numbers found
> > therein. For this accumulation, I have been running into
> > number-overruns.

>
> I don't get overruns until 10**307. Are you sure it is an overrun
> rather than just a loss of precision?
>


my problem is, after a certain value, it turns to -1.

> > + large in the range up to 10*2^^40 (10 Tera).

>
> On my 32 bit machine, I get unit-level precision up to over 1000*2**40.
>


thanks for checking.

> > + most important: adding and printing to ASCII

>
> Aside from the bignum and other modules previously mentioned, have you
> looked at using printf with a higher precision format? Maybe you are
> losing precision at the print stage rather than the internal
> representation.
>


I make use of Perl's hashes

my %hsh = ();

$hsh{$key} += @record{$index};

printf("%15d should be in the range up to 2**34\n",
$hsh{$key});


Any hint what goes wrong?

Thanks in advance.

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
Lukas Ruf
Guest
Posts: n/a
 
      07-11-2006
> Abigail [10 Jul 2006 22:26:45 GMT]:
>


thanks for answering,

> Lukas Ruf ((E-Mail Removed)) wrote on MMMMDCXCVI September MCMXCIII in
> <URL:news:(E-Mail Removed)>:


>
> A Perl using 32-bit integers will automatically convert integers to
> floats. If you're dealing with numbers up to about 2**53, there shouldn't
> be any loss of precision.
>


weird, why do I still get '-1' even when using 'bignum'?

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
Lukas Ruf
Guest
Posts: n/a
 
      07-11-2006
> Lukas Ruf [11 Jul 2006 08:28:18 +0100]:
>
> > (E-Mail Removed) [10 Jul 2006 20:21:24 GMT]:
> >
> > Aside from the bignum and other modules previously mentioned, have you
> > looked at using printf with a higher precision format? Maybe you are
> > losing precision at the print stage rather than the internal
> > representation.
> >

>
> I make use of Perl's hashes
>
> my %hsh = ();
>
> $hsh{$key} += @record{$index};
>
> printf("%15d should be in the range up to 2**34\n",
> $hsh{$key});
>
>
> Any hint what goes wrong?
>


according to your hint, I tried the following:

printf(tot_file "%20s $tot_key\n", $tot_val);
printf(tot_file "%20s %20llu\n", $tot_val, $tot_key);

with the following result:

==> byte_total_stat.txt <==
byte_total 156999049890
byte_total 4294967295

where the former seems to be reasonable while the later is 2**32-1

Thus my question:
Is there an easy way to format printouts with numbers of that size?
I.e. I would like to specify anything like '%20llu' that allows
for printing of the correct values.

Thanks in advance for any hint!

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
<http://lists.lpr.ch/muttprint> | muttprint mailing list
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      07-11-2006

"Lukas Ruf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > Abigail [10 Jul 2006 22:26:45 GMT]:
> >

>
> thanks for answering,
>
> > Lukas Ruf ((E-Mail Removed)) wrote on MMMMDCXCVI September MCMXCIII in
> > <URL:news:(E-Mail Removed)>:

>
> >
> > A Perl using 32-bit integers will automatically convert integers to
> > floats. If you're dealing with numbers up to about 2**53, there

shouldn't
> > be any loss of precision.
> >

>
> weird, why do I still get '-1' even when using 'bignum'?
>


Checkout 'perldoc -f sprintf' (which applies equally to the 'printf'
function). There you'll find:

%d a signed integer, in decimal

Specifying "%d" is good for numbers up to 31 bits (plus the 'sign' bit) only
on 32-bit systems (since that's the size of a signed integer). For larger
numbers up to 2 ** 53 use "%f" (or "%.0f" if you want to avoid the decimal
point) .... or you may find that simply using the 'print' function instead
of the 'printf' function produces the output you want.

It would probably help us to answer your questions about specific instances
if you could provide a (*minimal*) script (copy'n'paste) that demonstrate
the problem(s) - as opposed to code snippets and general descriptions.

As a starter - does the output of the following script fail in some way to
satisfy your requirements ?

---------------------------
use warnings;
use bignum;

$x = 2 ** 57 + 20345;
print $x, "\n";
__END__
---------------------------

Cheers,
Rob


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-11-2006
Lukas Ruf <(E-Mail Removed)> wrote:

> $hsh{$key} += @record{$index};



You should always enable warnings when developing Perl code.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
processing a Very Large file DJTB Python 4 05-18-2005 12:38 PM
Post-Processing RAW vs Post-Processing TIFF Mike Henley Digital Photography 42 01-30-2005 08:26 AM
Question: processing HTML, re-write default processing action of many tags Hubert Hung-Hsien Chang Python 2 09-17-2004 03:10 PM
Re: large file processing Thomas Matthews C++ 0 08-29-2003 04:15 PM
Processing file input for large files[100+ MB] - Performance suggestions? Maxim ASP .Net 0 07-07-2003 05:31 AM



Advertisments