From: David MacMahon Date: 2013-09-10T21:26:02-07:00 Subject: [ruby-core:57128] Re: [ruby-trunk - Bug #8883] Rational canonicalization unexpectedly converts to Fixnum On Sep 10, 2013, at 9:09 PM, marcandre (Marc-Andre Lafortune) wrote: > > Issue #8883 has been updated by marcandre (Marc-Andre Lafortune). > > > david_macmahon (David MacMahon) wrote: >> While playing around with this, I see that integer Floats also have some special handling: > > Right. Floats are inexact while Integers & Rational are exact, and so are Complex with exact components. Rational(1/1) and 1 should yield the same mathematical result, but with floats that can be tricky. For example there are infinitely many different bigdecimals that will map to 1.0 (say 1.000....1 and 1.000...2 with enough zeros), but they don't behave exactly the same way, for example if you substract 1), so we can't freely map them. That's all fine from a numerical/mathematical point of view, but it still seems like there is something missing from the duck typing: $ ruby -e 'p (1/1.0).nan?' false $ ruby -r mathn -e 'p (1/1.1).nan?' false $ ruby -r mathn -e 'p (1/1.0).nan?' -e:1:in `
': undefined method `nan?' for (1/1):Rational (NoMethodError) Though admittedly this is getting a bit far from the OP. > >> Oddly though, this can result in non-reduced Rationals: >> >> $ ruby -r mathn -e 'p [2/2.0, 2/2.5]' >> [(2/2), 0.8] > > Oh oh, that's a bug. It's not even related to 'mathn'. I opened a new issue about this: https://bugs.ruby-lang.org/issues/8894 Thanks! Dave