class Radix::Rational
Represents rational numbers.
@!attribute [r] value
@return [Rational] Ruby Rational representation of self in Base-10.
@!attribute [r] base
@return [Fixnum] The base level of Rational instance.
@!attribute [r] code
@return [Array<String>, nil] Substitution chars or nil if default.
Attributes
Base
encoding table.
@return [Array<String>, nil] Substitution chars or nil if default.
Stores the Rational
value in Base-10.
@return [Rational]
Public Class Methods
Alternative to new.
@return [Radix::Rational]
# File lib/radix/rational.rb, line 21 def self.[](n,d=nil,b=10) new(n,d,b) end
Create a new Radix::Rational
instance. @example
Rational.new(<Integer>, <Integer>, <Integer>) Rational.new(<Rational>, <Integer>)
@param [Radix::Rational, ::Rational, Fixnum] numerator A rational number
or a fixnum for the numerator of a new Rational.
@param [Fixnum] denominator Denominator for new Rational
. @param [Fixnum] base Base
level for new Rational
. @return [void]
# File lib/radix/rational.rb, line 55 def initialize(numerator, denominator=nil, base=10) case numerator when ::Rational, Rational ratn = numerator base = denominator @value = Rational(ratn.numerator, ratn.denominator) else n = parse_value(numerator, base) d = parse_value(denominator, base) @value = Rational(n, d) end @base, @code = parse_base(base) end
Public Instance Methods
Simple equality requires equal values only.
@todo This may need improvement to be more percise. @param [#to_f] other The value to compare to. @return [Boolean] True if equal values.
# File lib/radix/rational.rb, line 190 def ==(other) a, b = self.to_f, other.to_f a == b end
Returns new Radix::Rational
of passed value in base-10 and self as an Array
.
@return [Array<(Radix::Rational
, Radix::Rational
)>]
# File lib/radix/rational.rb, line 242 def coerce(value) [Radix::Rational.new(value), self] end
Convert rational to new base.
@param [Fixnum] base Desired base. @return [Radix::Rational] Returns new Radix::Rational
in passed base.
# File lib/radix/rational.rb, line 118 def convert(base) self.class.new(numerator, denominator, base) end
The denominator.
@return [Fixnum] The denominator of Radix::Rational
# File lib/radix/rational.rb, line 101 def denominator @value.denominator end
Returns an array representation of the numerator and denominator with each column’s value.
@return [Array<String, Fixnum>] Values per column of @base as array.
Prepended with "-" if negative.
# File lib/radix/rational.rb, line 211 def digits n = base_conversion(numerator, base) d = base_conversion(denominator, base) i = n + ['/'] + d i.unshift('-') if negative? i end
Returns digits, or coded version of digits if @code.
@return [Array<String, Fixnum>] Values per column of @base as array.
Prepended with "-" if negative. Or encoded version if @code is defined.
# File lib/radix/rational.rb, line 225 def digits_encoded base_encode(digits) end
Creates a string representation of self.
@return [String] String
rep of self.digits and @base.
# File lib/radix/rational.rb, line 233 def inspect "#{digits.join(' ')} (#{base})" end
Is the value negative?
@return [Boolean] Returns true if value < 0.
# File lib/radix/rational.rb, line 109 def negative? value < 0 end
The numerator.
@return [Fixnum] The numerator of Radix::Rational
# File lib/radix/rational.rb, line 93 def numerator @value.numerator end
Returns an irreducible version of self in current base.
@todo Is this method neccesary since @value is a Ruby Rational
and
therefore already irreducible?
@return [Radix::Rational]
# File lib/radix/rational.rb, line 201 def reduce self.class.new(Rational(numerator, denominator), base) end
Translate value into an array of places. Uses current base unless specified.
@param [Fixnum] base Desired base. @return [Array<Fixnum, String>] Array
of place values.
# File lib/radix/rational.rb, line 153 def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end
Convert to Float
by dividing the numerator by the denominator.
Returns the converted value. [Float]
# File lib/radix/rational.rb, line 134 def to_f numerator.to_f / denominator.to_f end
Convert to rational.
@return [Rational] Returns the value.
# File lib/radix/rational.rb, line 126 def to_r value end
Convert the value into a string representation of the given base.
@param [Fixnum] base The base to convert. @param [String] divider The string char(s) to divided with. @return [String] Translated value.
# File lib/radix/rational.rb, line 167 def to_s(base=nil, divider=nil) divider = divider.to_s if divider if base convert(base).to_s(nil, divider) else if code digits_encoded.join(divider) else if @base > 10 digits.join(divider || DIVIDER) else digits.join(divider) end end end end
Private Instance Methods
Perform base conversion.
@return [Array<Fixnum, String>] Array
of places.
# File lib/radix/rational.rb, line 262 def base_conversion(value, base) #if value < 0 # @negative, value = true, value.abs #end i = value.abs a = [] while i > 0 i, r = i.divmod(base) a << r end a << 0 if a.empty? a.reverse end
Perform passed arithmetic operation.
@param [#to_r] other
@return [Radix::Rational] Returns the result of the operation in @base.
# File lib/radix/rational.rb, line 253 def operation(op, other) x = value.__send__(op, other.to_r) self.class.new(x, base) end
Parses String
, Array
, Radix::Float
, Radix::Integer
or Ruby numerics and returns the decimal value from base context for storage in @value.
@param [Fixnum] base
# File lib/radix/rational.rb, line 74 def parse_value(value, base) case value when Float, Integer # Radix parse_numeric(value.to_i, base) when ::Array parse_array(value, base) when ::String parse_string(value, base) when ::Numeric parse_numeric(value.to_i, base) end end