class Radix::Numeric

Radix::Numeric class inherits from Ruby’s Numeric class. It is then subclassed by Radix::Integer and Radix::Float.

@todo Make immutable, but best way to do it? @example First suggestion

class << self
  alias_method :_new, :new
  private :_new
end

@example Second suggestion

def self.new(value, base=10)
  @cache ||= {}
  @cache[[value, base]] ||= _new(value, base)
end

Public Instance Methods

*(other) click to toggle source

Multiplication, binary operation.

@param [Radix::Numeric] other

@return [Radix::Numeric] Result of arithmetic operation.

# File lib/radix/numeric.rb, line 57
def *(other)
  operation(:*, other)
end
+(other) click to toggle source

Addition, binary operation.

@param [Radix::Numeric] other

@return [Radix::Numeric] Result of arithmetic operation.

# File lib/radix/numeric.rb, line 37
def +(other)
  operation(:+, other)
end
-(other) click to toggle source

Subtraction, binary operation.

@param [Radix::Numeric] other

@return [Radix::Numeric] Result of arithmetic operation.

# File lib/radix/numeric.rb, line 47
def -(other)
  operation(:-, other)
end
/(other) click to toggle source

Division, binary operation.

@param [Radix::Numeric] other

@return [Radix::Numeric] Result of arithmetic operation.

# File lib/radix/numeric.rb, line 67
def /(other)
  operation(:/, other)
end

Private Instance Methods

base_decode(digits) click to toggle source

Decode an encoded array. Defaults to BASE::B62 if self.code is not set.

@param [Array<String, Numeric>] digits The encoded characters.

@return [Array<String, Numeric>] Decoded array.

# File lib/radix/numeric.rb, line 200
def base_decode(digits)
  #return digits unless code
  code = self.code || BASE::B62
  digits.map do |c|
    case c
    when '-', DOT, DIV
      c
    when ::Numeric
      c
    else
      code.index(c)  # TODO: Could speed up with an reverse index.
    end
  end
end
base_encode(digits) click to toggle source

Map array of values to base encoding. If no encoding is defined this simply returns the digits unchanged.

@param [Array<String, Numeric>] digits

@return [Array<String, Fixnum>] Encoded digits, or digits if @code is nil

# File lib/radix/numeric.rb, line 182
def base_encode(digits)
  return digits unless @code
  digits.map do |i|
    case i
    when '-', DOT, DIV
      i
    else
      code[i]
    end
  end
end
decimal(digits, base) click to toggle source

Convert array of values of a different base to decimal. This handles integer values. The method for Radix::Float is slighly different.

@param [Array<Numeric, String>] digits

Representation of base values.

@param [Fixnum, Array<String>] base

The base to convert from.

@return [Integer] The digits of base converted to decimal.

# File lib/radix/numeric.rb, line 165
def decimal(digits, base)
  e = digits.size - 1
  v = 0
  digits.each do |n|
    v += n.to_i * base**e
    e -= 1
  end
  v
end
parse_array(value, base) click to toggle source

Take an Array in the form of [d1, d2, …, DOT, d-1, d-2, …] and convert it to base ten, and store in @value.

@param [Array<String, Numeric>] value Given value. @param [Fixnum, Array<String>] base Desired base.

@return [Fixnum] Decimal version of passed array in base context.

# File lib/radix/numeric.rb, line 134
def parse_array(value, base)
  value = value.dup

  if value.first == '-'
    neg = true
    value.shift
  else
    neg = false
  end

  value = base_decode(value)

  ## raise an error if any digit is not less than base
  raise ArgumentError if value.any?{ |e| ::Numeric === e && base < e }

  v = decimal(value, base)

  neg ? -v : v # Returns negated v if value array.first == "-"
end
parse_base(base) click to toggle source

Parses the value of the base and character set to use.

@param [Fixnum, Array<String>] base

The value of the base, or a set of characters to use as
representation of the base.

@note If an array of String characters is passed, its length is the

value of the base level.

@return [Array<(Fixnum, [Array<String>, nil])>] Two part array:

0 - Fixnum value of the base.
1 - Nil, or Array of characters representing the base values.
# File lib/radix/numeric.rb, line 86
def parse_base(base)
  case base
   when Array
    code = base
    base = base.size
  else
    code = nil
    base = base
  end
  return base, code
end
parse_numeric(value, base) click to toggle source

Simply returns the passed value. Used for simplifying creation of Radix::Numeric instances.

@param [Radix::Float, Radix::Integer] value Given value. @param [Fixnum, Array<String>] base Desired base.

@return [Radix::Float, Radix::Integer] The passed value.

# File lib/radix/numeric.rb, line 106
def parse_numeric(value, base)
  value
end
parse_string(value, base) click to toggle source

If a float style string is passed in for value, e.g. “9.5”, the decimal will simply be truncated. So “9.x” would become “9”.

@param [String] value

Given value.

@param [Fixnum, Array<String>] base

Desired base.

@return [Radix::Float, Radix::Integer] The passed value.

# File lib/radix/numeric.rb, line 121
def parse_string(value, base)
  digits = value.split(//)
  parse_array(digits, base)
end