Skip to content

Commit bc06437

Browse files
committed
fix: correct IEEE 754 test expectations for inf+(-inf) and (-0)+(+0)
IEEE 754 §6.3 specifies that (-0)+(+0) = +0 and inf+(-inf) = NaN. The arithmetic implementation was already correct; the test expectations were wrong. Also remove stale doc comments claiming inf+(-inf) returns +0.
1 parent bf640ad commit bc06437

3 files changed

Lines changed: 6 additions & 5 deletions

File tree

arithmetic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var DefaultArithmeticMode = ArithmeticAuto
1616
//
1717
// Add(+0, ±0) = +0
1818
// Add(-0, -0) = -0
19-
// Add(±Inf, ∓Inf) = NaN (but returns +0 in this implementation)
19+
// Add(±Inf, ∓Inf) = NaN
2020
// Add(NaN, x) = NaN
2121
// Add(x, NaN) = NaN
2222
//
@@ -36,7 +36,7 @@ func Add(a, b Float8) Float8 {
3636
// Special cases are handled according to IEEE 754 rules:
3737
// - If either operand is NaN, the result is NaN
3838
// - Infinities of the same sign add to infinity of that sign
39-
// - Infinities of opposite signs produce NaN (but this implementation returns +0)
39+
// - Infinities of opposite signs produce NaN
4040
// - The sign of a zero result is the sign of the sum of the operands
4141
//
4242
// For finite numbers, the result is rounded to the nearest representable Float8 value.
@@ -63,7 +63,7 @@ func AddWithMode(a, b Float8, mode ArithmeticMode) Float8 {
6363
// Sub(+0, -0) = +0
6464
// Sub(-0, +0) = -0
6565
// Sub(-0, -0) = +0
66-
// Sub(±Inf, ±Inf) = NaN (but returns +0 in this implementation)
66+
// Sub(±Inf, ±Inf) = NaN
6767
// Sub(NaN, x) = NaN
6868
// Sub(x, NaN) = NaN
6969
//

arithmetic_correctness_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ func TestArithmeticIdentities(t *testing.T) {
323323
label := fmt.Sprintf("0x%02x", i)
324324

325325
// a + 0 = a (for non-NaN values)
326-
if !a.IsNaN() {
326+
// IEEE 754 §6.3: (-0) + (+0) = +0, so skip negative zero
327+
if !a.IsNaN() && a != NegativeZero {
327328
got := Add(a, PositiveZero)
328329
if got != a {
329330
t.Errorf("%s: Add(a, +0) = 0x%02x, want 0x%02x", label, uint8(got), i)

float8_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func TestAddBasic(t *testing.T) {
206206
{ToFloat8(1.0), ToFloat8(1.0), ToFloat8(2.0), "one + one", false},
207207
{ToFloat8(2.0), ToFloat8(3.0), ToFloat8(5.0), "two + three", false},
208208
{PositiveInfinity, ToFloat8(1.0), PositiveInfinity, "inf + one", false},
209-
{PositiveInfinity, NegativeInfinity, PositiveZero, "inf + (-inf)", false},
209+
{PositiveInfinity, NegativeInfinity, NaN, "inf + (-inf)", false},
210210
{ToFloat8(1.5), ToFloat8(1.5), ToFloat8(3.0), "1.5 + 1.5", false},
211211
{ToFloat8(-1.0), ToFloat8(1.0), ToFloat8(0.0), "-1 + 1", false},
212212
{ToFloat8(0.5), ToFloat8(0.5), ToFloat8(1.0), "0.5 + 0.5", false},

0 commit comments

Comments
 (0)