From kargl@troutmask.apl.washington.edu  Fri Feb 26 00:14:22 2010
Return-Path: <kargl@troutmask.apl.washington.edu>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 93A761065687
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 26 Feb 2010 00:14:22 +0000 (UTC)
	(envelope-from kargl@troutmask.apl.washington.edu)
Received: from troutmask.apl.washington.edu (troutmask.apl.washington.edu [128.208.78.105])
	by mx1.freebsd.org (Postfix) with ESMTP id 739E78FC16
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 26 Feb 2010 00:14:22 +0000 (UTC)
Received: from troutmask.apl.washington.edu (localhost.apl.washington.edu [127.0.0.1])
	by troutmask.apl.washington.edu (8.14.4/8.14.4) with ESMTP id o1Q0EM6S028398
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 25 Feb 2010 16:14:22 -0800 (PST)
	(envelope-from kargl@troutmask.apl.washington.edu)
Received: (from kargl@localhost)
	by troutmask.apl.washington.edu (8.14.4/8.14.4/Submit) id o1Q0EMer028397;
	Thu, 25 Feb 2010 16:14:22 -0800 (PST)
	(envelope-from kargl)
Message-Id: <201002260014.o1Q0EMer028397@troutmask.apl.washington.edu>
Date: Thu, 25 Feb 2010 16:14:22 -0800 (PST)
From: "Steven G. Kargl" <kargl@troutmask.apl.washington.edu>
Reply-To: "Steven G. Kargl" <kargl@troutmask.apl.washington.edu>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Nasty bug in jn(3)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         144306
>Category:       bin
>Synopsis:       [libm] [patch] Nasty bug in jn(3)
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    uqs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Feb 26 00:20:04 UTC 2010
>Closed-Date:    Tue Nov 23 19:16:08 UTC 2010
>Last-Modified:  Tue Nov 23 19:16:08 UTC 2010
>Originator:     Steven G. Kargl
>Release:        FreeBSD 9.0-CURRENT amd64
>Organization:
APL/UW
>Environment:
System: FreeBSD troutmask.apl.washington.edu 9.0-CURRENT FreeBSD 9.0-CURRENT #0 r204219M: Mon Feb 22 12:41:49 PST 2010 kargl@troutmask.apl.washington.edu:/usr/obj/usr/src/sys/SPEW amd64


	
>Description:

troutmask:kargl[466] cat testjn.c 
#include <stdio.h>
#include <math.h>

int
main(void)
{
        double z;
        int n;

        z = 2.4048255576957729;
        for (n = 2; n < 10; n++)
                printf("%d %e\n", n, jn(n,z));
        return (0);
}
troutmask:kargl[467] cc -o z testjn.c -lm
troutmask:kargl[468] ./z
2 4.317548e-01
3 -inf
4 4.069027e-02
5 -inf
6 3.247664e-03
7 -inf
8 7.495602e-05
9 -inf

The value of a integer order Bessel function with
argument z = 2.4048255576957729 is never -inf.

>How-To-Repeat:
	See above
>Fix:

	


>Release-Note:
>Audit-Trail:

From: "Steven G. Kargl" <kargl@troutmask.apl.washington.edu>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: bin/144306: Nasty bug in jn(3)
Date: Thu, 25 Feb 2010 20:15:33 -0800 (PST)

 This patch fixes the problem.
 
 Index: e_jn.c
 ===================================================================
 --- e_jn.c	(revision 204219)
 +++ e_jn.c	(working copy)
 @@ -200,7 +200,12 @@
  			}
  	     	    }
  		}
 -	    	b = (t*__ieee754_j0(x)/b);
 +		z = __ieee754_j0(x);
 +		w = __ieee754_j1(x);
 +		if (fabs(z) >= fabs(w))
 +	    		b = (t*z/b);
 +		else
 +			b = (t*w/a);		
  	    }
  	}
  	if(sgn==1) return -b; else return b;
 
 -- 
 Steve
 http://troutmask.apl.washington.edu/~kargl/

From: "Steven G. Kargl" <kargl@troutmask.apl.washington.edu>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: bin/144306: Nasty bug in jn(3)
Date: Fri, 26 Feb 2010 11:58:33 -0800 (PST)

 A review of the source history at
 
 http://svn.freebsd.org/viewvc/base/head/lib/msun/src/e_jn.c?view=log
 
 shows that this bug has been around for at 15 years.
 
 Hopefully, the patch doesn't sit in idly in the bug database.
 
 -- 
 Steve
 http://troutmask.apl.washington.edu/~kargl/

From: Ulrich =?utf-8?B?U3DDtnJsZWlu?= <uqs@spoerlein.net>
To: bug-followup@FreeBSD.org, kargl@troutmask.apl.washington.edu
Cc:  
Subject: Re: bin/144306: [libm] [patch] Nasty bug in jn(3)
Date: Tue, 9 Nov 2010 21:02:44 +0100

 --IS0zKkzwUGydFO0o
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 Hello Steve, I saw your updated patch for jnf(3) on the mailing list and
 was wondering what the correct values for your test case would look like?
 I'm asking because on Ubuntu I get slightly different values. Especially
 disturbing is, that for FreeBSD the jn/jnf values are identical in this
 case.
 
 Ubuntu 10.04:
 
 # cc -o testjn -lm testjn.c && ./testjn
 2 4.317548e-01
 3 1.850071e-01
 4 6.121505e-02
 5 1.568141e-02
 6 3.251921e-03
 7 5.737757e-04
 8 8.808225e-05
 9 1.195869e-05
 # cc -o testjnf -lm testjnf.c && ./testjnf
 2 4.317548e-01
 3 2.126431e-01
 4 6.348598e-02
 5 1.606404e-02
 6 3.341151e-03
 7 5.894695e-04
 8 9.044447e-05
 9 1.228349e-05
 
 
 FreeBSD -CURRENT:
 
 freebsd64# cc -o testjn -lm testjn.c && ./testjn
 2 4.317548e-01
 3 1.989999e-01
 4 6.474667e-02
 5 1.638924e-02
 6 3.404818e-03
 7 6.006884e-04
 8 9.216579e-05
 9 1.251727e-05
 freebsd64# cc -o testjnf -lm testjnf.c && ./testjnf
 2 4.317548e-01
 3 1.989999e-01
 4 6.474666e-02
 5 1.638924e-02
 6 3.404818e-03
 7 6.006881e-04
 8 9.216577e-05
 9 1.251727e-05
 
 (both systems are amd64, I tried gcc and clang on FreeBSD, giving the
 same results.)
 
 Here's the full patch, for archival purposes:
 
 
 --IS0zKkzwUGydFO0o
 Content-Type: text/x-diff; charset=utf-8
 Content-Disposition: attachment; filename="msun.diff"
 Content-Transfer-Encoding: 8bit
 
 commit 1db46def1f105e5b8a2d814eeaac46716b1a663c
 Author: Ulrich Spörlein <uqs@spoerlein.net>
 Date:   Tue Nov 9 20:39:57 2010 +0100
 
     Fix bug with jn(3) and jnf(3)
     
     PR:		bin/144306
     Submitted by:	Steven G. Kargl <kargl@troutmask.apl.washington.edu>
 
 diff --git a/lib/msun/src/e_jn.c b/lib/msun/src/e_jn.c
 index e277e30..8b0bc62 100644
 --- a/lib/msun/src/e_jn.c
 +++ b/lib/msun/src/e_jn.c
 @@ -200,7 +200,12 @@ __ieee754_jn(int n, double x)
  			}
  	     	    }
  		}
 -	    	b = (t*__ieee754_j0(x)/b);
 +		z = __ieee754_j0(x);
 +		w = __ieee754_j1(x);
 +		if (fabs(z) >= fabs(w))
 +		    b = (t*z/b);
 +		else
 +		    b = (t*w/a);
  	    }
  	}
  	if(sgn==1) return -b; else return b;
 diff --git a/lib/msun/src/e_jnf.c b/lib/msun/src/e_jnf.c
 index 3bbf7b7..d045bb05 100644
 --- a/lib/msun/src/e_jnf.c
 +++ b/lib/msun/src/e_jnf.c
 @@ -152,7 +152,12 @@ __ieee754_jnf(int n, float x)
  			}
  	     	    }
  		}
 -	    	b = (t*__ieee754_j0f(x)/b);
 +		z = __ieee754_j0f(x);
 +		w = __ieee754_j1f(x);
 +		if (fabs(z) >= fabs(w))
 +		    b = (t*z/b);
 +		else
 +		    b = (t*w/a);
  	    }
  	}
  	if(sgn==1) return -b; else return b;
 
 --IS0zKkzwUGydFO0o--

From: "Steven G. Kargl" <kargl@troutmask.apl.washington.edu>
To: =?UTF-8?Q?Ulrich_Sp=C3=B6rlein?= <uqs@spoerlein.net>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/144306: [libm] [patch] Nasty bug in jn(3)
Date: Fri, 12 Nov 2010 10:01:54 -0800 (PST)

 Ulrich Sp?rlein wrote:
 > Hello Steve, I saw your updated patch for jnf(3) on the mailing list and
 > was wondering what the correct values for your test case would look like?
 > I'm asking because on Ubuntu I get slightly different values. Especially
 > disturbing is, that for FreeBSD the jn/jnf values are identical in this
 > case.
 
 Apologies for a delayed response.  It seems spamassasin decided
 your email was junk mail and put your email into my junk mail
 directory.
 
 Bruce has already given you some answers towards
 the correct values.  You can also use MPFR to generate
 a set of values for comparison.
 
 Another way to look at the issue is to start near a 
 zero of j0f and compute say jnf(3,x) with x ranging
 over a small set of values. 
 
 #include<stdio.h>
 #include<math.h>
 
 int
 main(void)
 {
    float x;
 
    x = 2.404820;
    do {
       x = nextafterf(x, 10.);
       printf("%e %e %e\n", x, jnf(3,x), jn(3,(double)x));
    } while(x < 2.404826);
    return (0);
 }
 
 Without the patch, the 2nd column of the output is not 
 a smooth function, which is counter to the mathematical
 properties of a bessel fcn.  Note, the 3rd column is 
 jn(3,x) where I have applied the patch.
 
 troutmask:kargl[202] cc -o z t.c -lm && ./z
 2.404820e+00 1.989337e-01 1.989989e-01
 2.404820e+00 1.977170e-01 1.989990e-01
 2.404821e+00 1.997695e-01 1.989990e-01
 2.404821e+00 1.977899e-01 1.989991e-01
 2.404821e+00 2.007952e-01 1.989991e-01
 2.404821e+00 1.986288e-01 1.989991e-01
 2.404822e+00 1.970318e-01 1.989992e-01
 2.404822e+00 1.999776e-01 1.989992e-01
 2.404822e+00 1.976307e-01 1.989993e-01
 2.404822e+00 2.017480e-01 1.989993e-01
 2.404823e+00 1.987786e-01 1.989994e-01
 2.404823e+00 1.961335e-01 1.989994e-01
 2.404823e+00 1.999664e-01 1.989994e-01
 2.404823e+00 2.053210e-01 1.989995e-01
 (12 lines removed)
 
 With the patch and with bde's correction for fabsf(), you get
 
 troutmask:kargl[204] cc -o z t.c -lm && ./z
 2.404820e+00 1.989989e-01 1.989989e-01
 2.404820e+00 1.989990e-01 1.989990e-01
 2.404821e+00 1.989990e-01 1.989990e-01
 2.404821e+00 1.989991e-01 1.989991e-01
 2.404821e+00 1.989991e-01 1.989991e-01
 2.404821e+00 1.989991e-01 1.989991e-01
 2.404822e+00 1.989992e-01 1.989992e-01
 2.404822e+00 1.989992e-01 1.989992e-01
 2.404822e+00 1.989993e-01 1.989993e-01
 2.404822e+00 1.989994e-01 1.989993e-01
 2.404823e+00 1.989994e-01 1.989994e-01
 2.404823e+00 1.989994e-01 1.989994e-01
 2.404823e+00 1.989995e-01 1.989994e-01
 2.404823e+00 1.989995e-01 1.989995e-01
 (12 lines removed)
 
 > 
 > Ubuntu 10.04:
 > 
 
 I could not find an on-line source code repository for Ubuntu.
 I suspect that Ubuntu uses an algorithm for jn[f] based on the
 code from fdlibm.  This problem appears to date back to at
 least 1995 or so.
 
 Now to explain the problem and the solution.  jn[f](n,x) for
 certain ranges of x uses downward recursion to compute the
 value of the function.  The recursion sequence that is generated
 is proportional to the actual desired value, so a normalization
 step is taken.  This normalization is j0[f](x) divided by
 the the zeroth sequence member.  As Bruce notes, near the 
 zeros of j0[f](x) the computed value can have giga-ULP inaccuracy.
 I found for the 1st zero of j0f(x) only the leading decimal digit
 is correct.  The solution to the issue is fairly straight
 forward.  The zeros of j0(x) and j1(x) never coincide, so as
 j0(x) approaches a zero, the normalization constant switches to
 j1[f](x) divided by the 2nd sequence member.  The expectation is
 that j1[f](x) is an more accurately computed value.
 
 -- 
 Steve
 http://troutmask.apl.washington.edu/~kargl/
