@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.
- package/CHANGELOG.md +38 -0
- package/README.md +5 -5
- package/dist/ieee754/binary/ieee-binary-bits.d.ts +19 -0
- package/dist/ieee754/binary/ieee-binary-bits.d.ts.map +1 -0
- package/dist/ieee754/binary/ieee-binary-bits.js +39 -0
- package/dist/ieee754/binary/ieee-binary-bits.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/num/binary-fp.d.ts +12 -0
- package/dist/num/binary-fp.d.ts.map +1 -1
- package/dist/num/binary-fp.js +17 -0
- package/dist/num/binary-fp.js.map +1 -1
- package/dist/num/binary-fp128.d.ts +9 -0
- package/dist/num/binary-fp128.d.ts.map +1 -1
- package/dist/num/binary-fp128.js +13 -0
- package/dist/num/binary-fp128.js.map +1 -1
- package/dist/num/binary-fp16.d.ts +5 -0
- package/dist/num/binary-fp16.d.ts.map +1 -1
- package/dist/num/binary-fp16.js +7 -0
- package/dist/num/binary-fp16.js.map +1 -1
- package/dist/num/binary-fp32.d.ts +5 -0
- package/dist/num/binary-fp32.d.ts.map +1 -1
- package/dist/num/binary-fp32.js +7 -0
- package/dist/num/binary-fp32.js.map +1 -1
- package/dist/num/binary-fp64.d.ts +11 -0
- package/dist/num/binary-fp64.d.ts.map +1 -1
- package/dist/num/binary-fp64.js +19 -1
- package/dist/num/binary-fp64.js.map +1 -1
- package/dist/num/decimal-num.d.ts +9 -1
- package/dist/num/decimal-num.d.ts.map +1 -1
- package/dist/num/decimal-num.js +16 -0
- package/dist/num/decimal-num.js.map +1 -1
- package/dist/num/fraction.d.ts +18 -0
- package/dist/num/fraction.d.ts.map +1 -1
- package/dist/num/fraction.js +24 -0
- package/dist/num/fraction.js.map +1 -1
- package/dist/num/integer-num.d.ts +4 -2
- package/dist/num/integer-num.d.ts.map +1 -1
- package/dist/num/integer-num.js +4 -2
- package/dist/num/integer-num.js.map +1 -1
- package/dist/rational/rational-from-number.d.ts +7 -2
- package/dist/rational/rational-from-number.d.ts.map +1 -1
- package/dist/rational/rational-from-number.js +20 -34
- package/dist/rational/rational-from-number.js.map +1 -1
- package/dist/rational/rational-limit-denominator.d.ts +25 -0
- package/dist/rational/rational-limit-denominator.d.ts.map +1 -0
- package/dist/rational/rational-limit-denominator.js +78 -0
- package/dist/rational/rational-limit-denominator.js.map +1 -0
- package/dist/rational/rational-to-number.d.ts +8 -4
- package/dist/rational/rational-to-number.d.ts.map +1 -1
- package/dist/rational/rational-to-number.js +24 -17
- package/dist/rational/rational-to-number.js.map +1 -1
- 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
|
|
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
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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/
|
|
1
|
+
{"version":3,"file":"rational-to-number.js","sourceRoot":"","sources":["../../src/rational/rational-to-number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAI/D,+EAA+E;AAC/E,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAyB;IACzD,MAAM,QAAQ,GAAG,KAAK,CAAC,SAAS,GAAG,EAAE,CAAC;IACtC,MAAM,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC;IAChE,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;QACtB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,MAAM,WAAW,GAAG,KAAK,CAAC,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.
|
|
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.
|
|
49
|
-
"@ac-kit/math-algebra": "^0.
|
|
50
|
-
"@ac-kit/math-integer": "^0.
|
|
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.
|
|
55
|
-
"@ac-kit/math-random": "0.4.
|
|
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.
|
|
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",
|