jax-ml/jax · error · ValueError

series_order must be non-negative.

Error message

series_order must be non-negative.

What it means

log_ndtr's series_order must be >= 0. A negative order is meaningless for the asymptotic series used for the lower tail, so the public function validates it and raises ValueError before doing any work.

Source

Thrown at jax/_src/scipy/special.py:1668


  Args:
    x: an array of type `float32`, `float64`.
    series_order: Positive Python integer. Maximum depth to
      evaluate the asymptotic expansion. This is the `N` above.

  Returns:
    an array with `dtype=x.dtype`.

  Raises:
    TypeError: if `x.dtype` is not handled.
    TypeError: if `series_order` is a not Python `integer.`
    ValueError:  if `series_order` is not in `[0, 30]`.
  """
  if not isinstance(series_order, int):
    raise TypeError("series_order must be a Python integer.")
  if series_order < 0:
    raise ValueError("series_order must be non-negative.")
  if series_order > 30:
    raise ValueError("series_order must be <= 30.")

  x_arr = jnp.asarray(x)
  dtype = lax.dtype(x_arr)

  if dtype == np.float64:
    lower_segment: np.ndarray = _LOGNDTR_FLOAT64_LOWER
    upper_segment: np.ndarray = _LOGNDTR_FLOAT64_UPPER
  elif dtype == np.float32:
    lower_segment = _LOGNDTR_FLOAT32_LOWER
    upper_segment = _LOGNDTR_FLOAT32_UPPER
  else:
    raise TypeError(f"x.dtype={np.dtype(dtype)} is not supported.")

  # The basic idea here was ported from:
  #   https://root.cern.ch/doc/v608/SpecFuncCephesInv_8cxx_source.html
  # We copy the main idea, with a few changes

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Clamp: series_order = max(0, int(order))
  2. Fix the arithmetic that produced the negative value (off-by-one in len(x)-style expressions)
  3. Add a unit test asserting 0 <= series_order <= 30 for all configs

Example fix

// before
jax.scipy.special.log_ndtr(x, series_order=k-1)  # k=0 -> -1
// after
jax.scipy.special.log_ndtr(x, series_order=max(0, k-1))
Defensive patterns

Strategy: validation

Validate before calling

series_order = max(0, int(series_order))

Type guard

def valid_order(o):
    return isinstance(o, int) and 0 <= o <= 30

Prevention

When it happens

Trigger: Calling log_ndtr(x, series_order=-1) or with a computed negative order (e.g. order = n - k where the expression goes negative).

Common situations: Off-by-one bugs computing order from array lengths or loop indices; configs with negative defaults; sweeping order with range that starts below zero.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/963a3d35f65a49fc. Report an issue: GitHub.