4 # Arbitrary length float math package
7 # canonical strings have the form /[+-]\d+E[+-]\d+/
8 # Input values can have inbedded whitespace
10 # 'NaN' An input parameter was "Not a Number" or
11 # divide by zero or sqrt of negative number
12 # Division is computed to
13 # max($div_scale,length(dividend).length(divisor))
15 # Also used for default sqrt scale
19 # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
25 # fadd(NSTR, NSTR) return NSTR addition
26 # fsub(NSTR, NSTR) return NSTR subtraction
27 # fmul(NSTR, NSTR) return NSTR multiplication
28 # fdiv(NSTR, NSTR[,SCALE]) returns NSTR division to SCALE places
29 # fneg(NSTR) return NSTR negation
30 # fabs(NSTR) return NSTR absolute value
31 # fcmp(NSTR,NSTR) return CODE compare undef,<0,=0,>0
32 # fround(NSTR, SCALE) return NSTR round to SCALE digits
33 # ffround(NSTR, SCALE) return NSTR round at SCALEth place
34 # fnorm(NSTR) return (NSTR) normalize
35 # fsqrt(NSTR[, SCALE]) return NSTR sqrt to SCALE places
37 # Convert a number to canonical string form.
38 # Takes something that looks like a number and converts it to
39 # the form /^[+-]\d+E[+-]\d+$/.
40 sub main'fnorm { #(string) return fnum_str
42 s/\s+//g; # strip white space
43 if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/ && "$2$4" ne '') {
44 &norm(($1 ? "$1$2$4" : "+$2$4"),(($4 ne '') ? $6-length($4) : $6));
50 # normalize number -- for internal use
51 sub norm { #(mantissa, exponent) return fnum_str
56 s/^([+-])0+/$1/; # strip leading zeros
57 if (length($_) == 1) {
60 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
61 sprintf("%sE%+ld", $_, $exp);
67 sub main'fneg { #(fnum_str) return fnum_str
68 local($_) = &'fnorm($_[0]);
69 substr($_,0,1) =~ tr/+-/-+/ if ($_ ne '+0E+0'); # flip sign
74 sub main'fabs { #(fnum_str) return fnum_str
75 local($_) = &'fnorm($_[0]);
76 substr($_,0,1) = '+' unless $_ eq 'NaN'; # mash sign
81 sub main'fmul { #(fnum_str, fnum_str) return fnum_str
82 local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1]));
83 if ($x eq 'NaN' || $y eq 'NaN') {
86 local($xm,$xe) = split('E',$x);
87 local($ym,$ye) = split('E',$y);
88 &norm(&'bmul($xm,$ym),$xe+$ye);
93 sub main'fadd { #(fnum_str, fnum_str) return fnum_str
94 local($x,$y) = (&'fnorm($_[0]),&'fnorm($_[1]));
95 if ($x eq 'NaN' || $y eq 'NaN') {
98 local($xm,$xe) = split('E',$x);
99 local($ym,$ye) = split('E',$y);
100 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
101 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
106 sub main'fsub { #(fnum_str, fnum_str) return fnum_str
107 &'fadd($_[0],&'fneg($_[1]));
111 # args are dividend, divisor, scale (optional)
112 # result has at most max(scale, length(dividend), length(divisor)) digits
113 sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
115 local($x,$y,$scale) = (&'fnorm($_[0]),&'fnorm($_[1]),$_[2]);
116 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
119 local($xm,$xe) = split('E',$x);
120 local($ym,$ye) = split('E',$y);
121 $scale = $div_scale if (!$scale);
122 $scale = length($xm)-1 if (length($xm)-1 > $scale);
123 $scale = length($ym)-1 if (length($ym)-1 > $scale);
124 $scale = $scale + length($ym) - length($xm);
125 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),$ym),
130 # round int $q based on fraction $r/$base using $rnd_mode
131 sub round { #(int_str, int_str, int_str) return int_str
132 local($q,$r,$base) = @_;
133 if ($q eq 'NaN' || $r eq 'NaN') {
135 } elsif ($rnd_mode eq 'trunc') {
138 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
141 ( $rnd_mode eq 'zero' ||
142 ($rnd_mode eq '-inf' && (substr($q,0,1) eq '+')) ||
143 ($rnd_mode eq '+inf' && (substr($q,0,1) eq '-')) ||
144 ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
145 ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
148 &'badd($q, ((substr($q,0,1) eq '-') ? '-1' : '+1'));
154 # round the mantissa of $x to $scale digits
155 sub main'fround { #(fnum_str, scale) return fnum_str
156 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
157 if ($x eq 'NaN' || $scale <= 0) {
160 local($xm,$xe) = split('E',$x);
161 if (length($xm)-1 <= $scale) {
164 &norm(&round(substr($xm,0,$scale+1),
165 "+0".substr($xm,$scale+1,1),"+10"),
166 $xe+length($xm)-$scale-1);
171 # round $x at the 10 to the $scale digit place
172 sub main'ffround { #(fnum_str, scale) return fnum_str
173 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
177 local($xm,$xe) = split('E',$x);
181 $xe = length($xm)+$xe-$scale;
185 &norm(&round('+0',"+0".substr($xm,1,1),"+10"), $scale);
187 &norm(&round(substr($xm,0,$trunc),
188 "+0".substr($xm,$trunc,1),"+10"), $scale);
194 # compare 2 values returns one of undef, <0, =0, >0
195 # returns undef if either or both input value are not numbers
196 sub main'fcmp #(fnum_str, fnum_str) return cond_code
198 local($x, $y) = (&'fnorm($_[0]),&'fnorm($_[1]));
199 if ($x eq "NaN" || $y eq "NaN") {
203 } elsif (ord($x) != ord($y)) {
204 (ord($y) - ord($x)); # based on signs
206 local($xm,$xe) = split('E',$x);
207 local($ym,$ye) = split('E',$y);
209 ($xe - $ye) * (substr($x,0,1).'1');
211 &bigint'cmp($xm,$ym); # based on value
216 # square root by Newtons method.
217 sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
218 local($x, $scale) = (&'fnorm($_[0]), $_[1]);
219 if ($x eq 'NaN' || $x =~ /^-/) {
221 } elsif ($x eq '+0E+0') {
224 local($xm, $xe) = split('E',$x);
225 $scale = $div_scale if (!$scale);
226 $scale = length($xm)-1 if ($scale < length($xm)-1);
227 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
228 while ($gs < 2*$scale) {
229 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
232 &'fround($guess, $scale);