Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparisons between integers and floats are inexact #14303

Open
HertzDevil opened this issue Feb 18, 2024 · 0 comments
Open

Comparisons between integers and floats are inexact #14303

HertzDevil opened this issue Feb 18, 2024 · 0 comments

Comments

@HertzDevil
Copy link
Contributor

HertzDevil commented Feb 18, 2024

Int::Primitive#==(Float::Primitive) and Float::Primitive#==(Int::Primitive), as well as the other relational operators, all convert the integer argument to the same type as the float argument before doing the comparison. This leads to some odd results when the conversion is inexact:

UInt64::MAX == 2.0 ** 64          # => true
UInt128::MAX == Float32::INFINITY # => true

This is problematic because #hash is exact:

{UInt64::MAX => 'a', 2.0 ** 64 => 'b'}[UInt64::MAX] # => 'b'

The inexact pairs of types are:

  • Int32, Float32
  • UInt32, Float32
  • Int64, Float32 | Float64
  • UInt64, Float32 | Float64
  • Int128, Float32 | Float64
  • UInt128, Float32 | Float64

Python handles this correctly, which they call a nightmare. Ruby handles equality here and comparisons here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant