Functional Weave
Code in TypeScript

agri.yield-per-hectare

Crop yield per hectare and per acre from harvested quantity and field area in any area unit, exactly, as decimal text.

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

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

What it does

Crop yield per hectare and per acre from the quantity harvested and the area it came from, with the area given in any unit `units.convert` knows (ha, acre, m2, ft2, ...). `yieldPerHectare("85", "10", "ha", 2)` is 8.5 per hectare, 3.44 per acre, 10 ha, 24.71 acres.

The quantity is unitless: the answer is in whatever it was counted in (t, kg, bales, litres of milk from grass), so it works for tonnes of wheat, bales of hay or kg from a trial plot alike. Convert the quantity first with `units.convert` if you need a different mass unit.

For example

  • yieldPerHectare(85, 10, ha, 2) → per hectare 8.5, per acre 3.44, hectares 10, acres 24.71 85 t of wheat from 10 ha is 8.5 t/ha and 3.44 t/acre
  • yieldPerHectare(250, 100, acre, 2) → per hectare 6.18, per acre 2.5, hectares 40.47, acres 100 250 t from 100 acres is 2.5 t/acre and 6.18 t/ha
  • yieldPerHectare(12.345, 1.5, ha, 3) → per hectare 8.23, per acre 3.331, hectares 1.5, acres 3.707 a fractional harvest from a fractional area, to three places

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 yieldPerHectare(quantity: string, area: string, areaUnit: string, decimals: number): YieldPerArea
quantitystringharvested amount as plain decimal text ("85", "12.345"), in whatever unit you count: t, kg, bales, litres
areastringthe area it came from, plain decimal text, more than zero
areaUnitstringan area symbol of units.convert: ha, acre, m2, km2, ft2, yd2 ...
decimalsint0 to 12; every result is rounded half up to this many places
returnsYieldPerArea

The type it declares, generated into your project

/** The yield both ways, and the area both ways, as decimal text with trailing zeros trimmed. */
export interface YieldPerArea {
  /** quantity per hectare, in the quantity's unit */
  readonly perHectare: string;
  /** quantity per international acre (4046.8564224 m²) */
  readonly perAcre: string;
  readonly hectares: string;
  readonly acres: string;
}

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

import { yieldPerHectare } from "#fune/agri.yield-per-hectare@^1";
impl/typescript.ts · 91 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 { convertUnits } from "./units_convert.ts";  ← from units.convert ^1.0.0 · built alongside by fune
import { type YieldPerArea } from "./agri_yield_per_hectare_types.ts";

const DECIMAL = /^[0-9]+(\.[0-9]+)?$/;

interface Decimal {
  digits: bigint;
  scale: number;
}

function parse(name: string, text: string, limited: boolean): Decimal {
  if (typeof text !== "string" || !DECIMAL.test(text)) {
    throw new RangeError(`${name} must be plain non-negative decimal text like "12.5", received "${text}"`);
  }
  const [whole, rawFraction = ""] = text.split(".");
  const fraction = rawFraction.replace(/0+$/, "");
  const significant = (whole + fraction).replace(/^0+/, "");
  if (limited && (significant.length > 12 || fraction.length > 6)) {
    throw new RangeError(`${name} "${text}" has too many digits: at most 12 significant digits and 6 decimal places`);
  }
  return { digits: BigInt(significant === "" ? "0" : significant), scale: fraction.length };
}

// (product of top) / (product of bottom), each a decimal, as integers.
function ratio(top: readonly Decimal[], bottom: readonly Decimal[]): [bigint, bigint] {
  let num = 1n;
  let den = 1n;
  let exponent = 0; // power of ten on the numerator
  for (const d of top) {
    num *= d.digits;
    exponent -= d.scale;
  }
  for (const d of bottom) {
    den *= d.digits;
    exponent += d.scale;
  }
  if (exponent > 0) num *= 10n ** BigInt(exponent);
  else den *= 10n ** BigInt(-exponent);
  return [num, den];
}

// Long division, one digit at a time, rounded half up, trailing zeros trimmed.
function toText([num, den]: [bigint, bigint], decimals: number): string {
  let whole = num / den;
  let remainder = num % den;
  let fraction = 0n;
  for (let i = 0; i < decimals; i++) {
    remainder *= 10n;
    fraction = fraction * 10n + remainder / den;
    remainder %= den;
  }
  if (remainder * 2n >= den) {
    fraction += 1n;
    if (fraction === 10n ** BigInt(decimals)) {
      fraction = 0n;
      whole += 1n;
    }
  }
  let text = whole.toString();
  if (decimals > 0) {
    const shown = fraction.toString().padStart(decimals, "0").replace(/0+$/, "");
    if (shown !== "") text += "." + shown;
  }
  return text;
}

/**
 * Yield per hectare and per acre, exactly.
 *
 * The area unit's size comes from units.convert as exact decimal text
 * (1 acre = 4046.8564224 m²), so every answer is one exact fraction rounded
 * once. In floats, 1.005 t from 1 ha is 1.00 t/ha to two places; here it is
 * 1.01.
 */
