Class: Bignum

Inherits:
Integer show all

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)

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

#, #, #>, #>=, #between?

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.

Overloads:

- (Integer) &(numeric)

Performs bitwise and between big and numeric.

Returns:

- (Numeric) *(other)

Multiplies big and other, returning the result.

Returns:

- (Numeric) **(exponent)

Raises big to the exponent power (which may be an integer, float, or anything that will coerce to a number). The result may be a Fixnum, Bignum, or Float

123456789 ** 2      #=> 15241578750190521
123456789 ** 1.2    #=> 5126464716.09932
123456789 ** -2     #=> 6.5610001194102e-17

Returns:

- (Numeric) +(other)

Adds big and other, returning the result.

Returns:

- (Numeric) -(other)

Subtracts other from big, returning the result.

Returns:

- (Integer) -

Unary minus (returns an integer whose value is 0-big)

Returns:

- (Numeric) /(other)

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

Returns:

- (Integer) <<(numeric)

Shifts big left numeric positions (right if numeric is negative).

Returns:

- (-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.

Returns:

  • (-1, 0, +1, nil)

- (Boolean) ==(obj)

Returns true only if obj has the same value as big. Contrast this with Bignum#eql?, which requires obj to be a Bignum.

68719476736 == 68719476736.0   #=> true

Returns:

  • (Boolean)

- (Integer) >>(numeric)

Shifts big right numeric positions (left if numeric is negative).

Returns:

- (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

Returns:

  • (0, 1)

- (Integer) ^(numeric)

Performs bitwise exclusive or between big and numeric.

Returns:

- (Bignum) abs

Returns the absolute value of big.

-1234567890987654321.abs   #=> 1234567890987654321

Returns:

- (Object) coerce

MISSING: documentation

- (Integer) div(other)

Performs integer division: returns integer value.

Returns:

- (Array) divmod(numeric)

See Numeric#divmod.

Returns:

- (Boolean) eql?(obj)

Returns true only if obj is a Bignum with the same value as big. Contrast this with Bignum#==, which performs type conversions.

68719476736.eql?(68719476736.0)   #=> false

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Boolean) even?

Returns true if big is an even number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Float) fdiv(numeric)

Returns the floating point result of dividing big by numeric.

-1234567890987654321.fdiv(13731)      #=> -89910996357705.5
-1234567890987654321.fdiv(13731.24)   #=> -89909424858035.7

Returns:

- (Fixnum) hash

Compute a hash based on the value of big.

Returns:

- (Numeric) %(other) - (Numeric) modulo(other)

Returns big modulo other. See Numeric.divmod for more information.

Overloads:

- (Boolean) odd?

Returns true if big is an odd number.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Numeric) remainder(numeric)

Returns the remainder after dividing big by numeric.

-1234567890987654321.remainder(13731)      #=> -6966
-1234567890987654321.remainder(13731.24)   #=> -9906.22531493148

Returns:

- (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

Returns:

- (Float) to_f

Converts big to a Float. If big doesn't fit in a Float, the result is infinity.

Returns:

- (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"

Returns:

- (Integer) |(numeric)

Performs bitwise or between big and numeric.

Returns:

- (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"

Returns: