lending.daily-interest Unreviewed
Interest accrued day by day on a changing balance under a day-count convention, summed exactly and rounded once.
1.0.1 · published 2026-10-03 by charlie · Anterra
Pinned by 21 tests, run in TypeScript, Python and Rust.
Unreviewed. This capability’s implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified consumer-credit compliance specialist has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.
Not professional advice. This capability calculates lending figures from published rules. It is a software component for developers, not financial advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed in its README, and have a consumer-credit compliance specialist review how you use it, before anyone relies on the output. Provided “as is” under its licence, without warranty.
What it does
Interest on an account whose balance changes during the period: a savings account with deposits, a loan with a payment mid-month, an overdraft that goes in and out. Each balance accrues balance × rate × (the year fraction for the days it was held), under the day-count convention the contract names, and the pieces are added up.
## How it is computed
For example
dailyInterest(balances ×1, 2026-01-01, 2026-01-31, 3.65%, act-365f, half-up)→ interest £30.00, exact …, days 30 £10,000 at 3.65% for 30 days on ACT/365 is exactly £30dailyInterest(balances ×2, 2026-03-01, 2026-04-01, 4.5%, act-365f, half-up)→ interest £13.68, exact …, days 31 a deposit mid-month: the new balance earns from its datedailyInterest(balances ×2, 2026-01-01, 2026-02-01, 5%, act-365f, half-up)→ interest £6.51, exact …, days 31 a balance before the period starts is carried in
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 dailyInterest(balances: readonly DatedBalance[], fromIso: string, toIso: string, annualRateBasisPoints: number, convention: DayCountConvention, mode: RoundingMode): DailyInterest
| balances | DatedBalance[] | each balance and the date it starts, in strictly ascending date order; the first on or before fromIso |
| fromIso | date | first day of accrual, included |
| toIso | date | end of accrual, excluded: interest runs to the end of the day before |
| annualRateBasisPoints | int | nominal annual rate, -100000 to 100000; 450 = 4.5% |
| convention | DayCountConvention | the day count basis, as dates.day-count-fraction |
| mode | RoundingMode | how the exact total becomes whole minor units |
| returns | DailyInterest |
The types it declares, generated into your project
/** A balance and the day it takes effect; it holds until the next one. */
export interface DatedBalance {
readonly date: string;
/** negative for a debt, e.g. an overdrawn account */
readonly balance: Money;
}
/** The posted interest, the exact amount behind it, and the days counted. */
export interface DailyInterest {
/** the exact total rounded once */
readonly interest: Money;
/** the exact total in minor units, before rounding */
readonly exact: Rational;
/** calendar days from fromIso to toIso */
readonly days: number;
}
Your code names it in one line, in the file that uses it
import { dailyInterest } from "#fune/lending.daily-interest@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { type RoundingMode } from "./math_round_div.ts"; ← from math.round-div ^1.0.0 · built alongside by fune
import { type Money, money } from "./money_amount.ts"; ← from money.amount ^1.0.0 · built alongside by fune
import { type Rational, addRational, multiplyRational, rational, rationalToInteger } from "./math_rational.ts"; ← from math.rational ^1.0.0 · built alongside by fune
import { type DayCountConvention, dayCountFraction } from "./dates_day_count_fraction.ts"; ← from dates.day-count-fraction ^1.0.0 · built alongside by fune
import { daysBetween } from "./dates_days_between.ts"; ← from dates.days-between ^1.0.0 · built alongside by fune
import { type DailyInterest, type DatedBalance } from "./lending_daily_interest_types.ts";
/**
* Interest on a balance that changes during the period: each balance earns
* (or is charged) balance × rate × year fraction for the days it was held,
* the year fractions come from dates.day-count-fraction, and the pieces are
* added as exact fractions. Only the total is rounded, once, so splitting a
* month into more balance changes never moves the answer by a stray penny.
*/
export function dailyInterest(
balances: readonly DatedBalance[],
fromIso: string,
toIso: string,
annualRateBasisPoints: number,
convention: DayCountConvention,
mode: RoundingMode,
): DailyInterest {
if (!Number.isInteger(annualRateBasisPoints) || annualRateBasisPoints < -100000 || annualRateBasisPoints > 100000) {
throw new RangeError(`annualRateBasisPoints must be between -100000 and 100000, received ${annualRateBasisPoints}`);
}
if (balances.length === 0) {
throw new RangeError("balances must not be empty");
}
const days = daysBetween(fromIso, toIso);
if (days < 0) {
throw new RangeError(`fromIso ${fromIso} must not be after toIso ${toIso}`);
}
const currency = balances[0].balance.currency;
for (let i = 0; i < balances.length; i += 1) {
daysBetween(balances[i].date, balances[i].date);
if (balances[i].balance.currency !== currency) {
throw new RangeError(`currency mismatch: ${currency} and ${balances[i].balance.currency}`);
}
if (i > 0 && balances[i].date <= balances[i - 1].date) {
throw new RangeError("balances must be in strictly ascending date order");
}
}
if (balances[0].date > fromIso) {
throw new RangeError(`the first balance starts on ${balances[0].date}, after fromIso ${fromIso}`);
}
const rate = rational(annualRateBasisPoints, 10000);
let total: Rational = rational(0, 1);
for (let i = 0; i < balances.length; i += 1) {
const start = balances[i].date > fromIso ? balances[i].date : fromIso;
const next = i + 1 < balances.length ? balances[i + 1].date : toIso;
const end = next < toIso ? next : toIso;
if (start >= end) continue;
const fraction = dayCountFraction(start, end, convention);
const amount = multiplyRational(rational(balances[i].balance.minor, 1), rate);
total = addRational(total, multiplyRational(amount, rational(fraction.numerator, fraction.denominator)));
}
return { interest: money(rationalToInteger(total, mode), currency), exact: total, days };
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 5 dependencies, 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 lending.daily-interest
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./lending.daily-interest-1.0.1-typescript.fune, or fetch it from a terminal with fune pull lending.daily-interest@1.0.1:typescript.
The whole function, every language, is one file too: lending.daily-interest-1.0.1.fune, 26,958 bytes, sha256 2c05773e8a51ceee16cb0d3f7b369aaebb047265e4c8eb990b39c4186dae89ed. 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 lending.daily-interest
after — your function gets the result and the arguments, and returns the final result.
// fune: after lending.daily-interest
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 dates.day-count-fraction in lending.daily-interest
// fune: replace dates.days-between in lending.daily-interest
// fune: replace math.rational in lending.daily-interest
// fune: replace math.round-div in lending.daily-interest
// fune: replace money.amount in lending.daily-interest
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 lending.daily-interest --steps.
// fune: step lending.daily-interest 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 | |
|---|---|---|---|
| £10,000 at 3.65% for 30 days on ACT/365 is exactly £30 | balances ×1, 2026-01-01, 2026-01-31, 3.65%, act-365f, half-up | → | interest £30.00, exact …, days 30 |
| a deposit mid-month: the new balance earns from its date | balances ×2, 2026-03-01, 2026-04-01, 4.5%, act-365f, half-up | → | interest £13.68, exact …, days 31 |
| a balance before the period starts is carried in | balances ×2, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | interest £6.51, exact …, days 31 |
| changes after the period end are ignored | balances ×2, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | interest £4.25, exact …, days 31 |
| ACT/360 charges more than ACT/365 for the same days | balances ×1, 2026-01-01, 2026-01-31, 3.65%, act-360, half-up | → | interest £30.42, exact …, days 30 |
| 30/360: February counts as 30 days | balances ×1, 2026-02-01, 2026-03-01, 6%, 30-360, half-up | → | interest £60.00, exact …, days 28 |
| ACT/ACT ISDA across a leap-year boundary | balances ×1, 2027-12-01, 2028-02-01, 4%, act-act-isda, half-up | → | interest £339.26, exact …, days 62 |
| an overdrawn balance accrues negative interest | balances ×1, 2026-05-01, 2026-06-01, 19.9%, act-365f, half-up | → | interest -£8.45, exact …, days 31 |
| the same debt with "up" rounds away from zero | balances ×1, 2026-05-01, 2026-06-01, 19.9%, act-365f, up | → | interest -£8.46, exact …, days 31 |
| rounding once: many small changes, one rounding | balances ×4, 2026-01-01, 2026-01-05, 25%, act-365f, half-up | → | interest £0.01, exact …, days 4 |
Show the other 11 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a zero-length period accrues nothing | balances ×1, 2026-01-10, 2026-01-10, 5%, act-365f, half-up | → | interest £0.00, exact …, days 0 |
| a negative rate charges a positive balance | balances ×1, 2026-01-01, 2026-04-01, -0.5%, act-365f, half-even | → | interest -£123.29, exact …, days 90 |
| a whole leap year on ACT/365F is 366/365 of a year's interest | balances ×1, 2028-01-01, 2029-01-01, 10%, act-365f, half-up | → | interest £100.27, exact …, days 366 |
| an empty balance list is refused | , 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | error: balances must not be empty |
| a first balance after the start is refused | balances ×1, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | error: after fromIso |
| balances out of order are refused | balances ×2, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | error: strictly ascending date order |
| an end before the start is refused | balances ×1, 2026-02-01, 2026-01-01, 5%, act-365f, half-up | → | error: must not be after toIso |
| mixed currencies are refused | balances ×2, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | error: currency mismatch |
| an impossible date is refused | balances ×1, 2026-01-01, 2026-02-01, 5%, act-365f, half-up | → | error: is not a real calendar date |
| an unknown convention is refused | balances ×1, 2026-01-01, 2026-02-01, 5%, act-365, half-up | → | error: unknown day count convention |
| a rate out of range is refused | balances ×1, 2026-01-01, 2026-02-01, 1000.01%, act-365f, half-up | → | error: annualRateBasisPoints must be between -100000 and 100000 |
More from the author
- `balances` is the balance history: each entry holds from its date until the next entry's date. The first entry must start on or before `fromIso` (the opening balance); entries after `toIso` are ignored. - The period is `fromIso` included to `toIso` excluded, the usual accrual convention: 1 January to 31 January is 30 days of interest. - Each piece's year fraction comes from dates.day-count-fraction (ACT/365F, ACT/360, 30/360, 30E/360, ACT/ACT ISDA), as an exact fraction. - The pieces are summed exactly with math.rational, and the total is rounded once, with the mode you pass. `exact` is that unrounded total in minor units, for audit and for carrying fractions of a penny into the next period if your ledger does that.
Rounding once is the point. Rounding each day (or each balance change) and adding the rounded amounts drifts: three days at 0.23p each would post 0p where the true total is 0.68p, and more balance changes would mean more drift. Some ledgers do post whole pence daily; if yours does, call this once per day instead.
## Signs
A negative balance (overdrawn, or a loan held as a debt) accrues negative interest, and a negative rate charges a positive balance. The rounding modes are those of math.round-div, symmetric about zero: `down` is towards zero, `up` away from it.
## Splitting and 30/360
Under ACT/365F, ACT/360 and ACT/ACT the pieces add up to the whole period exactly. Under 30/360 each piece is measured on its own, as a contract that names 30/360 per balance period would, so a piece ending on the 31st can differ by a day from measuring the whole month at once.
## Limits
The exact running total must fit math.rational (numerator and denominator within 2^53 - 1); that holds for balances into the hundreds of millions of pounds at ordinary rates, and a larger one is an error ("rational overflow"), never a wrong answer. The rate is -100000 to 100000 basis points, and the rate is fixed for the period: for a rate change, call once per rate and add.
## Before you rely on this
**Not professional advice.** This capability calculates lending figures from published rules. It is a software component for developers, not financial advice. Rules change and every rate here has an effective date. Check that the dates cover your case. Verify results against the official sources listed above, and have a consumer-credit compliance specialist review how you use it, before anyone relies on the output. Provided "as is" under its licence, without warranty.
**Unreviewed.** This capability's implementations agree in every language and pass its published test vectors, which were worked out from the official sources cited. But no qualified consumer-credit compliance specialist has yet checked those vectors, or confirmed that the capability covers the cases it claims. Treat it as a draft. Do not use it for real people, money or decisions without your own expert review. Once a qualified reviewer signs off, this notice is replaced with their name, qualification and the date. Each new version needs fresh sign-off.
1.0.1 marks it unreviewed. The code and the tests are unchanged.
Files
| Path | Bytes |
|---|---|
| README.md | 3,520 |
| impl/python.py | 2,712 |
| impl/rust.rs | 3,769 |
| impl/typescript.ts | 2,874 |
| vectors.json | 8,927 |