Functional Weave
Code in Python

manufacturing.variance

Standard costing variances: material price and usage, labour rate and efficiency, favourable or adverse.

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

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

What it does

The four basic standard-costing variances for a period, flexed to the output actually produced:

material price = actual material cost - AQ x SP material usage = (AQ - SQ) x SP SQ = standard quantity per unit x actual output labour rate = actual labour cost - AH x SR labour efficiency = (AH - SH) x SR SH = standard hours per unit x actual output

For example

  • cost_variances(standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half-up) → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £36,000.00, actual cost £36,610.00 the textbook case: 2 kg at £3 and 3 h at £10 a unit, 1000 made with 2200 kg for £6160 and 2900 h for £30450
  • cost_variances(standard price £1.25, standard quantity …, actual quantity …, actual cost £42.00, standard rate £11.50, standard hours …, actual hours …, actual cost £300.00, 100, half-up) → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £329.17, actual cost £342.00 fractional standards: 1/3 kg at £1.25 and 1/4 h at £11.50 a unit, 100 made using 33.5 kg; each flexed cost is rounded once and the variances still add up
  • cost_variances(standard price £1.25, standard quantity …, actual quantity …, actual cost £42.00, standard rate £11.50, standard hours …, actual hours …, actual cost £300.00, 100, down) → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £329.16, actual cost £342.00 the same rounded down: 4187.5p and 4166.67p become 4187p and 4166p

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.

def cost_variances(material: MaterialActual, labour: LabourActual, actual_output: int, mode: RoundingMode) -> CostVariances
materialMaterialActualstandard and actual material for the period
labourLabourActualstandard and actual labour for the period
actual_outputintunits actually produced, which the standard is flexed to
modeRoundingModehow each flexed cost becomes whole minor units
returnsCostVariances

The types it declares, generated into your project

@dataclass(frozen=True)
class MaterialActual:
    """Material standard per unit of output, and what was actually used and paid."""

    #: per unit of material (per kg, per metre)
    standard_price: Money
    #: material per unit of output
    standard_quantity: Rational
    #: material actually used
    actual_quantity: Rational
    #: what the material used actually cost
    actual_cost: Money

@dataclass(frozen=True)
class LabourActual:
    """Labour standard per unit of output, and what was actually worked and paid."""

    #: per hour
    standard_rate: Money
    #: hours per unit of output
    standard_hours: Rational
    #: hours actually worked
    actual_hours: Rational
    #: what those hours actually cost
    actual_cost: Money

@dataclass(frozen=True)
class Variance:
    """One variance: how big, and which way."""

    #: never negative
    amount: Money
    effect: VarianceEffect

VarianceEffect = Literal["favourable", "adverse", "none"]

@dataclass(frozen=True)
class CostVariances:
    """The four variances, their total, and the costs they reconcile."""

    #: actual cost - actual quantity x standard price
    material_price: Variance
    #: (actual quantity - standard quantity for actual output) x standard price
    material_usage: Variance
    #: actual cost - actual hours x standard rate
    labour_rate: Variance
    #: (actual hours - standard hours for actual output) x standard rate
    labour_efficiency: Variance
    #: actualCost - standardCost; always the sum of the four
    total: Variance
    #: standard material and labour for the actual output
    standard_cost: Money
    #: actual material and labour cost
    actual_cost: Money

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

from fune.manufacturing.variance import cost_variances  # manufacturing.variance@^1
impl/python.py · 62 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.

from .manufacturing_variance_types import CostVariances, LabourActual, MaterialActual, Variance
from .math_rational import Rational, multiply_rational, rational, rational_to_integer  ← from math.rational ^1.0.0 · built alongside by fune
from .math_round_div import RoundingMode  ← from math.round-div ^1.0.0 · built alongside by fune
from .money_amount import Money, assert_same_currency, money  ← from money.amount ^1.0.0 · built alongside by fune

MAX_SAFE = 2**53 - 1


def _not_negative(name: str, value: Rational) -> Rational:
    r = rational(value.numerator, value.denominator)
    if r.numerator < 0:
        raise ValueError("%s must not be negative" % (name,))
    return r


