Functional Weave
Code in Rust

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

  • daily_interest(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 £30
  • daily_interest(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 date
  • daily_interest(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.

pub fn daily_interest(balances: &[DatedBalance], from_iso: &str, to_iso: &str, annual_rate_basis_points: i64, convention: &str, mode: &str) -> DailyInterest
balancesDatedBalance[]each balance and the date it starts, in strictly ascending date order; the first on or before fromIso
from_isodatefirst day of accrual, included
to_isodateend of accrual, excluded: interest runs to the end of the day before
annual_rate_basis_pointsintnominal annual rate, -100000 to 100000; 450 = 4.5%
conventionDayCountConventionthe day count basis, as dates.day-count-fraction
modeRoundingModehow the exact total becomes whole minor units
returnsDailyInterest

The types it declares, generated into your project

/// A balance and the day it takes effect; it holds until the next one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DatedBalance {
    pub date: String,
    /// negative for a debt, e.g. an overdrawn account
    pub balance: Money,
}

/// The posted interest, the exact amount behind it, and the days counted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DailyInterest {
    /// the exact total rounded once
    pub interest: Money,
    /// the exact total in minor units, before rounding
    pub exact: Rational,
    /// calendar days from fromIso to toIso
    pub days: i64,
}

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

fune!(lending.daily-interest@^1);  // then call daily_interest(…)
impl/rust.rs · 98 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.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::dates_day_count_fraction::day_count_fraction;  ← from dates.day-count-fraction ^1.0.0 · built alongside by fune
use super::dates_days_between::days_between;  ← from dates.days-between ^1.0.0 · built alongside by fune
use super::math_rational::{add_rational, multiply_rational, rational, rational_to_integer, rational_to_value};  ← from math.rational ^1.0.0 · built alongside by fune
use super::money_amount::{money, money_from_value, money_to_value};  ← from money.amount ^1.0.0 · built alongside by fune

/// 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.
///
/// # Panics
/// Panics on an empty or unordered balance list, mixed currencies, a first
/// balance after fromIso, bad dates, a rate out of range, or an exact total
/// too large for math.rational.
pub fn daily_interest(
    balances: &[DatedBalance],
    from_iso: &str,
    to_iso: &str,
    annual_rate_basis_points: i64,
    convention: &str,
    mode: &str,
) -> DailyInterest {
    let b = annual_rate_basis_points;
    if b < -100000 || b > 100000 {
        panic!("annualRateBasisPoints must be between -100000 and 100000, received {}", b);
    }
    if balances.is_empty() {
        panic!("balances must not be empty");
    }
    let days = days_between(from_iso, to_iso);
    if days < 0 {
        panic!("fromIso {} must not be after toIso {}", from_iso, to_iso);
    }
    let currency = balances[0].balance.currency.clone();
    for i in 0..balances.len() {
        days_between(&balances[i].date, &balances[i].date);
        if balances[i].balance.currency != currency {
            panic!("currency mismatch: {} and {}", currency, balances[i].balance.currency);
        }
        if i > 0 && balances[i].date <= balances[i - 1].date {
            panic!("balances must be in strictly ascending date order");
        }
    }
    if balances[0].date.as_str() > from_iso {
        panic!("the first balance starts on {}, after fromIso {}", balances[0].date, from_iso);
    }
    let rate = rational(b, 10000);
    let mut total = rational(0, 1);
    for i in 0..balances.len() {
        let date = balances[i].date.as_str();
        let start = if date > from_iso { date } else { from_iso };
        let next = if i + 1 < balances.len() { balances[i + 1].date.as_str() } else { to_iso };
        let end = if next < to_iso { next } else { to_iso };
        if start >= end {
            continue;
        }
        let fraction = day_count_fraction(start, end, convention);
        let amount = multiply_rational(&rational(balances[i].balance.minor, 1), &rate);
        total = add_rational(
            &total,
            &multiply_rational(&amount, &rational(fraction.numerator, fraction.denominator)),
        );
    }
    DailyInterest {
        interest: money(rational_to_integer(&total, mode), &currency),
        exact: total,
        days,
    }
}

pub fn dated_balance_from_value(v: &Value) -> DatedBalance {
    DatedBalance {
        date: v.get("date").as_str().to_string(),
        balance: money_from_value(v.get("balance")),
    }
}

pub fn daily_interest_to_value(result: &DailyInterest) -> Value {
    Value::obj(vec![
        ("interest", money_to_value(&result.interest)),
        ("exact", rational_to_value(&result.exact)),
        ("days", Value::Int(result.days)),
    ])
}

pub fn fune_vector(args: &[Value]) -> Value {
    let balances: Vec<DatedBalance> = args[0].as_arr().iter().map(dated_balance_from_value).collect();
    daily_interest_to_value(&daily_interest(
        &balances,
        args[1].as_str(),
        args[2].as_str(),
        args[3].as_i64(),
        args[4].as_str(),
        args[5].as_str(),
    ))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 5 dependencies, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add lending.daily-interest
Download for Rust lending.daily-interest-1.0.1-rust.fune · 21,191 bytes sha256 f311de8d3ae9ba9b23a9a021480cf269bc171478b1b84aef4b179d1d62054ab9

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./lending.daily-interest-1.0.1-rust.fune, or fetch it from a terminal with fune pull lending.daily-interest@1.0.1:rust.

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.

CaseArgumentsExpected
£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
CaseArgumentsExpected
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

PathBytes
README.md3,520
impl/python.py2,712
impl/rust.rs3,769
impl/typescript.ts2,874
vectors.json8,927