perl 4.0 patch 14: patch #11, continued
[p5sagit/p5-mst-13.2.git] / lib / bigfloat.pl
CommitLineData
5303340c 1package bigfloat;
2require "bigint.pl";
3
4# Arbitrary length float math package
5#
6# number format
7# canonical strings have the form /[+-]\d+E[+-]\d+/
8# Input values can have inbedded whitespace
9# Error returns
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))
14# digits by default.
15# Also used for default sqrt scale
16
17$div_scale = 40;
18
19# Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
20
21$rnd_mode = 'even';
22
23# bigfloat routines
24#
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
36\f
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+$/.
40sub main'fnorm { #(string) return fnum_str
41 local($_) = @_;
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));
45 } else {
46 'NaN';
47 }
48}
49
50# normalize number -- for internal use
51sub norm { #(mantissa, exponent) return fnum_str
52 local($_, $exp) = @_;
53 if ($_ eq 'NaN') {
54 'NaN';
55 } else {
56 s/^([+-])0+/$1/; # strip leading zeros
57 if (length($_) == 1) {
58 '+0E+0';
59 } else {
60 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
61 sprintf("%sE%+ld", $_, $exp);
62 }
63 }
64}
65
66# negation
67sub main'fneg { #(fnum_str) return fnum_str
68 local($_) = &'fnorm($_[0]);
69 substr($_,0,1) =~ tr/+-/-+/ if ($_ ne '+0E+0'); # flip sign
70 $_;
71}
72
73# absolute value
74sub main'fabs { #(fnum_str) return fnum_str
75 local($_) = &'fnorm($_[0]);
fe14fcc3 76 substr($_,0,1) = '+' unless $_ eq 'NaN'; # mash sign
5303340c 77 $_;
78}
79
80# multiplication
81sub 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') {
84 'NaN';
85 } else {
86 local($xm,$xe) = split('E',$x);
87 local($ym,$ye) = split('E',$y);
88 &norm(&'bmul($xm,$ym),$xe+$ye);
89 }
90}
91\f
92# addition
93sub 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') {
96 'NaN';
97 } else {
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);
102 }
103}
104
105# subtraction
106sub main'fsub { #(fnum_str, fnum_str) return fnum_str
107 &'fadd($_[0],&'fneg($_[1]));
108}
109
110# division
111# args are dividend, divisor, scale (optional)
112# result has at most max(scale, length(dividend), length(divisor)) digits
113sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
114{
115 local($x,$y,$scale) = (&'fnorm($_[0]),&'fnorm($_[1]),$_[2]);
116 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
117 'NaN';
118 } else {
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),
126 $xe-$ye-$scale);
127 }
128}
129\f
130# round int $q based on fraction $r/$base using $rnd_mode
131sub round { #(int_str, int_str, int_str) return int_str
132 local($q,$r,$base) = @_;
133 if ($q eq 'NaN' || $r eq 'NaN') {
134 'NaN';
135 } elsif ($rnd_mode eq 'trunc') {
136 $q; # just truncate
137 } else {
138 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
139 if ( $cmp < 0 ||
140 ($cmp == 0 &&
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]$/) )) ) {
146 $q; # round down
147 } else {
148 &'badd($q, ((substr($q,0,1) eq '-') ? '-1' : '+1'));
149 # round up
150 }
151 }
152}
153
154# round the mantissa of $x to $scale digits
155sub main'fround { #(fnum_str, scale) return fnum_str
156 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
157 if ($x eq 'NaN' || $scale <= 0) {
158 $x;
159 } else {
160 local($xm,$xe) = split('E',$x);
161 if (length($xm)-1 <= $scale) {
162 $x;
163 } else {
164 &norm(&round(substr($xm,0,$scale+1),
165 "+0".substr($xm,$scale+1,1),"+10"),
166 $xe+length($xm)-$scale-1);
167 }
168 }
169}
170\f
171# round $x at the 10 to the $scale digit place
172sub main'ffround { #(fnum_str, scale) return fnum_str
173 local($x,$scale) = (&'fnorm($_[0]),$_[1]);
174 if ($x eq 'NaN') {
175 'NaN';
176 } else {
177 local($xm,$xe) = split('E',$x);
178 if ($xe >= $scale) {
179 $x;
180 } else {
181 $xe = length($xm)+$xe-$scale;
182 if ($xe < 1) {
183 '+0E+0';
184 } elsif ($xe == 1) {
185 &norm(&round('+0',"+0".substr($xm,1,1),"+10"), $scale);
186 } else {
187 &norm(&round(substr($xm,0,$trunc),
188 "+0".substr($xm,$trunc,1),"+10"), $scale);
189 }
190 }
191 }
192}
193
194# compare 2 values returns one of undef, <0, =0, >0
195# returns undef if either or both input value are not numbers
196sub main'fcmp #(fnum_str, fnum_str) return cond_code
197{
198 local($x, $y) = (&'fnorm($_[0]),&'fnorm($_[1]));
199 if ($x eq "NaN" || $y eq "NaN") {
200 undef;
201 } elsif ($x eq $y) {
202 0;
203 } elsif (ord($x) != ord($y)) {
204 (ord($y) - ord($x)); # based on signs
205 } else {
206 local($xm,$xe) = split('E',$x);
207 local($ym,$ye) = split('E',$y);
208 if ($xe ne $ye) {
209 ($xe - $ye) * (substr($x,0,1).'1');
210 } else {
211 &bigint'cmp($xm,$ym); # based on value
212 }
213 }
214}
215\f
216# square root by Newtons method.
217sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
218 local($x, $scale) = (&'fnorm($_[0]), $_[1]);
219 if ($x eq 'NaN' || $x =~ /^-/) {
220 'NaN';
221 } elsif ($x eq '+0E+0') {
222 '+0E+0';
223 } else {
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");
230 $gs *= 2;
231 }
232 &'fround($guess, $scale);
233 }
234}
235
2361;