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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here