Skip to content

Add math.cbrt() and math.exp2() - #1271

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/math-exp2-cbrt
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/math-exp2-cbrt

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Fixes #1260

math.cbrt and math.exp2, added in Python 3.11, were missing from the math module. CPython defines both as math_1 wrappers over libm, with overflow checking for exp2 only (mathmodule.c#L1113-L1115, #L1174-L1176).

AS-IS

>>> math.cbrt(8)
AttributeError: module 'math' has no attribute 'cbrt'
>>> math.exp2(3)
AttributeError: module 'math' has no attribute 'exp2'

TO-BE

>>> math.cbrt(8)
2.0
>>> math.exp2(3)
8.0
>>> math.exp2(1024)
OverflowError: math range error

(matching CPython)

Changes

  • Add CbrtNode, returning Math.cbrt(value).
  • Add Exp2Node, returning Math.pow(2.0, value) with the same range check as ExpNode.
  • Enable test.test_math.MathTests.testCbrt and testExp2 in unittest_tags/test_math.txt.

Testing

mx python-jvm on darwin-aarch64, then --tagged test_math (81 passed; master passes 79). Eighteen cbrt and exp2 calls, including infinities, NaN, -0.0 and the overflow case, return the same values as CPython 3.13.14.

Both functions were added to the math module in Python 3.11 and were
missing, so math.cbrt and math.exp2 raised AttributeError. cbrt uses
Math.cbrt; exp2 uses Math.pow(2.0, x) and raises OverflowError on an
infinite result from a finite argument, as exp does. Enable CPython's
testCbrt and testExp2, which now pass.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

1 participant