-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package of numerical math functions
(closes #269)
- Loading branch information
Showing
8 changed files
with
126 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,20 @@ | ||
/** | ||
* Constants | ||
* @module | ||
*/ | ||
|
||
export const TAU = 2 * Math.PI; | ||
export const DEG2RAD = Math.PI / 180; | ||
export const RAD2DEG = 180 / Math.PI; | ||
export const HALF_PI = Math.PI / 2; | ||
|
||
export const MIN_Z = 0; | ||
export const MAX_Z = 24; | ||
export const MIN_K = (256 * Math.pow(2, MIN_Z)) / TAU; | ||
export const MAX_K = (256 * Math.pow(2, MAX_Z)) / TAU; | ||
|
||
export const MAX_PHI = 2 * Math.atan(Math.exp(Math.PI)) - HALF_PI; // 85.0511287798 in radians | ||
export const MIN_PHI = -MAX_PHI; | ||
|
||
export const EQUATORIAL_RADIUS = 6378137.0; | ||
export const POLAR_RADIUS = 6356752.314245179; |
This file contains 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 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 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,38 @@ | ||
/** | ||
* 🔢 Numeric math functions | ||
* @module | ||
*/ | ||
|
||
|
||
/** Clamp a number within a min..max range | ||
* @param num | ||
* @param min | ||
* @param max | ||
* @returns result | ||
* @example ``` | ||
* numClamp(-1, 0, 10); // returns 0, (below min) | ||
* numClamp(5, 0, 10); // returns 5, (in range) | ||
* numClamp(11, 0, 10); // returns 10, (above max) | ||
* ``` | ||
*/ | ||
export function numClamp(num: number, min: number, max: number): number { | ||
return Math.max(min, Math.min(num, max)); | ||
} | ||
|
||
|
||
/** Wrap a number around a min..max range | ||
* Similar to modulo, but works for negative numbers too. | ||
* @param num | ||
* @param min | ||
* @param max | ||
* @returns result | ||
* @example ``` | ||
* numWrap(-1, 0, 10); // returns 9, (below min) | ||
* numWrap(5, 0, 10); // returns 5, (in range) | ||
* numWrap(11, 0, 10); // returns 1, (above max) | ||
* ``` | ||
*/ | ||
export function numWrap(num: number, min: number, max: number): number { | ||
const d = max - min; | ||
return ((num - min) % d + d) % d + min; | ||
} |
This file contains 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,38 @@ | ||
import { describe, it } from 'node:test'; | ||
import { strict as assert } from 'node:assert'; | ||
import * as test from '../built/math.mjs'; | ||
|
||
|
||
assert.closeTo = function(a, b, epsilon = 1e-6) { | ||
if (Math.abs(a - b) > epsilon) { | ||
assert.fail(`${a} is not close to ${b} within ${epsilon}`); | ||
} | ||
} | ||
|
||
describe('math/number', () => { | ||
describe('numClamp', () => { | ||
it('clamps integers within a min..max range', () => { | ||
assert.equal(test.numClamp(-1, 0, 10), 0); | ||
assert.equal(test.numClamp(5, 0, 10), 5); | ||
assert.equal(test.numClamp(11, 0, 10), 10); | ||
}); | ||
it('clamps floats within a min..max range', () => { | ||
assert.equal(test.numClamp(-Math.PI, 0, 2 * Math.PI), 0); | ||
assert.equal(test.numClamp(Math.PI, 0, 2 * Math.PI), Math.PI); | ||
assert.equal(test.numClamp(3 * Math.PI, 0, 2 * Math.PI), 2 * Math.PI); | ||
}); | ||
}); | ||
|
||
describe('numWrap', () => { | ||
it('wraps integers around a min..max range', () => { | ||
assert.equal(test.numWrap(-1, 0, 10), 9); | ||
assert.equal(test.numWrap(5, 0, 10), 5); | ||
assert.equal(test.numWrap(11, 0, 10), 1); | ||
}); | ||
it('wraps floats around a min..max range', () => { | ||
assert.closeTo(test.numWrap(-Math.PI, 0, 2 * Math.PI), Math.PI); | ||
assert.closeTo(test.numWrap(Math.PI, 0, 2 * Math.PI), Math.PI); | ||
assert.closeTo(test.numWrap(3 * Math.PI, 0, 2 * Math.PI), Math.PI); | ||
}); | ||
}); | ||
}); |