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
//! # PV Function
//!
//! Returns a Double specifying the present value of an annuity based on periodic, fixed payments to be paid in the future and a fixed interest rate.
//!
//! ## Syntax
//!
//! ```vb
//! PV(rate, nper, pmt, [fv], [type])
//! ```
//!
//! ## Parameters
//!
//! - `rate` - Required. Double specifying interest rate per period. For example, if you get a car loan at an annual percentage rate (APR) of 10% and make monthly payments, the rate per period is 0.1/12, or 0.0083.
//! - `nper` - Required. Integer specifying total number of payment periods in the annuity. For example, if you make monthly payments on a 4-year car loan, your loan has 4 * 12 (or 48) payment periods.
//! - `pmt` - Required. Double specifying payment to be made each period. Payments usually contain principal and interest that does not change over the life of the annuity.
//! - `fv` - Optional. Variant specifying future value or cash balance you want after you've made the final payment. For example, the future value of a loan is $0 because that's its value after the final payment. However, if you want to save $50,000 over 18 years for your child's education, then $50,000 is the future value. If omitted, 0 is assumed.
//! - `type` - Optional. Variant specifying when payments are due. Use 0 if payments are due at the end of the payment period, or use 1 if payments are due at the beginning of the period. If omitted, 0 is assumed.
//!
//! ## Return Value
//!
//! Returns a `Double` specifying the present value of an annuity. The present value is the current value of a series of future payments or the current value of a future lump sum.
//!
//! ## Remarks
//!
//! The `PV` function is the inverse of the `FV` function. While `FV` calculates what a series of payments will be worth in the future,
//! `PV` calculates what those same future payments are worth today, discounted by a rate of return.
//!
//! An annuity is a series of fixed cash payments made over a period of time. An annuity can be a loan (such as a home mortgage)
//! or an investment (such as a monthly savings plan).
//!
//! The `rate` and `nper` arguments must be calculated using payment periods expressed in the same units. For example, if `rate`
//! is calculated using months, `nper` must also be calculated using months.
//!
//! For all arguments, cash paid out (such as deposits to savings) is represented by negative numbers; cash received
//! (such as dividend checks) is represented by positive numbers.
//!
//! **Important Uses**:
//! - **Loan Affordability**: Calculate how much you can borrow given a specific payment amount
//! - **Investment Valuation**: Determine current value of future cash flows
//! - **Annuity Pricing**: Calculate lump sum value of periodic payments
//! - **Lease Analysis**: Determine present value of lease payments
//!
//! ## Typical Uses
//!
//! 1. **Loan Affordability**: Calculate maximum loan amount based on affordable payment
//! 2. **Investment Valuation**: Determine present value of future investment returns
//! 3. **Annuity Valuation**: Calculate lump sum value of annuity payments
//! 4. **Bond Pricing**: Value bonds based on coupon payments and face value
//! 5. **Lease vs Buy Analysis**: Compare present value of lease payments to purchase price
//! 6. **Pension Valuation**: Calculate current value of future pension payments
//! 7. **Structured Settlement**: Determine lump sum value of periodic payments
//! 8. **Capital Budgeting**: Evaluate present value of project cash flows
//!
//! ## Basic Examples
//!
//! ### Example 1: Loan Affordability
//! ```vb
//! ' How much can you borrow if you can afford $500/month for 5 years at 6% APR?
//! Dim loanAmount As Double
//! loanAmount = Abs(PV(0.06 / 12, 5 * 12, -500))
//! ' Returns approximately $25,775 (negative payment = money you pay out)
//! ```
//!
//! ### Example 2: Investment Present Value
//! ```vb
//! ' What's the present value of receiving $1,000/month for 10 years at 5% return?
//! Dim presentValue As Double
//! presentValue = Abs(PV(0.05 / 12, 10 * 12, 1000))
//! ' Returns approximately $94,289 (positive payment = money you receive)
//! ```
//!
//! ### Example 3: Annuity Valuation
//! ```vb
//! ' Value of annuity paying $2,000/month for 20 years at 4% discount rate
//! Dim annuityValue As Double
//! annuityValue = Abs(PV(0.04 / 12, 20 * 12, 2000))
//! ' Returns the lump sum equivalent value
//! ```
//!
//! ### Example 4: Lump Sum with Future Value
//! ```vb
//! ' Present value of $50,000 in 10 years at 6% annual return (no periodic payments)
//! Dim presentValue As Double
//! presentValue = Abs(PV(0.06, 10, 0, -50000))
//! ' Returns approximately $27,920 (what you'd need to invest today)
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `CalculateAffordableLoan`
//! ```vb
//! Function CalculateAffordableLoan(monthlyPayment As Double, _
//! annualRate As Double, _
//! years As Integer) As Double
//! Dim monthlyRate As Double
//! Dim numPayments As Integer
//!
//! monthlyRate = annualRate / 12
//! numPayments = years * 12
//!
//! ' Negative payment because it's money flowing out
//! CalculateAffordableLoan = Abs(PV(monthlyRate, numPayments, -monthlyPayment))
//! End Function
//! ```
//!
//! ### Pattern 2: `ComparePaymentOptions`
//! ```vb
//! Function ComparePaymentOptions(payment1 As Double, years1 As Integer, _
//! payment2 As Double, years2 As Integer, _
//! rate As Double) As String
//! Dim pv1 As Double
//! Dim pv2 As Double
//!
//! pv1 = Abs(PV(rate / 12, years1 * 12, -payment1))
//! pv2 = Abs(PV(rate / 12, years2 * 12, -payment2))
//!
//! If pv1 > pv2 Then
//! ComparePaymentOptions = "Option 1 allows borrowing $" & _
//! Format(pv1 - pv2, "#,##0") & " more"
//! Else
//! ComparePaymentOptions = "Option 2 allows borrowing $" & _
//! Format(pv2 - pv1, "#,##0") & " more"
//! End If
//! End Function
//! ```
//!
//! ### Pattern 3: `CalculateLumpSumEquivalent`
//! ```vb
//! Function CalculateLumpSumEquivalent(monthlyPayment As Double, _
//! years As Integer, _
//! discountRate As Double) As Double
//! ' Calculate what a stream of payments is worth as a lump sum today
//! Dim monthlyRate As Double
//!
//! monthlyRate = discountRate / 12
//! CalculateLumpSumEquivalent = Abs(PV(monthlyRate, years * 12, monthlyPayment))
//! End Function
//! ```
//!
//! ### Pattern 4: `ValidatePVParameters`
//! ```vb
//! Function ValidatePVParameters(rate As Double, nper As Integer, _
//! pmt As Double) As Boolean
//! ValidatePVParameters = False
//!
//! If nper <= 0 Then
//! MsgBox "Number of periods must be positive"
//! Exit Function
//! End If
//!
//! If rate < -1 Then
//! MsgBox "Interest rate cannot be less than -100%"
//! Exit Function
//! End If
//!
//! ValidatePVParameters = True
//! End Function
//! ```
//!
//! ### Pattern 5: `CalculateBreakEvenLoanAmount`
//! ```vb
//! Function CalculateBreakEvenLoanAmount(payment As Double, _
//! rate As Double, _
//! years As Integer, _
//! upfrontCosts As Double) As Double
//! ' Calculate loan amount where total cost equals upfront costs
//! Dim loanPV As Double
//!
//! loanPV = Abs(PV(rate / 12, years * 12, -payment))
//! CalculateBreakEvenLoanAmount = loanPV - upfrontCosts
//! End Function
//! ```
//!
//! ### Pattern 6: `PVOfMixedCashFlows`
//! ```vb
//! Function PVOfMixedCashFlows(regularPayment As Double, _
//! rate As Double, _
//! nper As Integer, _
//! futureValue As Double) As Double
//! ' Calculate PV when you have both regular payments and a lump sum
//! PVOfMixedCashFlows = Abs(PV(rate, nper, regularPayment, futureValue))
//! End Function
//! ```
//!
//! ### Pattern 7: `CalculateRequiredDownPayment`
//! ```vb
//! Function CalculateRequiredDownPayment(homePrice As Double, _
//! monthlyPayment As Double, _
//! rate As Double, _
//! years As Integer) As Double
//! Dim maxLoan As Double
//!
//! maxLoan = Abs(PV(rate / 12, years * 12, -monthlyPayment))
//!
//! If maxLoan >= homePrice Then
//! CalculateRequiredDownPayment = 0
//! Else
//! CalculateRequiredDownPayment = homePrice - maxLoan
//! End If
//! End Function
//! ```
//!
//! ### Pattern 8: `CompareLumpSumVsAnnuity`
//! ```vb
//! Function CompareLumpSumVsAnnuity(lumpSum As Double, _
//! annuityPayment As Double, _
//! years As Integer, _
//! discountRate As Double) As String
//! Dim annuityPV As Double
//! Dim difference As Double
//!
//! annuityPV = Abs(PV(discountRate / 12, years * 12, annuityPayment))
//! difference = lumpSum - annuityPV
//!
//! If difference > 0 Then
//! CompareLumpSumVsAnnuity = "Lump sum is better by $" & Format(difference, "#,##0")
//! ElseIf difference < 0 Then
//! CompareLumpSumVsAnnuity = "Annuity is better by $" & Format(Abs(difference), "#,##0")
//! Else
//! CompareLumpSumVsAnnuity = "Both options are equal"
//! End If
//! End Function
//! ```
//!
//! ### Pattern 9: `CalculateLeaseValue`
//! ```vb
//! Function CalculateLeaseValue(monthlyLease As Double, _
//! leaseTermMonths As Integer, _
//! discountRate As Double) As Double
//! ' Calculate present value of all lease payments
//! CalculateLeaseValue = Abs(PV(discountRate / 12, leaseTermMonths, -monthlyLease))
//! End Function
//! ```
//!
//! ### Pattern 10: `FindAffordablePayment`
//! ```vb
//! Function FindAffordablePayment(desiredLoan As Double, _
//! rate As Double, _
//! nper As Integer) As Double
//! ' Reverse calculation: find payment from desired loan amount
//! ' This uses Pmt, but demonstrates PV relationship
//! FindAffordablePayment = Abs(Pmt(rate, nper, desiredLoan))
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Comprehensive Loan Calculator
//! ```vb
//! ' Calculate loan amounts based on payment affordability
//! Class LoanAffordabilityCalculator
//! Private m_monthlyIncome As Double
//! Private m_monthlyDebts As Double
//! Private m_annualRate As Double
//! Private m_loanYears As Integer
//! Private m_debtToIncomeRatio As Double
//!
//! Public Sub Initialize(monthlyIncome As Double, monthlyDebts As Double, _
//! annualRate As Double, loanYears As Integer)
//! m_monthlyIncome = monthlyIncome
//! m_monthlyDebts = monthlyDebts
//! m_annualRate = annualRate
//! m_loanYears = loanYears
//! m_debtToIncomeRatio = 0.43 ' Standard 43% DTI ratio
//! End Sub
//!
//! Public Function GetMaxMonthlyPayment() As Double
//! Dim maxTotalDebt As Double
//! Dim maxPayment As Double
//!
//! maxTotalDebt = m_monthlyIncome * m_debtToIncomeRatio
//! maxPayment = maxTotalDebt - m_monthlyDebts
//!
//! If maxPayment < 0 Then maxPayment = 0
//! GetMaxMonthlyPayment = maxPayment
//! End Function
//!
//! Public Function GetMaxLoanAmount() As Double
//! Dim maxPayment As Double
//! Dim monthlyRate As Double
//! Dim numPayments As Integer
//!
//! maxPayment = GetMaxMonthlyPayment()
//! monthlyRate = m_annualRate / 12
//! numPayments = m_loanYears * 12
//!
//! ' Use PV to find how much can be borrowed
//! GetMaxLoanAmount = Abs(PV(monthlyRate, numPayments, -maxPayment))
//! End Function
//!
//! Public Function GetLoanWithDownPayment(downPayment As Double) As Double
//! GetLoanWithDownPayment = GetMaxLoanAmount() + downPayment
//! End Function
//!
//! Public Function GetRequiredDownPayment(homePrice As Double) As Double
//! Dim maxLoan As Double
//!
//! maxLoan = GetMaxLoanAmount()
//!
//! If maxLoan >= homePrice Then
//! GetRequiredDownPayment = 0
//! Else
//! GetRequiredDownPayment = homePrice - maxLoan
//! End If
//! End Function
//!
//! Public Function GenerateAffordabilityReport() As String
//! Dim report As String
//! Dim maxPayment As Double
//! Dim maxLoan As Double
//!
//! maxPayment = GetMaxMonthlyPayment()
//! maxLoan = GetMaxLoanAmount()
//!
//! report = "Loan Affordability Analysis" & vbCrLf
//! report = report & String(50, "=") & vbCrLf
//! report = report & "Monthly Income: $" & Format(m_monthlyIncome, "#,##0") & vbCrLf
//! report = report & "Existing Debts: $" & Format(m_monthlyDebts, "#,##0") & vbCrLf
//! report = report & "DTI Ratio: " & Format(m_debtToIncomeRatio * 100, "0") & "%" & vbCrLf
//! report = report & String(50, "-") & vbCrLf
//! report = report & "Max Monthly Payment: $" & Format(maxPayment, "#,##0") & vbCrLf
//! report = report & "Interest Rate: " & Format(m_annualRate * 100, "0.00") & "%" & vbCrLf
//! report = report & "Loan Term: " & m_loanYears & " years" & vbCrLf
//! report = report & String(50, "-") & vbCrLf
//! report = report & "Maximum Loan Amount: $" & Format(maxLoan, "#,##0")
//!
//! GenerateAffordabilityReport = report
//! End Function
//! End Class
//! ```
//!
//! ### Example 2: Annuity Comparison Tool
//! ```vb
//! ' Compare different annuity and lump sum options
//! Module AnnuityComparison
//! Private Type AnnuityOption
//! Name As String
//! Payment As Double
//! Years As Integer
//! IsLumpSum As Boolean
//! LumpSumAmount As Double
//! End Type
//!
//! Public Function CompareOptions(options() As AnnuityOption, _
//! discountRate As Double) As String
//! Dim report As String
//! Dim i As Integer
//! Dim pv As Double
//! Dim monthlyRate As Double
//! Dim bestValue As Double
//! Dim bestIndex As Integer
//!
//! monthlyRate = discountRate / 12
//! bestValue = 0
//! bestIndex = LBound(options)
//!
//! report = "Annuity Option Comparison" & vbCrLf
//! report = report & "Discount Rate: " & Format(discountRate * 100, "0.0") & "%" & vbCrLf
//! report = report & String(60, "=") & vbCrLf
//! report = report & "Option Type Present Value" & vbCrLf
//! report = report & String(60, "-") & vbCrLf
//!
//! For i = LBound(options) To UBound(options)
//! If options(i).IsLumpSum Then
//! pv = options(i).LumpSumAmount
//! Else
//! pv = Abs(PV(monthlyRate, options(i).Years * 12, options(i).Payment))
//! End If
//!
//! If pv > bestValue Then
//! bestValue = pv
//! bestIndex = i
//! End If
//!
//! report = report & Left(options(i).Name & Space(20), 20) & _
//! IIf(options(i).IsLumpSum, "Lump Sum ", "Annuity ") & _
//! "$" & Format(pv, "#,##0")
//!
//! If i = bestIndex Then report = report & " *BEST*"
//! report = report & vbCrLf
//! Next i
//!
//! report = report & String(60, "-") & vbCrLf
//! report = report & "Recommended: " & options(bestIndex).Name
//!
//! CompareOptions = report
//! End Function
//!
//! Public Function CalculateAnnuityYield(lumpSum As Double, _
//! monthlyPayment As Double, _
//! years As Integer) As Double
//! ' Find the discount rate that makes PV equal to lump sum
//! ' This is a simplified approximation
//! Dim rate As Double
//! Dim pv As Double
//! Dim diff As Double
//!
//! rate = 0.05 ' Starting guess
//! Do
//! pv = Abs(PV(rate / 12, years * 12, monthlyPayment))
//! diff = pv - lumpSum
//!
//! If Abs(diff) < 0.01 Then Exit Do
//!
//! ' Adjust rate
//! If diff > 0 Then
//! rate = rate + 0.0001
//! Else
//! rate = rate - 0.0001
//! End If
//! Loop While Abs(diff) > 0.01
//!
//! CalculateAnnuityYield = rate
//! End Function
//! End Module
//! ```
//!
//! ### Example 3: Lease vs Buy Analyzer
//! ```vb
//! ' Compare leasing vs buying with present value analysis
//! Class LeaseVsBuyAnalyzer
//! Private m_purchasePrice As Double
//! Private m_monthlyLease As Double
//! Private m_leaseTermMonths As Integer
//! Private m_discountRate As Double
//! Private m_residualValue As Double
//!
//! Public Sub Initialize(purchasePrice As Double, monthlyLease As Double, _
//! leaseTermMonths As Integer, discountRate As Double, _
//! residualValue As Double)
//! m_purchasePrice = purchasePrice
//! m_monthlyLease = monthlyLease
//! m_leaseTermMonths = leaseTermMonths
//! m_discountRate = discountRate
//! m_residualValue = residualValue
//! End Sub
//!
//! Public Function GetLeasePresentValue() As Double
//! ' Calculate PV of all lease payments
//! GetLeasePresentValue = Abs(PV(m_discountRate / 12, m_leaseTermMonths, -m_monthlyLease))
//! End Function
//!
//! Public Function GetBuyPresentValue() As Double
//! ' Calculate PV of buying (purchase price minus PV of residual value)
//! Dim pvResidual As Double
//!
//! ' PV of residual value (what it's worth after lease term)
//! pvResidual = Abs(PV(m_discountRate / 12, m_leaseTermMonths, 0, -m_residualValue))
//!
//! GetBuyPresentValue = m_purchasePrice - pvResidual
//! End Function
//!
//! Public Function GetRecommendation() As String
//! Dim leasePV As Double
//! Dim buyPV As Double
//! Dim difference As Double
//!
//! leasePV = GetLeasePresentValue()
//! buyPV = GetBuyPresentValue()
//! difference = Abs(buyPV - leasePV)
//!
//! If leasePV < buyPV Then
//! GetRecommendation = "LEASE - Saves $" & Format(difference, "#,##0") & " in PV"
//! ElseIf leasePV > buyPV Then
//! GetRecommendation = "BUY - Saves $" & Format(difference, "#,##0") & " in PV"
//! Else
//! GetRecommendation = "Either option - PV is equal"
//! End If
//! End Function
//!
//! Public Function GenerateAnalysis() As String
//! Dim analysis As String
//! Dim leasePV As Double
//! Dim buyPV As Double
//!
//! leasePV = GetLeasePresentValue()
//! buyPV = GetBuyPresentValue()
//!
//! analysis = "Lease vs Buy Analysis" & vbCrLf
//! analysis = analysis & String(50, "=") & vbCrLf
//! analysis = analysis & "Purchase Price: $" & Format(m_purchasePrice, "#,##0") & vbCrLf
//! analysis = analysis & "Monthly Lease: $" & Format(m_monthlyLease, "#,##0") & vbCrLf
//! analysis = analysis & "Lease Term: " & m_leaseTermMonths & " months" & vbCrLf
//! analysis = analysis & "Discount Rate: " & Format(m_discountRate * 100, "0.0") & "%" & vbCrLf
//! analysis = analysis & "Residual Value: $" & Format(m_residualValue, "#,##0") & vbCrLf
//! analysis = analysis & String(50, "-") & vbCrLf
//! analysis = analysis & "Lease PV: $" & Format(leasePV, "#,##0") & vbCrLf
//! analysis = analysis & "Buy PV: $" & Format(buyPV, "#,##0") & vbCrLf
//! analysis = analysis & String(50, "-") & vbCrLf
//! analysis = analysis & "Recommendation: " & GetRecommendation()
//!
//! GenerateAnalysis = analysis
//! End Function
//! End Class
//! ```
//!
//! ### Example 4: Pension Valuation Calculator
//! ```vb
//! ' Calculate present value of pension benefits
//! Class PensionValuator
//! Private m_monthlyPension As Double
//! Private m_yearsToRetirement As Integer
//! Private m_yearsOfPayments As Integer
//! Private m_discountRate As Double
//! Private m_inflationRate As Double
//!
//! Public Sub SetPensionDetails(monthlyPension As Double, _
//! yearsToRetirement As Integer, _
//! yearsOfPayments As Integer)
//! m_monthlyPension = monthlyPension
//! m_yearsToRetirement = yearsToRetirement
//! m_yearsOfPayments = yearsOfPayments
//! End Sub
//!
//! Public Sub SetEconomicAssumptions(discountRate As Double, inflationRate As Double)
//! m_discountRate = discountRate
//! m_inflationRate = inflationRate
//! End Sub
//!
//! Public Function GetPensionPresentValue() As Double
//! Dim monthlyRate As Double
//! Dim numPayments As Integer
//! Dim pvAtRetirement As Double
//! Dim pvToday As Double
//!
//! monthlyRate = m_discountRate / 12
//! numPayments = m_yearsOfPayments * 12
//!
//! ' Calculate PV at retirement
//! pvAtRetirement = Abs(PV(monthlyRate, numPayments, m_monthlyPension))
//!
//! ' Discount back to today
//! pvToday = Abs(PV(m_discountRate, m_yearsToRetirement, 0, -pvAtRetirement))
//!
//! GetPensionPresentValue = pvToday
//! End Function
//!
//! Public Function GetInflationAdjustedValue() As Double
//! Dim realRate As Double
//! Dim monthlyRate As Double
//! Dim numPayments As Integer
//! Dim pvAtRetirement As Double
//! Dim pvToday As Double
//!
//! ' Fisher equation: (1 + nominal) = (1 + real)(1 + inflation)
//! realRate = ((1 + m_discountRate) / (1 + m_inflationRate)) - 1
//! monthlyRate = realRate / 12
//! numPayments = m_yearsOfPayments * 12
//!
//! pvAtRetirement = Abs(PV(monthlyRate, numPayments, m_monthlyPension))
//! pvToday = Abs(PV(realRate, m_yearsToRetirement, 0, -pvAtRetirement))
//!
//! GetInflationAdjustedValue = pvToday
//! End Function
//!
//! Public Function GenerateValuationReport() As String
//! Dim report As String
//! Dim nominalPV As Double
//! Dim realPV As Double
//! Dim totalPayments As Double
//!
//! nominalPV = GetPensionPresentValue()
//! realPV = GetInflationAdjustedValue()
//! totalPayments = m_monthlyPension * m_yearsOfPayments * 12
//!
//! report = "Pension Valuation Report" & vbCrLf
//! report = report & String(50, "=") & vbCrLf
//! report = report & "Monthly Pension: $" & Format(m_monthlyPension, "#,##0") & vbCrLf
//! report = report & "Years to Retirement: " & m_yearsToRetirement & vbCrLf
//! report = report & "Years of Payments: " & m_yearsOfPayments & vbCrLf
//! report = report & "Discount Rate: " & Format(m_discountRate * 100, "0.0") & "%" & vbCrLf
//! report = report & "Inflation Rate: " & Format(m_inflationRate * 100, "0.0") & "%" & vbCrLf
//! report = report & String(50, "-") & vbCrLf
//! report = report & "Total Nominal Payments: $" & Format(totalPayments, "#,##0") & vbCrLf
//! report = report & "Present Value (Nominal): $" & Format(nominalPV, "#,##0") & vbCrLf
//! report = report & "Present Value (Real): $" & Format(realPV, "#,##0") & vbCrLf
//! report = report & String(50, "-") & vbCrLf
//! report = report & "Value Reduction from Inflation: $" & _
//! Format(nominalPV - realPV, "#,##0") & " (" & _
//! Format(((nominalPV - realPV) / nominalPV) * 100, "0.0") & "%)"
//!
//! GenerateValuationReport = report
//! End Function
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! The `PV` function can raise errors in the following situations:
//!
//! - **Invalid Procedure Call (Error 5)**: When:
//! - `nper` is 0 or negative
//! - `rate` is -1 (causes division by zero in the formula)
//! - **Type Mismatch (Error 13)**: When arguments cannot be converted to numeric values
//! - **Overflow (Error 6)**: When calculated values exceed Double range
//!
//! Always validate input parameters:
//!
//! ```vb
//! On Error Resume Next
//! presentValue = PV(rate, nper, pmt, fv, type)
//! If Err.Number <> 0 Then
//! MsgBox "Error calculating present value: " & Err.Description
//! Err.Clear
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - The `PV` function is very fast for individual calculations
//! - Avoid calling repeatedly in tight loops if parameters don't change
//! - Pre-calculate monthly rates and other constants outside loops
//! - For sensitivity analysis, consider caching results
//!
//! ## Best Practices
//!
//! 1. **Convert Rates Properly**: Always divide annual rates by 12 for monthly calculations
//! 2. **Match Time Units**: Ensure rate and nper use the same time period
//! 3. **Use Absolute Value**: Use `Abs()` to display positive values to users
//! 4. **Validate Inputs**: Check that nper > 0 and rate is reasonable
//! 5. **Handle Sign Conventions**: Remember negative = outflow, positive = inflow
//! 6. **Round for Display**: Use `Format()` for currency display
//! 7. **Document Assumptions**: Clearly state discount rates and time periods
//! 8. **Consider Inflation**: Use real rates for inflation-adjusted analysis
//! 9. **Test Edge Cases**: Verify behavior with 0% rate, very long terms
//! 10. **Compare with Pmt**: Understand the inverse relationship between PV and Pmt
//!
//! ## Comparison with Related Functions
//!
//! | Function | Purpose | Returns | Use Case |
//! |----------|---------|---------|----------|
//! | **PV** | Present value of annuity | Double (current value) | Loan affordability, investment valuation |
//! | **FV** | Future value of annuity | Double (future value) | Investment growth, savings goals |
//! | **Pmt** | Periodic payment | Double (payment amount) | Loan payments, inverse of PV |
//! | **NPV** | Net present value | Double (NPV) | Project evaluation with irregular cash flows |
//! | **`NPer`** | Number of periods | Double (period count) | Time to goal calculation |
//! | **Rate** | Interest rate | Double (rate per period) | Finding effective rate |
//!
//! ## Platform and Version Notes
//!
//! - Available in all versions of VBA and VB6
//! - Behavior is consistent across Windows platforms
//! - Uses standard present value formulas from financial mathematics
//! - For zero interest rates, PV is simply pmt * nper + fv
//! - Maximum precision limited by Double data type
//!
//! ## Limitations
//!
//! - Assumes constant interest rate over entire period
//! - Assumes equal payment amounts (standard annuity)
//! - Does not account for taxes, fees, or transaction costs
//! - Cannot handle variable rate scenarios without recalculation
//! - Does not consider payment frequency other than what you specify
//! - Sign convention can be confusing (negative for outflows)
//!
//! ## Related Functions
//!
use crate;
use crate;
use err_number;
/// Implementation of the `PV` function.
///
/// Calculates the present value of an annuity based on periodic, fixed payments
/// and a fixed interest rate. The calculation supports both ordinary annuities
/// (payments at end of period) and annuities due (payments at beginning of period).
///
/// VB6 behavior:
/// - `rate` is the interest rate per period
/// - `nper` must be positive; if <= 0, raises error 5 (Invalid procedure call)
/// - `fv` defaults to 0 if omitted
/// - `type` defaults to 0 (end of period) if omitted; use 1 for beginning of period
/// - Returns a `Double`