Bug #17631


`Numeric#real?` incorrectly returns true for `NaN` and `INFINITY`

Added by jtannas (Joel Tannas) 11 months ago. Updated 8 months ago.

Target version:
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]


In mathematics, infinity is not a real number. See
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:

Related issues

Related to Ruby master - Feature #10378: [PATCH 0/3] It's better (1 + 0i).real? return trueOpenActions

Updated by mrkn (Kenta Murata) 11 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 true. BigDecimal also employs the extended real number system, so BigDecimal::INFINITY.real? can be true, too.

Float::NAN and 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 chrisseaton (Chris Seaton) 11 months ago

I think 'real' in this context just means 'not complex'.

Actions #3

Updated by jtannas (Joel Tannas) 11 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.

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.
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.

Updated by sawa (Tsuyoshi Sawada) 8 months ago

It does not make sense to discuss the mathematical definition of real numbers in this context because digital computers cannot handle (the entire) real numbers in the first place. Hence, we are dealing with floating point numbers, which are approximation, or a quotient set, of real numbers, but not real numbers themselves.There is no real numbers to begin with.

Whenever you see the word "real" in the context of Ruby, you need to understand that its use departs from the mathematical definition. The best you can propose is to avoid the word "real" and change the method name into something else.

Updated by jtannas (Joel Tannas) 8 months ago

Fair enough - If we're not worrying about following the mathematical definitions too closely then we can gloss over a lot of these details about the definition of "real numbers" and leave it as-is. Instead, how about a new method #rational? that responds with a boolean on whether a number will respond successfully to #rationalize?

  • #rationalize fails for complex numbers, NaN, and the infinities so rational? would give the desired behaviour
  • it meshes nicely with the existing Rational class
  • it gives users a simple check for if a number is "well behaved"

Updated by mame (Yusuke Endoh) 8 months ago

FYI: numpy also says inf is a real.

>>> import numpy
>>> import math
>>> numpy.isreal(math.inf)
Actions #7

Updated by mame (Yusuke Endoh) 8 months ago

  • Related to Feature #10378: [PATCH 0/3] It's better (1 + 0i).real? return true added

Updated by universato (Yoshimine Sato) 8 months ago


p Float::INFINITY.real #=> Infinity
p Float::INFINITY.imag #=> 0
p Float::NAN.real      #=> NaN
p Float::NAN.imag      #=> 0

Python and Octave have same behavior. Probably MATLAB does too.
If we change the behavior of real?, we should also consider the behavior of real a bit

Updated by matz (Yukihiro Matsumoto) 8 months ago

  • Status changed from Open to Rejected

Traditionally, real just means being floating-point numbers in Computer Science. Other languages treat infinity and NaN in similar manner.



Also available in: Atom PDF