Project

General

Profile

Feature #12039

Fixnum#infinite?/Bignum#infinite or Numeric#infinte, consistent with Float#infinite? and BigDecimal#infinite?

Added by ioquatix (Samuel Williams) over 1 year ago. Updated about 1 year ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:73618]

Description

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.

[13] pry(main)> Float::INFINITY.infinite?                                       
=> 1
[14] pry(main)> -Float::INFINITY.infinite?                                      
=> -1
[15] pry(main)> BigDecimal::INFINITY.infinite?                                  
=> 1
[16] 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.


Related issues

Related to Ruby trunk - Feature #10641: Introduce Fixnum#finite? and Bignum#finite?Closed

Associated revisions

Revision 55702
Added by mrkn (Kenta Murata) over 1 year ago

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]

  • 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
    Integer#infinite?

  • test/ruby/test_fixnum.rb: ditto.

  • test/ruby/test_rational.rb: Add test for Rational#finite? and
    Rational#infinite?

  • test/ruby/test_complex.rb: Add test for Complex#finite? and
    Complex#infinite?

Revision 55702
Added by mrkn (Kenta Murata) over 1 year ago

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]

  • 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
    Integer#infinite?

  • test/ruby/test_fixnum.rb: ditto.

  • test/ruby/test_rational.rb: Add test for Rational#finite? and
    Rational#infinite?

  • test/ruby/test_complex.rb: Add test for Complex#finite? and
    Complex#infinite?

History

#1 [ruby-core:73619] Updated by ioquatix (Samuel Williams) over 1 year ago

It might also make sense to consider if other "predicates" make sense, e.g. nan? They should be declared on Numeric with a default logical implementation where possible.

#2 [ruby-core:73622] Updated by seantheprogrammer (Sean Griffin) over 1 year ago

This is probably a useful method to be able to rely on universally, even if Fixnum and Bignum just return false.

#3 [ruby-core:73917] Updated by shyouhei (Shyouhei Urabe) over 1 year 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.

#4 [ruby-core:74365] Updated by matz (Yukihiro Matsumoto) over 1 year ago

Agreed. Add #finite? and #infinite?

Matz.

#5 [ruby-core:74367] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Tracker changed from Bug to Feature

#6 [ruby-core:74458] Updated by mrkn (Kenta Murata) over 1 year ago

What behavior is desirable for a Complex?

#7 [ruby-core:74459] Updated by mrkn (Kenta Murata) over 1 year ago

I think the following definition is acceptable for a Complex:

def inifinite?
  self.magnitude.infinite?
end

#8 [ruby-core:75850] Updated by ioquatix (Samuel Williams) over 1 year ago

This looks good, do you think we can integrate this at some point? What do we need to do?

#9 [ruby-core:76286] Updated by mrkn (Kenta Murata) over 1 year ago

  • Assignee set to mrkn (Kenta Murata)

#10 Updated by mrkn (Kenta Murata) over 1 year 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]

  • 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
    Integer#infinite?

  • test/ruby/test_fixnum.rb: ditto.

  • test/ruby/test_rational.rb: Add test for Rational#finite? and
    Rational#infinite?

  • test/ruby/test_complex.rb: Add test for Complex#finite? and
    Complex#infinite?

#11 [ruby-core:77470] Updated by ioquatix (Samuel Williams) about 1 year ago

Thank you for your hard work :)

#12 Updated by shyouhei (Shyouhei Urabe) 10 months ago

  • Related to Feature #10641: Introduce Fixnum#finite? and Bignum#finite? added

Also available in: Atom PDF