From: universato@... Date: 2021-05-20T07:55:23+00:00 Subject: [ruby-core:103909] [Ruby master Bug#17631] `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY` Issue #17631 has been updated by universato (Yoshimine Sato). FYI: Octave had the same behavior. Probably MATLAB does too. ```matlab real(Inf) %=> Inf imag(Inf) %=> 0 real(NaN) %=> NaN imag(NaN) %=> 0 ``` ```ruby p Float::INFINITY.real #=> Infinity p Float::INFINITY.imag #=> 0 p Float::NAN.real #=> NaN p Float::NAN.imag #=> 0 ``` I'm not sure how useful this behavior is. If we consider the behavior of `real?` we should also consider the behavior of `real`. ---------------------------------------- Bug #17631: `Numeric#real?` incorrectly returns true for `NaN` and `INFINITY` https://bugs.ruby-lang.org/issues/17631#change-92038 * Author: jtannas (Joel Tannas) * Status: Open * Priority: Normal * ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN ---------------------------------------- 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. ``` ruby 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 -- https://bugs.ruby-lang.org/ Unsubscribe: