Merge main into clamp branch, keeping mod and clamp exports
2 files+14−10/2 viewed
| 9 | 9 | return a * b; | |
| 10 | 10 | } | |
| 11 | 11 | ||
| 12 | + | /** Returns the non-negative remainder of a divided by b; throws RangeError when b is 0. */ | |
| 13 | + | function mod(a, b) { | |
| 14 | + | if (b === 0) { | |
| 15 | + | throw new RangeError("mod by zero"); | |
| 16 | + | } | |
| 17 | + | return ((a % b) + Math.abs(b)) % Math.abs(b); | |
| 18 | + | } | |
| 19 | + | ||
| 12 | 20 | /** Returns a minus b. */ | |
| 13 | 21 | function subtract(a, b) { | |
| 14 | 22 | return a - b; | |
| ⋯ | |||
| 67 | 75 | return Math.min(Math.max(value, min), max); | |
| 68 | 76 | } | |
| 69 | 77 | ||
| 70 | − | module.exports = { add, multiply, subtract, divide, power, mean, median, standard_deviation, clamp }; | |
| 78 | + | module.exports = { add, multiply, subtract, divide, mod, power, mean, median, standard_deviation, clamp }; | |
| 6 | 6 | assert.strictEqual(math.multiply(4, 5), 20); | |
| 7 | 7 | assert.strictEqual(math.divide(10, 4), 2.5); | |
| 8 | 8 | assert.throws(() => math.divide(1, 0), RangeError); | |
| 9 | + | assert.strictEqual(math.mod(7, 3), 1); | |
| 10 | + | assert.strictEqual(math.mod(-1, 3), 2); | |
| 11 | + | assert.strictEqual(math.mod(6, 3), 0); | |
| 12 | + | assert.strictEqual(math.mod(-1, -3), 2); | |
| 13 | + | assert.throws(() => math.mod(1, 0), RangeError); | |
| 9 | 14 | assert.strictEqual(math.power(2, 10), 1024); | |
| 10 | 15 | assert.strictEqual(math.power(5, 0), 1); | |
| 11 | 16 | assert.throws(() => math.power(2, -1), RangeError); |