fix(xlsx): honor PMT/FV/PV/NPER type argument for annuity-due - #232
Open
mvanhorn wants to merge 1 commit into
Open
fix(xlsx): honor PMT/FV/PV/NPER type argument for annuity-due#232mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
PMT, FV, PV, and NPER accept an optional 5th type argument (0 = payment at period end, the default; 1 = payment at beginning / annuity-due). The four evaluators only read args[0..3] and never read args[4], so type=1 silently returned the ordinary-annuity result. Read the optional type from args[4] (default 0) and apply the standard annuity-due adjustment (the (1 + rate*type) factor from Excel's TVM identity). type=0 and the omitted-5th-arg case are unchanged; the rate==0 short-circuits are left intact. PPMT's internal PMT delegation is kept on the ordinary-annuity path so its observable output does not change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #213.
Summary
Makes
PMT,FV,PV, andNPERhonor their optional 5thtypeargument (annuity-due). The four evaluators insrc/officecli/Core/Formula/FormulaEvaluator.Functions.csnow readargs[4]and apply the standard beginning-of-period adjustment, so=PMT(0.05/12,12,1000,0,1)returns the annuity-due value instead of the ordinary-annuity value. One file, one root cause.Background
Excel's
PMT,FV,PV, andNPERaccept a 5thtypeargument that controls when payments occur:0= end of each period (ordinary annuity, the default), any nonzero value = beginning of each period (annuity-due).EvalPmt,EvalFv,EvalPv, andEvalNperonly readargs[0..3]and never readargs[4], so thetypeargument was silently ignored:=PMT(0.05/12,12,1000,0,1)returned the ordinary-annuity result (-85.607...) instead of the annuity-due result (-85.252...) that Excel and LibreOffice produce. Everytype=1call returned the same number astype=0.Fix
src/officecli/Core/Formula/FormulaEvaluator.Functions.csonly. Each of the four functions now reads an optionaltypefromargs[4]and applies the annuity-due adjustment derived from Excel's time-value-of-money identity:PMTdivides the payment by(1 + rate*type).FV/PVmultiply the payment-driven term by(1 + rate*type).NPERsolves with the effective paymentpmt*(1 + rate*type).typeis normalized to0/1(any nonzero value is treated as beginning-of-period), matching Excel, which treats the argument as a boolean flag. Therate == 0short-circuit branches are unchanged (timing has no effect at zero rate), andtype=0/omitted-arg results are bit-for-bit identical to before, so the common case is unaffected.PPMT's internal delegation toPMTis kept on the ordinary-annuity path soPPMToutput is unchanged (annuity-due forPPMT/IPMTis a separate concern, out of scope for this issue).This is one atomic change: a single root cause (the uniformly-ignored
typeparameter) across one cohesive function family.Validation (before / after)
Built locally (
dotnet build src/officecli/officecli.csproj) and ran the CLI. Command sequence:Before the fix (
typeignored — A2 wrongly equals A1):After the fix (A2 is the annuity-due value):
The same before/after pattern holds for
FV,PV, andNPER(before:type=1equalstype=0; after:type=0unchanged,type=1differs). All post-fix values match Microsoft Excel and LibreOffice Calc:PMT(0.05/12,12,1000,0,t)FV(0.05/12,12,-100,0,t)PV(0.05/12,12,-100,0,t)NPER(0.05/12,-100,1000,0,t)Nonzero
typevalues other than 1 also match Excel's beginning-of-period behavior, e.g.PMT(0.05/12,12,1000,0,2)now returns-85.2522640217104(was-84.89998...).