{"record":{"id":"5836579496f92f59","repo":"jax-ml/jax","slug":"cov-dtype-must-be-a-subclass-of-float-or-complex","errorCode":null,"errorMessage":"cov: dtype must be a subclass of float or complex; got {dtype=}","messagePattern":"cov: dtype must be a subclass of float or complex; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9188,"sourceCode":"    >>> x = jax.random.normal(key, shape=(3, 100))\n    >>> with jnp.printoptions(precision=2):\n    ...   print(jnp.cov(x))\n    [[0.9  0.03 0.1 ]\n     [0.03 1.   0.01]\n     [0.1  0.01 0.85]]\n  \"\"\"\n  if y is not None:\n    m, y = util.promote_args_inexact(\"cov\", m, y)\n    if y.ndim > 2:\n      raise ValueError(\"y has more than 2 dimensions\")\n  else:\n    m, = util.promote_args_inexact(\"cov\", m)\n\n  if m.ndim > 2:\n    raise ValueError(\"m has more than 2 dimensions\")  # same as numpy error\n\n  if dtype is not None and not dtypes.issubdtype(dtype, np.inexact):\n    raise ValueError(f\"cov: dtype must be a subclass of float or complex; got {dtype=}\")\n\n  X = atleast_2d(m)\n  if not rowvar and m.ndim != 1:\n    X = X.T\n  if X.shape[0] == 0:\n    return array([]).reshape(0, 0)\n\n  if y is not None:\n    y_arr = atleast_2d(y)\n    if not rowvar and y_arr.shape[0] != 1:\n      y_arr = y_arr.T\n    X = concatenate((X, y_arr), axis=0)\n  if X.shape[1] == 0:\n    cov_shape = () if X.shape[0] == 1 else (X.shape[0], X.shape[0])\n    return array_creation.full(cov_shape, np.nan, dtype=X.dtype)\n\n  if ddof is None:\n    ddof = 1 if bias == 0 else 0","sourceCodeStart":9170,"sourceCodeEnd":9206,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9170-L9206","documentation":"jnp.cov accepts an optional dtype parameter to control the result dtype, but it must be an inexact (float or complex) dtype since covariance is computed via floating-point averaging. If dtype is provided and not a subclass of np.inexact, a ValueError with the offending dtype is raised.","triggerScenarios":"jnp.cov(m, dtype=jnp.int32) or dtype=float-with-integer-numpy-type like np.dtype(int64); any integer or boolean dtype request.","commonSituations":"Users assuming dtype works like astype on arbitrary arrays; passing a config-driven dtype variable that may be int in some code paths.","solutions":["Omit dtype to use the default float promotion","Use a float dtype: dtype=jnp.float32 or jnp.float64","Validate dtype with jnp.issubdtype(dtype, jnp.inexact) before passing"],"exampleFix":"// before\njnp.cov(m, dtype=jnp.int32)  # ValueError\n// after\njnp.cov(m, dtype=jnp.float32)","handlingStrategy":"type-guard","validationCode":"if dtype is not None:\n    assert jnp.issubdtype(dtype, jnp.inexact), 'cov dtype must be float/complex'\njnp.cov(m, dtype=dtype)","typeGuard":"def is_inexact_dtype(d) -> bool:\n    return d is None or jnp.issubdtype(d, jnp.inexact)","tryCatchPattern":null,"preventionTips":["Only pass float/complex dtypes to cov","Default dtype is fine in most cases","Validate injected dtype variables"],"tags":["jax","covariance","dtype-validation"],"backgroundTag":"dtype-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}