Skip to content

Commit

Permalink
feat(math): add signedPow(), add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 25, 2024
1 parent 5e09baf commit 5207ba3
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/math/src/abs.ts
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;

0 comments on commit 5207ba3

Please sign in to comment.