A supplier raises prices 8%, rent and wages go up 5%. Two numbers answer "can we absorb it?": the price rise that wins the money back, and the largest rise the business can take and still break even. They need opposite rounding directions, which is an easy bug to ship. Money here is integer cents in BigInt, percentages are basis points.

// money in integer cents (BigInt), percentages in basis points (1% = 100) const ceilDiv = (a, b) => (a + b - 1n) / b; function costRise({ salesC, marginBp, otherC, directRiseBp, otherRiseBp }) { const directC = salesC - (salesC * marginBp) / 10000n; // sales − gross profit const profitC = salesC - directC - otherC; const extraC = (directC * directRiseBp + otherC * otherRiseBp) / 10000n; // price rise that wins the money back, rounded UP to 0.1% (10 bp) const priceRiseBp = ceilDiv(ceilDiv(extraC * 10000n, salesC), 10n) * 10n; // largest equal rise on every cost that still breaks even, rounded DOWN to 0.01% (1 bp) const breakEvenBp = ((salesC - directC - otherC) * 10000n) / (directC + otherC); return { directC, profitC, extraC, newProfitC: profitC - extraC, priceRiseBp, breakEvenBp }; } console.log(costRise({ salesC: 8500000n, marginBp: 4200n, otherC: 3100000n, directRiseBp: 800n, otherRiseBp: 500n })); // directC 4930000n ($49,300), profitC 470000n ($4,700), extraC 549400n ($5,494), // newProfitC -79400n (−$794), priceRiseBp 650n (6.5%), breakEvenBp 585n (5.85%) 

Why the two directions differ:

  1. The price rise is rounded up (to 0.1%). 6.47% rounded to nearest would be 6.5% here, but in other inputs nearest rounding gives a rise that leaves you a few dollars short every month. A recommendation to charge more must cover the gap, so it rounds up.
  2. The break-even limit is rounded down (to 0.01%). 5.853% becomes 5.85%. Rounding it up would tell you a rise is survivable when, by a few cents, it is not.
  3. Integer division in BigInt truncates toward zero. That is a floor only for non-negative values, which is why newProfitC is computed by subtraction and never divided.

The pair of assertions that keeps the directions honest:

const r = costRise({ salesC: 8500000n, marginBp: 4200n, otherC: 3100000n, directRiseBp: 800n, otherRiseBp: 500n }); console.assert((8500000n * r.priceRiseBp) / 10000n >= r.extraC); // the price rise covers the extra cost console.assert(((4930000n + 3100000n) * (10000n + r.breakEvenBp)) / 10000n <= 8500000n); // limit still breaks even 

The same arithmetic runs in our free Cost Increase Calculator (no sign-up): https://gettidycents.com/tools/cost-increase-calculator

How do you document the rounding direction of each output in your own code?