public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix error in fdim/f for infinities
@ 2020-03-10 10:24 Fabian Schriever
  2020-03-10 14:47 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Fabian Schriever @ 2020-03-10 10:24 UTC (permalink / raw)
  To: newlib; +Cc: Fabian Schriever

The comparison c == FP_INFINITE causes the function to return +inf as it
expects x = +inf to always be larger than y. This shortcut causes
several issues as it also returns +inf for the following cases:
 - fdim(+inf, +inf), expected (as per C99): +0.0
 - fdim(-inf, any non NaN), expected: +0.0

I don't see a reason to keep the comparison as all the infinity cases
return the correct result using just the ternary operation.
---
 newlib/libm/common/s_fdim.c  | 5 +----
 newlib/libm/common/sf_fdim.c | 5 +----
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/newlib/libm/common/s_fdim.c b/newlib/libm/common/s_fdim.c
index 73a027953..61a4908f3 100644
--- a/newlib/libm/common/s_fdim.c
+++ b/newlib/libm/common/s_fdim.c
@@ -49,11 +49,8 @@ ANSI C, POSIX.
 	double y;
 #endif
 {
-  int c = __fpclassifyd(x);
-  if (c == FP_NAN)  return(x);
+  if (__fpclassifyd(x) == FP_NAN)  return(x);
   if (__fpclassifyd(y) == FP_NAN)  return(y);
-  if (c == FP_INFINITE)
-    return HUGE_VAL;
 
   return x > y ? x - y : 0.0;
 }
diff --git a/newlib/libm/common/sf_fdim.c b/newlib/libm/common/sf_fdim.c
index fe349098b..8fee57002 100644
--- a/newlib/libm/common/sf_fdim.c
+++ b/newlib/libm/common/sf_fdim.c
@@ -14,11 +14,8 @@
 	float y;
 #endif
 {
-  int c = __fpclassifyf(x);
-  if (c == FP_NAN)  return(x);
+  if (__fpclassifyf(x) == FP_NAN)  return(x);
   if (__fpclassifyf(y) == FP_NAN)  return(y);
-  if (c == FP_INFINITE)
-    return HUGE_VALF;
 
   return x > y ? x - y : 0.0;
 }
-- 
2.24.1.windows.2



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix error in fdim/f for infinities
  2020-03-10 10:24 [PATCH] Fix error in fdim/f for infinities Fabian Schriever
@ 2020-03-10 14:47 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2020-03-10 14:47 UTC (permalink / raw)
  To: Fabian Schriever; +Cc: newlib

[-- Attachment #1: Type: text/plain, Size: 1784 bytes --]

On Mar 10 11:24, Fabian Schriever wrote:
> The comparison c == FP_INFINITE causes the function to return +inf as it
> expects x = +inf to always be larger than y. This shortcut causes
> several issues as it also returns +inf for the following cases:
>  - fdim(+inf, +inf), expected (as per C99): +0.0
>  - fdim(-inf, any non NaN), expected: +0.0
> 
> I don't see a reason to keep the comparison as all the infinity cases
> return the correct result using just the ternary operation.
> ---
>  newlib/libm/common/s_fdim.c  | 5 +----
>  newlib/libm/common/sf_fdim.c | 5 +----
>  2 files changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/newlib/libm/common/s_fdim.c b/newlib/libm/common/s_fdim.c
> index 73a027953..61a4908f3 100644
> --- a/newlib/libm/common/s_fdim.c
> +++ b/newlib/libm/common/s_fdim.c
> @@ -49,11 +49,8 @@ ANSI C, POSIX.
>  	double y;
>  #endif
>  {
> -  int c = __fpclassifyd(x);
> -  if (c == FP_NAN)  return(x);
> +  if (__fpclassifyd(x) == FP_NAN)  return(x);
>    if (__fpclassifyd(y) == FP_NAN)  return(y);
> -  if (c == FP_INFINITE)
> -    return HUGE_VAL;
>  
>    return x > y ? x - y : 0.0;
>  }
> diff --git a/newlib/libm/common/sf_fdim.c b/newlib/libm/common/sf_fdim.c
> index fe349098b..8fee57002 100644
> --- a/newlib/libm/common/sf_fdim.c
> +++ b/newlib/libm/common/sf_fdim.c
> @@ -14,11 +14,8 @@
>  	float y;
>  #endif
>  {
> -  int c = __fpclassifyf(x);
> -  if (c == FP_NAN)  return(x);
> +  if (__fpclassifyf(x) == FP_NAN)  return(x);
>    if (__fpclassifyf(y) == FP_NAN)  return(y);
> -  if (c == FP_INFINITE)
> -    return HUGE_VALF;
>  
>    return x > y ? x - y : 0.0;
>  }
> -- 
> 2.24.1.windows.2
> 

Pushed.

Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-03-10 14:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-10 10:24 [PATCH] Fix error in fdim/f for infinities Fabian Schriever
2020-03-10 14:47 ` Corinna Vinschen

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).