Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/vm/integer_float_residuals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
16 changes: 15 additions & 1 deletion internal/vm/numeric_edges.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down