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
yield_per_hectare(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/acreyield_per_hectare(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/hayield_per_hectare(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.
def yield_per_hectare(quantity: str, area: str, area_unit: str, decimals: int) -> YieldPerArea
| quantity | string | harvested amount as plain decimal text ("85", "12.345"), in whatever unit you count: t, kg, bales, litres |
| area | string | the area it came from, plain decimal text, more than zero |
| area_unit | string | an area symbol of units.convert: ha, acre, m2, km2, ft2, yd2 ... |
| decimals | int | 0 to 12; every result is rounded half up to this many places |
| returns | YieldPerArea |
The type it declares, generated into your project
@dataclass(frozen=True)
class YieldPerArea:
"""The yield both ways, and the area both ways, as decimal text with trailing zeros trimmed."""
#: quantity per hectare, in the quantity's unit
per_hectare: str
#: quantity per international acre (4046.8564224 m²)
per_acre: str
hectares: str
acres: str
Your code names it in one line, in the file that uses it
from fune.agri.yield_per_hectare import yield_per_hectare # agri.yield-per-hectare@^1
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 List, Sequence, Tuple
from .agri_yield_per_hectare_types import YieldPerArea
from .units_convert import convert_units ← from units.convert ^1.0.0 · built alongside by fune
DECIMAL = re.compile(r"[0-9]+(\.[0-9]+)?")
Decimal = Tuple[int, int] # digits, scale: digits / 10^scale
def _parse(name: str, text: str, limited: bool) -> Decimal:
if not isinstance(text, str) or not DECIMAL.fullmatch(text):
raise ValueError('%s must be plain non-negative decimal text like "12.5", received "%s"' % (name, text))
whole, _, raw_fraction = text.partition(".")
fraction = raw_fraction.rstrip("0")
significant = (whole + fraction).lstrip("0")
if limited and (len(significant) > 12 or len(fraction) > 6):
raise ValueError('%s "%s" has too many digits: at most 12 significant digits and 6 decimal places' % (name, text))
return (int(significant) if significant else 0, len(fraction))
def _ratio(top: Sequence[Decimal], bottom: Sequence[Decimal]) -> Tuple[int, int]:
"""(product of top) / (product of bottom), each a decimal, as integers."""
num, den, exponent = 1, 1, 0
for digits, scale in top:
num *= digits
exponent -= scale
for digits, scale in bottom:
den *= digits
exponent += scale
if exponent > 0:
num *= 10**exponent
else:
den *= 10 ** (-exponent)
return num, den
def _to_text(fraction_pair: Tuple[int, int], decimals: int) -> str:
"""Long division, one digit at a time, rounded half up, trailing zeros trimmed."""
num, den = fraction_pair
whole, remainder = divmod(num, den)
fraction = 0
for _ in range(decimals):
remainder *= 10
fraction = fraction * 10 + remainder // den
remainder %= den
if remainder * 2 >= den:
fraction += 1
if fraction == 10**decimals:
fraction = 0
whole += 1
text = str(whole)
if decimals > 0:
shown = str(fraction).rjust(decimals, "0").rstrip("0")
if shown:
text += "." + shown
return text
def yield_per_hectare(quantity: str, area: str, area_unit: str, decimals: int) -> YieldPerArea:
"""Yield per hectare and per acre, exactly.
The area unit's size comes from units.convert as exact decimal text
(1 acre = 4046.8564224 m2), 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.
"""
q = _parse("quantity", quantity, True)
a = _parse("area", area, True)
if isinstance(decimals, bool) or not isinstance(decimals, int) or decimals < 0 or decimals > 12:
raise ValueError("decimals must be a whole number from 0 to 12, received %r" % (decimals,))
unit = _parse("unit size", convert_units("1", area_unit, "m2", 12), False)
if a[0] == 0:
raise ValueError("area must be greater than zero")
acre = _parse("acre", convert_units("1", "acre", "m2", 12), False)
hectare: Decimal = (10000, 0)
parts: List[str] = [
_to_text(_ratio([q, hectare], [a, unit]), decimals),
_to_text(_ratio([q, acre], [a, unit]), decimals),
_to_text(_ratio([a, unit], [hectare]), decimals),
_to_text(_ratio([a, unit], [acre]), decimals),
]
return YieldPerArea(per_hectare=parts[0], per_acre=parts[1], hectares=parts[2], acres=parts[3])Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, 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 agri.yield-per-hectare
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./agri.yield-per-hectare-1.0.1-python.fune, or fetch it from a terminal with fune pull agri.yield-per-hectare@1.0.1:python.
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.
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Path | Bytes |
|---|---|
| README.md | 1,809 |
| impl/python.py | 3,333 |
| impl/rust.rs | 5,308 |
| impl/typescript.ts | 3,357 |
| vectors.json | 4,272 |