line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
eval 'exec /usr2/local/bin/perl -S $0 ${1+"$@"}'
|
15
|
|
|
|
|
|
|
if 0;
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
package Math::FixedPrecision;
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
require 5.005_02;
|
20
|
1
|
|
|
1
|
|
7726
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
6
|
use Exporter;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
23
|
1
|
|
|
1
|
|
17896
|
use Math::BigFloat(1.27);
|
|
1
|
|
|
|
|
32598
|
|
|
1
|
|
|
|
|
5
|
|
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
476
|
use vars qw($VERSION @ISA @EXPORT
|
26
|
|
|
|
|
|
|
@EXPORT_OK %EXPORT_TAGS $PACKAGE
|
27
|
1
|
|
|
1
|
|
138581
|
$accuracy $precision $round_mode $div_scale);
|
|
1
|
|
|
|
|
2
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
%EXPORT_TAGS = ( 'all' => [ qw(
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
) ] );
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
@EXPORT = qw(
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
);
|
47
|
|
|
|
|
|
|
$VERSION = (qw$Revision: 2.1 $)[1]/10;
|
48
|
|
|
|
|
|
|
$PACKAGE = 'Math::FixedPrecision';
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Globals
|
51
|
|
|
|
|
|
|
$accuracy = $precision = undef;
|
52
|
|
|
|
|
|
|
$round_mode = 'even'; # Banker's rounding obviously
|
53
|
|
|
|
|
|
|
$div_scale = 40;
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Preloaded methods go here.
|
56
|
|
|
|
|
|
|
############################################################################
|
57
|
|
|
|
|
|
|
sub new #04/20/00 12:08:PM
|
58
|
|
|
|
|
|
|
############################################################################
|
59
|
|
|
|
|
|
|
{
|
60
|
49
|
|
|
49
|
1
|
15398
|
my $proto = shift;
|
61
|
49
|
|
33
|
|
|
187
|
my $class = ref($proto) || $proto;
|
62
|
49
|
|
33
|
|
|
110
|
my $parent = ref($proto) && $proto;
|
63
|
|
|
|
|
|
|
|
64
|
49
|
|
100
|
|
|
103
|
my $value = shift || 0; # Set to 0 if not provided
|
65
|
49
|
|
|
|
|
48
|
my $decimal = shift;
|
66
|
49
|
|
|
|
|
52
|
my $radix = 0;
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Store the floating point value
|
69
|
49
|
|
|
|
|
143
|
my $self = bless Math::BigFloat->new($value), $class;
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Normalize the number to 1234567.890
|
72
|
49
|
100
|
|
|
|
6527
|
if ( ( $radix = length($value) - index($value,'.') - 1 ) != length($value) ) # Already has a decimal
|
73
|
|
|
|
|
|
|
{
|
74
|
32
|
50
|
66
|
|
|
117
|
if ( defined $decimal and $radix <= $decimal ) # higher precision overrides actual
|
|
|
100
|
|
|
|
|
|
75
|
|
|
|
|
|
|
{
|
76
|
0
|
|
|
|
|
0
|
$radix = $decimal;
|
77
|
|
|
|
|
|
|
}
|
78
|
|
|
|
|
|
|
elsif ( defined $decimal ) # Too many decimal places
|
79
|
|
|
|
|
|
|
{
|
80
|
2
|
|
|
|
|
45
|
$self = $self->ffround(-1 * $decimal);
|
81
|
2
|
|
|
|
|
537
|
$radix = undef; # force the use of the asserted decimal
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
}
|
84
|
|
|
|
|
|
|
else
|
85
|
|
|
|
|
|
|
{
|
86
|
17
|
|
|
|
|
21
|
$radix = undef; # infinite precision
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
|
89
|
49
|
100
|
|
|
|
96
|
if ( defined $radix )
|
|
|
100
|
|
|
|
|
|
90
|
|
|
|
|
|
|
{
|
91
|
30
|
|
|
|
|
158
|
$self->{_p} = - $radix;
|
92
|
|
|
|
|
|
|
}
|
93
|
|
|
|
|
|
|
elsif ( defined $decimal )
|
94
|
|
|
|
|
|
|
{
|
95
|
5
|
|
|
|
|
9
|
$self->{_p} = - $decimal;
|
96
|
|
|
|
|
|
|
}
|
97
|
|
|
|
|
|
|
else
|
98
|
|
|
|
|
|
|
{
|
99
|
14
|
|
|
|
|
20
|
$self->{_p} = undef;
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
49
|
|
|
|
|
148
|
return $self;
|
103
|
|
|
|
|
|
|
} ##new
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program.
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1;
|
108
|
|
|
|
|
|
|
__END__
|