Programmering i Ruby

Den Pragmatiske Programmerers Veiledning

Forrige < Innhold ^
Neste >
klassen Bignum
Forelder: Integer
Versjon: 1.6

Indeks:

Arithmetic operations Bit operations <=> [ ] size to_f to_i to_s


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.

instansmetoder
Arithmetic operations

Performs various arithmetic operations on big.

big + aNumeric Addition
big -- aNumeric Subtraction
big * aNumeric Multiplication
big / aNumeric Division
big % aNumeric Modulo
big ** aNumeric Exponentiation

Bit operations

Performs various operations on the binary representations of the Bignum

~ big Invert bits
big | aNumeric Bitwise OR
big & aNumeric Bitwise AND
big ^ aNumeric Bitwise EXCLUSIVE OR
big << aNumeric Left-shift aNumeric bits
big >> aNumeric Right-shift aNumeric bits (with sign extension)

<=> big <=> aNumeric -> -1, 0, +1

Comparison---Returns -1, 0, or +1 depending on whether big is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.

[ ] big[ n ] -> 0, 1

Bit Reference---Returns the nth bit in the (assumed) binary representation of big, where big[0] is the least significant bit.

a = 9**15
50.downto(0) do |n|
  print a[n]
end
produces:
000101110110100000111000011110010100111100010111001

size big.size -> anInteger

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

to_f big.to_f -> aFloat

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

to_i big.to_i -> big

Returns big.

to_s big.to_s -> aString

Returns a string containing the decimal representation of big.


Forrige < Innhold ^
Neste >

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide".
Translation to norwegian by Norway Ruby User Group.
Copyright for the english original authored by David Thomas and Andrew Hunt:
Copyright © 2001 Addison Wesley Longman, Inc.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).

(Please note that the license for the original has changed from the above. The above is the license of the original version that was used as a foundation for the translation efforts.)

Copyright for the norwegian translation:
Copyright © 2002 Norway Ruby User Group.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.