aspose file tools*
The moose likes Beginning Java and the fly likes Computing natural logarithm without using Math.log()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Computing natural logarithm without using Math.log()?" Watch "Computing natural logarithm without using Math.log()?" New topic
Author

Computing natural logarithm without using Math.log()?

Marcus Stone
Greenhorn

Joined: Apr 26, 2013
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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Marcus Stone
Greenhorn

Joined: Apr 26, 2013
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
Never use Math.pow(x, 2). Use x * x
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Mar 17, 2011
Posts: 7064
    
  16

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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

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.


SCJP, SCWCD.
|Asking Good Questions|
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Nice attempted ghost edit there, Campbell.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3379
    
    9
Winston Gutkowski wrote:That statement is tautological


Don't you mean: a contradiction? ;)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Computing natural logarithm without using Math.log()?
 
Similar Threads
possible error in the book
byte problem
Sorting an integer array(ascending order)
Changes not reflected in browser
java Applet using cgi-bin - help a young lady in distress