Functional Weave
Code in TypeScript

inventory.eoq

Economic order quantity (Harris/Wilson): the order size that minimises ordering plus holding cost, in whole units.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 20 tests, run in TypeScript, Python and Rust.

What it does

The economic order quantity of Harris (1913), popularised by Wilson (1934):

EOQ = sqrt(2 * D * S / H)

For example

  • economicOrderQuantity(1,000, £10.00, £0.50, half-up) → 200 a whole answer: 1000 a year, £10 an order, 50p to hold, is 200
  • economicOrderQuantity(1,000, £10.00, £0.50, up) → 200 a perfect square stays put when rounding up
  • economicOrderQuantity(10,000, £40.00, £2.00, half-up) → 632 sqrt(400000) = 632.46 rounds half-up to 632

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

export function economicOrderQuantity(annualDemand: number, orderCost: Money, holdingCost: Money, mode: RoundingMode): number
annualDemandintunits used per year, not negative
orderCostMoneythe fixed cost of placing one order, whatever its size
holdingCostMoneythe cost of holding one unit for a year; same currency, more than zero
modeRoundingModehow sqrt(2DS/H) becomes whole units: half-up, half-even, down or up
returnsintthe order quantity in whole units

Your code names it in one line, in the file that uses it

import { economicOrderQuantity } from "#fune/inventory.eoq@^1";
impl/typescript.ts · 49 lines · open · raw

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 } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { integerSqrt } from "./math_integer_sqrt.ts";  ← from math.integer-sqrt ^1.0.0 · built alongside by fune
import { type Money, assertSameCurrency } from "./money_amount.ts";  ← from money.amount ^1.0.0 · built alongside by fune

/**
 * Economic order quantity, sqrt(2DS / H), in whole units.
 *
 * The root is never taken in floating point: the floor is an exact integer
 * square root and the rounding decision compares whole numbers, so an exact
 * half (sqrt(6.25) = 2.5) rounds by `mode` and not by the language.
 */
