-
Notifications
You must be signed in to change notification settings - Fork 653
feat(math/unstable): add math
package with basic math utilities
#6823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lionel-rowe
wants to merge
7
commits into
denoland:main
Choose a base branch
from
lionel-rowe:math-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+406
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da04151
feat(math/unstable): add `math` package with basic math utilities
lionel-rowe 9b2ea23
Add unary `end` signature to `integerRange` function with implied `st…
lionel-rowe 004dee2
Change IntegerRange to an iterable class with step method
lionel-rowe 7258ef6
Remove IntegerRange
lionel-rowe c844649
Check modulo parity with python `%` operator (floored modulo)
lionel-rowe 67bd2a4
Full parity with python `%` operator (floored modulo)
lionel-rowe be0467a
Copy edits
lionel-rowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
"./io", | ||
"./json", | ||
"./jsonc", | ||
"./math", | ||
"./media_types", | ||
"./msgpack", | ||
"./net", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ | |
"./io", | ||
"./json", | ||
"./jsonc", | ||
"./math", | ||
"./media_types", | ||
"./msgpack", | ||
"./net", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** | ||
* Clamp a number within the inclusive [min, max] range. | ||
* | ||
* @param num The number to be clamped | ||
* @param limits The inclusive [min, max] range | ||
* @returns The clamped number | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { clamp } from "@std/math/clamp"; | ||
* import { assertEquals } from "@std/assert"; | ||
* assertEquals(clamp(5, [1, 10]), 5); | ||
* assertEquals(clamp(-5, [1, 10]), 1); | ||
* assertEquals(clamp(15, [1, 10]), 10); | ||
* ``` | ||
*/ | ||
export function clamp(num: number, limits: [min: number, max: number]): number { | ||
const [min, max] = limits; | ||
if (min > max) { | ||
throw new RangeError("`min` must be less than or equal to `max`"); | ||
} | ||
|
||
return Math.min(Math.max(num, min), max); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
import { clamp } from "./clamp.ts"; | ||
import { assert, assertEquals, assertThrows } from "@std/assert"; | ||
|
||
Deno.test("clamp()", async (t) => { | ||
await t.step("basic functionality", () => { | ||
assertEquals(clamp(5, [1, 10]), 5); | ||
assertEquals(clamp(-5, [1, 10]), 1); | ||
assertEquals(clamp(15, [1, 10]), 10); | ||
}); | ||
|
||
await t.step("NaN", () => { | ||
assertEquals(clamp(NaN, [0, 1]), NaN); | ||
assertEquals(clamp(0, [NaN, 0]), NaN); | ||
assertEquals(clamp(0, [0, NaN]), NaN); | ||
}); | ||
|
||
await t.step("infinities", () => { | ||
assertEquals(clamp(5, [0, Infinity]), 5); | ||
assertEquals(clamp(-5, [-Infinity, 10]), -5); | ||
}); | ||
|
||
await t.step("+/-0", () => { | ||
assert(Object.is(clamp(0, [0, 1]), 0)); | ||
assert(Object.is(clamp(-0, [-0, 1]), -0)); | ||
|
||
assert(Object.is(clamp(-2, [0, 1]), 0)); | ||
assert(Object.is(clamp(-2, [-0, 1]), -0)); | ||
assert(Object.is(clamp(2, [-1, 0]), 0)); | ||
assert(Object.is(clamp(2, [-1, -0]), -0)); | ||
|
||
assert(Object.is(clamp(-0, [0, 1]), 0)); | ||
assert(Object.is(clamp(0, [-0, 1]), 0)); | ||
|
||
assert(Object.is(clamp(-0, [-1, 0]), -0)); | ||
assert(Object.is(clamp(0, [-1, -0]), -0)); | ||
}); | ||
|
||
await t.step("errors", () => { | ||
assertThrows( | ||
() => clamp(5, [10, 1]), | ||
RangeError, | ||
"`min` must be less than or equal to `max`", | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@std/math", | ||
"version": "0.0.0", | ||
"exports": { | ||
".": "./mod.ts", | ||
"./clamp": "./clamp.ts", | ||
"./modulo": "./modulo.ts", | ||
"./round-to": "./round_to.ts" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** | ||
* Math functions such as modulo and clamp. | ||
* | ||
* ```ts | ||
* import { clamp, modulo } from "@std/math"; | ||
* import { assertEquals } from "@std/assert"; | ||
* | ||
* for (let n = -3; n <= 3; ++n) { | ||
* const val = n * 12 + 5; | ||
* // 5 o'clock is always 5 o'clock, no matter how many twelve-hour cycles you add or remove | ||
* assertEquals(modulo(val, 12), 5); | ||
* assertEquals(clamp(val, [0, 11]), n === 0 ? 5 : n > 0 ? 11 : 0); | ||
* } | ||
* ``` | ||
* | ||
* @module | ||
*/ | ||
|
||
export * from "./clamp.ts"; | ||
export * from "./modulo.ts"; | ||
export * from "./round_to.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** | ||
* Computes the floored modulo of a number. | ||
* | ||
* @param num The number to be reduced | ||
* @param modulus The modulus | ||
* @returns The reduced number | ||
* | ||
* @example Usage | ||
* ```ts | ||
* import { modulo } from "@std/math/modulo"; | ||
* import { assertEquals } from "@std/assert"; | ||
* | ||
* for (let n = -3; n <= 3; ++n) { | ||
* const val = n * 12 + 5; | ||
* // 5 o'clock is always 5 o'clock, no matter how many twelve-hour cycles you add or remove | ||
* assertEquals(modulo(val, 12), 5); | ||
* } | ||
* ``` | ||
*/ | ||
export function modulo(num: number, modulus: number): number { | ||
if (!Number.isFinite(num) || Number.isNaN(modulus) || modulus === 0) { | ||
return NaN; | ||
} | ||
if (num === 0) return modulus < 0 ? -0 : 0; | ||
if (modulus === Infinity) return num < 0 ? Infinity : num; | ||
if (modulus === -Infinity) return num > 0 ? -Infinity : num; | ||
|
||
return (num % modulus + modulus) % modulus; | ||
} | ||
Comment on lines
+23
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an alternative implementation for export function modulo(num: number, modulus: number): number {
num %= modulus;
if (num === 0) {
num = modulus < 0 ? -0 : 0;
} else if ((num < 0) !== (modulus < 0)) {
num += modulus;
}
return num;
} In my benchmarks (code below), it was faster than the current implementation in the common case where both inputs are finite and nonzero, although slower when either input is infinite, NaN or 0. All tests still pass. // `baseline` and `modulo` are different implementations.
// `cases` is taken from the python parity test.
const sink = new Set<number>();
for (const [a, b] of cases) {
const group = Deno.inspect([a, b]);
Deno.bench("baseline", { group }, () => {
sink.add(baseline(a, b));
});
Deno.bench("modulo", { group }, () => {
sink.add(modulo(a, b));
});
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
import { modulo } from "./modulo.ts"; | ||
import { assert, assertEquals } from "@std/assert"; | ||
|
||
Deno.test("modulo()", async (t) => { | ||
await t.step("basic functionality", async (t) => { | ||
for (let n = -3; n <= 3; ++n) { | ||
const val = n * 12 + 5; | ||
await t.step(`modulo(${val}, 12) == 5`, () => { | ||
assertEquals(modulo(val, 12), 5); | ||
}); | ||
} | ||
}); | ||
|
||
await t.step("non-integer values", () => { | ||
assertEquals(modulo(5.5, 2), 1.5); | ||
assertEquals(modulo(-5.5, 2), 0.5); | ||
assertEquals(modulo(5, 0.5), 0); | ||
assertEquals(modulo(-5.5, 0.5), 0); | ||
}); | ||
|
||
await t.step("edge cases", () => { | ||
assertEquals(modulo(NaN, 5), NaN); | ||
assertEquals(modulo(5, NaN), NaN); | ||
assertEquals(modulo(Infinity, 5), NaN); | ||
assertEquals(modulo(-Infinity, 5), NaN); | ||
assertEquals(modulo(5, Infinity), 5); | ||
assertEquals(modulo(5, -Infinity), -Infinity); | ||
assertEquals(modulo(5, 0), NaN); | ||
assertEquals(modulo(5, -0), NaN); | ||
assert(Object.is(modulo(0, 5), 0)); | ||
assert(Object.is(modulo(-0, 5), 0)); | ||
}); | ||
|
||
await t.step("parity with python `%` operator (floored modulo)", () => { | ||
/** | ||
* ```python | ||
* def modulo(a, b): | ||
* try: return a % b | ||
* except: return float("nan") | ||
* xs = [float('inf'), float('-inf'), float('nan'), 0.0, -0.0, 0.5, 1.0, 2.0, -0.5, -1.0, -2.0] | ||
* cases = [(a, b, modulo(a, b)) for a in xs for b in xs] | ||
* ``` | ||
*/ | ||
const cases: [a: number, b: number, result: number][] = [ | ||
[Infinity, Infinity, NaN], | ||
[Infinity, -Infinity, NaN], | ||
[Infinity, NaN, NaN], | ||
[Infinity, 0.0, NaN], | ||
[Infinity, -0.0, NaN], | ||
[Infinity, 0.5, NaN], | ||
[Infinity, 1.0, NaN], | ||
[Infinity, 2.0, NaN], | ||
[Infinity, -0.5, NaN], | ||
[Infinity, -1.0, NaN], | ||
[Infinity, -2.0, NaN], | ||
[-Infinity, Infinity, NaN], | ||
[-Infinity, -Infinity, NaN], | ||
[-Infinity, NaN, NaN], | ||
[-Infinity, 0.0, NaN], | ||
[-Infinity, -0.0, NaN], | ||
[-Infinity, 0.5, NaN], | ||
[-Infinity, 1.0, NaN], | ||
[-Infinity, 2.0, NaN], | ||
[-Infinity, -0.5, NaN], | ||
[-Infinity, -1.0, NaN], | ||
[-Infinity, -2.0, NaN], | ||
[NaN, Infinity, NaN], | ||
[NaN, -Infinity, NaN], | ||
[NaN, NaN, NaN], | ||
[NaN, 0.0, NaN], | ||
[NaN, -0.0, NaN], | ||
[NaN, 0.5, NaN], | ||
[NaN, 1.0, NaN], | ||
[NaN, 2.0, NaN], | ||
[NaN, -0.5, NaN], | ||
[NaN, -1.0, NaN], | ||
[NaN, -2.0, NaN], | ||
[0.0, Infinity, 0.0], | ||
[0.0, -Infinity, -0.0], | ||
[0.0, NaN, NaN], | ||
[0.0, 0.0, NaN], | ||
[0.0, -0.0, NaN], | ||
[0.0, 0.5, 0.0], | ||
[0.0, 1.0, 0.0], | ||
[0.0, 2.0, 0.0], | ||
[0.0, -0.5, -0.0], | ||
[0.0, -1.0, -0.0], | ||
[0.0, -2.0, -0.0], | ||
[-0.0, Infinity, 0.0], | ||
[-0.0, -Infinity, -0.0], | ||
[-0.0, NaN, NaN], | ||
[-0.0, 0.0, NaN], | ||
[-0.0, -0.0, NaN], | ||
[-0.0, 0.5, 0.0], | ||
[-0.0, 1.0, 0.0], | ||
[-0.0, 2.0, 0.0], | ||
[-0.0, -0.5, -0.0], | ||
[-0.0, -1.0, -0.0], | ||
[-0.0, -2.0, -0.0], | ||
[0.5, Infinity, 0.5], | ||
[0.5, -Infinity, -Infinity], | ||
[0.5, NaN, NaN], | ||
[0.5, 0.0, NaN], | ||
[0.5, -0.0, NaN], | ||
[0.5, 0.5, 0.0], | ||
[0.5, 1.0, 0.5], | ||
[0.5, 2.0, 0.5], | ||
[0.5, -0.5, -0.0], | ||
[0.5, -1.0, -0.5], | ||
[0.5, -2.0, -1.5], | ||
[1.0, Infinity, 1.0], | ||
[1.0, -Infinity, -Infinity], | ||
[1.0, NaN, NaN], | ||
[1.0, 0.0, NaN], | ||
[1.0, -0.0, NaN], | ||
[1.0, 0.5, 0.0], | ||
[1.0, 1.0, 0.0], | ||
[1.0, 2.0, 1.0], | ||
[1.0, -0.5, -0.0], | ||
[1.0, -1.0, -0.0], | ||
[1.0, -2.0, -1.0], | ||
[2.0, Infinity, 2.0], | ||
[2.0, -Infinity, -Infinity], | ||
[2.0, NaN, NaN], | ||
[2.0, 0.0, NaN], | ||
[2.0, -0.0, NaN], | ||
[2.0, 0.5, 0.0], | ||
[2.0, 1.0, 0.0], | ||
[2.0, 2.0, 0.0], | ||
[2.0, -0.5, -0.0], | ||
[2.0, -1.0, -0.0], | ||
[2.0, -2.0, -0.0], | ||
[-0.5, Infinity, Infinity], | ||
[-0.5, -Infinity, -0.5], | ||
[-0.5, NaN, NaN], | ||
[-0.5, 0.0, NaN], | ||
[-0.5, -0.0, NaN], | ||
[-0.5, 0.5, 0.0], | ||
[-0.5, 1.0, 0.5], | ||
[-0.5, 2.0, 1.5], | ||
[-0.5, -0.5, -0.0], | ||
[-0.5, -1.0, -0.5], | ||
[-0.5, -2.0, -0.5], | ||
[-1.0, Infinity, Infinity], | ||
[-1.0, -Infinity, -1.0], | ||
[-1.0, NaN, NaN], | ||
[-1.0, 0.0, NaN], | ||
[-1.0, -0.0, NaN], | ||
[-1.0, 0.5, 0.0], | ||
[-1.0, 1.0, 0.0], | ||
[-1.0, 2.0, 1.0], | ||
[-1.0, -0.5, -0.0], | ||
[-1.0, -1.0, -0.0], | ||
[-1.0, -2.0, -1.0], | ||
[-2.0, Infinity, Infinity], | ||
[-2.0, -Infinity, -2.0], | ||
[-2.0, NaN, NaN], | ||
[-2.0, 0.0, NaN], | ||
[-2.0, -0.0, NaN], | ||
[-2.0, 0.5, 0.0], | ||
[-2.0, 1.0, 0.0], | ||
[-2.0, 2.0, 0.0], | ||
[-2.0, -0.5, -0.0], | ||
[-2.0, -1.0, -0.0], | ||
[-2.0, -2.0, -0.0], | ||
]; | ||
|
||
for (const [a, b, result] of cases) { | ||
const actual = modulo(a, b); | ||
assert( | ||
Object.is(actual, result), | ||
`modulo(${Deno.inspect(a)}, ${Deno.inspect(b)}) == ${ | ||
Deno.inspect(result) | ||
} (actual ${Deno.inspect(actual)})`, | ||
); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Passing
limits
as an array means that this array needs to be allocated prior to passing it to this function. Given that math functions are often used in high performance scenarios it might be more desirable to avoid the need to allocate at all for determining whether a value is in-between two others.