Functional Weave
Code in Rust

math.percent-change@1.0.0

README.md

1,182 bytes · view raw

# math.percent-change

`(toValue - fromValue) / |fromValue|`, in basis points: 100 to 150 is 5000
(+50%), 150 to 100 is -3333 (-33.33% half-up). The inputs are integers in any
unit - minor units of money, counts, basis points - and the rounding to a
whole basis point uses the `math.round-div` mode you pass, so no float is
involved.

**Dividing by the absolute value.** From -100 to -50 is an improvement, and
this returns +5000 for it. Dividing by the signed starting value would report
-5000, a fall, for a loss that halved. The sign of the result is always the
direction of the change.

**From zero** there is no percentage change: growth from nothing is not
infinite percent, and it is not zero percent either. The result is null, as
`finance.margin` does for its undefined ratios, so a dashboard can show "new"
or "n/a" rather than a number that means nothing. Treating null as 0 will
understate.

**Limits.** `|toValue - fromValue|` must be at most 900,719,925,474 (so that
the difference times 10000 is still an exact integer in JavaScript); a larger
difference is an error rather than a rounded answer. Values in minor units up
to nine billion pounds are well inside that.