class Radix::Integer

Advanced integer class for Radix conversions and mathematical operations with other bases.

@!attribute [r] value

@return [Fixnum] Integer's decimal value.

@!attribute [r] base

@return [Fixnum] The base level of Integer instance.

@!attribute [r] code

@return [Array<String>, nil] Substitution chars or nil if default.

Attributes

base[R]

Base of the number.

@return [Fixnum] The base level of Integer instance.

code[R]

Base encoding table.

@return [Array<String>, nil] Substitution chars or nil if default.

value[R]

Stores the numeric value as normal number.

@return [Fixnum] Integer’s decimal value.

Public Class Methods

new(value, base=10) click to toggle source

Starts a new instance of the Radix::Integer class

@param [Radix::Numeric, Numeric, Array, String] value

The value of the new integer in context of base.

@param [Fixnum, Array<String>] base The base context in which value is

determined. Can be an array of characters to use in place of default.

@return [void]

# File lib/radix/integer.rb, line 47
def initialize(value, base=10)
  @base, @code = parse_base(base)
  @value = parse_value(value, @base)
end

Public Instance Methods

%(other) click to toggle source

Modulo binary operation.

@param [#to_i] other

@return [Radix::Integer] Modulo result of division operation.

# File lib/radix/integer.rb, line 277
def %(other)
  operation(:%, other)
end
&(integer) click to toggle source

AND bitwise operator

@param [#to_int] integer

@return [Radix::Integer] The logical AND.

# File lib/radix/integer.rb, line 301
def &(integer)
  Radix::Integer.new(to_int & integer.to_int, base)
end
*(other) click to toggle source

Multiplication binary operation.

@param [#to_i] other

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

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

Power, exponentional operation.

@param [#to_i] other

The exponent by which to raise Integer.

@return [Radix::Integer] Result of exponential operation.

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

Addition binary operation.

@param [#to_i] other

@example Which operand determines the base?

> i = Radix::Integer.new(123,16)
7 11 (16)
> i2 = Radix::Integer.new(456,10)
4 5 6 (10)
> i + i2          # i is base 16 and is first operand
2 4 3 (16)        # so base of return is 16
> i2 + i          # i2 is base 10 and is first operand
5 7 9 (10)        # so base of return is 10

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

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

Subtraction binary operation.

@param [#to_i] other

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

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

Division binary operation.

@param [#to_i] other

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

# File lib/radix/integer.rb, line 256
def /(other)
  operation(:/, other)
end
<<(integer) click to toggle source

Leftwise bit shift operator.

@note Negative numbers will shift rightward. This will truncate bytes

that get carried past zero.

@param [#to_int] integer

The number of places to shift the bits of self.

@return [Radix::Integer] The new Radix::Integer with shifted bits.

# File lib/radix/integer.rb, line 291
def <<(integer)
  Radix::Integer.new(to_int << integer.to_int, base)
end
<=>(other) click to toggle source

Comparitive binary operation. Very useful for sorting methods.

@param [#to_f] other The object to compare value against.

@example Comparison testing

> lower = Radix::Integer.new(123,10)
1 2 3 (10)
> higher = Radix::Integer.new(456, 16)
1 12 8 (16)
> lower <=> higher
-1
> lower <=> 123
0
> higher <=> lower
1

@return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than.

# File lib/radix/integer.rb, line 359
def <=>(other)
  value <=> other.to_f  # to_num
end
==(other) click to toggle source

Simple equality requires equal values only. @todo Handle Float and Radix::Float.

@param [#value] other

Any object that responds to value.

@return [Boolean] True if values are equal.

# File lib/radix/integer.rb, line 332
def ==(other)
  case other
  when Float, Integer  # Radix
    value == other.value
  else
    value == other
  end
end
abs() click to toggle source

Returns the absolute value of self in @base.

@return [Radix::Integer] Absolute of @value.

# File lib/radix/integer.rb, line 309
def abs
  self.class.new(value.abs, base)
end
coerce(value) click to toggle source

Create a new Radix::Integer from value in Base-10

@return [Array<Radix::Integer>] An array of the new Integer object and

self.
# File lib/radix/integer.rb, line 368
def coerce(value)
  [Radix::Integer.new(value), self]  
end
convert(base) click to toggle source

Converts Integer to a new base.

@param [Fixnum, Array<String>] base

The base context in which value is determined. Can be an array
of characters to use in place of default.

@return [Radix:Integer] New Integer of same value, different base.

# File lib/radix/integer.rb, line 204
def convert(base)
  self.class.new(value, base)
  #new_digits = Radix::Base.convert_base(digits, base, new_base)
  #self.class.new(new_digits, new_base)
end
digits() click to toggle source

Returns an array representation of each column’s value in decimal chars.

@return [Array<String, Fixnum>] Values per column of @base as array.

Prepended with "-" if negative.
# File lib/radix/integer.rb, line 174
def digits
  i = base_conversion(value, base)
  i.unshift('-') if negative?
  i
end
digits_encoded() click to toggle source

Returns the encoded version of digits.

@return [Array<String>] The encoded digits. Or digits if @code exists.

# File lib/radix/integer.rb, line 184
def digits_encoded
  base_encode(digits)
end
eql?(num) click to toggle source

Strict equality requires same class as well as value.

@param [Object] num

Object to compare.

@return [Boolean] True if class and value are equal.

# File lib/radix/integer.rb, line 320
def eql?(num)
  self.class.equal?(num.class) && self == num
end
inspect() click to toggle source

Creates a string representation of self.

@return [String] String rep of self.digits and @base.

# File lib/radix/integer.rb, line 165
def inspect
  "#{digits.join(' ')} (#{base})"
end
negative?() click to toggle source

Returns true if the number is negative.

@return [Boolean] True if negative.

# File lib/radix/integer.rb, line 192
def negative?
  value < 0
end
to_a(base=nil) click to toggle source

Makes this Radix::Integer an array using code if defined. Returns an array using default chars otherwise.

@param [Fixnum] base Desired base.

@return [Array<Fixnum, String>] Current base encoded array.

# File lib/radix/integer.rb, line 124
def to_a(base=nil)
  if base
    convert(base).digits_encoded
  else
    digits_encoded
  end
end
to_f() click to toggle source

Makes this Radix::Integer a ruby float.

@return [Float] Base(10) value as float.

# File lib/radix/integer.rb, line 113
def to_f
  value.to_f #(sign + convert(10).digits.join('')).to_f
end
to_i() click to toggle source

Makes this Radix::Integer a ruby integer.

@return [Fixnum] Base(10) value.

# File lib/radix/integer.rb, line 103
def to_i
  value.to_i #(sign + convert(10).digits.join('')).to_i
end
Also aliased as: to_int
to_int()
Alias for: to_i
to_s(base=nil, divider=nil) click to toggle source

Creates an encoded string in desired base, with desired digit divider.

@note For base 10 or less does not use a divider unless specified.

@param [Fixnum, Array<String>] base

Desired base.

@param [String] divider

Desired divider character(s).

@return [String] Encoded string with specified divider.

# File lib/radix/integer.rb, line 144
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

base_conversion(value, base) click to toggle source

Returns the value as an array of decimal values where each column is a place of @base.

@param (see Radix::Integer.value) @param (see Radix::Integer.base)

@return [Array<Fixnum>]

# File lib/radix/integer.rb, line 405
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

  # if nothing add zero
  a << 0 if a.empty?

  a.reverse
end
operation(op, other) click to toggle source

Perform passed arithmetic operation.

@param [#to_i] other

@example Which operand determines the base?

> i = Radix::Integer.new(123,16)
7 11 (16)
> i2 = Radix::Integer.new(456,10)
4 5 6 (10)
> i + i2          # i is base 16 and is first operand
2 4 3 (16)        # so base of return is 16
> i2 + i          # i2 is base 10 and is first operand
5 7 9 (10)        # so base of return is 10

@return [Radix::Integer] Result of binary operation in @base.

# File lib/radix/integer.rb, line 390
def operation(op, other)
  a = self.to_i
  b = other.to_i
  x = a.__send__(op, b)
  self.class.new(x, base)
end
parse_array(value, base) click to toggle source

Take an Array in the form of […, d2, d1, d0] and convert it to base ten, and store in @value.

@note If a float style array is passed in for value, e.g. [9, ‘.’, 5],

the fractional part will simply be truncated.

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

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

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

Calls superclass method
# File lib/radix/integer.rb, line 88
def parse_array(value, base)
  if i = value.index(DOT)
    value = [0...i]
  end
  super(value, base)
end
parse_value(value, base) click to toggle source

Takes a Radix::Numeric, String or array and returns the decimal value for storage in @value.

@param [Radix::Numeric, Numeric, String, Array<Numeric, String>] value

The value of the integer in base context.

@param [Fixnum, Array<String>] base

The context base of value.

@return [Fixnum] Decimal value of Integer.

# File lib/radix/integer.rb, line 63
def parse_value(value, base)
  case value
  when Integer, Float # Radix
    parse_numeric(value.to_i, base)
  when ::Array
    parse_array(value, base)
  when ::String
    parse_string(value, base)
  when ::Numeric
    parse_numeric(value, base)
  end
end