def _cost_of(name: str, value: Money, unit: Money) -> int:
    assert_same_currency(unit, value)
    if value.minor < 0:
        raise ValueError("%s must not be negative, received %d" % (name, value.minor))
    return value.minor


def _variance(signed: int, currency: str) -> Variance:
    effect = "adverse" if signed > 0 else "favourable" if signed < 0 else "none"
    return Variance(amount=money(abs(signed), currency), effect=effect)


def cost_variances(material: MaterialActual, labour: LabourActual, actual_output: int, mode: RoundingMode) -> CostVariances:
    """Material price and usage, labour rate and efficiency variances, flexed
    to actual output. The four flexed costs are each rounded once and every
    variance is a difference of them, so the variances always sum exactly.
    """
    if isinstance(actual_output, bool) or not isinstance(actual_output, int) or actual_output < 0 or actual_output > MAX_SAFE:
        raise ValueError("actualOutput must be a whole number, not negative, received %r" % (actual_output,))
    unit = material.standard_price
    currency = unit.currency
    sp = _cost_of("material standardPrice", material.standard_price, unit)
    mat_actual = _cost_of("material actualCost", material.actual_cost, unit)
    sr = _cost_of("labour standardRate", labour.standard_rate, unit)
    lab_actual = _cost_of("labour actualCost", labour.actual_cost, unit)
    sq = _not_negative("material standardQuantity", material.standard_quantity)
    aq = _not_negative("material actualQuantity", material.actual_quantity)
    sh = _not_negative("labour standardHours", labour.standard_hours)
    ah = _not_negative("labour actualHours", labour.actual_hours)
    output = rational(actual_output, 1)

    actual_at_standard = rational_to_integer(multiply_rational(aq, rational(sp, 1)), mode)
    standard_material = rational_to_integer(multiply_rational(multiply_rational(sq, output), rational(sp, 1)), mode)
    hours_at_standard = rational_to_integer(multiply_rational(ah, rational(sr, 1)), mode)
    standard_labour = rational_to_integer(multiply_rational(multiply_rational(sh, output), rational(sr, 1)), mode)

    standard_cost = standard_material + standard_labour
    actual_cost = mat_actual + lab_actual
    return CostVariances(
        material_price=_variance(mat_actual - actual_at_standard, currency),
        material_usage=_variance(actual_at_standard - standard_material, currency),
        labour_rate=_variance(lab_actual - hours_at_standard, currency),
        labour_efficiency=_variance(hours_at_standard - standard_labour, currency),
        total=_variance(actual_cost - standard_cost, currency),
        standard_cost=money(standard_cost, currency),
        actual_cost=money(actual_cost, currency),
    )

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 3 dependencies, pins them in fune.lock, downloads only the Python 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 manufacturing.variance
Download for Python manufacturing.variance-1.0.0-python.fune · 24,559 bytes sha256 260377595097891f590392f19669ac164721754d7e8c2b2ab267dd8c24209ab8

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./manufacturing.variance-1.0.0-python.fune, or fetch it from a terminal with fune pull manufacturing.variance@1.0.0:python.

The whole function, every language, is one file too: manufacturing.variance-1.0.0.fune, 33,167 bytes, sha256 891beb9988a990d3a686a6a4e0993bc51814dd5d685d087f0e5ebdcb2c68455c. 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 manufacturing.variance

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

# fune: after manufacturing.variance

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.rational in manufacturing.variance
# fune: replace math.round-div in manufacturing.variance
# fune: replace money.amount in manufacturing.variance

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 manufacturing.variance --steps.

