Inherits:
Numeric
• Object
show all
Defined in:

## Overview

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

## Instance Attribute Summary collapse

Base of the number.

Base encoding table.

Stores the numeric value as normal number.

## Instance Method Summary collapse

• Modulo binary operation.

• AND bitwise operator.

• Multiplication binary operation.

• Power, exponentional operation.

• Subtraction binary operation.

• Division binary operation.

• Leftwise bit shift operator.

• Comparitive binary operation.

• Simple equality requires equal values only.

• Returns the absolute value of self in @base.

• private

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

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

• Converts Integer to a new base.

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

• Returns the encoded version of digits.

• Strict equality requires same class as well as value.

• constructor private

Starts a new instance of the Radix::Integer class.

• Creates a string representation of self.

• Returns true if the number is negative.

• private

Perform passed arithmetic operation.

• private

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

• private

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

• Makes this Radix::Integer an array using code if defined.

• Makes this Radix::Integer a ruby float.

• #to_i ⇒ Fixnum (also: #to_int)

Makes this Radix::Integer a ruby integer.

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

## Constructor Details

### #initialize(value, base = 10) ⇒ void(private)

Starts a new instance of the Radix::Integer class

Parameters:

• value

The value of the new integer in context of base.

• base (Fixnum, Array<String>) (defaults to: 10)

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

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

## Instance Attribute Details

Base of the number.

Returns:

• (Fixnum)

The base level of Integer instance.

 ``` 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423``` ```# File 'lib/radix/integer.rb', line 15 class Integer < Numeric ## # Stores the numeric value as normal number. # # @return [Fixnum] Integer's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Integer instance. attr :base ## # Base encoding table. # # @return [Array, nil] Substitution chars or nil if default. attr :code private ## # 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] base The base context in which value is # determined. Can be an array of characters to use in place of default. # # @return [void] def initialize(value, base=10) @base, @code = parse_base(base) @value = parse_value(value, @base) end ## # Takes a Radix::Numeric, String or array and returns the decimal value for # storage in @value. # # @param [Radix::Numeric, Numeric, String, Array] value # The value of the integer in base context. # # @param [Fixnum, Array] base # The context base of value. # # @return [Fixnum] Decimal value of Integer. 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 ## # 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] value Given value. # # @param [Fixnum, Array] base Desired base. # # @return [Fixnum] Decimal version of array value in base context. def parse_array(value, base) if i = value.index(DOT) value = [0...i] end super(value, base) end ## digits << #Radix.convert(d, base, 10).to_i public ## # Makes this Radix::Integer a ruby integer. # # @return [Fixnum] Base(10) value. def to_i value.to_i #(sign + convert(10).digits.join('')).to_i end alias :to_int :to_i ## # Makes this Radix::Integer a ruby float. # # @return [Float] Base(10) value as float. def to_f value.to_f #(sign + convert(10).digits.join('')).to_f end ## # Makes this Radix::Integer an array using code if defined. Returns an # array using default chars otherwise. # # @param [Fixnum] base Desired base. # # @return [Array] Current base encoded array. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # 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] base # Desired base. # # @param [String] divider # Desired divider character(s). # # @return [String] Encoded string with specified divider. 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 ## # Creates a string representation of self. # # @return [String] String rep of self.digits and @base. def inspect "#{digits.join(' ')} (#{base})" end ## # Returns an array representation of each column's value in decimal chars. # # @return [Array] Values per column of @base as array. # Prepended with "-" if negative. def digits i = base_conversion(value, base) i.unshift('-') if negative? i end ## # Returns the encoded version of digits. # # @return [Array] The encoded digits. Or digits if @code exists. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative. # # @return [Boolean] True if negative. def negative? value < 0 end ## # Converts Integer to a new base. # # @param [Fixnum, Array] 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. 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 ## # 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. def +(other) operation(:+, other) end ## # Subtraction binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def -(other) operation(:-, other) end ## # Multiplication binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def *(other) operation(:*, other) end ## # Division binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def /(other) operation(:/, other) end ## # Power, exponentional operation. # # @param [#to_i] other # The exponent by which to raise Integer. # # @return [Radix::Integer] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Modulo result of division operation. def %(other) operation(:%, other) end ## # 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. def <<(integer) Radix::Integer.new(to_int << integer.to_int, base) end ## # AND bitwise operator # # @param [#to_int] integer # # @return [Radix::Integer] The logical AND. def &(integer) Radix::Integer.new(to_int & integer.to_int, base) end ## # Returns the absolute value of self in @base. # # @return [Radix::Integer] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Strict equality requires same class as well as value. # # @param [Object] num # Object to compare. # # @return [Boolean] True if class and value are equal. def eql?(num) self.class.equal?(num.class) && self == num end ## # 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. def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end ## # 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. def <=>(other) value <=> other.to_f # to_num end ## # Create a new Radix::Integer from value in Base-10 # # @return [Array] An array of the new Integer object and # self. def coerce(value) [Radix::Integer.new(value), self] end private ## # 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. def operation(op, other) a = self.to_i b = other.to_i x = a.__send__(op, b) self.class.new(x, base) end ## # 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] 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 end```

Base encoding table.

Returns:

• (Array<String>, nil)

Substitution chars or nil if default.

 ``` 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423``` ```# File 'lib/radix/integer.rb', line 15 class Integer < Numeric ## # Stores the numeric value as normal number. # # @return [Fixnum] Integer's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Integer instance. attr :base ## # Base encoding table. # # @return [Array, nil] Substitution chars or nil if default. attr :code private ## # 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] base The base context in which value is # determined. Can be an array of characters to use in place of default. # # @return [void] def initialize(value, base=10) @base, @code = parse_base(base) @value = parse_value(value, @base) end ## # Takes a Radix::Numeric, String or array and returns the decimal value for # storage in @value. # # @param [Radix::Numeric, Numeric, String, Array] value # The value of the integer in base context. # # @param [Fixnum, Array] base # The context base of value. # # @return [Fixnum] Decimal value of Integer. 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 ## # 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] value Given value. # # @param [Fixnum, Array] base Desired base. # # @return [Fixnum] Decimal version of array value in base context. def parse_array(value, base) if i = value.index(DOT) value = [0...i] end super(value, base) end ## digits << #Radix.convert(d, base, 10).to_i public ## # Makes this Radix::Integer a ruby integer. # # @return [Fixnum] Base(10) value. def to_i value.to_i #(sign + convert(10).digits.join('')).to_i end alias :to_int :to_i ## # Makes this Radix::Integer a ruby float. # # @return [Float] Base(10) value as float. def to_f value.to_f #(sign + convert(10).digits.join('')).to_f end ## # Makes this Radix::Integer an array using code if defined. Returns an # array using default chars otherwise. # # @param [Fixnum] base Desired base. # # @return [Array] Current base encoded array. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # 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] base # Desired base. # # @param [String] divider # Desired divider character(s). # # @return [String] Encoded string with specified divider. 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 ## # Creates a string representation of self. # # @return [String] String rep of self.digits and @base. def inspect "#{digits.join(' ')} (#{base})" end ## # Returns an array representation of each column's value in decimal chars. # # @return [Array] Values per column of @base as array. # Prepended with "-" if negative. def digits i = base_conversion(value, base) i.unshift('-') if negative? i end ## # Returns the encoded version of digits. # # @return [Array] The encoded digits. Or digits if @code exists. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative. # # @return [Boolean] True if negative. def negative? value < 0 end ## # Converts Integer to a new base. # # @param [Fixnum, Array] 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. 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 ## # 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. def +(other) operation(:+, other) end ## # Subtraction binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def -(other) operation(:-, other) end ## # Multiplication binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def *(other) operation(:*, other) end ## # Division binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def /(other) operation(:/, other) end ## # Power, exponentional operation. # # @param [#to_i] other # The exponent by which to raise Integer. # # @return [Radix::Integer] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Modulo result of division operation. def %(other) operation(:%, other) end ## # 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. def <<(integer) Radix::Integer.new(to_int << integer.to_int, base) end ## # AND bitwise operator # # @param [#to_int] integer # # @return [Radix::Integer] The logical AND. def &(integer) Radix::Integer.new(to_int & integer.to_int, base) end ## # Returns the absolute value of self in @base. # # @return [Radix::Integer] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Strict equality requires same class as well as value. # # @param [Object] num # Object to compare. # # @return [Boolean] True if class and value are equal. def eql?(num) self.class.equal?(num.class) && self == num end ## # 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. def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end ## # 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. def <=>(other) value <=> other.to_f # to_num end ## # Create a new Radix::Integer from value in Base-10 # # @return [Array] An array of the new Integer object and # self. def coerce(value) [Radix::Integer.new(value), self] end private ## # 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. def operation(op, other) a = self.to_i b = other.to_i x = a.__send__(op, b) self.class.new(x, base) end ## # 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] 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 end```

