Class: Radix::Float
Overview
Make fully immutable. After that we can catch @digits and the library should be a good bit faster.
Advanced float class for Radix conversions and mathematical operations with other bases.
Instance Attribute Summary collapse
-
#base ⇒ Fixnum
readonly
Base of the number.
-
#code ⇒ Array<String>?
readonly
Base encoding table.
-
#value ⇒ Float
readonly
Internal floating point value.
Instance Method Summary collapse
-
#%(other) ⇒ Radix::Float
(also: #modulo)
Modulo binary operation.
-
#**(other) ⇒ Radix::Float
Power exponentional operation.
-
#<=>(other) ⇒ Fixnum
Comparitive binary operation.
-
#==(other) ⇒ Boolean
Simple equality requires equal values only.
-
#abs ⇒ Radix::Float
Returns the absolute value of self in @base.
-
#base_conversion(value, base, prec = 10) ⇒ Array<(Array[Fixnum], Array[Fixnum])>
private
Returns two arrays.
-
#ceil ⇒ Radix::Float
Returns the largest integer greater than or equal to self as a Radix::Float.
-
#coerce(other) ⇒ Array<Radix::Float>
Create a new Radix::Float from value in Base-10.
-
#convert(new_base) ⇒ Radix::Float
Creates a new Radix::Float of same value in different base.
-
#decimal(digits, base) ⇒ Float
private
Convert array of values of a different base to decimal as called by parse_array.
-
#digits ⇒ Array<String, Fixnum>
Returns an array representation of each column’s value in decimal chars.
-
#digits_encoded ⇒ Array<String, Fixnum>
Returns digits, or coded version of digits if @code.
-
#eql?(num) ⇒ Boolean
Strict equality requires same class as well as value.
-
#floor ⇒ Radix::Float
Returns the smallest integer less than or equal to self as a Radix::Float.
-
#initialize(value, base = 10) ⇒ void
constructor
private
Starts a new instance of the Radix::Float class.
-
#inspect ⇒ String
Creates a string representation of self.
-
#negative? ⇒ Boolean
Returns true if the number is negative?.
-
#operation(op, other) ⇒ Radix::Float
private
Perform passed arithmetic operation.
-
#parse_value(value, base) ⇒ Float
private
Takes a Radix::Numeric, String or array and returns the decimal float value for storage in @value.
-
#round ⇒ Radix::Float
Returns a new Radix::Float instance of same base, rounded to the nearest whole integer.
-
#split_digits(value) ⇒ Array<(Array<Fixnum>, Array<Fixnum>)>
private
Returns the I-Part and F-Part of the passed value as arrays of fixnums.
-
#split_float(float) ⇒ Array<(Integer, Float)>
private
Returns an array of Integer and Float portions of the Radix::Float.
-
#to_a(base = nil) ⇒ Array<Fixnum, String>
Makes this Radix::Float an array using code if defined.
-
#to_f ⇒ Float
Convert Radix::Float to a Ruby float.
-
#to_i ⇒ Integer
(also: #to_int)
Convert the Radix::Float a Ruby Integer.
-
#to_s(base = nil, divider = nil) ⇒ String
Creates an encoded string in passed base, with passed digit divider.
Methods inherited from Numeric
#*, #+, #-, #/, #base_decode, #base_encode, #parse_array, #parse_base, #parse_numeric, #parse_string
Constructor Details
#initialize(value, base = 10) ⇒ void (private)
Starts a new instance of the Radix::Float class.
51 52 53 54 |
# File 'lib/radix/float.rb', line 51 def initialize(value, base=10) @base, @code = parse_base(base) @value = parse_value(value, @base) end |
Instance Attribute Details
#base ⇒ Fixnum (readonly)
Base of the number.
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/radix/float.rb', line 18 class Float < Numeric ## # Internal floating point value. # # @return [Float] Float's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Float instance. attr :base ## # Base encoding table. # # @return [Array<String>, nil] Substitution chars or nil if default. attr :code private ## # Starts a new instance of the Radix::Float 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] 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 float # 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 [Float] Float value of Integer. def parse_value(value, base) case value when Float, Integer # Radix parse_numeric(value.to_f, base) when ::Array parse_array(value, base) when ::String parse_string(value, base) when ::Numeric parse_numeric(value.to_f, base) end end public ## # Convert the Radix::Float a Ruby Integer. # # @return [Integer] Base(10) value as Integer. def to_i to_f.to_i end alias_method :to_int, :to_i ## # Convert Radix::Float to a Ruby float. # # @return [Float] Base(10) value as Float. def to_f value.to_f end ## # Makes this Radix::Float 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. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # Creates an encoded string in passed base, with passed 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. 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<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. def digits i, f = base_conversion(value, base) if negative? ['-'] + i + [DOT] + f else i + [DOT] + f end end ## # Returns digits, or coded version of digits if @code. # # @return [Array<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. # Or encoded version if @code is defined. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative? # # @return [Boolean] True if float value is < 0. def negative? value < 0 end ## # Creates a new Radix::Float of same value in different base. # # @return [Radix::Float] New float of same value in different base. def convert(new_base) self.class.new(value, new_base) end ## # Power exponentional operation. # # @param [#to_f] other # The exponent by which to raise Float. # # @return [Radix::Float] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_f] other # # @return [Radix::Float] Modulo result of division operation. def %(other) operation(:%, other) end alias_method :modulo, :% ## # Returns the absolute value of self in @base. # # @return [Radix::Float] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Returns the largest integer greater than or equal to self as a # Radix::Float. # # @return [Radix::Float] def ceil self.class.new(value.ceil, base) end ## # Returns the smallest integer less than or equal to self as a # Radix::Float. # # @return [Radix::Float] def floor self.class.new(value.floor, base) end ## # Returns a new Radix::Float instance of same base, rounded to the nearest # whole integer. # # @example Rounding Radix Float # > round_test = Radix::Float.new(123.03, 16) # 7 11 . 0 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.03 # > round_test.round # 7 11 . 0 (16) # > round_test.round.value # 123.0 # > round_test += 0.5 # 7 11 . 8 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.53 # > round_test.round # 7 12 . 0 (16) # > round_test.round.value # 124.0 # # @return [Radix::Float] New Instance def round return self.class.new((value + 0.5).floor, base) if self > 0.0 return self.class.new((value - 0.5).ceil, base) if self < 0.0 return self.class.new(0, 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. # # @param [Numeric] other # Any Numeric instance. # # @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::Float.new(123.00,10) # 1 2 3 . 0 (10) # > higher = Radix::Float.new(456.00,16) # 1 12 8 . 0 (16) # > lower <=> higher # -1 # > lower <=> 123 # 0 # > lower <=> "123" # 0 # > higher <=> lower # 1 # # @return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than. def <=>(other) to_f <=> other.to_f end # #def infinite? # digits[0,2] == [0, DOT] #end # #def finite? # !infinite #end # #def nan? # digits[-2,2] == [DOT, 0] #end ## # Create a new Radix::Float from value in Base-10. # # @param [Numeric, Array, String] other # The value of the new integer in base-10. # # @return [Array<Radix::Float>] An array of the new Float object and self. def coerce(other) [Radix::Float.new(other), self] end private ## # Perform passed arithmetic operation. # # @param [#to_f] other # # @return [Radix::Float] Result of binary operation in @base. def operation(op, other) a = self.to_f b = other.to_f x = a.__send__(op, b) Radix::Float.new(x, base) end ## # Returns two arrays. The integer part and the fractional part of the Float # value in param base. # # @param [Float] value Float's decimal value. # @param [Fixnum] base The base level of Float instance. # @param [Fixnum] prec The # of places to extend F-part. # # @return [Array<(Array[Fixnum], Array[Fixnum])>] def base_conversion(value, base, prec=10) #if value < 0 # @negative, value = true, value.abs #end value = value.to_f.abs i, f = split_float(value) a = [] while i > 0 i, r = i.divmod(base) a << r end #c = [] # f-cache p = prec b = [] while !f.zero? k = (f * base) r, f = split_float(k) #c.include?(f) ? break : c << f break if p == 0; p -= 1 b << r end a << 0 if a.empty? b << 0 if b.empty? [a.reverse, b] end ## # Convert array of values of a different base to decimal as called by # parse_array. # # @param [Array<Numeric, String>] digits Representation of Base values. # @param [Fixnum, Array<String>] base The base to convert from. # # @return [Float] The digits of base converted to decimal. def decimal(digits, base) i, f = split_digits(digits) e = i.size - 1 v = 0 (i + f).each do |n| v += n * base**e e -= 1 end v end ## # Returns the I-Part and F-Part of the passed value as arrays of fixnums. # # @param [Array<Numeric, String>] value # The array of decimal values per column of @base. # # @return [Array<(Array<Fixnum>, Array<Fixnum>)>] def split_digits(value) if d = value.index(DOT) || value.index('.') i, f = value[0...d], value[d+1..-1] else i, f = value, [0] end i.map!{ |x| x.to_i } f.map!{ |x| x.to_i } return i, f end ## # Returns an array of Integer and Float portions of the Radix::Float # # @param [Radix::Float] float Float value to split # # @return [Array<(Integer, Float)>] def split_float(float) i, f = float.to_s.split('.') return i.to_i, ('0.'+f).to_f end end |
#code ⇒ Array<String>? (readonly)
Base encoding table.
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/radix/float.rb', line 18 class Float < Numeric ## # Internal floating point value. # # @return [Float] Float's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Float instance. attr :base ## # Base encoding table. # # @return [Array<String>, nil] Substitution chars or nil if default. attr :code private ## # Starts a new instance of the Radix::Float 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] 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 float # 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 [Float] Float value of Integer. def parse_value(value, base) case value when Float, Integer # Radix parse_numeric(value.to_f, base) when ::Array parse_array(value, base) when ::String parse_string(value, base) when ::Numeric parse_numeric(value.to_f, base) end end public ## # Convert the Radix::Float a Ruby Integer. # # @return [Integer] Base(10) value as Integer. def to_i to_f.to_i end alias_method :to_int, :to_i ## # Convert Radix::Float to a Ruby float. # # @return [Float] Base(10) value as Float. def to_f value.to_f end ## # Makes this Radix::Float 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. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # Creates an encoded string in passed base, with passed 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. 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<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. def digits i, f = base_conversion(value, base) if negative? ['-'] + i + [DOT] + f else i + [DOT] + f end end ## # Returns digits, or coded version of digits if @code. # # @return [Array<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. # Or encoded version if @code is defined. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative? # # @return [Boolean] True if float value is < 0. def negative? value < 0 end ## # Creates a new Radix::Float of same value in different base. # # @return [Radix::Float] New float of same value in different base. def convert(new_base) self.class.new(value, new_base) end ## # Power exponentional operation. # # @param [#to_f] other # The exponent by which to raise Float. # # @return [Radix::Float] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_f] other # # @return [Radix::Float] Modulo result of division operation. def %(other) operation(:%, other) end alias_method :modulo, :% ## # Returns the absolute value of self in @base. # # @return [Radix::Float] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Returns the largest integer greater than or equal to self as a # Radix::Float. # # @return [Radix::Float] def ceil self.class.new(value.ceil, base) end ## # Returns the smallest integer less than or equal to self as a # Radix::Float. # # @return [Radix::Float] def floor self.class.new(value.floor, base) end ## # Returns a new Radix::Float instance of same base, rounded to the nearest # whole integer. # # @example Rounding Radix Float # > round_test = Radix::Float.new(123.03, 16) # 7 11 . 0 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.03 # > round_test.round # 7 11 . 0 (16) # > round_test.round.value # 123.0 # > round_test += 0.5 # 7 11 . 8 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.53 # > round_test.round # 7 12 . 0 (16) # > round_test.round.value # 124.0 # # @return [Radix::Float] New Instance def round return self.class.new((value + 0.5).floor, base) if self > 0.0 return self.class.new((value - 0.5).ceil, base) if self < 0.0 return self.class.new(0, 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. # # @param [Numeric] other # Any Numeric instance. # # @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::Float.new(123.00,10) # 1 2 3 . 0 (10) # > higher = Radix::Float.new(456.00,16) # 1 12 8 . 0 (16) # > lower <=> higher # -1 # > lower <=> 123 # 0 # > lower <=> "123" # 0 # > higher <=> lower # 1 # # @return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than. def <=>(other) to_f <=> other.to_f end # #def infinite? # digits[0,2] == [0, DOT] #end # #def finite? # !infinite #end # #def nan? # digits[-2,2] == [DOT, 0] #end ## # Create a new Radix::Float from value in Base-10. # # @param [Numeric, Array, String] other # The value of the new integer in base-10. # # @return [Array<Radix::Float>] An array of the new Float object and self. def coerce(other) [Radix::Float.new(other), self] end private ## # Perform passed arithmetic operation. # # @param [#to_f] other # # @return [Radix::Float] Result of binary operation in @base. def operation(op, other) a = self.to_f b = other.to_f x = a.__send__(op, b) Radix::Float.new(x, base) end ## # Returns two arrays. The integer part and the fractional part of the Float # value in param base. # # @param [Float] value Float's decimal value. # @param [Fixnum] base The base level of Float instance. # @param [Fixnum] prec The # of places to extend F-part. # # @return [Array<(Array[Fixnum], Array[Fixnum])>] def base_conversion(value, base, prec=10) #if value < 0 # @negative, value = true, value.abs #end value = value.to_f.abs i, f = split_float(value) a = [] while i > 0 i, r = i.divmod(base) a << r end #c = [] # f-cache p = prec b = [] while !f.zero? k = (f * base) r, f = split_float(k) #c.include?(f) ? break : c << f break if p == 0; p -= 1 b << r end a << 0 if a.empty? b << 0 if b.empty? [a.reverse, b] end ## # Convert array of values of a different base to decimal as called by # parse_array. # # @param [Array<Numeric, String>] digits Representation of Base values. # @param [Fixnum, Array<String>] base The base to convert from. # # @return [Float] The digits of base converted to decimal. def decimal(digits, base) i, f = split_digits(digits) e = i.size - 1 v = 0 (i + f).each do |n| v += n * base**e e -= 1 end v end ## # Returns the I-Part and F-Part of the passed value as arrays of fixnums. # # @param [Array<Numeric, String>] value # The array of decimal values per column of @base. # # @return [Array<(Array<Fixnum>, Array<Fixnum>)>] def split_digits(value) if d = value.index(DOT) || value.index('.') i, f = value[0...d], value[d+1..-1] else i, f = value, [0] end i.map!{ |x| x.to_i } f.map!{ |x| x.to_i } return i, f end ## # Returns an array of Integer and Float portions of the Radix::Float # # @param [Radix::Float] float Float value to split # # @return [Array<(Integer, Float)>] def split_float(float) i, f = float.to_s.split('.') return i.to_i, ('0.'+f).to_f end end |
#value ⇒ Float (readonly)
Internal floating point value.
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/radix/float.rb', line 18 class Float < Numeric ## # Internal floating point value. # # @return [Float] Float's decimal value. attr :value ## # Base of the number. # # @return [Fixnum] The base level of Float instance. attr :base ## # Base encoding table. # # @return [Array<String>, nil] Substitution chars or nil if default. attr :code private ## # Starts a new instance of the Radix::Float 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] 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 float # 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 [Float] Float value of Integer. def parse_value(value, base) case value when Float, Integer # Radix parse_numeric(value.to_f, base) when ::Array parse_array(value, base) when ::String parse_string(value, base) when ::Numeric parse_numeric(value.to_f, base) end end public ## # Convert the Radix::Float a Ruby Integer. # # @return [Integer] Base(10) value as Integer. def to_i to_f.to_i end alias_method :to_int, :to_i ## # Convert Radix::Float to a Ruby float. # # @return [Float] Base(10) value as Float. def to_f value.to_f end ## # Makes this Radix::Float 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. def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end ## # Creates an encoded string in passed base, with passed 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. 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<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. def digits i, f = base_conversion(value, base) if negative? ['-'] + i + [DOT] + f else i + [DOT] + f end end ## # Returns digits, or coded version of digits if @code. # # @return [Array<String, Fixnum>] # Values per column of @base as array. Prepended with "-" if negative. # Or encoded version if @code is defined. def digits_encoded base_encode(digits) end ## # Returns true if the number is negative? # # @return [Boolean] True if float value is < 0. def negative? value < 0 end ## # Creates a new Radix::Float of same value in different base. # # @return [Radix::Float] New float of same value in different base. def convert(new_base) self.class.new(value, new_base) end ## # Power exponentional operation. # # @param [#to_f] other # The exponent by which to raise Float. # # @return [Radix::Float] Result of exponential operation. def **(other) operation(:**, other) end ## # Modulo binary operation. # # @param [#to_f] other # # @return [Radix::Float] Modulo result of division operation. def %(other) operation(:%, other) end alias_method :modulo, :% ## # Returns the absolute value of self in @base. # # @return [Radix::Float] Absolute of @value. def abs self.class.new(value.abs, base) end ## # Returns the largest integer greater than or equal to self as a # Radix::Float. # # @return [Radix::Float] def ceil self.class.new(value.ceil, base) end ## # Returns the smallest integer less than or equal to self as a # Radix::Float. # # @return [Radix::Float] def floor self.class.new(value.floor, base) end ## # Returns a new Radix::Float instance of same base, rounded to the nearest # whole integer. # # @example Rounding Radix Float # > round_test = Radix::Float.new(123.03, 16) # 7 11 . 0 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.03 # > round_test.round # 7 11 . 0 (16) # > round_test.round.value # 123.0 # > round_test += 0.5 # 7 11 . 8 7 10 14 1 4 7 10 14 1 (16) # > round_test.value # 123.53 # > round_test.round # 7 12 . 0 (16) # > round_test.round.value # 124.0 # # @return [Radix::Float] New Instance def round return self.class.new((value + 0.5).floor, base) if self > 0.0 return self.class.new((value - 0.5).ceil, base) if self < 0.0 return self.class.new(0, 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. # # @param [Numeric] other # Any Numeric instance. # # @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::Float.new(123.00,10) # 1 2 3 . 0 (10) # > higher = Radix::Float.new(456.00,16) # 1 12 8 . 0 (16) # > lower <=> higher # -1 # > lower <=> 123 # 0 # > lower <=> "123" # 0 # > higher <=> lower # 1 # # @return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than. def <=>(other) to_f <=> other.to_f end # #def infinite? # digits[0,2] == [0, DOT] #end # #def finite? # !infinite #end # #def nan? # digits[-2,2] == [DOT, 0] #end ## # Create a new Radix::Float from value in Base-10. # # @param [Numeric, Array, String] other # The value of the new integer in base-10. # # @return [Array<Radix::Float>] An array of the new Float object and self. def coerce(other) [Radix::Float.new(other), self] end private ## # Perform passed arithmetic operation. # # @param [#to_f] other # # @return [Radix::Float] Result of binary operation in @base. def operation(op, other) a = self.to_f b = other.to_f x = a.__send__(op, b) Radix::Float.new(x, base) end ## # Returns two arrays. The integer part and the fractional part of the Float # value in param base. # # @param [Float] value Float's decimal value. # @param [Fixnum] base The base level of Float instance. # @param [Fixnum] prec The # of places to extend F-part. # # @return [Array<(Array[Fixnum], Array[Fixnum])>] def base_conversion(value, base, prec=10) #if value < 0 # @negative, value = true, value.abs #end value = value.to_f.abs i, f = split_float(value) a = [] while i > 0 i, r = i.divmod(base) a << r end #c = [] # f-cache p = prec b = [] while !f.zero? k = (f * base) r, f = split_float(k) #c.include?(f) ? break : c << f break if p == 0; p -= 1 b << r end a << 0 if a.empty? b << 0 if b.empty? [a.reverse, b] end ## # Convert array of values of a different base to decimal as called by # parse_array. # # @param [Array<Numeric, String>] digits Representation of Base values. # @param [Fixnum, Array<String>] base The base to convert from. # # @return [Float] The digits of base converted to decimal. def decimal(digits, base) i, f = split_digits(digits) e = i.size - 1 v = 0 (i + f).each do |n| v += n * base**e e -= 1 end v end ## # Returns the I-Part and F-Part of the passed value as arrays of fixnums. # # @param [Array<Numeric, String>] value # The array of decimal values per column of @base. # # @return [Array<(Array<Fixnum>, Array<Fixnum>)>] def split_digits(value) if d = value.index(DOT) || value.index('.') i, f = value[0...d], value[d+1..-1] else i, f = value, [0] end i.map!{ |x| x.to_i } f.map!{ |x| x.to_i } return i, f end ## # Returns an array of Integer and Float portions of the Radix::Float # # @param [Radix::Float] float Float value to split # # @return [Array<(Integer, Float)>] def split_float(float) i, f = float.to_s.split('.') return i.to_i, ('0.'+f).to_f end end |
Instance Method Details
#%(other) ⇒ Radix::Float Also known as: modulo
Modulo binary operation.
210 211 212 |
# File 'lib/radix/float.rb', line 210 def %(other) operation(:%, other) end |
#**(other) ⇒ Radix::Float
Power exponentional operation.
200 201 202 |
# File 'lib/radix/float.rb', line 200 def **(other) operation(:**, other) end |
#<=>(other) ⇒ Fixnum
Comparitive binary operation. Very useful for sorting methods.
319 320 321 |
# File 'lib/radix/float.rb', line 319 def <=>(other) to_f <=> other.to_f end |
#==(other) ⇒ Boolean
Simple equality requires equal values only.
289 290 291 292 293 294 295 296 |
# File 'lib/radix/float.rb', line 289 def ==(other) case other when Float, Integer # Radix value == other.value else value == other end end |
#abs ⇒ Radix::Float
Returns the absolute value of self in @base.
220 221 222 |
# File 'lib/radix/float.rb', line 220 def abs self.class.new(value.abs, base) end |
#base_conversion(value, base, prec = 10) ⇒ Array<(Array[Fixnum], Array[Fixnum])> (private)
Returns two arrays. The integer part and the fractional part of the Float value in param base.
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 |
# File 'lib/radix/float.rb', line 374 def base_conversion(value, base, prec=10) #if value < 0 # @negative, value = true, value.abs #end value = value.to_f.abs i, f = split_float(value) a = [] while i > 0 i, r = i.divmod(base) a << r end #c = [] # f-cache p = prec b = [] while !f.zero? k = (f * base) r, f = split_float(k) #c.include?(f) ? break : c << f break if p == 0; p -= 1 b << r end a << 0 if a.empty? b << 0 if b.empty? [a.reverse, b] end |
#ceil ⇒ Radix::Float
Returns the largest integer greater than or equal to self as a Radix::Float.
229 230 231 |
# File 'lib/radix/float.rb', line 229 def ceil self.class.new(value.ceil, base) end |
#coerce(other) ⇒ Array<Radix::Float>
Create a new Radix::Float from value in Base-10.
345 346 347 |
# File 'lib/radix/float.rb', line 345 def coerce(other) [Radix::Float.new(other), self] end |
#convert(new_base) ⇒ Radix::Float
Creates a new Radix::Float of same value in different base.
189 190 191 |
# File 'lib/radix/float.rb', line 189 def convert(new_base) self.class.new(value, new_base) end |
#decimal(digits, base) ⇒ Float (private)
Convert array of values of a different base to decimal as called by parse_array.
413 414 415 416 417 418 419 420 421 422 |
# File 'lib/radix/float.rb', line 413 def decimal(digits, base) i, f = split_digits(digits) e = i.size - 1 v = 0 (i + f).each do |n| v += n * base**e e -= 1 end v end |
#digits ⇒ Array<String, Fixnum>
Returns an array representation of each column’s value in decimal chars.
158 159 160 161 162 163 164 165 |
# File 'lib/radix/float.rb', line 158 def digits i, f = base_conversion(value, base) if negative? ['-'] + i + [DOT] + f else i + [DOT] + f end end |
#digits_encoded ⇒ Array<String, Fixnum>
Returns digits, or coded version of digits if @code.
173 174 175 |
# File 'lib/radix/float.rb', line 173 def digits_encoded base_encode(digits) end |
#eql?(num) ⇒ Boolean
Strict equality requires same class as well as value.
278 279 280 |
# File 'lib/radix/float.rb', line 278 def eql?(num) self.class.equal?(num.class) && self == num end |
#floor ⇒ Radix::Float
Returns the smallest integer less than or equal to self as a Radix::Float.
238 239 240 |
# File 'lib/radix/float.rb', line 238 def floor self.class.new(value.floor, base) end |
#inspect ⇒ String
Creates a string representation of self.
149 150 151 |
# File 'lib/radix/float.rb', line 149 def inspect "#{digits.join(' ')} (#{base})" end |
#negative? ⇒ Boolean
Returns true if the number is negative?
181 182 183 |
# File 'lib/radix/float.rb', line 181 def negative? value < 0 end |
#operation(op, other) ⇒ Radix::Float (private)
Perform passed arithmetic operation.
357 358 359 360 361 362 363 |
# File 'lib/radix/float.rb', line 357 def operation(op, other) a = self.to_f b = other.to_f x = a.__send__(op, b) Radix::Float.new(x, base) end |
#parse_value(value, base) ⇒ Float (private)
Takes a Radix::Numeric, String or array and returns the decimal float value for storage in @value.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/radix/float.rb', line 67 def parse_value(value, base) case value when Float, Integer # Radix parse_numeric(value.to_f, base) when ::Array parse_array(value, base) when ::String parse_string(value, base) when ::Numeric parse_numeric(value.to_f, base) end end |
#round ⇒ Radix::Float
Returns a new Radix::Float instance of same base, rounded to the nearest whole integer.
265 266 267 268 269 |
# File 'lib/radix/float.rb', line 265 def round return self.class.new((value + 0.5).floor, base) if self > 0.0 return self.class.new((value - 0.5).ceil, base) if self < 0.0 return self.class.new(0, base) end |
#split_digits(value) ⇒ Array<(Array<Fixnum>, Array<Fixnum>)> (private)
Returns the I-Part and F-Part of the passed value as arrays of fixnums.
431 432 433 434 435 436 437 438 439 440 |
# File 'lib/radix/float.rb', line 431 def split_digits(value) if d = value.index(DOT) || value.index('.') i, f = value[0...d], value[d+1..-1] else i, f = value, [0] end i.map!{ |x| x.to_i } f.map!{ |x| x.to_i } return i, f end |
#split_float(float) ⇒ Array<(Integer, Float)> (private)
Returns an array of Integer and Float portions of the Radix::Float
448 449 450 451 |
# File 'lib/radix/float.rb', line 448 def split_float(float) i, f = float.to_s.split('.') return i.to_i, ('0.'+f).to_f end |
#to_a(base = nil) ⇒ Array<Fixnum, String>
Makes this Radix::Float an array using code if defined. Returns an array using default chars otherwise.
108 109 110 111 112 113 114 |
# File 'lib/radix/float.rb', line 108 def to_a(base=nil) if base convert(base).digits_encoded else digits_encoded end end |
#to_f ⇒ Float
Convert Radix::Float to a Ruby float.
96 97 98 |
# File 'lib/radix/float.rb', line 96 def to_f value.to_f end |
#to_i ⇒ Integer Also known as: to_int
Convert the Radix::Float a Ruby Integer.
86 87 88 |
# File 'lib/radix/float.rb', line 86 def to_i to_f.to_i end |
#to_s(base = nil, divider = nil) ⇒ String
For base 10 or less does not use a divider unless specified.
Creates an encoded string in passed base, with passed digit divider.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/radix/float.rb', line 128 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 |