{"record":{"id":"01842d97126f5995","repo":"jax-ml/jax","slug":"dtype-argument-to-f-must-be-a-float-dtype-got","errorCode":null,"errorMessage":"dtype argument to `f` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `f` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":2770,"sourceCode":"      jax_enable_x64 is true, otherwise float32).\n    out_sharding: optional, specifies how the output array should be sharded\n      across devices in multi-device computation. Can be a\n      :class:`~jax.sharding.NamedSharding`, a :class:`~jax.sharding.PartitionSpec`\n      (``P``), or ``None`` (default). When specified, the output will be sharded\n      according to the given sharding specification. Primarily used in explicit\n      sharding mode.\n      See the `explicit sharding tutorial <https://docs.jax.dev/en/latest/parallel.html>`_\n      for more details.\n\n  Returns:\n    A random array with the specified dtype and with shape given by ``shape`` if\n    ``shape`` is not None, or else by ``df.shape``.\n  \"\"\"\n  key, _ = _check_prng_key(\"f\", key)\n  dtype = dtypes.check_and_canonicalize_user_dtype(\n      float if dtype is None else dtype)\n  if not dtypes.issubdtype(dtype, np.floating):\n    raise ValueError(\"dtype argument to `f` must be a float \"\n                     f\"dtype, got {dtype}\")\n  shape = _check_broadcast_shapes(\"f\", shape, dfnum, dfden)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"f\", shape)\n  _check_all_safe_to_cast(\"f\", dtype, dfnum, dfden)\n  return _f(key, dfnum, dfden, shape, dtype, out_sharding)\n\n@jit(static_argnums=(3, 4, 5))\ndef _f(key, dfnum, dfden, shape, dtype, out_sharding) -> Array:\n  dfden = lax.convert_element_type(dfden, dtype)\n  dfnum = lax.convert_element_type(dfnum, dtype)\n  key_dfd, key_dfn = _split(key)\n  chi2_dfn = chisquare(key_dfn, dfnum, shape, dtype, out_sharding=out_sharding)\n  chi2_dfd = chisquare(key_dfd, dfden, shape, dtype, out_sharding=out_sharding)\n  num = lax.div(chi2_dfn, dfnum)\n  den = lax.div(chi2_dfd ,dfden)\n  f = lax.div(num, den)\n  return f\n","sourceCodeStart":2752,"sourceCodeEnd":2788,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L2752-L2788","documentation":"jax.random.f (F-distribution) requires a floating-point dtype; the sampler combines two chi-square/gamma variates in float arithmetic. Non-float dtypes raise ValueError. The call also validates that shape broadcasts against both dfnum.shape and dfden.shape and that both cast safely to dtype.","triggerScenarios":"jax.random.f(key, dfnum, dfden, dtype=jnp.int32) or any dtype failing dtypes.issubdtype(dtype, np.floating).","commonSituations":"Statistical testing utilities with a configurable dtype; sharing dtype across samplers; passing degrees-of-freedom arrays whose float64 values must be safely castable to a narrower dtype.","solutions":["Pass jnp.float32/jnp.float64 or omit dtype.","Ensure shape broadcasts against both dfnum and dfden shapes (both are checked via _check_broadcast_shapes).","Validate configurable dtypes against np.floating and watch for the safe-cast check on dfnum/dfden."],"exampleFix":"// before\nx = jax.random.f(key, 5.0, 3.0, dtype=jnp.int32)\n\n// after\nx = jax.random.f(key, 5.0, 3.0, dtype=jnp.float32)","handlingStrategy":"type-guard","validationCode":"from jax._src import dtypes\nassert dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)","typeGuard":"def is_float_dtype(dtype) -> bool:\n    from jax._src import dtypes\n    import numpy as np\n    return dtypes.issubdtype(dtypes.check_and_canonicalize_user_dtype(dtype or float), np.floating)","tryCatchPattern":null,"preventionTips":["Also check shape broadcasts against both dfnum and dfden."],"tags":["jax","random","f-distribution","dtype","input-validation"],"backgroundTag":"invalid-dtype-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}