{"record":{"id":"ffb878700b6980bc","repo":"jax-ml/jax","slug":"dtype-argument-to-maxwell-must-be-a-float-dtype","errorCode":null,"errorMessage":"dtype argument to `maxwell` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `maxwell` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":2875,"sourceCode":"      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 jnp.array of samples, of shape `shape`.\n\n  \"\"\"\n  # Generate samples using:\n  # sqrt(X^2 + Y^2 + Z^2), X,Y,Z ~N(0,1)\n  key, _ = _check_prng_key(\"maxwell\", 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(f\"dtype argument to `maxwell` must be a float \"\n                     f\"dtype, got {dtype}\")\n  shape = core.canonicalize_shape(shape)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"maxwell\", shape)\n  return _maxwell(key, shape, dtype, out_sharding)\n\n\n@jit(static_argnums=(1, 2, 3))\ndef _maxwell(key, shape, dtype, out_sharding) -> Array:\n  shape = shape + (3,)\n  if out_sharding is not None:\n    new_partitions = (*out_sharding.spec, None)\n    out_sharding = out_sharding.update(\n        spec=out_sharding.spec.update(partitions=new_partitions))\n  norm_rvs = normal(key=key, shape=shape, dtype=dtype, out_sharding=out_sharding)\n  return jnp_linalg.norm(norm_rvs, axis=-1)\n\n\ndef double_sided_maxwell(key: ArrayLike,","sourceCodeStart":2857,"sourceCodeEnd":2893,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L2857-L2893","documentation":"jax.random.maxwell (Maxwell-Boltzmann speed distribution) requires a floating-point dtype; it generates three standard normal components and takes the norm sqrt(X^2+Y^2+Z^2), which is float-only math. Integer or complex dtypes raise ValueError.","triggerScenarios":"jax.random.maxwell(key, shape, dtype=jnp.int32) or any dtype where dtypes.issubdtype(dtype, np.floating) is False.","commonSituations":"Molecular-dvelocity initialization scripts with a shared dtype constant; assuming a default int dtype; debugging configs that hard-code int32 globally.","solutions":["Pass jnp.float32/jnp.float64 or omit dtype (defaults to float).","If integer speeds are truly needed, sample float then cast afterwards: jax.random.maxwell(key, shape).astype(jnp.int32).","Validate configurable dtypes against np.floating before calling."],"exampleFix":"// before\nv = jax.random.maxwell(key, (1000, 3), dtype=jnp.int32)\n\n// after\nv = jax.random.maxwell(key, (1000, 3), 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":["Sample float speeds; cast to int afterwards if needed."],"tags":["jax","random","maxwell","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"}