jax-ml/jax · error · ValueError
Arguments to jax.numpy.gcd must be integers.
Error message
Arguments to jax.numpy.gcd must be integers.
What it means
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.
Source
Thrown at jax/_src/numpy/lax_numpy.py:8861
Array inputs:
>>> x1 = jnp.array([12, 18, 24])
>>> x2 = jnp.array([5, 10, 15])
>>> jnp.gcd(x1, x2)
Array([1, 2, 3], dtype=int32)
Broadcasting:
>>> x1 = jnp.array([12])
>>> x2 = jnp.array([6, 9, 12])
>>> jnp.gcd(x1, x2)
Array([ 6, 3, 12], dtype=int32)
"""
x1, x2 = util.ensure_arraylike("gcd", x1, x2)
x1, x2 = util.promote_dtypes(x1, x2)
if not issubdtype(x1.dtype, np.integer):
raise ValueError("Arguments to jax.numpy.gcd must be integers.")
x1, x2 = broadcast_arrays(x1, x2)
gcd, _ = control_flow.while_loop(_gcd_cond_fn, _gcd_body_fn, (ufuncs.abs(x1), ufuncs.abs(x2)))
return gcd
@export
@api.jit
def lcm(x1: ArrayLike, x2: ArrayLike) -> Array:
"""Compute the least common multiple of two arrays.
JAX implementation of :func:`numpy.lcm`.
Args:
x1: First input array. The elements must have integer dtype.
x2: Second input array. The elements must have integer dtype.
Returns:
An array containing the least common multiple of the correspondingView on GitHub (pinned to 1e1c6a8fc0)
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)
Example fix
// before jnp.gcd(6.0, 4.0) # ValueError // after jnp.gcd(jnp.asarray(6.0, dtype=jnp.int32), jnp.asarray(4.0, dtype=jnp.int32))
Defensive patterns
Strategy: validation
Validate before calling
def as_int_pair(x1, x2):
x1, x2 = jnp.asarray(x1), jnp.asarray(x2)
if not jnp.issubdtype(jnp.result_type(x1, x2), jnp.integer):
x1, x2 = x1.astype(jnp.int32), x2.astype(jnp.int32)
return x1, x2
x1, x2 = as_int_pair(x1, x2)
jnp.gcd(x1, x2) Type guard
def is_integer_array(x) -> bool:
return jnp.issubdtype(jnp.asarray(x).dtype, jnp.integer) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Arguments to jax.numpy.lcm must be integers.
- dtype argument to jnp.std must be inexact; got {dtype}
- len() of unsized object
- numpy masked arrays are not supported as direct inputs to JA
- Unsupported scalar attribute type: {type(val)}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/6a94fc2631d4978b.
Report an issue: GitHub.