@ac-kit/math-numbers 0.2.10 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +5 -5
  3. package/dist/ieee754/binary/ieee-binary-bits.d.ts +19 -0
  4. package/dist/ieee754/binary/ieee-binary-bits.d.ts.map +1 -0
  5. package/dist/ieee754/binary/ieee-binary-bits.js +39 -0
  6. package/dist/ieee754/binary/ieee-binary-bits.js.map +1 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/num/binary-fp.d.ts +12 -0
  12. package/dist/num/binary-fp.d.ts.map +1 -1
  13. package/dist/num/binary-fp.js +17 -0
  14. package/dist/num/binary-fp.js.map +1 -1
  15. package/dist/num/binary-fp128.d.ts +9 -0
  16. package/dist/num/binary-fp128.d.ts.map +1 -1
  17. package/dist/num/binary-fp128.js +13 -0
  18. package/dist/num/binary-fp128.js.map +1 -1
  19. package/dist/num/binary-fp16.d.ts +5 -0
  20. package/dist/num/binary-fp16.d.ts.map +1 -1
  21. package/dist/num/binary-fp16.js +7 -0
  22. package/dist/num/binary-fp16.js.map +1 -1
  23. package/dist/num/binary-fp32.d.ts +5 -0
  24. package/dist/num/binary-fp32.d.ts.map +1 -1
  25. package/dist/num/binary-fp32.js +7 -0
  26. package/dist/num/binary-fp32.js.map +1 -1
  27. package/dist/num/binary-fp64.d.ts +11 -0
  28. package/dist/num/binary-fp64.d.ts.map +1 -1
  29. package/dist/num/binary-fp64.js +19 -1
  30. package/dist/num/binary-fp64.js.map +1 -1
  31. package/dist/num/decimal-num.d.ts +9 -1
  32. package/dist/num/decimal-num.d.ts.map +1 -1
  33. package/dist/num/decimal-num.js +16 -0
  34. package/dist/num/decimal-num.js.map +1 -1
  35. package/dist/num/fraction.d.ts +18 -0
  36. package/dist/num/fraction.d.ts.map +1 -1
  37. package/dist/num/fraction.js +24 -0
  38. package/dist/num/fraction.js.map +1 -1
  39. package/dist/num/integer-num.d.ts +4 -2
  40. package/dist/num/integer-num.d.ts.map +1 -1
  41. package/dist/num/integer-num.js +4 -2
  42. package/dist/num/integer-num.js.map +1 -1
  43. package/dist/rational/rational-from-number.d.ts +7 -2
  44. package/dist/rational/rational-from-number.d.ts.map +1 -1
  45. package/dist/rational/rational-from-number.js +20 -34
  46. package/dist/rational/rational-from-number.js.map +1 -1
  47. package/dist/rational/rational-limit-denominator.d.ts +25 -0
  48. package/dist/rational/rational-limit-denominator.d.ts.map +1 -0
  49. package/dist/rational/rational-limit-denominator.js +78 -0
  50. package/dist/rational/rational-limit-denominator.js.map +1 -0
  51. package/dist/rational/rational-to-number.d.ts +8 -4
  52. package/dist/rational/rational-to-number.d.ts.map +1 -1
  53. package/dist/rational/rational-to-number.js +24 -17
  54. package/dist/rational/rational-to-number.js.map +1 -1
  55. package/package.json +9 -7
@@ -1,25 +1,32 @@
1
- import { bigIntBitLength } from "@ac-kit/core";
1
+ import { bigIntBitLength, partsToFloat64 } from "@ac-kit/core";
2
+ /** Quotient bits kept before the single rounding: 53, a guard and a sticky. */
3
+ const QUOTIENT_BITS_ = 55;
2
4
  /**
3
- * The nearest binary64 approximation.
5
+ * The binary64 nearest `value`, correctly rounded (to nearest, ties to even)
6
+ * over the whole range: subnormal results keep the bits they can, results past
7
+ * `Number.MAX_VALUE` are `±Infinity`, and operands of any width are exact.
8
+ * Dividing the two `Number` conversions would round three times and lose the
9
+ * value once either part leaves binary64's range.
4
10
  *
5
- * Dividing the two `Number` conversions loses the value entirely once either
6
- * part exceeds `Number.MAX_VALUE`, so wide operands are scaled down by their
7
- * common bit length first.
11
+ * The quotient is taken in `bigint` to 55 significant bits, the remainder
12
+ * folded into the last as a sticky bit, then rounded once by `partsToFloat64`.
13
+ * O(n²) in the operands' bit length.
8
14
  */
9
15
  export function rationalToNumber(value) {
10
- const numerator = Number(value.numerator);
11
- const denominator = Number(value.denominator);
12
- if (Number.isFinite(numerator) && Number.isFinite(denominator)) {
13
- return numerator / denominator;
14
- }
15
- // One or both parts are past binary64's range. Dropping the same number of
16
- // low bits from each leaves the quotient unchanged to within an ulp, and
17
- // 1000 bits is the widest that still converts to a finite double.
18
16
  const negative = value.numerator < 0n;
19
17
  const magnitude = negative ? -value.numerator : value.numerator;
20
- const widest = Math.max(bigIntBitLength(magnitude), bigIntBitLength(value.denominator));
21
- const shift = BigInt(Math.max(widest - 1000, 0));
22
- const quotient = Number(magnitude >> shift) / Number(value.denominator >> shift);
23
- return negative ? -quotient : quotient;
18
+ if (magnitude === 0n) {
19
+ return 0;
20
+ }
21
+ const denominator = value.denominator;
22
+ const exponent = bigIntBitLength(magnitude) - bigIntBitLength(denominator) - QUOTIENT_BITS_;
23
+ const dividend = exponent < 0 ? magnitude << BigInt(-exponent) : magnitude;
24
+ const divisor = exponent > 0 ? denominator << BigInt(exponent) : denominator;
25
+ let quotient = dividend / divisor;
26
+ if (quotient * divisor !== dividend) {
27
+ quotient |= 1n;
28
+ }
29
+ const result = partsToFloat64(quotient, exponent);
30
+ return negative ? -result : result;
24
31
  }
25
32
  //# sourceMappingURL=rational-to-number.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"rational-to-number.js","sourceRoot":"","sources":["../../src/rational/rational-to-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAI/C;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAyB;IACzD,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC1C,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAE9C,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QAChE,OAAO,SAAS,GAAG,WAA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AC,WAAW,CAAC;IACtC,MAAM,QAAQ,GACb,eAAe,CAAC,SAAS,CAAC,GAAG,eAAe,CAAC,WAAW,CAAC,GAAG,cAAc,CAAC;IAC5E,MAAM,QAAQ,GAAG,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC3E,MAAM,OAAO,GAAG,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;IAC7E,IAAI,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAC;IAClC,IAAI,QAAQ,GAAG,OAAO,KAAK,QAAQ,EAAE,CAAC;QACrC,QAAQ,IAAI,EAAE,CAAC;IAChB,CAAC;IACD,MAAM,MAAM,GAAG,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAElD,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;AACpC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/math-numbers",
3
- "version": "0.2.10",
3
+ "version": "0.3.0",
4
4
  "description": "Concrete numeric representations: the number tower from naturals to reals, plus the fixed-width and arbitrary-precision types that model each set.",
5
5
  "keywords": [
6
6
  "arbitrary-precision",
@@ -45,19 +45,21 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/core": "^0.13.0",
49
- "@ac-kit/math-algebra": "^0.1.12",
50
- "@ac-kit/math-integer": "^0.3.7",
48
+ "@ac-kit/core": "^0.14.0",
49
+ "@ac-kit/math-algebra": "^0.2.0",
50
+ "@ac-kit/math-integer": "^0.4.0",
51
51
  "@types/web": "^0.0.367"
52
52
  },
53
53
  "devDependencies": {
54
- "@ac-kit/codegen-util": "0.1.1",
55
- "@ac-kit/math-random": "0.4.1",
54
+ "@ac-kit/codegen-util": "0.1.2",
55
+ "@ac-kit/math-random": "0.4.2",
56
56
  "@ac-kit/tsconfig": "0.1.0",
57
57
  "@ac-kit/typedoc-config": "0.1.0",
58
- "@ac-kit/vitest-config": "0.2.0",
58
+ "@ac-kit/vitest-config": "0.3.0",
59
59
  "@types/node": "26.6.11",
60
60
  "@typescript/native": "typescript@7.0.2",
61
+ "decimal.js": "10.6.0",
62
+ "fraction.js": "5.3.4",
61
63
  "rimraf": "6.1.4",
62
64
  "tsx": "4.23.15",
63
65
  "typedoc": "0.28.20",