X-Sharp / XSharpPublic

Public repository for the source code for the XSharp Compiler, Runtime, Project System and Tools.
Apache License 2.0
112 stars 38 forks source link

Payment(), FV() and PV() - Foxpro Enhancement #964

Closed Karl1155 closed 2 years ago

Karl1155 commented 2 years ago

attached is a XIDE viaef (foxpro dialect) which implements the funcs:

FUNCTION FV ( nPayment AS FLOAT , nInterestRate AS FLOAT , nPeriods AS FLOAT ) AS FLOAT 
FUNCTION Payment( nPrincipal AS FLOAT , nInterestRate AS FLOAT , nPayments AS FLOAT ) AS FLOAT
FUNCTION PV ( nPayment AS FLOAT , nInterestRate AS FLOAT , nTotalPayments AS FLOAT ) AS FLOAT 

it seems that these funcs are not often used. At least, i have found only a few references. The site https://hackfox.github.io/section4/s4g052.html describes some pitfalls. What´s missing there is the description of another strange behaviour: If the third parameter has decimal places, VFP rounds up/down the value ...

The results you see in the X# console should be equal to the results you see when you run this code with VFP:

? "--- Payment() ---"
? Payment ( 100000 , .105/12 , (20*12)  )
? Payment ( -100000 , .105/12 , (20*12)  ) 
? Payment ( -100000 , .105/12 , 250.5  )
? Payment ( 100235 , .105/12 , 250.5 ) 
? Payment ( 100235.56 , .105/12 , -250.2 ) 
? Payment ( -100235.56 , -.105/12 , 250.2 ) 
? Payment ( 100000 , -.08/12 , -180.2 ) 
? Payment ( 100000 , .08/12 , 180 ) 
? Payment (  100000 , -.08/12   , (30*12) ) 
? Payment (  200000 , .04/12 , 30*12 ) 
? Payment(10000, .12/12, 12) 
? Payment(10000, .12/52, 52) 
? Payment(10000, .12, 1) 
?                    
? "--- PV() ---"
? PV (  500 ,.075/12 , 48 ) 
? PV ( -500 , - .075/12 , 48 ) 
? PV (  500 , .075/12 , 48.5 ) 
? PV (  -500 ,.075/12 , -48.5 )
?        
? "--- FV() ---"
? FV ( 500 , .075/12 , 48 )  
? FV ( 10000 , .02 , 12 )  
? FV ( -10000 , .02 , 12 ) 
? FV ( -10000 , .02 , 12.4999 ) 
? FV ( 10000.45 , 0.02 , 12.4 ) 
? FV ( 10000.45 , -0.02 , 12.4 ) 
? FV ( 10000.45 , 0.02 , -12.4 ) 
? FV ( 10000.45 , 0.02 , 12.5 ) 
? FV ( 10000.45 , 0.02 , 13 ) 
? FV ( -10000.45 , 0.02 , 13.4 ) 
?

BTW. I´m not a financial mathematic, so don't ask me about the details of the formulas used ;-)

Karl-Heinz

VFPFinancal.zip

RobertvanderHulst commented 2 years ago

Karl, Thanks. I will evaluate the code and will integrate it.

cpyrgas commented 2 years ago

Confirmed done and tested