A Fixnum
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
.
Convert obj
to a Fixnum. Works with
numeric parameters. Also works with Symbols, but this is deprecated.
static VALUE rb_fix_induced_from(klass, x) VALUE klass, x; { return rb_num2fix(x); }
Returns fix
modulo other
. See
Numeric.divmod
for more information.
static VALUE fix_mod(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long mod; fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod); return LONG2NUM(mod); } return rb_num_coerce_bin(x, y); }
Bitwise AND.
static VALUE fix_and(x, y) VALUE x, y; { long val; if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_and(y, x); } val = FIX2LONG(x) & FIX2LONG(y); return LONG2NUM(val); }
Performs multiplication: the class of the resulting object depends on the
class of numeric
and on the magnitude of the result.
static VALUE fix_mul(x, y) VALUE x, y; { if (FIXNUM_P(y)) { #ifdef __HP_cc /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */ volatile #endif long a, b, c; VALUE r; a = FIX2LONG(x); if (a == 0) return x; b = FIX2LONG(y); c = a * b; r = LONG2FIX(c); if (FIX2LONG(r) != c || c/a != b) { r = rb_big_mul(rb_int2big(a), rb_int2big(b)); } return r; } if (TYPE(y) == T_FLOAT) { return rb_float_new((double)FIX2LONG(x) * RFLOAT(y)->value); } return rb_num_coerce_bin(x, y); }
Raises fix
to the other
power, which may be
negative or fractional.
2 ** 3 #=> 8 2 ** -1 #=> 0.5 2 ** 0.5 #=> 1.4142135623731
static VALUE fix_pow(x, y) VALUE x, y; { static const double zero = 0.0; long a = FIX2LONG(x); if (FIXNUM_P(y)) { long b; b = FIX2LONG(y); if (b == 0) return INT2FIX(1); if (b == 1) return x; a = FIX2LONG(x); if (a == 0) { if (b > 0) return INT2FIX(0); return rb_float_new(1.0 / zero); } if (a == 1) return INT2FIX(1); if (a == -1) { if (b % 2 == 0) return INT2FIX(1); else return INT2FIX(-1); } if (b > 0) { return rb_big_pow(rb_int2big(a), y); } return rb_float_new(pow((double)a, (double)b)); } switch (TYPE(y)) { case T_BIGNUM: if (a == 0) return INT2FIX(0); if (a == 1) return INT2FIX(1); if (a == -1) { if (int_even_p(y)) return INT2FIX(1); else return INT2FIX(-1); } x = rb_int2big(FIX2LONG(x)); return rb_big_pow(x, y); case T_FLOAT: if (a == 0) { return rb_float_new(RFLOAT(y)->value < 0 ? (1.0 / zero) : 0.0); } if (a == 1) return rb_float_new(1.0); return rb_float_new(pow((double)a, RFLOAT(y)->value)); default: return rb_num_coerce_bin(x, y); } }
Performs addition: the class of the resulting object depends on the class
of numeric
and on the magnitude of the result.
static VALUE fix_plus(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a, b, c; VALUE r; a = FIX2LONG(x); b = FIX2LONG(y); c = a + b; r = LONG2NUM(c); return r; } if (TYPE(y) == T_FLOAT) { return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value); } return rb_num_coerce_bin(x, y); }
Performs subtraction: the class of the resulting object depends on the
class of numeric
and on the magnitude of the result.
static VALUE fix_minus(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a, b, c; VALUE r; a = FIX2LONG(x); b = FIX2LONG(y); c = a - b; r = LONG2NUM(c); return r; } if (TYPE(y) == T_FLOAT) { return rb_float_new((double)FIX2LONG(x) - RFLOAT(y)->value); } return rb_num_coerce_bin(x, y); }
Negates fix
(which might return a Bignum).
static VALUE fix_uminus(num) VALUE num; { return LONG2NUM(-FIX2LONG(num)); }
Performs division: the class of the resulting object depends on the class
of numeric
and on the magnitude of the result.
static VALUE fix_div(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long div; fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0); return LONG2NUM(div); } return rb_num_coerce_bin(x, y); }
Returns true
if the value of fix
is less than
that of other
.
static VALUE fix_lt(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a = FIX2LONG(x), b = FIX2LONG(y); if (a < b) return Qtrue; return Qfalse; } else { return rb_num_coerce_relop(x, y); } }
Shifts fix left count positions (right if count is negative).
static VALUE rb_fix_lshift(x, y) VALUE x, y; { long val, width; val = NUM2LONG(x); if (!FIXNUM_P(y)) return rb_big_lshift(rb_int2big(val), y); width = FIX2LONG(y); if (width < 0) return fix_rshift(val, (unsigned long)-width); return fix_lshift(val, width); }
Returns true
if the value of fix
is less thanor
equal to that of other
.
static VALUE fix_le(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a = FIX2LONG(x), b = FIX2LONG(y); if (a <= b) return Qtrue; return Qfalse; } else { return rb_num_coerce_relop(x, y); } }
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
.
static VALUE fix_cmp(x, y) VALUE x, y; { if (x == y) return INT2FIX(0); if (FIXNUM_P(y)) { long a = FIX2LONG(x), b = FIX2LONG(y); if (a > b) return INT2FIX(1); return INT2FIX(-1); } else { return rb_num_coerce_cmp(x, y); } }
Return true
if fix
equals other
numerically.
1 == 2 #=> false 1 == 1.0 #=> true
static VALUE fix_equal(x, y) VALUE x, y; { if (x == y) return Qtrue; if (FIXNUM_P(y)) return Qfalse; return num_equal(x, y); }
Returns true
if the value of fix
is greater than
that of other
.
static VALUE fix_gt(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a = FIX2LONG(x), b = FIX2LONG(y); if (a > b) return Qtrue; return Qfalse; } else { return rb_num_coerce_relop(x, y); } }
Returns true
if the value of fix
is greater than
or equal to that of other
.
static VALUE fix_ge(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long a = FIX2LONG(x), b = FIX2LONG(y); if (a >= b) return Qtrue; return Qfalse; } else { return rb_num_coerce_relop(x, y); } }
Shifts fix right count positions (left if count is negative).
static VALUE rb_fix_rshift(x, y) VALUE x, y; { long i, val; val = FIX2LONG(x); if (!FIXNUM_P(y)) return rb_big_rshift(rb_int2big(val), y); i = FIX2LONG(y); if (i == 0) return x; if (i < 0) return fix_lshift(val, (unsigned long)-i); return fix_rshift(val, i); }
Bit Reference—Returns the nth bit in the binary representation of fix, where fix is the least significant bit.
a = 0b11001100101010 30.downto(0) do |n| print a[n] end
produces:
0000000000000000011001100101010
static VALUE fix_aref(fix, idx) VALUE fix, idx; { long val = FIX2LONG(fix); long i; if (!FIXNUM_P(idx = fix_coerce(idx))) { idx = rb_big_norm(idx); if (!FIXNUM_P(idx)) { if (!RBIGNUM(idx)->sign || val >= 0) return INT2FIX(0); return INT2FIX(1); } } i = FIX2LONG(idx); if (i < 0) return INT2FIX(0); if (sizeof(VALUE)*CHAR_BIT-1 < i) { if (val < 0) return INT2FIX(1); return INT2FIX(0); } if (val & (1L<<i)) return INT2FIX(1); return INT2FIX(0); }
Bitwise EXCLUSIVE OR.
static VALUE fix_xor(x, y) VALUE x, y; { long val; if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_xor(y, x); } val = FIX2LONG(x) ^ FIX2LONG(y); return LONG2NUM(val); }
Returns the absolute value of fix.
-12345.abs #=> 12345 12345.abs #=> 12345
static VALUE fix_abs(fix) VALUE fix; { long i = FIX2LONG(fix); if (i < 0) i = -i; return LONG2NUM(i); }
Performs division: the class of the resulting object depends on the class
of numeric
and on the magnitude of the result.
static VALUE fix_div(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long div; fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0); return LONG2NUM(div); } return rb_num_coerce_bin(x, y); }
See Numeric#divmod
.
static VALUE fix_divmod(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long div, mod; fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod); return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod)); } return rb_num_coerce_bin(x, y); }
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"
static VALUE fix_id2name(fix) VALUE fix; { char *name = rb_id2name(FIX2UINT(fix)); if (name) return rb_str_new2(name); return Qnil; }
Returns fix
modulo other
. See
Numeric.divmod
for more information.
static VALUE fix_mod(x, y) VALUE x, y; { if (FIXNUM_P(y)) { long mod; fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod); return LONG2NUM(mod); } return rb_num_coerce_bin(x, y); }
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124 654321.quo(13731.24) #=> 47.6519964693647
static VALUE fix_quo(x, y) VALUE x, y; { if (FIXNUM_P(y)) { return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y)); } return rb_num_coerce_bin(x, y); }
Returns the number of bytes in the machine representation of a
Fixnum
.
1.size #=> 4 -1.size #=> 4 2147483647.size #=> 4
static VALUE fix_size(fix) VALUE fix; { return INT2FIX(sizeof(long)); }
Converts fix to a Float
.
static VALUE fix_to_f(num) VALUE num; { double val; val = (double)FIX2LONG(num); return rb_float_new(val); }
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"
static VALUE fix_to_s(argc, argv, x) int argc; VALUE *argv; VALUE x; { VALUE b; int base; rb_scan_args(argc, argv, "01", &b); if (argc == 0) base = 10; else base = NUM2INT(b); return rb_fix2str(x, base); }
Returns the symbol whose integer value is fix. See also
Fixnum#id2name
.
fred = :fred.to_i fred.id2name #=> "fred" fred.to_sym #=> :fred
static VALUE fix_to_sym(fix) VALUE fix; { ID id = FIX2UINT(fix); if (rb_id2name(id)) { return ID2SYM(id); } return Qnil; }
Returns true
if fix is zero.
static VALUE fix_zero_p(num) VALUE num; { if (FIX2LONG(num) == 0) { return Qtrue; } return Qfalse; }