{"record":{"id":"6a94fc2631d4978b","repo":"jax-ml/jax","slug":"arguments-to-jax-numpy-gcd-must-be-integers","errorCode":null,"errorMessage":"Arguments to jax.numpy.gcd must be integers.","messagePattern":"Arguments to jax\\.numpy\\.gcd must be integers\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":8861,"sourceCode":"\n    Array inputs:\n\n    >>> x1 = jnp.array([12, 18, 24])\n    >>> x2 = jnp.array([5, 10, 15])\n    >>> jnp.gcd(x1, x2)\n    Array([1, 2, 3], dtype=int32)\n\n    Broadcasting:\n\n    >>> x1 = jnp.array([12])\n    >>> x2 = jnp.array([6, 9, 12])\n    >>> jnp.gcd(x1, x2)\n    Array([ 6,  3, 12], dtype=int32)\n  \"\"\"\n  x1, x2 = util.ensure_arraylike(\"gcd\", x1, x2)\n  x1, x2 = util.promote_dtypes(x1, x2)\n  if not issubdtype(x1.dtype, np.integer):\n    raise ValueError(\"Arguments to jax.numpy.gcd must be integers.\")\n  x1, x2 = broadcast_arrays(x1, x2)\n  gcd, _ = control_flow.while_loop(_gcd_cond_fn, _gcd_body_fn, (ufuncs.abs(x1), ufuncs.abs(x2)))\n  return gcd\n\n\n@export\n@api.jit\ndef lcm(x1: ArrayLike, x2: ArrayLike) -> Array:\n  \"\"\"Compute the least common multiple of two arrays.\n\n  JAX implementation of :func:`numpy.lcm`.\n\n  Args:\n    x1: First input array. The elements must have integer dtype.\n    x2: Second input array. The elements must have integer dtype.\n\n  Returns:\n    An array containing the least common multiple of the corresponding","sourceCodeStart":8843,"sourceCodeEnd":8879,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L8843-L8879","documentation":"jax.numpy.gcd computes the greatest common divisor elementwise and only accepts integer-dtyped inputs. After converting arguments to arrays and promoting dtypes, it checks that the common dtype is a subclass of np.integer and raises ValueError otherwise. This mirrors NumPy's requirement that gcd operands be integers.","triggerScenarios":"Calling jnp.gcd(x1, x2) where either argument is float/complex/bool-promoted-to-float, e.g. jnp.gcd(6.0, 4.0) or arrays with dtype float32. Promotion of mixed int/float inputs yields a float dtype, failing the check.","commonSituations":"Passing Python floats or float arrays computed from division/mean operations; loading data with np.loadtxt (float64 default) then calling gcd; mixing an int array with a Python float scalar.","solutions":["Cast inputs to an integer dtype before calling: jnp.gcd(x1.astype(jnp.int32), x2.astype(jnp.int32))","Ensure upstream computations don't produce floats (e.g. replace / with // where integer results are expected)","Check dtypes beforehand with jnp.issubdtype(x.dtype, jnp.integer)"],"exampleFix":"// before\njnp.gcd(6.0, 4.0)  # ValueError\n// after\njnp.gcd(jnp.asarray(6.0, dtype=jnp.int32), jnp.asarray(4.0, dtype=jnp.int32))","handlingStrategy":"validation","validationCode":"def as_int_pair(x1, x2):\n    x1, x2 = jnp.asarray(x1), jnp.asarray(x2)\n    if not jnp.issubdtype(jnp.result_type(x1, x2), jnp.integer):\n        x1, x2 = x1.astype(jnp.int32), x2.astype(jnp.int32)\n    return x1, x2\nx1, x2 = as_int_pair(x1, x2)\njnp.gcd(x1, x2)","typeGuard":"def is_integer_array(x) -> bool:\n    return jnp.issubdtype(jnp.asarray(x).dtype, jnp.integer)","tryCatchPattern":null,"preventionTips":["Keep gcd/lcm inputs in integer dtypes end-to-end","Use // instead of / when integer results are intended","Check jnp.result_type(x1, x2) is integer before calling"],"tags":["jax","numpy","dtype-validation","integer-required"],"backgroundTag":"dtype-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}