in reply to Math::BigPositiveOkayPrecision prototype

Ahh, I understand now. Quite simple, yet very effective. I'm a mechanical engineer, so I find the your problem interesting. One suggestion that I would make (although it may not help that much) would be to make the individual operation methods simpler by extracting their common code to another method. For instance, the beginning portions of mul, div, pow, add, shl, shr, cmp, prd are all practically identical. You could write:

sub compute { my( $opsub, $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return $opsub->($x,$y); }

This would allow you to simplify the operation methods in a manner similar to that below.

sub mul { my $mulsub = sub { bless \( $$_[0] + $$_[1] ) }; return &compute($mulsub,@_); }

This may have adverse effects on performance since it increases the number of function calls, but I'm not experienced enough to know how much. However, it might make your code easier to understand and maintain.

Replies are listed 'Best First'.
Re^2: Math::BigPositiveOkayPrecision prototype (refactored)
by tye (Sage) on Aug 08, 2007 at 21:34 UTC

    Thanks. I've added:

    sub __args { my( $x, $y, $rev )= @_; $y= $x->new( $y ) if ! ref $y; ( $x, $y )= ( $y, $x ) if $rev; return( $x, $y ); }

    So that boiler-plate reduces to: my( $x, $y )= __args( @_ ); in 6 places.

    - tye