Fixnum#infinite?/Bignum#infinite or Numeric#infinte, consistent with Float#infinite? and BigDecimal#infinite?
We have an issue where it is not easy to ask if a number is +ve or -ve infinity. https://github.com/rails/arel/issues/411
Both Float and BigDecimal do respond to infinite? but Integer derivatives don't. It sort of makes sense, since we don't have an infinity value for Fixnum/Bignum. However, it makes polymorphic code hard.
 pry(main)> Float::INFINITY.infinite? => 1  pry(main)> -Float::INFINITY.infinite? => -1  pry(main)> BigDecimal::INFINITY.infinite? => 1  pry(main)> -BigDecimal::INFINITY.infinite? => -1
Given a Numeric value x, it would be nice to query if it is INFINITY or not. Propose adding a default implementation to Numeric, which always returns false or nil.
Updated by shyouhei (Shyouhei Urabe) almost 5 years ago
Another real-world use-case where I found Numeric#infinite? useful is JSON validation. Infinities are not allowed in JSON so a programmer want to filter them out before converting a Numeric into JSON, but that is not straight-forward right now. With this method it becomes much OO-ish.
Updated by mrkn (Kenta Murata) over 4 years ago
- Status changed from Open to Closed
Applied in changeset r55702.
numeric.c, complex.c: Add finite? and infinite? consistent with Float
numeric.c (num_finite_p, num_infinite_p): Add Numeric#finite? and
Numeric#infinite? [Feature #12039] [ruby-core:73618]
complex.c (rb_complex_finite_p): Add Complex#finite?
complex.c (rb_complex_infinite_p): Add Complex#infinite?
test/ruby/test_bignum.rb: Add test for Integer#finite? and
test/ruby/test_rational.rb: Add test for Rational#finite? and
test/ruby/test_complex.rb: Add test for Complex#finite? and