@gkucmierz/utils 3.0.1 → 3.0.2
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/main.mjs +2 -2
- package/package.json +1 -1
- package/src/golden-ratio.mjs +18 -15
package/main.mjs
CHANGED
|
@@ -42,7 +42,7 @@ import {
|
|
|
42
42
|
getType
|
|
43
43
|
} from './src/get-type.mjs'
|
|
44
44
|
import {
|
|
45
|
-
goldenRatio, goldenRatioBI
|
|
45
|
+
goldenRatio, goldenRatioBI, goldenRatioStr
|
|
46
46
|
} from './src/golden-ratio.mjs'
|
|
47
47
|
import {
|
|
48
48
|
gpn, gpnBI
|
|
@@ -166,5 +166,5 @@ export * from './src/square-root.mjs';
|
|
|
166
166
|
export * from './src/tonelli-shanks.mjs';
|
|
167
167
|
|
|
168
168
|
export default [
|
|
169
|
-
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
169
|
+
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
170
170
|
];
|
package/package.json
CHANGED
package/src/golden-ratio.mjs
CHANGED
|
@@ -9,11 +9,9 @@ export const goldenRatio = ((5 ** 0.5) + 1) / 2;
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Calculates the Golden Ratio (phi) to an arbitrary precision using BigInt.
|
|
12
|
-
* Returns a boxed BigInt object with an overridden toString() method to
|
|
13
|
-
* automatically format the output with the correct decimal point placement.
|
|
14
12
|
*
|
|
15
13
|
* @param {number|bigint} precision - The number of decimal places to calculate.
|
|
16
|
-
* @returns {BigInt} A
|
|
14
|
+
* @returns {BigInt} A primitive BigInt representing the Golden Ratio scaled by 10^precision.
|
|
17
15
|
* @throws {Error} If precision is negative.
|
|
18
16
|
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IOZQDYBMCmA7AJQEM4QoAhASQQF8EAzGKMBAIgAFUBrAVwGMwIHDABeAeh6kMAZ1YBuYMHTZ8xUhUoKgA|▶ Try it live in Instacode}
|
|
19
17
|
*/
|
|
@@ -24,17 +22,22 @@ export const goldenRatioBI = precision => {
|
|
|
24
22
|
|
|
25
23
|
// (sqrt(5 * exp^2) + exp) / 2
|
|
26
24
|
const result = (squareRootBI(5n * exp * exp) + exp) / 2n;
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Calculates the Golden Ratio (phi) to an arbitrary precision and returns it as a formatted string.
|
|
30
|
+
* @param {number|bigint} precision - The number of decimal places to calculate.
|
|
31
|
+
* @returns {string} The formatted Golden Ratio string (e.g., '1.618...').
|
|
32
|
+
* @throws {Error} If precision is negative.
|
|
33
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IOZQDYBMCmA7AJQEM4QoBlOGBAXwQDMYowEAiAAVQGsBXAYzAgcMAF4B6HqQwBnVgG5gwdNnzFSFKgqA|▶ Try it live in Instacode}
|
|
34
|
+
*/
|
|
35
|
+
export const goldenRatioStr = precision => {
|
|
36
|
+
if (precision < 0) throw new Error('Precision cannot be negative');
|
|
37
|
+
const str = goldenRatioBI(precision).toString();
|
|
38
|
+
if (precision === 0 || precision === 0n) return str;
|
|
27
39
|
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const str = result.toString();
|
|
32
|
-
if (precision === 0) return str;
|
|
33
|
-
|
|
34
|
-
// The Golden Ratio is ~1.618, so the integer part is always '1'.
|
|
35
|
-
// The length of str is exactly precision + 1.
|
|
36
|
-
return str.slice(0, 1) + '.' + str.slice(1);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return boxedResult;
|
|
40
|
+
// The Golden Ratio is ~1.618, so the integer part is always '1'.
|
|
41
|
+
// The length of str is exactly precision + 1.
|
|
42
|
+
return str.slice(0, 1) + '.' + str.slice(1);
|
|
40
43
|
};
|