Class: Radix::Integer
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Base of the number.
-
#code ⇒ Object
readonly
Base encoding table.
-
#value ⇒ Object
readonly
Stores the numerc value as normal number.
Instance Method Summary collapse
-
#%(other) ⇒ Object
Modulo.
-
#&(o) ⇒ Object
AND bit operator.
-
#*(other) ⇒ Object
Multiplication.
-
#**(other) ⇒ Object
Power.
-
#+(other) ⇒ Object
Addition.
-
#-(other) ⇒ Object
Subtraction.
-
#/(other) ⇒ Object
Division.
-
#<<(o) ⇒ Object
Asymmetric binary shift operator.
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Simple equality requires equal values only.
- #abs ⇒ Object
- #coerce(value) ⇒ Object
- #convert(base) ⇒ Object
- #digits ⇒ Object
- #digits_encoded ⇒ Object
-
#eql?(num) ⇒ Boolean
Strict equality requires same class as well as value.
- #inspect ⇒ Object
-
#negative? ⇒ Boolean
Returns true if the number is negative.
- #to_a(base = nil) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object (also: #to_int)
- #to_s(base = nil, divider = nil) ⇒ Object
Instance Attribute Details
#base ⇒ Object (readonly)
Base of the number.
12 13 14 |
# File 'lib/radix/integer.rb', line 12 def base @base end |
#code ⇒ Object (readonly)
Base encoding table.
15 16 17 |
# File 'lib/radix/integer.rb', line 15 def code @code end |
#value ⇒ Object (readonly)
Stores the numerc value as normal number.
9 10 11 |
# File 'lib/radix/integer.rb', line 9 def value @value end |
Instance Method Details
#%(other) ⇒ Object
Modulo
150 151 152 |
# File 'lib/radix/integer.rb', line 150 def %(other) operation(:%, other) end |
#&(o) ⇒ Object
AND bit operator
160 161 162 |
# File 'lib/radix/integer.rb', line 160 def &(o) Radix::Integer.new(to_int & o.to_int, base) end |
#*(other) ⇒ Object
Multiplication
135 136 137 |
# File 'lib/radix/integer.rb', line 135 def *(other) operation(:*, other) end |
#**(other) ⇒ Object
Power
145 146 147 |
# File 'lib/radix/integer.rb', line 145 def **(other) operation(:**, other) end |
#+(other) ⇒ Object
Addition
125 126 127 |
# File 'lib/radix/integer.rb', line 125 def +(other) operation(:+, other) end |
#-(other) ⇒ Object
Subtraction
130 131 132 |
# File 'lib/radix/integer.rb', line 130 def -(other) operation(:-, other) end |
#/(other) ⇒ Object
Division
140 141 142 |
# File 'lib/radix/integer.rb', line 140 def /(other) operation(:/, other) end |
#<<(o) ⇒ Object
Asymmetric binary shift operator.
155 156 157 |
# File 'lib/radix/integer.rb', line 155 def <<(o) Radix::Integer.new(to_int << o.to_int, base) end |
#<=>(other) ⇒ Object
186 187 188 |
# File 'lib/radix/integer.rb', line 186 def <=>(other) value <=> other.to_f # to_num end |
#==(other) ⇒ Object
Simple equality requires equal values only. TODO: Handle Float and Radix::Float.
176 177 178 179 180 181 182 183 |
# File 'lib/radix/integer.rb', line 176 def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end |
#abs ⇒ Object
165 166 167 |
# File 'lib/radix/integer.rb', line 165 def abs self.class.new(value.abs, base) end |
#coerce(value) ⇒ Object
191 192 193 |
# File 'lib/radix/integer.rb', line 191 def coerce(value) [Radix::Integer.new(value), self] end |
#convert(base) ⇒ Object
118 119 120 121 122 |
# File 'lib/radix/integer.rb', line 118 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 ⇒ Object
101 102 103 104 105 |
# File 'lib/radix/integer.rb', line 101 def digits i = base_conversion(value, base) i.unshift('-') if negative? i end |
#digits_encoded ⇒ Object
108 109 110 |
# File 'lib/radix/integer.rb', line 108 def digits_encoded base_encode(digits) end |
#eql?(num) ⇒ Boolean
Strict equality requires same class as well as value.
170 171 172 |
# File 'lib/radix/integer.rb', line 170 def eql?(num) self.class.equal?(num.class) && self == num end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/radix/integer.rb', line 96 def inspect "#{digits.join(' ')} (#{base})" end |
#negative? ⇒ Boolean
Returns true if the number is negative.
113 114 115 |
# File 'lib/radix/integer.rb', line 113 def negative? value < 0 end |
#to_a(base = nil) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/radix/integer.rb', line 69 def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end |
#to_f ⇒ Object
64 65 66 |
# File 'lib/radix/integer.rb', line 64 def to_f value.to_f #(sign + convert(10).digits.join('')).to_f end |
#to_i ⇒ Object Also known as: to_int
56 57 58 |
# File 'lib/radix/integer.rb', line 56 def to_i value.to_i #(sign + convert(10).digits.join('')).to_i end |
#to_s(base = nil, divider = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/radix/integer.rb', line 78 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 |