Improve (sinh x) : x around 0 (#3473)

* sinh - Taylor expansion near 0
* fast path for |z| < 1e-8 in this case the second term is already to small to matter
This commit is contained in:
bdeket 2020-10-30 17:36:13 +01:00 committed by GitHub
parent 07148a22a0
commit 8137798937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -279,10 +279,10 @@
(test -inf.0 sinh -inf.0)
(test -inf.0 sinh -max.0)
(test #t double=? sinh-1 (sinh -1.0))
(test -0.0 sinh -min.0)
(test -min.0 sinh -min.0)
(test -0.0 sinh -0.0)
(test 0.0 sinh 0.0)
(test 0.0 sinh +min.0)
(test +min.0 sinh +min.0)
(test #t double=? sinh+1 (sinh 1.0))
(test +inf.0 sinh +max.0)
(test +inf.0 sinh +inf.0)

View File

@ -58,6 +58,12 @@
[(real? z)
(let loop ([z z])
(cond [(z . < . 0) (- (loop (- z)))]
[(< z 1e-8) (exact->inexact z)]
[(< z .13)
;; Taylor expansion near 0 to avoid cancellation
;~ z+z^3/3!+z^5/5!+...
(define z^2 (* z z))
(+ z (* z z^2 (+ #i1/6 (* z^2 (+ #i1/120 (* z^2 (+ #i1/5040 (* z^2 #i1/362880))))))))]
[else (/ (- (exp z) (exp (- z))) 2)]))]
[else (/ (- (exp z) (exp (- z))) 2)]))