Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Add math helpers, tests and checks | 1 | // Small helpers for numbers. |
| Merge #3: Add multiply to math.js | 2 | /** Returns the sum of a and b. */ |
| Add math helpers, tests and checks | 3 | function add(a, b) { |
| 4 | return a + b; | |
| 5 | } | |
| 6 | ||
| Merge #3: Add multiply to math.js | 7 | /** Returns the product of a and b. */ |
| 8 | function multiply(a, b) { | |
| 9 | return a * b; | |
| 10 | } | |
| 11 | ||
| Merge main into clamp branch | 12 | /** Returns a minus b. */ |
| 13 | function subtract(a, b) { | |
| 14 | return a - b; | |
| 15 | } | |
| 16 | ||
| Add power function to math.js | 17 | /** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */ |
| 18 | function power(base, exponent) { | |
| 19 | if (exponent < 0) { | |
| 20 | throw new RangeError("exponent must not be negative"); | |
| 21 | } | |
| 22 | let result = 1; | |
| 23 | for (let i = 0; i < exponent; i++) { | |
| 24 | result *= base; | |
| 25 | } | |
| 26 | return result; | |
| 27 | } | |
| 28 | ||
| Add mean, median, and standard_deviation to math.js | 29 | /** Returns the average of numbers; throws a TypeError if the array is empty. */ |
| 30 | function mean(numbers) { | |
| 31 | if (numbers.length === 0) { | |
| 32 | throw new TypeError("numbers must not be empty"); | |
| 33 | } | |
| 34 | return numbers.reduce((sum, n) => sum + n, 0) / numbers.length; | |
| 35 | } | |
| 36 | ||
| 37 | /** Returns the middle value of numbers (the average of the two middle values when the length is even); throws a TypeError if the array is empty. */ | |
| 38 | function median(numbers) { | |
| 39 | if (numbers.length === 0) { | |
| 40 | throw new TypeError("numbers must not be empty"); | |
| 41 | } | |
| 42 | const sorted = [...numbers].sort((a, b) => a - b); | |
| 43 | const mid = Math.floor(sorted.length / 2); | |
| 44 | return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid]; | |
| 45 | } | |
| 46 | ||
| 47 | /** Returns the population standard deviation of numbers; throws a TypeError if the array is empty. */ | |
| 48 | function standard_deviation(numbers) { | |
| 49 | const avg = mean(numbers); | |
| 50 | const variance = numbers.reduce((sum, n) => sum + (n - avg) ** 2, 0) / numbers.length; | |
| 51 | return Math.sqrt(variance); | |
| 52 | } | |
| 53 | ||
| Add clamp function to math.js | 54 | /** Returns value limited to the range [min, max]; throws a RangeError if min is greater than max. */ |
| 55 | function clamp(value, min, max) { | |
| 56 | if (min > max) { | |
| 57 | throw new RangeError("min must not be greater than max"); | |
| 58 | } | |
| 59 | return Math.min(Math.max(value, min), max); | |
| 60 | } | |
| 61 | ||
| Merge main into clamp branch | 62 | module.exports = { add, multiply, subtract, power, mean, median, standard_deviation, clamp }; |
This file's history is long; its oldest lines are credited to the oldest commit read.