refactor(gui): the viewer's Decimal arithmetic goes through the kernel (K-U12)

The detector (scripts/raw-decimal-arith.sh ledger-gui) counted 123 places
in the viewer that did Decimal arithmetic outside the kernel; it counts 0
now. No figure the viewer shows changes: a raw operator panicked when the
exact answer did not fit, and add_in_range / sub_in_range / neg panic in
exactly that case; rust_decimal's checked_* methods, which round past 96
bits, became the kernel's Amount::checked_* (exact or None), with the
existing None handling untouched.

Places that are not plain sums or differences, and what each became:
- Sums of balances and posted lines (receivables, due to members, a
  disposal's true-up, a statement row across columns, cleared total):
  kernel::total / total_of; a refused sum is an expect where the figure
  is drawn from the books, and a blank cell in the cash-flow keyed rows
  (which already blank a refused statement).
- The share split (books.rs): share_of(v, share, net), the same multiply
  then divide; the scaled parts use checked_mul_rounded because the
  fraction is a quotient, truncated to cents on the next line.
- Percents of a share (capital, company, reports): checked_mul_rounded
  by 100, exactly what * did.
- Averages and runway (monthly burn, cash runway, net per month, a
  third of an amount, build-in pct): Amount::checked_div, a None joins
  the None or "can't compute" the code already has.
- The 125% watch threshold: checked_mul_rounded and is_some_and.
- Rescale to one place (pct): rescale_exact, keeping the value when it
  would round.
- The demo's rounding to a step ((x / 50).round() * 50): to_step, the
  same half-to-even rounding through round_places.
- f32 widths and usize counts: plain folds, no allow.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-10 01:07:35 -07:00
1 parent 0de906e9ed
commit e4c41ccb3f
16 files changed
+244 -151

No files matched your search

+30 -16
View File
@@ -8,6 +8,8 @@
//! attention" list. Deterministic for a given `today`.
use chrono::{Datelike, Days, Months, NaiveDate};
use ledger_core::kernel::Amount;
use ledger_core::money::{add_in_range, sub_in_range};
use ledger_core::{
AccountType, CloseTo, Currency, Dated1099, Decision1099, Form1099, Frequency, InstrumentKind,
Money, NewAsset, NewRecurring, NewSchedule, ScheduleKind, Side, arap::DocumentLine,
@@ -61,6 +63,15 @@ fn usd(d: Decimal) -> Money {
Money::new(d, Currency::USD)
}
/// `amount` to the nearest whole `step`, a half to the even multiple.
fn to_step(amount: Decimal, step: Decimal) -> Decimal {
use ledger_core::kernel::{Rounding, round_places};
let steps = Amount::checked_div(amount, step)
.and_then(|q| round_places(q, 0, Rounding::HalfToEven))
.expect("a demo amount over a whole step rounds");
Amount::checked_mul(steps, step).expect("a whole number of steps fits")
}
fn dec(n: i64) -> Decimal {
Decimal::from(n)
}
@@ -608,7 +619,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
None,
None,
)?;
card_balance += amt;
card_balance = add_in_range(card_balance, amt);
}
// Advertising some months.
if month_index % 3 == 1 {
@@ -629,7 +640,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
None,
None,
)?;
card_balance += amt;
card_balance = add_in_range(card_balance, amt);
}
}
// Meals and supplies.
@@ -659,7 +670,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
None,
None,
)?;
card_balance += amt;
card_balance = add_in_range(card_balance, amt);
}
if month_index % 4 == 2 {
let date = day(m, 21);
@@ -685,13 +696,13 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
None,
None,
)?;
card_balance += amt;
card_balance = add_in_range(card_balance, amt);
}
}
if month_index == 7 {
let date = day(m, 9);
if in_range(date) {
let amt = dec(1186) + rng.amount(0, 90);
let amt = add_in_range(dec(1186), rng.amount(0, 90));
post(
&mut s,
date,
@@ -712,7 +723,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
None,
None,
)?;
card_balance += amt;
card_balance = add_in_range(card_balance, amt);
}
}
// Pay the card in full on the 25th.
@@ -748,7 +759,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
continue;
}
let cust = customers[rng.pick(customers.len() as u64) as usize];
let amt = (rng.amount(1800, 7800) / dec(50)).round() * dec(50);
let amt = to_step(rng.amount(1800, 7800), dec(50));
let inv = s.create_invoice(
cust,
date,
@@ -791,14 +802,17 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
// A booked figure, so the kernel's rounding and not the
// display's: ten percent to the cent, a half to the even
// cent, as these books have always been drawn.
let fee = ledger_core::kernel::round_places(
gross * Decimal::new(10, 2),
2,
ledger_core::kernel::Rounding::HalfToEven,
)
.expect("a tenth of a four-digit sale rounds to the cent")
+ Decimal::new(30, 2);
let net = gross - fee;
let fee = add_in_range(
ledger_core::kernel::round_places(
Amount::checked_mul(gross, Decimal::new(10, 2))
.expect("a tenth of a four-digit sale fits"),
2,
ledger_core::kernel::Rounding::HalfToEven,
)
.expect("a tenth of a four-digit sale rounds to the cent"),
Decimal::new(30, 2),
);
let net = sub_in_range(gross, fee);
let e = s.create_entry(date, "Gumroad payout — template sales")?;
s.add_line(e, checking, Side::Debit, usd(net))?;
s.add_line(e, fees, Side::Debit, usd(fee))?;
@@ -830,7 +844,7 @@ pub fn build(dir: &Path, today: NaiveDate) -> Result<(), StoreError> {
if month_index % 2 == 0 && month_index > 0 {
let date = day(m, 28);
if in_range(date) {
let amt = (rng.amount(900, 2200) / dec(25)).round() * dec(25);
let amt = to_step(rng.amount(900, 2200), dec(25));
post(
&mut s,
date,