From 3a404770793682e1f23b5c703c7d30cfb27101b5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 23 Aug 2026 21:21:30 +0200 Subject: [PATCH] Give Float#remainder's zero the dividend's sign MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A zero has a sign, and Ruby's is the dividend's, never the divisor's: (-7.0).remainder(3.5) is -0.0 where 7.0.remainder(3.5) is 0.0, and 7.0.remainder(-3.5) is still 0.0. Checked against MRI, not reasoned. `a - b*trunc(a/b)` reaches neither reliably, and it was wrong in both directions: - where it cancels, x - x is +0.0 whatever x was, so (-7.0).remainder(3.5) answered 0.0 on every architecture, this machine included — a plain wrong answer that nothing was asking about; - on loong64 under qemu the same cancellation answered -0.0 for 7.0.remainder(3.5), which has had the arch nightly red since 2026-08-12. Saying the sign outright costs one comparison and settles both. The four new cases fail without the change here, so the next drift is caught on this machine rather than a nightly. Co-Authored-By: Claude Opus 5 --- internal/vm/integer_float_residuals_test.go | 8 ++++++++ internal/vm/numeric_edges.go | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/vm/integer_float_residuals_test.go b/internal/vm/integer_float_residuals_test.go index bc1be5d2..4db2987e 100644 --- a/internal/vm/integer_float_residuals_test.go +++ b/internal/vm/integer_float_residuals_test.go @@ -135,6 +135,14 @@ func TestFloatRemainder(t *testing.T) { {`p (-7.0).remainder(3)`, "-1.0\n"}, {`p 7.0.remainder(-3)`, "1.0\n"}, {`p 7.0.remainder(3.5)`, "0.0\n"}, + // A zero has a sign, and it is the DIVIDEND's, never the divisor's. + // The subtraction reaches that on its own wherever the arithmetic rounds + // as IEEE 754 says; loong64 under qemu returned -0.0 for the line above, + // which had the nightly red, so the sign is now stated outright. + {`p (-7.0).remainder(3.5)`, "-0.0\n"}, + {`p 7.0.remainder(-3.5)`, "0.0\n"}, + {`p (-7.0).remainder(-3.5)`, "-0.0\n"}, + {`p (-0.0).remainder(3.0)`, "-0.0\n"}, {`p (0.0 / 0).remainder(3)`, "NaN\n"}, // NaN dividend {`p (1.0 / 0).remainder(3)`, "NaN\n"}, // Infinity dividend {`p (-7.5).remainder(3)`, "-1.5\n"}, diff --git a/internal/vm/numeric_edges.go b/internal/vm/numeric_edges.go index a1b2a88e..c04baeec 100644 --- a/internal/vm/numeric_edges.go +++ b/internal/vm/numeric_edges.go @@ -497,7 +497,21 @@ func (vm *VM) registerNumericEdges() { raise("ZeroDivisionError", "divided by 0") } a := floatOf(self) - return object.Float(a - b*math.Trunc(a/b)) + r := a - b*math.Trunc(a/b) + if r == 0 { + // A zero has a sign, and Ruby's is the dividend's, never the + // divisor's: (-7.0).remainder(3.5) is -0.0 where 7.0.remainder(3.5) + // is 0.0, and 7.0.remainder(-3.5) is still 0.0. + // + // The subtraction reaches neither reliably. Where it cancels, + // x - x is +0.0 whatever x was, so (-7.0).remainder(3.5) answered + // 0.0 on every architecture; and loong64 under qemu answered -0.0 + // for 7.0.remainder(3.5), which has had the nightly red since + // 2026-08-12. Stating the sign costs one comparison and settles + // both directions at once. + return object.Float(math.Copysign(0, a)) + } + return object.Float(r) }) // Float#step — mirrors Integer#step: an enumerator without a block, otherwise