{"record":{"id":"a43179455f5df75f","repo":"jax-ml/jax","slug":"integers-cannot-be-raised-to-negative-powers-got","errorCode":null,"errorMessage":"Integers cannot be raised to negative powers, got integer_pow({x}, {y})","messagePattern":"Integers cannot be raised to negative powers, got integer_pow\\((.+?), (.+?)\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":4915,"sourceCode":"  y_dtype = dtypes.dtype(y)\n  assert dtypes.issubdtype(y_dtype, np.inexact)\n  return convert_element_type(mul(g, mul(log(_replace_zero(x)), ans)), y_dtype)\nad.defjvp2(pow_p, _pow_jvp_lhs, _pow_jvp_rhs)\n\ndef _pow_lower(ctx, x, y):\n  x_aval, y_aval = ctx.avals_in\n  if x_aval.dtype != y_aval.dtype:\n    out_aval, = ctx.avals_out\n    y_aval = y_aval.update(dtype=out_aval.dtype)\n    y = hlo.convert(mlir.aval_to_ir_type(ctx.module_context, y_aval), y)\n    ctx = ctx.replace(avals_in=[x_aval, y_aval])\n  return _nary_lower_hlo(hlo.power, ctx, x, y)\nmlir.register_lowering(pow_p, _pow_lower)\n\ndef _integer_pow_dtype_rule(x, *, y):\n  dtype = unop_dtype_rule(_identity, _int | _float | _complex, 'integer_pow', x)\n  if y < 0 and dtypes.issubdtype(dtype, np.integer):\n    raise TypeError(\"Integers cannot be raised to negative powers, got \"\n                    f\"integer_pow({x}, {y})\")\n  return dtype\n\ndef _integer_pow_jvp(g, x, *, y):\n  if y == 0:\n    return _zeros(g)\n  if y == 1:\n    return g\n  if y == 2:\n    return mul(g, mul(_const(x, y), x))\n  return mul(g, mul(_const(x, y), integer_pow(x, y - 1)))\n\ninteger_pow_p = standard_primitive(\n  _attrgetter('shape'), _integer_pow_dtype_rule, 'integer_pow',\n  sharding_rule=_attrgetter('sharding'), vma_rule=lambda x, **_: x.mat.varying)\nbatching.defvectorized(integer_pow_p)\nad.defjvp(integer_pow_p, _integer_pow_jvp)\n","sourceCodeStart":4897,"sourceCodeEnd":4933,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L4897-L4933","documentation":"lax.integer_pow implements x**y for integer bases with a compile-time integer exponent y. A negative exponent on integers would require fractional results, so its dtype rule raises TypeError immediately.","triggerScenarios":"x ** -1 or x ** -2 on integer arrays (which lower to integer_pow), e.g. jnp.asarray(2) ** -1, or int32 tensors raised to -1 for reciprocal computation.","commonSituations":"Writing 1/x as x**-1 on integer tensors; reciprocal/inverse computations on counts or indices; exponents computed as -k where k positive on int inputs.","solutions":["Cast the base to float first: x.astype(jnp.float32) ** -1","Use explicit reciprocals: 1.0 / x (which promotes) or jnp.reciprocal(x.astype(float))","Keep integer exponents non-negative for integer bases"],"exampleFix":"// before\ninv = int_array ** -1\n// after\ninv = int_array.astype(jnp.float32) ** -1\n# or: inv = 1.0 / int_array","handlingStrategy":"validation","validationCode":"if y < 0 and jnp.issubdtype(x.dtype, jnp.integer):\n    x = x.astype(jnp.float32)\nout = x ** y","typeGuard":"def safe_integer_pow(x, y) -> bool:\n    import numpy as np\n    return not (y < 0 and np.issubdtype(x.dtype, np.integer))","tryCatchPattern":null,"preventionTips":["Write 1.0/x instead of x**-1","Cast integer tensors to float before reciprocal-style math"],"tags":["jax","lax","integer-pow","negative-exponent","type-error"],"backgroundTag":"negative-power-of-integer","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}