Numbers and data in Jennifer

Jennifer

Money that adds up, statistics, a regression three ways, charts and graphs: decimal, intl, stats, linalg, ml, plot, dot and the @deckyard/money deck.

November 2026, by @mplx.
Based on Jennifer 0.26.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 22

The plan

QuestionTool
Does the invoice add up to the cent?decimal, @deckyard/money
How do I show it to a reader in Vienna?intl, printf grouping
What does a normal day look like?stats
How much does the weather sell?stats, linalg, ml
Can I see it?plot (SVG), dot (Graphviz™)

Reading the data in is the CSV and spreadsheets talk.

2 / 22

Floats cannot count money

io.printf("%v %t\n", 0.1 + 0.2, 0.1 + 0.2 == 0.3);
def net as float init 0.0;
for (def p in [19.99, 4.95, 0.35]) { $net = $net + $p * 3.0; }
io.printf("net %v, VAT %v\n", $net, $net * 0.2);
io.printf("%f|prec=2 %f|prec=2\n", 2.675, 1.005);
0.30000000000000004 false
net 75.86999999999999, VAT 15.174
2.67 1.00
3 / 22

decimal: exact, and you say when to round

def lines as list of decimal.Decimal init [];
for (def p in ["19.99", "4.95", "0.35"]) {
    $lines[] = decimal.mul($p, 3);
}
def net as decimal.Decimal init decimal.sum($lines);
def vat as decimal.Decimal init decimal.mul($net, "0.20");
io.printf("net %v, VAT %v -> %v\n", $net, $vat,
    decimal.round($vat, 2));
io.printf("%t\n", decimal.add("0.1", "0.2") == decimal.from("0.3"));
net 75.87, VAT 15.1740 -> 15.17
true
4 / 22

Rounding modes and division

for (def m in ["half-even", "half-up", "down"]) {
    io.printf("%s|pad=10 %v %v %v\n", $m,
        decimal.round("2.345", 2, $m), decimal.round("2.355", 2, $m),
        decimal.round("-2.345", 2, $m));
}
io.printf("%v %v\n", decimal.div(100, 8), decimal.div(100, 3, 2));
try { decimal.div(100, 3); } catch (e) { show($e); }
half-even  2.34 2.36 -2.34
half-up    2.35 2.36 -2.35
down       2.34 2.35 -2.34
12.5 33.33
decimal: decimal.div: the quotient has no finite decimal form; pass
  a scale (and a rounding mode)
5 / 22

Round per line, or on the total?

def lines as list of string init ["0.99", "0.99", "0.99"];
for (def n in $lines) {
    $perLine[] =
        decimal.round(decimal.mul($n, "0.19"), 2, "half-up");
}
def onTotal as decimal.Decimal init
    decimal.mul(decimal.sum($lines), "0.19");
def share as decimal.Decimal init decimal.div(100, 3, 2, "down");
def last as decimal.Decimal init
    decimal.sub(100, decimal.mul($share, 2));
per line:  0.57
on total:  0.5643 -> 0.56
split 100: 33.33 + 33.33 + 33.34
6 / 22

@deckyard/money: minor units and a code

def three as money.Money init
    money.multiply(money.parse("19.99", "EUR"), "3", "half-even");
io.printf("%v\n", $three);
io.printf("%t\n", money.add($three, money.fromMinor(3, "EUR"))
    == money.parse("60.00", "EUR"));
def gross as money.Money init
    money.multiply($three, "1.19", "half-up");
io.printf("%s EUR = %s JPY\n", money.format($gross), money.format(
    money.exchange($gross, "JPY", "162.345", "half-even")));
money.Money{amount: 5997, currency: "EUR"}
true
71.36 EUR = 11585 JPY
7 / 22

money.allocate: no cent lost

def bill as money.Money init money.fromMinor(10000, "EUR");
def tip as money.Money init money.fromMinor(5, "EUR");
printParts(money.allocateTo($bill, 3));
printParts(money.allocate($bill, ["2", "1"]));
printParts(money.allocate($tip, ["0.7", "0.3"]));
def yen as money.Money init money.fromMinor(1200, "JPY");
try { money.add($bill, $yen); } catch (e) {
    io.printf("%s | %s\n", $e.kind, $e.message);
}
33.34 + 33.33 + 33.33
66.67 + 33.33
0.04 + 0.01
money | money: different currencies: EUR and JPY
8 / 22

Money for a reader: grouping and the mark

func money(d as decimal.Decimal, sep as string, mark as string) {
    def r as decimal.Decimal init decimal.round(decimal.abs($d), 2);
    def p as list of string init
        strings.split(decimal.toString($r), ".");
    def whole as string init io.sprintf("%d|group=3|sep={$sep}",
        convert.toInt($p[0]));
    def sign as string init "";
    if (decimal.sign($d) < 0) { $sign = "-"; }
    return $sign + $whole + $mark + $p[1];
}
3.703,50 | 3,703.50
-1.234.567,00
9 / 22

intl: the words around the number

intl.load("en", {"due": "Amount due: EUR %sum% (%n% items)"});
intl.load("de", {"due": "Zu zahlen: %sum% EUR (%n% Artikel)"});
for (def loc in ["de-AT", "en-GB", "nl"]) {
    intl.setLocale($loc);
    def m as list of string init $marks[$loc];
    def amount as string init money($sum, $m[0], $m[1]);
    io.printf("%s\n", intl.tr("due", {"sum": $amount, "n": 3}));
}
io.printf("%s\n", intl.tr("refund"));
Zu zahlen: 3.703,50 EUR (3 Artikel)
Amount due: EUR 3,703.50 (3 items)
Amount due: EUR 3.703,50 (3 items)
refund
10 / 22

stats: what a normal day looks like

def cones as list of int init [112, 98, 128, 165, 139, 108, 118,
    120, 151, 160, 196, 94, 121, 410];      # 14 days, one festival
io.printf("mean %f|prec=1, median %f|prec=1\n", stats.mean($cones),
    stats.median($cones));
io.printf("stddev %f|prec=1, sample %f|prec=1\n",
    stats.stddev($cones), stats.sampleStddev($cones));
io.printf("p90 %f|prec=1, IQR %f|prec=1, MAD %f|prec=1\n",
    stats.percentile($cones, 90), stats.iqr($cones),
    stats.mad($cones));
mean 151.4, median 124.5
stddev 76.7, sample 79.6
p90 186.7, IQR 44.2, MAD 21.5
11 / 22

Correlation and a straight line

# 13 ordinary days: max temperature (C) and cones sold
def temp as list of float init [19.0, 17.5, 23.0, 27.5, 26.0, 16.0,
    17.0, 21.0, 25.0, 29.0, 31.5, 18.5, 18.0];
io.printf("r = %f|prec=3\n", stats.correlation($temp, $cones));
def fit as stats.Regression init
    stats.linearRegression($temp, $cones);
io.printf("cones = %f|prec=2 * temp %f|prec=2|sign=always\n",
    $fit.slope, $fit.intercept);
io.printf("r2 %f|prec=3, slope p %f|prec=1|sci=true\n", $fit.r2,
    $fit.pValue);
r = 0.930
cones = 5.36 * temp +12.44
r2 0.865, slope p 4.1e-06
12 / 22

The same fit with linalg

def X as list of list of float init [];
for (def t in $temp) { $X[] = [1.0, $t]; }      # a column of ones
def Xt as list of list of float init linalg.transpose($X);
def beta as list of float init linalg.solve(linalg.matmul($Xt, $X),
    linalg.matmul($Xt, $cones));
io.printf("intercept %f|prec=2, slope %f|prec=2\n", $beta[0],
    $beta[1]);
def res as list of float init
    linalg.sub($cones, linalg.matmul($X, $beta));
io.printf("residual norm %f|prec=1\n", linalg.norm($res));
intercept 12.44, slope 5.36
residual norm 37.5
13 / 22

ml: a second feature, and a forecast

