Skip to content

Commit ff3090d

Browse files
committed
test: verify E4M3FN arithmetic against reference values
Full 256-value table verification for Add/Sub/Mul/Div.
1 parent 9af9e96 commit ff3090d

2 files changed

Lines changed: 511 additions & 7 deletions

File tree

arithmetic.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,32 @@ func DivWithMode(a, b Float8, mode ArithmeticMode) Float8 {
148148
// Algorithmic implementations
149149

150150
func addAlgorithmic(a, b Float8) Float8 {
151-
// Handle special cases
151+
// Handle NaN cases first — NaN propagates through all operations
152+
if a.IsNaN() || b.IsNaN() {
153+
return NaN
154+
}
155+
156+
// Handle zero cases with IEEE 754 sign rules: (+0) + (-0) = +0
157+
if a.IsZero() && b.IsZero() {
158+
if a == NegativeZero && b == NegativeZero {
159+
return NegativeZero
160+
}
161+
return PositiveZero
162+
}
152163
if a.IsZero() {
153164
return b
154165
}
155166
if b.IsZero() {
156167
return a
157168
}
158169

159-
// Handle infinity cases
170+
// Handle infinity cases: Inf + (-Inf) = NaN
160171
if a.IsInf() || b.IsInf() {
161172
if a == PositiveInfinity && b == NegativeInfinity {
162-
return PositiveZero // NaN case, but we return zero
173+
return NaN
163174
}
164175
if a == NegativeInfinity && b == PositiveInfinity {
165-
return PositiveZero // NaN case, but we return zero
176+
return NaN
166177
}
167178
if a.IsInf() {
168179
return a
@@ -179,18 +190,29 @@ func addAlgorithmic(a, b Float8) Float8 {
179190
}
180191

181192
func subAlgorithmic(a, b Float8) Float8 {
182-
// Handle special cases
193+
// Handle NaN cases first — NaN propagates through all operations
194+
if a.IsNaN() || b.IsNaN() {
195+
return NaN
196+
}
197+
198+
// Handle zero cases with IEEE 754 sign rules: (-0) - (-0) = +0
199+
if a.IsZero() && b.IsZero() {
200+
if a == NegativeZero && b != NegativeZero {
201+
return NegativeZero
202+
}
203+
return PositiveZero
204+
}
183205
if b.IsZero() {
184206
return a
185207
}
186208
if a.IsZero() {
187209
return b.Neg()
188210
}
189211

190-
// Handle infinity cases
212+
// Handle infinity cases: Inf - Inf = NaN
191213
if a.IsInf() || b.IsInf() {
192214
if a == b && a.IsInf() {
193-
return PositiveZero // NaN case, but we return zero
215+
return NaN
194216
}
195217
if a.IsInf() {
196218
return a

0 commit comments

Comments
 (0)