-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat8_test.go
More file actions
1503 lines (1350 loc) · 52.1 KB
/
float8_test.go
File metadata and controls
1503 lines (1350 loc) · 52.1 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
package float8
import (
"math"
"testing"
)
// abs returns the absolute value of a float32
func abs(f float32) float32 {
if f < 0 {
return -f
}
return f
}
// Test conversion functions
func TestToFloat8Basic(t *testing.T) {
tests := []struct {
input float32
expected Float8
name string
}{
{0.0, PositiveZero, "positive zero"},
{float32(math.Copysign(0.0, -1.0)), 0x80, "negative zero"}, // 0x80 is the correct representation for -0.0
{1.0, 0x38, "one"},
{-1.0, 0xB8, "negative one"},
{2.0, 0x40, "two"},
{0.5, 0x30, "half"},
{float32(math.Inf(1)), PositiveInfinity, "positive infinity"},
{float32(math.Inf(-1)), NegativeInfinity, "negative infinity"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := ToFloat8(test.input)
if result != test.expected {
t.Errorf("ToFloat8(%g) = 0x%02x, expected 0x%02x",
test.input, result, test.expected)
}
})
}
}
func TestToFloat8NaN(t *testing.T) {
result := ToFloat8(float32(math.NaN()))
if result != NaN {
t.Errorf("ToFloat8(NaN) = 0x%02x, expected 0x%02x (NaN)",
result, NaN)
}
}
func TestToFloat8WithModeStrict(t *testing.T) {
// Test overflow in strict mode
_, err := ToFloat8WithMode(1e10, ModeStrict)
if err == nil {
t.Error("Expected overflow error in strict mode")
}
// Test underflow in strict mode
_, err = ToFloat8WithMode(1e-10, ModeStrict)
if err == nil {
t.Error("Expected underflow error in strict mode")
}
// Test NaN in strict mode
_, err = ToFloat8WithMode(float32(math.NaN()), ModeStrict)
if err == nil {
t.Error("Expected NaN error in strict mode")
}
}
func TestToFloat32(t *testing.T) {
tests := []struct {
input Float8
expected float32
name string
}{
{PositiveZero, 0.0, "positive zero"},
{NegativeZero, -0.0, "negative zero"}, // Negative zero should preserve its sign
{0x38, 1.0, "one"},
{0xB8, -1.0, "negative one"},
{0x40, 2.0, "two"},
{0x30, 0.5, "half"},
{0x48, 4.0, "four"},
{0x28, 0.25, "quarter"},
{0x20, 0.125, "one eighth"},
{0x10, 0.03125, "denormalized small positive"}, // Smallest positive denormalized number
{0x90, -0.03125, "denormalized small negative"}, // Smallest negative denormalized number
{0x7E, 448.0, "max normal positive"}, // Maximum normal positive number (0x7E = 126 -> 2^6 * 1.75 = 64 * 7.0 = 448)
{0xFE, -448.0, "max normal negative"}, // Maximum normal negative number
{0x78, float32(math.Inf(1)), "positive infinity"}, // Positive infinity (IEEE 754 E4M3FN)
{0xF8, float32(math.Inf(-1)), "negative infinity"}, // Negative infinity (IEEE 754 E4M3FN)
{0x7F, float32(math.NaN()), "NaN"}, // NaN (IEEE 754 E4M3FN)
{0xFF, float32(math.NaN()), "NaN"}, // NaN (IEEE 754 E4M3FN)
}
// Test with lookup table disabled to ensure algorithmic path is tested
t.Run("algorithmic path", func(t *testing.T) {
// Save current state
table := conversionTable
conversionTable = nil
defer func() { conversionTable = table }() // Restore after test
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.input.ToFloat32()
// Special handling for infinities
if math.IsInf(float64(test.expected), 0) {
if !math.IsInf(float64(result), 0) || math.Signbit(float64(result)) != math.Signbit(float64(test.expected)) {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected %g",
test.input, result, test.expected)
}
return
}
// Special handling for zeros to ensure sign is correct
if result == 0 || result == -0.0 || test.expected == 0 || test.expected == -0.0 {
// For zero values, only check that both are zero (regardless of sign)
if (result != 0 && result != -0.0) || (test.expected != 0 && test.expected != -0.0) {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected a zero value",
test.input, result)
}
return
}
// For other values, allow a small tolerance for floating-point imprecision
if result != test.expected && math.Abs(float64(result-test.expected)) > 1e-7 {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected %g",
test.input, result, test.expected)
}
})
}
})
// Test with lookup table enabled (if available)
t.Run("lookup table path", func(t *testing.T) {
// Ensure lookup table is enabled
if conversionTable == nil {
initConversionTable()
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.input.ToFloat32()
// Special handling for infinities
if math.IsInf(float64(test.expected), 0) {
if !math.IsInf(float64(result), 0) || math.Signbit(float64(result)) != math.Signbit(float64(test.expected)) {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected %g",
test.input, result, test.expected)
}
return
}
// Special handling for zeros to ensure sign is correct
if result == 0 || result == -0.0 || test.expected == 0 || test.expected == -0.0 {
// For zero values, only check that both are zero (regardless of sign)
if (result != 0 && result != -0.0) || (test.expected != 0 && test.expected != -0.0) {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected a zero value",
test.input, result)
}
return
}
// For other values, allow a small tolerance for floating-point imprecision
if result != test.expected && math.Abs(float64(result-test.expected)) > 1e-7 {
t.Errorf("Float8(0x%02x).ToFloat32() = %g, expected %g",
test.input, result, test.expected)
}
})
}
})
}
func TestRoundTripConversion(t *testing.T) {
// Test that converting Float8 -> Float32 -> Float8 is identity
for i := 0; i < 256; i++ {
f8 := Float8(i)
f32 := f8.ToFloat32()
f8_back := ToFloat8(f32)
// Special handling for NaN values - all NaN values should round-trip to some NaN
if f8.IsNaN() && f8_back.IsNaN() {
continue // NaN values may not preserve exact bit patterns
}
if f8 != f8_back {
t.Errorf("Round trip failed for 0x%02X (input) -> %v (float32) -> 0x%02X (output)", uint8(f8), f32, uint8(f8_back))
}
}
}
// Test arithmetic operations
func TestAddBasic(t *testing.T) {
// Save the current configuration to restore it later
origConfig := DefaultConfig()
// Restore the original configuration after the test
defer Configure(origConfig)
tests := []struct {
a, b Float8
expected Float8
name string
imprecise bool // true if the result might be imprecise due to floating-point limitations
}{
{PositiveZero, PositiveZero, PositiveZero, "zero + zero", false},
{ToFloat8(1.0), PositiveZero, ToFloat8(1.0), "one + zero", false},
{ToFloat8(1.0), ToFloat8(1.0), ToFloat8(2.0), "one + one", false},
{ToFloat8(2.0), ToFloat8(3.0), ToFloat8(5.0), "two + three", false},
{PositiveInfinity, ToFloat8(1.0), PositiveInfinity, "inf + one", false},
{PositiveInfinity, NegativeInfinity, NaN, "inf + (-inf)", false},
{ToFloat8(1.5), ToFloat8(1.5), ToFloat8(3.0), "1.5 + 1.5", false},
{ToFloat8(-1.0), ToFloat8(1.0), ToFloat8(0.0), "-1 + 1", false},
{ToFloat8(0.5), ToFloat8(0.5), ToFloat8(1.0), "0.5 + 0.5", false},
{ToFloat8(0.1), ToFloat8(0.2), ToFloat8(0.3125), "0.1 + 0.2 (imprecise)", true},
}
// Test with different arithmetic modes
modes := []struct {
name ArithmeticMode
desc string
}{
{ArithmeticAuto, "auto"},
{ArithmeticAlgorithmic, "algorithmic"},
{ArithmeticLookup, "lookup"},
}
for _, mode := range modes {
t.Run(mode.desc, func(t *testing.T) {
// Enable or disable lookup tables based on mode
if mode.name == ArithmeticLookup {
EnableFastArithmetic()
} else if mode.name == ArithmeticAlgorithmic {
DisableFastArithmetic()
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Test AddWithMode with the current mode
result := AddWithMode(test.a, test.b, mode.name)
// For imprecise operations, allow a small tolerance
if test.imprecise {
tolerance := ToFloat8(0.01)
diff := result - test.expected
absDiff := diff
if absDiff < 0 {
t.Logf("Negative diff, taking absolute value")
t.Logf("diff type: %T, tolerance type: %T", diff, tolerance)
t.Logf("diff value: %v, tolerance value: %v", diff, tolerance)
t.Logf("result: %v, expected: %v", result, test.expected)
t.Logf("result bits: %08b, expected bits: %08b", result, test.expected)
t.Logf("result float: %v, expected float: %v", result.ToFloat32(), test.expected.ToFloat32())
t.Logf("result == expected: %v", result == test.expected)
t.Logf("diff < -tolerance: %v, diff > tolerance: %v", diff < -tolerance, diff > tolerance)
t.Logf("diff < -tolerance: %v < -%v: %v, diff > tolerance: %v > %v: %v",
diff, tolerance, diff < -tolerance,
diff, tolerance, diff > tolerance)
absDiff = -absDiff
}
if absDiff > tolerance {
t.Errorf("AddWithMode(%s, %s, %s) = %s (0x%02x, %v), expected close to %s (0x%02x, %v), diff=%v, tolerance=%v, absDiff=%v",
test.a, test.b, mode.desc,
result, result, result.ToFloat32(),
test.expected, test.expected, test.expected.ToFloat32(),
diff, tolerance, absDiff)
} else {
// If we get here, the result is within tolerance, so the test passes
t.Logf("Test passed: result %v is within tolerance %v of expected %v (diff=%v, absDiff=%v)",
result, tolerance, test.expected, diff, absDiff)
return
}
} else if result != test.expected {
t.Errorf("AddWithMode(0x%02x, 0%02x, %s) = 0x%02x, expected 0x%02x",
test.a, test.b, mode.desc, result, test.expected)
}
})
}
})
}
// Test that Add uses the default mode
t.Run("default_mode", func(t *testing.T) {
// Set to algorithmic mode and test Add uses it
DisableFastArithmetic()
result1 := Add(ToFloat8(1.0), ToFloat8(2.0))
// Set to lookup mode and test Add uses it
EnableFastArithmetic()
result2 := Add(ToFloat8(1.0), ToFloat8(2.0))
// Both should give the same result (just testing the function works with defaults)
if result1 != result2 {
t.Errorf("Add with different default modes gave different results: 0x%02x vs 0x%02x",
result1, result2)
}
})
}
func TestSubBasic(t *testing.T) {
// Save the current configuration to restore it later
origConfig := DefaultConfig()
// Restore the original configuration after the test
defer Configure(origConfig)
tests := []struct {
a, b Float8
expected Float8
name string
imprecise bool // true if the result might be imprecise due to floating-point limitations
}{
{PositiveZero, PositiveZero, PositiveZero, "zero - zero", false},
{ToFloat8(1.0), PositiveZero, ToFloat8(1.0), "one - zero", false},
{ToFloat8(3.0), ToFloat8(1.0), ToFloat8(2.0), "three - one", false},
{ToFloat8(1.0), ToFloat8(3.0), ToFloat8(-2.0), "one - three", false},
{ToFloat8(0.5), ToFloat8(0.25), ToFloat8(0.25), "0.5 - 0.25", false},
{ToFloat8(0.3), ToFloat8(0.1), ToFloat8(0.2), "0.3 - 0.1 (imprecise)", true},
{PositiveInfinity, ToFloat8(1.0), PositiveInfinity, "inf - one", false},
{PositiveInfinity, NegativeInfinity, PositiveInfinity, "inf - (-inf)", false},
{ToFloat8(1.5), ToFloat8(0.5), ToFloat8(1.0), "1.5 - 0.5", false},
}
// Test with different arithmetic modes
modes := []struct {
name ArithmeticMode
desc string
}{
{ArithmeticAuto, "auto"},
{ArithmeticAlgorithmic, "algorithmic"},
{ArithmeticLookup, "lookup"},
}
for _, mode := range modes {
t.Run(mode.desc, func(t *testing.T) {
// Enable or disable lookup tables based on mode
if mode.name == ArithmeticLookup {
EnableFastArithmetic()
} else if mode.name == ArithmeticAlgorithmic {
DisableFastArithmetic()
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Test SubWithMode with the current mode
result := SubWithMode(test.a, test.b, mode.name)
// For imprecise operations, allow a small tolerance
if test.imprecise {
tolerance := ToFloat8(0.01)
diff := result - test.expected
absDiff := diff
if absDiff < 0 {
t.Logf("Negative diff, taking absolute value")
t.Logf("diff type: %T, tolerance type: %T", diff, tolerance)
t.Logf("diff value: %v, tolerance value: %v", diff, tolerance)
t.Logf("result: %v, expected: %v", result, test.expected)
t.Logf("result bits: %08b, expected bits: %08b", result, test.expected)
t.Logf("result float: %v, expected float: %v", result.ToFloat32(), test.expected.ToFloat32())
t.Logf("result == expected: %v", result == test.expected)
t.Logf("diff < -tolerance: %v, diff > tolerance: %v", diff < -tolerance, diff > tolerance)
t.Logf("diff < -tolerance: %v < -%v: %v, diff > tolerance: %v > %v: %v",
diff, tolerance, diff < -tolerance,
diff, tolerance, diff > tolerance)
absDiff = -absDiff
}
if absDiff > tolerance {
t.Errorf("SubWithMode(%s, %s, %s) = %s (0x%02x, %v), expected close to %s (0x%02x, %v), diff=%v, tolerance=%v, absDiff=%v",
test.a, test.b, mode.desc,
result, result, result.ToFloat32(),
test.expected, test.expected, test.expected.ToFloat32(),
diff, tolerance, absDiff)
} else {
// If we get here, the result is within tolerance, so the test passes
t.Logf("Test passed: result %v is within tolerance %v of expected %v (diff=%v, absDiff=%v)",
result, tolerance, test.expected, diff, absDiff)
return
}
} else if result != test.expected {
t.Errorf("SubWithMode(0x%02x, 0%02x, %s) = 0x%02x, expected 0x%02x",
test.a, test.b, mode.desc, result, test.expected)
}
})
}
})
}
// Test that Sub uses the default mode
t.Run("default_mode", func(t *testing.T) {
// Set to algorithmic mode and test Sub uses it
DisableFastArithmetic()
result1 := Sub(ToFloat8(1.0), ToFloat8(0.5))
// Set to lookup mode and test Sub uses it
EnableFastArithmetic()
result2 := Sub(ToFloat8(1.0), ToFloat8(0.5))
// Both should give the same result (just testing the function works with defaults)
if result1 != result2 {
t.Errorf("Sub with different default modes gave different results: 0x%02x vs 0x%02x",
result1, result2)
}
})
}
func TestMulBasic(t *testing.T) {
// Save the current configuration to restore it later
origConfig := DefaultConfig()
// Restore the original configuration after the test
defer Configure(origConfig)
tests := []struct {
a, b Float8
expected Float8
name string
imprecise bool // true if the result might be imprecise due to floating-point limitations
}{
// Basic multiplication
{PositiveZero, ToFloat8(1.0), PositiveZero, "zero * one", false},
{ToFloat8(1.0), ToFloat8(1.0), ToFloat8(1.0), "one * one", false},
{ToFloat8(2.0), ToFloat8(3.0), ToFloat8(6.0), "two * three", false},
{ToFloat8(-1.0), ToFloat8(1.0), ToFloat8(-1.0), "(-one) * one", false},
// Zero multiplication
{PositiveZero, PositiveZero, PositiveZero, "+0 * +0", false},
{NegativeZero, NegativeZero, PositiveZero, "-0 * -0", false},
{PositiveZero, NegativeZero, NegativeZero, "+0 * -0", false},
{NegativeZero, PositiveZero, NegativeZero, "-0 * +0", false},
{PositiveZero, ToFloat8(5.0), PositiveZero, "+0 * 5", false},
{ToFloat8(5.0), NegativeZero, NegativeZero, "5 * -0", false},
// Infinity multiplication
{PositiveInfinity, ToFloat8(2.0), PositiveInfinity, "+Inf * 2", false},
{NegativeInfinity, ToFloat8(2.0), NegativeInfinity, "-Inf * 2", false},
{PositiveInfinity, ToFloat8(-2.0), NegativeInfinity, "+Inf * -2", false},
{NegativeInfinity, ToFloat8(-2.0), PositiveInfinity, "-Inf * -2", false},
{PositiveInfinity, PositiveInfinity, PositiveInfinity, "+Inf * +Inf", false},
{PositiveInfinity, NegativeInfinity, NegativeInfinity, "+Inf * -Inf", false},
{NegativeInfinity, NegativeInfinity, PositiveInfinity, "-Inf * -Inf", false},
// NaN cases
{NaN, ToFloat8(1.0), NaN, "NaN * 1", false},
{ToFloat8(1.0), NaN, NaN, "1 * NaN", false},
{NaN, NaN, NaN, "NaN * NaN", false},
{NaN, PositiveInfinity, NaN, "NaN * +Inf", false},
{PositiveInfinity, NaN, NaN, "+Inf * NaN", false},
// Fractional multiplication
{ToFloat8(0.5), ToFloat8(0.5), ToFloat8(0.25), "0.5 * 0.5", false},
{ToFloat8(0.3), ToFloat8(0.3), ToFloat8(0.09), "0.3 * 0.3 (imprecise)", true},
{ToFloat8(1.5), ToFloat8(0.5), ToFloat8(0.75), "1.5 * 0.5", false},
{ToFloat8(1.5), ToFloat8(2.0), ToFloat8(3.0), "1.5 * 2.0", false},
// Edge cases
{PositiveInfinity, ToFloat8(0.0), NaN, "+Inf * 0", false}, // Should be NaN per IEEE 754
{NegativeInfinity, ToFloat8(0.0), NaN, "-Inf * 0", false}, // Should be NaN per IEEE 754
{ToFloat8(1.0), PositiveInfinity, PositiveInfinity, "1 * +Inf", false},
{ToFloat8(-1.0), PositiveInfinity, NegativeInfinity, "-1 * +Inf", false},
}
// Test with different arithmetic modes
modes := []struct {
name ArithmeticMode
desc string
}{
{ArithmeticAuto, "auto"},
{ArithmeticAlgorithmic, "algorithmic"},
{ArithmeticLookup, "lookup"},
}
for _, mode := range modes {
t.Run(mode.desc, func(t *testing.T) {
// Enable or disable lookup tables based on mode
if mode.name == ArithmeticLookup {
EnableFastArithmetic()
} else if mode.name == ArithmeticAlgorithmic {
DisableFastArithmetic()
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Test MulWithMode with the current mode
result := MulWithMode(test.a, test.b, mode.name)
// For imprecise operations, allow a small tolerance
if test.imprecise {
tolerance := ToFloat8(0.01)
diff := result - test.expected
absDiff := diff
if absDiff < 0 {
t.Logf("Negative diff, taking absolute value")
t.Logf("diff type: %T, tolerance type: %T", diff, tolerance)
t.Logf("diff value: %v, tolerance value: %v", diff, tolerance)
t.Logf("result: %v, expected: %v", result, test.expected)
t.Logf("result bits: %08b, expected bits: %08b", result, test.expected)
t.Logf("result float: %v, expected float: %v", result.ToFloat32(), test.expected.ToFloat32())
t.Logf("result == expected: %v", result == test.expected)
t.Logf("diff < -tolerance: %v, diff > tolerance: %v", diff < -tolerance, diff > tolerance)
t.Logf("diff < -tolerance: %v < -%v: %v, diff > tolerance: %v > %v: %v",
diff, tolerance, diff < -tolerance,
diff, tolerance, diff > tolerance)
absDiff = -absDiff
}
if absDiff > tolerance {
t.Errorf("MulWithMode(%s, %s, %s) = %s (0x%02x, %v), expected close to %s (0x%02x, %v), diff=%v, tolerance=%v, absDiff=%v",
test.a, test.b, mode.desc,
result, result, result.ToFloat32(),
test.expected, test.expected, test.expected.ToFloat32(),
diff, tolerance, absDiff)
} else {
// If we get here, the result is within tolerance, so the test passes
t.Logf("Test passed: result %v is within tolerance %v of expected %v (diff=%v, absDiff=%v)",
result, tolerance, test.expected, diff, absDiff)
return
}
} else if result != test.expected {
t.Errorf("MulWithMode(0x%02x, 0%02x, %s) = 0x%02x, expected 0x%02x",
test.a, test.b, mode.desc, result, test.expected)
}
})
}
})
}
// Test that Mul uses the default mode
t.Run("default_mode", func(t *testing.T) {
// Set to algorithmic mode and test Mul uses it
DisableFastArithmetic()
result1 := Mul(ToFloat8(1.5), ToFloat8(2.0))
// Set to lookup mode and test Mul uses it
EnableFastArithmetic()
result2 := Mul(ToFloat8(1.5), ToFloat8(2.0))
// Both should give the same result (just testing the function works with defaults)
if result1 != result2 {
t.Errorf("Mul with different default modes gave different results: 0x%02x vs 0x%02x",
result1, result2)
}
})
}
func TestDivBasic(t *testing.T) {
// Save the current configuration to restore it later
origConfig := DefaultConfig()
// Restore the original configuration after the test
defer Configure(origConfig)
tests := []struct {
a, b Float8
expected Float8
name string
imprecise bool // true if the result might be imprecise due to floating-point limitations
}{
// Basic division
{PositiveZero, ToFloat8(1.0), PositiveZero, "zero / one", false},
{ToFloat8(6.0), ToFloat8(2.0), ToFloat8(3.0), "six / two", false},
{ToFloat8(1.0), ToFloat8(2.0), ToFloat8(0.5), "one / two", false},
{ToFloat8(1.0), ToFloat8(4.0), ToFloat8(0.25), "one / four", false},
{ToFloat8(9.0), ToFloat8(3.0), ToFloat8(3.0), "nine / three", false},
{ToFloat8(-1.0), ToFloat8(2.0), ToFloat8(-0.5), "-one / two", false},
{ToFloat8(-1.0), ToFloat8(-2.0), ToFloat8(0.5), "-one / -two", false},
// Division by zero
{ToFloat8(1.0), PositiveZero, PositiveInfinity, "one / +0", false},
{ToFloat8(-1.0), PositiveZero, NegativeInfinity, "-one / +0", false},
{ToFloat8(1.0), NegativeZero, NegativeInfinity, "one / -0", false},
{ToFloat8(-1.0), NegativeZero, PositiveInfinity, "-one / -0", false},
// Zero divided by non-zero
{PositiveZero, ToFloat8(2.0), PositiveZero, "+0 / two", false},
{NegativeZero, ToFloat8(2.0), NegativeZero, "-0 / two", false},
{PositiveZero, ToFloat8(-2.0), NegativeZero, "+0 / -two", false},
{NegativeZero, ToFloat8(-2.0), PositiveZero, "-0 / -two", false},
// Zero divided by zero (should return NaN per IEEE 754)
{PositiveZero, PositiveZero, NaN, "+0 / +0 (NaN case)", false},
{PositiveZero, NegativeZero, NaN, "+0 / -0 (NaN case)", false},
{NegativeZero, PositiveZero, NaN, "-0 / +0 (NaN case)", false},
{NegativeZero, NegativeZero, NaN, "-0 / -0 (NaN case)", false},
// Infinity cases
{PositiveInfinity, ToFloat8(2.0), PositiveInfinity, "+Inf / two", false},
{PositiveInfinity, ToFloat8(-2.0), NegativeInfinity, "+Inf / -two", false},
{NegativeInfinity, ToFloat8(2.0), NegativeInfinity, "-Inf / two", false},
{NegativeInfinity, ToFloat8(-2.0), PositiveInfinity, "-Inf / -two", false},
{ToFloat8(2.0), PositiveInfinity, PositiveZero, "two / +Inf", false},
{ToFloat8(-2.0), PositiveInfinity, NegativeZero, "-two / +Inf", false},
{ToFloat8(2.0), NegativeInfinity, NegativeZero, "two / -Inf", false},
{ToFloat8(-2.0), NegativeInfinity, PositiveZero, "-two / -Inf", false},
{PositiveInfinity, PositiveInfinity, NaN, "+Inf / +Inf (NaN case)", false},
{PositiveInfinity, NegativeInfinity, NaN, "+Inf / -Inf (NaN case)", false},
{NegativeInfinity, PositiveInfinity, NaN, "-Inf / +Inf (NaN case)", false},
{NegativeInfinity, NegativeInfinity, NaN, "-Inf / -Inf (NaN case)", false},
// NaN cases
{NaN, ToFloat8(2.0), NaN, "NaN / two", false},
{ToFloat8(2.0), NaN, NaN, "two / NaN", false},
{NaN, NaN, NaN, "NaN / NaN", false},
{NaN, PositiveInfinity, NaN, "NaN / +Inf", false},
{PositiveInfinity, NaN, NaN, "+Inf / NaN", false},
// Imprecise divisions
{ToFloat8(0.3), ToFloat8(0.1), ToFloat8(3.0), "0.3 / 0.1 (imprecise)", true},
{ToFloat8(1.0), ToFloat8(3.0), ToFloat8(0.333333), "one / three (imprecise)", true},
{ToFloat8(1.0), ToFloat8(10.0), ToFloat8(0.1), "one / ten (imprecise)", true},
{ToFloat8(1.0), ToFloat8(7.0), ToFloat8(0.142857), "one / seven (imprecise)", true},
}
// Test with different arithmetic modes
modes := []struct {
name ArithmeticMode
desc string
}{
{ArithmeticAuto, "auto"},
{ArithmeticAlgorithmic, "algorithmic"},
{ArithmeticLookup, "lookup"},
}
for _, mode := range modes {
t.Run(mode.desc, func(t *testing.T) {
// Enable or disable lookup tables based on mode
if mode.name == ArithmeticLookup {
EnableFastArithmetic()
} else if mode.name == ArithmeticAlgorithmic {
DisableFastArithmetic()
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Test DivWithMode with the current mode
result := DivWithMode(test.a, test.b, mode.name)
// For imprecise operations, allow a small tolerance
if test.imprecise {
tolerance := ToFloat8(0.01)
diff := result - test.expected
absDiff := diff
if absDiff < 0 {
t.Logf("Negative diff, taking absolute value")
t.Logf("diff type: %T, tolerance type: %T", diff, tolerance)
t.Logf("diff value: %v, tolerance value: %v", diff, tolerance)
t.Logf("result: %v, expected: %v", result, test.expected)
t.Logf("result bits: %08b, expected bits: %08b", result, test.expected)
t.Logf("result float: %v, expected float: %v", result.ToFloat32(), test.expected.ToFloat32())
t.Logf("result == expected: %v", result == test.expected)
t.Logf("diff < -tolerance: %v, diff > tolerance: %v", diff < -tolerance, diff > tolerance)
t.Logf("diff < -tolerance: %v < -%v: %v, diff > tolerance: %v > %v: %v",
diff, tolerance, diff < -tolerance,
diff, tolerance, diff > tolerance)
t.Logf("result / expected: %v, expected / result: %v",
result.ToFloat32()/test.expected.ToFloat32(),
test.expected.ToFloat32()/result.ToFloat32())
t.Logf("1 - (result / expected): %v, 1 - (expected / result): %v",
1-result.ToFloat32()/test.expected.ToFloat32(),
1-test.expected.ToFloat32()/result.ToFloat32())
absDiff = -absDiff
}
// For division, we should also check the relative error
// since small absolute differences can be significant for small numbers
relTolerance := float32(0.05) // 5% relative tolerance
resultF := result.ToFloat32()
expectedF := test.expected.ToFloat32()
// Avoid division by zero or very small numbers
if expectedF != 0 && abs(float32(absDiff)) > tolerance.ToFloat32() {
relDiff := abs(1 - (resultF / expectedF))
if relDiff > relTolerance {
t.Errorf("DivWithMode(%s, %s, %s) = %s (0x%02x, %v), expected close to %s (0x%02x, %v), diff=%v, absDiff=%v, relDiff=%.2f%%",
test.a, test.b, mode.desc,
result, result, resultF,
test.expected, test.expected, expectedF,
diff, absDiff, relDiff*100)
} else {
t.Logf("Test passed within relative tolerance: result %v is within %.2f%% of expected %v (diff=%v, absDiff=%v, relDiff=%.2f%%)",
result, relTolerance*100, test.expected, diff, absDiff, relDiff*100)
return
}
} else if absDiff > tolerance {
t.Errorf("DivWithMode(%s, %s, %s) = %s (0x%02x, %v), expected close to %s (0x%02x, %v), diff=%v, tolerance=%v, absDiff=%v",
test.a, test.b, mode.desc,
result, result, resultF,
test.expected, test.expected, expectedF,
diff, tolerance, absDiff)
} else {
// If we get here, the result is within tolerance, so the test passes
t.Logf("Test passed: result %v is within tolerance %v of expected %v (diff=%v, absDiff=%v)",
result, tolerance, test.expected, diff, absDiff)
return
}
} else if result != test.expected {
t.Errorf("DivWithMode(0x%02x, 0%02x, %s) = 0x%02x, expected 0x%02x",
test.a, test.b, mode.desc, result, test.expected)
}
})
}
})
}
// Test that Div uses the default mode
t.Run("default_mode", func(t *testing.T) {
// Set to algorithmic mode and test Div uses it
DisableFastArithmetic()
result1 := Div(ToFloat8(1.0), ToFloat8(2.0))
// Set to lookup mode and test Div uses it
EnableFastArithmetic()
result2 := Div(ToFloat8(1.0), ToFloat8(2.0))
// Both should give the same result (just testing the function works with defaults)
if result1 != result2 {
t.Errorf("Div with different default modes gave different results: 0x%02x vs 0x%02x",
result1, result2)
}
})
}
// Test comparison operations
func TestComparisons(t *testing.T) {
a := ToFloat8(1.0)
b := ToFloat8(2.0)
c := ToFloat8(1.0)
if !Less(a, b) {
t.Error("1.0 should be less than 2.0")
}
if !Greater(b, a) {
t.Error("2.0 should be greater than 1.0")
}
if !Equal(a, c) {
t.Error("1.0 should equal 1.0")
}
if !LessEqual(a, b) {
t.Error("1.0 should be less than or equal to 2.0")
}
if !LessEqual(a, c) {
t.Error("1.0 should be less than or equal to 1.0")
}
if !GreaterEqual(b, a) {
t.Error("2.0 should be greater than or equal to 1.0")
}
if !GreaterEqual(a, c) {
t.Error("1.0 should be greater than or equal to 1.0")
}
}
func TestEqual(t *testing.T) {
tests := []struct {
a, b Float8
expected bool
desc string
}{
// Zero cases
{PositiveZero, PositiveZero, true, "+0 == +0"},
{NegativeZero, NegativeZero, true, "-0 == -0"},
{PositiveZero, NegativeZero, true, "+0 == -0"},
{NegativeZero, PositiveZero, true, "-0 == +0"},
// Regular number cases
{ToFloat8(1.0), ToFloat8(1.0), true, "1.0 == 1.0"},
{ToFloat8(1.0), ToFloat8(2.0), false, "1.0 != 2.0"},
{ToFloat8(-1.0), ToFloat8(-1.0), true, "-1.0 == -1.0"},
{ToFloat8(1.5), ToFloat8(1.5), true, "1.5 == 1.5"},
// Infinity cases
{PositiveInfinity, PositiveInfinity, true, "+Inf == +Inf"},
{NegativeInfinity, NegativeInfinity, true, "-Inf == -Inf"},
{PositiveInfinity, NegativeInfinity, false, "+Inf != -Inf"},
{NegativeInfinity, PositiveInfinity, false, "-Inf != +Inf"},
// NaN cases (NaN is not equal to anything, including itself)
{NaN, NaN, false, "NaN != NaN (by definition)"},
{NaN, PositiveInfinity, false, "NaN != +Inf"},
{NaN, NegativeInfinity, false, "NaN != -Inf"},
{NaN, PositiveZero, false, "NaN != +0"},
{NaN, NegativeZero, false, "NaN != -0"},
{NaN, ToFloat8(1.0), false, "NaN != 1.0"},
{PositiveInfinity, NaN, false, "+Inf != NaN"},
{NegativeInfinity, NaN, false, "-Inf != NaN"},
{PositiveZero, NaN, false, "+0 != NaN"},
{NegativeZero, NaN, false, "-0 != NaN"},
{ToFloat8(1.0), NaN, false, "1.0 != NaN"},
// Mixed cases
{PositiveInfinity, ToFloat8(1.0), false, "+Inf != 1.0"},
{NegativeInfinity, ToFloat8(-1.0), false, "-Inf != -1.0"},
{PositiveZero, ToFloat8(0.0), true, "+0 == 0.0"},
{NegativeZero, ToFloat8(0.0), true, "-0 == 0.0"},
{ToFloat8(1.0), ToFloat8(2.0), false, "1.0 != 2.0 (different values)"},
{ToFloat8(1.0), ToFloat8(1.5), false, "1.0 != 1.5 (different values)"},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
result := Equal(test.a, test.b)
if result != test.expected {
t.Errorf("Equal(%v, %v) = %v, expected %v", test.a, test.b, result, test.expected)
}
})
}
}
func TestMinMax(t *testing.T) {
tests := []struct {
a, b Float8
expectedMin, expectedMax Float8
desc string
}{
{ToFloat8(1.0), ToFloat8(2.0), ToFloat8(1.0), ToFloat8(2.0), "1.0 < 2.0"},
{ToFloat8(2.0), ToFloat8(1.0), ToFloat8(1.0), ToFloat8(2.0), "2.0 > 1.0"},
{ToFloat8(-1.0), ToFloat8(1.0), ToFloat8(-1.0), ToFloat8(1.0), "-1.0 < 1.0"},
{ToFloat8(1.0), ToFloat8(1.0), ToFloat8(1.0), ToFloat8(1.0), "1.0 == 1.0"},
{PositiveZero, NegativeZero, PositiveZero, PositiveZero, "min/max of +0 and -0"},
{PositiveInfinity, ToFloat8(1.0), ToFloat8(1.0), PositiveInfinity, "min/max with +Inf"},
{NegativeInfinity, ToFloat8(1.0), NegativeInfinity, ToFloat8(1.0), "min/max with -Inf"},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
// Debug output
t.Logf("Test case: %s", test.desc)
t.Logf("a: %v (bits: %08b), b: %v (bits: %08b)", test.a, test.a, test.b, test.b)
t.Logf("a.IsInf(): %v, b.IsInf(): %v", test.a.IsInf(), test.b.IsInf())
t.Logf("a == PositiveInfinity: %v, a == NegativeInfinity: %v", test.a == PositiveInfinity, test.a == NegativeInfinity)
t.Logf("b == PositiveInfinity: %v, b == NegativeInfinity: %v", test.b == PositiveInfinity, test.b == NegativeInfinity)
// Test Min
minResult := Min(test.a, test.b)
t.Logf("Min(%v, %v) = %v (bits: %08b), expected %v (bits: %08b)",
test.a, test.b, minResult, minResult, test.expectedMin, test.expectedMin)
if !Equal(minResult, test.expectedMin) {
t.Errorf("Min(%v, %v) = %v, expected %v",
test.a, test.b, minResult, test.expectedMin)
}
// Test Max
maxResult := Max(test.a, test.b)
t.Logf("Max(%v, %v) = %v (bits: %08b), expected %v (bits: %08b)",
test.a, test.b, maxResult, maxResult, test.expectedMax, test.expectedMax)
if !Equal(maxResult, test.expectedMax) {
t.Errorf("Max(%v, %v) = %v, expected %v",
test.a, test.b, maxResult, test.expectedMax)
}
// Test with arguments reversed (should be commutative)
revMin := Min(test.b, test.a)
t.Logf("Min(%v, %v) = %v (commutative test)", test.b, test.a, revMin)
if !Equal(revMin, minResult) {
t.Errorf("Min(%v, %v) = %v, expected %v (commutative test)",
test.b, test.a, revMin, minResult)
}
revMax := Max(test.b, test.a)
t.Logf("Max(%v, %v) = %v (commutative test)", test.b, test.a, revMax)
if !Equal(revMax, maxResult) {
t.Errorf("Max(%v, %v) = %v, expected %v (commutative test)",
test.b, test.a, revMax, maxResult)
}
})
}
// Additional test for NaN handling
t.Run("NaN handling", func(t *testing.T) {
a := ToFloat8(1.0)
// Min/Max with NaN as first argument
minResult1 := Min(NaN, a)
if !minResult1.IsNaN() {
t.Errorf("Min(NaN, %v) = %v, expected NaN", a, minResult1)
}
maxResult1 := Max(NaN, a)
if !maxResult1.IsNaN() {
t.Errorf("Max(NaN, %v) = %v, expected NaN", a, maxResult1)
}
// Min/Max with NaN as second argument
minResult2 := Min(a, NaN)
if !minResult2.IsNaN() {
t.Errorf("Min(%v, NaN) = %v, expected NaN", a, minResult2)
}
maxResult2 := Max(a, NaN)
if !maxResult2.IsNaN() {
t.Errorf("Max(%v, NaN) = %v, expected NaN", a, maxResult2)
}
})
}
// Test special value methods
func TestSpecialValueMethods(t *testing.T) {
if !PositiveZero.IsZero() {
t.Error("PositiveZero should be zero")
}
if !NegativeZero.IsZero() {
t.Error("NegativeZero should be zero")
}
if !PositiveInfinity.IsInf() {
t.Error("PositiveInfinity should be infinity")
}
if !NegativeInfinity.IsInf() {
t.Error("NegativeInfinity should be infinity")
}
if !ToFloat8(1.0).IsFinite() {
t.Error("1.0 should be finite")
}
if PositiveInfinity.IsFinite() {
t.Error("PositiveInfinity should not be finite")
}
}
func TestSign(t *testing.T) {
tests := []struct {
name string
input Float8
expected int
}{
{"positive", ToFloat8(1.5), 1},
{"negative", ToFloat8(-1.5), -1},
{"positive_zero", PositiveZero, 0},
{"negative_zero", NegativeZero, 0},
{"infinity", PositiveInfinity, 1},
{"negative_infinity", NegativeInfinity, -1},
{"nan", NaN, 0},
// Additional edge cases
// Note: Small positive/negative values that round to zero are treated as zero
{"small_positive", ToFloat8(1e-1), 1}, // Use a larger value that doesn't round to zero
{"small_negative", ToFloat8(-1e-1), -1}, // Use a larger value that doesn't round to zero
{"max_positive", MaxValue, 1},
{"min_negative", ToFloat8(-1 * float32(MaxValue.ToFloat32())), -1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.input.Sign()
if result != test.expected {
t.Errorf("Sign(%v) = %d, expected %d", test.input, result, test.expected)
}
})
}
}
func TestAbsNeg(t *testing.T) {
a := ToFloat8(-1.0)
if a.Abs() != ToFloat8(1.0) {
t.Error("Abs(-1.0) should be 1.0")
}
if a.Neg() != ToFloat8(1.0) {
t.Error("Neg(-1.0) should be 1.0")
}
b := ToFloat8(1.0)
if b.Neg() != ToFloat8(-1.0) {
t.Error("Neg(1.0) should be -1.0")
}
}
// Test slice operations
func TestToSlice8(t *testing.T) {
// Test with nil slice
t.Run("nil_slice", func(t *testing.T) {
var input []float32
result := ToSlice8(input)
if result != nil {
t.Error("Expected nil result for nil input")
}
})
// Test with empty slice
t.Run("empty_slice", func(t *testing.T) {
input := make([]float32, 0)
result := ToSlice8(input)
if result == nil {
t.Error("Expected non-nil result for empty slice")
}
if len(result) != 0 {
t.Errorf("Expected empty slice, got length %d", len(result))
}
})
// Test with single element
t.Run("single_element", func(t *testing.T) {
input := []float32{1.5}
expected := []Float8{ToFloat8(1.5)}
result := ToSlice8(input)
if len(result) != 1 || result[0] != expected[0] {
t.Errorf("Expected %v, got %v", expected, result)
}
})
// Test with multiple elements
t.Run("multiple_elements", func(t *testing.T) {
// Create a proper negative zero value
negZero := float32(math.Copysign(0, -1))