*
The moose likes Beginning Java and the fly likes log base 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "log base 10" Watch "log base 10" New topic
Author

log base 10

Anita Raj
Greenhorn

Joined: Mar 02, 2003
Posts: 7
I want to find the log base 10 of a number. The way to convert from log base e to log base 10 is : log(n) = ln(n)/ln(10).
Since java.lang.Math.log(n) is base e, I wrote these statements:
x=Math.log(5/2)/Math.log(10) //result = 0.301029996
y=Math.log(5/1)/Math.log(10) //result = 0.69897
But my calculator gives 0.39794 for log(5/2)
and 0.69897 for log(5/1). Why is it working for some numbers and not for others?
[ March 13, 2003: Message edited by: Anita Raj ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that 5 is an int literal value in Java. 5/2 would equate to 2 - not 2.5 - since it's integer division.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
are x and y double values or are they int values? It seems like you have them set up as double values since you are getting a decimal point value.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
my bad, it looks like it might be your n variable. Double or int?
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
You have: log(n) = ln(n)/ln(10).
try log(n)=log(n)/log(10)
I know that when you are using base e you don't need to use log, but rather use the natural log.
For example, to convert from base e to base 10, we would say:
log[e]x
log[10]x = ----------
log[e]10

dunno bro, give it a try.
Anita Raj
Greenhorn

Joined: Mar 02, 2003
Posts: 7
Yes, you guys are right. I should declare n as double.
double n=5/2;
double x=Math.log(n)/Math.log(10);
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Not quite: double n = 5.0 / 2 is what you would need.
double n = 5 / 2 would give n a value of 2.0 ;
Or double n = ((double)5) / 2 ; or double n = 5 / ( (double)2 ); or double n = 5 / 2.0 ;
Why? Because 5 / 2 is "only" 2.
Maybe plain double n = 2.5 would do you too.
Suppose you have:
int top , bottom ;
top = 5 ;
bottom = 2 ;
Then
double n = ( (double)top ) / bottom ;
would do the trick.
[ March 14, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
B. you love that phrase dont you?
"That should do the trick."
Thanks for the info anyway
btw, do I get a ranch hand pin or something???
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Steve said:
B. you love that phrase dont you?
"That should do the trick."

Sort of. I caught it from one of my University professors who was always saying it at the end of proofs in Topology and Analysis lectures. It was "cooler" than QED in those days.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: log base 10
 
Similar Threads
1000 decimal digits? how do I count them?
Big O
meaning of "method runs in...time"
Math.log problem in JDK1.4
Getting same numbers