lib/radix/rational.rb in radix-2.0.1 vs lib/radix/rational.rb in radix-2.1.1

- old
+ new

@@ -1,10 +1,11 @@ require 'rational' require 'radix/numeric' module Radix + # Represents rational numbers. # class Rational < Numeric # Alternative to #new. def self.[](n,d=nil,b=10) @@ -55,55 +56,74 @@ end end public + # The numerator. # def numerator @value.numerator end + # The denominator. # def denominator @value.denominator end + # Is the value negative? # def negative? value < 0 end + # Convert rational to new base. # + # Returns new rational. [Radix::Rational] def convert(base) self.class.new(numerator, denominator, base) end + # Convert to rational. # + # Returns the internal value. [Rational] def to_r value end + # Convert to Float by dividing the numerator by the denominator. # + # Returns the converted value. [Float] def to_f numerator.to_f / denominator.to_f end + # Convert to Integer by converting to Float first then + # appling #to_i to the float. # + # Returns the converted value. [Integer] def to_i to_f.to_i end + # Translate value into an array of places. # + # Returns array of places. [Array] def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end + # Convert the value into a string representation of the given base. # + # base - the base to convert + # divider - symbol to used ot divide numbers + # + # Returns translated value. [String] def to_s(base=nil, divider=nil) divider = divider.to_s if divider if base convert(base).to_s(nil, divider) else @@ -117,11 +137,15 @@ end end end end + # Are two rationals equal? # + # TODO: this may need improvement to be more percise. + # + # Returns true if equal, otherwise false. [Boolean] def ==(other) a, b = self.to_f, other.to_f a == b end @@ -142,11 +166,11 @@ # def digits_encoded base_encode(digits) end - # + # Returns [String] def inspect "#{digits.join(' ')} (#{base})" end # @@ -154,17 +178,21 @@ [Radix::Rational.new(value), self] end private + # Perform operation. # + # Returns new rational. [Radix::Rational] def operation(op, other) x = value.__send__(op, other.to_r) self.class.new(x, base) end + # Perform base conversion. # + # Returns array of places. [Array] def base_conversion(value, base) #if value < 0 # @negative, value = true, value.abs #end i = value.abs @@ -173,31 +201,38 @@ while i > 0 i, r = i.divmod(base) a << r end + a << 0 if a.empty? + a.reverse end end end class ::Array # Convenience method for creating a Radix::Rational. + # # TODO: Keep #br? Or find another way? + # + # Returns [Radix::Rational] def br(base=nil) args = dup args << base if base Radix::Rational.new(*args) end end -class ::Float - # - def to_r - n, f = to_s.split('.') - d = (10 * f.size).to_i - n = (n.to_i * d) + f.to_i - Rational(n, d) +if RUBY_VERSION < '1.9' + class ::Float + def to_r + n, f = to_s.split('.') + d = (10 ** f.size).to_i + n = (n.to_i * d) + f.to_i + Rational(n, d) + end end end +