programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Writing this formula in Java

Greenhorn
Posts: 8
• Number of slices to send:
Optional 'thank-you' note:
http://oi43.tinypic.com/2vbvc50.jpg

That's what I'm using now, but it's not giving me the right answer. All three variables are doubles. It's for a mortgage calculator, n being months, p principal, "r" rate.

Any help is appreciated!

Bartender
Posts: 2236
63
• Number of slices to send:
Optional 'thank-you' note:
double result = p *((r *(Math.pow((1 + r),n))) /(Math.pow((1 + r),n) - 1));
In the pic there is + in this place. You've got *.

Bartender
Posts: 4568
9
• Number of slices to send:
Optional 'thank-you' note:

Pawel Pawlowicz wrote:double result = p *((r *(Math.pow((1 + r),n))) /(Math.pow((1 + r),n) - 1));
In the pic there is + in this place. You've got *.

No, it's not that, he's just done a simplification: r + r/(X - 1) = rX/(X - 1)

My question would be: how do you know it's not giving the right answer? What inputs have you used, what result did you get, and what were you expecting?

Paweł Baczyński
Bartender
Posts: 2236
63
• Number of slices to send:
Optional 'thank-you' note:

Matthew Brown wrote:No, it's not that, he's just done a simplification...

You are right. I wasn't too careful when looking at that formula...

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Robert Stannis wrote:That's what I'm using now, but it's not giving me the right answer. All three variables are doubles. It's for a mortgage calculator, n being months, p principal, "r" rate.

Well, the first thing I'd do is put it in a method and spread things out a bit, viz:and then try to rationalise it.

The first thing I notice is that 'Math.pow((1 + r),n)' is repeated, so take that out:Do you see how much easier it is to read? you can even go a bit further:(I've removed the redundant brackets and corrected the literal types)

Now: is THAT what you want? ie: Is it the correct formula? I'm not familiar with this particular one, so you'll have to decide.

Lesson: Whenever you have big formulas like this, BREAK THEM UP; and if you can, give each component a meaningful name ('division' probably isn't the best, but I don't know what it's supposed to represent).
Just as with a large program, this will help you to work out what your code is actually doing.

HIH

Winston

Bartender
Posts: 5471
212
• Number of slices to send:
Optional 'thank-you' note:
Yes, the formula is correct, although in this form it is impossible to remember and
impossible to understand.
It is also valid under certain preconditions:

Greetz,
Piet

Edit: I have to use the code tags here, since I was forbidden to use the symbol 'r'.

Winston Gutkowski
Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Piet Souris wrote:Yes, the formula is correct...

Piet,

You seem to know your stuff when it comes to finance, so I'd be interested in your take on this "Meaningless drivel" topic. I posted it there, rather than hijacking this one.