File Coverage

blib/lib/bigfloat.pl
Criterion Covered Total %
statement 84 88 95.4
branch 55 62 88.7
condition 34 42 80.9
subroutine 13 13 100.0
pod n/a
total 186 205 90.7


line stmt bran cond sub pod time code
1             package bigfloat;
2             require "bigint.pl";
3             #
4             # This library is no longer being maintained, and is included for backward
5             # compatibility with Perl 4 programs which may require it.
6             #
7             # In particular, this should not be used as an example of modern Perl
8             # programming techniques.
9             #
10             # Suggested alternative: Math::BigFloat
11             #
12             # Arbitrary length float math package
13             #
14             # by Mark Biggar
15             #
16             # number format
17             # canonical strings have the form /[+-]\d+E[+-]\d+/
18             # Input values can have embedded whitespace
19             # Error returns
20             # 'NaN' An input parameter was "Not a Number" or
21             # divide by zero or sqrt of negative number
22             # Division is computed to
23             # max($div_scale,length(dividend)+length(divisor))
24             # digits by default.
25             # Also used for default sqrt scale
26              
27             $div_scale = 40;
28              
29             # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
30              
31             $rnd_mode = 'even';
32              
33             # bigfloat routines
34             #
35             # fadd(NSTR, NSTR) return NSTR addition
36             # fsub(NSTR, NSTR) return NSTR subtraction
37             # fmul(NSTR, NSTR) return NSTR multiplication
38             # fdiv(NSTR, NSTR[,SCALE]) returns NSTR division to SCALE places
39             # fneg(NSTR) return NSTR negation
40             # fabs(NSTR) return NSTR absolute value
41             # fcmp(NSTR,NSTR) return CODE compare undef,<0,=0,>0
42             # fround(NSTR, SCALE) return NSTR round to SCALE digits
43             # ffround(NSTR, SCALE) return NSTR round at SCALEth place
44             # fnorm(NSTR) return (NSTR) normalize
45             # fsqrt(NSTR[, SCALE]) return NSTR sqrt to SCALE places
46            
47             # Convert a number to canonical string form.
48             # Takes something that looks like a number and converts it to
49             # the form /^[+-]\d+E[+-]\d+$/.
50             sub main'fnorm { #(string) return fnum_str
51 914     914   31216 local($_) = @_;
52 914         2294 s/\s+//g; # strip white space
53 914 100 66     7986 if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
      66        
