Skip to content

Commit 4912c7c

Browse files
committed
clang-tidy and test cleanup
1 parent 05e7483 commit 4912c7c

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

tests/tetests.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,7 +4208,7 @@ TEST_CASE("PPMT", "[finance]")
42084208
WITHIN_TYPE_CAST(tep.evaluate("IPMT(0.05/12, 1, 60, 10000)")),
42094209
Catch::Matchers::WithinRel(
42104210
WITHIN_TYPE_CAST(-41.6666666667),
4211-
WITHIN_TYPE_CAST(0.00001)));
4211+
WITHIN_TYPE_CAST(0.0001)));
42124212

42134213
// Excel: Same loan, second payment interest
42144214
// Formula: =IPMT(0.05/12, 2, 60, 10000)
@@ -4217,7 +4217,7 @@ TEST_CASE("PPMT", "[finance]")
42174217
WITHIN_TYPE_CAST(tep.evaluate("IPMT(0.05/12, 2, 60, 10000)")),
42184218
Catch::Matchers::WithinRel(
42194219
WITHIN_TYPE_CAST(-41.053976375944),
4220-
WITHIN_TYPE_CAST(0.00001)));
4220+
WITHIN_TYPE_CAST(0.0001)));
42214221

42224222
// Excel: Beginning-of-period payments (type = 1)
42234223
// First payment has no interest
@@ -4227,7 +4227,7 @@ TEST_CASE("PPMT", "[finance]")
42274227
WITHIN_TYPE_CAST(tep.evaluate("IPMT(0.05/12, 1, 60, 10000, 0, 1)")),
42284228
Catch::Matchers::WithinRel(
42294229
WITHIN_TYPE_CAST(0.0),
4230-
WITHIN_TYPE_CAST(0.000001)));
4230+
WITHIN_TYPE_CAST(0.0001)));
42314231

42324232
// Excel: Beginning-of-period payments, second payment
42334233
// Formula: =IPMT(0.05/12, 2, 60, 10000, 0, 1)
@@ -4236,7 +4236,7 @@ TEST_CASE("PPMT", "[finance]")
42364236
WITHIN_TYPE_CAST(tep.evaluate("IPMT(0.05/12, 2, 60, 10000, 0, 1)")),
42374237
Catch::Matchers::WithinRel(
42384238
WITHIN_TYPE_CAST(-40.8836279263),
4239-
WITHIN_TYPE_CAST(0.00001)));
4239+
WITHIN_TYPE_CAST(0.0001)));
42404240

42414241
// LibreOffice / Excel: Extreme negative rate uses linear interest regime
42424242
// Formula: =IPMT(-1.1, 1, 60, 10000)
@@ -4245,7 +4245,7 @@ TEST_CASE("PPMT", "[finance]")
42454245
WITHIN_TYPE_CAST(tep.evaluate("IPMT(-1.1, 1, 60, 10000)")),
42464246
Catch::Matchers::WithinRel(
42474247
WITHIN_TYPE_CAST(11000.0),
4248-
WITHIN_TYPE_CAST(0.00001)));
4248+
WITHIN_TYPE_CAST(0.0001)));
42494249

42504250
// Excel: Zero interest rate
42514251
// Formula: =IPMT(0, 10, 60, 10000)
@@ -4254,7 +4254,7 @@ TEST_CASE("PPMT", "[finance]")
42544254
WITHIN_TYPE_CAST(tep.evaluate("IPMT(0, 10, 60, 10000)")),
42554255
Catch::Matchers::WithinRel(
42564256
WITHIN_TYPE_CAST(0.0),
4257-
WITHIN_TYPE_CAST(0.000001)));
4257+
WITHIN_TYPE_CAST(0.00001)));
42584258

42594259
// Excel Help: Loan of $2,000, 10% annual interest, 2-year term, monthly payments
42604260
// Principal payment for month 1
@@ -4306,7 +4306,7 @@ TEST_CASE("PPMT", "[finance]")
43064306
WITHIN_TYPE_CAST(tep.evaluate("PPMT(0, 5, 10, 1000)")),
43074307
Catch::Matchers::WithinRel(
43084308
WITHIN_TYPE_CAST(-100.0),
4309-
WITHIN_TYPE_CAST(0.00001)));
4309+
WITHIN_TYPE_CAST(0.0001)));
43104310

43114311
// Excel: Beginning-of-period payments
43124312
// Loan: $10,000, 5% annual, 60 months
@@ -4316,7 +4316,7 @@ TEST_CASE("PPMT", "[finance]")
43164316
WITHIN_TYPE_CAST(tep.evaluate("PPMT(0.05/12, 1, 60, 10000, 0, 1)")),
43174317
Catch::Matchers::WithinRel(
43184318
WITHIN_TYPE_CAST(-187.929297699694),
4319-
WITHIN_TYPE_CAST(0.00001)));
4319+
WITHIN_TYPE_CAST(0.0001)));
43204320

