NetBSD Problem Report #44875

From www@NetBSD.org  Mon Apr 18 08:53:21 2011
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id C918663C28D
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 18 Apr 2011 08:53:20 +0000 (UTC)
Message-Id: <20110418085320.35EEA63C02F@www.NetBSD.org>
Date: Mon, 18 Apr 2011 08:53:20 +0000 (UTC)
From: henning.petersen@t-online.de
Reply-To: henning.petersen@t-online.de
To: gnats-bugs@NetBSD.org
Subject: Return y in nextafter.c and nextafterf.c
X-Send-Pr-Version: www-1.0

>Number:         44875
>Category:       lib
>Synopsis:       Return y in nextafter.c and nextafterf.c
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Apr 18 08:55:00 +0000 2011
>Closed-Date:    Mon Apr 18 16:03:16 +0000 2011
>Last-Modified:  Fri Apr 29 18:50:02 +0000 2011
>Originator:     Henning Petersen
>Release:        NetBSD-current
>Organization:
>Environment:
>Description:
Man page is saying, that if (x == y) return is y;
>How-To-Repeat:

>Fix:
diff -u -r1.11 s_nextafter.c
--- lib/libm/src/s_nextafter.c	26 May 2002 22:01:57 -0000	1.11
+++ lib/libm/src/s_nextafter.c	17 Apr 2011 03:51:13 -0000
@@ -39,7 +39,7 @@
 	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
 	   ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */
 	   return x+y;