# [temperature, weekend 0/1] per day
def X as list of list of float init [[19.0, 0.0], [17.5, 0.0], ...];
def m as ml.Model init ml.linearRegression($X, $y);
io.printf("coef %a|prec=2, intercept %f|prec=2\n",
    ml.coefficients($m), ml.intercept($m));
def fit as list of float init ml.predict($m, $X);
io.printf("r2 %f|prec=3, rmse %f|prec=1\n", ml.r2($y, $fit),
    ml.rmse($y, $fit));
io.printf("Saturday, 30 C: %a|prec=0\n",
    ml.predict($m, [[30.0, 1.0]]));
coef [6.55, 23.92], intercept -19.63
r2 0.949, rmse 6.4
Saturday, 30 C: [201]
14 / 22

ml: clusters, then neighbours

# [average basket in EUR, visits per month] for eight customers
def X as list of list of float init [[4.2, 22.0], [3.8, 25.0], ...];
math.randSeed(7);
def scaler as ml.Model init ml.standardScaler($X);
def Z as list of list of float init ml.transform($scaler, $X);
def km as ml.Model init ml.kMeans($Z, 3);
io.printf("clusters %a\n", ml.predict($km, $Z));
def nb as ml.Model init ml.kNN($Z, ml.predict($km, $Z), 3);
io.printf("new customer -> %a\n",
    ml.predict($nb, ml.transform($scaler, [[17.0, 5.0]])));
clusters [1, 1, 1, 0, 0, 0, 2, 2]
new customer -> [0]
15 / 22

plot: a chart as an SVG string

def opts as plot.Options init plot.defaults();
$opts.title = "Cones sold by temperature";
$opts.legendPos = "top-left";
def days as plot.Series init plot.points("days", $temp, $cones);
def line as plot.Series init plot.series("fit", $xs, $ys);
$line.dash = true;
plot.save(plot.chart([$days, $line], $opts), "cones.svg");
$ rsvg-convert -w 640 cones.svg -o cones.png
16 / 22

The chart

Cones sold by temperature, with the fitted line

17 / 22

Bars, grouped

$opts.barLabels = true;
$opts.refLines = [plot.hline(124.5, "")];
plot.save(plot.bars($days, [$w1, $w2], $opts), "week.svg");

Cones sold per day, two weeks side by side

18 / 22

dot: a graph from data

def steps as list of list of string init [
    ["draft", "send", "sent"], ["sent", "pay", "paid"],
    ["sent", "due date", "overdue"],
    ["overdue", "remind", "reminded"], ["reminded", "pay", "paid"],
    ["reminded", "90 days", "written off"]];
def g as dot.Graph init dot.digraph("invoice");
$g = dot.graphAttr($g, "rankdir", "LR");
for (def t in $steps) {
    $g = dot.edgeWith($g, $t[0], $t[2], {"label": $t[1]});
}
io.printf("%s", dot.render($g));

The invoice state machine, drawn by Graphviz

19 / 22

Strict at the boundaries

def big as int init 9223372036854775807;
try { io.printf("%d\n", $big + 1); } catch (e) { show($e); }
try { io.printf("%v\n", 1e308 * 10.0); } catch (e) { show($e); }
try { io.printf("%v\n", math.sqrt(-1.0)); } catch (e) { show($e); }
try { io.printf("%v\n", stats.mean([])); } catch (e) { show($e); }
io.printf("%v\n", decimal.pow(2, 64));
runtime: integer overflow: 9223372036854775807 + 1
runtime: float overflow: 1e+308 * 10 produced a non-finite result
runtime: sqrt(): undefined for negative input -1.0
runtime: stats.mean: list is empty
18446744073709551616
20 / 22

When to reach for which

You haveUse
exact sums, tax rates, fixed placesdecimal
currency amounts, splits, FX@deckyard/money
measurements and ratiosfloat, math
a column to summarise or teststats
a fit, a forecast, clusterslinalg, ml
a chart or a diagramplot, dot
21 / 22

Read more

Based on Jennifer 0.26.0, November 2026.

22 / 22