jax icon indicating copy to clipboard operation
jax copied to clipboard

`jax.scipy.special.zeta` returns zero after intermediate underflow when the result is representable

Open the-Shallow opened this issue 1 month ago • 4 comments

Description

jax.scipy.special.zeta returns exactly zero for sufficiently large q, even when the mathematical result is representable in the requested floating-point type. For example, the float32 result of zeta(4, 3037000576) should be approximately 1.19e-29, which is well above the smallest normal float32 value (1.175e-38), but JAX returns zero.

The same behavior affects jax.scipy.special.polygamma, which is implemented in terms of zeta. With 64-bit values enabled, zeta(4, 1e77) also returns zero although the expected result is about 3.33e-232, which is representable in float64.

Steps to reproduce

import jax
import numpy as np
import scipy.special as scipy_special
from jax.scipy.special import polygamma, zeta

x, q = np.float32(4.0), np.float32(3037000576.0)
print(float(zeta(x, q)))
print(scipy_special.zeta(float(x), float(q)))
print(np.finfo(np.float32).tiny)

print(float(polygamma(9, np.float32(1e4))))
print(scipy_special.polygamma(9, 1e4))

jax.config.update("jax_enable_x64", True)
print(float(zeta(4.0, 1e77)))
print(scipy_special.zeta(4.0, 1e77))

Expected behavior

zeta and downstream polygamma calls should retain representable nonzero results rather than underflowing prematurely to zero.

System info (python version, jaxlib version, accelerator, etc.)

System info

JAX / jaxlib 0.11.2 (latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64

the-Shallow avatar Oct 08 '26 03:10 the-Shallow

Thanks for the report. Note a number of related reports on the numerics of zeta at edge-cases:

  • #38199
  • #38271
  • #38637

jakevdp avatar Oct 08 '26 18:10 jakevdp

(cross-listing under #38651)

jakevdp avatar Oct 08 '26 18:10 jakevdp

Numerical Root Cause Analysis

The premature underflow to zero comes from factoring out ^{-x}$ in the Euler-Maclaurin expansion of the Hurwitz zeta function:

1737\zeta(x, q) = q^{-x} \left( \frac{q}{x - 1} + \frac{1}{2} + \frac{x}{12 q} - \frac{x(x+1)(x+2)}{720 q^3} + \dots \right)1737

Notice that while the true leading term is: 1737\frac{q^{1-x}}{x - 1} = \frac{q^{-(x-1)}}{x - 1}1737 factoring out ^{-x}$ evaluates ^{-x}$ directly as an intermediate sub-expression before multiplying by $\left( \frac{q}{x - 1} + \dots \right)$.

Whenever ^{-x} < \text{finfo}(\text{dtype}).\text{tiny}$ (the minimum representable normal float), ^{-x}$ underflows to zsh.0$, zeroing out the entire product:

  1. In float32: For = 4$ and = 3037000576.0$:

    • $\text{finfo}(\text{float32}).\text{tiny} \approx 1.1754944 \times 10^{-38}$
    • Intermediate ^{-4} \approx 1.1754941 \times 10^{-38} \to 0.0$ (subnormal/underflow)
    • But true leading term $\frac{q^{-3}}{3} \approx 1.18999 \times 10^{-29}$, which is over 9 orders of magnitude above the float32 underflow threshold!
  2. In float64: For = 4$ and = 10^{77}$:

    • $\text{finfo}(\text{float64}).\text{tiny} \approx 2.225 \times 10^{-308}$
    • Intermediate ^{-4} = 10^{-308} \to 0.0$ (underflow)
    • But true leading term $\frac{q^{-3}}{3} \approx 3.33 \times 10^{-232}$, which is 76 orders of magnitude above the float64 underflow threshold!

Suggested Resolution

In the large-$ asymptotic branch, avoid factoring ^{-x}$ across the first term $\frac{q^{1-x}}{x - 1}$. Specifically, evaluating: 1737\zeta(x, q) \approx \frac{q^{1-x}}{x - 1} + q^{-x} \left( \frac{1}{2} + \frac{x}{12 q} - \dots \right)1737 allows the leading term to remain representable whenever $\zeta(x, q)$ itself is representable.

hrideymarwah15 avatar Oct 10 '26 21:10 hrideymarwah15

Should be fixed by https://github.com/tensorflow/tensorflow/pull/129009 (with regression test in https://github.com/jax-ml/jax/pull/41652)

This should be part of the next JAX release, in v0.12.1.

jakevdp avatar Oct 19 '26 19:10 jakevdp