Stores the numeric value as normal number.

Returns:

• (Fixnum)

Integer's decimal value.

 ``` 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423``` ```# File 'lib/radix/integer.rb', line 15 class Integer < Numeric ## # Stores the numeric value as normal number. # # @return [Fixnum] Integer's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Integer instance. attr :base ## # Base encoding table. # # @return [Array, nil] Substitution chars or nil if default. attr :code private ## # 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] base The base context in which value is # determined. Can be an array of characters to use in place of default. # # @return [void] def initialize(value, base=10) @base, @code = parse_base(base) @value = parse_value(value, @base) end ## # Takes a Radix::Numeric, String or array and returns the decimal value for # storage in @value. # # @param [Radix::Numeric, Numeric, String, Array] value # The value of the integer in base context. # # @param [Fixnum, Array] base # The context base of value. # # @return [Fixnum] Decimal value of Integer. 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 ## # 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] value Given value. # # @param [Fixnum, Array] base Desired base. # # @return [Fixnum] Decimal version of array value in base context. def parse_array(value, base) if i = value.index(DOT) value = [0...i] end super(value, base) end ## digits << #Radix.convert(d, base, 10).to_i public ## # Makes this Radix::Integer a ruby integer. # # @return [Fixnum] Base(10) value. def to_i value.to_i #(sign + convert(10).digits.join('')).to_i end alias :to_int :to_i ## # Makes this Radix::Integer a ruby float. # # @return [Float] Base(10) value as float. def to_f value.to_f #(sign + convert(10).digits.join('')).to_f end ## # Makes this Radix::Integer an array using code if defined. Returns an # array using default chars otherwise. # # @param [Fixnum] base Desired base. # # @return [Array] Current base encoded array. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # 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] base # Desired base. # # @param [String] divider # Desired divider character(s). # # @return [String] Encoded string with specified divider. 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 ## # Creates a string representation of self. # # @return [String] String rep of self.digits and @base. def inspect "#{digits.join(' ')} (#{base})" end ## # Returns an array representation of each column's value in decimal chars. # # @return [Array] Values per column of @base as array. # Prepended with "-" if negative. def digits i = base_conversion(value, base) i.unshift('-') if negative? i end ## # Returns the encoded version of digits. # # @return [Array] The encoded digits. Or digits if @code exists. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative. # # @return [Boolean] True if negative. def negative? value < 0 end ## # Converts Integer to a new base. # # @param [Fixnum, Array] 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. 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 ## # 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. def +(other) operation(:+, other) end ## # Subtraction binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def -(other) operation(:-, other) end ## # Multiplication binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def *(other) operation(:*, other) end ## # Division binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Result of arithmetic operation. def /(other) operation(:/, other) end ## # Power, exponentional operation. # # @param [#to_i] other # The exponent by which to raise Integer. # # @return [Radix::Integer] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_i] other # # @return [Radix::Integer] Modulo result of division operation. def %(other) operation(:%, other) end ## # 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. def <<(integer) Radix::Integer.new(to_int << integer.to_int, base) end ## # AND bitwise operator # # @param [#to_int] integer # # @return [Radix::Integer] The logical AND. def &(integer) Radix::Integer.new(to_int & integer.to_int, base) end ## # Returns the absolute value of self in @base. # # @return [Radix::Integer] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Strict equality requires same class as well as value. # # @param [Object] num # Object to compare. # # @return [Boolean] True if class and value are equal. def eql?(num) self.class.equal?(num.class) && self == num end ## # 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. def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end ## # 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. def <=>(other) value <=> other.to_f # to_num end ## # Create a new Radix::Integer from value in Base-10 # # @return [Array] An array of the new Integer object and # self. def coerce(value) [Radix::Integer.new(value), self] end private ## # 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. def operation(op, other) a = self.to_i b = other.to_i x = a.__send__(op, b) self.class.new(x, base) end ## # 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] 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 end```

