-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(math): add signedPow(), add docs
- Loading branch information
1 parent
5e09baf
commit 5207ba3
Showing
1 changed file
with
25 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
import type { FnN2 } from "@thi.ng/api"; | ||
import { EPS } from "./api.js"; | ||
|
||
export const absDiff: FnN2 = (x, y) => Math.abs(x - y); | ||
/** | ||
* Returns the absolute difference between `a` and `b`. | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const absDiff: FnN2 = (a, b) => Math.abs(a - b); | ||
|
||
/** | ||
* Similar to `Math.sign()`, but uses `eps` to determine the zero value (i.e. if | ||
* `x` is in [-eps,eps] interval). | ||
* | ||
* @param x | ||
* @param eps | ||
*/ | ||
export const sign = (x: number, eps = EPS) => (x > eps ? 1 : x < -eps ? -1 : 0); | ||
|
||
/** | ||
* Raises `x` to `k` power and multiplies it with the {@link sign} of `x`, using | ||
* `eps` to determine zero. | ||
* | ||
* @param x | ||
* @param k | ||
* @param eps | ||
*/ | ||
export const signedPow = (x: number, k: number, eps = EPS) => | ||
sign(x, eps) * Math.abs(x) ** k; |