This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

While calculating logarithm values in JDK1.4 it returns some unpredicted values. As Math.log() returns double so there is loss of precision and exact log base 10 values cannot be calculated.

int dispcode = (int) (-1 * (Math.log(Math.abs(Double.parseDouble(lsbrsln))) / Math.log(10)));

For example, if lsbrsln = 0.001, the formula is expected to return 3, instead it returns 2. The following part of the formula

Math.log (Math.abs (Double.parseDouble(0.001))) / Math.log(10) is returning -2.999999� and finally the formula is returning 2 while casting it into int.

In JDK1.5 we have Math.log10 method itself for calculating logbase10 values. But since we need to support JDK1.4 also we were not able to use that also.