From: Paul Zimmermann <Paul.Zimmermann@inria.fr>
To: libc-alpha@sourceware.org
Subject: fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is tiny
Date: Mon, 27 Jul 2020 19:15:09 +0200 [thread overview]
Message-ID: <mwmu3knale.fsf@tomate.loria.fr> (raw)
Hi,
before the patch below, the maximum ulp error for j0 in the whole binary32
range is 6177902 ulps (for x = 3.153646966e+38).
After this patch, it is 900691 ulps (for x = 2.404825449e+00).
The patch fixes the case where x >= 2^127 and tiny sin(x)+cos(x).
Large remaining errors are due to a cancellation in another branch of the code.
Paul
PS: the same method can be applied to j1 and y1.
PS2: this can wait for 2.33 of course.
From 6b731f36b1a5badf4704645d0dda40957cedd0db Mon Sep 17 00:00:00 2001
From: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Date: Mon, 27 Jul 2020 19:01:18 +0200
Subject: [PATCH] fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is
tiny
---
sysdeps/ieee754/flt-32/e_j0f.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/sysdeps/ieee754/flt-32/e_j0f.c b/sysdeps/ieee754/flt-32/e_j0f.c
index c89b9f2688..f85d8a59e0 100644
--- a/sysdeps/ieee754/flt-32/e_j0f.c
+++ b/sysdeps/ieee754/flt-32/e_j0f.c
@@ -56,6 +56,22 @@ __ieee754_j0f(float x)
if ((s*c)<zero) cc = z/ss;
else ss = z/cc;
}
+ else {
+ /* we subtract (exactly) a value x0 such that cos(x0)+sin(x0)
+ is very near from 0, and use the identity
+ sin(x-x0) = sin(x)*cos(x0)-cos(x)*sin(x0) to get
+ sin(x) + cos(x) with extra accuracy */
+ float x0 = 3.153646966e+38f;
+ float y = x - x0; /* exact */
+ /* sin(y) = sin(x)*cos(x0)-cos(x)*sin(x0) */
+ z = __sinf (y);
+ float eps = 8.17583368e-8f;
+ /* cos(x0) ~ -sin(x0) + eps */
+ z += eps * __cosf (x);
+ /* now z ~ (sin(x)-cos(x))*cos(x0) */
+ float cosx0 = -0.707106740f;
+ cc = z / cosx0;
+ }
/*
* j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x)
* y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x)
--
2.27.0
next reply other threads:[~2020-07-27 17:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-27 17:15 Paul Zimmermann [this message]
2020-07-27 21:35 ` fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is tiny Joseph Myers
2020-07-28 8:23 ` Paul Zimmermann
2020-07-28 9:19 ` Andreas Schwab
2020-07-28 10:50 ` Paul Zimmermann
2020-07-28 18:09 ` Adhemerval Zanella via Libc-alpha
2020-07-29 7:03 ` fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is tiny (v2) Paul Zimmermann
2020-07-29 20:25 ` Adhemerval Zanella via Libc-alpha
2020-07-30 7:20 ` fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is tiny (v3) Paul Zimmermann
2020-07-30 8:53 ` Florian Weimer via Libc-alpha
2020-08-03 13:19 ` fix inaccuracy of j0f for x >= 2^127 when sin(x)+cos(x) is tiny (v4) Paul Zimmermann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/libc/involved.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=mwmu3knale.fsf@tomate.loria.fr \
--to=paul.zimmermann@inria.fr \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).