-	if(x==y) return x;		/* x=y, return x */
+	if(x==y) return y;		/* x=y, return y */
 	if((ix|lx)==0) {			/* x == 0 */
 	    INSERT_WORDS(x,hy&0x80000000,1);	/* return +-minsubnormal */
 	    y = x*x;

diff -u -r1.7 s_nextafterf.c
--- lib/libm/src/s_nextafterf.c	26 May 2002 22:01:58 -0000	1.7
+++ lib/libm/src/s_nextafterf.c	17 Apr 2011 03:51:13 -0000
@@ -34,7 +34,7 @@
 	if((ix>0x7f800000) ||   /* x is nan */
 	   (iy>0x7f800000))     /* y is nan */
 	   return x+y;
-	if(x==y) return x;		/* x=y, return x */
+	if(x==y) return y;		/* x=y, return y */
 	if(ix==0) {				/* x == 0 */
 	    SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */
 	    y = x*x;

>Release-Note:

>Audit-Trail:
From: Martin Husemann <martin@duskware.de>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 12:49:41 +0200

 There is some (subtle) humour in this PR making fun of the man page?
 Or what else am I not getting?

 Martin

From: Jukka Ruohonen <jruohonen@iki.fi>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 14:09:38 +0300

 On Mon, Apr 18, 2011 at 10:50:04AM +0000, Martin Husemann wrote:
 >  There is some (subtle) humour in this PR making fun of the man page?
 >  Or what else am I not getting?

 No. The (newly adjusted) man-page is entirely correct:

 See the C99 specification (p. 237):

 	"The nextafter functions return y if x equals y."

 and POSIX:

 	"If x== y, y (of the type x) shall be returned."

 http://pubs.opengroup.org/onlinepubs/9699919799/functions/nextafter.html

 This is a  bug in NetBSD.

From: Martin Husemann <martin@duskware.de>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 13:14:46 +0200

 On Mon, Apr 18, 2011 at 11:10:07AM +0000, Jukka Ruohonen wrote:
 >  This is a  bug in NetBSD.

 I'm sorry, I still don't get it. How can you tell the difference if not
 looking at the source? I even wonder if the compiler would produce different
 object code. What am I missing?

 Martin

From: Jukka Ruohonen <jruohonen@iki.fi>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 14:22:08 +0300

 On Mon, Apr 18, 2011 at 11:15:05AM +0000, Martin Husemann wrote:
 > The following reply was made to PR lib/44875; it has been noted by GNATS.
 > 
 > From: Martin Husemann <martin@duskware.de>
 > To: gnats-bugs@NetBSD.org
 > Cc: 
 > Subject: Re: lib/44875
 > Date: Mon, 18 Apr 2011 13:14:46 +0200
 > 
 >  On Mon, Apr 18, 2011 at 11:10:07AM +0000, Jukka Ruohonen wrote:
 >  >  This is a  bug in NetBSD.
 >  
 >  I'm sorry, I still don't get it. How can you tell the difference if not
 >  looking at the source? I even wonder if the compiler would produce different
 >  object code. What am I missing?

 You are of course right about the humor with respect to the typical (all? 
 most?) primitive functions in libm, but 'x == y' in any serious FP
 computations is just plain stupid, i.e. cf. it may be that x \approxneq y.

From: Jukka Ruohonen <jruohonen@iki.fi>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 14:13:34 +0300

 On Mon, Apr 18, 2011 at 10:50:04AM +0000, Martin Husemann wrote:
 >  There is some (subtle) humour in this PR making fun of the man page?
 >  Or what else am I not getting?

 And if I may futher add, the wording probably implies that if "x is y" in
 terms of (sufficient) floating point representation/rounding, then y is
 returned.

From: Martin Husemann <martin@duskware.de>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Mon, 18 Apr 2011 14:47:26 +0200

 On Mon, Apr 18, 2011 at 11:45:02AM +0000, Jukka Ruohonen wrote:
 >  And if I may futher add, the wording probably implies that if "x is y" in
 >  terms of (sufficient) floating point representation/rounding, then y is
 >  returned.

 Yes, all fine - but is there any (even if hypothetical) case where the
 caller could possibly find out the difference?

 I understood we are talking about floting point and == is a hairy operator,
 but still I think the suggested change is a no-op. Given the way it is
 implemented, how could the returned result differ? Are there FP values that
 compare equal and still are distinguishable? Even different inexecat values
 should still compare not equal, shouldn't they? The NaN case is handled
 separately, so we know neither x nor y could be any variant of NaN.

 Btw: I would suggest to go through libm and always use isnan() and friends
 instead of homegrown checks (even if equivalent).

 Martin

From: "Matthias Drochner" <drochner@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/44875 CVS commit: src/lib/libm/src
Date: Mon, 18 Apr 2011 15:59:09 +0000

 Module Name:	src
 Committed By:	drochner
 Date:		Mon Apr 18 15:59:09 UTC 2011

 Modified Files:
 	src/lib/libm/src: s_nextafter.c s_nextafterf.c

 Log Message:
 according to C99/POSIX, nextafter(x,y) should return y if x==y, from
 Henning Petersen per PR lib/44875


 To generate a diff of this commit:
 cvs rdiff -u -r1.11 -r1.12 src/lib/libm/src/s_nextafter.c
 cvs rdiff -u -r1.7 -r1.8 src/lib/libm/src/s_nextafterf.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: drochner@NetBSD.org
State-Changed-When: Mon, 18 Apr 2011 16:03:16 +0000
State-Changed-Why:
patch applied, thanks


From: Alan Barrett <apb@cequrux.com>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/44875
Date: Fri, 29 Apr 2011 20:46:24 +0200

 On Mon, 18 Apr 2011, Martin Husemann wrote:
 > Are there FP values that compare equal and still are 
 > distinguishable?

 Yes.  Positive zero and negative zero compare equal, yet are 
 distinguishable, in at least some floating point representations.

 When I run this program:

 #include <stdio.h>
 int main(void)
 {
      double x = 0.0;        /* positive zero */
      double y = -1.0 * 0.0; /* negative zero */

      printf("x = %g\n", x);
      printf("y = %g\n", y);
      printf("%s\n", (x == y ? "x == y" : "!(x == y)"));
      return 0;
 }

 I get this output:

 x = 0
 y = -0
 x == y

 I can also imagine floating point representations in which 
 many values have multiple representations, analogous to the 
 correspondence in decimal between 1.0e0, 10.0e-1, and 0.1e1.

 --apb (Alan Barrett)

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.