Functional Weave
Code in Rust

finance.margin@1.0.0

README.md

1,097 bytes · view raw

# finance.margin

Margin is profit over PRICE. Markup is profit over COST. They are not the same
number and they are not interchangeable: buy at 10.00, sell at 15.00, and that
is a 50% markup but only a 33.33% margin.

Quoting the markup where a margin was meant is how a retailer prices a whole
range to a target it never actually hits, so this returns both and the caller
has to pick one on purpose.

To go the other way: markup = margin / (1 - margin), margin = markup / (1 +
markup).

Both ratios are returned in basis points (10000 = 100%), rounded half-up,
because the registry never puts a rate in a float.

Division by zero is reported as null rather than an error, because both zeroes
are real trade: a free sample has zero cost and therefore no meaningful markup
(any profit is infinitely more than nothing), and a giveaway has zero price and
therefore no meaningful margin. The other ratio, the profit and the currency are
still correct in both cases, so failing the whole call would throw away good
answers. Callers that treat null as 0% will understate; treat it as "not
applicable".