{"record":{"id":"507420505044ac97","repo":"jax-ml/jax","slug":"dtype-argument-to-triangular-must-be-a-float-dty","errorCode":null,"errorMessage":"dtype argument to `triangular` must be a float dtype, got {dtype}","messagePattern":"dtype argument to `triangular` must be a float dtype, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/core.py","lineNumber":3400,"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 ``left.shape``, ``mode.shape`` and ``right.shape``.\n  \"\"\"\n  key, _ = _check_prng_key(\"triangular\", 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 `triangular` must be a float \"\n                     f\"dtype, got {dtype}\")\n  shape = _check_broadcast_shapes(\"triangular\", shape, left, mode, right)\n  out_sharding = canonicalize_sharding_for_samplers(out_sharding, \"triangular\", shape)\n  _check_all_safe_to_cast(\"triangular\", dtype, left, mode, right)\n  return maybe_auto_axes(_triangular, out_sharding, shape=shape, dtype=dtype)(key, left, mode, right)\n\n@jit(static_argnums=(4, 5), inline=True)\ndef _triangular(key, left, mode, right, shape, dtype) -> Array:\n  # https://en.wikipedia.org/wiki/Triangular_distribution#Generating_triangular-distributed_random_variates\n  left = jnp.broadcast_to(lax.convert_element_type(left, dtype), shape)\n  right = jnp.broadcast_to(lax.convert_element_type(right, dtype), shape)\n  mode = jnp.broadcast_to(lax.convert_element_type(mode, dtype), shape)\n  fc = (mode - left) / (right - left)\n  u = uniform(key, shape, dtype)\n  out1 = left + lax.sqrt(u * (right - left) * (mode - left))\n  out2 = right - lax.sqrt((1 - u) * (right - left) * (right - mode))\n  tri = lax.select(u < fc, out1, out2)\n  return tri","sourceCodeStart":3382,"sourceCodeEnd":3418,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/core.py#L3382-L3418","documentation":"jax.random.triangular requires a floating-point dtype because its output is continuous. The canonicalized dtype must satisfy dtypes.issubdtype(dtype, np.floating); integer, bool, or complex dtypes raise this ValueError.","triggerScenarios":"Calling jax.random.triangular(key, left, mode, right, shape, dtype) with a non-float dtype like np.int16 or jnp.complex64.","commonSituations":"Sharing one dtype config across a simulation that mixes discrete and continuous noise; passing bfloat16 works, but passing bool/int does not.","solutions":["Omit dtype or pass jnp.float32/np.float64","Ensure left/mode/right are float arrays so _check_all_safe_to_cast also passes"],"exampleFix":"// before\nt = jax.random.triangular(key, 0.0, 0.5, 1.0, dtype=jnp.int32)\n// after\nt = jax.random.triangular(key, 0.0, 0.5, 1.0, dtype=jnp.float32)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert dtype is None or np.issubdtype(np.dtype(dtype).type, np.floating), 'triangular needs float dtype'","typeGuard":"def is_float_dtype(d) -> bool:\n    import numpy as np\n    return d is None or np.issubdtype(np.dtype(d).type, np.floating)","tryCatchPattern":null,"preventionTips":["Keep left/mode/right as float arrays to also satisfy safe-cast checks"],"tags":["jax","random","dtype-validation"],"backgroundTag":"invalid-dtype-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}