• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing this formula in Java  RSS feed

 
Robert Stannis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 *.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Be interested in your comments.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!