This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Math.log problem in JDK1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Math.log problem in JDK1.4

Suganthi Velliah
Greenhorn

Joined: Apr 30, 2007
Posts: 17
Hi All,



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.



For a CR in TP, a formula was given as:


DispCode = Int ( -1 * log10( abs(LsbRsln)))

which is written in Java as,


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.

Please let me know if you have the solution.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42364
    
  64
Please do not post the same question multiple times. Let's continue the discussion in this duplicate thread, where you have an answer already.


Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Math.log problem in JDK1.4