# Computing natural logarithm without using Math.log()?

Marcus Stone
Greenhorn
Posts: 2
Given a value of x, I'm trying to compute the ln(x) without using the Math class at all.

Any ideas on where to even start?

Mike Simmons
Ranch Hand
Posts: 3090
14
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.

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
I'd also suggest you get out your handy pencil and paper.

work through the algorithm by hand a few times, until you are sure you understand it. Only after doing that should you start coding anything.

Marcus Stone
Greenhorn
Posts: 2
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?

Campbell Ritchie
Sheriff
Posts: 50168
79
Never use Math.pow(x, 2). Use x * x

Mike Simmons
Ranch Hand
Posts: 3090
14
That's true, but of course has nothing to do with the accuracy of the result. Marcus, I think you need to look at the value of i each time though the loop. Is it changing? Should it? Perhaps there's been a mix-up between "i" and "increment". I'm having a hard time following what the other variables are supposed to be doing there.

Winston Gutkowski
Bartender
Posts: 10527
64
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.

(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.

Winston

Bartender
Posts: 2854
10
Campbell Ritchie wrote:Never use Math.pow(x, 2). Use x * x

That would also comply to the original requirement of not using the Math class at all.

Mike Simmons
Ranch Hand
Posts: 3090
14
Nice attempted ghost edit there, Campbell.

Mansukhdeep Thind
Ranch Hand
Posts: 1158
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?

Stephan van Hulst
Bartender
Posts: 6310
77
Winston Gutkowski wrote:That statement is tautological

Don't you mean: a contradiction? ;)

Mike Simmons
Ranch Hand
Posts: 3090
14
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.

Mike Simmons
Ranch Hand
Posts: 3090
14
Stephan van Hulst wrote:
Winston Gutkowski wrote:That statement is tautological

Don't you mean: a contradiction? ;)

Maybe it's a contraption. Or a contraction.