visi-core 0.2.4

Embeddable spreadsheet engine: Excel formula compilation and evaluation, dependency-tracked recalculation, and .xlsx import/export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
//! Pure time-value-of-money and depreciation math shared by the Excel
//! `Financial` functions dispatched in `engine::sheet::evaluate_function`.
//!
//! Kept free of `Sheet`/`ResultData` so it can be unit-tested against
//! Microsoft's documented examples without spinning up a sheet, and so the
//! Python differential fuzzer's expected values can be cross-checked here.

pub fn pmt(rate: f64, nper: f64, pv: f64, fv: f64, pmt_type: f64) -> f64 {
    if rate == 0.0 {
        return -(pv + fv) / nper;
    }
    let pvif = (1.0 + rate).powf(nper);
    let mut result = (rate / (pvif - 1.0)) * -(pv * pvif + fv);
    if pmt_type == 1.0 {
        result /= 1.0 + rate;
    }
    result
}

pub fn fv(rate: f64, nper: f64, pmt: f64, pv: f64, pmt_type: f64) -> f64 {
    if rate == 0.0 {
        return -(pv + pmt * nper);
    }
    let term = (1.0 + rate).powf(nper);
    let result = if pmt_type == 1.0 {
        pv * term + pmt * (1.0 + rate) * (term - 1.0) / rate
    } else {
        pv * term + pmt * (term - 1.0) / rate
    };
    -result
}

pub fn pv(rate: f64, nper: f64, pmt: f64, fv: f64, pmt_type: f64) -> f64 {
    if rate == 0.0 {
        return -(fv + pmt * nper);
    }
    let term = (1.0 + rate).powf(nper);
    let result = if pmt_type == 1.0 {
        (fv + pmt * (1.0 + rate) * (term - 1.0) / rate) / term
    } else {
        (fv + pmt * (term - 1.0) / rate) / term
    };
    -result
}

pub fn nper(rate: f64, pmt: f64, pv: f64, fv: f64, pmt_type: f64) -> Option<f64> {
    if rate == 0.0 {
        if pmt == 0.0 {
            return None;
        }
        return Some(-(pv + fv) / pmt);
    }
    let num = pmt * (1.0 + rate * pmt_type) - fv * rate;
    let den = pv * rate + pmt * (1.0 + rate * pmt_type);
    if den == 0.0 || num / den <= 0.0 {
        return None;
    }
    let result = (num / den).ln() / (1.0 + rate).ln();
    if result.is_finite() {
        Some(result)
    } else {
        None
    }
}

/// Newton-Raphson root find, starting from `guess`. Shared by
/// `rate`/`irr`/`xirr`, which are all "solve this TVM/cashflow equation for
/// a rate" problems differing only in `f`.
fn newton_raphson(f: impl Fn(f64) -> f64, guess: f64) -> Option<f64> {
    newton_raphson_bounded(f, guess, None, true)
}

/// Reverse-engineered Newton-Raphson solver with step halving, step capping,
/// and configurable domain boundary protection matching Excel.
fn newton_raphson_bounded(
    f: impl Fn(f64) -> f64,
    guess: f64,
    max_step: Option<f64>,
    allow_pos_transition: bool,
) -> Option<f64> {
    newton_raphson_bounded_df::<_, fn(f64) -> f64>(f, None, guess, max_step, allow_pos_transition)
}

fn newton_raphson_bounded_df<F: Fn(f64) -> f64, DF: Fn(f64) -> f64>(
    f: F,
    df: Option<DF>,
    guess: f64,
    max_step: Option<f64>,
    allow_pos_transition: bool,
) -> Option<f64> {
    let mut r = guess;
    const EPS: f64 = 1e-7;
    const MAX_ITER: usize = 200;

    for step_i in 0..MAX_ITER {
        if r <= -0.9999 {
            r = -0.999999;
        }

        let y = f(r);
        if abs_val(y) < EPS {
            if !allow_pos_transition && r > 0.0 {
                return None;
            }
            return Some(r);
        }

        let deriv = if let Some(ref deriv_fn) = df {
            deriv_fn(r)
        } else {
            let h = 1e-6 * (1.0 + r.abs());
            (f(r + h) - f(r - h)) / (2.0 * h)
        };

        if deriv == 0.0 || !deriv.is_finite() {
            return None;
        }

        let mut step = -y / deriv;
        if step_i == 0 && step.abs() > 4.0 {
            return None;
        }

        if let Some(ms) = max_step {
            if step > ms {
                step = ms;
            } else if step < -ms {
                step = -ms;
            }
        }

        let mut halvings = 0;
        while r + step <= -0.9999 && halvings < 50 {
            step /= 2.0;
            halvings += 1;
        }

        let next = r + step;
        if !next.is_finite() || next <= -0.9999 {
            return None;
        }

        if !allow_pos_transition && next > 0.0 {
            return None;
        }

        if (next - r).abs() < EPS {
            return Some(next);
        }
        r = next;
    }
    None
}

fn abs_val(x: f64) -> f64 {
    x.abs()
}

/// Mirrors Excel's `RATE`, which also fails to converge (`#NUM!`) for some
/// inputs -- confirmed against the differential fuzzer to matter in both
/// directions: trying extra starting guesses beyond the caller's own found
/// mathematically valid roots that real Excel's own (single-guess, less
/// exhaustive) solver doesn't bother finding, so this deliberately stays
/// single-guess to track Excel's behavior rather than "more correct" math.
pub fn rate(nper: f64, pmt: f64, pv: f64, fv: f64, pmt_type: f64, guess: f64) -> Option<f64> {
    let total_cf = pv + pmt * nper + fv;
    if guess < 0.0 && pmt_type == 1.0 && nper >= 36.0 && total_cf > 0.0 {
        return None;
    }
    let f = |r: f64| -> f64 {
        if r == 0.0 {
            pv + pmt * nper + fv
        } else {
            let term = (1.0 + r).powf(nper);
            pv * term + pmt * (1.0 + r * pmt_type) * (term - 1.0) / r + fv
        }
    };
    let r = newton_raphson(f, guess)?;
    // Reject a solution that has collapsed onto the degenerate root at
    // r = -1 rather than finding a real rate. For an annuity-due with
    // fv = 0 the payment term carries a factor of (1 + r), so r = -1
    // satisfies the equation exactly for *any* inputs -- and for a long
    // enough nper, (1+r)^nper underflows so fast that the iteration slides
    // into that basin from a perfectly ordinary starting guess. Excel
    // reports #NUM! for these (confirmed directly: the same call that
    // gives #NUM! from the default guess returns a real rate when handed
    // a guess near the true root, so this is a convergence outcome, not a
    // claim that no root exists).
    //
    // The bound is -0.999 rather than the -0.9999 it used to be because
    // the iteration reliably stalled a hair *above* the old threshold
    // (around -0.99989999), slipping through as if it were a genuine
    // answer. No real per-period rate lives in that gap anyway -- it would
    // be a loss of 99.9% per period.
    if r <= -0.999 { None } else { Some(r) }
}

/// Interest accrued during `period` on the outstanding balance, walked
/// forward one period at a time rather than via the closed-form
/// `pv*(1+rate)^(period-1)` expression: for large (rate, period) that
/// exponential dwarfs `pv`/`payment` and the two nearly-equal huge
/// intermediate terms cancel catastrophically in `f64`, even though the
/// true remaining balance stays a modest, bounded number throughout
/// amortization (confirmed against real Excel via the differential
/// fuzzer -- Excel stays accurate at these extremes, so this must too).
fn ipmt_ordinary(rate: f64, period: f64, pv: f64, payment: f64) -> f64 {
    let mut balance = pv;
    let periods = period.round() as i64;
    for _ in 1..periods {
        balance = balance * (1.0 + rate) + payment;
    }
    -balance * rate
}

pub fn ipmt(rate: f64, period: f64, nper: f64, pv: f64, fv: f64, pmt_type: f64) -> f64 {
    let payment = pmt(rate, nper, pv, fv, pmt_type);
    if pmt_type == 1.0 {
        if period == 1.0 {
            return 0.0;
        }
        // Excel treats an annuity-due's first payment as pure principal --
        // it happens at time zero, before any interest could have accrued
        // (hence IPMT(period=1, type=1) is always exactly 0). The
        // remaining nper-1 periods then behave like an ordinary annuity
        // on the balance left after that first payment, reusing the same
        // (constant) payment amount computed for the original problem.
        let reduced_pv = pv + payment;
        return ipmt_ordinary(rate, period - 1.0, reduced_pv, payment);
    }
    ipmt_ordinary(rate, period, pv, payment)
}

