Class: Fixnum
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: | Integer |
Defined in: | numeric.c, numeric.c |
Overview
Holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum
exceeds this range, the value is automatically converted to a ::Bignum.
Fixnum
objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object.
Assignment does not alias Fixnum
objects. There is effectively only one Fixnum
object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum
. Any attempt to add a singleton method to a Fixnum
object will raise a ::TypeError.
Instance Attribute Summary
-
#even? ⇒ Boolean
readonly
Returns
true
iffix
is an even number. -
#odd? ⇒ Boolean
readonly
Returns
true
iffix
is an odd number. -
#zero? ⇒ Boolean
readonly
Returns
true
iffix
is zero.
::Integer - Inherited
#even? | Returns |
#integer? | Since |
#odd? | Returns |
::Numeric - Inherited
Instance Method Summary
-
#%(other) ⇒ Numeric
(also: #modulo)
Returns
fix
moduloother
. -
#&(integer) ⇒ integer_result
Bitwise AND.
-
#*(numeric) ⇒ numeric_result
Performs multiplication: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#**(numeric) ⇒ numeric_result
Raises
fix
to the power ofnumeric
, which may be negative or fractional. -
#+(numeric) ⇒ numeric_result
Performs addition: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#-(numeric) ⇒ numeric_result
Performs subtraction: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#- ⇒ Integer
Negates
fix
, which may return a ::Bignum. -
#/(numeric) ⇒ numeric_result
Performs division: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#<(real) ⇒ Boolean
Returns
true
if the value offix
is less than that ofreal
. -
#<<(count) ⇒ Integer
Shifts
fix
leftcount
positions, or right ifcount
is negative. -
#<=(real) ⇒ Boolean
Returns
true
if the value offix
is less than or equal to that ofreal
. -
#<=>(numeric) ⇒ 1, ...
Comparison—Returns
-1
,0
, +1
ornil
depending on whetherfix
is less than, equal to, or greater thannumeric
. -
#==(other) ⇒ Boolean
(also: #===)
Return
true
iffix
equalsother
numerically. -
#===(other) ⇒ Boolean
Alias for #==.
-
#>(real) ⇒ Boolean
Returns
true
if the value offix
is greater than that ofreal
. -
#>=(real) ⇒ Boolean
Returns
true
if the value offix
is greater than or equal to that ofreal
. -
#>>(count) ⇒ Integer
Shifts
fix
rightcount
positions, or left ifcount
is negative. -
#[](n) ⇒ 0, 1
Bit Reference—Returns the
n
th bit in the binary representation offix
, wherefix[0]
is the least significant bit. -
#^(integer) ⇒ integer_result
Bitwise EXCLUSIVE OR.
-
#abs ⇒ Integer
(also: #magnitude)
Returns the absolute value of
fix
. -
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
-
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing
fix
bynumeric
. -
#divmod(numeric) ⇒ Array
See Numeric#divmod.
-
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing
fix
bynumeric
. -
#inspect(base = 10) ⇒ String
Alias for #to_s.
-
#magnitude ⇒ Integer
Alias for #abs.
-
#modulo(other) ⇒ Numeric
Alias for #%.
-
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of
fix
. -
#succ ⇒ Integer
Returns the ::Integer equal to
int
+ 1. -
#to_f ⇒ Float
Converts
fix
to a ::Float. -
#to_s(base = 10) ⇒ String
(also: #inspect)
Returns a string containing the representation of
fix
radixbase
(between 2 and 36). -
#|(integer) ⇒ integer_result
Bitwise OR.
-
#~ ⇒ Integer
One's complement: returns a number where each bit is flipped.
::Integer - Inherited
#ceil | Alias for Integer#to_i. |
#chr | Returns a string containing the character represented by the |
#denominator | Returns 1. |
#downto | Iterates the given block, passing decreasing values from |
#floor | Alias for Integer#to_i. |
#gcd | Returns the greatest common divisor (always positive). |
#gcdlcm | Returns an array; [int.gcd(int2), int.lcm(int2)]. |
#lcm | Returns the least common multiple (always positive). |
#next | Returns the ::Integer equal to |
#numerator | Returns self. |
#ord | Returns the |
#pred | Returns the ::Integer equal to |
#rationalize | Returns the value as a rational. |
#round | Rounds |
#succ | Alias for Integer#next. |
#times | Iterates the given block |
#to_i | As |
#to_int | Alias for Integer#to_i. |
#to_r | Returns the value as a rational. |
#truncate | Alias for Integer#to_i. |
#upto | Iterates the given block, passing in integer values from |
::Numeric - Inherited
#% | x.modulo(y) means x-y*(x/y).floor. |
#+@ | Unary Plus—Returns the receiver's value. |
#-@ | Unary Minus—Returns the receiver's value, negated. |
#<=> | Returns zero if |
#abs | Returns the absolute value of |
#abs2 | Returns square of self. |
#angle | Alias for Numeric#arg. |
#arg | Returns 0 if the value is positive, pi otherwise. |
#ceil | Returns the smallest possible ::Integer that is greater than or equal to |
#coerce | If a +numeric is the same type as |
#conj | Returns self. |
#conjugate | Alias for Numeric#conj. |
#denominator | Returns the denominator (always positive). |
#div | Uses #/ to perform division, then converts the result to an integer. |
#divmod | Returns an array containing the quotient and modulus obtained by dividing |
#eql? | Returns |
#fdiv | Returns float division. |
#floor | Returns the largest integer less than or equal to |
#i | Returns the corresponding imaginary number. |
#imag | Returns zero. |
#imaginary | Alias for Numeric#imag. |
#initialize_copy | Numerics are immutable values, which should not be copied. |
#magnitude | Alias for Numeric#abs. |
#modulo | Alias for Numeric#%. |
#numerator | Returns the numerator. |
#phase | Alias for Numeric#arg. |
#polar | Returns an array; [num.abs, num.arg]. |
#quo | Returns most exact division (rational for integers, float for floats). |
#rect | Returns an array; [num, 0]. |
#rectangular | Alias for Numeric#rect. |
#remainder | x.remainder(y) means x-y*(x/y).truncate. |
#round | Rounds |
#singleton_method_added | Trap attempts to add methods to ::Numeric objects. |
#step | Invokes the given block with the sequence of numbers starting at |
#to_c | Returns the value as a complex. |
#to_int | Invokes the child class's |
#truncate | Returns |
::Comparable - Included
#< | Compares two objects based on the receiver's #<=> method, returning true if it returns -1. |
#<= | Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0. |
#== | Compares two objects based on the receiver's #<=> method, returning true if it returns 0. |
#> | Compares two objects based on the receiver's #<=> method, returning true if it returns 1. |
#>= | Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1. |
#between? |
Instance Attribute Details
#even? ⇒ Boolean
(readonly)
Returns true
if fix
is an even number.
#odd? ⇒ Boolean
(readonly)
Returns true
if fix
is an odd number.
#zero? ⇒ Boolean
(readonly)
Returns true
if fix
is zero.
Instance Method Details
Also known as: #modulo
Returns fix
modulo other
.
See Numeric#divmod for more information.
#&(integer) ⇒ integer_result
Bitwise AND.
#*(numeric) ⇒ numeric_result
Performs multiplication: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a ::Bignum.
#**(numeric) ⇒ numeric_result
Raises fix
to the power of numeric
, which may be negative or fractional.
2 ** 3 #=> 8
2 ** -1 #=> (1/2)
2 ** 0.5 #=> 1.4142135623731
#+(numeric) ⇒ numeric_result
Performs addition: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a ::Bignum.
#-(numeric) ⇒ numeric_result
Performs subtraction: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a ::Bignum.
#- ⇒ Integer
Negates fix
, which may return a ::Bignum.
#/(numeric) ⇒ numeric_result
Performs division: the class of the resulting object depends on the class of numeric
and on the magnitude of the result. It may return a ::Bignum.
#<(real) ⇒ Boolean
Returns true
if the value of fix
is less than that of real
.
#<<(count) ⇒ Integer
Shifts fix
left count
positions, or right if count
is negative.
#<=(real) ⇒ Boolean
Returns true
if the value of fix
is less than or equal to that of real
.
#<=>(numeric) ⇒ 1
, ...
Comparison—Returns -1
, 0
, +1
or nil
depending on whether fix
is less than, equal to, or greater than numeric
.
This is the basis for the tests in the ::Comparable module.
nil
is returned if the two values are incomparable.
#==(other) ⇒ Boolean
Also known as: #===
Return true
if fix
equals other
numerically.
1 == 2 #=> false
1 == 1.0 #=> true
#==(other) ⇒ Boolean
#===(other) ⇒ Boolean
Boolean
#===(other) ⇒ Boolean
Alias for #==.
#>(real) ⇒ Boolean
Returns true
if the value of fix
is greater than that of real
.
#>=(real) ⇒ Boolean
Returns true
if the value of fix
is greater than or equal to that of real
.
#>>(count) ⇒ Integer
Shifts fix
right count
positions, or left if count
is negative.
#[](n) ⇒ 0
, 1
Bit Reference—Returns the n
th bit in the binary representation of fix
, where fix[0]
is the least significant bit.
For example:
a = 0b11001100101010
30.downto(0) do |n| print a[n] end
#=> 0000000000000000011001100101010
#^(integer) ⇒ integer_result
Bitwise EXCLUSIVE OR.
Also known as: #magnitude
Returns the absolute value of fix
.
-12345.abs #=> 12345
12345.abs #=> 12345
#bit_length ⇒ Integer
Returns the number of bits of the value of int.
“the number of bits” means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.
I.e
. This method returns ceil(log2(int < 0 ? -int : int+1)).
(-2**12-1).bit_length #=> 13
(-2**12).bit_length #=> 12
(-2**12+1).bit_length #=> 12
-0x101.bit_length #=> 9
-0x100.bit_length #=> 8
-0xff.bit_length #=> 8
-2.bit_length #=> 1
-1.bit_length #=> 0
0.bit_length #=> 0
1.bit_length #=> 1
0xff.bit_length #=> 8
0x100.bit_length #=> 9
(2**12-1).bit_length #=> 12
(2**12).bit_length #=> 13
(2**12+1).bit_length #=> 13
This method can be used to detect overflow in Array#pack as follows.
if n.bit_length < 32
[n].pack("l") # no overflow
else
raise "overflow"
end
#div(numeric) ⇒ Integer
Performs integer division: returns integer result of dividing fix
by numeric
.
#divmod(numeric) ⇒ Array
See Numeric#divmod.
#fdiv(numeric) ⇒ Float
Returns the floating point result of dividing fix
by numeric
.
654321.fdiv(13731) #=> 47.6528293642124
654321.fdiv(13731.24) #=> 47.6519964693647
Alias for #to_s.
Alias for #abs.
Alias for #%.
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of fix
.
1.size #=> 4
-1.size #=> 4
2147483647.size #=> 4
Returns the ::Integer equal to int
+ 1.
1.next #=> 2
(-1).next #=> 0
#to_f ⇒ Float
Converts fix
to a ::Float.
#to_s(base = 10) ⇒ String Also known as: #inspect
Returns a string containing the representation of fix
radix base
(between 2 and 36).
12345.to_s #=> "12345"
12345.to_s(2) #=> "11000000111001"
12345.to_s(8) #=> "30071"
12345.to_s(10) #=> "12345"
12345.to_s(16) #=> "3039"
12345.to_s(36) #=> "9ix"
#|(integer) ⇒ integer_result
Bitwise OR.
#~ ⇒ Integer
One's complement: returns a number where each bit is flipped.