Functional Weave
Code in Python

fleet.fuel-cost

Fuel cost of a trip from distance, fuel economy (UK or US mpg, or L/100km) and price per litre, exactly.

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

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

What it does

The fuel a trip burns and what it costs, from the distance, the vehicle's fuel economy and the pump price per litre. `fuelCost("100", "mi", "50", "mpg-uk", 1459, "GBP", "half-up")` is 9.092 litres costing 13.27.

## Inputs

For example

  • fuel_cost(100, mi, 50, mpg-uk, 1,459, GBP, half-up) → litres 9.092, cost £13.27 100 miles at 50 mpg (UK) and 145.9p: 9.09218 litres, 1326.549p
  • fuel_cost(100, km, 5.5, l-per-100km, 1,459, GBP, half-up) → litres 5.500, cost £8.02 100 km at 5.5 L/100km and 145.9p: 5.5 litres, 802.45p rounds down under half-up
  • fuel_cost(100, km, 5.5, l-per-100km, 1,459, GBP, up) → litres 5.500, cost £8.03 the same trip rounded up

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 fuel_cost(distance: str, distance_unit: DistanceUnit, economy: str, economy_unit: EconomyUnit, price_per_litre_tenths: int, currency: str, mode: RoundingMode) -> FuelCost
distancestringplain decimal text, 0 or more: "250", "12.5"
distance_unitDistanceUnitmi or km
economystringplain decimal text, above 0: "48.7" mpg or "5.9" L/100km
economy_unitEconomyUnitmpg-uk (imperial gallon), mpg-us (US gallon) or l-per-100km
price_per_litre_tenthsintpump price in tenths of a minor unit, as pumps show it: 1459 = 145.9p
currencystringISO 4217 code of the price
modeRoundingModehow the exact cost is rounded to a minor unit
returnsFuelCost

The types it declares, generated into your project

DistanceUnit = Literal["mi", "km"]

EconomyUnit = Literal["mpg-uk", "mpg-us", "l-per-100km"]

@dataclass(frozen=True)
class FuelCost:
    """The fuel a trip burns and what it costs."""

    #: exact litres rounded half-up to 3 decimal places, for display: "9.092"
    litres: str
    #: from the exact litres, not the rounded ones
    cost: Money

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

from fune.fleet.fuel_cost import fuel_cost  # fleet.fuel-cost@^1
impl/python.py · 70 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 re
from typing import Tuple

from .fleet_fuel_cost_types import DistanceUnit, EconomyUnit, FuelCost
from .math_round_div import RoundingMode  ← from math.round-div ^1.0.0 · built alongside by fune
from .math_round_div_big import round_div_big  ← from math.round-div-big ^1.0.0 · built alongside by fune
from .money_amount import money  ← from money.amount ^1.0.0 · built alongside by fune
from .units_convert import convert_units  ← from units.convert ^1.0.0 · built alongside by fune

DECIMAL = re.compile(r"^[0-9]+(\.[0-9]+)?$")
MAX_SAFE = 9007199254740991


def _decimal(name: str, text: str) -> Tuple[int, int]:
    """A plain decimal as an exact fraction: (digits without the point, power of ten)."""
    if not isinstance(text, str) or not DECIMAL.fullmatch(text):
        raise ValueError('%s must be a plain decimal of 0 or more, received "%s"' % (name, text))
    point = text.find(".")
    places = 0 if point < 0 else len(text) - point - 1
    return int(text.replace(".", "")), 10 ** places


