| 1 | #!/usr2/local/bin/perl -w | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # PROGRAM: Math::FixedPrecision.pm # - 04/26/00 9:10:AM | ||||||||||||||||||||||||||||||||
| 4 | # PURPOSE: Perform precise decimal calculations without floating point errors | ||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 7 | # Copyright (c) 2001 John Peacock | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | # You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||||||
| 10 | # License or the Artistic License, as specified in the Perl README file, | ||||||||||||||||||||||||||||||||
| 11 | # with the exception that it cannot be placed on a CD-ROM or similar media | ||||||||||||||||||||||||||||||||
| 12 | # for commercial distribution without the prior approval of the author. | ||||||||||||||||||||||||||||||||
| 13 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 14 | 1 | eval 'exec /usr2/local/bin/perl -S $0 ${1+"$@"}' | |||||||||||||||||||||||||||||||
| 15 | if 0; | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | package Math::FixedPrecision; | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 1 | require 5.005_02; | |||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Math::BigFloat(1.27); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | @EXPORT_OK %EXPORT_TAGS $PACKAGE | ||||||||||||||||||||||||||||||||
| 27 | 1 | $accuracy $precision $round_mode $div_scale); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 1 | @ISA = qw(Exporter Math::BigFloat); | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||||||
| 32 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||||||
| 33 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # This allows declaration use Math::FixedPrecision ':all'; | ||||||||||||||||||||||||||||||||
| 36 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||||||
| 37 | # will save memory. | ||||||||||||||||||||||||||||||||
| 38 | 1 | %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | ) ] ); | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 1 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | @EXPORT = qw( | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||
| 47 | 1 | $VERSION = (qw$Revision: 2.1 $)[1]/10; | |||||||||||||||||||||||||||||||
| 48 | 1 | $PACKAGE = 'Math::FixedPrecision'; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # Globals | ||||||||||||||||||||||||||||||||
| 51 | 1 | $accuracy = $precision = undef; | |||||||||||||||||||||||||||||||
| 52 | 1 | $round_mode = 'even'; # Banker's rounding obviously | |||||||||||||||||||||||||||||||
| 53 | 1 | $div_scale = 40; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | # Preloaded methods go here. | ||||||||||||||||||||||||||||||||
| 56 | ############################################################################ | ||||||||||||||||||||||||||||||||
| 57 | sub new #04/20/00 12:08:PM | ||||||||||||||||||||||||||||||||
| 58 | ############################################################################ | ||||||||||||||||||||||||||||||||
| 59 | 49 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 60 | 49 | my $proto = shift; | |||||||||||||||||||||||||||||||
| 61 | 49 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||
| 62 | 49 | ▶ | my $parent = ref($proto) && $proto; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto && $proto
MC/DC0%0/2
ref $proto && $proto
ref $proto
$proto
Truth table33%1/3
A && B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 49 | ▶ | my $value = shift || 0; # Set to 0 if not provided | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 65 | 49 | my $decimal = shift; | |||||||||||||||||||||||||||||||
| 66 | 49 | my $radix = 0; | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | # Store the floating point value | ||||||||||||||||||||||||||||||||
| 69 | 49 | my $self = bless Math::BigFloat->new($value), $class; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # Normalize the number to 1234567.890 | ||||||||||||||||||||||||||||||||
| 72 | 49 | ▶ | if ( ( $radix = length($value) - index($value,'.') - 1 ) != length($value) ) # Already has a decimal | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||
| 74 | 32 | ▶ | if ( defined $decimal and $radix <= $decimal ) # higher precision overrides actual | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $decimal and $radix <= $decimal
MC/DC0%0/2
defined $decimal and $radix <= $decimal
defined $decimal
$radix <= $decimal
Truth table66%2/3
A and B
A= defined $decimalB= $radix <= $decimal
| |||||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||
| 76 | 0 | $radix = $decimal; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | elsif ( defined $decimal ) # Too many decimal places | ||||||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||||||
| 80 | 2 | $self = $self->ffround(-1 * $decimal); | |||||||||||||||||||||||||||||||
| 81 | 2 | $radix = undef; # force the use of the asserted decimal | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | else | ||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||
| 86 | 17 | $radix = undef; # infinite precision | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 49 | ▶ | if ( defined $radix ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||
| 91 | 30 | $self->{_p} = - $radix; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | elsif ( defined $decimal ) | ||||||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||||||
| 95 | 5 | $self->{_p} = - $decimal; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | else | ||||||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||||||
| 99 | 14 | $self->{_p} = undef; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 49 | return $self; | |||||||||||||||||||||||||||||||
| 103 | } ##new | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||||||