• Justus Sagemüller's avatar
    Stable area hyperbolic sine for `Double` and `Float`. · 3ea33411
    Justus Sagemüller authored
    This function was unstable, in particular for negative arguments.
    
    https://ghc.haskell.org/trac/ghc/ticket/14927
    
    The reason is that the formula `log (x + sqrt (1 + x*x))` is dominated
    by the numerical error of the `sqrt` function when x is strongly negative
    (and thus the summands in the `log` mostly cancel). However, the area
    hyperbolic sine is an odd function, thus the negative side can as well
    be calculated by flipping over the positive side, which avoids this instability.
    
    Furthermore, for _very_ big arguments, the `x*x` subexpression overflows. However,
    long before that happens, the square root is anyways completely dominated
    by that term, so we can neglect the `1 +` and get
    
        sqrt (1 + x*x) ≈ sqrt (x*x) = x
    
    and therefore
    
        asinh x ≈ log (x + x) = log (2*x) = log 2 + log x
    
    which does not overflow for any normal-finite positive argument, but
    perfectly matches the exact formula within the floating-point accuracy.
    3ea33411
Float.hs 50.1 KB