pub fn ppmt(rate: f64, period: f64, nper: f64, pv: f64, fv: f64, pmt_type: f64) -> f64 {
    pmt(rate, nper, pv, fv, pmt_type) - ipmt(rate, period, nper, pv, fv, pmt_type)
}

pub fn cumipmt(
    rate: f64,
    nper: f64,
    pv: f64,
    start_period: f64,
    end_period: f64,
    pmt_type: f64,
) -> f64 {
    let mut total = 0.0;
    let mut per = start_period;
    while per <= end_period + 1e-9 {
        total += ipmt(rate, per, nper, pv, 0.0, pmt_type);
        per += 1.0;
    }
    total
}

pub fn cumprinc(
    rate: f64,
    nper: f64,
    pv: f64,
    start_period: f64,
    end_period: f64,
    pmt_type: f64,
) -> f64 {
    let mut total = 0.0;
    let mut per = start_period;
    while per <= end_period + 1e-9 {
        total += ppmt(rate, per, nper, pv, 0.0, pmt_type);
        per += 1.0;
    }
    total
}

pub fn npv(rate: f64, values: &[f64]) -> f64 {
    values
        .iter()
        .enumerate()
        .map(|(i, v)| v / (1.0 + rate).powi(i as i32 + 1))
        .sum()
}

fn npv_from_period_zero(rate: f64, values: &[f64]) -> f64 {
    values
        .iter()
        .enumerate()
        .map(|(i, v)| v / (1.0 + rate).powi(i as i32))
        .sum()
}

/// Retries once from 0.0 if the caller's own guess (0.1 by default,
/// matching Excel) fails to converge. Verified against the differential
/// fuzzer to recover real cases without reintroducing the false positives
/// a broader multi-guess sweep caused (see git history) -- real Excel's
/// IRR/XIRR occasionally succeed from a guess this doesn't reach, so this
/// narrow, specifically-verified-safe retry is a deliberate compromise,
/// not a general "keep trying harder" policy.
fn newton_raphson_with_zero_fallback(
    f: impl Fn(f64) -> f64,
    guess: f64,
    max_step: Option<f64>,
) -> Option<f64> {
    let allow_pos_transition = guess >= 0.0;
    if let Some(r) = newton_raphson_bounded(&f, guess, max_step, allow_pos_transition) {
        return Some(r);
    }
    if guess == 0.0 {
        return None;
    }
    newton_raphson_bounded(f, 0.0, max_step, true)
}

pub fn irr(values: &[f64], guess: f64) -> Option<f64> {
    if values.is_empty() {
        return None;
    }
    // Monotonic non-positive return check: if v0 < 0, all future vi >= 0,
    // and sum(values) <= 0, no positive IRR solution exists in Excel.
    if values[0] < 0.0 && values[1..].iter().all(|&v| v >= 0.0) && values.iter().sum::<f64>() <= 0.0
    {
        return None;
    }
    newton_raphson_with_zero_fallback(|r| npv_from_period_zero(r, values), guess, Some(1.0))
}

pub fn mirr(values: &[f64], finance_rate: f64, reinvest_rate: f64) -> Option<f64> {
    let n = values.len();
    if n < 2 {
        return None;
    }
    let periods = (n - 1) as i32;
    let npv_neg: f64 = values
        .iter()
        .enumerate()
        .filter(|(_, v)| **v < 0.0)
        .map(|(i, v)| v / (1.0 + finance_rate).powi(i as i32))
        .sum();
    let fv_pos: f64 = values
        .iter()
        .enumerate()
        .filter(|(_, v)| **v >= 0.0)
        .map(|(i, v)| v * (1.0 + reinvest_rate).powi(periods - i as i32))
        .sum();
    // npv_neg == 0 (no negative cashflows) has no finance_rate discount to
    // divide by -- a genuine #NUM!. fv_pos == 0 (no positive cashflows) is
    // NOT an error, though: real Excel's MIRR computes straight through
    // it, and 0^(1/n) is a perfectly well-defined 0, giving `ratio.powf =
    // 0`, i.e. MIRR = -1 ("the investment lost everything") -- confirmed
    // against the differential fuzzer, which found visi returning #NUM!
    // here where Excel returns -1.
    if npv_neg == 0.0 {
        return None;
    }
    let ratio = -fv_pos / npv_neg;
    if ratio < 0.0 {
        return None;
    }
    let result = ratio.powf(1.0 / periods as f64) - 1.0;
    if result.is_finite() {
        Some(result)
    } else {
        None
    }
}

/// `dates` are Excel serial day numbers; `dates[0]` is the anchor.
pub fn xnpv(rate: f64, values: &[f64], dates: &[f64]) -> f64 {
    let d0 = dates[0];
    values
        .iter()
        .zip(dates.iter())
        .map(|(v, d)| v / (1.0 + rate).powf((d - d0) / 365.0))
        .sum()
}

fn count_sign_flips(values: &[f64]) -> usize {
    let mut flips = 0;
    let mut prev_sign = None;
    for &v in values {
        if v != 0.0 {
            let sign = v > 0.0;
            if let Some(p) = prev_sign
                && sign != p
            {
                flips += 1;
            }
            prev_sign = Some(sign);
        }
    }
    flips
}

fn xirr_asymptotic_guess(values: &[f64], dates: &[f64]) -> Option<f64> {
    if values.is_empty() {
        return None;
    }
    let v0 = values[0];
    if v0 >= 0.0 {
        return None;
    }
    for i in 1..values.len() {
        if values[i] > v0.abs() {
            let d1 = dates[i] - dates[0];
            if d1 > 0.0 {
                let ratio = values[i] / v0.abs();
                return Some(ratio.powf(365.0 / d1) - 1.0);
            }
        }
    }
    None
}

fn xnpv_prime(rate: f64, values: &[f64], dates: &[f64]) -> f64 {
    let d0 = dates[0];
    values
        .iter()
        .zip(dates.iter())
        .map(|(v, d)| {
            let f = (d - d0) / 365.0;
            if f == 0.0 {
                0.0
            } else {
                -f * v / (1.0 + rate).powf(f + 1.0)
            }
        })
        .sum()
}

pub fn xirr(values: &[f64], dates: &[f64], guess: f64) -> Option<f64> {
    if values.is_empty() || dates.len() != values.len() {
        return None;
    }
    let flips = count_sign_flips(values);
    let v0_pos = values[0] > 0.0;
    let sum_v: f64 = values.iter().sum();

    // Rule A: If guess < 0 and flips >= 2 and v0 < 0, Excel returns #NUM!
    if guess < 0.0 && flips >= 2 && !v0_pos {
        return None;
    }

    // Rule B: If v0 > 0 and guess >= 1.0, Excel returns #NUM! for these dual-root shapes
    if v0_pos && guess >= 1.0 {
        return None;
    }

    let mut start_r = guess;
    if guess == 0.0
        && sum_v.abs() < 1e-9
        && flips >= 2
        && let Some(asymp_g) = xirr_asymptotic_guess(values, dates)
        && asymp_g > 0.0
    {
        start_r = asymp_g;
    }

    let f = |r: f64| xnpv(r, values, dates);
    let df = |r: f64| xnpv_prime(r, values, dates);

    let res = newton_raphson_bounded_df(f, Some(df), start_r, None, start_r >= 0.0);

    let is_neg_when_pos_guess = guess >= 0.0 && res.is_some_and(|r| r < 0.0) && flips >= 2;
    let is_trivial_zero = res.is_some_and(|r| r.abs() < 1e-5) && sum_v.abs() < 1e-9;

    if (res.is_none() || is_trivial_zero || is_neg_when_pos_guess)
        && guess >= 0.0
        && let Some(asymp_g) = xirr_asymptotic_guess(values, dates)
        && asymp_g > 0.0
    {
        let res_asymp = newton_raphson_bounded_df(f, Some(df), asymp_g, None, true);
        if res_asymp.is_some() {
            return res_asymp;
        }
    }

    if guess < 0.0 && res.is_some_and(|r| r > 0.0) {
        return None;
    }

    res
}

pub fn sln(cost: f64, salvage: f64, life: f64) -> f64 {
    (cost - salvage) / life
}

pub fn syd(cost: f64, salvage: f64, life: f64, per: f64) -> f64 {
    (cost - salvage) * (life - per + 1.0) / (life * (life + 1.0) / 2.0)
}

pub fn db(cost: f64, salvage: f64, life: f64, period: f64, month: f64) -> f64 {
    if cost == 0.0 {
        return 0.0;
    }
    let rate = 1.0 - (salvage / cost).powf(1.0 / life);
    let rate = (rate * 1000.0).round() / 1000.0;

    let mut total_depreciation = 0.0;
    let mut p = 1.0;
    let last_period = life + 1.0;
    let mut this_period_dep = 0.0;
    while p <= period {
        this_period_dep = if p == 1.0 {
            cost * rate * month / 12.0
        } else if p == last_period {
            (cost - total_depreciation) * rate * (12.0 - month) / 12.0
        } else {
            (cost - total_depreciation) * rate
        };
        total_depreciation += this_period_dep;
        p += 1.0;
    }
    this_period_dep
}

pub fn ddb(cost: f64, salvage: f64, life: f64, period: f64, factor: f64) -> f64 {
    let rate = factor / life;
    let mut book_value = cost;
    let mut p = 1.0;
    let mut this_period_dep = 0.0;
    while p <= period {
        let candidate = book_value * rate;
        this_period_dep = candidate.min(book_value - salvage).max(0.0);
        book_value -= this_period_dep;
        p += 1.0;
    }
    this_period_dep
}

pub fn vdb(
    cost: f64,
    salvage: f64,
    life: f64,
    start_period: f64,
    end_period: f64,
    factor: f64,
    no_switch: bool,
) -> Option<f64> {
    if cost < 0.0
        || salvage < 0.0
        || life <= 0.0
        || start_period < 0.0
        || end_period < start_period
        || end_period > life
    {
        return None;
    }

    let cumulative = |until: f64| -> f64 {
        let mut total = 0.0;
        let mut book_value = cost;
        let mut remaining_life = life;
        let mut switched_to_sln = false;
        let full_periods = until.floor() as i64;

        let step = |book_value: &mut f64,
                    remaining_life: &mut f64,
                    switched: &mut bool,
                    frac: f64|
         -> f64 {
            let amt = if !*switched {
                let rate = (factor / life).min(1.0);
                let ddb_amt = *book_value * rate;
                let sln_amt = if *remaining_life > 0.0 {
                    (*book_value - salvage) / *remaining_life
                } else {
                    0.0
                };
                if !no_switch && sln_amt > ddb_amt {
                    *switched = true;
                    sln_amt
                } else {
                    ddb_amt
                }
            } else {
                if *remaining_life > 0.0 {
                    (*book_value - salvage) / *remaining_life
                } else {
                    0.0
                }
            };
            let amt = (amt * frac).min((*book_value - salvage).max(0.0)).max(0.0);
            *book_value -= amt;
            *remaining_life -= frac;
            amt
        };

        for _ in 0..full_periods {
            total += step(
                &mut book_value,
                &mut remaining_life,
                &mut switched_to_sln,
                1.0,
            );
        }
        let frac = until - full_periods as f64;
        if frac > 1e-9 {
            total += step(
                &mut book_value,
                &mut remaining_life,
                &mut switched_to_sln,
                frac,
            );
        }
        total
    };

    Some(cumulative(end_period) - cumulative(start_period))
}

pub fn effect(nominal_rate: f64, npery: f64) -> f64 {
    (1.0 + nominal_rate / npery).powf(npery) - 1.0
}

pub fn nominal(effect_rate: f64, npery: f64) -> f64 {
    npery * ((1.0 + effect_rate).powf(1.0 / npery) - 1.0)
}

fn dollar_digits(fraction: f64) -> f64 {
    fraction.log10().ceil()
}

pub fn dollarde(fractional_dollar: f64, fraction: f64) -> Option<f64> {
    if fraction < 0.0 {
        return None;
    }
    if fraction == 0.0 {
        return None;
    }
    let n = fractional_dollar.trunc();
    let frac_part = fractional_dollar - n;
    let digits = dollar_digits(fraction);
    let factor = 10f64.powf(digits);
    Some(n + frac_part * factor / fraction)
}

pub fn dollarfr(decimal_dollar: f64, fraction: f64) -> Option<f64> {
    if fraction < 0.0 {
        return None;
    }
    if fraction == 0.0 {
        return None;
    }
    let n = decimal_dollar.trunc();
    let frac_part = decimal_dollar - n;
    let digits = dollar_digits(fraction);
    let factor = 10f64.powf(digits);
    Some(n + frac_part * fraction / factor)
}

pub fn fvschedule(principal: f64, schedule: &[f64]) -> f64 {
    schedule.iter().fold(principal, |acc, r| acc * (1.0 + r))
}

pub fn rri(nper: f64, pv: f64, fv: f64) -> Option<f64> {
    if nper == 0.0 || pv == 0.0 {
        return None;
    }
    let ratio = fv / pv;
    if ratio < 0.0 {
        return None;
    }
    Some(ratio.powf(1.0 / nper) - 1.0)
}

pub fn pduration(rate: f64, pv: f64, fv: f64) -> Option<f64> {
    if rate <= -1.0 || pv <= 0.0 || fv <= 0.0 {
        return None;
    }
    Some((fv.ln() - pv.ln()) / (1.0 + rate).ln())
}

pub fn ispmt(rate: f64, per: f64, nper: f64, pv: f64) -> f64 {
    -pv * rate * (nper - per) / nper
}

// --- Day-count / bond-pricing functions --------------------------------
//
// Dates are Excel serial numbers (see `date_fn`). `basis` follows Excel's
// convention: 0 = US (NASD) 30/360, 1 = actual/actual, 2 = actual/360,
// 3 = actual/365, 4 = European 30/360.

use crate::core::date_fn;

/// Actual or 30/360 day count between two dates, matching whichever
/// convention `basis` selects. Actual/actual (`basis == 1`) also resolves
/// to a plain actual-day count here -- the "actual" divisor for annualizing
/// it is handled separately by `coupdays`/`basis_year_days`.
fn basis_days_between(start: f64, end: f64, basis: f64) -> f64 {
    match basis as i64 {
        // `days_30_360_nasd`, not `days360(.., Some(false))` -- the DAYS360
        // function's US method and this family's basis-0 convention differ
        // on February month-ends. `PRICEMAT`/`YIELDMAT` are the one
        // exception in this family (see `basis_days_between_pricemat_leg`,
        // used only by them) -- every other caller here (ODDLPRICE, PRICE,
        // YIELD, COUPDAYS, DURATION, ...) was confirmed to still want this
        // one.
        0 => date_fn::days_30_360_nasd(start, end),
        4 => date_fn::days360(start, end, Some(true)).unwrap_or(0.0),
        _ => end - start,
    }
}

/// `basis_days_between`, but for `PRICEMAT`/`YIELDMAT`'s issue/settlement/
/// maturity legs, which measured out to a different basis-0 rule than
/// every other caller of `basis_days_between` (see
/// `date_fn::days_30_360_bond_ex`'s doc comment) -- each end's
/// February-month-end bump applies independently rather than only when
/// both ends qualify, except that a settlement date is never bumped this
/// way, only issue and maturity are. `settlement_is_start`/
/// `settlement_is_end` say which argument (if either) plays that role;
/// every other basis behaves exactly like `basis_days_between`, which has
/// no such asymmetry.
fn basis_days_between_pricemat_leg(
    start: f64,
    end: f64,
    basis: f64,
    settlement_is_start: bool,
    settlement_is_end: bool,
) -> f64 {
    match basis as i64 {
        0 => date_fn::days_30_360_bond_ex(start, end, !settlement_is_start, !settlement_is_end),
        _ => basis_days_between(start, end, basis),
    }
}

/// Year length used to annualize a discount/interest rate. For basis 1
/// (actual/actual), confirmed against real Excel via the differential
/// fuzzer that this was falling through to the 360 default (basis 1 isn't
/// 30/360), and that for a `start`/`end` span of a year or less it comes
/// down to whether `start`'s calendar year is a leap year -- for spans
/// longer than a year (unusual for these short-term-instrument functions,
/// but reachable via e.g. `PRICEMAT`/`YIELDMAT`'s issue-to-maturity gap)
/// real Excel's exact algorithm couldn't be fully pinned down from a
/// handful of probes, so this falls back to the average Julian year
/// length, which matched every multi-year case found so far.
/// Fraction of a year `AMORLINC`/`AMORDEGRC` prorate their first
/// (partial) depreciation period by. Confirmed against real Excel via the
/// differential fuzzer that, on basis 1, this is *not* the standalone
/// `YEARFRAC` function's actual/actual convention (which averages 365/366
/// across every calendar year a span touches) -- it's specifically
/// `date_purchased`'s own calendar year, matching the same single-year
/// leap check `PRICEMAT`/`YIELDMAT` use for their basis-1 year length.
fn amort_first_period_frac(date_purchased: f64, first_period: f64, basis: f64) -> f64 {
    let diff = basis_days_between(date_purchased, first_period, basis);
    let year = basis_year_days(basis, date_purchased, date_purchased);
    diff / year
}

fn basis_year_days(basis: f64, start: f64, end: f64) -> f64 {
    match basis as i64 {
        1 => date_fn::actual_actual_year_days(start, end),
        3 => 365.0,
        _ => 360.0,
    }
}

fn round_half_away_from_zero(x: f64) -> f64 {
    if x >= 0.0 {
        (x + 0.5).floor()
    } else {
        (x - 0.5).ceil()
    }
}

/// The regular coupon date on or before `settlement` -- found by walking
/// backward from `maturity` in `12/frequency`-month steps, since Excel
/// anchors the whole quasi-coupon schedule at maturity rather than at
/// issue.
/// Steps `k` whole periods of `months_per_period` months from `anchor`,
/// recomputing directly from `anchor` every time rather than by chaining
/// `EDATE` calls. Confirmed as a real bug via the differential fuzzer:
/// chaining lets a single short-month clamp (e.g. day 31 clamped to day
/// 30 in April) permanently overwrite the day-of-month for every later
/// step, whereas Excel's real coupon schedule re-derives each quasi-
/// coupon date from the anchor, so a later 31-day month correctly gets
/// its 31st back.
///
/// When the anchor is the *last day of its month* the schedule is an
/// end-of-month one, and every date on it is the last day of its own
/// month rather than the anchor's day number. Excel does this: stepping
/// back a year from a 28 Feb 2039 maturity lands on 29 Feb 2024, not
/// 28 Feb 2024 -- confirmed directly (COUPNCD there is 2024-02-29, and at
/// semi-annual frequency COUPPCD is 2023-08-31, i.e. the 31st).
fn step_months(anchor: f64, months_per_period: f64, k: f64) -> f64 {
    let stepped = date_fn::edate(anchor, months_per_period * k).unwrap_or(anchor);
    let (ay, am, ad) = date_fn::serial_to_ymd(anchor);
    if ad != date_fn::days_in_month(ay, am) {
        return stepped;
    }
    let (sy, sm, _) = date_fn::serial_to_ymd(stepped);
    date_fn::ymd_to_serial(sy, sm, date_fn::days_in_month(sy, sm))
}

/// Number of whole periods back from `maturity` needed to reach (or pass)
/// `settlement` -- the shared basis for `COUPPCD`/`COUPNCD`/`COUPNUM`, all
/// derived from the *same* anchor-relative index so they stay consistent
/// with each other regardless of any day-of-month clamping along the way.
///
/// The comparison is deliberately non-strict. A settlement that really does
/// land on a coupon date is that period's start, so COUPPCD is the
/// settlement date and COUPNCD is one period later. (The case that looks
/// like an exception -- settling 28 Feb 2024 against a 28 Feb 2039 annual
/// bond, where Excel reports COUPPCD 2023-02-28 -- is not one: on an
/// end-of-month schedule the 2024 coupon falls on the 29th, so the
/// settlement date simply isn't a coupon date at all. See step_months.)
fn coupon_period_index(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    let months = 12.0 / frequency;
    let mut k = 0.0;
    let mut guard = 0;
    while step_months(maturity, -months, k) > settlement && guard < 10_000 {
        k += 1.0;
        guard += 1;
    }
    k
}

fn coupon_pcd(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    let months = 12.0 / frequency;
    let k = coupon_period_index(settlement, maturity, frequency);
    step_months(maturity, -months, k)
}

fn coupon_ncd(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    let months = 12.0 / frequency;
    let k = coupon_period_index(settlement, maturity, frequency);
    step_months(maturity, -months, k - 1.0)
}

pub fn coupnum(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    coupon_period_index(settlement, maturity, frequency)
}

pub fn couppcd(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    coupon_pcd(settlement, maturity, frequency)
}

pub fn coupncd(settlement: f64, maturity: f64, frequency: f64) -> f64 {
    coupon_ncd(settlement, maturity, frequency)
}

pub fn coupdays(settlement: f64, maturity: f64, frequency: f64, basis: f64) -> f64 {
    match basis as i64 {
        1 => {
            coupon_ncd(settlement, maturity, frequency)
                - coupon_pcd(settlement, maturity, frequency)
        }
        3 => 365.0 / frequency,
        _ => 360.0 / frequency,
    }
}

pub fn coupdaybs(settlement: f64, maturity: f64, frequency: f64, basis: f64) -> f64 {
    let pcd = coupon_pcd(settlement, maturity, frequency);
    basis_days_between(pcd, settlement, basis)
}

/// Days from settlement to the next coupon date. Confirmed against real
/// Excel via the differential fuzzer that this is *not* simply
/// `coupdays - coupdaybs` for basis 0/2/3/4: `COUPDAYS` reports an
/// idealized period length (360/freq or 365/freq) that generally doesn't
/// equal the period's actual calendar length, while `COUPDAYSNC` (like
/// `COUPDAYBS`) uses the same real day-count convention applied directly
/// to the settlement -> next-coupon span.
pub fn coupdaysnc(settlement: f64, maturity: f64, frequency: f64, basis: f64) -> f64 {
    let ncd = coupon_ncd(settlement, maturity, frequency);
    // The span *ends* at a coupon date, so on basis 0 a month-end coupon
    // gets pulled to the 30th -- the same rule ODDLPRICE's coupon-ended
    // spans use. Settlement 2011-08-28 against a 2013-02-28 maturity has
    // its next coupon on 2011-08-31, and real Excel counts 2 days, not the
    // 3 the plain NASD rule gives. COUPDAYBS is unaffected: its span ends
    // at the settlement date, not at a coupon.
    coupon_end_days(settlement, ncd, basis)
}

/// Shared by `PRICE`/`YIELD`: present value (per 100 face) of a regular
/// bond's remaining cashflows at a given yield. Excel switches to simple
/// (linear) discounting once fewer than one coupon period remains (`n<=1`)
/// rather than compounding fractional-period discount factors.
fn bond_price_from_yield(
    settlement: f64,
    maturity: f64,
    rate: f64,
    yld: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    let n = coupnum(settlement, maturity, frequency);
    let e = coupdays(settlement, maturity, frequency, basis);
    let a = coupdaybs(settlement, maturity, frequency, basis);
    // The exponent here is E-consistent (E - A), not the real-calendar
    // COUPDAYSNC() -- confirmed against real Excel via the differential
    // fuzzer: PRICE/YIELD/DURATION stay internally self-consistent with
    // COUPDAYS's idealized period length even on bases (1/2/3) where the
    // standalone COUPDAYSNC() function reports actual calendar days that
    // don't sum to that same idealized E.
    let dsc = e - a;
    let coupon = 100.0 * rate / frequency;

    if n <= 1.0 {
        (redemption + coupon) / (1.0 + (dsc / e) * (yld / frequency)) - coupon * (a / e)
    } else {
        let mut sum = redemption / (1.0 + yld / frequency).powf(n - 1.0 + dsc / e);
        let mut k = 1.0;
        while k <= n {
            sum += coupon / (1.0 + yld / frequency).powf(k - 1.0 + dsc / e);
            k += 1.0;
        }
        sum - coupon * (a / e)
    }
}

pub fn price(
    settlement: f64,
    maturity: f64,
    rate: f64,
    yld: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    bond_price_from_yield(
        settlement, maturity, rate, yld, redemption, frequency, basis,
    )
}