export function economicOrderQuantity(annualDemand: number, orderCost: Money, holdingCost: Money, mode: RoundingMode): number {
  if (!Number.isInteger(annualDemand) || annualDemand < 0) {
    throw new RangeError(`annualDemand must be a whole number of units, not negative, received ${annualDemand}`);
  }
  assertSameCurrency(orderCost, holdingCost);
  if (orderCost.minor < 0) {
    throw new RangeError(`orderCost must not be negative, received ${orderCost.minor}`);
  }
  if (holdingCost.minor <= 0) {
    throw new RangeError(`holdingCost must be greater than zero, received ${holdingCost.minor}`);
  }
  const d = BigInt(annualDemand);
  const s = BigInt(orderCost.minor);
  const h = BigInt(holdingCost.minor);
  const eightDS = 8n * d * s;
  if (eightDS > BigInt(Number.MAX_SAFE_INTEGER)) {
    throw new RangeError("annualDemand and orderCost are too large: 8 x demand x order cost must stay within 2^53 - 1");
  }
  const twoDS = 2n * d * s;
  const n = BigInt(integerSqrt(Number(twoDS / h)));
  const odd = 2n * n + 1n;
  switch (mode) {
    case "down":
      return Number(n);
    case "up":
      return Number(n * n * h === twoDS ? n : n + 1n);
    case "half-up":
      return Number(odd * odd * h <= eightDS ? n + 1n : n);
    case "half-even": {
      const half = odd * odd * h;
      if (half < eightDS) return Number(n + 1n);
      if (half > eightDS) return Number(n);
      return Number(n % 2n === 0n ? n : n + 1n);
    }
    default:
      throw new RangeError(`unknown rounding mode "${mode}"`);
  }
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add inventory.eoq
Download for TypeScript inventory.eoq-1.0.0-typescript.fune · 10,183 bytes sha256 fe17662618772e2df0a1c8e25deab2c7ddbd18d8498b5e4850a491889b740d14

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./inventory.eoq-1.0.0-typescript.fune, or fetch it from a terminal with fune pull inventory.eoq@1.0.0:typescript.

The whole function, every language, is one file too: inventory.eoq-1.0.0.fune, 14,944 bytes, sha256 f6c821a1432734350964c729182dcec1ec2b14422daa6830c0e1a6c227918003. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before inventory.eoq

after — your function gets the result and the arguments, and returns the final result.

// fune: after inventory.eoq

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace math.integer-sqrt in inventory.eoq
// fune: replace math.round-div in inventory.eoq
// fune: replace money.amount in inventory.eoq

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show inventory.eoq --steps.

// fune: step inventory.eoq after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
a whole answer: 1000 a year, £10 an order, 50p to hold, is 200 1,000, £10.00, £0.50, half-up → 200
a perfect square stays put when rounding up 1,000, £10.00, £0.50, up → 200
sqrt(400000) = 632.46 rounds half-up to 632 10,000, £40.00, £2.00, half-up → 632
sqrt(400000) = 632.46 rounds up to 633 10,000, £40.00, £2.00, up → 633
a fractional 2DS/H: sqrt(208333.33) = 456.44 rounds to 456 5,000, £25.00, £1.20, half-up → 456
an exact half, sqrt(6.25) = 2.5, rounds half-up to 3 25, £0.01, £0.08, half-up → 3
an exact half, sqrt(6.25) = 2.5, rounds half-even to 2 (Math.round would say 3) 25, £0.01, £0.08, half-even → 2
an exact half, sqrt(12.25) = 3.5, rounds half-even to 4 49, £0.01, £0.08, half-even → 4
just under a half, sqrt(6.24) = 2.498, rounds half-up to 2 312, £0.01, £1.00, half-up → 2
a hair over a whole number, sqrt(40000.01), rounds up to 201 4,000,001, £0.01, £2.00, up → 201
Show the other 10 tests
CaseArgumentsExpected
a hair over a whole number, sqrt(40000.01), rounds down to 200 4,000,001, £0.01, £2.00, down → 200
no demand orders nothing, even rounding up 0, €10.00, €0.50, up → 0
a free order gives zero 1,000, €0.00, €0.50, half-up → 0
costs in two currencies are an error 1,000, £10.00, €0.50, half-up → error: currency mismatch
a zero holding cost is an error 1,000, £10.00, £0.00, half-up → error: holdingCost must be greater than zero
negative demand is an error -1, £10.00, £0.50, half-up → error: annualDemand must be a whole number of units, not negative
fractional demand is an error 10.5, £10.00, £0.50, half-up → error: annualDemand must be a whole number of units, not negative
a negative order cost is an error 1,000, -£0.01, £0.50, half-up → error: orderCost must not be negative
8DS beyond 2^53 - 1 is an error 1,000,000,000, £100,000.00, £0.50, half-up → error: too large
an unknown rounding mode is an error 1,000, £10.00, £0.50, nearest → error: unknown rounding mode

More from the author

where `D` is annual demand in units, `S` the fixed cost of placing an order and `H` the cost of holding one unit in stock for a year. It is the order size at which annual ordering cost (`D / Q * S`) equals annual holding cost (`Q / 2 * H`), and their sum is least.

**Whole units, rounded exactly.** The answer is a square root, so it is rarely whole. Rounding it through floating point is where implementations disagree: `sqrt(6.25)` is exactly 2.5, and JavaScript's `Math.round` makes that 3 while Python's `round` makes it 2. Here the root is never taken in floating point. The floor comes from `math.integer-sqrt` applied to `floor(2DS / H)` (the floor of a square root only changes at whole numbers, so this is exact), and the rounding decision compares whole numbers:

- `down`: the floor. - `up`: the floor, plus one unless `2DS / H` is a perfect square. - `half-up`: plus one when `(2n + 1)^2 * H <= 8DS`, i.e. the root is at least `n + 0.5`. - `half-even`: as half-up, but an exact half goes to the even neighbour.

`half-up` is the usual choice; `up` never under-orders.

**Money is exact.** `orderCost` and `holdingCost` are `Money` in minor units and must be in the same currency; the currencies cancel, so the result is a plain unit count. A holding cost quoted as a percentage of unit cost (a 25% carrying rate) is `money.apply-rate(unitCost, 2500, ...)` first. `8 * D * S` has to stay within 2^53 - 1, which allows, for example, a million units a year at £1,000,000 an order.

Zero demand gives 0. Holding cost must be positive (with free holding the EOQ is unbounded) and neither demand nor order cost may be negative.

Sources: F. W. Harris, "How Many Parts to Make at Once", Factory, The Magazine of Management 10(2), 1913; R. H. Wilson, "A Scientific Routine for Stock Control", Harvard Business Review 13, 1934.

Files

PathBytes
README.md1,960
impl/python.py1,931
impl/rust.rs2,635
impl/typescript.ts2,001
vectors.json3,714