4 # This library is no longer being maintained, and is included for backward
5 # compatibility with Perl 4 programs which may require it.
6 # This legacy library is deprecated and will be removed in a future
9 # In particular, this should not be used as an example of modern Perl
10 # programming techniques.
12 # Suggested alternative: Math::BigFloat
14 # Arbitrary length float math package
19 # canonical strings have the form /[+-]\d+E[+-]\d+/
20 # Input values can have embedded whitespace
22 # 'NaN' An input parameter was "Not a Number" or
23 # divide by zero or sqrt of negative number
24 # Division is computed to
25 # max($div_scale,length(dividend)+length(divisor))
27 # Also used for default sqrt scale
31 # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
37 # fadd(NSTR, NSTR) return NSTR addition
38 # fsub(NSTR, NSTR) return NSTR subtraction
39 # fmul(NSTR, NSTR) return NSTR multiplication
40 # fdiv(NSTR, NSTR[,SCALE]) returns NSTR division to SCALE places
41 # fneg(NSTR) return NSTR negation
42 # fabs(NSTR) return NSTR absolute value
43 # fcmp(NSTR,NSTR) return CODE compare undef,<0,=0,>0
44 # fround(NSTR, SCALE) return NSTR round to SCALE digits
45 # ffround(NSTR, SCALE) return NSTR round at SCALEth place
46 # fnorm(NSTR) return (NSTR) normalize
47 # fsqrt(NSTR[, SCALE]) return NSTR sqrt to SCALE places
49 # Convert a number to canonical string form.
50 # Takes something that looks like a number and converts it to
51 # the form /^[+-]\d+E[+-]\d+$/.
52 sub main'fnorm { #(string) return fnum_str
54 s/\s+//g; # strip white space
55 if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
56 && ($2 ne '' || defined($4))) {
57 my $x = defined($4) ? $4 : '';
58 &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
64 # normalize number -- for internal use
65 sub norm { #(mantissa, exponent) return fnum_str
70 s/^([+-])0+/$1/; # strip leading zeros
71 if (length($_) == 1) {
74 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
75 sprintf("%sE%+ld", $_, $exp);
81 sub main'fneg { #(fnum_str) return fnum_str
82 local($_) = &'fnorm($_[$[]);
83 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
84 if ( ord("\t") == 9 ) { # ascii
87 else { # ebcdic character set
94 sub main'fabs { #(fnum_str) return fnum_str
95 local($_) = &'fnorm($_[$[]);
101 sub main'fmul { #(fnum_str, fnum_str) return fnum_str
102 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
103 if ($x eq 'NaN' || $y eq 'NaN') {
106 local($xm,$xe) = split('E',$x);
107 local($ym,$ye) = split('E',$y);
108 &norm(&'bmul($xm,$ym),$xe+$ye);
113 sub main'fadd { #(fnum_str, fnum_str) return fnum_str
114 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
115 if ($x eq 'NaN' || $y eq 'NaN') {
118 local($xm,$xe) = split('E',$x);
119 local($ym,$ye) = split('E',$y);
120 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
121 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
126 sub main'fsub { #(fnum_str, fnum_str) return fnum_str
127 &'fadd($_[$[],&'fneg($_[$[+1]));
131 # args are dividend, divisor, scale (optional)
132 # result has at most max(scale, length(dividend), length(divisor)) digits
133 sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
135 local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
136 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
139 local($xm,$xe) = split('E',$x);
140 local($ym,$ye) = split('E',$y);
141 $scale = $div_scale if (!$scale);
142 $scale = length($xm)-1 if (length($xm)-1 > $scale);
143 $scale = length($ym)-1 if (length($ym)-1 > $scale);
144 $scale = $scale + length($ym) - length($xm);
145 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
150 # round int $q based on fraction $r/$base using $rnd_mode
151 sub round { #(int_str, int_str, int_str) return int_str
152 local($q,$r,$base) = @_;
153 if ($q eq 'NaN' || $r eq 'NaN') {
155 } elsif ($rnd_mode eq 'trunc') {
158 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
161 ( $rnd_mode eq 'zero' ||
162 ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
163 ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
164 ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
165 ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
168 &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
174 # round the mantissa of $x to $scale digits
175 sub main'fround { #(fnum_str, scale) return fnum_str
176 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
177 if ($x eq 'NaN' || $scale <= 0) {
180 local($xm,$xe) = split('E',$x);
181 if (length($xm)-1 <= $scale) {
184 &norm(&round(substr($xm,$[,$scale+1),
185 "+0".substr($xm,$[+$scale+1,1),"+10"),
186 $xe+length($xm)-$scale-1);
191 # round $x at the 10 to the $scale digit place
192 sub main'ffround { #(fnum_str, scale) return fnum_str
193 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
197 local($xm,$xe) = split('E',$x);
201 $xe = length($xm)+$xe-$scale;
205 # The first substr preserves the sign, which means that
206 # we'll pass a non-normalized "-0" to &round when rounding
207 # -0.006 (for example), purely so that &round won't lose
209 &norm(&round(substr($xm,$[,1).'0',
210 "+0".substr($xm,$[+1,1),"+10"), $scale);
212 &norm(&round(substr($xm,$[,$xe),
213 "+0".substr($xm,$[+$xe,1),"+10"), $scale);
219 # compare 2 values returns one of undef, <0, =0, >0
220 # returns undef if either or both input value are not numbers
221 sub main'fcmp #(fnum_str, fnum_str) return cond_code
223 local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
224 if ($x eq "NaN" || $y eq "NaN") {
229 ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
230 (($xe <=> $ye) * (substr($x,$[,1).'1')
231 || &bigint'cmp($xm,$ym))
236 # square root by Newtons method.
237 sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
238 local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
239 if ($x eq 'NaN' || $x =~ /^-/) {
241 } elsif ($x eq '+0E+0') {
244 local($xm, $xe) = split('E',$x);
245 $scale = $div_scale if (!$scale);
246 $scale = length($xm)-1 if ($scale < length($xm)-1);
247 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
248 while ($gs < 2*$scale) {
249 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
252 &'fround($guess, $scale);