Skip to content

Commit 4106697

Browse files
committed
Fix var length warning, add Excel example to unit test
1 parent 8f3568f commit 4106697

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

tests/tetests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,6 +4269,22 @@ TEST_CASE("PV", "[finance]")
42694269
// Excel: =PV(0.03, 1, 0, 1000)
42704270
CHECK_THAT(WITHIN_TYPE_CAST(tep.evaluate("PV(0.03, 1, 0, 1000)")),
42714271
Catch::Matchers::WithinRel(WITHIN_TYPE_CAST(-970.87), WITHIN_TYPE_CAST(0.0001)));
4272+
4273+
// Excel example:
4274+
// Money paid out of an insurance annuity at the end of every month: $500
4275+
// Interest rate: 8% annually
4276+
// Term: 20 years
4277+
//
4278+
// Excel formula:
4279+
// =PV(A3/12, 12*A4, A2, , 0)
4280+
// =PV(0.08/12, 240, 500, 0, 0)
4281+
//
4282+
// Excel result = -59,775.15
4283+
CHECK_THAT(
4284+
WITHIN_TYPE_CAST(tep.evaluate("PV(0.08/12, 12*20, 500, 0, 0)")),
4285+
Catch::Matchers::WithinRel(
4286+
WITHIN_TYPE_CAST(-59775.15),
4287+
WITHIN_TYPE_CAST(0.0001)));
42724288
}
42734289

42744290
TEST_CASE("Nominal", "[finance]")

tinyexpr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ namespace te_builtins
256256
}
257257

258258
[[nodiscard]]
259-
static te_type te_pv(te_type rate, te_type nper, te_type pmt, te_type fv, te_type type)
259+
static te_type te_pv(te_type rate, te_type nper, te_type pmt, te_type futureValue, te_type type)
260260
{
261261
if (!std::isfinite(rate) || !std::isfinite(nper) || !std::isfinite(pmt))
262262
{
263263
return te_parser::te_nan;
264264
}
265265

266266
// optional args default like Excel
267-
if (!std::isfinite(fv))
267+
if (!std::isfinite(futureValue))
268268
{
269-
fv = 0;
269+
futureValue = 0;
270270
}
271271
if (!std::isfinite(type))
272272
{
@@ -289,7 +289,7 @@ namespace te_builtins
289289

290290
if (rate == 0.0)
291291
{
292-
return -(fv + pmt * nper);
292+
return -(futureValue + pmt * nper);
293293
}
294294

295295
const te_type powVal = std::pow(1 + rate, nper);
@@ -298,7 +298,7 @@ namespace te_builtins
298298
return te_parser::te_nan;
299299
}
300300

301-
return -(fv + pmt * (1 + rate * type) * (powVal - 1) / rate) / powVal;
301+
return -(futureValue + pmt * (1 + rate * type) * (powVal - 1) / rate) / powVal;
302302
}
303303

304304
[[nodiscard]]

0 commit comments

Comments
 (0)