3 # Arbitrary length float math package
8 # canonical strings have the form /[+-]\d+E[+-]\d+/
9 # Input values can have inbedded whitespace
11 # 'NaN' An input parameter was "Not a Number" or
12 # divide by zero or sqrt of negative number
13 # Division is computed to
14 # max($div_scale,length(dividend)+length(divisor))
16 # Also used for default sqrt scale
20 # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
26 # fadd(NSTR, NSTR) return NSTR addition
27 # fsub(NSTR, NSTR) return NSTR subtraction
28 # fmul(NSTR, NSTR) return NSTR multiplication
29 # fdiv(NSTR, NSTR[,SCALE]) returns NSTR division to SCALE places
30 # fneg(NSTR) return NSTR negation
31 # fabs(NSTR) return NSTR absolute value
32 # fcmp(NSTR,NSTR) return CODE compare undef,<0,=0,>0
33 # fround(NSTR, SCALE) return NSTR round to SCALE digits
34 # ffround(NSTR, SCALE) return NSTR round at SCALEth place
35 # fnorm(NSTR) return (NSTR) normalize
36 # fsqrt(NSTR[, SCALE]) return NSTR sqrt to SCALE places
38 # Convert a number to canonical string form.
39 # Takes something that looks like a number and converts it to
40 # the form /^[+-]\d+E[+-]\d+$/.
41 sub main'fnorm { #(string) return fnum_str
43 s/\s+//g; # strip white space
44 if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
45 && ($2 ne '' || defined($4))) {
46 my $x = defined($4) ? $4 : '';
47 &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
53 # normalize number -- for internal use
54 sub norm { #(mantissa, exponent) return fnum_str
59 s/^([+-])0+/$1/; # strip leading zeros
60 if (length($_) == 1) {
63 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
64 sprintf("%sE%+ld", $_, $exp);
70 sub main'fneg { #(fnum_str) return fnum_str
71 local($_) = &'fnorm($_[$[]);
72 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
78 sub main'fabs { #(fnum_str) return fnum_str
79 local($_) = &'fnorm($_[$[]);
85 sub main'fmul { #(fnum_str, fnum_str) return fnum_str
86 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
87 if ($x eq 'NaN' || $y eq 'NaN') {
90 local($xm,$xe) = split('E',$x);
91 local($ym,$ye) = split('E',$y);
92 &norm(&'bmul($xm,$ym),$xe+$ye);
97 sub main'fadd { #(fnum_str, fnum_str) return fnum_str
98 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
99 if ($x eq 'NaN' || $y eq 'NaN') {
102 local($xm,$xe) = split('E',$x);
103 local($ym,$ye) = split('E',$y);
104 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
105 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
110 sub main'fsub { #(fnum_str, fnum_str) return fnum_str
111 &'fadd($_[$[],&'fneg($_[$[+1]));
115 # args are dividend, divisor, scale (optional)
116 # result has at most max(scale, length(dividend), length(divisor)) digits
117 sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
119 local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
120 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
123 local($xm,$xe) = split('E',$x);
124 local($ym,$ye) = split('E',$y);
125 $scale = $div_scale if (!$scale);
126 $scale = length($xm)-1 if (length($xm)-1 > $scale);
127 $scale = length($ym)-1 if (length($ym)-1 > $scale);
128 $scale = $scale + length($ym) - length($xm);
129 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),$ym),
134 # round int $q based on fraction $r/$base using $rnd_mode
135 sub round { #(int_str, int_str, int_str) return int_str
136 local($q,$r,$base) = @_;
137 if ($q eq 'NaN' || $r eq 'NaN') {
139 } elsif ($rnd_mode eq 'trunc') {
142 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
145 ( $rnd_mode eq 'zero' ||
146 ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
147 ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
148 ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
149 ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
152 &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
158 # round the mantissa of $x to $scale digits
159 sub main'fround { #(fnum_str, scale) return fnum_str
160 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
161 if ($x eq 'NaN' || $scale <= 0) {
164 local($xm,$xe) = split('E',$x);
165 if (length($xm)-1 <= $scale) {
168 &norm(&round(substr($xm,$[,$scale+1),
169 "+0".substr($xm,$[+$scale+1,1),"+10"),
170 $xe+length($xm)-$scale-1);
175 # round $x at the 10 to the $scale digit place
176 sub main'ffround { #(fnum_str, scale) return fnum_str
177 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
181 local($xm,$xe) = split('E',$x);
185 $xe = length($xm)+$xe-$scale;
189 &norm(&round('+0',"+0".substr($xm,$[+1,1),"+10"), $scale);
191 &norm(&round(substr($xm,$[,$xe),
192 "+0".substr($xm,$[+$xe,1),"+10"), $scale);
198 # compare 2 values returns one of undef, <0, =0, >0
199 # returns undef if either or both input value are not numbers
200 sub main'fcmp #(fnum_str, fnum_str) return cond_code
202 local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
203 if ($x eq "NaN" || $y eq "NaN") {
208 ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
209 (($xe <=> $ye) * (substr($x,$[,1).'1')
210 || &bigint'cmp($xm,$ym))
215 # square root by Newtons method.
216 sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
217 local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
218 if ($x eq 'NaN' || $x =~ /^-/) {
220 } elsif ($x eq '+0E+0') {
223 local($xm, $xe) = split('E',$x);
224 $scale = $div_scale if (!$scale);
225 $scale = length($xm)-1 if ($scale < length($xm)-1);
226 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
227 while ($gs < 2*$scale) {
228 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
231 &'fround($guess, $scale);