-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharithmetic_rounding_test.go
More file actions
110 lines (102 loc) · 3.13 KB
/
arithmetic_rounding_test.go
File metadata and controls
110 lines (102 loc) · 3.13 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
package float16
import (
"fmt"
"math"
"testing"
)
func modes() []RoundingMode {
return []RoundingMode{
RoundNearestEven,
RoundNearestAway,
RoundTowardZero,
RoundTowardPositive,
RoundTowardNegative,
}
}
func TestAddWithMode_RoundingMatchesConverter(t *testing.T) {
cases := [][2]float32{
{1.0, float32(math.Pow(2, -11))}, // halfway between 1.0 and next
{1.0, 1e-3}, // general positive
{-1.0, float32(math.Pow(2, -11))}, // negative with halfway increment
{-0.75, 0.125}, // mixed signs, exact binary fractions
}
for _, c := range cases {
for _, m := range modes() {
name := func(a, b float32, mode RoundingMode) string {
return fmt.Sprintf("a=%g b=%g mode=%v", a, b, mode)
}(c[0], c[1], m)
t.Run(name, func(t *testing.T) {
a16 := FromFloat32(c[0])
b16 := FromFloat32(c[1])
expected := FromFloat32WithRounding(a16.ToFloat32()+b16.ToFloat32(), m)
got, err := AddWithMode(a16, b16, ModeIEEEArithmetic, m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != expected {
t.Fatalf("AddWithMode mismatch: got=%v want=%v (a=%g b=%g mode=%v)", got, expected, c[0], c[1], m)
}
})
}
}
}
func TestMulWithMode_RoundingMatchesConverter(t *testing.T) {
cases := [][2]float32{
{1.25, 0.2}, // positive * positive
{-1.25, 0.2}, // negative * positive
{1.5, -0.75}, // positive * negative
{-0.5, -0.125}, // negative * negative
{float32(math.Pow(2, -3)), float32(math.Pow(2, -8))}, // exact powers of two
}
for _, c := range cases {
for _, m := range modes() {
name := func(a, b float32, mode RoundingMode) string {
return fmt.Sprintf("a=%g b=%g mode=%v", a, b, mode)
}(c[0], c[1], m)
t.Run(name, func(t *testing.T) {
a16 := FromFloat32(c[0])
b16 := FromFloat32(c[1])
expected := FromFloat32WithRounding(a16.ToFloat32()*b16.ToFloat32(), m)
got, err := MulWithMode(a16, b16, ModeIEEEArithmetic, m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != expected {
t.Fatalf("MulWithMode mismatch: got=%v want=%v (a=%g b=%g mode=%v)", got, expected, c[0], c[1], m)
}
})
}
}
}
func TestDivWithMode_RoundingMatchesConverter(t *testing.T) {
cases := [][2]float32{
{1.25, 0.2}, // positive / positive
{-1.25, 0.2}, // negative / positive
{1.5, -0.75}, // positive / negative
{-0.5, -0.125}, // negative / negative
{7.0, 3.0}, // non-terminating in binary
}
for _, c := range cases {
// avoid division by zero
if c[1] == 0 {
continue
}
for _, m := range modes() {
name := func(a, b float32, mode RoundingMode) string {
return fmt.Sprintf("a=%g b=%g mode=%v", a, b, mode)
}(c[0], c[1], m)
t.Run(name, func(t *testing.T) {
a16 := FromFloat32(c[0])
b16 := FromFloat32(c[1])
expected := FromFloat32WithRounding(a16.ToFloat32()/b16.ToFloat32(), m)
got, err := DivWithMode(a16, b16, ModeIEEEArithmetic, m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != expected {
t.Fatalf("DivWithMode mismatch: got=%v want=%v (a=%g b=%g mode=%v)", got, expected, c[0], c[1], m)
}
})
}
}
}