export function yieldPerHectare(quantity: string, area: string, areaUnit: string, decimals: number): YieldPerArea {
  const q = parse("quantity", quantity, true);
  const a = parse("area", area, true);
  if (!Number.isInteger(decimals) || decimals < 0 || decimals > 12) {
    throw new RangeError(`decimals must be a whole number from 0 to 12, received ${decimals}`);
  }
  const unit = parse("unit size", convertUnits("1", areaUnit, "m2", 12), false);
  if (a.digits === 0n) throw new RangeError("area must be greater than zero");
  const acre = parse("acre", convertUnits("1", "acre", "m2", 12), false);
  const hectare: Decimal = { digits: 10000n, scale: 0 };
  return {
    perHectare: toText(ratio([q, hectare], [a, unit]), decimals),
    perAcre: toText(ratio([q, acre], [a, unit]), decimals),
    hectares: toText(ratio([a, unit], [hectare]), decimals),
    acres: toText(ratio([a, unit], [acre]), decimals),
  };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 1 dependency, 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 agri.yield-per-hectare
Download for TypeScript agri.yield-per-hectare-1.0.1-typescript.fune · 12,860 bytes sha256 97ccd029557845480671604db094b1fb13538e069f43206a49313c1e44a1ff21

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./agri.yield-per-hectare-1.0.1-typescript.fune, or fetch it from a terminal with fune pull agri.yield-per-hectare@1.0.1:typescript.

The whole function, every language, is one file too: agri.yield-per-hectare-1.0.1.fune, 21,893 bytes, sha256 5f62d53b3f77ada6b3b1cbaa826f843dc3b8732e82a6bc4b91f08c2ecc229d9a. 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 agri.yield-per-hectare

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

// fune: after agri.yield-per-hectare

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 units.convert in agri.yield-per-hectare

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 agri.yield-per-hectare --steps.

// fune: step agri.yield-per-hectare 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
85 t of wheat from 10 ha is 8.5 t/ha and 3.44 t/acre 85, 10, ha, 2 → per hectare 8.5, per acre 3.44, hectares 10, acres 24.71
250 t from 100 acres is 2.5 t/acre and 6.18 t/ha 250, 100, acre, 2 → per hectare 6.18, per acre 2.5, hectares 40.47, acres 100
a fractional harvest from a fractional area, to three places 12.345, 1.5, ha, 3 → per hectare 8.23, per acre 3.331, hectares 1.5, acres 3.707
a trial plot in square metres 500, 2500, m2, 2 → per hectare 2000, per acre 809.37, hectares 0.25, acres 0.62
1.005 t/ha rounds half up to 1.01, where a float says 1.00 1.005, 1, ha, 2 → per hectare 1.01, per acre 0.41, hectares 1, acres 2.47
nothing harvested is zero per hectare 0, 3.2, ha, 2 → per hectare 0, per acre 0, hectares 3.2, acres 7.91
zero decimals rounds to whole numbers, half up 7, 3, ha, 0 → per hectare 2, per acre 1, hectares 3, acres 7
a garden plot in square feet: 9.7 kg from 30.25 ft² is exactly 13968 kg/acre 9.7, 30.25, ft2, 4 → per hectare 34515.6797, per acre 13968, hectares 0.0003, acres 0.0007
trailing zeros are trimmed and leading zeros accepted 085.000, 010, ha, 3 → per hectare 8.5, per acre 3.44, hectares 10, acres 24.711
zero area is an error 85, 0, ha, 2 → error: area must be greater than zero
Show the other 9 tests
CaseArgumentsExpected
a unit that is not an area is an error 85, 10, kg, 2 → error: cannot convert kg (mass) to m2 (area)
an unknown unit is an error 85, 10, hectare, 2 → error: unknown unit "hectare"
a negative quantity is an error -5, 10, ha, 2 → error: quantity must be plain non-negative decimal text
exponent notation is an error 1e3, 10, ha, 2 → error: quantity must be plain non-negative decimal text
more than 12 significant digits is an error 1234567890123, 10, ha, 2 → error: has too many digits
more than 6 decimal places is an error 85, 10.1234567, ha, 2 → error: has too many digits
decimals above 12 is an error 85, 10, ha, 13 → error: decimals must be a whole number from 0 to 12
a trailing newline after quantity is not plain decimal text 12.5 , 10, ha, 2 → error: quantity must be plain non-negative decimal text
a trailing newline after area is not plain decimal text 5, 10 , ha, 2 → error: area must be plain non-negative decimal text

More from the author

## Exactness

Quantity and area arrive as decimal text, and the size of the area unit comes from `units.convert` as exact decimal text (`convertUnits("1", "acre", "m2", 12)` is `"4046.8564224"`, the international acre). Each result is therefore one exact fraction, rounded once, half up, to `decimals` places, with trailing zeros trimmed: `"8.5"`, not `"8.50"`. In floating point 1.005 t from 1 ha shows as 1.00 t/ha to two places; this gives 1.01.

The acre is the international acre (43,560 international square feet, 4046.8564224 m²), as in `units.convert`; the US survey acre was retired at the end of 2022.

## Limits

Quantity and area are plain non-negative decimal text: digits with an optional fractional part, no sign, separators or exponent, at most 12 significant digits and 6 decimal places. The area must be more than zero; a quantity of zero is a yield of zero. `decimals` is 0 to 12. These limits keep the Rust port exact in `i128`.

## Sources

The unit sizes are `units.convert`'s: NIST SP 811 (2008), Appendix B, and NIST, "U.S. Survey Foot: Revised Unit Conversion Factors" (the acre as 4046.8564224 m²); 1 ha = 10,000 m² by definition.

1.0.1 fixes Python accepting a trailing newline in quantity and area; adds tests.

Files

PathBytes
README.md1,809
impl/python.py3,333
impl/rust.rs5,308
impl/typescript.ts3,357
vectors.json4,272