I thought it would be nice to be able to convert Floats to Rationals, so
I went through the exercise of figuring out how to do it, even though
it’s (by the nature of floating point arithmetic) an inexact science.
Here’s my extension to class Float. The main feature is the to_rational
instance method, but you can also get the maximum number of decimal
digits representable by a Float on your computer by using the
max_precision instance and/or class methods.
class Float
require ‘rational’
@@max_num_significant_digits_minus_1 = nil
@@scaling_factor = nil
def to_rational
unless ! @@max_num_significant_digits_minus_1.nil?
Float.calculate_max_num_significant_digits
end
Rational( (self * @@scaling_factor).to_i, @@scaling_factor )
end
def Float.calculate_max_num_significant_digits
i = j = 0
until 1.0 - i == 0.0
j += 1
i = 1.0 + 0.1**j
end
@@max_num_significant_digits_minus_1 = j - 1
@@scaling_factor = 10**@@max_num_significant_digits_minus_1
end
def Float.max_precision
@@max_num_significant_digits_minus_1 + 1
end
def max_precision
Float.max_precision
end
end
Al