43214321
// Excel: Sign symmetry — flipping PV flips PPMT
43224322
// Formula: =PPMT(0.05/12, 10, 60, -10000)
@@ -4325,7 +4325,7 @@ TEST_CASE("PPMT", "[finance]")
43254325
WITHIN_TYPE_CAST(tep.evaluate("PPMT(0.05/12, 10, 60, -10000)")),
43264326
Catch::Matchers::WithinRel(
43274327
WITHIN_TYPE_CAST(152.652685047979),
4328-
WITHIN_TYPE_CAST(0.00001)));
4328+
WITHIN_TYPE_CAST(0.0001)));
43294329

43304330
// Excel: Final period principal payment
43314331
// Formula: =PPMT(0.05/12, 60, 60, 10000)
@@ -4334,7 +4334,7 @@ TEST_CASE("PPMT", "[finance]")
43344334
WITHIN_TYPE_CAST(tep.evaluate("PPMT(0.05/12, 60, 60, 10000)")),
43354335
Catch::Matchers::WithinRel(
43364336
WITHIN_TYPE_CAST(-187.929297699694),
4337-
WITHIN_TYPE_CAST(0.00001)));
4337+
WITHIN_TYPE_CAST(0.0001)));
43384338
}
43394339

43404340
// CUMPRINC
@@ -4354,7 +4354,7 @@ TEST_CASE("CUMPRINC", "[finance]")
43544354
),
43554355
Catch::Matchers::WithinRel(
43564356
WITHIN_TYPE_CAST(-934.107123420898),
4357-
WITHIN_TYPE_CAST(0.00001)));
4357+
WITHIN_TYPE_CAST(0.0001)));
43584358

43594359
// Excel Help example:
43604360
// Same loan, payments at BEGINNING of period
@@ -4365,7 +4365,7 @@ TEST_CASE("CUMPRINC", "[finance]")
43654365
tep.evaluate("CUMPRINC(0.09/12, 30*12, 125000, 13, 24, 1)")),
43664366
Catch::Matchers::WithinRel(
43674367
WITHIN_TYPE_CAST(-927.153472378064),
4368-
WITHIN_TYPE_CAST(0.00001)));
4368+
WITHIN_TYPE_CAST(0.0001)));
43694369

43704370
// Excel: single-period principal equals PPMT
43714371
// Formula: =CUMPRINC(0.05/12, 60, 10000, 1, 1, 0)
@@ -4375,7 +4375,7 @@ TEST_CASE("CUMPRINC", "[finance]")
43754375
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 1, 1, 0)")),
43764376
Catch::Matchers::WithinRel(
43774377
WITHIN_TYPE_CAST(-147.045669773443),
4378-
WITHIN_TYPE_CAST(0.00001)));
4378+
WITHIN_TYPE_CAST(0.0001)));
43794379

43804380
// LibreOffice / Excel: CUMPRINC does NOT support zero interest
43814381
// Formula: =CUMPRINC(0, 10, 1000, 1, 10, 0)
@@ -4416,7 +4416,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44164416
tep.evaluate("CUMPRINC(0.09/12, 30*12, 125000, 13, 24, 0)")),
44174417
Catch::Matchers::WithinRel(
44184418
WITHIN_TYPE_CAST(-934.107123420898),
4419-
WITHIN_TYPE_CAST(0.00001)));
4419+
WITHIN_TYPE_CAST(0.0001)));
44204420

44214421
// LibreOffice / Excel:
44224422
// Same loan, payments at BEGINNING of period
@@ -4427,7 +4427,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44274427
tep.evaluate("CUMPRINC(0.09/12, 30*12, 125000, 13, 24, 1)")),
44284428
Catch::Matchers::WithinRel(
44294429
WITHIN_TYPE_CAST(-927.153472378064),
4430-
WITHIN_TYPE_CAST(0.00001)));
4430+
WITHIN_TYPE_CAST(0.0001)));
44314431

44324432
// LibreOffice / Excel:
44334433
// Single period cumulative principal equals PPMT
@@ -4438,7 +4438,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44384438
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 1, 1, 0)")),
44394439
Catch::Matchers::WithinRel(
44404440
WITHIN_TYPE_CAST(-147.045669773443),
4441-
WITHIN_TYPE_CAST(0.00001)));
4441+
WITHIN_TYPE_CAST(0.0001)));
44424442

