fraction.mjs - <short description TODO> Copyright (C) 2022 Strudel contributors - see https://codeberg.org/uzu/strudel/src/branch/main/packages/core/fraction.mjs This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Returns the start of the cycle.
Returns the start of the next cycle.
Returns a TimeSpan representing the begin and end of the Time value's cycle
The position of a time value relative to the start of its cycle.
31Fraction.prototype.lt = function (other) { 32 return this.compare(other) < 0; 33}; 34 35Fraction.prototype.gt = function (other) { 36 return this.compare(other) > 0; 37}; 38 39Fraction.prototype.lte = function (other) { 40 return this.compare(other) <= 0; 41}; 42 43Fraction.prototype.gte = function (other) { 44 return this.compare(other) >= 0; 45}; 46 47Fraction.prototype.eq = function (other) { 48 return this.compare(other) == 0; 49}; 50 51Fraction.prototype.ne = function (other) { 52 return this.compare(other) != 0; 53}; 54 55Fraction.prototype.max = function (other) { 56 return this.gt(other) ? this : other; 57}; 58 59Fraction.prototype.maximum = function (...others) { 60 others = others.map((x) => new Fraction(x)); 61 return others.reduce((max, other) => other.max(max), this); 62}; 63 64Fraction.prototype.min = function (other) { 65 return this.lt(other) ? this : other; 66}; 67 68Fraction.prototype.mulmaybe = function (other) { 69 return other !== undefined ? this.mul(other) : undefined; 70}; 71 72Fraction.prototype.divmaybe = function (other) { 73 return other !== undefined ? this.div(other) : undefined; 74}; 75 76Fraction.prototype.addmaybe = function (other) { 77 return other !== undefined ? this.add(other) : undefined; 78}; 79 80Fraction.prototype.submaybe = function (other) { 81 return other !== undefined ? this.sub(other) : undefined; 82}; 83 84Fraction.prototype.show = function (/* excludeWhole = false */) { 85 // return this.toFraction(excludeWhole); 86 return this.s * this.n + '/' + this.d; 87}; 88 89Fraction.prototype.or = function (other) { 90 return this.eq(0) ? other : this; 91}; 92 93const fraction = (n) => { 94 if (typeof n === 'number') { 95 /* 96 https://github.com/infusion/Fraction.js/#doubles 97 „If you pass a double as it is, Fraction.js will perform a number analysis based on Farey Sequences." 98 „If you want to keep the number as it is, convert it to a string, as the string parser will not perform any further observations“
-> those farey sequences turn out to make pattern querying ~20 times slower! always use strings! -> still, some optimizations could be done: .mul .div .add .sub calls still use numbers
n = String(n); // this is actually faster but imprecise...
108export const gcd = (...fractions) => { 109 fractions = removeUndefineds(fractions); 110 if (fractions.length === 0) { 111 return undefined; 112 } 113 114 return fractions.reduce((gcd, fraction) => gcd.gcd(fraction), fraction(1)); 115}; 116 117export const lcm = (...fractions) => { 118 fractions = removeUndefineds(fractions); 119 if (fractions.length === 0) { 120 return undefined; 121 } 122 const x = fractions.pop(); 123 return fractions.reduce( 124 (lcm, fraction) => (lcm === undefined || fraction === undefined ? undefined : lcm.lcm(fraction)), 125 x, 126 ); 127}; 128 129export const isFraction = (x) => x instanceof Fraction; 130 131fraction._original = Fraction; 132 133export default fraction;
"If you concern performance, cache Fraction.js objects and pass arrays/objects.“ -> tested memoized version, but it's slower than unmemoized, even with repeated evaluation const memo = {}; const memoizedFraction = (n) => { if (typeof n === 'number') { n = String(n); } if (memo[n] !== undefined) { return memo[n]; } memo[n] = Fraction(n); return memo[n]; };