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
. -
#id2name ⇒ String?
Returns the name of the object whose symbol id is fix.
-
#modulo ⇒ Object
Returns
fix
moduloother
. -
#quo(numeric) ⇒ Float
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, #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.
1836 1837 1838 |
# File 'numeric.c', line 1836 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.
2147 2148 2149 |
# File 'numeric.c', line 2147 static VALUE fix_mod(x, y) VALUE x, y; |
#&(other) ⇒ Integer
Bitwise AND.
2375 2376 2377 |
# File 'numeric.c', line 2375 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.
2034 2035 2036 |
# File 'numeric.c', line 2034 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
2192 2193 2194 |
# File 'numeric.c', line 2192 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.
1974 1975 1976 |
# File 'numeric.c', line 1974 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.
2004 2005 2006 |
# File 'numeric.c', line 2004 static VALUE fix_minus(x, y) VALUE x, y; |
#- ⇒ Integer
Negates fix
(which might return a Bignum).
1896 1897 1898 |
# File 'numeric.c', line 1896 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.
2125 2126 2127 |
# File 'numeric.c', line 2125 static VALUE fix_div(x, y) VALUE x, y; |
#<(other) ⇒ Boolean
Returns true
if the value of fix
is less than that of other
.
2313 2314 2315 |
# File 'numeric.c', line 2313 static VALUE fix_lt(x, y) VALUE x, y; |
#<<(count) ⇒ Integer
Shifts fix left count positions (right if count is negative).
2437 2438 2439 |
# File 'numeric.c', line 2437 static VALUE fix_lshift(x, y) VALUE x, y; |
#<=(other) ⇒ Boolean
Returns true
if the value of fix
is less thanor equal to that of other
.
2336 2337 2338 |
# File 'numeric.c', line 2336 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
.
2243 2244 2245 |
# File 'numeric.c', line 2243 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
2225 2226 2227 |
# File 'numeric.c', line 2225 static VALUE fix_equal(x, y) VALUE x, y; |
#>(other) ⇒ Boolean
Returns true
if the value of fix
is greater than that of other
.
2267 2268 2269 |
# File 'numeric.c', line 2267 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
.
2290 2291 2292 |
# File 'numeric.c', line 2290 static VALUE fix_ge(x, y) VALUE x, y; |
#>>(count) ⇒ Integer
Shifts fix right count positions (left if count is negative).
2462 2463 2464 |
# File 'numeric.c', line 2462 static VALUE 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
2497 2498 2499 |
# File 'numeric.c', line 2497 static VALUE fix_aref(fix, idx) VALUE fix, idx; |
#^(other) ⇒ Integer
Bitwise EXCLUSIVE OR.
2415 2416 2417 |
# File 'numeric.c', line 2415 static VALUE fix_xor(x, y) VALUE x, y; |
#abs ⇒ aFixnum
Returns the absolute value of fix.
-12345.abs #=> 12345
12345.abs #=> 12345
2554 2555 2556 |
# File 'numeric.c', line 2554 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.
2125 2126 2127 |
# File 'numeric.c', line 2125 static VALUE fix_div(x, y) VALUE x, y; |
#divmod(numeric) ⇒ Array
See Numeric#divmod
.
2166 2167 2168 |
# File 'numeric.c', line 2166 static VALUE fix_divmod(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"
2580 2581 2582 |
# File 'numeric.c', line 2580 static VALUE fix_id2name(fix) VALUE fix; |
#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric
Returns fix
modulo other
. See Numeric.divmod
for more information.
2147 2148 2149 |
# File 'numeric.c', line 2147 static VALUE fix_mod(x, y) VALUE x, y; |
#quo(numeric) ⇒ Float
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124
654321.quo(13731.24) #=> 47.6519964693647
2105 2106 2107 |
# File 'numeric.c', line 2105 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
2627 2628 2629 |
# File 'numeric.c', line 2627 static VALUE fix_size(fix) VALUE fix; |
#to_f ⇒ Float
Converts fix to a Float
.
2532 2533 2534 |
# File 'numeric.c', line 2532 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"
1949 1950 1951 |
# File 'numeric.c', line 1949 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
2602 2603 2604 |
# File 'numeric.c', line 2602 static VALUE fix_to_sym(fix) VALUE fix; |
#zero? ⇒ Boolean
Returns true
if fix is zero.
2759 2760 2761 |
# File 'numeric.c', line 2759 static VALUE fix_zero_p(num) VALUE num; |
#|(other) ⇒ Integer
Bitwise OR.
2395 2396 2397 |
# File 'numeric.c', line 2395 static VALUE fix_or(x, y) VALUE x, y; |
#~ ⇒ Integer
One’s complement: returns a number where each bit is flipped.
2358 2359 2360 |
# File 'numeric.c', line 2358 static VALUE fix_rev(num) VALUE num; |