This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid infinite recursion when Thread.pm croaks during
[perl5.git] / lib / bigfloat.pl
CommitLineData
5303340c
LW
1package bigfloat;
2require "bigint.pl";
a6d71656
GS
3#
4# This library is no longer being maintained, and is included for backward
5# compatibility with Perl 4 programs which may require it.
6#
7# In particular, this should not be used as an example of modern Perl
8# programming techniques.
9#
10# Suggested alternative: Math::BigFloat
11#
5303340c
LW
12# Arbitrary length float math package
13#
68decaef
LW
14# by Mark Biggar
15#
5303340c
LW
16# number format
17# canonical strings have the form /[+-]\d+E[+-]\d+/
5d7098d5 18# Input values can have embedded whitespace
5303340c
LW
19# Error returns
20# 'NaN' An input parameter was "Not a Number" or
21# divide by zero or sqrt of negative number
22# Division is computed to
79072805 23# max($div_scale,length(dividend)+length(divisor))
5303340c
LW
24# digits by default.
25# Also used for default sqrt scale
26
27$div_scale = 40;
28
29# Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
30
31$rnd_mode = 'even';
32
33# bigfloat routines
34#
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
46\f
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+$/.
50sub main'fnorm { #(string) return fnum_str
51 local($_) = @_;
52 s/\s+//g; # strip white space
afea815c
CS
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));
5303340c
LW
57 } else {
58 'NaN';
59 }
60}
61
62# normalize number -- for internal use
63sub norm { #(mantissa, exponent) return fnum_str
64 local($_, $exp) = @_;
65 if ($_ eq 'NaN') {
66 'NaN';
67 } else {
68 s/^([+-])0+/$1/; # strip leading zeros
69 if (length($_) == 1) {
70 '+0E+0';
71 } else {
72 $exp += length($1) if (s/(0+)$//); # strip trailing zeros
73 sprintf("%sE%+ld", $_, $exp);
74 }
75 }
76}
77
78# negation
79sub main'fneg { #(fnum_str) return fnum_str
79072805 80 local($_) = &'fnorm($_[$[]);
e334a159 81 vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
68decaef 82 s/^H/N/;
5303340c
LW
83 $_;
84}
85
86# absolute value
87sub main'fabs { #(fnum_str) return fnum_str
79072805 88 local($_) = &'fnorm($_[$[]);
68decaef 89 s/^-/+/; # mash sign
5303340c
LW
90 $_;
91}
92
93# multiplication
94sub main'fmul { #(fnum_str, fnum_str) return fnum_str
79072805 95 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
5303340c
LW
96 if ($x eq 'NaN' || $y eq 'NaN') {
97 'NaN';
98 } else {
99 local($xm,$xe) = split('E',$x);
100 local($ym,$ye) = split('E',$y);
101 &norm(&'bmul($xm,$ym),$xe+$ye);
102 }
103}
104\f
105# addition
106sub main'fadd { #(fnum_str, fnum_str) return fnum_str
79072805 107 local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
5303340c
LW
108 if ($x eq 'NaN' || $y eq 'NaN') {
109 'NaN';
110 } else {
111 local($xm,$xe) = split('E',$x);
112 local($ym,$ye) = split('E',$y);
113 ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
114 &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
115 }
116}
117
118# subtraction
119sub main'fsub { #(fnum_str, fnum_str) return fnum_str
79072805 120 &'fadd($_[$[],&'fneg($_[$[+1]));
5303340c
LW
121}
122
123# division
124# args are dividend, divisor, scale (optional)
125# result has at most max(scale, length(dividend), length(divisor)) digits
126sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
127{
79072805 128 local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
5303340c
LW
129 if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
130 'NaN';
131 } else {
132 local($xm,$xe) = split('E',$x);
133 local($ym,$ye) = split('E',$y);
134 $scale = $div_scale if (!$scale);
135 $scale = length($xm)-1 if (length($xm)-1 > $scale);
136 $scale = length($ym)-1 if (length($ym)-1 > $scale);
137 $scale = $scale + length($ym) - length($xm);
5d7098d5 138 &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
5303340c
LW
139 $xe-$ye-$scale);
140 }
141}
142\f
143# round int $q based on fraction $r/$base using $rnd_mode
144sub round { #(int_str, int_str, int_str) return int_str
145 local($q,$r,$base) = @_;
146 if ($q eq 'NaN' || $r eq 'NaN') {
147 'NaN';
148 } elsif ($rnd_mode eq 'trunc') {
149 $q; # just truncate
150 } else {
151 local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
152 if ( $cmp < 0 ||
153 ($cmp == 0 &&
154 ( $rnd_mode eq 'zero' ||
79072805
LW
155 ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
156 ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
5303340c
LW
157 ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
158 ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
159 $q; # round down
160 } else {
79072805 161 &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
5303340c
LW
162 # round up
163 }
164 }
165}
166
167# round the mantissa of $x to $scale digits
168sub main'fround { #(fnum_str, scale) return fnum_str
79072805 169 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
5303340c
LW
170 if ($x eq 'NaN' || $scale <= 0) {
171 $x;
172 } else {
173 local($xm,$xe) = split('E',$x);
174 if (length($xm)-1 <= $scale) {
175 $x;
176 } else {
79072805
LW
177 &norm(&round(substr($xm,$[,$scale+1),
178 "+0".substr($xm,$[+$scale+1,1),"+10"),
5303340c
LW
179 $xe+length($xm)-$scale-1);
180 }
181 }
182}
183\f
184# round $x at the 10 to the $scale digit place
185sub main'ffround { #(fnum_str, scale) return fnum_str
79072805 186 local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
5303340c
LW
187 if ($x eq 'NaN') {
188 'NaN';
189 } else {
190 local($xm,$xe) = split('E',$x);
191 if ($xe >= $scale) {
192 $x;
193 } else {
194 $xe = length($xm)+$xe-$scale;
195 if ($xe < 1) {
196 '+0E+0';
197 } elsif ($xe == 1) {
5d7098d5
SK
198 # The first substr preserves the sign, which means that
199 # we'll pass a non-normalized "-0" to &round when rounding
200 # -0.006 (for example), purely so that &round won't lose
201 # the sign.
202 &norm(&round(substr($xm,$[,1).'0',
203 "+0".substr($xm,$[+1,1),"+10"), $scale);
5303340c 204 } else {
79072805
LW
205 &norm(&round(substr($xm,$[,$xe),
206 "+0".substr($xm,$[+$xe,1),"+10"), $scale);
5303340c
LW
207 }
208 }
209 }
210}
211
212# compare 2 values returns one of undef, <0, =0, >0
213# returns undef if either or both input value are not numbers
214sub main'fcmp #(fnum_str, fnum_str) return cond_code
215{
79072805 216 local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
5303340c
LW
217 if ($x eq "NaN" || $y eq "NaN") {
218 undef;
5303340c 219 } else {
68decaef
LW
220 ord($y) <=> ord($x)
221 ||
222 ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
79072805 223 (($xe <=> $ye) * (substr($x,$[,1).'1')
68decaef
LW
224 || &bigint'cmp($xm,$ym))
225 );
5303340c
LW
226 }
227}
228\f
229# square root by Newtons method.
230sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
79072805 231 local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
5303340c
LW
232 if ($x eq 'NaN' || $x =~ /^-/) {
233 'NaN';
234 } elsif ($x eq '+0E+0') {
235 '+0E+0';
236 } else {
237 local($xm, $xe) = split('E',$x);
238 $scale = $div_scale if (!$scale);
239 $scale = length($xm)-1 if ($scale < length($xm)-1);
240 local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
241 while ($gs < 2*$scale) {
242 $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
243 $gs *= 2;
244 }
245 &'fround($guess, $scale);
246 }
247}
248
2491;