accuracy of asinh and atanh
Edward Kmett
ekmett at gmail.com
Mon Jun 4 04:30:54 UTC 2018
Note: From skimming your readme it is worth noting that log1p _is_ in base
now (alongside expm1, log1pexp, and log1mexp). We added them all a couple
of years back as a result of the very thread linked in your README.
You need to `import Numeric` to see them, though.
Switching to more accurate functions for doubles and floats for asinh,
atanh, etc. to exploit this sort of functionality at least seems to make a
lot of sense.
That can be done locally without any user API impact as the current
definitions aren't supplied as defaults, merely as pointwise
implementations instance by instance. Things will just become more accurate.
In that same spirit, we can probably crib a better version for complex
numbers from somewhere as well, as it follows the same general simplistic
formula right now, even if it can't be plugged directly into the equations
you've given. For that matter, the log1p definition we're using for complex
numbers was the best I could come up with, but there may well be a more
accurate version you can find down in the mines of libm or another math
library written by real analysts.
log1p <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Float.html#log1p>
x <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304820>@(a
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>
:+ <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#%3A%2B>
b <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304822>)
| abs <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#abs>
a <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>
< 0.5 && abs <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#abs>
b <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304822>
< 0.5 , u <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304823>
<- 2* <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2A>a
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>
+ <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>
a <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>*
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2A>a
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>
+ <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>
b <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304822>*
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2A>b
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304822>
= log1p <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Float.html#log1p>
(u <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304823>/
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Real.html#%2F>(1
+ <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>
sqrt <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Float.html#sqrt>(u
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304823>+
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>1)))
:+ <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#%3A%2B>
atan2 <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Float.html#atan2>
(1 + <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>
a <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304821>)
b <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304822>
| otherwise
<https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#otherwise>
= log <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Float.html#log>
(1 + <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Num.html#%2B>
x <https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Complex.html#local-6989586621679304820>)
So, here's a +1 from the libraries committee side towards improving the
situation.
>From there, it's a small matter of implementation.
Here's where I'd usually get Ben involved. Hi Ben!
-Edward
On Sat, Jun 2, 2018 at 1:23 AM, Matt Peddie <mpeddie at gmail.com> wrote:
> Hi devs,
>
> I tried to use asinh :: Double -> Double and discovered that it's
> inaccurate compared to my system library (GNU libm), even returning
> -Infinity in place of finite values in the neighborhood of -22 for
> large negative arguments. `atanh` is also inaccurate compared to the
> system library. I wrote up a more detailed description of the problem
> including plots in the README file at
> https://github.com/peddie/ghc-inverse-hyperbolic -- this repository is
> package that can help you examine the error for yourself or generate
> the plots, and it also contains accurate pure-Haskell translations of
> the system library's implementation for these functions. What's the
> next step to fixing this in GHC?
>
> Cheers
>
> Matt Peddie
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180604/104bb042/attachment.html>
More information about the ghc-devs
mailing list