efultz
efultz

Reputation: 1267

Math.log is not working - why?

// The method below is not working correctly and I am not sure why. The log results are not correct. 0.5 is returning log value -0.6931471805599453 but my calculator shows it should be returning -.3.

public static void printCommonLogTable()
{
    double x = 0;
    while (x <= 10) 
      {
       System.out.println (x+ "  " + Math.log(x));
       x = x + .5;
      } 
}

Upvotes: 2

Views: 2024

Answers (3)

jeff_kile
jeff_kile

Reputation: 1835

Java's Math.log function uses base e and the answer you are getting from java appears to be the correct answer.

I think your test calculator is using a base other than e.

Java uses base e because you you can can easily calculate log base x of y using log base e with this formula

double log_base_x_of_y = Math.log(y)/Math.log(x);

This is called the change of base formula.

Upvotes: 0

Sean Owen
Sean Owen

Reputation: 66896

Because log, like the log method in any standard math library, uses base e. The answer you get is correct in that sense. You need a different computation to compute a log in the base you are thinking of.

Upvotes: 0

rgettman
rgettman

Reputation: 178303

The Math.log method uses base e, the natural log, not the common log, base 10. The natural log of 0.5 is -0.6931471805599453.

Returns the natural logarithm (base e) of a double value.

Use the Math.log10 method for the common log (base 10).

Returns the base 10 logarithm of a double value.

Upvotes: 6

Related Questions