From: Karl Brodowsky Date: 2009-04-22T19:39:57+09:00 Subject: [ruby-core:23286] [Bug #1397] to_f of rational Issue #1397 has been updated by Karl Brodowsky. A fix for this bug might be along the following line, but this might still cause rounding errors in case only one of numerator and denominator is above Float::MAX and the other one is pretty small and would loose significant digits. class Rational FLOAT_MAX_I = Float::MAX.to_i def to_f numerator = @numerator denominator = @denominator sign = numerator <=> 0 if (sign.zero?) return 0.0 elsif sign < 0 numerator = -numerator end while numerator >= FLOAT_MAX_I || denominator >= FLOAT_MAX_I do numerator >>= 8 denominator >>= 8 if (denominator == 0) raise ZeroDivisionError, "denominator too close to zero: #{@numerator}/{@denominator}" elsif numerator == 0 return 0.0 end end return numerator.to_f / denominator.to_f end end ---------------------------------------- http://redmine.ruby-lang.org/issues/show/1397 ---------------------------------------- http://redmine.ruby-lang.org