{"record":{"id":"d2e24f4043433e3c","repo":"jax-ml/jax","slug":"invalid-value-e-ty-encountered-in-ndtri","errorCode":null,"errorMessage":"invalid value ({e.ty}) encountered in ndtri.","messagePattern":"invalid value \\((.+?)\\) encountered in ndtri\\.","errorType":"exception","errorClass":"FloatingPointError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/special.py","lineNumber":1597,"sourceCode":"  second_term_small_p = jnp.polyval(p2, 1 / z) / jnp.polyval(q2, 1 / z) / z\n  second_term_otherwise = jnp.polyval(p1, 1 / z) / jnp.polyval(q1, 1 / z) / z\n  x_for_small_p = first_term - second_term_small_p\n  x_otherwise = first_term - second_term_otherwise\n\n  x = jnp.where(sanitized_mcp > dtype(np.exp(-2.)),\n                x_for_big_p,\n                jnp.where(z >= dtype(8.0), x_for_small_p, x_otherwise))\n\n  x = jnp.where(p > dtype(1. - np.exp(-2.)), x, -x)\n  with config.debug_infs(False):\n    infinity = jnp.full(shape, dtype(np.inf))\n    x = jnp.where(\n        p == dtype(0.0), -infinity, jnp.where(p == dtype(1.0), infinity, x))\n  if not isinstance(x, core.Tracer):\n    try:\n      dispatch.check_special(\"ndtri\", [x])\n    except api_util.InternalFloatingPointError as e:\n      raise FloatingPointError(\n          f\"invalid value ({e.ty}) encountered in ndtri.\") from None\n  return x\n\n\n@partial(custom_derivatives.custom_jvp, nondiff_argnums=(1,))\ndef log_ndtr(x: ArrayLike, series_order: int = 3) -> Array:\n  r\"\"\"Log Normal distribution function.\n\n  JAX implementation of :obj:`scipy.special.log_ndtr`.\n\n  For details of the Normal distribution function see `ndtr`.\n\n  This function calculates :math:`\\log(\\mathrm{ndtr}(x))` by either calling\n  :math:`\\log(\\mathrm{ndtr}(x))` or using an asymptotic series. Specifically:\n\n  - For `x > upper_segment`, use the approximation `-ndtr(-x)` based on\n    :math:`\\log(1-x) \\approx -x, x \\ll 1`.\n  - For `lower_segment < x <= upper_segment`, use the existing `ndtr` technique","sourceCodeStart":1579,"sourceCodeEnd":1615,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/special.py#L1579-L1615","documentation":"After computing the inverse normal CDF, _ndtri runs dispatch.check_special to detect invalid values (NaN etc.) produced during the computation. If an internal floating point error of type e.ty (e.g. invalid) is found, it is re-raised as a user-facing FloatingPointError. This only happens for concrete (non-traced) arrays.","triggerScenarios":"Calling jax.scipy.special.ndtri(p) eagerly (outside jit) with p containing NaN, or values that cause the rational approximations to produce NaN (e.g. wrong-dtype edge cases or p far outside [0,1] due to upstream bugs).","commonSituations":"NaN probabilities leaking from a model (log of negative, 0/0) then passed to ndtri; eager debugging sessions where NaN checks are active but jit hides them; jax_debug_nan or debug configurations surfacing hidden NaNs.","solutions":["Sanitize p before calling: replace NaNs, e.g. p = jnp.where(jnp.isnan(p), 0.5, p), and clip to [0,1] with jnp.clip(p, 0., 1.)","Find the upstream NaN source: check p with jnp.isnan(p).any() and print before the call","Wrap in jax.jit if you must defer NaN checking, but treat that as masking, not fixing"],"exampleFix":"// before\nq = jax.scipy.special.ndtri(p)  # p has NaNs\n// after\np = jnp.where(jnp.isnan(p), 0.5, jnp.clip(p, 0.0, 1.0))\nq = jax.scipy.special.ndtri(p)","handlingStrategy":"validation","validationCode":"p = jnp.asarray(p)\nassert not jnp.isnan(p).any(), 'NaN in ndtri input'\np = jnp.clip(jnp.where(jnp.isnan(p), 0.5, p), 0.0, 1.0)","typeGuard":"def no_nans(p):\n    return not bool(jnp.isnan(p).any())","tryCatchPattern":"try:\n    q = jax.scipy.special.ndtri(p)\nexcept FloatingPointError:\n    p = jnp.nan_to_num(p, nan=0.5)\n    q = jax.scipy.special.ndtri(p)","preventionTips":["Sanitize and clip probabilities before quantile functions","Investigate upstream NaN sources (log of <=0, 0/0) instead of masking with jit"],"tags":["jax","scipy-special","nan","ndtri","floating-point"],"backgroundTag":"nan-in-input","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}