r/learnjava • • Sep 07 '21

How would you write this maths formula in java?

1 Upvotes

6 comments sorted by

9

u/Apprehensive_Royal77 Sep 07 '21

Rather than write the entire formula as one, just break it down into pieces.

Do the numerator as one and the denominator as another, then divide the 2 results in a separate sum.

If there's a lot going on in the numerator or denominator break them into simpler parts.

If you still want it to be one formula then combine all the components into the one formula once you're sure it's working correctly. (Personally once it works I prefer to keep it in the broken down form as it's a lot easier to read for someone coming in cold to read your code)

2

u/Cuz1 Sep 07 '21 edited Sep 07 '21

I see what you mean, ill give it a shot and see how it goes! Mine seems to be abit of a mess at the moment so I think it will make things alot clearer. Its coming back with numbers like 2.0643236245577496E13

btw thanks for the help :)

*edit*

is this what you meant?

``double newMonthlyPayment=0, denominator, numerator;monthlyPayment = newMonthlyPayment;denominator = account.amount * account.interestRate * Math.pow(1+ account.interestRate / 12, account.numberOfMonths);numerator = 12 * (Math.pow(1 + account.interestRate / 12, account.numberOfMonths) - 1);monthlyPayment = numerator / denominator;``

1

u/Apprehensive_Royal77 Sep 07 '21

Yes, that's exactly what I mean.

Seeing as you're getting a number that appears to be too high, I would break it down a little more.

So I'd break out the Math.pow() portions and print each result to the console. It'll help you identify which section is causing the high number. At that point you should be able to fix the issue.

2

u/Cuz1 Sep 07 '21

Oops, forgot to post my attempt:This is with variables from my code.

p = account.amount * account.interestRate * Math.pow(1+ account.interestRate / 12, account.numberOfMonths) / 12 * (Math.pow(1 + account.interestRate / 12, account.numberOfMonths) - 1)

I just wanted to make sure it was right as it is coming back with some weird numbers.
for example: 2.0643236245577496E13

1

u/AutoModerator Sep 07 '21

It seems that you possibly have a screenshot of code in your post How would you write this maths formula in java? in /r/learnjava.

Screenshots of code instead of actual code text is against the Code posting rules of /r/learnjava as is outlined in the sidebar - Code posting.

  • No screenshots of code!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).

  • Pastebin for programs that consist of a single class only

  • Gist for multi-class programs, or programs that require additional files

  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.

  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/darkhorse1997 Sep 07 '21

I would create a variable for a of type double and calculate a^n since it's a different formula. Then I would break the big formula into numerator and denominator variables and then finally find the resultant. Also have to look out for double and int conversions.