Anita Raj

Greenhorn

Posts: 7

posted 14 years ago

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 ]

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

posted 14 years ago

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

Gabriel White

Ranch Hand

Posts: 233

Gabriel White

Ranch Hand

Posts: 233

posted 14 years ago

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.

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

posted 14 years ago

Yes, you guys are right. I should declare n as double.

double n=5/2;

double x=Math.log(n)/Math.log(10);

double n=5/2;

double x=Math.log(n)/Math.log(10);

Barry Gaunt

Ranch Hand

Posts: 7729

posted 14 years ago

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 ]

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

Posts: 233

Barry Gaunt

Ranch Hand

Posts: 7729

posted 14 years ago

Steve said:

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.

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.

Getting someone to think and try something out is much more useful than just telling them the answer.