@@ -35,22 +35,28 @@ def test_basic_equality():
35
35
36
36
37
37
@pytest .mark .parametrize ("op" , ["add" , "sub" , "mul" , "truediv" , "pow" , "copysign" ])
38
- @pytest .mark .parametrize ("other" , ["3.0" , "12.5" , "100.0" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
39
- def test_binary_ops (op , other ):
40
- if op == "truediv" and float (other ) == 0 :
38
+ @pytest .mark .parametrize ("a" , ["3.0" , "12.5" , "100.0" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
39
+ @pytest .mark .parametrize ("b" , ["3.0" , "12.5" , "100.0" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
40
+ def test_binary_ops (op , a , b ):
41
+ if op == "truediv" and float (b ) == 0 :
41
42
pytest .xfail ("float division by zero" )
42
43
43
44
op_func = getattr (operator , op , None ) or getattr (np , op )
44
- quad_a = QuadPrecision ("12.5" )
45
- quad_b = QuadPrecision (other )
46
- float_a = 12.5
47
- float_b = float (other )
45
+ quad_a = QuadPrecision (a )
46
+ quad_b = QuadPrecision (b )
47
+ float_a = float ( a )
48
+ float_b = float (b )
48
49
49
50
quad_result = op_func (quad_a , quad_b )
50
51
float_result = op_func (float_a , float_b )
51
52
52
53
np .testing .assert_allclose (np .float64 (quad_result ), float_result , atol = 1e-10 , rtol = 0 , equal_nan = True )
53
54
55
+ # Check sign for zero results
56
+ if float_result == 0.0 :
57
+ assert np .signbit (float_result ) == np .signbit (
58
+ quad_result ), f"Zero sign mismatch for { op } ({ a } , { b } )"
59
+
54
60
55
61
@pytest .mark .parametrize ("op" , ["eq" , "ne" , "le" , "lt" , "ge" , "gt" ])
56
62
@pytest .mark .parametrize ("a" , ["3.0" , "12.5" , "100.0" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
@@ -93,6 +99,11 @@ def test_array_minmax(op, a, b):
93
99
94
100
np .testing .assert_array_equal (quad_res .astype (float ), float_res )
95
101
102
+ # Check sign for zero results
103
+ if float_result == 0.0 :
104
+ assert np .signbit (float_result ) == np .signbit (
105
+ quad_result ), f"Zero sign mismatch for { op } ({ a } , { b } )"
106
+
96
107
97
108
@pytest .mark .parametrize ("op" , ["amin" , "amax" , "nanmin" , "nanmax" ])
98
109
@pytest .mark .parametrize ("a" , ["3.0" , "12.5" , "100.0" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
@@ -107,6 +118,11 @@ def test_array_aminmax(op, a, b):
107
118
108
119
np .testing .assert_array_equal (np .array (quad_res ).astype (float ), float_res )
109
120
121
+ # Check sign for zero results
122
+ if float_result == 0.0 :
123
+ assert np .signbit (float_result ) == np .signbit (
124
+ quad_result ), f"Zero sign mismatch for { op } ({ a } , { b } )"
125
+
110
126
111
127
@pytest .mark .parametrize ("op" , ["negative" , "positive" , "absolute" , "sign" , "signbit" , "isfinite" , "isinf" , "isnan" , "sqrt" , "square" , "reciprocal" ])
112
128
@pytest .mark .parametrize ("val" , ["3.0" , "-3.0" , "12.5" , "100.0" , "1e100" , "0.0" , "-0.0" , "inf" , "-inf" , "nan" , "-nan" ])
@@ -126,7 +142,7 @@ def test_unary_ops(op, val):
126
142
127
143
np .testing .assert_array_equal (np .array (quad_result ).astype (float ), float_result )
128
144
129
- if op in ["negative " , "positive " , "absolute " , "sign" ] :
145
+ if ( float_result == 0.0 ) and ( op not in ["signbit " , "isfinite " , "isinf " , "isnan" ]) :
130
146
assert np .signbit (float_result ) == np .signbit (quad_result )
131
147
132
148
@@ -290,6 +306,11 @@ def test_logarithmic_functions(op, val):
290
306
np .testing .assert_allclose (float (quad_result ), float_result , rtol = rtol , atol = atol ,
291
307
err_msg = f"Value mismatch for { op } ({ val } )" )
292
308
309
+ # Check sign for zero results
310
+ if float_result == 0.0 :
311
+ assert np .signbit (float_result ) == np .signbit (
312
+ quad_result ), f"Zero sign mismatch for { op } ({ a } , { b } )"
313
+
293
314
294
315
@pytest .mark .parametrize ("val" , [
295
316
# Basic cases around -1 (critical point for log1p)
@@ -304,6 +325,8 @@ def test_logarithmic_functions(op, val):
304
325
"-1.1" , "-2.0" , "-10.0" ,
305
326
# Large positive values
306
327
"1e10" , "1e15" , "1e100" ,
328
+ # Edge cases
329
+ "0.0" , "-0.0" ,
307
330
# Special values
308
331
"inf" , "-inf" , "nan" , "-nan"
309
332
])
@@ -341,9 +364,16 @@ def test_log1p(val):
341
364
np .testing .assert_allclose (float (quad_result ), float_result , rtol = rtol , atol = atol ,
342
365
err_msg = f"Value mismatch for log1p({ val } )" )
343
366
367
+ # Check sign for zero results
368
+ if float_result == 0.0 :
369
+ assert np .signbit (float_result ) == np .signbit (
370
+ quad_result ), f"Zero sign mismatch for { op } ({ val } )"
371
+
344
372
def test_inf ():
345
373
assert QuadPrecision ("inf" ) > QuadPrecision ("1e1000" )
374
+ assert np .signbit (QuadPrecision ("inf" )) == 0
346
375
assert QuadPrecision ("-inf" ) < QuadPrecision ("-1e1000" )
376
+ assert np .signbit (QuadPrecision ("-inf" )) == 1
347
377
348
378
349
379
def test_dtype_creation ():
0 commit comments