1

The documentation of the SE Math library is, thankfully, very transparent about rounding errors:

If a method always has an error less than 0.5 ulps, the method always returns the floating-point number nearest the exact result; such a method is correctly rounded. A correctly rounded method is generally the best a floating-point approximation can be; however, it is impractical for many floating-point methods to be correctly rounded. Instead, for the Math class, a larger error bound of 1 or 2 ulps is allowed for certain methods. Informally, with a 1 ulp error bound, when the exact result is a representable number, the exact result should be returned as the computed result; otherwise, either of the two floating-point values which bracket the exact result may be returned.

And every floating-point method mentions its error bounds in ulps. In particular, for Math.log():

Returns the natural logarithm (base e) of a double value...The computed result must be within 1 ulp of the exact result

Therefore, Math.log() will possibly round to the nearest representable value in the wrong direction.

I need a correctly rounded implementation of base-e log. Where might I find one?

AntsySysHack
  • 129
  • 1
  • 8
  • 1
    Have you seen this: [Logarithm of a BigDecimal](https://stackoverflow.com/questions/739532/logarithm-of-a-bigdecimal)? BigDecimal should offer better precision and ought to allow you to choose the rounding logic. (Will almost certainly be more computationally expensive, though.) – Bobulous Oct 16 '17 at 18:43
  • 1
    Asking for exactly rounded results from `log()` runs into the Table Maker's Dilemma – EOF Oct 16 '17 at 19:10
  • Is the problem really about rounding, or is it about insufficient precision in the stock math library? The precision problem is easy-ish to solve, by using a format with more, selectable precision, as others have said. If the problem really is about rounding behaviour at the limits of the selected precision, however, that's a highly technical problem that won't be so tractable. – Kevin Boone Oct 16 '17 at 20:23

0 Answers0