def fuel_cost(
    distance: str,
    distance_unit: DistanceUnit,
    economy: str,
    economy_unit: EconomyUnit,
    price_per_litre_tenths: int,
    currency: str,
    mode: RoundingMode,
) -> FuelCost:
    """The fuel a trip uses and what it costs.

    Everything stays an exact fraction until the one rounding step: litres come
    from the distance and economy, and the cost from those exact litres.
    Rounding the litres first, or using 4.546 for the imperial gallon, is a
    penny out often enough to break a reconciliation. The gallon and the mile
    come from units.convert, which defines them exactly.
    """
    if distance_unit not in ("mi", "km"):
        raise ValueError('unknown distance unit "%s"' % (distance_unit,))
    if economy_unit not in ("mpg-uk", "mpg-us", "l-per-100km"):
        raise ValueError('unknown economy unit "%s"' % (economy_unit,))
    d_num, d_den = _decimal("distance", distance)
    e_num, e_den = _decimal("economy", economy)
    if e_num == 0:
        raise ValueError("economy must be greater than zero")
    if isinstance(price_per_litre_tenths, bool) or not isinstance(price_per_litre_tenths, int):
        raise TypeError("pricePerLitreTenths must be an integer, received %r" % (price_per_litre_tenths,))
    if price_per_litre_tenths < 0:
        raise ValueError("pricePerLitreTenths must not be negative, received %d" % (price_per_litre_tenths,))
    money(0, currency)

    km_num, km_den = _decimal("mile", convert_units("1", "mi", "km", 12))
    if economy_unit == "l-per-100km":
        # litres = km x (L/100km) / 100
        l_num = d_num * e_num * (km_num if distance_unit == "mi" else 1)
        l_den = d_den * e_den * 100 * (km_den if distance_unit == "mi" else 1)
    else:
        # litres = miles / mpg x litres per gallon
        g_num, g_den = _decimal("gallon", convert_units("1", "gal_imp" if economy_unit == "mpg-uk" else "gal_us", "L", 12))
        l_num = d_num * e_den * g_num * (km_den if distance_unit == "km" else 1)
        l_den = d_den * e_num * g_den * (km_num if distance_unit == "km" else 1)

    cost = int(round_div_big(str(l_num * price_per_litre_tenths), str(l_den * 10), mode))
    if cost > MAX_SAFE:
        raise ValueError("the fuel cost exceeds 2^53 - 1 minor units")
    thousandths = int(round_div_big(str(l_num * 1000), str(l_den), "half-up"))
    litres = "%d.%03d" % (thousandths // 1000, thousandths % 1000)
    return FuelCost(litres=litres, cost=money(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 5 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 fleet.fuel-cost
Download for Python fleet.fuel-cost-1.0.0-python.fune · 13,427 bytes sha256 0edbc6275aba30343c1e0b029e4c018b7640a0e0ebb349c5c99d035da77cd1f7

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

The whole function, every language, is one file too: fleet.fuel-cost-1.0.0.fune, 22,193 bytes, sha256 5022019ff35116bcc4305837d21b577312e7a9eb682e807d301f46e7f00b5dc3. 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 fleet.fuel-cost

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

# fune: after fleet.fuel-cost

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.big-integer in fleet.fuel-cost
# fune: replace math.round-div in fleet.fuel-cost
# fune: replace math.round-div-big in fleet.fuel-cost
# fune: replace money.amount in fleet.fuel-cost
# fune: replace units.convert in fleet.fuel-cost

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 fleet.fuel-cost --steps.

# fune: step fleet.fuel-cost 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
100 miles at 50 mpg (UK) and 145.9p: 9.09218 litres, 1326.549p 100, mi, 50, mpg-uk, 1,459, GBP, half-up → litres 9.092, cost £13.27
100 km at 5.5 L/100km and 145.9p: 5.5 litres, 802.45p rounds down under half-up 100, km, 5.5, l-per-100km, 1,459, GBP, half-up → litres 5.500, cost £8.02
the same trip rounded up 100, km, 5.5, l-per-100km, 1,459, GBP, up → litres 5.500, cost £8.03
100 miles at 5.5 L/100km: 160.9344 km, 8.851392 litres 100, mi, 5.5, l-per-100km, 1,459, GBP, half-up → litres 8.851, cost £12.91
100 km at 50 mpg (UK): 454.609 / 80.4672 = 5.6496... litres 100, km, 50, mpg-uk, 1,459, GBP, half-up → litres 5.650, cost £8.24
100 miles at 30 mpg (US): the US gallon is 3.785411784 litres 100, mi, 30, mpg-us, 1,459, GBP, half-up → litres 12.618, cost £18.41
one mile at 1 mpg (UK) and 81.5p is 370.506335p: rounding to 4.546 litres first would give 370 1, mi, 1, mpg-uk, 815, GBP, half-up → litres 4.546, cost £3.71
an exact half penny: 1 litre at 100.5p, half-up 100, km, 1, l-per-100km, 1,005, GBP, half-up → litres 1.000, cost £1.01
an exact half penny, half-even keeps the even 100 100, km, 1, l-per-100km, 1,005, GBP, half-even → litres 1.000, cost £1.00
an exact half penny, down 100, km, 1, l-per-100km, 1,005, GBP, down → litres 1.000, cost £1.00
Show the other 14 tests
CaseArgumentsExpected
no distance costs nothing 0, mi, 45, mpg-uk, 1,459, GBP, half-up → litres 0.000, cost £0.00
free fuel costs nothing 250, mi, 45, mpg-uk, 0, GBP, half-up → litres 25.256, cost £0.00
euro cents work the same way: 12.5 km at 6.4 L/100km and 1.799 EUR a litre 12.5, km, 6.4, l-per-100km, 1,799, EUR, half-up → litres 0.800, cost €1.44
an economy of zero is an error 100, mi, 0, mpg-uk, 1,459, GBP, half-up → error: economy must be greater than zero
a negative distance is an error -5, mi, 50, mpg-uk, 1,459, GBP, half-up → error: distance must be a plain decimal of 0 or more
an exponent is not a plain decimal 1e3, km, 50, mpg-uk, 1,459, GBP, half-up → error: distance must be a plain decimal of 0 or more
a trailing point is not a plain decimal 50, km, 6., l-per-100km, 1,459, GBP, half-up → error: economy must be a plain decimal of 0 or more
an unknown distance unit is an error 100, yd, 50, mpg-uk, 1,459, GBP, half-up → error: unknown distance unit
mpg without saying which gallon is an error 100, mi, 50, mpg, 1,459, GBP, half-up → error: unknown economy unit
a negative price is an error 100, mi, 50, mpg-uk, -1, GBP, half-up → error: pricePerLitreTenths must not be negative
a fractional price must be given in tenths 100, mi, 50, mpg-uk, 145.9, GBP, half-up → error: pricePerLitreTenths must be an integer
a lower-case currency is an error 100, mi, 50, mpg-uk, 1,459, gbp, half-up → error: is not an uppercase ISO 4217 currency code
an unknown rounding mode is an error 100, mi, 50, mpg-uk, 1,459, GBP, nearest → error: unknown rounding mode
a cost beyond 2^53 - 1 minor units is refused 999999999999999999, mi, 1, mpg-uk, 99,999, GBP, half-up → error: the fuel cost exceeds 2^53 - 1 minor units

More from the author

- `distance` and `economy` are plain decimal text ("250", "48.7"), so a distance read off an odometer or a WLTP figure goes in exactly. - `economyUnit` names the gallon. UK figures are miles per **imperial** gallon (4.54609 L); US figures are miles per US gallon (3.785411784 L), about 20% smaller. Plain `mpg` is refused because the two are easy to mix up. Or give litres per 100 km. - `pricePerLitreTenths` is the price as a pump shows it, to a tenth of a penny: 145.9p is `1459`. A price in whole pence is just `1450` and so on.

## Exactness

The litres are an exact fraction of the inputs. The mile (1.609344 km) and both gallons come from `units.convert`, which defines them exactly. The cost is litres x price, rounded once to a minor unit with the mode you choose. The litres in the result are rounded half-up to 3 places for display only: the cost never uses them. That matters: one mile at 1 mpg (UK) and 81.5p is 370.506335p, which is 371p; rounding the litres to 4.546 first (or using the common approximation 4.546 L per gallon) gives 370p.

The product is held in big integers (`math.round-div-big`), so long distances with many decimal places cannot overflow; a cost beyond 2^53 - 1 minor units is refused because JavaScript could not represent it.

## Edges

Zero distance and a zero price cost nothing. A zero economy, a negative or malformed number, an unknown unit, a fractional or negative price, a bad currency code and an unknown mode are errors.

Files

PathBytes
README.md1,722
impl/python.py3,263
impl/rust.rs4,851
impl/typescript.ts3,523
vectors.json4,570