Skip to content

Commit b8db60d

Browse files
youknowoneterryluan12
authored andcommitted
Upgrade math,cmath to 3.14.2 and totally delegate to pymath (RustPython#6705)
* Upgrade test_math, test_cmath from 3.14.2 * pymath 0.1.5
1 parent e40eac4 commit b8db60d

9 files changed

Lines changed: 783 additions & 859 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ members = [
116116
".",
117117
"crates/*",
118118
]
119+
exclude = ["pymath"]
119120

120121
[workspace.package]
121122
version = "0.4.0"
@@ -184,7 +185,7 @@ once_cell = "1.20.3"
184185
parking_lot = "0.12.3"
185186
paste = "1.0.15"
186187
proc-macro2 = "1.0.105"
187-
pymath = "0.0.2"
188+
pymath = { version = "0.1.5", features = ["mul_add", "malachite-bigint", "complex"] }
188189
quote = "1.0.43"
189190
radium = "1.1.1"
190191
rand = "0.9"

Lib/test/mathdata/cmath_testcases.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ acosh1002 acosh 0.0 inf -> inf 1.5707963267948966
371371
acosh1003 acosh 2.3 inf -> inf 1.5707963267948966
372372
acosh1004 acosh -0.0 inf -> inf 1.5707963267948966
373373
acosh1005 acosh -2.3 inf -> inf 1.5707963267948966
374-
acosh1006 acosh 0.0 nan -> nan nan
374+
acosh1006 acosh 0.0 nan -> nan 1.5707963267948966 ignore-imag-sign
375375
acosh1007 acosh 2.3 nan -> nan nan
376-
acosh1008 acosh -0.0 nan -> nan nan
376+
acosh1008 acosh -0.0 nan -> nan 1.5707963267948966 ignore-imag-sign
377377
acosh1009 acosh -2.3 nan -> nan nan
378378
acosh1010 acosh -inf 0.0 -> inf 3.1415926535897931
379379
acosh1011 acosh -inf 2.3 -> inf 3.1415926535897931
@@ -1992,9 +1992,9 @@ tanh0065 tanh 1.797e+308 0.0 -> 1.0 0.0
19921992

19931993
--special values
19941994
tanh1000 tanh 0.0 0.0 -> 0.0 0.0
1995-
tanh1001 tanh 0.0 inf -> nan nan invalid
1995+
tanh1001 tanh 0.0 inf -> 0.0 nan invalid
19961996
tanh1002 tanh 2.3 inf -> nan nan invalid
1997-
tanh1003 tanh 0.0 nan -> nan nan
1997+
tanh1003 tanh 0.0 nan -> 0.0 nan
19981998
tanh1004 tanh 2.3 nan -> nan nan
19991999
tanh1005 tanh inf 0.0 -> 1.0 0.0
20002000
tanh1006 tanh inf 0.7 -> 1.0 0.0
@@ -2009,7 +2009,7 @@ tanh1014 tanh nan 2.3 -> nan nan
20092009
tanh1015 tanh nan inf -> nan nan
20102010
tanh1016 tanh nan nan -> nan nan
20112011
tanh1017 tanh 0.0 -0.0 -> 0.0 -0.0
2012-
tanh1018 tanh 0.0 -inf -> nan nan invalid
2012+
tanh1018 tanh 0.0 -inf -> 0.0 nan invalid
20132013
tanh1019 tanh 2.3 -inf -> nan nan invalid
20142014
tanh1020 tanh inf -0.0 -> 1.0 -0.0
20152015
tanh1021 tanh inf -0.7 -> 1.0 -0.0
@@ -2022,9 +2022,9 @@ tanh1027 tanh nan -0.0 -> nan -0.0
20222022
tanh1028 tanh nan -2.3 -> nan nan
20232023
tanh1029 tanh nan -inf -> nan nan
20242024
tanh1030 tanh -0.0 -0.0 -> -0.0 -0.0
2025-
tanh1031 tanh -0.0 -inf -> nan nan invalid
2025+
tanh1031 tanh -0.0 -inf -> -0.0 nan invalid
20262026
tanh1032 tanh -2.3 -inf -> nan nan invalid
2027-
tanh1033 tanh -0.0 nan -> nan nan
2027+
tanh1033 tanh -0.0 nan -> -0.0 nan
20282028
tanh1034 tanh -2.3 nan -> nan nan
20292029
tanh1035 tanh -inf -0.0 -> -1.0 -0.0
20302030
tanh1036 tanh -inf -0.7 -> -1.0 -0.0
@@ -2035,7 +2035,7 @@ tanh1040 tanh -inf -3.5 -> -1.0 -0.0
20352035
tanh1041 tanh -inf -inf -> -1.0 0.0 ignore-imag-sign
20362036
tanh1042 tanh -inf nan -> -1.0 0.0 ignore-imag-sign
20372037
tanh1043 tanh -0.0 0.0 -> -0.0 0.0
2038-
tanh1044 tanh -0.0 inf -> nan nan invalid
2038+
tanh1044 tanh -0.0 inf -> -0.0 nan invalid
20392039
tanh1045 tanh -2.3 inf -> nan nan invalid
20402040
tanh1046 tanh -inf 0.0 -> -1.0 0.0
20412041
tanh1047 tanh -inf 0.7 -> -1.0 0.0
@@ -2307,9 +2307,9 @@ tan0066 tan -8.79645943005142 0.0 -> 0.7265425280053614098 0.0
23072307

23082308
-- special values
23092309
tan1000 tan -0.0 0.0 -> -0.0 0.0
2310-
tan1001 tan -inf 0.0 -> nan nan invalid
2310+
tan1001 tan -inf 0.0 -> nan 0.0 invalid
23112311
tan1002 tan -inf 2.2999999999999998 -> nan nan invalid
2312-
tan1003 tan nan 0.0 -> nan nan
2312+
tan1003 tan nan 0.0 -> nan 0.0
23132313
tan1004 tan nan 2.2999999999999998 -> nan nan
23142314
tan1005 tan -0.0 inf -> -0.0 1.0
23152315
tan1006 tan -0.69999999999999996 inf -> -0.0 1.0
@@ -2324,7 +2324,7 @@ tan1014 tan -2.2999999999999998 nan -> nan nan
23242324
tan1015 tan -inf nan -> nan nan
23252325
tan1016 tan nan nan -> nan nan
23262326
tan1017 tan 0.0 0.0 -> 0.0 0.0
2327-
tan1018 tan inf 0.0 -> nan nan invalid
2327+
tan1018 tan inf 0.0 -> nan 0.0 invalid
23282328
tan1019 tan inf 2.2999999999999998 -> nan nan invalid
23292329
tan1020 tan 0.0 inf -> 0.0 1.0
23302330
tan1021 tan 0.69999999999999996 inf -> 0.0 1.0
@@ -2337,9 +2337,9 @@ tan1027 tan 0.0 nan -> 0.0 nan
23372337
tan1028 tan 2.2999999999999998 nan -> nan nan
23382338
tan1029 tan inf nan -> nan nan
23392339
tan1030 tan 0.0 -0.0 -> 0.0 -0.0
2340-
tan1031 tan inf -0.0 -> nan nan invalid
2340+
tan1031 tan inf -0.0 -> nan -0.0 invalid
23412341
tan1032 tan inf -2.2999999999999998 -> nan nan invalid
2342-
tan1033 tan nan -0.0 -> nan nan
2342+
tan1033 tan nan -0.0 -> nan -0.0
23432343
tan1034 tan nan -2.2999999999999998 -> nan nan
23442344
tan1035 tan 0.0 -inf -> 0.0 -1.0
23452345
tan1036 tan 0.69999999999999996 -inf -> 0.0 -1.0
@@ -2350,7 +2350,7 @@ tan1040 tan 3.5 -inf -> 0.0 -1.0
23502350
tan1041 tan inf -inf -> -0.0 -1.0 ignore-real-sign
23512351
tan1042 tan nan -inf -> -0.0 -1.0 ignore-real-sign
23522352
tan1043 tan -0.0 -0.0 -> -0.0 -0.0
2353-
tan1044 tan -inf -0.0 -> nan nan invalid
2353+
tan1044 tan -inf -0.0 -> nan -0.0 invalid
23542354
tan1045 tan -inf -2.2999999999999998 -> nan nan invalid
23552355
tan1046 tan -0.0 -inf -> -0.0 -1.0
23562356
tan1047 tan -0.69999999999999996 -inf -> -0.0 -1.0

Lib/test/mathdata/ieee754.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ inf
116116
>>> 0 ** -1
117117
Traceback (most recent call last):
118118
...
119-
ZeroDivisionError: 0.0 cannot be raised to a negative power
119+
ZeroDivisionError: zero to a negative power
120120
>>> pow(0, NAN)
121121
nan
122122

@@ -127,31 +127,31 @@ Trigonometric Functions
127127
>>> sin(INF)
128128
Traceback (most recent call last):
129129
...
130-
ValueError: math domain error
130+
ValueError: expected a finite input, got inf
131131
>>> sin(NINF)
132132
Traceback (most recent call last):
133133
...
134-
ValueError: math domain error
134+
ValueError: expected a finite input, got -inf
135135
>>> sin(NAN)
136136
nan
137137
>>> cos(INF)
138138
Traceback (most recent call last):
139139
...
140-
ValueError: math domain error
140+
ValueError: expected a finite input, got inf
141141
>>> cos(NINF)
142142
Traceback (most recent call last):
143143
...
144-
ValueError: math domain error
144+
ValueError: expected a finite input, got -inf
145145
>>> cos(NAN)
146146
nan
147147
>>> tan(INF)
148148
Traceback (most recent call last):
149149
...
150-
ValueError: math domain error
150+
ValueError: expected a finite input, got inf
151151
>>> tan(NINF)
152152
Traceback (most recent call last):
153153
...
154-
ValueError: math domain error
154+
ValueError: expected a finite input, got -inf
155155
>>> tan(NAN)
156156
nan
157157

@@ -169,11 +169,11 @@ True
169169
>>> asin(INF), asin(NINF)
170170
Traceback (most recent call last):
171171
...
172-
ValueError: math domain error
172+
ValueError: expected a number in range from -1 up to 1, got inf
173173
>>> acos(INF), acos(NINF)
174174
Traceback (most recent call last):
175175
...
176-
ValueError: math domain error
176+
ValueError: expected a number in range from -1 up to 1, got inf
177177
>>> equal(atan(INF), PI/2), equal(atan(NINF), -PI/2)
178178
(True, True)
179179

Lib/test/test_cmath.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ def test_cmath_matches_math(self):
276276
self.rAssertAlmostEqual(math.log(v, base), z.real)
277277
self.assertEqual(0., z.imag)
278278

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

539-
@unittest.expectedFailure # TODO: RUSTPYTHON
540537
@requires_IEEE_754
541538
def testAtanhSign(self):
542539
for z in complex_zeros:
@@ -583,7 +580,6 @@ def test_complex_near_zero(self):
583580
self.assertIsClose(0.001-0.001j, 0.001+0.001j, abs_tol=2e-03)
584581
self.assertIsNotClose(0.001-0.001j, 0.001+0.001j, abs_tol=1e-03)
585582

586-
@unittest.expectedFailure # TODO: RUSTPYTHON
587583
def test_complex_special(self):
588584
self.assertIsNotClose(INF, INF*1j)
589585
self.assertIsNotClose(INF*1j, INF)

Lib/test/test_math.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ def testFloor(self):
573573
#self.assertEqual(math.ceil(NINF), NINF)
574574
#self.assertTrue(math.isnan(math.floor(NAN)))
575575

576+
class TestFloorIsNone(float):
577+
__floor__ = None
576578
class TestFloor:
577579
def __floor__(self):
578580
return 42
@@ -588,6 +590,7 @@ class TestBadFloor:
588590
self.assertEqual(math.floor(FloatLike(41.9)), 41)
589591
self.assertRaises(TypeError, math.floor, TestNoFloor())
590592
self.assertRaises(ValueError, math.floor, TestBadFloor())
593+
self.assertRaises(TypeError, math.floor, TestFloorIsNone(3.5))
591594

592595
t = TestNoFloor()
593596
t.__floor__ = lambda *args: args
@@ -1125,6 +1128,15 @@ def __index__(self):
11251128
with self.assertRaises(TypeError):
11261129
math.isqrt(value)
11271130

1131+
@support.bigmemtest(2**32, memuse=0.85)
1132+
def test_isqrt_huge(self, size):
1133+
if size & 1:
1134+
size += 1
1135+
v = 1 << size
1136+
w = math.isqrt(v)
1137+
self.assertEqual(w.bit_length(), size // 2 + 1)
1138+
self.assertEqual(w.bit_count(), 1)
1139+
11281140
def test_lcm(self):
11291141
lcm = math.lcm
11301142
self.assertEqual(lcm(0, 0), 0)
@@ -1272,6 +1284,13 @@ def testLog10(self):
12721284
self.assertEqual(math.log(INF), INF)
12731285
self.assertTrue(math.isnan(math.log10(NAN)))
12741286

1287+
@support.bigmemtest(2**32, memuse=0.2)
1288+
def test_log_huge_integer(self, size):
1289+
v = 1 << size
1290+
self.assertAlmostEqual(math.log2(v), size)
1291+
self.assertAlmostEqual(math.log(v), size * 0.6931471805599453)
1292+
self.assertAlmostEqual(math.log10(v), size * 0.3010299956639812)
1293+
12751294
def testSumProd(self):
12761295
sumprod = math.sumprod
12771296
Decimal = decimal.Decimal
@@ -1380,7 +1399,6 @@ def test_sumprod_accuracy(self):
13801399
self.assertEqual(sumprod([True, False] * 10, [0.1] * 20), 1.0)
13811400
self.assertEqual(sumprod([1.0, 10E100, 1.0, -10E100], [1.0]*4), 2.0)
13821401

1383-
@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
13841402
@support.requires_resource('cpu')
13851403
def test_sumprod_stress(self):
13861404
sumprod = math.sumprod
@@ -2020,7 +2038,6 @@ def test_exceptions(self):
20202038
else:
20212039
self.fail("sqrt(-1) didn't raise ValueError")
20222040

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

2515+
def test_exception_messages(self):
2516+
x = -1.1
2517+
with self.assertRaisesRegex(ValueError,
2518+
f"expected a nonnegative input, got {x}"):
2519+
math.sqrt(x)
2520+
with self.assertRaisesRegex(ValueError,
2521+
f"expected a positive input, got {x}"):
2522+
math.log(x)
2523+
with self.assertRaisesRegex(ValueError,
2524+
f"expected a positive input, got {x}"):
2525+
math.log(123, x)
2526+
with self.assertRaisesRegex(ValueError,
2527+
f"expected a positive input, got {x}"):
2528+
math.log(x, 123)
2529+
with self.assertRaisesRegex(ValueError,
2530+
f"expected a positive input, got {x}"):
2531+
math.log2(x)
2532+
with self.assertRaisesRegex(ValueError,
2533+
f"expected a positive input, got {x}"):
2534+
math.log10(x)
2535+
x = decimal.Decimal('-1.1')
2536+
with self.assertRaisesRegex(ValueError,
2537+
f"expected a positive input, got {x}"):
2538+
math.log(x)
2539+
x = fractions.Fraction(1, 10**400)
2540+
with self.assertRaisesRegex(ValueError,
2541+
f"expected a positive input, got {float(x)}"):
2542+
math.log(x)
2543+
x = -123
2544+
with self.assertRaisesRegex(ValueError,
2545+
"expected a positive input$"):
2546+
math.log(x)
2547+
with self.assertRaisesRegex(ValueError,
2548+
f"expected a noninteger or positive integer, got {x}"):
2549+
math.gamma(x)
2550+
x = 1.0
2551+
with self.assertRaisesRegex(ValueError,
2552+
f"expected a number between -1 and 1, got {x}"):
2553+
math.atanh(x)
2554+
24982555
# Custom assertions.
24992556

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

0 commit comments

Comments
 (0)