Source code of Windows XP (NT5)
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.

235 lines
6.6 KiB

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