## Instance Method Details

Modulo binary operation.

Parameters:

Returns:

• Modulo result of division operation.

 ``` 277 278 279``` ```# File 'lib/radix/integer.rb', line 277 def %(other) operation(:%, other) end```

AND bitwise operator

Parameters:

Returns:

• The logical AND.

 ``` 301 302 303``` ```# File 'lib/radix/integer.rb', line 301 def &(integer) Radix::Integer.new(to_int & integer.to_int, base) end```

Multiplication binary operation.

Parameters:

Returns:

• Result of arithmetic operation.

 ``` 246 247 248``` ```# File 'lib/radix/integer.rb', line 246 def *(other) operation(:*, other) end```

Power, exponentional operation.

Parameters:

• other (#to_i)

The exponent by which to raise Integer.

Returns:

• Result of exponential operation.

 ``` 267 268 269``` ```# File 'lib/radix/integer.rb', line 267 def **(other) operation(:**, other) end```

Examples:

Which operand determines the base?

``````> i = Radix::Integer.new(123,16)
7 11 (16)
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``````

Parameters:

Returns:

• Result of arithmetic operation.

 ``` 226 227 228``` ```# File 'lib/radix/integer.rb', line 226 def +(other) operation(:+, other) end```

Subtraction binary operation.

Parameters:

Returns:

• Result of arithmetic operation.

 ``` 236 237 238``` ```# File 'lib/radix/integer.rb', line 236 def -(other) operation(:-, other) end```

Division binary operation.

Parameters:

Returns:

• Result of arithmetic operation.

 ``` 256 257 258``` ```# File 'lib/radix/integer.rb', line 256 def /(other) operation(:/, other) end```

Note:

Negative numbers will shift rightward. This will truncate bytes that get carried past zero.

Leftwise bit shift operator.

Parameters:

• integer (#to_int)

The number of places to shift the bits of self.

Returns:

• The new Radix::Integer with shifted bits.

 ``` 291 292 293``` ```# File 'lib/radix/integer.rb', line 291 def <<(integer) Radix::Integer.new(to_int << integer.to_int, base) end```

### #<=>(other) ⇒ Fixnum

Comparitive binary operation. Very useful for sorting methods.

Examples:

Comparison testing

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

Parameters:

• other (#to_f)

The object to compare value against.

Returns:

• (Fixnum)

Returns -1 for less than, 0 for equal or 1 for more than.

 ``` 359 360 361``` ```# File 'lib/radix/integer.rb', line 359 def <=>(other) value <=> other.to_f # to_num end```

### #==(other) ⇒ Boolean

TODO:

Simple equality requires equal values only.

Parameters:

• other (#value)

Any object that responds to value.

Returns:

• (Boolean)

True if values are equal.

 ``` 332 333 334 335 336 337 338 339``` ```# File 'lib/radix/integer.rb', line 332 def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end```

Returns the absolute value of self in @base.

Returns:

• Absolute of @value.

 ``` 309 310 311``` ```# File 'lib/radix/integer.rb', line 309 def abs self.class.new(value.abs, base) end```

### #base_conversion(value, base) ⇒ Array<Fixnum>(private)

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

Returns:

 ``` 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421``` ```# 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```

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

Returns:

• An array of the new Integer object and self.

 ``` 368 369 370``` ```# File 'lib/radix/integer.rb', line 368 def coerce(value) [Radix::Integer.new(value), self] end```

Converts Integer to a new base.

Parameters:

• base (Fixnum, Array<String>)

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

Returns:

New Integer of same value, different base.

 ``` 204 205 206 207 208``` ```# 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 ⇒ Array<String, Fixnum>

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

Returns:

• (Array<String, Fixnum>)

Values per column of @base as array. Prepended with “-” if negative.

 ``` 174 175 176 177 178``` ```# File 'lib/radix/integer.rb', line 174 def digits i = base_conversion(value, base) i.unshift('-') if negative? i end```

### #digits_encoded ⇒ Array<String>

Returns the encoded version of digits.

Returns:

• (Array<String>)

The encoded digits. Or digits if @code exists.

 ``` 184 185 186``` ```# File 'lib/radix/integer.rb', line 184 def digits_encoded base_encode(digits) end```

### #eql?(num) ⇒ Boolean

Strict equality requires same class as well as value.

Parameters:

• num (Object)

Object to compare.

Returns:

• (Boolean)

True if class and value are equal.

 ``` 320 321 322``` ```# File 'lib/radix/integer.rb', line 320 def eql?(num) self.class.equal?(num.class) && self == num end```

### #inspect ⇒ String

Creates a string representation of self.

Returns:

• (String)

String rep of self.digits and @base.

 ``` 165 166 167``` ```# File 'lib/radix/integer.rb', line 165 def inspect "#{digits.join(' ')} (#{base})" end```

### #negative? ⇒ Boolean

Returns true if the number is negative.

Returns:

• (Boolean)

True if negative.

 ``` 192 193 194``` ```# File 'lib/radix/integer.rb', line 192 def negative? value < 0 end```

Perform passed arithmetic operation.

Examples:

Which operand determines the base?

``````> i = Radix::Integer.new(123,16)
7 11 (16)
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``````

Parameters:

Returns:

• Result of binary operation in @base.

 ``` 390 391 392 393 394 395``` ```# 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) ⇒ Fixnum(private)

Note:

If a float style array is passed in for `value`, e.g. [9, '.', 5], the fractional part will simply be truncated.

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

Parameters:

Returns:

• (Fixnum)

Decimal version of array value in base context.

 ``` 88 89 90 91 92 93``` ```# 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) ⇒ Fixnum(private)

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

Parameters:

• value

The value of the integer in base context.

• base (Fixnum, Array<String>)

The context base of value.

Returns:

• (Fixnum)

Decimal value of Integer.

 ``` 63 64 65 66 67 68 69 70 71 72 73 74``` ```# 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```

### #to_a(base = nil) ⇒ Array<Fixnum, String>

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

Parameters:

• base (Fixnum) (defaults to: nil)

Desired base.

Returns:

• (Array<Fixnum, String>)

Current base encoded array.

 ``` 124 125 126 127 128 129 130``` ```# 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 ⇒ Float

Makes this Radix::Integer a ruby float.

Returns:

• (Float)

Base(10) value as float.

 ``` 113 114 115``` ```# File 'lib/radix/integer.rb', line 113 def to_f value.to_f #(sign + convert(10).digits.join('')).to_f end```

### #to_i ⇒ FixnumAlso known as: to_int

Makes this Radix::Integer a ruby integer.

Returns:

• (Fixnum)

Base(10) value.

 ``` 103 104 105``` ```# File 'lib/radix/integer.rb', line 103 def to_i value.to_i #(sign + convert(10).digits.join('')).to_i end```

### #to_s(base = nil, divider = nil) ⇒ String

Note:

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

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

Parameters:

• base (Fixnum, Array<String>) (defaults to: nil)

Desired base.

• divider (String) (defaults to: nil)

Desired divider character(s).

Returns:

• (String)

Encoded string with specified divider.

 ``` 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159``` ```# 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```