`Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`
In mathematics, infinity is not a real number. See https://math.stackexchange.com/a/750787
I don't have a source for this, but I also believe that
NaN is not a real number.
Numeric#real? incorrectly returns
true for both of these cases.
irb(main):001:0> Float::INFINITY.real? => true irb(main):002:0> Float::NAN.real? => true irb(main):003:0> require 'bigdecimal' => true irb(main):004:0> BigDecimal::NAN.real? => true irb(main):005:0> BigDecimal::INFINITY.real? => true
I ran into this while doing some math with logarithms, leading me to have to put in weird catches like
return nil if result.complex? || result.nan? || result.infinite?
Originally reported here: https://stackoverflow.com/q/64795265/7950458
Updated by mrkn (Kenta Murata) about 2 months ago
IEEE754 follows the extended real number system that is a real number system with positive and negative Infinities. So, at least,
Float::INFINITY.real? can return
BigDecimal also employs the extended real number system, so
BigDecimal::INFINITY.real? can be
BigDecimal::NAN are not-a-number, so
real? of them may be reasonable to be
false depending on the definition of the method.
Updated by jtannas (Joel Tannas) about 2 months ago
Sorry for the slow reply - I had to go get a lesson from our resident mathematician.
mrkn (Kenta Murata) wrote in #note-1:
IEEE754 follows the extended real number system that is a real number system with positive and negative Infinities.
The definition of that number set is that it's the set of all real numbers plus the infinities. The infinities are still not real, even though they're in the set. https://mathworld.wolfram.com/AffinelyExtendedRealNumbers.html
chrisseaton (Chris Seaton) wrote in #note-2:
I think 'real' in this context just means 'not complex'.
That's how the code is written, but it conflicts with the actual definitions of complex & real. Going by the official definition, complex numbers include all real numbers. https://math.stackexchange.com/questions/304207/difference-between-imaginary-and-complex-numbers/304211#304211
Changing the code to completely match the formal definitions would be a big change though, so I don't really know what the best option is.