Mike Simmons wrote:Sure. Wikipedia lists several formulas for finding natural logarithms. I would probably use the second one, listed after "to obtain a better rate of convergence". The later formulas are more precise but harder to understand, I think.
Okay, just read through the equation after "to obtain a better rate of convergence," wrote it down and tried to comprehend what's going on.
Here's the code I have for it so far, and yes I'm aware it's an infinite loop I just wanted to see the result.
Testing with x = 2 (which should be .6931471806), produces .823045267489
Any idea what the problem could be?
Marcus Stone wrote:Here's the code I have for it so far, and yes I'm aware it's an infinite loop I just wanted to see the result...
That statement is tautological: if it's an infinite loop, you will never see the result.
My suggestion: Follow all the other advice you've been given, AND
(a) (most important) Write down your solution in English (or your native language) before you write ONE line of Java code.
(b) Don't try to code it in main(). Write a separate method to do it.
Mansukhdeep Thind wrote:
Campbell Ritchie wrote:Never use Math.pow(x, 2). Use x * x
Why do you say so Campbell? Is it a suggestion specific to this problem that the OP is attempting to solve or is it a general rule of thumb that you suggest he follows?
I'm not Campbell, but I'd say it's a general rule of thumb: there is never any need for Math.pow(x, 2), and it's almost certainly slower than x * x, and for most of us, it's less readable. It doesn't actually create any error in the code above (the errors are elsewhere); it's just kind of silly.
Though Amit is correct that in this particular problem, it also violates the problem statement. So it's both a general rule and a problem-specific issue.