Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex expressions are not working #122

Open
jaydeepbhayani opened this issue Jan 7, 2024 · 3 comments
Open

Complex expressions are not working #122

jaydeepbhayani opened this issue Jan 7, 2024 · 3 comments

Comments

@jaydeepbhayani
Copy link

jaydeepbhayani commented Jan 7, 2024

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")
}
@RobertZenz
Copy link

Wolfram Alpha says that the result should be 2728?

If I'm not mistaken (which might be, am tired) then your code comes out as:

((0.341 / 100.0) * 8000.0) / (1 - ((1 + (341.0 / 100.0)) ^ -84))

which results in 27.28.

@RobertZenz
Copy link

Having said that, both expressions are correctly evaluated by exp4j (0.4.8):

(0.341 * 8000.0) / (1 - (1 + 0.341) ^ -84) = 2728
((0.341 / 100.0) * 8000.0) / (1 - ((1 + (341.0 / 100.0)) ^ -84)) = 27.28

@leogtzr
Copy link
Contributor

leogtzr commented Jan 8, 2024

I get the exact same result from the "bc" linux utility:

 ~/dev/repos/dev  story-ss-312…enkins-build *2 ?1  bc -lq                                                                                       ✔  2 task  15:44:15 
>>> (0.341 * 8000.0) / (1 - (1 + 0.341) ^ -84)
2728.00000005392957854258
>>> ((0.341 / 100.0) * 8000.0) / (1 - ((1 + (341.0 / 100.0)) ^ -84))
27.28000000000000000000
>>> 

exp4j is evaluating correctly the expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants