{"record":{"id":"963a3d35f65a49fc","repo":"jax-ml/jax","slug":"series-order-must-be-non-negative","errorCode":null,"errorMessage":"series_order must be non-negative.","messagePattern":"series_order must be non-negative\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/special.py","lineNumber":1668,"sourceCode":"\n\n  Args:\n    x: an array of type `float32`, `float64`.\n    series_order: Positive Python integer. Maximum depth to\n      evaluate the asymptotic expansion. This is the `N` above.\n\n  Returns:\n    an array with `dtype=x.dtype`.\n\n  Raises:\n    TypeError: if `x.dtype` is not handled.\n    TypeError: if `series_order` is a not Python `integer.`\n    ValueError:  if `series_order` is not in `[0, 30]`.\n  \"\"\"\n  if not isinstance(series_order, int):\n    raise TypeError(\"series_order must be a Python integer.\")\n  if series_order < 0:\n    raise ValueError(\"series_order must be non-negative.\")\n  if series_order > 30:\n    raise ValueError(\"series_order must be <= 30.\")\n\n  x_arr = jnp.asarray(x)\n  dtype = lax.dtype(x_arr)\n\n  if dtype == np.float64:\n    lower_segment: np.ndarray = _LOGNDTR_FLOAT64_LOWER\n    upper_segment: np.ndarray = _LOGNDTR_FLOAT64_UPPER\n  elif dtype == np.float32:\n    lower_segment = _LOGNDTR_FLOAT32_LOWER\n    upper_segment = _LOGNDTR_FLOAT32_UPPER\n  else:\n    raise TypeError(f\"x.dtype={np.dtype(dtype)} is not supported.\")\n\n  # The basic idea here was ported from:\n  #   https://root.cern.ch/doc/v608/SpecFuncCephesInv_8cxx_source.html\n  # We copy the main idea, with a few changes","sourceCodeStart":1650,"sourceCodeEnd":1686,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/special.py#L1650-L1686","documentation":"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.","triggerScenarios":"Calling log_ndtr(x, series_order=-1) or with a computed negative order (e.g. order = n - k where the expression goes negative).","commonSituations":"Off-by-one bugs computing order from array lengths or loop indices; configs with negative defaults; sweeping order with range that starts below zero.","solutions":["Clamp: series_order = max(0, int(order))","Fix the arithmetic that produced the negative value (off-by-one in len(x)-style expressions)","Add a unit test asserting 0 <= series_order <= 30 for all configs"],"exampleFix":"// before\njax.scipy.special.log_ndtr(x, series_order=k-1)  # k=0 -> -1\n// after\njax.scipy.special.log_ndtr(x, series_order=max(0, k-1))","handlingStrategy":"validation","validationCode":"series_order = max(0, int(series_order))","typeGuard":"def valid_order(o):\n    return isinstance(o, int) and 0 <= o <= 30","tryCatchPattern":null,"preventionTips":["Clamp computed orders to [0, 30]","Add boundary tests for order-arithmetic expressions (k-1, n-k)"],"tags":["jax","scipy-special","log-ndtr","argument-validation"],"backgroundTag":"out-of-range-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}