I just (more fully) realized how easy it is to do calculations with huge numbers in Perl so long as you don't have some unreasonable desire for a huge number of digits of precision (I settled for 10, which I think is usually too many anyway).

I threw this module prototype together in just a few minutes. Support for negative numbers and even zero is left as an exercise for the next round, however. It works surprisingly well (and fast). It is great for computing things like how many combinations / permutations some unreasonably large set of things has.

package Math::BigPositiveOkayPrecision; use overload '+' => \&add, #'-' => \&sub, '*' => \&mul, '/' => \&div, '**' => \&pow, '^' => \&prd, # $x^$y = factorial($y)/factorial($x) '<<' => \&shl, '>>' => \&shr, '<=>'=> \&cmp, '!' => \&fct, # !$x = factorial($x) '0+' => \&num, '""' => \&str, #'neg'=> \&neg, ; sub new { my $us= shift @_; if( ! @_ ) { die "new() needs an object or a value" if ! ref $us; return bless \( 0+$$us ); } my $val= shift @_; die "Non-positive values not supported yet ($val)" if $val <= 0; return bless \log( $val ); } *Okay::new= \&new; # Until we get a better module name sub mul { my( $x, $y )= @_; $y= $x->new( $y ) if ! ref $y; return bless \( $$x + $$y ); } sub div { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return bless \( $$x - $$y ); } sub pow { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return bless \( $$x * exp($$y) ); } sub add { my( $x, $y )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $$y < $$x; return bless \( $$y + log( 1 + exp( $$x - $$y ) ) ); } sub shl { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return bless \( $$x + exp($$y)*log(2) ); } sub shr { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return bless \( $$x - exp($$y)*log(2) ); } sub cmp { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return $$x <=> $$y; } sub prd { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return bless \0 if $$y < $$x; my $p= $x->new(); my $m= $x + 1; while( $m <= $y ) { $p= $p * $m; $m= $m + 1; } return $p; } sub fct { my( $x )= @_; return 1^$x; } sub num { my( $x )= @_; return exp( $$x ); } sub str { my( $x )= @_; my $exp= int( $$x / log(10) ); my $mant= exp( $$x - log(10)*$exp ); $mant= sprintf "%.10f", $mant; $mant .= $exp ? "e" . $exp : ""; return $mant if 9 < $exp; return 0 + $mant; } __PACKAGE__;

See Re^6: One Zero variants_without_repetition (logue) for a sample use and the most recent part of my inspiration for this.

- tye        


In reply to Math::BigPositiveOkayPrecision prototype by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.