• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Writing this formula in Java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 *.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic