This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# log base 10

Anita Raj
Greenhorn
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
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.

Gabriel White
Ranch Hand
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
Posts: 233
my bad, it looks like it might be your n variable. Double or int?

Gabriel White
Ranch Hand
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
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
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 ]

Gabriel White
Ranch Hand
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
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.