forked from numpy/numpy-user-dtypes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quaddtype.py
More file actions
1695 lines (1372 loc) · 62.8 KB
/
test_quaddtype.py
File metadata and controls
1695 lines (1372 loc) · 62.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
import sys
import numpy as np
import operator
import numpy_quaddtype
from numpy_quaddtype import QuadPrecDType, QuadPrecision
def test_create_scalar_simple():
assert isinstance(QuadPrecision("12.0"), QuadPrecision)
assert isinstance(QuadPrecision(1.63), QuadPrecision)
assert isinstance(QuadPrecision(1), QuadPrecision)
@pytest.mark.parametrize("name,expected", [("pi", np.pi), ("e", np.e), ("log2e", np.log2(np.e)), ("log10e", np.log10(np.e)), ("ln2", np.log(2.0)), ("ln10", np.log(10.0))])
def test_math_constant(name, expected):
assert isinstance(getattr(numpy_quaddtype, name), QuadPrecision)
assert np.float64(getattr(numpy_quaddtype, name)) == expected
def test_smallest_subnormal_value():
"""Test that smallest_subnormal has the correct value across all platforms."""
smallest_sub = numpy_quaddtype.smallest_subnormal
repr_str = repr(smallest_sub)
# The repr should show QuadPrecision('6.0e-4966', backend='sleef')
assert "6.0e-4966" in repr_str, f"Expected '6.0e-4966' in repr, got {repr_str}"
assert smallest_sub > 0, "smallest_subnormal should be positive"
@pytest.mark.parametrize("dtype", [
"bool",
"byte", "int8", "ubyte", "uint8",
"short", "int16", "ushort", "uint16",
"int", "int32", "uint", "uint32",
"long", "ulong",
"longlong", "int64", "ulonglong", "uint64",
"half", "float16",
"float", "float32",
"double", "float64",
"longdouble", "float96", "float128",
])
def test_supported_astype(dtype):
if dtype in ("float96", "float128") and getattr(np, dtype, None) is None:
pytest.skip(f"{dtype} is unsupported on the current platform")
orig = np.array(1, dtype=dtype)
quad = orig.astype(QuadPrecDType, casting="safe")
back = quad.astype(dtype, casting="unsafe")
assert quad == 1
assert back == orig
@pytest.mark.parametrize("dtype", ["S10", "U10", "T", "V10", "datetime64[ms]", "timedelta64[ms]"])
def test_unsupported_astype(dtype):
if dtype == "V10":
with pytest.raises(TypeError, match="cast"):
np.ones((3, 3), dtype="V10").astype(QuadPrecDType, casting="unsafe")
else:
with pytest.raises(TypeError, match="cast"):
np.array(1, dtype=dtype).astype(QuadPrecDType, casting="unsafe")
with pytest.raises(TypeError, match="cast"):
np.array(QuadPrecision(1)).astype(dtype, casting="unsafe")
def test_basic_equality():
assert QuadPrecision("12") == QuadPrecision(
"12.0") == QuadPrecision("12.00")
@pytest.mark.parametrize("op", ["add", "sub", "mul", "truediv", "pow", "copysign"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_binary_ops(op, a, b):
if op == "truediv" and float(b) == 0:
pytest.xfail("float division by zero")
op_func = getattr(operator, op, None) or getattr(np, op)
quad_a = QuadPrecision(a)
quad_b = QuadPrecision(b)
float_a = float(a)
float_b = float(b)
quad_result = op_func(quad_a, quad_b)
float_result = op_func(float_a, float_b)
np.testing.assert_allclose(np.float64(quad_result), float_result, atol=1e-10, rtol=0, equal_nan=True)
# Check sign for zero results
if float_result == 0.0:
assert np.signbit(float_result) == np.signbit(
quad_result), f"Zero sign mismatch for {op}({a}, {b})"
@pytest.mark.parametrize("op", ["eq", "ne", "le", "lt", "ge", "gt"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_comparisons(op, a, b):
op_func = getattr(operator, op)
quad_a = QuadPrecision(a)
quad_b = QuadPrecision(b)
float_a = float(a)
float_b = float(b)
assert op_func(quad_a, quad_b) == op_func(float_a, float_b)
@pytest.mark.parametrize("op", ["eq", "ne", "le", "lt", "ge", "gt"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_comparisons(op, a, b):
op_func = getattr(operator, op)
quad_a = np.array(QuadPrecision(a))
quad_b = np.array(QuadPrecision(b))
float_a = np.array(float(a))
float_b = np.array(float(b))
assert np.array_equal(op_func(quad_a, quad_b), op_func(float_a, float_b))
@pytest.mark.parametrize("op", ["minimum", "maximum", "fmin", "fmax"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_minmax(op, a, b):
op_func = getattr(np, op)
quad_a = np.array([QuadPrecision(a)])
quad_b = np.array([QuadPrecision(b)])
float_a = np.array([float(a)])
float_b = np.array([float(b)])
quad_res = op_func(quad_a, quad_b)
float_res = op_func(float_a, float_b)
# native implementation may not be sensitive to zero signs
# but we want to enforce it for the quad dtype
# e.g. min(+0.0, -0.0) = -0.0
if float_a == 0.0 and float_b == 0.0:
assert float_res == 0.0
float_res = np.copysign(0.0, op_func(np.copysign(1.0, float_a), np.copysign(1.0, float_b)))
np.testing.assert_array_equal(quad_res.astype(float), float_res)
# Check sign for zero results
if float_res == 0.0:
assert np.signbit(float_res) == np.signbit(
quad_res), f"Zero sign mismatch for {op}({a}, {b})"
@pytest.mark.parametrize("op", ["amin", "amax", "nanmin", "nanmax"])
@pytest.mark.parametrize("a", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("b", ["3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_array_aminmax(op, a, b):
op_func = getattr(np, op)
quad_ab = np.array([QuadPrecision(a), QuadPrecision(b)])
float_ab = np.array([float(a), float(b)])
quad_res = op_func(quad_ab)
float_res = op_func(float_ab)
# native implementation may not be sensitive to zero signs
# but we want to enforce it for the quad dtype
# e.g. min(+0.0, -0.0) = -0.0
if float(a) == 0.0 and float(b) == 0.0:
assert float_res == 0.0
float_res = np.copysign(0.0, op_func(np.array([np.copysign(1.0, float(a)), np.copysign(1.0, float(b))])))
np.testing.assert_array_equal(np.array(quad_res).astype(float), float_res)
# Check sign for zero results
if float_res == 0.0:
assert np.signbit(float_res) == np.signbit(
quad_res), f"Zero sign mismatch for {op}({a}, {b})"
@pytest.mark.parametrize("op", ["negative", "positive", "absolute", "sign", "signbit", "isfinite", "isinf", "isnan", "sqrt", "square", "reciprocal"])
@pytest.mark.parametrize("val", ["3.0", "-3.0", "12.5", "100.0", "1e100", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_unary_ops(op, val):
op_func = dict(negative=operator.neg, positive=operator.pos, absolute=operator.abs).get(op, None)
nop_func = getattr(np, op)
quad_val = QuadPrecision(val)
float_val = float(val)
for of in [op_func, nop_func]:
if of is None:
continue
quad_result = of(quad_val)
float_result = of(float_val)
np.testing.assert_array_equal(np.array(quad_result).astype(float), float_result)
if (float_result == 0.0) and (op not in ["signbit", "isfinite", "isinf", "isnan"]):
assert np.signbit(float_result) == np.signbit(quad_result)
@pytest.mark.parametrize("op", ["floor", "ceil", "trunc", "rint"])
@pytest.mark.parametrize("val", [
# Basic cases
"3.2", "-3.2", "3.8", "-3.8", "0.1", "-0.1",
# Edge cases around integers
"3.0", "-3.0", "0.0", "-0.0", "1.0", "-1.0",
# Halfway cases (important for rint)
"2.5", "-2.5", "3.5", "-3.5", "0.5", "-0.5",
# Large numbers
"1e10", "-1e10", "1e15", "-1e15",
# Small fractional numbers
"1e-10", "-1e-10", "1e-15", "-1e-15",
# Special values
"inf", "-inf", "nan", "-nan"
])
def test_rounding_functions(op, val):
"""Comprehensive test for rounding functions: floor, ceil, trunc, rint"""
op_func = getattr(np, op)
quad_val = QuadPrecision(val)
float_val = float(val)
quad_result = op_func(quad_val)
float_result = op_func(float_val)
# Handle NaN cases
if np.isnan(float_result):
assert np.isnan(
float(quad_result)), f"Expected NaN for {op}({val}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(
float(quad_result)), f"Expected inf for {op}({val}), got {float(quad_result)}"
assert np.sign(float_result) == np.sign(
float(quad_result)), f"Infinity sign mismatch for {op}({val})"
return
# For finite results, check value and sign
np.testing.assert_allclose(float(quad_result), float_result, rtol=1e-15, atol=1e-15,
err_msg=f"Value mismatch for {op}({val})")
# Check sign for zero results
if float_result == 0.0:
assert np.signbit(float_result) == np.signbit(
quad_result), f"Zero sign mismatch for {op}({val})"
def test_rint_near_halfway():
assert np.rint(QuadPrecision("7.4999999999999999")) == 7
assert np.rint(QuadPrecision("7.49999999999999999")) == 7
assert np.rint(QuadPrecision("7.5")) == 8
@pytest.mark.parametrize("op", ["exp", "exp2"])
@pytest.mark.parametrize("val", [
# Basic cases
"0.0", "-0.0", "1.0", "-1.0", "2.0", "-2.0",
# Small values (should be close to 1)
"1e-10", "-1e-10", "1e-15", "-1e-15",
# Medium values
"10.0", "-10.0", "20.0", "-20.0",
# Values that might cause overflow
"100.0", "200.0", "700.0", "1000.0",
# Values that might cause underflow
"-100.0", "-200.0", "-700.0", "-1000.0",
# Fractional values
"0.5", "-0.5", "1.5", "-1.5", "2.5", "-2.5",
# Special values
"inf", "-inf", "nan", "-nan"
])
def test_exponential_functions(op, val):
"""Comprehensive test for exponential functions: exp, exp2"""
op_func = getattr(np, op)
quad_val = QuadPrecision(val)
float_val = float(val)
quad_result = op_func(quad_val)
float_result = op_func(float_val)
# Handle NaN cases
if np.isnan(float_result):
assert np.isnan(
float(quad_result)), f"Expected NaN for {op}({val}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(
float(quad_result)), f"Expected inf for {op}({val}), got {float(quad_result)}"
assert np.sign(float_result) == np.sign(
float(quad_result)), f"Infinity sign mismatch for {op}({val})"
return
# Handle underflow to zero
if float_result == 0.0:
assert float(
quad_result) == 0.0, f"Expected 0 for {op}({val}), got {float(quad_result)}"
assert np.signbit(float_result) == np.signbit(
quad_result), f"Zero sign mismatch for {op}({val})"
return
# For finite non-zero results
# Use relative tolerance for exponential functions due to their rapid growth
rtol = 1e-14 if abs(float_result) < 1e100 else 1e-10
np.testing.assert_allclose(float(quad_result), float_result, rtol=rtol, atol=1e-15,
err_msg=f"Value mismatch for {op}({val})")
@pytest.mark.parametrize("op", ["log", "log2", "log10"])
@pytest.mark.parametrize("val", [
# Basic positive cases
"1.0", "2.0", "10.0", "100.0", "1000.0",
# Values close to 1 (important for log accuracy)
"1.01", "0.99", "1.001", "0.999", "1.0001", "0.9999",
# Small positive values
"1e-10", "1e-15", "1e-100", "1e-300",
# Large positive values
"1e10", "1e15", "1e100", "1e300",
# Fractional values
"0.5", "0.1", "0.01", "2.5", "5.5", "25.0",
# Edge cases
"0.0", "-0.0", # Should give -inf
# Invalid domain (negative values) - should give NaN
"-1.0", "-2.0", "-0.5", "-10.0",
# Special values
"inf", "-inf", "nan", "-nan"
])
def test_logarithmic_functions(op, val):
"""Comprehensive test for logarithmic functions: log, log2, log10"""
op_func = getattr(np, op)
quad_val = QuadPrecision(val)
float_val = float(val)
quad_result = op_func(quad_val)
float_result = op_func(float_val)
# Handle NaN cases (negative values, NaN input)
if np.isnan(float_result):
assert np.isnan(
float(quad_result)), f"Expected NaN for {op}({val}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(
float(quad_result)), f"Expected inf for {op}({val}), got {float(quad_result)}"
assert np.sign(float_result) == np.sign(
float(quad_result)), f"Infinity sign mismatch for {op}({val})"
return
# For finite results
# Use higher tolerance for values very close to 1 where log is close to 0
if abs(float(val) - 1.0) < 1e-10:
rtol = 1e-10
atol = 1e-15
else:
rtol = 1e-14
atol = 1e-15
np.testing.assert_allclose(float(quad_result), float_result, rtol=rtol, atol=atol,
err_msg=f"Value mismatch for {op}({val})")
# Check sign for zero results
if float_result == 0.0:
assert np.signbit(float_result) == np.signbit(
quad_result), f"Zero sign mismatch"
@pytest.mark.parametrize("val", [
# Basic cases around -1 (critical point for log1p)
"-0.5", "-0.1", "-0.01", "-0.001", "-0.0001",
# Cases close to 0 (where log1p is most accurate)
"1e-10", "-1e-10", "1e-15", "-1e-15", "1e-20", "-1e-20",
# Larger positive values
"0.1", "0.5", "1.0", "2.0", "10.0", "100.0",
# Edge case at -1 (should give -inf)
"-1.0",
# Invalid domain (< -1) - should give NaN
"-1.1", "-2.0", "-10.0",
# Large positive values
"1e10", "1e15", "1e100",
# Edge cases
"0.0", "-0.0",
# Special values
"inf", "-inf", "nan", "-nan"
])
def test_log1p(val):
"""Comprehensive test for log1p function"""
op = "log1p"
quad_val = QuadPrecision(val)
float_val = float(val)
quad_result = np.log1p(quad_val)
float_result = np.log1p(float_val)
# Handle NaN cases (values < -1, NaN input)
if np.isnan(float_result):
assert np.isnan(
float(quad_result)), f"Expected NaN for log1p({val}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(
float(quad_result)), f"Expected inf for log1p({val}), got {float(quad_result)}"
assert np.sign(float_result) == np.sign(
float(quad_result)), f"Infinity sign mismatch for log1p({val})"
return
# For finite results
# log1p is designed for high accuracy near 0, so use tight tolerances
if abs(float(val)) < 1e-10:
rtol = 1e-15
atol = 1e-20
else:
rtol = 1e-14
atol = 1e-15
np.testing.assert_allclose(float(quad_result), float_result, rtol=rtol, atol=atol,
err_msg=f"Value mismatch for log1p({val})")
# Check sign for zero results
if float_result == 0.0:
assert np.signbit(float_result) == np.signbit(
quad_result), f"Zero sign mismatch for {op}({val})"
@pytest.mark.parametrize("x", [
# Regular values
"0.0", "1.0", "2.0", "-1.0", "-2.0", "0.5", "-0.5",
# Large values (test numerical stability)
"100.0", "1000.0", "-100.0", "-1000.0",
# Small values
"1e-10", "-1e-10", "1e-20", "-1e-20",
# Special values
"inf", "-inf", "nan", "-nan", "-0.0"
])
@pytest.mark.parametrize("y", [
# Regular values
"0.0", "1.0", "2.0", "-1.0", "-2.0", "0.5", "-0.5",
# Large values
"100.0", "1000.0", "-100.0", "-1000.0",
# Small values
"1e-10", "-1e-10", "1e-20", "-1e-20",
# Special values
"inf", "-inf", "nan", "-nan", "-0.0"
])
def test_logaddexp(x, y):
"""Comprehensive test for logaddexp function: log(exp(x) + exp(y))"""
quad_x = QuadPrecision(x)
quad_y = QuadPrecision(y)
float_x = float(x)
float_y = float(y)
quad_result = np.logaddexp(quad_x, quad_y)
float_result = np.logaddexp(float_x, float_y)
# Handle NaN cases
if np.isnan(float_result):
assert np.isnan(float(quad_result)), \
f"Expected NaN for logaddexp({x}, {y}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(float(quad_result)), \
f"Expected inf for logaddexp({x}, {y}), got {float(quad_result)}"
if not np.isnan(float_result):
assert np.sign(float_result) == np.sign(float(quad_result)), \
f"Infinity sign mismatch for logaddexp({x}, {y})"
return
# For finite results, check with appropriate tolerance
# logaddexp is numerically sensitive, especially for large differences
if abs(float_x - float_y) > 50:
# When values differ greatly, result should be close to max(x, y)
rtol = 1e-10
atol = 1e-10
else:
rtol = 1e-13
atol = 1e-15
np.testing.assert_allclose(
float(quad_result), float_result,
rtol=rtol, atol=atol,
err_msg=f"Value mismatch for logaddexp({x}, {y})"
)
def test_logaddexp_special_properties():
"""Test special mathematical properties of logaddexp"""
# logaddexp(x, x) = x + log(2)
x = QuadPrecision("2.0")
result = np.logaddexp(x, x)
expected = float(x) + np.log(2.0)
np.testing.assert_allclose(float(result), expected, rtol=1e-14)
# logaddexp(x, -inf) = x
x = QuadPrecision("5.0")
result = np.logaddexp(x, QuadPrecision("-inf"))
np.testing.assert_allclose(float(result), float(x), rtol=1e-14)
# logaddexp(-inf, x) = x
result = np.logaddexp(QuadPrecision("-inf"), x)
np.testing.assert_allclose(float(result), float(x), rtol=1e-14)
# logaddexp(-inf, -inf) = -inf
result = np.logaddexp(QuadPrecision("-inf"), QuadPrecision("-inf"))
assert np.isinf(float(result)) and float(result) < 0
# logaddexp(inf, anything) = inf
result = np.logaddexp(QuadPrecision("inf"), QuadPrecision("100.0"))
assert np.isinf(float(result)) and float(result) > 0
# logaddexp(anything, inf) = inf
result = np.logaddexp(QuadPrecision("100.0"), QuadPrecision("inf"))
assert np.isinf(float(result)) and float(result) > 0
# Commutativity: logaddexp(x, y) = logaddexp(y, x)
x = QuadPrecision("3.0")
y = QuadPrecision("5.0")
result1 = np.logaddexp(x, y)
result2 = np.logaddexp(y, x)
np.testing.assert_allclose(float(result1), float(result2), rtol=1e-14)
@pytest.mark.parametrize("x", [
# Regular values
"0.0", "1.0", "2.0", "-1.0", "-2.0", "0.5", "-0.5",
# Large values (test numerical stability)
"100.0", "1000.0", "-100.0", "-1000.0",
# Small values
"1e-10", "-1e-10", "1e-20", "-1e-20",
# Special values
"inf", "-inf", "nan", "-nan", "-0.0"
])
@pytest.mark.parametrize("y", [
# Regular values
"0.0", "1.0", "2.0", "-1.0", "-2.0", "0.5", "-0.5",
# Large values
"100.0", "1000.0", "-100.0", "-1000.0",
# Small values
"1e-10", "-1e-10", "1e-20", "-1e-20",
# Special values
"inf", "-inf", "nan", "-nan", "-0.0"
])
def test_logaddexp2(x, y):
"""Comprehensive test for logaddexp2 function: log2(2^x + 2^y)"""
quad_x = QuadPrecision(x)
quad_y = QuadPrecision(y)
float_x = float(x)
float_y = float(y)
quad_result = np.logaddexp2(quad_x, quad_y)
float_result = np.logaddexp2(float_x, float_y)
# Handle NaN cases
if np.isnan(float_result):
assert np.isnan(float(quad_result)), \
f"Expected NaN for logaddexp2({x}, {y}), got {float(quad_result)}"
return
# Handle infinity cases
if np.isinf(float_result):
assert np.isinf(float(quad_result)), \
f"Expected inf for logaddexp2({x}, {y}), got {float(quad_result)}"
if not np.isnan(float_result):
assert np.sign(float_result) == np.sign(float(quad_result)), \
f"Infinity sign mismatch for logaddexp2({x}, {y})"
return
# For finite results, check with appropriate tolerance
# logaddexp2 is numerically sensitive, especially for large differences
if abs(float_x - float_y) > 50:
# When values differ greatly, result should be close to max(x, y)
rtol = 1e-10
atol = 1e-10
else:
rtol = 1e-13
atol = 1e-15
np.testing.assert_allclose(
float(quad_result), float_result,
rtol=rtol, atol=atol,
err_msg=f"Value mismatch for logaddexp2({x}, {y})"
)
def test_logaddexp2_special_properties():
"""Test special mathematical properties of logaddexp2"""
# logaddexp2(x, x) = x + 1 (since log2(2^x + 2^x) = log2(2 * 2^x) = log2(2) + log2(2^x) = 1 + x)
x = QuadPrecision("2.0")
result = np.logaddexp2(x, x)
expected = float(x) + 1.0
np.testing.assert_allclose(float(result), expected, rtol=1e-14)
# logaddexp2(x, -inf) = x
x = QuadPrecision("5.0")
result = np.logaddexp2(x, QuadPrecision("-inf"))
np.testing.assert_allclose(float(result), float(x), rtol=1e-14)
# logaddexp2(-inf, x) = x
result = np.logaddexp2(QuadPrecision("-inf"), x)
np.testing.assert_allclose(float(result), float(x), rtol=1e-14)
# logaddexp2(-inf, -inf) = -inf
result = np.logaddexp2(QuadPrecision("-inf"), QuadPrecision("-inf"))
assert np.isinf(float(result)) and float(result) < 0
# logaddexp2(inf, anything) = inf
result = np.logaddexp2(QuadPrecision("inf"), QuadPrecision("100.0"))
assert np.isinf(float(result)) and float(result) > 0
# logaddexp2(anything, inf) = inf
result = np.logaddexp2(QuadPrecision("100.0"), QuadPrecision("inf"))
assert np.isinf(float(result)) and float(result) > 0
# Commutativity: logaddexp2(x, y) = logaddexp2(y, x)
x = QuadPrecision("3.0")
y = QuadPrecision("5.0")
result1 = np.logaddexp2(x, y)
result2 = np.logaddexp2(y, x)
np.testing.assert_allclose(float(result1), float(result2), rtol=1e-14)
# Relationship with logaddexp: logaddexp2(x, y) = logaddexp(x*ln2, y*ln2) / ln2
x = QuadPrecision("2.0")
y = QuadPrecision("3.0")
result_logaddexp2 = np.logaddexp2(x, y)
ln2 = np.log(2.0)
result_logaddexp = np.logaddexp(float(x) * ln2, float(y) * ln2) / ln2
np.testing.assert_allclose(float(result_logaddexp2), result_logaddexp, rtol=1e-13)
@pytest.mark.parametrize(
"x_val",
[
0.0, 1.0, 2.0, -1.0, -2.0,
0.5, -0.5,
100.0, 1000.0, -100.0, -1000.0,
1e-10, -1e-10, 1e-20, -1e-20,
float("inf"), float("-inf"), float("nan"), float("-nan"), -0.0
]
)
@pytest.mark.parametrize(
"y_val",
[
0.0, 1.0, 2.0, -1.0, -2.0,
0.5, -0.5,
100.0, 1000.0, -100.0, -1000.0,
1e-10, -1e-10, 1e-20, -1e-20,
float("inf"), float("-inf"), float("nan"), float("-nan"), -0.0
]
)
def test_true_divide(x_val, y_val):
"""Test true_divide ufunc with comprehensive edge cases"""
x_quad = QuadPrecision(str(x_val))
y_quad = QuadPrecision(str(y_val))
# Compute using QuadPrecision
result_quad = np.true_divide(x_quad, y_quad)
# Compute using float64 for comparison
result_float64 = np.true_divide(np.float64(x_val), np.float64(y_val))
# Compare results
if np.isnan(result_float64):
assert np.isnan(float(result_quad)), f"Expected NaN for true_divide({x_val}, {y_val})"
elif np.isinf(result_float64):
assert np.isinf(float(result_quad)), f"Expected inf for true_divide({x_val}, {y_val})"
assert np.sign(float(result_quad)) == np.sign(result_float64), f"Sign mismatch for true_divide({x_val}, {y_val})"
else:
# For finite results, check relative tolerance
np.testing.assert_allclose(
float(result_quad), result_float64, rtol=1e-14,
err_msg=f"Mismatch for true_divide({x_val}, {y_val})"
)
def test_true_divide_special_properties():
"""Test special mathematical properties of true_divide"""
# Division by 1 returns the original value
x = QuadPrecision("42.123456789")
result = np.true_divide(x, QuadPrecision("1.0"))
np.testing.assert_allclose(float(result), float(x), rtol=1e-30)
# Division of 0 by any non-zero number is 0
result = np.true_divide(QuadPrecision("0.0"), QuadPrecision("5.0"))
assert float(result) == 0.0
# Division by 0 gives inf (with appropriate sign)
result = np.true_divide(QuadPrecision("1.0"), QuadPrecision("0.0"))
assert np.isinf(float(result)) and float(result) > 0
result = np.true_divide(QuadPrecision("-1.0"), QuadPrecision("0.0"))
assert np.isinf(float(result)) and float(result) < 0
# 0 / 0 = NaN
result = np.true_divide(QuadPrecision("0.0"), QuadPrecision("0.0"))
assert np.isnan(float(result))
# inf / inf = NaN
result = np.true_divide(QuadPrecision("inf"), QuadPrecision("inf"))
assert np.isnan(float(result))
# inf / finite = inf
result = np.true_divide(QuadPrecision("inf"), QuadPrecision("100.0"))
assert np.isinf(float(result)) and float(result) > 0
# finite / inf = 0
result = np.true_divide(QuadPrecision("100.0"), QuadPrecision("inf"))
assert float(result) == 0.0
# Self-division (x / x) = 1 for finite non-zero x
x = QuadPrecision("7.123456789")
result = np.true_divide(x, x)
np.testing.assert_allclose(float(result), 1.0, rtol=1e-30)
# Sign preservation: (-x) / y = -(x / y)
x = QuadPrecision("5.5")
y = QuadPrecision("2.2")
result1 = np.true_divide(-x, y)
result2 = -np.true_divide(x, y)
np.testing.assert_allclose(float(result1), float(result2), rtol=1e-30)
# Sign rule: negative / negative = positive
result = np.true_divide(QuadPrecision("-6.0"), QuadPrecision("-2.0"))
assert float(result) > 0
np.testing.assert_allclose(float(result), 3.0, rtol=1e-30)
@pytest.mark.parametrize(
"x_val",
[
0.0, 1.0, 2.0, -1.0, -2.0,
0.5, -0.5,
100.0, 1000.0, -100.0, -1000.0,
1e-10, -1e-10, 1e-20, -1e-20,
float("inf"), float("-inf"), float("nan"), float("-nan"), -0.0
]
)
@pytest.mark.parametrize(
"y_val",
[
0.0, 1.0, 2.0, -1.0, -2.0,
0.5, -0.5,
100.0, 1000.0, -100.0, -1000.0,
1e-10, -1e-10, 1e-20, -1e-20,
float("inf"), float("-inf"), float("nan"), float("-nan"), -0.0
]
)
def test_floor_divide(x_val, y_val):
"""Test floor_divide ufunc with comprehensive edge cases"""
x_quad = QuadPrecision(str(x_val))
y_quad = QuadPrecision(str(y_val))
# Compute using QuadPrecision
result_quad = np.floor_divide(x_quad, y_quad)
# Compute using float64 for comparison
result_float64 = np.floor_divide(np.float64(x_val), np.float64(y_val))
# Compare results
if np.isnan(result_float64):
assert np.isnan(float(result_quad)), f"Expected NaN for floor_divide({x_val}, {y_val})"
elif np.isinf(result_float64):
assert np.isinf(float(result_quad)), f"Expected inf for floor_divide({x_val}, {y_val})"
assert np.sign(float(result_quad)) == np.sign(result_float64), f"Sign mismatch for floor_divide({x_val}, {y_val})"
else:
# For finite results, check relative tolerance
# Use absolute tolerance for large numbers due to float64 precision limits
atol = max(1e-10, abs(result_float64) * 1e-9) if abs(result_float64) > 1e6 else 1e-10
np.testing.assert_allclose(
float(result_quad), result_float64, rtol=1e-12, atol=atol,
err_msg=f"Mismatch for floor_divide({x_val}, {y_val})"
)
def test_floor_divide_special_properties():
"""Test special mathematical properties of floor_divide"""
# floor_divide(x, 1) = floor(x)
x = QuadPrecision("42.7")
result = np.floor_divide(x, QuadPrecision("1.0"))
np.testing.assert_allclose(float(result), 42.0, rtol=1e-30)
# floor_divide(0, non-zero) = 0
result = np.floor_divide(QuadPrecision("0.0"), QuadPrecision("5.0"))
assert float(result) == 0.0
# floor_divide by 0 gives inf (with appropriate sign)
result = np.floor_divide(QuadPrecision("1.0"), QuadPrecision("0.0"))
assert np.isinf(float(result)) and float(result) > 0
result = np.floor_divide(QuadPrecision("-1.0"), QuadPrecision("0.0"))
assert np.isinf(float(result)) and float(result) < 0
# 0 / 0 = NaN
result = np.floor_divide(QuadPrecision("0.0"), QuadPrecision("0.0"))
assert np.isnan(float(result))
# inf / inf = NaN
result = np.floor_divide(QuadPrecision("inf"), QuadPrecision("inf"))
assert np.isnan(float(result))
# inf / finite_nonzero = NaN (NumPy behavior)
result = np.floor_divide(QuadPrecision("inf"), QuadPrecision("100.0"))
assert np.isnan(float(result))
# finite / inf = 0
result = np.floor_divide(QuadPrecision("100.0"), QuadPrecision("inf"))
assert float(result) == 0.0
# floor_divide rounds toward negative infinity
result = np.floor_divide(QuadPrecision("7.0"), QuadPrecision("3.0"))
assert float(result) == 2.0 # floor(7/3) = floor(2.333...) = 2
result = np.floor_divide(QuadPrecision("-7.0"), QuadPrecision("3.0"))
assert float(result) == -3.0 # floor(-7/3) = floor(-2.333...) = -3
result = np.floor_divide(QuadPrecision("7.0"), QuadPrecision("-3.0"))
assert float(result) == -3.0 # floor(7/-3) = floor(-2.333...) = -3
result = np.floor_divide(QuadPrecision("-7.0"), QuadPrecision("-3.0"))
assert float(result) == 2.0 # floor(-7/-3) = floor(2.333...) = 2
# floor_divide(x, x) = 1 for positive finite non-zero x
x = QuadPrecision("7.123456789")
result = np.floor_divide(x, x)
np.testing.assert_allclose(float(result), 1.0, rtol=1e-30)
# Relationship with floor and true_divide
x = QuadPrecision("10.5")
y = QuadPrecision("3.2")
result_floor_divide = np.floor_divide(x, y)
result_floor_true_divide = np.floor(np.true_divide(x, y))
np.testing.assert_allclose(float(result_floor_divide), float(result_floor_true_divide), rtol=1e-30)
@pytest.mark.parametrize("x_val,y_val", [
(x, y) for x in [-1e10, -100.0, -7.0, -1.0, -0.5, -0.0, 0.0, 0.5, 1.0, 7.0, 100.0, 1e10,
float('inf'), float('-inf'), float('nan'),
-6.0, 6.0, -0.1, 0.1, -3.14159, 3.14159]
for y in [-1e10, -100.0, -3.0, -1.0, -0.5, -0.0, 0.0, 0.5, 1.0, 3.0, 100.0, 1e10,
float('inf'), float('-inf'), float('nan'),
-2.0, 2.0, -0.25, 0.25, -1.5, 1.5]
])
def test_fmod(x_val, y_val):
"""Test fmod ufunc with comprehensive edge cases"""
x_quad = QuadPrecision(str(x_val))
y_quad = QuadPrecision(str(y_val))
# Compute using QuadPrecision
result_quad = np.fmod(x_quad, y_quad)
# Compute using float64 for comparison
result_float64 = np.fmod(np.float64(x_val), np.float64(y_val))
# Compare results
if np.isnan(result_float64):
assert np.isnan(float(result_quad)), f"Expected NaN for fmod({x_val}, {y_val})"
elif np.isinf(result_float64):
assert np.isinf(float(result_quad)), f"Expected inf for fmod({x_val}, {y_val})"
assert np.sign(float(result_quad)) == np.sign(result_float64), f"Sign mismatch for fmod({x_val}, {y_val})"
else:
# For finite results, check relative tolerance
atol = max(1e-10, abs(result_float64) * 1e-9) if abs(result_float64) > 1e6 else 1e-10
np.testing.assert_allclose(
float(result_quad), result_float64, rtol=1e-12, atol=atol,
err_msg=f"Mismatch for fmod({x_val}, {y_val})"
)
# Critical: Check sign preservation for zero results
if result_float64 == 0.0:
assert np.signbit(result_quad) == np.signbit(result_float64), \
f"Sign mismatch for zero result: fmod({x_val}, {y_val}), " \
f"expected signbit={np.signbit(result_float64)}, got signbit={np.signbit(result_quad)}"
def test_fmod_special_properties():
"""Test special mathematical properties of fmod"""
# fmod(x, 1) gives fractional part of x (with sign preserved)
x = QuadPrecision("42.7")
result = np.fmod(x, QuadPrecision("1.0"))
np.testing.assert_allclose(float(result), 0.7, rtol=1e-15, atol=1e-15)
# fmod(0, non-zero) = 0 with correct sign
result = np.fmod(QuadPrecision("0.0"), QuadPrecision("5.0"))
assert float(result) == 0.0 and not np.signbit(result)
result = np.fmod(QuadPrecision("-0.0"), QuadPrecision("5.0"))
assert float(result) == 0.0 and np.signbit(result)
# fmod by 0 gives NaN
result = np.fmod(QuadPrecision("1.0"), QuadPrecision("0.0"))
assert np.isnan(float(result))
result = np.fmod(QuadPrecision("-1.0"), QuadPrecision("0.0"))
assert np.isnan(float(result))
# 0 fmod 0 = NaN
result = np.fmod(QuadPrecision("0.0"), QuadPrecision("0.0"))
assert np.isnan(float(result))
# inf fmod x = NaN
result = np.fmod(QuadPrecision("inf"), QuadPrecision("100.0"))
assert np.isnan(float(result))
result = np.fmod(QuadPrecision("-inf"), QuadPrecision("100.0"))
assert np.isnan(float(result))
# x fmod inf = x (for finite x)
result = np.fmod(QuadPrecision("100.0"), QuadPrecision("inf"))
np.testing.assert_allclose(float(result), 100.0, rtol=1e-30)
result = np.fmod(QuadPrecision("-100.0"), QuadPrecision("inf"))
np.testing.assert_allclose(float(result), -100.0, rtol=1e-30)
# inf fmod inf = NaN
result = np.fmod(QuadPrecision("inf"), QuadPrecision("inf"))
assert np.isnan(float(result))
# fmod uses truncated division (rounds toward zero)
# Result has same sign as dividend (first argument)
result = np.fmod(QuadPrecision("7.0"), QuadPrecision("3.0"))
assert float(result) == 1.0 # 7 - trunc(7/3)*3 = 7 - 2*3 = 1
result = np.fmod(QuadPrecision("-7.0"), QuadPrecision("3.0"))
assert float(result) == -1.0 # -7 - trunc(-7/3)*3 = -7 - (-2)*3 = -1
result = np.fmod(QuadPrecision("7.0"), QuadPrecision("-3.0"))
assert float(result) == 1.0 # 7 - trunc(7/-3)*(-3) = 7 - (-2)*(-3) = 1
result = np.fmod(QuadPrecision("-7.0"), QuadPrecision("-3.0"))
assert float(result) == -1.0 # -7 - trunc(-7/-3)*(-3) = -7 - 2*(-3) = -1
# Sign preservation when result is exactly zero
result = np.fmod(QuadPrecision("6.0"), QuadPrecision("3.0"))
assert float(result) == 0.0 and not np.signbit(result)
result = np.fmod(QuadPrecision("-6.0"), QuadPrecision("3.0"))
assert float(result) == 0.0 and np.signbit(result)
result = np.fmod(QuadPrecision("6.0"), QuadPrecision("-3.0"))
assert float(result) == 0.0 and not np.signbit(result)
result = np.fmod(QuadPrecision("-6.0"), QuadPrecision("-3.0"))
assert float(result) == 0.0 and np.signbit(result)
# Difference from mod/remainder (which uses floor division)
# fmod result has sign of dividend, mod result has sign of divisor
x = QuadPrecision("-7.0")
y = QuadPrecision("3.0")
fmod_result = np.fmod(x, y)
mod_result = np.remainder(x, y)
assert float(fmod_result) == -1.0 # sign of dividend (negative)
assert float(mod_result) == 2.0 # sign of divisor (positive)
# Relationship: x = trunc(x/y) * y + fmod(x, y)
x = QuadPrecision("10.5")
y = QuadPrecision("3.2")
quotient = np.trunc(np.true_divide(x, y))
remainder = np.fmod(x, y)
reconstructed = np.add(np.multiply(quotient, y), remainder)
np.testing.assert_allclose(float(reconstructed), float(x), rtol=1e-30)
def test_inf():
assert QuadPrecision("inf") > QuadPrecision("1e1000")
assert np.signbit(QuadPrecision("inf")) == 0
assert QuadPrecision("-inf") < QuadPrecision("-1e1000")
assert np.signbit(QuadPrecision("-inf")) == 1
def test_dtype_creation():
dtype = QuadPrecDType()
assert isinstance(dtype, np.dtype)
assert dtype.name == "QuadPrecDType128"
def test_array_creation():
arr = np.array([1, 2, 3], dtype=QuadPrecDType())
assert arr.dtype.name == "QuadPrecDType128"
assert all(isinstance(x, QuadPrecision) for x in arr)
def test_array_operations():
arr1 = np.array(
[QuadPrecision("1.5"), QuadPrecision("2.5"), QuadPrecision("3.5")])
arr2 = np.array(
[QuadPrecision("0.5"), QuadPrecision("1.0"), QuadPrecision("1.5")])
result = arr1 + arr2
expected = np.array(
[QuadPrecision("2.0"), QuadPrecision("3.5"), QuadPrecision("5.0")])
assert all(x == y for x, y in zip(result, expected))
@pytest.mark.parametrize("backend", ["sleef", "longdouble"])