Skip to content

Give Float#remainder's zero the dividend's sign - #432

Merged
tannevaled merged 1 commit into
mainfrom
fix/float-remainder-zero-sign
Aug 23, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/float-remainder-zero-sign

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

A zero has a sign, and Ruby's is the dividend's, never the divisor's. Checked against MRI 3.4.5 rather than reasoned:

(-7.0).remainder(3.5)   # => -0.0
  7.0.remainder(-3.5)   # =>  0.0
(-7.0).remainder(-3.5)  # => -0.0
(-0.0).remainder(3.0)   # => -0.0

a - b*trunc(a/b) reaches none of that reliably, and it was wrong in both directions:

  • On every architecture, this machine included. Where the subtraction cancels, x - x is +0.0 whatever x was — so (-7.0).remainder(3.5) answered 0.0. A plain wrong answer that no lane was asking about, because no case covered it.

  • On loong64 under qemu, the same cancellation answered -0.0 for 7.0.remainder(3.5), which has had arch-qemu-nightly red since 2026-08-12:

    --- FAIL: TestFloatRemainder
        src="p 7.0.remainder(3.5)"  got="-0.0"  want="0.0"
    

Stating the sign outright — math.Copysign(0, a) when the result is zero — costs one comparison and settles both directions at once.

Proof

  • Four new cases in TestFloatRemainder, each checked against MRI first.
  • They fail without the change on this machine (not only on loong64):
    src="p (-7.0).remainder(3.5)"   got="0.0"  want="-0.0"
    src="p (-7.0).remainder(-3.5)"  got="0.0"  want="-0.0"
    src="p (-0.0).remainder(3.0)"   got="0.0"  want="-0.0"
    
    So the next drift is caught here rather than in a nightly.
  • Full ./internal/vm/ green.
  • The loong64 half is a claim this machine cannot make: arch-qemu-nightly is dispatched on this branch and its result is linked below before this merges.

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 <noreply@anthropic.com>
@tannevaled

Copy link
Copy Markdown
Contributor Author

Verified on the lane that catches it. loong64 is where the wrong sign showed, and this machine cannot reproduce that half. Both this and #431 were put on verify/arch-nightly and arch-qemu-nightly dispatched on it.

Run 32661057000 — all four green, after twelve consecutive red nights for loong64:

success  arch loong64 (qemu)    ok  …/internal/vm   990.692s
success  arch s390x (qemu)
success  arch riscv64 (qemu)
success  arch ppc64le (qemu)

0 --- FAIL lines, 0 --- SKIP lines, step is go test -timeout=30m -exec=qemu-loongarch64-static ./... with no -run filter.

Worth repeating from the description: the loong64 failure was only half the defect. (-7.0).remainder(3.5) answered 0.0 here too, where MRI says -0.0, on every architecture — and that half has a test now, which fails without this change on this machine.

@tannevaled
tannevaled merged commit 5ea444c into main Aug 23, 2026
7 checks passed
@tannevaled
tannevaled deleted the fix/float-remainder-zero-sign branch August 24, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant