Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ members = [
".",
"crates/*",
]
exclude = ["pymath"]

[workspace.package]
version = "0.4.0"
Expand Down Expand Up @@ -184,7 +185,7 @@ once_cell = "1.20.3"
parking_lot = "0.12.3"
paste = "1.0.15"
proc-macro2 = "1.0.105"
pymath = "0.0.2"
pymath = { version = "0.1.5", features = ["mul_add", "malachite-bigint", "complex"] }
quote = "1.0.43"
radium = "1.1.1"
rand = "0.9"
Expand Down
28 changes: 14 additions & 14 deletions Lib/test/mathdata/cmath_testcases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ acosh1002 acosh 0.0 inf -> inf 1.5707963267948966
acosh1003 acosh 2.3 inf -> inf 1.5707963267948966
acosh1004 acosh -0.0 inf -> inf 1.5707963267948966
acosh1005 acosh -2.3 inf -> inf 1.5707963267948966
acosh1006 acosh 0.0 nan -> nan nan
acosh1006 acosh 0.0 nan -> nan 1.5707963267948966 ignore-imag-sign
acosh1007 acosh 2.3 nan -> nan nan
acosh1008 acosh -0.0 nan -> nan nan
acosh1008 acosh -0.0 nan -> nan 1.5707963267948966 ignore-imag-sign
acosh1009 acosh -2.3 nan -> nan nan
acosh1010 acosh -inf 0.0 -> inf 3.1415926535897931
acosh1011 acosh -inf 2.3 -> inf 3.1415926535897931
Expand Down Expand Up @@ -1992,9 +1992,9 @@ tanh0065 tanh 1.797e+308 0.0 -> 1.0 0.0

--special values
tanh1000 tanh 0.0 0.0 -> 0.0 0.0
tanh1001 tanh 0.0 inf -> nan nan invalid
tanh1001 tanh 0.0 inf -> 0.0 nan invalid
tanh1002 tanh 2.3 inf -> nan nan invalid
tanh1003 tanh 0.0 nan -> nan nan
tanh1003 tanh 0.0 nan -> 0.0 nan
tanh1004 tanh 2.3 nan -> nan nan
tanh1005 tanh inf 0.0 -> 1.0 0.0
tanh1006 tanh inf 0.7 -> 1.0 0.0
Expand All @@ -2009,7 +2009,7 @@ tanh1014 tanh nan 2.3 -> nan nan
tanh1015 tanh nan inf -> nan nan
tanh1016 tanh nan nan -> nan nan
tanh1017 tanh 0.0 -0.0 -> 0.0 -0.0
tanh1018 tanh 0.0 -inf -> nan nan invalid
tanh1018 tanh 0.0 -inf -> 0.0 nan invalid
tanh1019 tanh 2.3 -inf -> nan nan invalid
tanh1020 tanh inf -0.0 -> 1.0 -0.0
tanh1021 tanh inf -0.7 -> 1.0 -0.0
Expand All @@ -2022,9 +2022,9 @@ tanh1027 tanh nan -0.0 -> nan -0.0
tanh1028 tanh nan -2.3 -> nan nan
tanh1029 tanh nan -inf -> nan nan
tanh1030 tanh -0.0 -0.0 -> -0.0 -0.0
tanh1031 tanh -0.0 -inf -> nan nan invalid
tanh1031 tanh -0.0 -inf -> -0.0 nan invalid
tanh1032 tanh -2.3 -inf -> nan nan invalid
tanh1033 tanh -0.0 nan -> nan nan
tanh1033 tanh -0.0 nan -> -0.0 nan
tanh1034 tanh -2.3 nan -> nan nan
tanh1035 tanh -inf -0.0 -> -1.0 -0.0
tanh1036 tanh -inf -0.7 -> -1.0 -0.0
Expand All @@ -2035,7 +2035,7 @@ tanh1040 tanh -inf -3.5 -> -1.0 -0.0
tanh1041 tanh -inf -inf -> -1.0 0.0 ignore-imag-sign
tanh1042 tanh -inf nan -> -1.0 0.0 ignore-imag-sign
tanh1043 tanh -0.0 0.0 -> -0.0 0.0
tanh1044 tanh -0.0 inf -> nan nan invalid
tanh1044 tanh -0.0 inf -> -0.0 nan invalid
tanh1045 tanh -2.3 inf -> nan nan invalid
tanh1046 tanh -inf 0.0 -> -1.0 0.0
tanh1047 tanh -inf 0.7 -> -1.0 0.0
Expand Down Expand Up @@ -2307,9 +2307,9 @@ tan0066 tan -8.79645943005142 0.0 -> 0.7265425280053614098 0.0

-- special values
tan1000 tan -0.0 0.0 -> -0.0 0.0
tan1001 tan -inf 0.0 -> nan nan invalid
tan1001 tan -inf 0.0 -> nan 0.0 invalid
tan1002 tan -inf 2.2999999999999998 -> nan nan invalid
tan1003 tan nan 0.0 -> nan nan
tan1003 tan nan 0.0 -> nan 0.0
tan1004 tan nan 2.2999999999999998 -> nan nan
tan1005 tan -0.0 inf -> -0.0 1.0
tan1006 tan -0.69999999999999996 inf -> -0.0 1.0
Expand All @@ -2324,7 +2324,7 @@ tan1014 tan -2.2999999999999998 nan -> nan nan
tan1015 tan -inf nan -> nan nan
tan1016 tan nan nan -> nan nan
tan1017 tan 0.0 0.0 -> 0.0 0.0
tan1018 tan inf 0.0 -> nan nan invalid
tan1018 tan inf 0.0 -> nan 0.0 invalid
tan1019 tan inf 2.2999999999999998 -> nan nan invalid
tan1020 tan 0.0 inf -> 0.0 1.0
tan1021 tan 0.69999999999999996 inf -> 0.0 1.0
Expand All @@ -2337,9 +2337,9 @@ tan1027 tan 0.0 nan -> 0.0 nan
tan1028 tan 2.2999999999999998 nan -> nan nan
tan1029 tan inf nan -> nan nan
tan1030 tan 0.0 -0.0 -> 0.0 -0.0
tan1031 tan inf -0.0 -> nan nan invalid
tan1031 tan inf -0.0 -> nan -0.0 invalid
tan1032 tan inf -2.2999999999999998 -> nan nan invalid
tan1033 tan nan -0.0 -> nan nan
tan1033 tan nan -0.0 -> nan -0.0
tan1034 tan nan -2.2999999999999998 -> nan nan
tan1035 tan 0.0 -inf -> 0.0 -1.0
tan1036 tan 0.69999999999999996 -inf -> 0.0 -1.0
Expand All @@ -2350,7 +2350,7 @@ tan1040 tan 3.5 -inf -> 0.0 -1.0
tan1041 tan inf -inf -> -0.0 -1.0 ignore-real-sign
tan1042 tan nan -inf -> -0.0 -1.0 ignore-real-sign
tan1043 tan -0.0 -0.0 -> -0.0 -0.0
tan1044 tan -inf -0.0 -> nan nan invalid
tan1044 tan -inf -0.0 -> nan -0.0 invalid
tan1045 tan -inf -2.2999999999999998 -> nan nan invalid
tan1046 tan -0.0 -inf -> -0.0 -1.0
tan1047 tan -0.69999999999999996 -inf -> -0.0 -1.0
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/mathdata/ieee754.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ inf
>>> 0 ** -1
Traceback (most recent call last):
...
ZeroDivisionError: 0.0 cannot be raised to a negative power
ZeroDivisionError: zero to a negative power
>>> pow(0, NAN)
nan

Expand All @@ -127,31 +127,31 @@ Trigonometric Functions
>>> sin(INF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got inf
>>> sin(NINF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got -inf
>>> sin(NAN)
nan
>>> cos(INF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got inf
>>> cos(NINF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got -inf
>>> cos(NAN)
nan
>>> tan(INF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got inf
>>> tan(NINF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a finite input, got -inf
>>> tan(NAN)
nan

Expand All @@ -169,11 +169,11 @@ True
>>> asin(INF), asin(NINF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a number in range from -1 up to 1, got inf
>>> acos(INF), acos(NINF)
Traceback (most recent call last):
...
ValueError: math domain error
ValueError: expected a number in range from -1 up to 1, got inf
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
(True, True)

Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_cmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ def test_cmath_matches_math(self):
self.rAssertAlmostEqual(math.log(v, base), z.real)
self.assertEqual(0., z.imag)

@unittest.expectedFailure # TODO: RUSTPYTHON
@requires_IEEE_754
def test_specific_values(self):
# Some tests need to be skipped on ancient OS X versions.
Expand Down Expand Up @@ -530,13 +529,11 @@ def testTanhSign(self):
# log1p function; If that system function doesn't respect the sign
# of zero, then atan and atanh will also have difficulties with
# the sign of complex zeros.
@unittest.expectedFailure # TODO: RUSTPYTHON
@requires_IEEE_754
def testAtanSign(self):
for z in complex_zeros:
self.assertComplexesAreIdentical(cmath.atan(z), z)

@unittest.expectedFailure # TODO: RUSTPYTHON
@requires_IEEE_754
def testAtanhSign(self):
for z in complex_zeros:
Expand Down Expand Up @@ -583,7 +580,6 @@ def test_complex_near_zero(self):
self.assertIsClose(0.001-0.001j, 0.001+0.001j, abs_tol=2e-03)
self.assertIsNotClose(0.001-0.001j, 0.001+0.001j, abs_tol=1e-03)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_complex_special(self):
self.assertIsNotClose(INF, INF*1j)
self.assertIsNotClose(INF*1j, INF)
Expand Down
64 changes: 62 additions & 2 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ def testFloor(self):
#self.assertEqual(math.ceil(NINF), NINF)
#self.assertTrue(math.isnan(math.floor(NAN)))

class TestFloorIsNone(float):
__floor__ = None
class TestFloor:
def __floor__(self):
return 42
Expand All @@ -588,6 +590,7 @@ class TestBadFloor:
self.assertEqual(math.floor(FloatLike(41.9)), 41)
self.assertRaises(TypeError, math.floor, TestNoFloor())
self.assertRaises(ValueError, math.floor, TestBadFloor())
self.assertRaises(TypeError, math.floor, TestFloorIsNone(3.5))

t = TestNoFloor()
t.__floor__ = lambda *args: args
Expand Down Expand Up @@ -1125,6 +1128,15 @@ def __index__(self):
with self.assertRaises(TypeError):
math.isqrt(value)

@support.bigmemtest(2**32, memuse=0.85)
def test_isqrt_huge(self, size):
if size & 1:
size += 1
v = 1 << size
w = math.isqrt(v)
self.assertEqual(w.bit_length(), size // 2 + 1)
self.assertEqual(w.bit_count(), 1)

def test_lcm(self):
lcm = math.lcm
self.assertEqual(lcm(0, 0), 0)
Expand Down Expand Up @@ -1272,6 +1284,13 @@ def testLog10(self):
self.assertEqual(math.log(INF), INF)
self.assertTrue(math.isnan(math.log10(NAN)))

@support.bigmemtest(2**32, memuse=0.2)
def test_log_huge_integer(self, size):
v = 1 << size
self.assertAlmostEqual(math.log2(v), size)
self.assertAlmostEqual(math.log(v), size * 0.6931471805599453)
self.assertAlmostEqual(math.log10(v), size * 0.3010299956639812)

def testSumProd(self):
sumprod = math.sumprod
Decimal = decimal.Decimal
Expand Down Expand Up @@ -1380,7 +1399,6 @@ def test_sumprod_accuracy(self):
self.assertEqual(sumprod([True, False] * 10, [0.1] * 20), 1.0)
self.assertEqual(sumprod([1.0, 10E100, 1.0, -10E100], [1.0]*4), 2.0)

@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@support.requires_resource('cpu')
def test_sumprod_stress(self):
sumprod = math.sumprod
Expand Down Expand Up @@ -2020,7 +2038,6 @@ def test_exceptions(self):
else:
self.fail("sqrt(-1) didn't raise ValueError")

@unittest.expectedFailure # TODO: RUSTPYTHON
@requires_IEEE_754
def test_testfile(self):
# Some tests need to be skipped on ancient OS X versions.
Expand Down Expand Up @@ -2495,6 +2512,46 @@ def test_input_exceptions(self):
self.assertRaises(TypeError, math.atan2, 1.0)
self.assertRaises(TypeError, math.atan2, 1.0, 2.0, 3.0)

def test_exception_messages(self):
x = -1.1
with self.assertRaisesRegex(ValueError,
f"expected a nonnegative input, got {x}"):
math.sqrt(x)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log(x)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log(123, x)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log(x, 123)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log2(x)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log10(x)
x = decimal.Decimal('-1.1')
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {x}"):
math.log(x)
x = fractions.Fraction(1, 10**400)
with self.assertRaisesRegex(ValueError,
f"expected a positive input, got {float(x)}"):
math.log(x)
x = -123
with self.assertRaisesRegex(ValueError,
"expected a positive input$"):
math.log(x)
with self.assertRaisesRegex(ValueError,
f"expected a noninteger or positive integer, got {x}"):
math.gamma(x)
x = 1.0
with self.assertRaisesRegex(ValueError,
f"expected a number between -1 and 1, got {x}"):
math.atanh(x)

# Custom assertions.

def assertIsNaN(self, value):
Expand Down Expand Up @@ -2724,6 +2781,9 @@ def test_fma_infinities(self):
or (sys.platform == "android" and platform.machine() == "x86_64")
or support.linked_to_musl(), # gh-131032
f"this platform doesn't implement IEE 754-2008 properly")
# gh-131032: musl is fixed but the fix is not yet released; when the fixed
# version is known change this to:
# or support.linked_to_musl() < (1, <m>, <p>)
def test_fma_zero_result(self):
nonnegative_finites = [0.0, 1e-300, 2.3, 1e300]

Expand Down
Loading
Loading