Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

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?

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.

lowercase baba
Bartender
Posts: 12602
50
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?

Marshal
Posts: 57476
175
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.

Bartender
Posts: 10575
66

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: 2856
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.

Ranch Hand
Posts: 1164

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?

Saloon Keeper
Posts: 8313
148

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.

 Message for you sir! I think it is a tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS