finance.depreciation-straight-line
Straight-line depreciation schedule whose charges sum exactly to cost less residual value.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 15 tests, run in TypeScript, Python and Rust.
What it does
The depreciable amount (cost less residual value) is split with money.allocate rather than divided by the number of years, so the charges add up to it exactly and the final closing value is the residual value to the penny. Dividing and rounding each year instead leaves 1000.00 over three years as 333.33 three times, a penny short, and an asset that never finishes writing down.
The odd minor units land in the earliest years, because that is money.allocate's largest-remainder rule and this capability does not invent a second, conflicting one. For 1000.00 over three years the charges are 333.34, 333.33, 333.33. An accounting policy that wants the difference in the final year should adjust the last charge itself, where the choice is visible.
For example
straight_line_depreciation(£1,200.00, £0.00, 3)→ ×3 1200.00 over three years divides evenly: 400.00 a year, down to nothingstraight_line_depreciation(£1,000.00, £0.00, 3)→ ×3 1000.00 over three years does not divide evenly and the odd penny is kept, not droppedstraight_line_depreciation(£1,000.00, £100.00, 3)→ ×3 a residual value is written down to, not through
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 straight_line_depreciation(cost: Money, residual_value: Money, years: int) -> List[DepreciationPeriod]
| cost | Money | capitalised cost of the asset |
| residual_value | Money | expected value at the end of its life; 0 if written down to nothing |
| years | int | useful life in whole years, at least 1 |
| returns | DepreciationPeriod[] |
The type it declares, generated into your project
@dataclass(frozen=True)
class DepreciationPeriod:
"""One year of the schedule."""
#: 1-based: year 1 is the first full year of the asset's life
year: int
opening_value: Money
charge: Money
closing_value: Money
Your code names it in one line, in the file that uses it
from fune.finance.depreciation_straight_line import straight_line_depreciation # finance.depreciation-straight-line@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import List
from .finance_depreciation_straight_line_types import DepreciationPeriod
from .money_add import subtract_money ← from money.add ^1.0.0 · built alongside by fune
from .money_allocate import allocate ← from money.allocate ^1.0.0 · built alongside by fune
from .money_amount import Money ← from money.amount ^1.0.0 · built alongside by fune
def straight_line_depreciation(cost: Money, residual_value: Money, years: int) -> List[DepreciationPeriod]:
"""A straight-line depreciation schedule, year by year.
The depreciable amount is split with ``money.allocate`` instead of being
divided by the number of years. Dividing leaves 1000.00 over three years as
333.33 three times - a penny short - and an asset that never quite finishes
writing down to its residual value. ``allocate`` hands out every minor unit,
so the last closing value equals the residual value exactly, by construction
rather than by a fudge at the end.
The odd units go to the earliest years, following ``allocate``'s largest-
remainder rule rather than a second rule invented here.
"""
if isinstance(years, bool) or not isinstance(years, int) or years < 1:
raise ValueError("years must be at least 1, received %r" % (years,))
if cost.minor < 0:
raise ValueError("cost must not be negative")
if residual_value.minor < 0:
raise ValueError("residual value must not be negative")
# subtract_money also rejects a cost and a residual value in different
# currencies, which would otherwise produce a plausible-looking schedule.
depreciable = subtract_money(cost, residual_value)
if depreciable.minor < 0:
raise ValueError("residual value must not exceed cost")
charges = allocate(depreciable, [1] * years)
schedule: List[DepreciationPeriod] = []
opening_value = cost
for index in range(years):
charge = charges[index]
closing_value = subtract_money(opening_value, charge)
schedule.append(
DepreciationPeriod(
year=index + 1,
opening_value=opening_value,
charge=charge,
closing_value=closing_value,
)
)
opening_value = closing_value
return schedule
def written_down_value(cost: Money, residual_value: Money, years: int, elapsed_years: int) -> Money:
"""The written-down value after ``elapsed_years`` complete years."""
schedule = straight_line_depreciation(cost, residual_value, years)
if isinstance(elapsed_years, bool) or not isinstance(elapsed_years, int) or not 0 <= elapsed_years <= years:
raise ValueError("elapsed_years must be between 0 and %d, received %r" % (years, elapsed_years))
return cost if elapsed_years == 0 else schedule[elapsed_years - 1].closing_valueInstall
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 finance.depreciation-straight-line
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./finance.depreciation-straight-line-1.0.0-python.fune, or fetch it from a terminal with fune pull finance.depreciation-straight-line@1.0.0:python.
The whole function, every language, is one file too: finance.depreciation-straight-line-1.0.0.fune, 21,560 bytes, sha256 0695a6a3ea4324d63b1e7b07067487ebe7fb608afa73b34475838341835c6495. 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 finance.depreciation-straight-line
after — your function gets the result and the arguments, and returns the final result.
# fune: after finance.depreciation-straight-line
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 money.add in finance.depreciation-straight-line
# fune: replace money.allocate in finance.depreciation-straight-line
# fune: replace money.amount in finance.depreciation-straight-line
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 finance.depreciation-straight-line --steps.
# fune: step finance.depreciation-straight-line 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 | |
|---|---|---|---|
| 1200.00 over three years divides evenly: 400.00 a year, down to nothing | £1,200.00, £0.00, 3 | → | ×3 |
| 1000.00 over three years does not divide evenly and the odd penny is kept, not dropped | £1,000.00, £0.00, 3 | → | ×3 |
| a residual value is written down to, not through | £1,000.00, £100.00, 3 | → | ×3 |
| 100.00 over seven years: four years carry the odd penny and the asset still lands on zero | £100.00, £0.00, 7 | → | ×7 |
| a one year life charges everything in year one | £500.00, £100.00, 1 | → | ×1 |
| an asset already at its residual value has nothing to depreciate | £750.00, £750.00, 4 | → | ×4 |
| a worthless asset gives a schedule of zeroes rather than an error | £0.00, £0.00, 2 | → | ×2 |
| an awkward residual value still closes exactly on it | £1,000.00, £333.33, 3 | → | ×3 |
| yen has no minor unit, so the odd unit is a whole yen | ¥1,000, ¥0, 3 | → | ×3 |
| a zero year life is an error, not an instant write-off | £1,000.00, £0.00, 0 | → | error: years must be at least 1 |
Show the other 5 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a fractional life is an error | £1,000.00, £0.00, 2.5 | → | error: years must be at least 1 |
| a residual value above cost is an error, not negative depreciation | £1,000.00, £1,500.00, 3 | → | error: residual value must not exceed cost |
| a negative residual value is an error: disposal costs are not depreciation | £1,000.00, -£1.00, 3 | → | error: residual value must not be negative |
| a negative cost is an error: a credit is not an asset | -£1,000.00, £0.00, 3 | → | error: cost must not be negative |
| costing in one currency and scrapping in another is an error | £1,000.00, €100.00, 3 | → | error: currency mismatch |
More from the author
The schedule is returned whole - opening value, charge and closing value for every year - because the closing value is the number that has to agree with the balance sheet, and recomputing it from the charges in caller code is where it stops agreeing.
A full year is charged in each of the `years` years: this is the simple convention, not a mid-month or mid-year one. Pro-rating the first and last period is finance.proration's job.
Files
| Path | Bytes |
|---|---|
| README.md | 1,222 |
| impl/python.py | 2,677 |
| impl/rust.rs | 3,909 |
| impl/typescript.ts | 2,565 |
| vectors.json | 7,544 |