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