# fune: step manufacturing.variance 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
the textbook case: 2 kg at £3 and 3 h at £10 a unit, 1000 made with 2200 kg for £6160 and 2900 h for £30450 standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £36,000.00, actual cost £36,610.00
fractional standards: 1/3 kg at £1.25 and 1/4 h at £11.50 a unit, 100 made using 33.5 kg; each flexed cost is rounded once and the variances still add up standard price £1.25, standard quantity …, actual quantity …, actual cost £42.00, standard rate £11.50, standard hours …, actual hours …, actual cost £300.00, 100, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £329.17, actual cost £342.00
the same rounded down: 4187.5p and 4166.67p become 4187p and 4166p standard price £1.25, standard quantity …, actual quantity …, actual cost £42.00, standard rate £11.50, standard hours …, actual hours …, actual cost £300.00, 100, down → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £329.16, actual cost £342.00
exactly on standard: every variance is none standard price £5.00, standard quantity …, actual quantity …, actual cost £50.00, standard rate £20.00, standard hours …, actual hours …, actual cost £100.00, 10, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £150.00, actual cost £150.00
nothing made: everything used is usage and efficiency variance standard price £5.00, standard quantity …, actual quantity …, actual cost £20.00, standard rate £20.00, standard hours …, actual hours …, actual cost £40.00, 0, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £0.00, actual cost £60.00
dear material used sparingly and cheap labour working fast: favourable overall standard price £3.00, standard quantity …, actual quantity …, actual cost £6,045.00, standard rate £10.00, standard hours …, actual hours …, actual cost £27,440.00, 1,000, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost £36,000.00, actual cost £33,485.00
hours in minutes as a fraction: 90 minutes is 3/2 hours standard price €10.00, standard quantity …, actual quantity …, actual cost €10.00, standard rate €24.00, standard hours …, actual hours …, actual cost €36.00, 1, half-up → material price …, material usage …, labour rate …, labour efficiency …, total …, standard cost €34.00, actual cost €46.00
costs in two currencies are an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate €10.00, standard hours …, actual hours …, actual cost €30,450.00, 1,000, half-up → error: currency mismatch
negative output is an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, -1, half-up → error: actualOutput must be a whole number, not negative
fractional output is an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 2.5, half-up → error: actualOutput must be a whole number, not negative
Show the other 5 tests
CaseArgumentsExpected
a negative actual quantity is an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half-up → error: material actualQuantity must not be negative
negative standard hours are an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half-up → error: labour standardHours must not be negative
a negative standard price is an error standard price -£3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half… → error: material standardPrice must not be negative
a negative actual labour cost is an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost -£0.01, 1,000, half-up → error: labour actualCost must not be negative
a zero denominator is an error standard price £3.00, standard quantity …, actual quantity …, actual cost £6,160.00, standard rate £10.00, standard hours …, actual hours …, actual cost £30,450.00, 1,000, half-up → error: denominator must not be zero

More from the author

A variance is **adverse** when actual cost is higher than standard and **favourable** when it is lower; `amount` is always the size and `effect` the direction (`none` for exactly zero). The textbook case: a standard of 2 kg at £3/kg and 3 hours at £10/h, 1,000 units made using 2,200 kg for £6,160 and 2,900 hours for £30,450, gives material price £440 F, usage £600 A, labour rate £1,450 A, efficiency £1,000 F, and £610 A in total.

**The variances always add up.** Quantities and hours are exact fractions (`Rational`: 1/3 kg, 1/4 hour), so the flexed costs (AQ x SP, SQ x SP, AH x SR, SH x SR) are exact fractions of a minor unit. Each of those four is rounded once by `mode`, and every variance is a difference of whole amounts, so price plus usage is exactly actual material cost less standard material cost, and the four together are exactly `actualCost - standardCost`. Rounding each variance on its own would leave a penny or two unexplained.

**Price variance on usage.** The material price variance is computed on the quantity used, not the quantity purchased; where stock is held at standard cost and the price variance is taken on purchase, pass purchases in a separate call with the purchase quantity as `actualQuantity`.

**Rules.** All money must be in the currency of `material.standardPrice`. Prices, rates, quantities, hours and costs may not be negative; output is a whole number, not negative. With zero output, all material and labour used is usage and efficiency variance. Overhead variances are not included.

Source: CIMA Official Terminology (2005), "variance" entries for direct material price, direct material usage, direct labour rate and direct labour efficiency variances; the same definitions appear in ACCA Performance Management (PM) study material on standard costing.

Files

PathBytes
README.md2,242
impl/python.py3,284
impl/rust.rs5,042
impl/typescript.ts3,265
vectors.json12,134