Project

General

Profile

Actions

Bug #17631

closed

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

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

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
[ruby-core:102513]

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

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

Also available in: Atom PDF