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.
posted 7 years ago
Using an "(int)" cast to create an integer from a floating point number is rather crude, especially since floating point arithmetic on computers is fundamentally imprecise (see #20 here to understand why). Depending on your circumstances, Math.round, Math.ceil or Math.floor may be a better way to do that.