Class: Fixnum
- Includes:
- Precision
- Defined in:
- numeric.c,
numeric.c
Overview
******************************************************************
A <code>Fixnum</code> holds <code>Integer</code> values that can be
represented in a native machine word (minus 1 bit). If any operation
on a <code>Fixnum</code> exceeds this range, the value is
automatically converted to a <code>Bignum</code>.
<code>Fixnum</code> 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 <code>Fixnum</code> objects. There is effectively only one
<code>Fixnum</code> object instance for any given integer value, so,
for example, you cannot add a singleton method to a
<code>Fixnum</code>.
Class Method Summary collapse
-
.induced_from(obj) ⇒ Fixnum
Convert
obj
to a Fixnum.
Instance Method Summary collapse
-
#% ⇒ Object
Returns
fix
moduloother
. -
#&(other) ⇒ Integer
Bitwise AND.
-
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#**(other) ⇒ Numeric
Raises
fix
to theother
power, which may be negative or fractional. -
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#-(numeric) ⇒ Object
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 might return a Bignum). -
#/ ⇒ Object
Performs division: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#<(other) ⇒ Boolean
Returns
true
if the value offix
is less than that ofother
. -
#<<(count) ⇒ Integer
Shifts fix left count positions (right if count is negative).
-
#<=(other) ⇒ Boolean
Returns
true
if the value offix
is less thanor equal to that ofother
. -
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than numeric.
-
#==(other) ⇒ Object
Return
true
iffix
equalsother
numerically. -
#>(other) ⇒ Boolean
Returns
true
if the value offix
is greater than that ofother
. -
#>=(other) ⇒ Boolean
Returns
true
if the value offix
is greater than or equal to that ofother
. -
#>>(count) ⇒ Integer
Shifts fix right count positions (left if count is negative).
-
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit.
-
#^(other) ⇒ Integer
Bitwise EXCLUSIVE OR.
-
#abs ⇒ aFixnum
Returns the absolute value of fix.
-
#div ⇒ Object
Performs division: the class of the resulting object depends on the class of
numeric
and on the magnitude of the result. -
#divmod(numeric) ⇒ Array
See
Numeric#divmod
. -
#even? ⇒ Boolean
Returns
true
if fix is an even number. -
#fdiv ⇒ Object
Returns the floating point result of dividing fix by numeric.
-
#id2name ⇒ String?
Returns the name of the object whose symbol id is fix.
-
#modulo ⇒ Object
Returns
fix
moduloother
. -
#odd? ⇒ Boolean
Returns
true
if fix is an odd number. -
#quo ⇒ Object
Returns the floating point result of dividing fix by numeric.
-
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of a
Fixnum
. -
#to_f ⇒ Float
Converts fix to a
Float
. -
#to_s(base = 10) ⇒ aString
Returns a string containing the representation of fix radix base (between 2 and 36).
-
#to_sym ⇒ aSymbol
Returns the symbol whose integer value is fix.
-
#zero? ⇒ Boolean
Returns
true
if fix is zero. -
#|(other) ⇒ Integer
Bitwise OR.
-
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
Methods included from Precision
included, #prec, #prec_f, #prec_i
Methods inherited from Integer
#ceil, #chr, #downto, #floor, #integer?, #next, #ord, #pred, #round, #succ, #times, #to_i, #to_int, #truncate, #upto
Methods inherited from Numeric
#+@, #ceil, #coerce, #eql?, #floor, #initialize_copy, #integer?, #nonzero?, #remainder, #round, #singleton_method_added, #step, #to_int, #truncate
Methods included from Comparable
Class Method Details
.induced_from(obj) ⇒ Fixnum
Convert obj
to a Fixnum. Works with numeric parameters. Also works with Symbols, but this is deprecated.
1934 1935 1936 |
# File 'numeric.c', line 1934 static VALUE rb_fix_induced_from(klass, x) VALUE klass, x; |
Instance Method Details
#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric
Returns fix
modulo other
. See Numeric.divmod
for more information.
2246 2247 2248 |
# File 'numeric.c', line 2246 static VALUE fix_mod(x, y) VALUE x, y; |
#&(other) ⇒ Integer
Bitwise AND.
2551 2552 2553 |
# File 'numeric.c', line 2551 static VALUE fix_and(x, y) VALUE x, y; |
#*(numeric) ⇒ Object
Performs multiplication: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
2132 2133 2134 |
# File 'numeric.c', line 2132 static VALUE fix_mul(x, y) VALUE x, y; |
#**(other) ⇒ Numeric
Raises fix
to the other
power, which may be negative or fractional.
2 ** 3 #=> 8
2 ** -1 #=> 0.5
2 ** 0.5 #=> 1.4142135623731
2330 2331 2332 |
# File 'numeric.c', line 2330 static VALUE fix_pow(x, y) VALUE x, y; |
#+(numeric) ⇒ Object
Performs addition: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
2072 2073 2074 |
# File 'numeric.c', line 2072 static VALUE fix_plus(x, y) VALUE x, y; |
#-(numeric) ⇒ Object
Performs subtraction: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
2102 2103 2104 |
# File 'numeric.c', line 2102 static VALUE fix_minus(x, y) VALUE x, y; |
#- ⇒ Integer
Negates fix
(which might return a Bignum).
1994 1995 1996 |
# File 'numeric.c', line 1994 static VALUE fix_uminus(num) VALUE num; |
#/(numeric) ⇒ Object #div(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
2224 2225 2226 |
# File 'numeric.c', line 2224 static VALUE fix_div(x, y) VALUE x, y; |
#<(other) ⇒ Boolean
Returns true
if the value of fix
is less than that of other
.
2479 2480 2481 |
# File 'numeric.c', line 2479 static VALUE fix_lt(x, y) VALUE x, y; |
#<<(count) ⇒ Integer
Shifts fix left count positions (right if count is negative).
2614 2615 2616 |
# File 'numeric.c', line 2614 static VALUE rb_fix_lshift(x, y) VALUE x, y; |
#<=(other) ⇒ Boolean
Returns true
if the value of fix
is less thanor equal to that of other
.
2502 2503 2504 |
# File 'numeric.c', line 2502 static VALUE fix_le(x, y) VALUE x, y; |
#<=>(numeric) ⇒ -1, ...
Comparison—Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable
.
2409 2410 2411 |
# File 'numeric.c', line 2409 static VALUE fix_cmp(x, y) VALUE x, y; |
#==(other) ⇒ Object
Return true
if fix
equals other
numerically.
1 == 2 #=> false
1 == 1.0 #=> true
2391 2392 2393 |
# File 'numeric.c', line 2391 static VALUE fix_equal(x, y) VALUE x, y; |
#>(other) ⇒ Boolean
Returns true
if the value of fix
is greater than that of other
.
2433 2434 2435 |
# File 'numeric.c', line 2433 static VALUE fix_gt(x, y) VALUE x, y; |
#>=(other) ⇒ Boolean
Returns true
if the value of fix
is greater than or equal to that of other
.
2456 2457 2458 |
# File 'numeric.c', line 2456 static VALUE fix_ge(x, y) VALUE x, y; |
#>>(count) ⇒ Integer
Shifts fix right count positions (left if count is negative).
2649 2650 2651 |
# File 'numeric.c', line 2649 static VALUE rb_fix_rshift(x, y) VALUE x, y; |
#[](n) ⇒ 0, 1
Bit Reference—Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit.
a = 0b11001100101010
30.downto(0) do |n| print a[n] end
produces:
0000000000000000011001100101010
2692 2693 2694 |
# File 'numeric.c', line 2692 static VALUE fix_aref(fix, idx) VALUE fix, idx; |
#^(other) ⇒ Integer
Bitwise EXCLUSIVE OR.
2591 2592 2593 |
# File 'numeric.c', line 2591 static VALUE fix_xor(x, y) VALUE x, y; |
#abs ⇒ aFixnum
Returns the absolute value of fix.
-12345.abs #=> 12345
12345.abs #=> 12345
2749 2750 2751 |
# File 'numeric.c', line 2749 static VALUE fix_abs(fix) VALUE fix; |
#/(numeric) ⇒ Object #div(numeric) ⇒ Object
Performs division: the class of the resulting object depends on the class of numeric
and on the magnitude of the result.
2224 2225 2226 |
# File 'numeric.c', line 2224 static VALUE fix_div(x, y) VALUE x, y; |
#divmod(numeric) ⇒ Array
See Numeric#divmod
.
2265 2266 2267 |
# File 'numeric.c', line 2265 static VALUE fix_divmod(x, y) VALUE x, y; |
#even? ⇒ Boolean
Returns true
if fix is an even number.
2993 2994 2995 2996 2997 2998 2999 3000 |
# File 'numeric.c', line 2993
static VALUE
fix_even_p(VALUE num)
{
if (num & 2) {
return Qfalse;
}
return Qtrue;
}
|
#quo(numeric) ⇒ Float #fdiv(numeric) ⇒ Float
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124
654321.quo(13731.24) #=> 47.6519964693647
2204 2205 2206 |
# File 'numeric.c', line 2204 static VALUE fix_quo(x, y) VALUE x, y; |
#id2name ⇒ String?
Returns the name of the object whose symbol id is fix. If there is no symbol in the symbol table with this value, returns nil
. id2name
has nothing to do with the Object.id
method. See also Fixnum#to_sym
, String#intern
, and class Symbol
.
symbol = :@inst_var #=> :@inst_var
id = symbol.to_i #=> 9818
id.id2name #=> "@inst_var"
2775 2776 2777 |
# File 'numeric.c', line 2775 static VALUE fix_id2name(fix) VALUE fix; |
#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric
Returns fix
modulo other
. See Numeric.divmod
for more information.
2246 2247 2248 |
# File 'numeric.c', line 2246 static VALUE fix_mod(x, y) VALUE x, y; |
#odd? ⇒ Boolean
Returns true
if fix is an odd number.
2977 2978 2979 2980 2981 2982 2983 2984 |
# File 'numeric.c', line 2977
static VALUE
fix_odd_p(VALUE num)
{
if (num & 2) {
return Qtrue;
}
return Qfalse;
}
|
#quo(numeric) ⇒ Float #fdiv(numeric) ⇒ Float
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124
654321.quo(13731.24) #=> 47.6519964693647
2204 2205 2206 |
# File 'numeric.c', line 2204 static VALUE fix_quo(x, y) VALUE x, y; |
#size ⇒ Fixnum
Returns the number of bytes in the machine representation of a Fixnum
.
1.size #=> 4
-1.size #=> 4
2147483647.size #=> 4
2822 2823 2824 |
# File 'numeric.c', line 2822 static VALUE fix_size(fix) VALUE fix; |
#to_f ⇒ Float
Converts fix to a Float
.
2727 2728 2729 |
# File 'numeric.c', line 2727 static VALUE fix_to_f(num) VALUE num; |
#to_s(base = 10) ⇒ aString
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"
2047 2048 2049 |
# File 'numeric.c', line 2047 static VALUE fix_to_s(argc, argv, x) int argc; |
#to_sym ⇒ aSymbol
Returns the symbol whose integer value is fix. See also Fixnum#id2name
.
fred = :fred.to_i
fred.id2name #=> "fred"
fred.to_sym #=> :fred
2797 2798 2799 |
# File 'numeric.c', line 2797 static VALUE fix_to_sym(fix) VALUE fix; |
#zero? ⇒ Boolean
Returns true
if fix is zero.
2960 2961 2962 |
# File 'numeric.c', line 2960 static VALUE fix_zero_p(num) VALUE num; |
#|(other) ⇒ Integer
Bitwise OR.
2571 2572 2573 |
# File 'numeric.c', line 2571 static VALUE fix_or(x, y) VALUE x, y; |
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
2524 2525 2526 |
# File 'numeric.c', line 2524 static VALUE fix_rev(num) VALUE num; |