Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { type RoundingMode, roundDiv } from "./math_round_div.ts"; ← from math.round-div ^1.0.0 · built alongside by fune
// (difference * 10000) must stay an exact integer in a JavaScript number.
const MAX_DIFFERENCE = 900719925474;
/**
* Percentage change from `fromValue` to `toValue`, in basis points.
*
* Divides by |fromValue| so the sign always says which way the value moved,
* even when the starting value is negative.
*/
export function percentChange(fromValue: number, toValue: number, mode: RoundingMode): number | null {
if (!Number.isSafeInteger(fromValue) || !Number.isSafeInteger(toValue)) {
throw new TypeError("percentChange operates on integers only");
}
if (fromValue === 0) return null;
const difference = toValue - fromValue;
if (Math.abs(difference) > MAX_DIFFERENCE) {
throw new RangeError(`the change from ${fromValue} to ${toValue} is too large to express exactly`);
}
return roundDiv(difference * 10000, Math.abs(fromValue), mode);
}