44434443
// LibreOffice / Excel:
44444444
// Early loan principal (month 1)
@@ -4449,7 +4449,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44494449
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 1, 1, 0)")),
44504450
Catch::Matchers::WithinRel(
44514451
WITHIN_TYPE_CAST(-147.045669773443),
4452-
WITHIN_TYPE_CAST(0.00001)));
4452+
WITHIN_TYPE_CAST(0.0001)));
44534453

44544454
// LibreOffice / Excel:
44554455
// Late loan principal (month 60)
@@ -4460,7 +4460,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44604460
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 60, 60, 0)")),
44614461
Catch::Matchers::WithinRel(
44624462
WITHIN_TYPE_CAST(-187.929297699694),
4463-
WITHIN_TYPE_CAST(0.00001)));
4463+
WITHIN_TYPE_CAST(0.0001)));
44644464

44654465
// LibreOffice / Excel:
44664466
// Principal paid in first year (months 1–12)
@@ -4471,7 +4471,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44714471
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 1, 12, 0)")),
44724472
Catch::Matchers::WithinRel(
44734473
WITHIN_TYPE_CAST(-1805.55252981598),
4474-
WITHIN_TYPE_CAST(0.00001)));
4474+
WITHIN_TYPE_CAST(0.0001)));
44754475

44764476
// LibreOffice / Excel:
44774477
// Beginning-of-period payments: first payment is mostly principal
@@ -4482,7 +4482,7 @@ TEST_CASE("CUMPRINC", "[finance]")
44824482
tep.evaluate("CUMPRINC(0.05/12, 60, 10000, 1, 1, 1)")),
44834483
Catch::Matchers::WithinRel(
44844484
WITHIN_TYPE_CAST(-187.929297699694),
4485-
WITHIN_TYPE_CAST(0.00001)));
4485+
WITHIN_TYPE_CAST(0.0001)));
44864486
}
44874487

44884488
// CUMIPMT

tinyexpr.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,18 @@ namespace te_builtins
449449
}
450450

451451
// compute FV at period - 1 (this must allow period - 1 == 0)
452-
const te_type n = period - 1.0;
453-
const te_type powVal = std::pow(1 + rate, n);
452+
const te_type num = period - 1.0;
453+
const te_type powVal = std::pow(1 + rate, num);
454454
if (!std::isfinite(powVal))
455455
{
456456
return te_parser::te_nan;
457457
}
458458

459459
// FV(r, n, pmt, pv, type) (note: no FV argument; payment already encodes it)
460-
const te_type fv_at_n =
460+
const te_type fvAtN =
461461
-(presentValue * powVal + payment * (1 + (rate * type)) * (powVal - 1) / rate);
462462

463-
te_type interest = fv_at_n * rate;
463+
te_type interest = fvAtN * rate;
464464

465465
// Excel adjustment for type == 1
466466
if (type == 1)
@@ -588,20 +588,20 @@ namespace te_builtins
588588
return te_parser::te_nan;
589589
}
590590

591-
const int64_t from = static_cast<int64_t>(startPeriod);
592-
const int64_t to = static_cast<int64_t>(endPeriod);
591+
const auto fromPeriod = static_cast<int64_t>(startPeriod);
592+
const auto toPeriod = static_cast<int64_t>(endPeriod);
593593

594594
te_type total{ 0 };
595595

596-
for (int64_t p = from; p <= to; ++p)
596+
for (int64_t period = fromPeriod; period <= toPeriod; ++period)
597597
{
598-
const te_type ip =
599-
te_ipmt(rate, static_cast<te_type>(p), periods, presentValue, 0, type);
600-
if (!std::isfinite(ip))
598+
const te_type interestPayment =
599+
te_ipmt(rate, static_cast<te_type>(period), periods, presentValue, 0, type);
600+
if (!std::isfinite(interestPayment))
601601
{
602602
return te_parser::te_nan;
603603
}
604-
total += ip;
604+
total += interestPayment;
605605
}
606606

607607
return total;
@@ -639,9 +639,11 @@ namespace te_builtins
639639

640640
te_type total{ 0 };
641641

642-
for (int64_t p = (int64_t)startPeriod; p <= (int64_t)endPeriod; ++p)
642+
for (auto period = static_cast<int64_t>(startPeriod);
643+
period <= static_cast<int64_t>(endPeriod); ++period)
643644
{
644-
const te_type principal = te_ppmt(rate, (te_type)p, periods, presentValue, 0, type);
645+
const te_type principal =
646+
te_ppmt(rate, static_cast<te_type>(period), periods, presentValue, 0, type);
645647

646648
if (!std::isfinite(principal))
647649
{

0 commit comments

Comments
 (0)