54             && ($2 ne '' || defined($4))) {
55 883 100       2669 my $x = defined($4) ? $4 : '';
56 883 100       4813 &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
    100          
57             } else {
58 31         166 'NaN';
59             }
60             }
61              
62             # normalize number -- for internal use
63             sub norm { #(mantissa, exponent) return fnum_str
64 1292     1292   3856 local($_, $exp) = @_;
65 1292 50       2998 if ($_ eq 'NaN') {
66 0         0 'NaN';
67             } else {
68 1292         2927 s/^([+-])0+/$1/; # strip leading zeros
69 1292 100       2851 if (length($_) == 1) {
70 70         489 '+0E+0';
71             } else {
72 1222 100       4130 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
73 1222         9784 sprintf("%sE%+ld", $_, $exp);
74             }
75             }
76             }
77              
78             # negation
79             sub main'fneg { #(fnum_str) return fnum_str
80 45     45   6698 local($_) = &'fnorm($_[0]);
81 45 100       261 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
82 45         117 if ( ord("\t") == 9 ) { # ascii
83 45         121 s/^H/N/;
84             }
85             else { # ebcdic character set
86             s/\373/N/;
87             }
88 45         229 $_;
89             }
90              
91             # absolute value
92             sub main'fabs { #(fnum_str) return fnum_str
93 8     8   6280 local($_) = &'fnorm($_[0]);
94 8         31 s/^-/+/; # mash sign
95 8         89 $_;
96             }
97              
98             # multiplication
99             sub main'fmul { #(fnum_str, fnum_str) return fnum_str
100 88     88   26544 local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1]));
101 88 100 100     434 if ($x eq 'NaN' || $y eq 'NaN') {
102 3         33 'NaN';
103             } else {
104 85         317 local($xm,$xe) = split('E',$x);
105 85         257 local($ym,$ye) = split('E',$y);
106 85         295 &norm(&'bmul($xm,$ym),$xe+$ye);
107             }
108             }
109            
110             # addition
111             sub main'fadd { #(fnum_str, fnum_str) return fnum_str
112 130     130   25333 local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1]));
113 130 100 100     594 if ($x eq 'NaN' || $y eq 'NaN') {
114 6         70 'NaN';
115             } else {
116 124         446 local($xm,$xe) = split('E',$x);
117 124         370 local($ym,$ye) = split('E',$y);
118 124 50       387 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
119 124         560 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
120             }
121             }
122              
123             # subtraction
124             sub main'fsub { #(fnum_str, fnum_str) return fnum_str
125 37     37   29958 &'fadd($_[0],&'fneg($_[1]));
126             }
127              
128             # division
129             # args are dividend, divisor, scale (optional)
130             # result has at most max(scale, length(dividend), length(divisor)) digits
131             sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
132             {
133 106     106   40553 local($x,$y,$scale) = (&'fnorm($_[0]),&'fnorm($_[1]),$_[2]);
134 106 100 100     635 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
      100        
135 6         68 'NaN';
136             } else {
137 100         361 local($xm,$xe) = split('E',$x);
138 100         324 local($ym,$ye) = split('E',$y);
139 100 100       274 $scale = $div_scale if (!$scale);
140 100 100       275 $scale = length($xm)-1 if (length($xm)-1 > $scale);
141 100 100       232 $scale = length($ym)-1 if (length($ym)-1 > $scale);
142 100         195 $scale = $scale + length($ym) - length($xm);
143 100         513 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
144             $xe-$ye-$scale);
145             }
146             }
147            
148             # round int $q based on fraction $r/$base using $rnd_mode
149             sub round { #(int_str, int_str, int_str) return int_str
150 200     200   640 local($q,$r,$base) = @_;
151 200 50 33     986 if ($q eq 'NaN' || $r eq 'NaN') {
    100          
152 0         0 'NaN';
153             } elsif ($rnd_mode eq 'trunc') {
154 15         74 $q; # just truncate
155             } else {
156 185         528 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
157 185 100 100     971 if ( $cmp < 0 ||
      100        
      100        
158             ($cmp == 0 &&
159             ( $rnd_mode eq 'zero' ||
160             ($rnd_mode eq '-inf' && (substr($q,0,1) eq '+')) ||
161             ($rnd_mode eq '+inf' && (substr($q,0,1) eq '-')) ||
162             ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
163             ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
164 117         551 $q; # round down
165             } else {
166 68 100       283 &'badd($q, ((substr($q,0,1) eq '-') ? '-1' : '+1'));
167             # round up
168             }
169             }
170             }
171              
172             # round the mantissa of $x to $scale digits
173             sub main'fround { #(fnum_str, scale) return fnum_str
174 44     44   28432 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
175 44 50 33     255 if ($x eq 'NaN' || $scale <= 0) {
176 0         0 $x;
177             } else {
178 44         191 local($xm,$xe) = split('E',$x);
179 44 100       133 if (length($xm)-1 <= $scale) {
180 3         45 $x;
181             } else {
182 41         222 &norm(&round(substr($xm,0,$scale+1),
183             "+0".substr($xm,$scale+1,1),"+10"),
184             $xe+length($xm)-$scale-1);
185             }
186             }
187             }
188            
189             # round $x at the 10 to the $scale digit place
190             sub main'ffround { #(fnum_str, scale) return fnum_str
191 75     75   41371 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
192 75 50       175 if ($x eq 'NaN') {
193 0         0 'NaN';
194             } else {
195 75         203 local($xm,$xe) = split('E',$x);
196 75 100       212 if ($xe >= $scale) {
197 10         85 $x;
198             } else {
199 65         117 $xe = length($xm)+$xe-$scale;
200 65 100       145 if ($xe < 1) {
    100          
201 6         51 '+0E+0';
202             } elsif ($xe == 1) {
203             # The first substr preserves the sign, which means that
204             # we'll pass a non-normalized "-0" to &round when rounding
205             # -0.006 (for example), purely so that &round won't lose
206             # the sign.
207 6         24 &norm(&round(substr($xm,0,1).'0',
208             "+0".substr($xm,1,1),"+10"), $scale);
209             } else {
210 53         199 &norm(&round(substr($xm,0,$xe),
211             "+0".substr($xm,$xe,1),"+10"), $scale);
212             }
213             }
214             }
215             }
216            
217             # compare 2 values returns one of undef, <0, =0, >0
218             # returns undef if either or both input value are not numbers
219             sub main'fcmp #(fnum_str, fnum_str) return cond_code
220             {
221 22     22   14124 local($x, $y) = (&'fnorm($_[0]),&'fnorm($_[1]));
222 22 100 100     115 if ($x eq "NaN" || $y eq "NaN") {
223 3         21 undef;
224             } else {
225 19 100 66     203 ord($y) <=> ord($x)
226             ||
227             ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
228             (($xe <=> $ye) * (substr($x,0,1).'1')
229             || &bigint'cmp($xm,$ym))
230             );
231             }
232             }
233            
234             # square root by Newtons method.
235             sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
236 13     13   10562 local($x, $scale) = (&'fnorm($_[0]), $_[1]);
237 13 100 66     98 if ($x eq 'NaN' || $x =~ /^-/) {
    100          
238 4         43 'NaN';
239             } elsif ($x eq '+0E+0') {
240 1         11 '+0E+0';
241             } else {
242 8         36 local($xm, $xe) = split('E',$x);
243 8 50       30 $scale = $div_scale if (!$scale);
244 8 50       27 $scale = length($xm)-1 if ($scale < length($xm)-1);
245 8         53 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
246 8         29 while ($gs < 2*$scale) {
247 56         133 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
248 56         188 $gs *= 2;
249             }
250 8         28 &'fround($guess, $scale);
251             }
252             }
253              
254             1;