wrengr / logfloat

Log-domain floating point numbers
BSD 3-Clause "New" or "Revised" License
8 stars 1 forks source link

make fromInteger/fromRational work with ludicrously large inputs #7

Closed dmwit closed 1 year ago

dmwit commented 7 years ago

Previously, LogFloat's fromRational and fromInteger would first convert to Double, then call log. For large Integers or Rationals this can overflow in the first step even if it wouldn't overflow in logspace. Here's a patch that attempts to fix that up, by converting 300 digits of the Integer/Rational at a time.

The fromRational change might be slightly controversial; for fractions whose result does fit in a Double this implementation may be a bit slower.

wrengr commented 3 years ago

Sorry it took so long to get back to this.

I like where you're headed with this, but I'm curious if you have a use case for this? I'm mainly worried about having a test suite to ensure correctness and avoid performance regressions here. E.g., I can think of various other algorithms/tweaks, like using argmax (\x -> 2^x < (10 :: Integer) ^ (300 :: Int)) for the base, or using variations on log numerator - log denominator, etc; so I'd like to have a safety net to play around with those variations.

dmwit commented 3 years ago

Uh, well. I suspect I had a use case for it five years ago. I also asked this StackOverflow question around that time, so I must have been playing with something that had big numbers. No idea what it was now, though. The most I can tell you is that it looks like I was pretty actively making commits to my Dr. Mario ratings engine around that time, but I'm not sure I could reproduce whatever problem it was I was having then. I do vaguely remember having trouble evaluating polynomials with very large exponents (for the derivative of the probability of a win given two ratings) with enough accuracy. (You get these numbers with enormous magnitudes that are supposed to all add up to something between 0 and 1...)