Responsible-Changed-From-To: freebsd-bugs->uqs 
Responsible-Changed-By: uqs 
Responsible-Changed-When: Fri Nov 12 20:37:46 UTC 2010 
Responsible-Changed-Why:  
Grab this 

http://www.freebsd.org/cgi/query-pr.cgi?pr=144306 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/144306: commit references a PR
Date: Sat, 13 Nov 2010 10:54:15 +0000 (UTC)

 Author: uqs
 Date: Sat Nov 13 10:54:10 2010
 New Revision: 215237
 URL: http://svn.freebsd.org/changeset/base/215237
 
 Log:
   Fix bug in jn(3) and jnf(3) that led to -inf results
   
   Explanation by Steve:
   jn[f](n,x) for certain ranges of x uses downward recursion to compute
   the value of the function.  The recursion sequence that is generated is
   proportional to the actual desired value, so a normalization step is
   taken.  This normalization is j0[f](x) divided by the zeroth sequence
   member.  As Bruce notes, near the zeros of j0[f](x) the computed value
   can have giga-ULP inaccuracy. I found for the 1st zero of j0f(x) only
   the leading decimal digit is correct.  The solution to the issue is
   fairly straight forward.  The zeros of j0(x) and j1(x) never coincide,
   so as j0(x) approaches a zero, the normalization constant switches to
   j1[f](x) divided by the 2nd sequence member.  The expectation is that
   j1[f](x) is a more accurately computed value.
   
   PR:		bin/144306
   Submitted by:	Steven G. Kargl <kargl@troutmask.apl.washington.edu>
   Reviewed by:	bde
   MFC after:	7 days
 
 Modified:
   head/lib/msun/src/e_jn.c
   head/lib/msun/src/e_jnf.c
 
 Modified: head/lib/msun/src/e_jn.c
 ==============================================================================
 --- head/lib/msun/src/e_jn.c	Sat Nov 13 10:38:06 2010	(r215236)
 +++ head/lib/msun/src/e_jn.c	Sat Nov 13 10:54:10 2010	(r215237)
 @@ -200,7 +200,12 @@ __ieee754_jn(int n, double x)
  			}
  	     	    }
  		}
 -	    	b = (t*__ieee754_j0(x)/b);
 +		z = __ieee754_j0(x);
 +		w = __ieee754_j1(x);
 +		if (fabs(z) >= fabs(w))
 +		    b = (t*z/b);
 +		else
 +		    b = (t*w/a);
  	    }
  	}
  	if(sgn==1) return -b; else return b;
 
 Modified: head/lib/msun/src/e_jnf.c
 ==============================================================================
 --- head/lib/msun/src/e_jnf.c	Sat Nov 13 10:38:06 2010	(r215236)
 +++ head/lib/msun/src/e_jnf.c	Sat Nov 13 10:54:10 2010	(r215237)
 @@ -152,7 +152,12 @@ __ieee754_jnf(int n, float x)
  			}
  	     	    }
  		}
 -	    	b = (t*__ieee754_j0f(x)/b);
 +		z = __ieee754_j0f(x);
 +		w = __ieee754_j1f(x);
 +		if (fabsf(z) >= fabsf(w))
 +		    b = (t*z/b);
 +		else
 +		    b = (t*w/a);
  	    }
  	}
  	if(sgn==1) return -b; else return b;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: arundel 
State-Changed-When: Sat Nov 13 21:39:51 UTC 2010 
State-Changed-Why:  
Patched in HEAD (r215237). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=144306 
State-Changed-From-To: patched->closed 
State-Changed-By: uqs 
State-Changed-When: Tue Nov 23 19:15:42 UTC 2010 
State-Changed-Why:  
Fixes merged to stable/7 and stable/8, thanks for your submission! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=144306 
>Unformatted:
