Skip to content

[fix] Bignum comparison with Float in <=> and ==#9337

Merged
headius merged 1 commit intojruby:jruby-10.0from
kares:bignum-cmp-10
Mar 28, 2026
Merged

[fix] Bignum comparison with Float in <=> and ==#9337
headius merged 1 commit intojruby:jruby-10.0from
kares:bignum-cmp-10

Conversation

@kares
Copy link
Copy Markdown
Member

@kares kares commented Mar 28, 2026

op_cmp converted the Bignum to double via big2dbl before comparing with the Float, losing precision for large values. For example, (2**64 + 1) <=> (2**64).to_f returned 0 instead of 1 because big2dbl(2**64 + 1) rounds to 2**64.

op_equal had the same issue: (2**64 + 1) == (2**64).to_f returned true instead of false.

Fix op_cmp to use the existing float_cmp method (rb_integer_float_cmp) which decomposes the float into integer and fractional parts and compares without precision loss. Add float_eq (rb_integer_float_eq) for op_equal using the same approach.

Note: big_op (>, >=, <, <=) already used float_cmp correctly.

op_cmp converted the Bignum to double via big2dbl before comparing
with the Float, losing precision for large values. For example,
(2**64 + 1) <=> (2**64).to_f returned 0 instead of 1 because
big2dbl(2**64 + 1) rounds to 2**64.

op_equal had the same issue: (2**64 + 1) == (2**64).to_f returned
true instead of false.

Fix op_cmp to use the existing float_cmp method (rb_integer_float_cmp)
which decomposes the float into integer and fractional parts and
compares without precision loss. Add float_eq (rb_integer_float_eq)
for op_equal using the same approach.

Note: big_op (>, >=, <, <=) already used float_cmp correctly.
@kares kares added this to the JRuby 10.0.5.0 milestone Mar 28, 2026
Copy link
Copy Markdown
Member

@headius headius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Unsure if these specs are specific enough to stay in JRuby. Perhaps move some or all to ruby/spec.

@headius headius merged commit bd5053a into jruby:jruby-10.0 Mar 28, 2026
107 checks passed
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.

2 participants