public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Fabian Schriever <fabian.schriever@gtd-gmbh.de>
To: newlib@sourceware.org
Cc: Fabian Schriever <fabian.schriever@gtd-gmbh.de>
Subject: [PATCH] Fix error in exp in magnitude [2e-32,2e-28]
Date: Fri, 06 Mar 2020 14:49:00 -0000	[thread overview]
Message-ID: <20200306144634.2421-1-fabian.schriever@gtd-gmbh.de> (raw)

While testing the exp function we noticed some errors at the specified
magnitude. Within this range the exp function returns the input value +1
as an output. We chose to run a test of 1m exponentially spaced values
in the ranges [-2^-27,-2^-32] and [2^-32,2^-27] which showed 7603 and
3912 results with an error of >=0.5 ULP (compared with MPFR in 128 bit)
with the highest being 0.56 ULP and 0.53 ULP.

It's easy to fix by changing the magnitude at which the input value +1
is returned from <2^-28 to <2^-32 and using the polynomial instead. This
reduces the number of results with an error of >=0.5 ULP to 485 and 479
in above tests, all of which are exactly 0.5 ULP.

As we were already checking on exp we also took a look at expf. For expf
the magnitude where the input value +1 is returned can be increased from
<2^-28 to <2^-23 without accuracy loss for a slight performance
improvement. To ensure this was the correct value we tested all values
in the ranges [-2^-17,-2^-28] and [2^-28,2^-17] (~92.3m values each).
---
 newlib/libm/math/e_exp.c  | 2 +-
 newlib/libm/math/ef_exp.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/newlib/libm/math/e_exp.c b/newlib/libm/math/e_exp.c
index 81ea64dfb..d23b1162b 100644
--- a/newlib/libm/math/e_exp.c
+++ b/newlib/libm/math/e_exp.c
@@ -142,7 +142,7 @@ P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
 	    }
 	    x  = hi - lo;
 	} 
-	else if(hx < 0x3e300000)  {	/* when |x|<2**-28 */
+	else if(hx < 0x3df00000)  {	/* when |x|<2**-32 */
 	    if(huge+x>one) return one+x;/* trigger inexact */
 	}
 
diff --git a/newlib/libm/math/ef_exp.c b/newlib/libm/math/ef_exp.c
index e817370ac..fb3e2ffe6 100644
--- a/newlib/libm/math/ef_exp.c
+++ b/newlib/libm/math/ef_exp.c
@@ -77,7 +77,7 @@ P5   =  4.1381369442e-08; /* 0x3331bb4c */
 	    }
 	    x  = hi - lo;
 	} 
-	else if(hx < 0x31800000)  {	/* when |x|<2**-28 */
+	else if(hx < 0x34000000)  {	/* when |x|<2**-23 */
 	    if(huge+x>one) return one+x;/* trigger inexact */
 	}
 
-- 
2.24.1.windows.2


             reply	other threads:[~2020-03-06 14:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-06 14:49 Fabian Schriever [this message]
2020-03-09  9:12 ` Corinna Vinschen

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200306144634.2421-1-fabian.schriever@gtd-gmbh.de \
    --to=fabian.schriever@gtd-gmbh.de \
    --cc=newlib@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).