## Bug #17631

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

**Description**

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

**Related issues**

#### Updated by mrkn (Kenta Murata) 4 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) 4 months ago

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

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

#### Updated by sawa (Tsuyoshi Sawada) about 2 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) about 1 month 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) about 1 month ago

FYI: numpy also says inf is a real.

>>> import numpy >>> import math >>> numpy.isreal(math.inf) True

#### Updated by mame (Yusuke Endoh) about 1 month ago

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

#### Updated by universato (Yoshimine Sato) about 1 month ago

FYI:

```
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) about 1 month 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.

Matz.