4 # This library is no longer being maintained, and is included for backward
5 # compatibility with Perl 4 programs which may require it.
7 # In particular, this should not be used as an example of modern Perl
8 # programming techniques.
10 # Suggested alternative: Math::BigFloat
12 # Arbitrary length float math package
17 # canonical strings have the form /[+-]\d+E[+-]\d+/
18 # Input values can have embedded whitespace
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))
25 # Also used for default sqrt scale
29 # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
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
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
52 s/\s+//g; # strip white space
53 if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
54 && ($2 ne '' || defined($4))) {
55 my $x = defined($4) ? $4 : '';
56 &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
62 # normalize number -- for internal use
63 sub norm { #(mantissa, exponent) return fnum_str
68 s/^([+-])0+/$1/; # strip leading zeros
69 if (length($_) == 1) {
72 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
73 sprintf("%sE%+ld", $_, $exp);
79 sub main'fneg { #(fnum_str) return fnum_str
80 local($_) = &'fnorm($_[$[]);
81 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
82 if ( ord("\t") == 9 ) { # ascii
85 else { # ebcdic character set
92 sub main'fabs { #(fnum_str) return fnum_str
93 local($_) = &'fnorm($_[$[]);
99 sub main'fmul { #(fnum_str, fnum_str) return fnum_str
100 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
101 if ($x eq 'NaN' || $y eq 'NaN') {
104 local($xm,$xe) = split('E',$x);
105 local($ym,$ye) = split('E',$y);
106 &norm(&'bmul($xm,$ym),$xe+$ye);
111 sub main'fadd { #(fnum_str, fnum_str) return fnum_str
112 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
113 if ($x eq 'NaN' || $y eq 'NaN') {
116 local($xm,$xe) = split('E',$x);
117 local($ym,$ye) = split('E',$y);
118 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
119 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
124 sub main'fsub { #(fnum_str, fnum_str) return fnum_str
125 &'fadd($_[$[],&'fneg($_[$[+1]));
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
133 local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
134 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
137 local($xm,$xe) = split('E',$x);
138 local($ym,$ye) = split('E',$y);
139 $scale = $div_scale if (!$scale);
140 $scale = length($xm)-1 if (length($xm)-1 > $scale);
141 $scale = length($ym)-1 if (length($ym)-1 > $scale);
142 $scale = $scale + length($ym) - length($xm);
143 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
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 local($q,$r,$base) = @_;
151 if ($q eq 'NaN' || $r eq 'NaN') {
153 } elsif ($rnd_mode eq 'trunc') {
156 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
159 ( $rnd_mode eq 'zero' ||
160 ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
161 ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
162 ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
163 ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
166 &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
172 # round the mantissa of $x to $scale digits
173 sub main'fround { #(fnum_str, scale) return fnum_str
174 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
175 if ($x eq 'NaN' || $scale <= 0) {
178 local($xm,$xe) = split('E',$x);
179 if (length($xm)-1 <= $scale) {
182 &norm(&round(substr($xm,$[,$scale+1),
183 "+0".substr($xm,$[+$scale+1,1),"+10"),
184 $xe+length($xm)-$scale-1);
189 # round $x at the 10 to the $scale digit place
190 sub main'ffround { #(fnum_str, scale) return fnum_str
191 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
195 local($xm,$xe) = split('E',$x);
199 $xe = length($xm)+$xe-$scale;
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
207 &norm(&round(substr($xm,$[,1).'0',
208 "+0".substr($xm,$[+1,1),"+10"), $scale);
210 &norm(&round(substr($xm,$[,$xe),
211 "+0".substr($xm,$[+$xe,1),"+10"), $scale);
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
221 local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
222 if ($x eq "NaN" || $y eq "NaN") {
227 ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
228 (($xe <=> $ye) * (substr($x,$[,1).'1')
229 || &bigint'cmp($xm,$ym))
234 # square root by Newtons method.
235 sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
236 local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
237 if ($x eq 'NaN' || $x =~ /^-/) {
239 } elsif ($x eq '+0E+0') {
242 local($xm, $xe) = split('E',$x);
243 $scale = $div_scale if (!$scale);
244 $scale = length($xm)-1 if ($scale < length($xm)-1);
245 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
246 while ($gs < 2*$scale) {
247 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
250 &'fround($guess, $scale);