Class: HyperInt

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperint.rb,
lib/hyperint/arithmetic.rb,
lib/hyperint/comparison.rb,
lib/hyperint/conversion.rb

Constant Summary collapse

BASE =
1_000_000_000
BASE_DIGITS =
9

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, base = 10) ⇒ HyperInt

Returns a new instance of HyperInt.

Raises:

  • (TypeError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/hyperint.rb', line 11

def initialize(value, base = 10)
  raise TypeError, "value must be an Integer or String" unless value.is_a?(Integer) || value.is_a?(String)
  raise ArgumentError, "base must be between 2 and 36" unless (2..36).include?(base)

  if value.is_a?(Integer)
    initialize_from_integer(value)
  else
    initialize_from_string(value, base)
  end
end

Instance Attribute Details

#digits ⇒ Object (readonly)

Returns the value of attribute digits.



9
10
11
# File 'lib/hyperint.rb', line 9

def digits
  @digits
end

#sign ⇒ Object (readonly)

Returns the value of attribute sign.



9
10
11
# File 'lib/hyperint.rb', line 9

def sign
  @sign
end

Class Method Details

.from_integer(value) ⇒ Object



22
23
24
# File 'lib/hyperint.rb', line 22

def self.from_integer(value)
  new(value)
end

.one ⇒ Object



49
50
51
# File 'lib/hyperint/conversion.rb', line 49

def self.one
  new(1)
end

.zero ⇒ Object



45
46
47
# File 'lib/hyperint/conversion.rb', line 45

def self.zero
  new(0)
end

Instance Method Details

#abs ⇒ Object



19
20
21
22
23
24
# File 'lib/hyperint/conversion.rb', line 19

def abs
  copy = HyperInt.new(0)
  copy.instance_variable_set(:@sign, @sign.abs)
  copy.instance_variable_set(:@digits, @digits.dup)
  copy
end

#add(other) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hyperint/arithmetic.rb', line 2

def add(other)
  check_type(other)

  if zero?
    return other.abs
  end

  if other.zero?
    return abs
  end

  if @sign == other.sign
    result = add_magnitudes(@digits, other.digits)
    return build_from_digits(@sign, result)
  end

  comparison = compare_magnitudes(@digits, other.digits)

  if comparison == 0
    return HyperInt.zero
  end

  if comparison > 0
    result = subtract_magnitudes(@digits, other.digits)
    build_from_digits(@sign, result)
  else
    result = subtract_magnitudes(other.digits, @digits)
    build_from_digits(other.sign, result)
  end
end

#div(other) ⇒ Object

Raises:

  • (ZeroDivisionError)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hyperint/arithmetic.rb', line 71

def div(other)
  check_type(other)
  raise ZeroDivisionError, "divided by 0" if other.zero?

  quotient, = divide_magnitudes(@digits, other.digits)

  if quotient == [0]
    return HyperInt.zero
  end

  sign = @sign == other.sign ? 1 : -1
  build_from_digits(sign, quotient)
end

#divide_small(divisor) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hyperint/conversion.rb', line 53

def divide_small(divisor)
  result = []
  remainder = 0

  @digits.reverse_each do |digit|
    current = remainder * BASE + digit
    quotient = current / divisor
    remainder = current % divisor
    result << quotient
  end

  result.reverse!

  while result.length > 1 && result[-1] == 0
    result.pop
  end

  quotient = HyperInt.new(0)
  quotient.instance_variable_set(:@sign, result == [0] ? 0 : @sign)
  quotient.instance_variable_set(:@digits, result)

  [quotient, remainder]
end

#equals?(other) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
# File 'lib/hyperint/comparison.rb', line 2

def equals?(other)
  check_type(other)
  to_i == other.to_i
end

#greater_than?(other) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/hyperint/comparison.rb', line 11

def greater_than?(other)
  check_type(other)
  to_i > other.to_i
end

#greater_than_or_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/hyperint/comparison.rb', line 16

def greater_than_or_equal?(other)
  check_type(other)
  to_i >= other.to_i
end

#less_than?(other) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/hyperint/comparison.rb', line 21

def less_than?(other)
  check_type(other)
  to_i < other.to_i
end

#less_than_or_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/hyperint/comparison.rb', line 26

def less_than_or_equal?(other)
  check_type(other)
  to_i <= other.to_i
end

#mod(other) ⇒ Object

Raises:

  • (ZeroDivisionError)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hyperint/arithmetic.rb', line 85

def mod(other)
  check_type(other)
  raise ZeroDivisionError, "divided by 0" if other.zero?

  _, remainder = divide_magnitudes(@digits, other.digits)

  if remainder == [0]
    return HyperInt.zero
  end

  build_from_digits(@sign, remainder)
end

#mul(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hyperint/arithmetic.rb', line 38

def mul(other)
  check_type(other)

  return HyperInt.zero if zero? || other.zero?

  result = Array.new(@digits.length + other.digits.length, 0)

  @digits.each_with_index do |a, i|
    carry = 0

    other.digits.each_with_index do |b, j|
      index = i + j
      value = result[index] + a * b + carry

      result[index] = value % BASE
      carry = value / BASE
    end

    index = i + other.digits.length

    while carry > 0
      value = result[index] + carry
      result[index] = value % BASE
      carry = value / BASE
      index += 1
      result << 0 if index == result.length && carry > 0
    end
  end

  sign = @sign == other.sign ? 1 : -1
  build_from_digits(sign, result)
end

#negate ⇒ Object



26
27
28
29
30
31
# File 'lib/hyperint/conversion.rb', line 26

def negate
  copy = HyperInt.new(0)
  copy.instance_variable_set(:@sign, -@sign)
  copy.instance_variable_set(:@digits, @digits.dup)
  copy
end

#negative? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/hyperint/conversion.rb', line 41

def negative?
  @sign < 0
end

#not_equals?(other) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/hyperint/comparison.rb', line 7

def not_equals?(other)
  !equals?(other)
end

#positive? ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/hyperint/conversion.rb', line 37

def positive?
  @sign > 0
end

#pow(other) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hyperint/arithmetic.rb', line 98

def pow(other)
  check_type(other)
  raise ArgumentError, "negative exponent" if other.negative?

  result = HyperInt.one
  base = abs
  exponent = other.abs
  two = HyperInt.new(2)

  while !exponent.zero?
    if exponent.mod(two).equals?(HyperInt.one)
      result = result.mul(base)
    end

    exponent = exponent.div(two)

    unless exponent.zero?
      base = base.mul(base)
    end
  end

  if @sign < 0 && other.mod(two).equals?(HyperInt.one)
    result.negate
  else
    result
  end
end

#sub(other) ⇒ Object



33
34
35
36
# File 'lib/hyperint/arithmetic.rb', line 33

def sub(other)
  check_type(other)
  add(other.negate)
end

#to_i ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/hyperint.rb', line 26

def to_i
  value = 0

  @digits.reverse_each do |digit|
    value = value * BASE + digit
  end

  @sign < 0 ? -value : value
end

#to_s(base = 10) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hyperint/conversion.rb', line 2

def to_s(base = 10)
  raise ArgumentError, "base must be between 2 and 36" unless (2..36).include?(base)
  return "0" if zero?

  value = self
  result = +""

  while !value.zero?
    quotient, remainder = value.divide_small(base)
    result << remainder.to_s(36)
    value = quotient
  end

  result.reverse!
  @sign < 0 ? "-#{result}" : result
end

#zero? ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/hyperint/conversion.rb', line 33

def zero?
  @sign == 0
end