Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.4 KiB

  1. package bigfloat;
  2. require "bigint.pl";
  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. #
  12. # Arbitrary length float math package
  13. #
  14. # by Mark Biggar
  15. #
  16. # number format
  17. # canonical strings have the form /[+-]\d+E[+-]\d+/
  18. # Input values can have embedded whitespace
  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
  23. # max($div_scale,length(dividend)+length(divisor))
  24. # digits by default.
  25. # Also used for default sqrt scale
  26. $div_scale = 40;
  27. # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
  28. $rnd_mode = 'even';
  29. # bigfloat routines
  30. #
  31. # fadd(NSTR, NSTR) return NSTR addition
  32. # fsub(NSTR, NSTR) return NSTR subtraction
  33. # fmul(NSTR, NSTR) return NSTR multiplication
  34. # fdiv(NSTR, NSTR[,SCALE]) returns NSTR division to SCALE places
  35. # fneg(NSTR) return NSTR negation
  36. # fabs(NSTR) return NSTR absolute value
  37. # fcmp(NSTR,NSTR) return CODE compare undef,<0,=0,>0
  38. # fround(NSTR, SCALE) return NSTR round to SCALE digits
  39. # ffround(NSTR, SCALE) return NSTR round at SCALEth place
  40. # fnorm(NSTR) return (NSTR) normalize
  41. # fsqrt(NSTR[, SCALE]) return NSTR sqrt to SCALE places
  42. # Convert a number to canonical string form.
  43. # Takes something that looks like a number and converts it to
  44. # the form /^[+-]\d+E[+-]\d+$/.
  45. sub main'fnorm { #(string) return fnum_str
  46. local($_) = @_;
  47. s/\s+//g; # strip white space
  48. if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
  49. && ($2 ne '' || defined($4))) {
  50. my $x = defined($4) ? $4 : '';
  51. &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
  52. } else {
  53. 'NaN';
  54. }
  55. }
  56. # normalize number -- for internal use
  57. sub norm { #(mantissa, exponent) return fnum_str
  58. local($_, $exp) = @_;
  59. if ($_ eq 'NaN') {
  60. 'NaN';
  61. } else {
  62. s/^([+-])0+/$1/; # strip leading zeros
  63. if (length($_) == 1) {
  64. '+0E+0';
  65. } else {
  66. $exp += length($1) if (s/(0+)$//); # strip trailing zeros
  67. sprintf("%sE%+ld", $_, $exp);
  68. }
  69. }
  70. }
  71. # negation
  72. sub main'fneg { #(fnum_str) return fnum_str
  73. local($_) = &'fnorm($_[$[]);
  74. vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
  75. if ( ord("\t") == 9 ) { # ascii
  76. s/^H/N/;
  77. }
  78. else { # ebcdic character set
  79. s/\373/N/;
  80. }
  81. $_;
  82. }
  83. # absolute value
  84. sub main'fabs { #(fnum_str) return fnum_str
  85. local($_) = &'fnorm($_[$[]);
  86. s/^-/+/; # mash sign
  87. $_;
  88. }
  89. # multiplication
  90. sub main'fmul { #(fnum_str, fnum_str) return fnum_str
  91. local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  92. if ($x eq 'NaN' || $y eq 'NaN') {
  93. 'NaN';
  94. } else {
  95. local($xm,$xe) = split('E',$x);
  96. local($ym,$ye) = split('E',$y);
  97. &norm(&'bmul($xm,$ym),$xe+$ye);
  98. }
  99. }
  100. # addition
  101. sub main'fadd { #(fnum_str, fnum_str) return fnum_str
  102. local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  103. if ($x eq 'NaN' || $y eq 'NaN') {
  104. 'NaN';
  105. } else {
  106. local($xm,$xe) = split('E',$x);
  107. local($ym,$ye) = split('E',$y);
  108. ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
  109. &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
  110. }
  111. }
  112. # subtraction
  113. sub main'fsub { #(fnum_str, fnum_str) return fnum_str
  114. &'fadd($_[$[],&'fneg($_[$[+1]));
  115. }
  116. # division
  117. # args are dividend, divisor, scale (optional)
  118. # result has at most max(scale, length(dividend), length(divisor)) digits
  119. sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
  120. {
  121. local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
  122. if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
  123. 'NaN';
  124. } else {
  125. local($xm,$xe) = split('E',$x);
  126. local($ym,$ye) = split('E',$y);
  127. $scale = $div_scale if (!$scale);
  128. $scale = length($xm)-1 if (length($xm)-1 > $scale);
  129. $scale = length($ym)-1 if (length($ym)-1 > $scale);
  130. $scale = $scale + length($ym) - length($xm);
  131. &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
  132. $xe-$ye-$scale);
  133. }
  134. }
  135. # round int $q based on fraction $r/$base using $rnd_mode
  136. sub round { #(int_str, int_str, int_str) return int_str
  137. local($q,$r,$base) = @_;
  138. if ($q eq 'NaN' || $r eq 'NaN') {
  139. 'NaN';
  140. } elsif ($rnd_mode eq 'trunc') {
  141. $q; # just truncate
  142. } else {
  143. local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
  144. if ( $cmp < 0 ||
  145. ($cmp == 0 &&
  146. ( $rnd_mode eq 'zero' ||
  147. ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
  148. ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
  149. ($rnd_mode eq 'even' && $q =~ /[24680]$/) ||
  150. ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) {
  151. $q; # round down
  152. } else {
  153. &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
  154. # round up
  155. }
  156. }
  157. }
  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) {
  162. $x;
  163. } else {
  164. local($xm,$xe) = split('E',$x);
  165. if (length($xm)-1 <= $scale) {
  166. $x;
  167. } else {
  168. &norm(&round(substr($xm,$[,$scale+1),
  169. "+0".substr($xm,$[+$scale+1,1),"+10"),
  170. $xe+length($xm)-$scale-1);
  171. }
  172. }
  173. }
  174. # round $x at the 10 to the $scale digit place
  175. sub main'ffround { #(fnum_str, scale) return fnum_str
  176. local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
  177. if ($x eq 'NaN') {
  178. 'NaN';
  179. } else {
  180. local($xm,$xe) = split('E',$x);
  181. if ($xe >= $scale) {
  182. $x;
  183. } else {
  184. $xe = length($xm)+$xe-$scale;
  185. if ($xe < 1) {
  186. '+0E+0';
  187. } elsif ($xe == 1) {
  188. # The first substr preserves the sign, which means that
  189. # we'll pass a non-normalized "-0" to &round when rounding
  190. # -0.006 (for example), purely so that &round won't lose
  191. # the sign.
  192. &norm(&round(substr($xm,$[,1).'0',
  193. "+0".substr($xm,$[+1,1),"+10"), $scale);
  194. } else {
  195. &norm(&round(substr($xm,$[,$xe),
  196. "+0".substr($xm,$[+$xe,1),"+10"), $scale);
  197. }
  198. }
  199. }
  200. }
  201. # compare 2 values returns one of undef, <0, =0, >0
  202. # returns undef if either or both input value are not numbers
  203. sub main'fcmp #(fnum_str, fnum_str) return cond_code
  204. {
  205. local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  206. if ($x eq "NaN" || $y eq "NaN") {
  207. undef;
  208. } else {
  209. ord($y) <=> ord($x)
  210. ||
  211. ( local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
  212. (($xe <=> $ye) * (substr($x,$[,1).'1')
  213. || &bigint'cmp($xm,$ym))
  214. );
  215. }
  216. }
  217. # square root by Newtons method.
  218. sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
  219. local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
  220. if ($x eq 'NaN' || $x =~ /^-/) {
  221. 'NaN';
  222. } elsif ($x eq '+0E+0') {
  223. '+0E+0';
  224. } else {
  225. local($xm, $xe) = split('E',$x);
  226. $scale = $div_scale if (!$scale);
  227. $scale = length($xm)-1 if ($scale < length($xm)-1);
  228. local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
  229. while ($gs < 2*$scale) {
  230. $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
  231. $gs *= 2;
  232. }
  233. &'fround($guess, $scale);
  234. }
  235. }
  236. 1;