Complex expressions are not working
Opened this issue · 3 comments
I have an expression like this -> "(0.341 * 8000.0) / (1 - (1 + 0.341) ^ -84)"
Expected result - "109.71908469357446"
Actual result - "1.538906384258725e-36"
calculation should be done like below snippet
fun calculateMonthlyInstallment(loanAmount: Double, monthlyInterestRate: Double, numberOfMonths: Int): Double {
val monthlyInterestRateDecimal = monthlyInterestRate / 100.0 // Convert percentage to decimal
val power = Math.pow(1 + monthlyInterestRateDecimal, -numberOfMonths.toDouble())
return (monthlyInterestRateDecimal * loanAmount) / (1 - power)
}
fun main() {
val loanAmount = 8000.0
val monthlyInterestRate = 0.341 // 0.5% interest rate
val numberOfMonths = 84
val monthlyInstallment = calculateMonthlyInstallment(loanAmount, monthlyInterestRate, numberOfMonths)
println("Monthly Installment: $monthlyInstallment")
}
Hi, @jaydeepbhayani . Thanks for the feedback!
I think something is wrong with your calculations. I tested your expression and everything seems correct with the library.
I have an expression like this -> "(0.341 * 8000.0) / (1 - (1 + 0.341) ^ -84)"
Expected result - "109.71908469357446"
I tested the expected result with wolfram alpha: calculation link . Please check it, the expected result is 2728.0000000539295785612886912317598649469480731689161625146408794
and the result from the library is 2728.0000000539294
I also checked your code, and it does not match the expression provided.
I added a comment to the main function to highlight the mistake you made
fun main() {
val loanAmount = 8000.0
val monthlyInterestRate = 34.1 // THIS LINE: you convert this percentage value by dividing by 100 in the function later
val numberOfMonths = 84
val monthlyInstallment = calculateMonthlyInstallment(loanAmount, monthlyInterestRate, numberOfMonths)
println("Monthly Installment: $monthlyInstallment")
}
I added the test case for this expression: test link