Class: Bignum
Overview
Bignum objects hold integers outside the range of Fixnum. Bignum objects are created automatically when integer calculations would otherwise overflow a Fixnum. When a calculation involving Bignum objects returns a result that will fit in a Fixnum, the result is automatically converted.
For the purposes of the bitwise operations and [], a Bignum is treated as if it were an infinite-length bitstring with 2's complement representation.
While Fixnum values are immediate, Bignum objects are not---assignment and parameter passing work with references to objects, not the objects themselves.
Instance Method Summary (collapse)
-
- %
Returns big modulo other.
-
- &
Performs bitwise and between big and numeric.
-
- *
Multiplies big and other, returning the result.
-
- **
Raises big to the exponent power (which may be an integer, float, or anything that will coerce to a number).
-
- +
Adds big and other, returning the result.
-
- -
Subtracts other from big, returning the result.
-
- -
Unary minus (returns an integer whose value is 0-big).
-
- /
Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.
-
- <<
Shifts big left numeric positions (right if numeric is negative).
-
- <=>
Comparison---Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than numeric.
-
- ==
Returns true only if obj has the same value as big.
-
- >>
Shifts big right numeric positions (left if numeric is negative).
-
- []
Bit Reference---Returns the nth bit in the (assumed) binary representation of big, where big is the least significant bit.
-
- ^
Performs bitwise exclusive or between big and numeric.
-
- abs
Returns the absolute value of big.
-
- coerce
MISSING: documentation.
-
- div
Performs integer division: returns integer value.
-
- divmod
See Numeric#divmod.
-
- eql?
Returns true only if obj is a Bignum with the same value as big.
-
- even?
Returns true if big is an even number.
-
- fdiv
Returns the floating point result of dividing big by numeric.
-
- hash
Compute a hash based on the value of big.
-
- modulo
Returns big modulo other.
-
- odd?
Returns true if big is an odd number.
-
- remainder
Returns the remainder after dividing big by numeric.
-
- size
Returns the number of bytes in the machine representation of big.
-
- to_f
Converts big to a Float.
-
- to_s
Returns a string containing the representation of big radix base (2 through 36).
-
- |
Performs bitwise or between big and numeric.
-
- ~
Inverts the bits in big.
Methods inherited from Integer
#ceil, #chr, #denominator, #downto, #floor, #gcd, #gcdlcm, induced_from, #integer?, #lcm, #next, #numerator, #ord, #pred, #rationalize, #round, #succ, #times, #to_i, #to_int, #to_r, #truncate, #upto
Methods included from Precision
included, #prec, #prec_f, #prec_i
Methods inherited from Numeric
#+@, #abs2, #angle, #arg, #ceil, #class, #conj, #conjugate, #denominator, #dup, #floor, #i, #imag, #imaginary, #initialize_copy, #integer?, #magnitude, #nonzero?, #numerator, #phase, #polar, #quo, #real, #real?, #rect, #rectangular, #round, #scalar?, #singleton_method_added, #step, #to_c, #to_int, #truncate, #zero?
Methods included from Comparable
Methods inherited from NSNumber
#boolValue, #charValue, #compare:, #decimalValue, #descriptionWithLocale:, #doubleValue, #floatValue, #initWithBool:, #initWithChar:, #initWithDouble:, #initWithFloat:, #initWithInt:, #initWithInteger:, #initWithLong:, #initWithLongLong:, #initWithShort:, #initWithUnsignedChar:, #initWithUnsignedInt:, #initWithUnsignedInteger:, #initWithUnsignedLong:, #initWithUnsignedLongLong:, #initWithUnsignedShort:, #intValue, #integerValue, #isEqualToNumber:, #longLongValue, #longValue, numberWithBool:, numberWithChar:, numberWithDouble:, numberWithFloat:, numberWithInt:, numberWithInteger:, numberWithLong:, numberWithLongLong:, numberWithShort:, numberWithUnsignedChar:, numberWithUnsignedInt:, numberWithUnsignedInteger:, numberWithUnsignedLong:, numberWithUnsignedLongLong:, numberWithUnsignedShort:, #objCType, #shortValue, #stringValue, #unsignedCharValue, #unsignedIntValue, #unsignedIntegerValue, #unsignedLongLongValue, #unsignedLongValue, #unsignedShortValue
Methods inherited from NSValue
#getValue:, #initWithBytes:objCType:, #isEqualToValue:, #nonretainedObjectValue, #objCType, #pointerValue, #rangeValue, value:withObjCType:, valueWithBytes:objCType:, valueWithNonretainedObject:, valueWithPointer:, valueWithRange:
Methods inherited from NSObject
#!, #!=, #!~, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Method Details
- (Numeric) %(other) - (Numeric) modulo(other)
Returns big modulo other. See Numeric.divmod for more information.
- (Integer) &(numeric)
Performs bitwise and between big and numeric.
- (Numeric) *(other)
Multiplies big and other, returning the result.
- (Numeric) **(exponent)
- (Numeric) +(other)
Adds big and other, returning the result.
- (Numeric) -(other)
Subtracts other from big, returning the result.
- (Integer) -
Unary minus (returns an integer whose value is 0-big)
- (Numeric) /(other)
Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.
- (Integer) <<(numeric)
Shifts big left numeric positions (right if numeric is negative).
- (-1, ...) <=>(numeric)
Comparison---Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.
- (Boolean) ==(obj)
- (Integer) >>(numeric)
Shifts big right numeric positions (left if numeric is negative).
- (0, 1) [](n)
Bit Reference---Returns the nth bit in the (assumed) binary representation of big, where big is the least significant bit.
a = 9**15
50.downto(0) do |n|
print a[n]
end
produces:
000101110110100000111000011110010100111100010111001
- (Integer) ^(numeric)
Performs bitwise exclusive or between big and numeric.
- (Bignum) abs
Returns the absolute value of big.
-1234567890987654321.abs #=> 1234567890987654321
- (Object) coerce
MISSING: documentation
- (Integer) div(other)
Performs integer division: returns integer value.
- (Boolean) eql?(obj)
- (Boolean) even?
Returns true if big is an even number.
- (Float) fdiv(numeric)
Returns the floating point result of dividing big by numeric.
-1234567890987654321.fdiv(13731) #=> -89910996357705.5
-1234567890987654321.fdiv(13731.24) #=> -89909424858035.7
- (Fixnum) hash
Compute a hash based on the value of big.
- (Numeric) %(other) - (Numeric) modulo(other)
Returns big modulo other. See Numeric.divmod for more information.
- (Boolean) odd?
Returns true if big is an odd number.
- (Numeric) remainder(numeric)
Returns the remainder after dividing big by numeric.
-1234567890987654321.remainder(13731) #=> -6966
-1234567890987654321.remainder(13731.24) #=> -9906.22531493148
- (Integer) size
Returns the number of bytes in the machine representation of big.
(256**10 - 1).size #=> 12
(256**20 - 1).size #=> 20
(256**40 - 1).size #=> 40
- (String) to_s(base = 10)
Returns a string containing the representation of big radix base (2 through 36).
12345654321.to_s #=> "12345654321"
12345654321.to_s(2) #=> "1011011111110110111011110000110001"
12345654321.to_s(8) #=> "133766736061"
12345654321.to_s(16) #=> "2dfdbbc31"
78546939656932.to_s(36) #=> "rubyrules"
- (Integer) |(numeric)
Performs bitwise or between big and numeric.
- (Integer) ~
Inverts the bits in big. As Bignums are conceptually infinite length, the result acts as if it had an infinite number of one bits to the left. In hex representations, this is displayed as two periods to the left of the digits.
sprintf("%X", ~0x1122334455) #=> "..FEEDDCCBBAA"