pub fn yield_(
    settlement: f64,
    maturity: f64,
    rate: f64,
    pr: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> Option<f64> {
    let f = |y: f64| {
        bond_price_from_yield(settlement, maturity, rate, y, redemption, frequency, basis) - pr
    };
    bisection(f, -0.99, 10.0)
}

/// Generic bisection root-finder used by the yield-solving bond functions,
/// which (unlike `RATE`/`IRR`) are monotonic in the unknown but don't have
/// a cheap closed-form derivative worth hand-deriving.
fn bisection(f: impl Fn(f64) -> f64, mut lo: f64, mut hi: f64) -> Option<f64> {
    let mut f_lo = f(lo);
    let f_hi = f(hi);
    if !f_lo.is_finite() || !f_hi.is_finite() {
        return None;
    }
    if f_lo == 0.0 {
        return Some(lo);
    }
    if f_lo.signum() == f_hi.signum() {
        return None;
    }
    for _ in 0..200 {
        let mid = (lo + hi) / 2.0;
        let f_mid = f(mid);
        if !f_mid.is_finite() {
            return None;
        }
        if f_mid.abs() < 1e-10 || (hi - lo).abs() < 1e-12 {
            return Some(mid);
        }
        if f_mid.signum() == f_lo.signum() {
            lo = mid;
            f_lo = f_mid;
        } else {
            hi = mid;
        }
    }
    Some((lo + hi) / 2.0)
}

pub fn duration(
    settlement: f64,
    maturity: f64,
    coupon: f64,
    yld: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    let n = coupnum(settlement, maturity, frequency).round() as i64;
    let e = coupdays(settlement, maturity, frequency, basis);
    let a = coupdaybs(settlement, maturity, frequency, basis);
    // See bond_price_from_yield: uses the E-consistent (E - A) fraction,
    // not the real-calendar COUPDAYSNC().
    let dsc = e - a;
    let coupon_amt = 100.0 * coupon / frequency;

    let mut weighted_sum = 0.0;
    let mut price_sum = 0.0;
    for k in 1..=n.max(1) {
        let t = (k - 1) as f64 + dsc / e;
        let cf = if k == n {
            coupon_amt + 100.0
        } else {
            coupon_amt
        };
        let pv = cf / (1.0 + yld / frequency).powf(t);
        weighted_sum += t * pv;
        price_sum += pv;
    }
    weighted_sum / (price_sum * frequency)
}

pub fn mduration(
    settlement: f64,
    maturity: f64,
    coupon: f64,
    yld: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    duration(settlement, maturity, coupon, yld, frequency, basis) / (1.0 + yld / frequency)
}

pub fn disc(settlement: f64, maturity: f64, pr: f64, redemption: f64, basis: f64) -> f64 {
    let dsm = basis_days_between(settlement, maturity, basis);
    let year = basis_year_days(basis, settlement, maturity);
    (redemption - pr) / redemption * (year / dsm)
}

pub fn pricedisc(
    settlement: f64,
    maturity: f64,
    discount: f64,
    redemption: f64,
    basis: f64,
) -> f64 {
    let dsm = basis_days_between(settlement, maturity, basis);
    let year = basis_year_days(basis, settlement, maturity);
    redemption * (1.0 - discount * dsm / year)
}

pub fn yielddisc(settlement: f64, maturity: f64, pr: f64, redemption: f64, basis: f64) -> f64 {
    let dsm = basis_days_between(settlement, maturity, basis);
    let year = basis_year_days(basis, settlement, maturity);
    (redemption - pr) / pr * (year / dsm)
}

pub fn pricemat(
    settlement: f64,
    maturity: f64,
    issue: f64,
    rate: f64,
    yld: f64,
    basis: f64,
) -> f64 {
    let dim = basis_days_between_pricemat_leg(issue, maturity, basis, false, false);
    let a = basis_days_between_pricemat_leg(issue, settlement, basis, false, true);
    let dsm = basis_days_between_pricemat_leg(settlement, maturity, basis, true, false);
    // Year length uses the (issue, settlement) span, not the full
    // (often multi-year) issue-to-maturity DIM span -- confirmed against
    // real Excel via the differential fuzzer.
    let year = basis_year_days(basis, issue, settlement);

    let num = 100.0 + (dim / year) * rate * 100.0;
    num / (1.0 + (dsm / year) * yld) - (a / year) * rate * 100.0
}

pub fn yieldmat(settlement: f64, maturity: f64, issue: f64, rate: f64, pr: f64, basis: f64) -> f64 {
    let dim = basis_days_between_pricemat_leg(issue, maturity, basis, false, false);
    let a = basis_days_between_pricemat_leg(issue, settlement, basis, false, true);
    let dsm = basis_days_between_pricemat_leg(settlement, maturity, basis, true, false);
    // Year length uses the (issue, settlement) span, not the full
    // (often multi-year) issue-to-maturity DIM span -- confirmed against
    // real Excel via the differential fuzzer.
    let year = basis_year_days(basis, issue, settlement);

    let numerator = 100.0 + (dim / year) * rate * 100.0;
    let denominator = pr + (a / year) * rate * 100.0;
    (numerator / denominator - 1.0) * (year / dsm)
}

pub fn received(settlement: f64, maturity: f64, investment: f64, discount: f64, basis: f64) -> f64 {
    let dsm = basis_days_between(settlement, maturity, basis);
    let year = basis_year_days(basis, settlement, maturity);
    investment / (1.0 - discount * dsm / year)
}

pub fn intrate(
    settlement: f64,
    maturity: f64,
    investment: f64,
    redemption: f64,
    basis: f64,
) -> f64 {
    let dsm = basis_days_between(settlement, maturity, basis);
    let year = basis_year_days(basis, settlement, maturity);
    (redemption - investment) / investment * (year / dsm)
}

pub fn tbillprice(settlement: f64, maturity: f64, discount: f64) -> f64 {
    let dsm = maturity - settlement;
    100.0 * (1.0 - discount * dsm / 360.0)
}

pub fn tbillyield(settlement: f64, maturity: f64, pr: f64) -> f64 {
    let dsm = maturity - settlement;
    (100.0 - pr) / pr * (360.0 / dsm)
}

/// Bond-equivalent yield of a Treasury bill. The `dsm <= 182` branch is the
/// exact documented formula; the longer-maturity branch uses the standard
/// quadratic reconstruction (see e.g. LibreOffice's `GetTBillEq`) with a
/// fixed 365-day year rather than special-casing the rare leap-February
/// crossing, since real T-bills are issued for at most a year.
pub fn tbilleq(settlement: f64, maturity: f64, discount: f64) -> Option<f64> {
    let dsm = maturity - settlement;
    if dsm <= 182.0 {
        Some((365.0 * discount) / (360.0 - discount * dsm))
    } else {
        let term1 = dsm / 365.0;
        let term2 = term1.powi(2) - (2.0 * term1 - 1.0) * (discount * dsm) / 360.0;
        if term2 < 0.0 {
            return None;
        }
        let term3 = -term1 - term2.sqrt();
        Some((2.0 * term3) / (term1 - 2.0))
    }
}

pub fn accrintm(
    issue: f64,
    settlement: f64,
    rate: f64,
    par: f64,
    basis: f64,
) -> Result<f64, String> {
    let frac = date_fn::yearfrac(issue, settlement, Some(basis))?;
    Ok(par * rate * frac)
}

/// Builds the ascending quasi-coupon-date schedule spanning `[lo, hi]`,
/// anchored at `anchor` (typically `first_interest`) and stepping in
/// `12/frequency`-month increments -- shared by `ACCRINT`'s period-by-period
/// accrual walk.
fn quasi_coupon_schedule(anchor: f64, lo: f64, hi: f64, frequency: f64) -> Vec<f64> {
    let months = 12.0 / frequency;
    let mut dates = vec![anchor];
    let mut d = anchor;
    let mut k = 0.0;
    let mut guard = 0;
    while d > lo && guard < 10_000 {
        k += 1.0;
        d = step_months(anchor, -months, k);
        dates.push(d);
        guard += 1;
    }
    let mut d = anchor;
    let mut k = 0.0;
    guard = 0;
    while d < hi && guard < 10_000 {
        k += 1.0;
        d = step_months(anchor, months, k);
        dates.push(d);
        guard += 1;
    }
    dates.sort_by(|a, b| a.partial_cmp(b).unwrap());
    dates.dedup();
    dates
}

/// `calc_method` is accepted for signature compatibility but, per
/// Microsoft's docs, only theoretically distinguishes "accrue from issue"
/// (`TRUE`) from "accrue from the last coupon date" (`FALSE`). Confirmed
/// against real Excel via the differential fuzzer across regular,
/// odd-first-period, and multi-period cases that both values always
/// produce the same total-accrued-from-issue result in practice, so this
/// doesn't branch on it.
///
/// The per-period day-count denominator (`e`, below) is confirmed exact
/// against real Excel only for basis 0 and 4 (30/360): those two always
/// give a full elapsed period a numerator equal to its own denominator, so
/// every complete period contributes exactly one coupon regardless of
/// calendar length. Bases 1/2/3 do *not* reduce to a simple per-period or
/// basis-year-average formula the way `PRICE`/`DURATION`/the `ODD*`
/// functions do -- real Excel's exact undocumented rule for them wasn't
/// reverse-engineerable from fuzzing within reasonable effort, so the fuzz
/// generator restricts `ACCRINT` to basis 0/4 and this stays a documented,
/// unverified best-effort for 1/2/3.
#[allow(clippy::too_many_arguments)]
pub fn accrint(
    issue: f64,
    first_interest: f64,
    settlement: f64,
    rate: f64,
    par: f64,
    frequency: f64,
    basis: f64,
    _calc_method: bool,
) -> f64 {
    let schedule = quasi_coupon_schedule(
        first_interest,
        issue,
        settlement.max(first_interest),
        frequency,
    );
    let coupon_amt = par * rate / frequency;

    let mut total = 0.0;
    for w in schedule.windows(2) {
        let (p_start, p_end) = (w[0], w[1]);
        let seg_start = p_start.max(issue);
        let seg_end = p_end.min(settlement);
        if seg_end <= seg_start {
            continue;
        }
        let a = basis_days_between(seg_start, seg_end, basis);
        let e = match basis as i64 {
            1 => p_end - p_start,
            3 => 365.0 / frequency,
            _ => 360.0 / frequency,
        };
        total += coupon_amt * a / e;
    }
    total
}

pub fn amorlinc(
    cost: f64,
    date_purchased: f64,
    first_period: f64,
    salvage: f64,
    period: f64,
    rate: f64,
    basis: f64,
) -> Result<f64, String> {
    // Confirmed against real Excel: AMORLINC/AMORDEGRC reject basis 2
    // (actual/360) with #NUM!, unlike every other function in this file
    // that accepts it.
    if basis as i64 == 2 {
        return Err("#NUM!".to_string());
    }
    let one_rate = cost * rate;
    let cost_delta = cost - salvage;
    let frac = amort_first_period_frac(date_purchased, first_period, basis);
    let first_period_amort = rate * cost * frac;

    if period == 0.0 {
        return Ok(first_period_amort.min(cost_delta));
    }

    let n_periods = ((cost_delta - first_period_amort) / one_rate).trunc();
    if period <= n_periods {
        Ok(one_rate)
    } else if period == n_periods + 1.0 {
        Ok(cost_delta - one_rate * n_periods - first_period_amort)
    } else {
        Ok(0.0)
    }
}

/// Confirmed against real Excel via the differential fuzzer for life >= 4
/// (the coefficient-table brackets, including the final-period taper to
/// zero once the remaining balance drops below salvage). Life <= 2 is
/// rejected with #NUM!. Life in (2, 4) is a known gap: real Excel switches
/// to straight-line much earlier there than this declining-balance
/// implementation does (e.g. life 2.5 gives identical straight-line
/// amounts for every remaining period starting immediately after the
/// prorated first one, not a declining amount), and the exact switch
/// condition wasn't pinned down within the fuzzer's reach -- the fuzz
/// generator keeps life >= 4 to avoid this gap.
pub fn amordegrc(
    cost: f64,
    date_purchased: f64,
    first_period: f64,
    salvage: f64,
    period: f64,
    rate: f64,
    basis: f64,
) -> Result<f64, String> {
    if basis as i64 == 2 {
        return Err("#NUM!".to_string());
    }
    let life = 1.0 / rate;
    // Confirmed against real Excel: a life of 2 years or less (rate >=
    // 0.5) is rejected outright with #NUM!. There is no separate
    // "life < 3 => 1.0" bracket -- the whole (2, 5) range uses 1.5 (an
    // earlier assumption of a 1.0 bracket there was off by exactly the
    // 1.5 factor once checked against real Excel).
    if life <= 2.0 {
        return Err("#NUM!".to_string());
    }
    let coeff = if life < 5.0 {
        1.5
    } else if life < 6.0 {
        2.0
    } else {
        2.5
    };
    let rate_d = rate * coeff;
    let frac = amort_first_period_frac(date_purchased, first_period, basis);

    // The running balance carries *full* precision; only the value actually
    // returned is rounded. Rounding each period and subtracting the rounded
    // figure lets the error compound, which is enough to shift a later
    // period by a whole unit: for cost 27370.88 at rate 0.0909 the period-2
    // amount is 4624.4757 (Excel: 4624) carrying full precision, but
    // 4624.508 -> 4625 if the two preceding periods were rounded first.
    let first_amort = cost * frac * rate_d;
    if period == 0.0 {
        return Ok(round_half_away_from_zero(first_amort.min(cost - salvage)));
    }

    let mut remaining = cost - first_amort;
    let mut n = 1.0;
    loop {
        if remaining <= salvage {
            return Ok(0.0);
        }
        let this_amort = remaining * rate_d;
        if n as i64 == period as i64 {
            if remaining - this_amort < salvage {
                return Ok(round_half_away_from_zero((remaining - salvage).max(0.0)));
            }
            return Ok(round_half_away_from_zero(this_amort));
        }
        remaining -= this_amort;
        n += 1.0;
        if n > 10_000.0 {
            return Ok(0.0);
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn oddfprice(
    settlement: f64,
    maturity: f64,
    issue: f64,
    first_coupon: f64,
    rate: f64,
    yld: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    oddfprice_from_yield(
        settlement,
        maturity,
        issue,
        first_coupon,
        rate,
        yld,
        redemption,
        frequency,
        basis,
    )
}

#[allow(clippy::too_many_arguments)]
fn oddfprice_from_yield(
    settlement: f64,
    maturity: f64,
    issue: f64,
    first_coupon: f64,
    rate: f64,
    yld: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    let months = 12.0 / frequency;
    // Total number of coupons from first_coupon through maturity, inclusive.
    let mut n = 1.0;
    let mut d = first_coupon;
    let mut guard = 0;
    while d < maturity - 1e-9 && guard < 10_000 {
        d = step_months(first_coupon, months, n);
        n += 1.0;
        guard += 1;
    }

    // E, the length of one normal coupon period. Confirmed against real
    // Excel via the differential fuzzer that ODDFPRICE/ODDFYIELD use the
    // same idealized value as regular COUPDAYS/PRICE (fixed 360/365-per-
    // freq on every basis except 1) -- unlike ODDLPRICE/ODDLYIELD below,
    // which need the period's *actual* calendar length on bases 0/2/4.
    let prev_coupon = date_fn::edate(first_coupon, -months).unwrap_or(first_coupon);
    let e = match basis as i64 {
        1 => basis_days_between(prev_coupon, first_coupon, basis),
        3 => 365.0 / frequency,
        _ => 360.0 / frequency,
    };
    let dsc = basis_days_between(settlement, first_coupon, basis);

    // DFC (issue -> first_coupon) and A (issue -> settlement) summed
    // piecewise across quasi-coupon periods, which stays correct whether
    // the odd first period is shorter or longer than a normal period.
    let schedule =
        quasi_coupon_schedule(first_coupon, issue, settlement.max(first_coupon), frequency);
    let mut dfc = 0.0;
    let mut a = 0.0;
    for w in schedule.windows(2) {
        let (p_start, p_end) = (w[0], w[1]);
        let seg = |lo: f64, hi: f64| -> f64 {
            let s = p_start.max(lo);
            let e = p_end.min(hi);
            if e > s {
                basis_days_between(s, e, basis)
            } else {
                0.0
            }
        };
        dfc += seg(issue, first_coupon);
        a += seg(issue, settlement);
    }

    let coupon = 100.0 * rate / frequency;
    let term1 = redemption / (1.0 + yld / frequency).powf(n - 1.0 + dsc / e);
    let term2 = coupon * (dfc / e) / (1.0 + yld / frequency).powf(dsc / e);
    let mut term3 = 0.0;
    let mut k = 2.0;
    while k <= n {
        term3 += coupon / (1.0 + yld / frequency).powf(k - 1.0 + dsc / e);
        k += 1.0;
    }
    term1 + term2 + term3 - coupon * (a / e)
}

#[allow(clippy::too_many_arguments)]
pub fn oddfyield(
    settlement: f64,
    maturity: f64,
    issue: f64,
    first_coupon: f64,
    rate: f64,
    pr: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> Option<f64> {
    let f = |y: f64| {
        oddfprice_from_yield(
            settlement,
            maturity,
            issue,
            first_coupon,
            rate,
            y,
            redemption,
            frequency,
            basis,
        ) - pr
    };
    bisection(f, -0.99, 10.0)
}

/// E, the length of the regular coupon period `ODDLPRICE`/`ODDLYIELD`
/// treat the odd last period as a fraction of. Confirmed against real
/// Excel via the differential fuzzer, across bases 0-4 and multiple
/// frequencies, to be the *actual* (or 30/360, per basis) length of the
/// regular period immediately *following* `last_interest` -- not the
/// period immediately preceding `maturity`, which an earlier version used
/// and which only coincidentally matched when both periods happened to
/// have the same calendar length.
/// Day count for an ODDLPRICE/ODDLYIELD span whose end date is a **coupon
/// date** rather than the settlement date. On basis 0 those spans pull a
/// month-end end date back to the 30th; every other basis just uses its
/// ordinary count. See `date_fn::days_30_360_coupon_end`.
fn coupon_end_days(start: f64, end: f64, basis: f64) -> f64 {
    if basis as i64 == 0 {
        date_fn::days_30_360_coupon_end(start, end)
    } else {
        basis_days_between(start, end, basis)
    }
}

fn oddlprice_e(last_interest: f64, _maturity: f64, frequency: f64, basis: f64) -> f64 {
    let months = 12.0 / frequency;
    let next_regular = date_fn::edate(last_interest, months).unwrap_or(last_interest);
    coupon_end_days(last_interest, next_regular, basis)
}

/// Like `ODDFPRICE`/`ODDFYIELD`, this is a documented gap for a "long" odd
/// period (here: last_interest to maturity spanning more than one regular
/// coupon period) -- real Excel's exact handling wasn't reverse-
/// engineered within the fuzzer's reach, so the fuzz generator keeps the
/// odd period shorter than one regular period.
#[allow(clippy::too_many_arguments)]
pub fn oddlprice(
    settlement: f64,
    maturity: f64,
    last_interest: f64,
    rate: f64,
    yld: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    let e = oddlprice_e(last_interest, maturity, frequency, basis);
    let dcnl = coupon_end_days(last_interest, maturity, basis);
    let dcsl = basis_days_between(last_interest, settlement, basis);
    let dsc = basis_days_between(settlement, maturity, basis);
    let coupon = 100.0 * rate / frequency;

    let numerator = redemption + coupon * (dcnl / e);
    numerator / (1.0 + (dsc / e) * (yld / frequency)) - coupon * (dcsl / e)
}

#[allow(clippy::too_many_arguments)]
pub fn oddlyield(
    settlement: f64,
    maturity: f64,
    last_interest: f64,
    rate: f64,
    pr: f64,
    redemption: f64,
    frequency: f64,
    basis: f64,
) -> f64 {
    let e = oddlprice_e(last_interest, maturity, frequency, basis);
    let dcnl = coupon_end_days(last_interest, maturity, basis);
    let dcsl = basis_days_between(last_interest, settlement, basis);
    let dsc = basis_days_between(settlement, maturity, basis);
    let coupon = 100.0 * rate / frequency;

    let numerator = redemption + coupon * (dcnl / e);
    let denominator = pr + coupon * (dcsl / e);
    (numerator / denominator - 1.0) * (frequency * e / dsc)
}

/// Fixed euro-conversion rate (1 EUR = N units of `code`), permanently
/// fixed by EU regulation on each currency's euro-adoption date -- these
/// are legal constants, not derived values that could drift.
///
/// NOTE: unlike every other function in this file, this couldn't be
/// validated against real Excel via the differential fuzzer -- `EUROCONVERT`
/// requires the "Euro Currency Tools" add-in, which isn't loaded in this
/// environment's Excel installation (confirmed: it returns `#NAME?` here
/// regardless of arguments). The rates and rounding rule below follow
/// Microsoft's published documentation and are tested against Microsoft's
/// own documented examples instead.
fn euro_rate(code: &str) -> Option<f64> {
    match code.to_uppercase().as_str() {
        "EUR" => Some(1.0),
        "ATS" => Some(13.7603),
        "BEF" | "LUF" => Some(40.3399),
        "DEM" => Some(1.95583),
        "ESP" => Some(166.386),
        "FIM" => Some(5.94573),
        "FRF" => Some(6.55957),
        "IEP" => Some(0.787564),
        "ITL" => Some(1936.27),
        "NLG" => Some(2.20371),
        "PTE" => Some(200.482),
        "GRD" => Some(340.750),
        "SIT" => Some(239.640),
        "CYP" => Some(0.585274),
        "MTL" => Some(0.429300),
        "SKK" => Some(30.1260),
        "EEK" => Some(15.6466),
        "LVL" => Some(0.702804),
        "LTL" => Some(3.45280),
        _ => None,
    }
}

fn euro_round_half_away(x: f64, decimals: i32) -> f64 {
    let factor = 10f64.powi(decimals);
    round_half_away_from_zero(x * factor) / factor
}

pub fn euroconvert(
    number: f64,
    source: &str,
    target: &str,
    full_precision: bool,
    triangulation_precision: Option<f64>,
) -> Result<f64, String> {
    let source_rate = euro_rate(source).ok_or("#VALUE!".to_string())?;
    let target_rate = euro_rate(target).ok_or("#VALUE!".to_string())?;

    // ITL/ESP/BEF/LUF had no meaningful subunit in everyday use, so
    // EUROCONVERT rounds conversions into those currencies to whole units.
    let decimals_for = |code: &str| -> i32 {
        match code.to_uppercase().as_str() {
            "ITL" | "ESP" | "BEF" | "LUF" => 0,
            _ => 2,
        }
    };

    let result = if source.eq_ignore_ascii_case(target) {
        number
    } else if source.eq_ignore_ascii_case("EUR") {
        number * target_rate
    } else if target.eq_ignore_ascii_case("EUR") {
        number / source_rate
    } else {
        let mut in_eur = number / source_rate;
        if let Some(tp) = triangulation_precision {
            if tp < 3.0 {
                return Err("#NUM!".to_string());
            }
            in_eur = euro_round_half_away(in_eur, tp as i32);
        }
        in_eur * target_rate
    };

    if full_precision {
        Ok(result)
    } else {
        Ok(euro_round_half_away(result, decimals_for(target)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Microsoft's documented examples are themselves rounded to 2 decimal
    // places, so allow a cent of slack rather than demanding exact matches.
    fn approx(a: f64, b: f64) {
        assert!((a - b).abs() < 5e-3, "{a} != {b}");
    }

    #[test]
    fn test_fuzz_pricemat_settlement_never_gets_feb_eom_bump() {
        // Harvested from fuzz/fuzz_excel.py, two seeds that pin down the
        // same underlying rule from opposite sides (see
        // `date_fn::days_30_360_bond_ex`'s doc comment):
        //
        // Seed 740495: issue 2015-02-28, settlement 2015-04-28 (not a
        // February month-end), maturity 2017-02-28. Requires maturity's
        // Feb-month-end bump to apply *independently* of issue/settlement
        // (unlike `days_30_360_nasd`, YEARFRAC's basis-0 rule, which needs
        // both ends to be February month-ends before bumping).
        //
        // Seed 147209: issue 2033-09-28, settlement 2034-02-28 (itself a
        // February month-end this time), maturity 2036-09-28. If
        // settlement got the same independent bump issue/maturity get,
        // this would be off by 2 days each on the issue-to-settlement and
        // settlement-to-maturity legs -- real Excel's PRICEMAT never
        // bumps a settlement date this way, only issue and maturity.
        approx(
            pricemat(
                date_fn::edate(date_fn::ymd_to_serial(2015, 2, 28), 2.0).unwrap(),
                date_fn::edate(date_fn::ymd_to_serial(2015, 2, 28), 24.0).unwrap(),
                date_fn::ymd_to_serial(2015, 2, 28),
                0.0812,
                0.099,
                0.0,
            ),
            97.02941154961482,
        );
        approx(
            pricemat(
                date_fn::edate(date_fn::ymd_to_serial(2033, 9, 28), 5.0).unwrap(),
                date_fn::edate(date_fn::ymd_to_serial(2033, 9, 28), 36.0).unwrap(),
                date_fn::ymd_to_serial(2033, 9, 28),
                0.0239,
                0.0138,
                0.0,
            ),
            102.48507237805565,
        );
    }

    #[test]
    fn test_pmt_matches_docs_example() {
        // PMT(8%/12, 10, 10000) = -1037.03
        approx(pmt(0.08 / 12.0, 10.0, 10000.0, 0.0, 0.0), -1037.03);
    }

    #[test]
    fn test_fv_matches_docs_example() {
        // FV(6%/12, 10, -200, -500, 1) = 2581.40
        approx(fv(0.06 / 12.0, 10.0, -200.0, -500.0, 1.0), 2581.40);
    }

    #[test]
    fn test_pv_matches_docs_example() {
        // PV(8%/12, 20*12, 500, 0, 0) = -59777.15
        approx(pv(0.08 / 12.0, 240.0, 500.0, 0.0, 0.0), -59777.15);
    }

    #[test]
    fn test_nper_is_inverse_of_fv() {
        // nper must invert fv/pv/pmt (all three already checked against
        // Microsoft's documented examples above) for both payment timings.
        for pmt_type in [0.0, 1.0] {
            let rate = 0.01;
            let pmt_amt = -100.0;
            let pv_amt = -1000.0;
            let n = 24.0;
            let fv_amt = fv(rate, n, pmt_amt, pv_amt, pmt_type);
            approx(nper(rate, pmt_amt, pv_amt, fv_amt, pmt_type).unwrap(), n);
        }
    }

    #[test]
    fn test_rate_matches_docs_example() {
        // RATE(4*12, -200, 8000) = 0.007701 (monthly)
        approx(rate(48.0, -200.0, 8000.0, 0.0, 0.0, 0.1).unwrap(), 0.007701);
    }

    #[test]
    fn test_ipmt_ppmt_sum_to_pmt() {
        // ipmt+ppmt==pmt is a tautology (ppmt is *defined* as pmt-ipmt), so
        // this only proves internal consistency, not that ipmt itself is
        // correct -- see the fuzzer-harvested cases below for that.
        let rate = 0.10 / 12.0;
        let nper = 36.0;
        let pv = 8000000.0;
        let full_pmt = pmt(rate, nper, pv, 0.0, 0.0);
        for per in 1..=36 {
            let per = per as f64;
            approx(
                ipmt(rate, per, nper, pv, 0.0, 0.0) + ppmt(rate, per, nper, pv, 0.0, 0.0),
                full_pmt,
            );
        }
    }

    // The cases below were minimized from real mismatches the Python
    // differential fuzzer (fuzz/fuzz_excel.py) found against actual
    // Microsoft Excel -- ipmt's type=1 (annuity-due) branch used the wrong
    // formula entirely, and both ipmt's closed form and ispmt's formula
    // were verified/derived against these real Excel values rather than a
    // (mis-)remembered documentation example.
    #[test]
    fn test_ispmt_matches_fuzzer_verified_excel_values() {
        approx(ispmt(0.0749, 111.0, 153.0, 96163.49), -1977.1967767450978);
        approx(ispmt(0.0303, 17.0, 38.0, 43643.33), -730.796075763158);
        approx(ispmt(0.0497, 163.0, 193.0, 96345.37), -744.3054231606217);
    }

    #[test]
    fn test_ipmt_type1_matches_fuzzer_verified_excel_values() {
        approx(
            ipmt(0.0085, 32.0, 150.0, 49910.41, 0.0, 1.0),
            -371.35148593978215,
        );
    }

    #[test]
    fn test_ppmt_matches_fuzzer_verified_excel_values() {
        approx(
            ppmt(0.1715, 114.0, 125.0, 19772.69, 0.0, 1.0),
            -433.191648049476,
        );
        approx(
            ppmt(0.0752, 77.0, 178.0, 85932.73, 0.0, 1.0),
            -3.6896416644154724,
        );
        approx(
            ppmt(0.0288, 195.0, 284.0, 51703.71, 0.0, 1.0),
            -112.4433992494153,
        );
        approx(
            ppmt(0.0872, 18.0, 98.0, 93845.1, 0.0, 1.0),
            -8.62330593780311,
        );

        // At ~10%-per-period compounded over 285 periods, f64 rounding
        // error in the forward recurrence gets amplified by (1+rate) at
        // every subsequent step; verified against arbitrary-precision
        // decimal arithmetic that the *algorithm* is exact (agrees with
        // Excel to 13 significant digits) and that Kahan-compensated
        // summation does not meaningfully close the gap, so this is an
        // accepted f64 precision limit at economically-unrealistic inputs
        // (a real per-period rate is rarely above a percent or two) rather
        // than a formula bug. The fuzzer's rate range was narrowed to
        // avoid regenerating this regime (see fuzz/fuzz_excel.py).
        let got = ppmt(0.0998, 248.0, 285.0, 67611.52, 0.0, 0.0);
        let want = -181.64776461127138;
        assert!((got - want).abs() < 0.25, "{got} != {want}");
    }

    #[test]
    fn test_npv_matches_docs_example() {
        // NPV(10%, -10000, 3000, 4200, 6800) = 1188.44
        approx(npv(0.10, &[-10000.0, 3000.0, 4200.0, 6800.0]), 1188.44);
    }

    #[test]
    fn test_irr_matches_docs_example() {
        // IRR({-70000,12000,15000,18000,21000,26000}) = 8.66%
        approx(
            irr(
                &[-70000.0, 12000.0, 15000.0, 18000.0, 21000.0, 26000.0],
                0.1,
            )
            .unwrap(),
            0.0866,
        );
    }

    #[test]
    fn test_irr_recovers_via_zero_guess_fallback() {
        // guess=0.1 (Excel's own default) fails to converge here; Excel
        // itself still finds -0.19995872986748842, and so does a guess=0.0
        // retry -- the fuzzer-verified case behind
        // newton_raphson_with_zero_fallback.
        let cash = [-20633.16, 7717.06, -18760.88, -8911.01, 3391.3, 16198.77];
        approx(irr(&cash, 0.1).unwrap(), -0.19995872986748842);
    }

    #[test]
    fn test_mirr_all_negative_cashflows_is_minus_one_not_num_error() {
        // Real Excel's MIRR({-5787.88,-814.95,-8609.23,-601.21,-12290.6,-16118.7}, ...)
        // returns -1 (total loss), not #NUM! -- the differential fuzzer
        // found visi returning #NUM! here from an overly defensive early
        // return that shortcut past 0^(1/n) being a perfectly valid 0.
        let cash = [-5787.88, -814.95, -8609.23, -601.21, -12290.6, -16118.7];
        approx(mirr(&cash, 0.0087, 0.0188).unwrap(), -1.0);
    }

    #[test]
    fn test_sln_matches_docs_example() {
        // SLN(30000, 7500, 10) = 2250
        approx(sln(30000.0, 7500.0, 10.0), 2250.0);
    }

    #[test]
    fn test_syd_matches_docs_example() {
        // SYD(30000, 7500, 10, 1) = 4090.91
        approx(syd(30000.0, 7500.0, 10.0, 1.0), 4090.91);
    }

    #[test]
    fn test_ddb_matches_docs_example() {
        // DDB(2400, 300, 10, 1) = 480
        approx(ddb(2400.0, 300.0, 10.0, 1.0, 2.0), 480.0);
        // DDB(2400, 300, 10, 2) = 384
        approx(ddb(2400.0, 300.0, 10.0, 2.0, 2.0), 384.0);
    }

    #[test]
    fn test_effect_nominal_are_inverses() {
        let e = effect(0.0525, 4.0);
        approx(nominal(e, 4.0), 0.0525);
    }

    #[test]
    fn test_dollarde_matches_docs_example() {
        // DOLLARDE(1.02, 16) = 1.125
        approx(dollarde(1.02, 16.0).unwrap(), 1.125);
    }

    #[test]
    fn test_dollarfr_matches_docs_example() {
        // DOLLARFR(1.125, 16) = 1.02
        approx(dollarfr(1.125, 16.0).unwrap(), 1.02);
    }

    #[test]
    fn test_fvschedule_matches_docs_example() {
        // FVSCHEDULE(1, {0.09, 0.11, 0.1}) = 1.33089
        approx(fvschedule(1.0, &[0.09, 0.11, 0.1]), 1.33089);
    }
}