🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.freebsd.bugs
Article #623

[Bug 300884] [libm] What to do about Bessel functions.

#623
From: bugzilla-noreply
Date: Sat, 21 Nov 2026 19:26
64 lines
4413 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300884

--- Comment #10 from Steve Kargl <kargl@FreeBSD.org> ---
Created attachment 277540
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=277540&action=edit
Update/fix handling of tiny, finite values of x

This is the first functional change to jn(x) and jnf(x).  For tiny
x, the cylindrical Bessel function is approximated by the first
term in its Taylor series representation, jn(x) = (1/n!)*(x/2)^n,
where n! is a factorial and (x/2)^n is (x/2) raised to the nth power.  

Prior to this patch, magic numbers are used with no explanation for
their choice.  Here's the code

if (ix < 0x3e100000) {          /* x < 2**-29 */
   if (n > 33)     /* underflow */
      b = zero;
   else {
      temp = x * 0.5;
      b = temp;
      for (a = one, i = 2; i <= n; i++) {
         a *= i;        /* a = n! */
         b *= temp;     /* b = (x/2)^n */
      }
      b = b / a;
   }
} else

First, 2**-29 is likely selected as a threshold because a two term
approximation is jn(x) = (1/n!)*(x/2)^n * [1 - (x/2)^2/(n+1)].  With
0x1p-29, the 2nd term in square brackets is 0x1p-60/(n+1) and we
have n >= 2 here.  This term is less than DBL_EPSILON, so [...] = 1.
This choice is quite conservative, and could have been 0x1p-25.
Using the two term approximation allows for 0x1p-23 (if not a
bit larger).

Second, 33 seems to come -1024/(-29) ~ 35, that is (0x1p-29)^33
underflows when one considers the presence of the factorial.  But,
the shortcut for n > 33 sets b to zero without raising FE_UNDERFLOW.

Third, the for-loops executes n-2 times, which may unnecessarily
compute zero multiply times.  Consider x = 0x1p-600 and n = 20.
The first iteration underflows to zero; yet, another 17 iterations
are computed.

Fourth, accumulating the factorial in 'a' and the powers of (x/2)
in b is not needed.

Fifth, when the double precision jn(x) was converted the float jnf(x),
the magic numbers were left unchanged.  Given that FLT_TRUE_MIN 
is 0x1p-149, so with 0x1p-29 at most 5 iterations are needed before
underflow occurs.  The magic number 33 is unneeded, and removed by
the patch.

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID: <bug-300884-227-MsCbWdoixp@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
References: <bug-300884-227@https.bugs.freebsd.org/bugzilla/>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: bugzilla-noreply@freebsd.org
Newsgroups: muc.lists.freebsd.bugs
Subject: [Bug 300884] [libm] What to do about Bessel functions.
Date: Sat, 21 Nov 2026 19:26:03 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-300884-227-MsCbWdoixp@https.bugs.freebsd.org/bugzilla/>
References: <bug-300884-227@https.bugs.freebsd.org/bugzilla/>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="10107"; mail-complaints-to="news-admin@muc.de"
X-Original-To: bugs@mlmmj.nyi.freebsd.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
	t=1795289163;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=t4u4b8BJl69XgKVDhHudKUYJy1ZxH/C+MVX1fOhm2YQ=;
	b=TCfwIkYVsSgW8htbRrAheWpzRl4mOXoltXZsS5zl/H2irl6u1rwEfQjw+alsYS62ba6g2H
	/yQbXZI7lENw2/6cF9tVxFJxDpzUWQIH2OtOUrJPGYIYZqsQp7hF+5G0DRX7WJJXnlCBfh
	qKJZjo1F1gVjYy9d9jZNJXkShOxOa6FkSWuW79wBDTNbB/z0z7hcw71PDcOmOnaPg+7v9R
	xY3hFuaMaz0XksXY4nCHDCSFGPDLz75qW1KMpLK70taDzD/x4sQynl+9eMkrvCd6kgVwZI
	v9OPq2a4qN7N1pRNNsdvlUfGmWWg7snHQ6uhJ3GqqAY2rv2fshQM/Pd0uXNHGQ==
X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f
In-Reply-To: <bug-300884-227@https.bugs.freebsd.org/bugzilla/>
Auto-Submitted: auto-generated
Precedence: list
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes