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.
/* * call-seq: * Fixnum.induced_from(obj) => fixnum * * Convert <code>obj</code> 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.
/* * call-seq: * fix % other => Numeric * fix.modulo(other) => Numeric * * Returns <code>fix</code> modulo <code>other</code>. * See <code>Numeric.divmod</code> 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.
/* * call-seq: * fix & other => integer * * 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.
/* * call-seq: * fix * numeric => numeric_result * * Performs multiplication: the class of the resulting object depends on * the class of <code>numeric</code> 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
/* * call-seq: * fix ** other => Numeric * * Raises <code>fix</code> to the <code>other</code> 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 = FIX2LONG(y); if (b == 0) return INT2FIX(1); if (b == 1) return 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 int_pow(a, b); } 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 (RFLOAT(y)->value == 0.0) return rb_float_new(1.0); 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.
/* * call-seq: * fix + numeric => numeric_result * * Performs addition: the class of the resulting object depends on * the class of <code>numeric</code> 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.
/* * call-seq: * fix - numeric => numeric_result * * Performs subtraction: the class of the resulting object depends on * the class of <code>numeric</code> 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); }
Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.
/* * call-seq: * fix / numeric => numeric_result * fix.div(numeric) => numeric_result * * Performs division: the class of the resulting object depends on * the class of <code>numeric</code> 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.
/* * call-seq: * fix < other => true or false * * Returns <code>true</code> if the value of <code>fix</code> is * less than that of <code>other</code>. */ 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).
/* * call-seq: * fix << count => integer * * 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.
/* * call-seq: * fix <= other => true or false * * Returns <code>true</code> if the value of <code>fix</code> is * less thanor equal to that of <code>other</code>. */ 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.
/* * call-seq: * fix <=> numeric => -1, 0, +1 * * Comparison---Returns -1, 0, or +1 depending on whether <i>fix</i> is * less than, equal to, or greater than <i>numeric</i>. This is the * basis for the tests in <code>Comparable</code>. */ 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
/* * call-seq: * fix == other * * Return <code>true</code> if <code>fix</code> equals <code>other</code> * 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.
/* * call-seq: * fix > other => true or false * * Returns <code>true</code> if the value of <code>fix</code> is * greater than that of <code>other</code>. */ 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.
/* * call-seq: * fix >= other => true or false * * Returns <code>true</code> if the value of <code>fix</code> is * greater than or equal to that of <code>other</code>. */ 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).
/* * call-seq: * fix >> count => integer * * 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[0] is the least significant bit.
a = 0b11001100101010 30.downto(0) do |n| print a[n] end
produces:
0000000000000000011001100101010
/* * call-seq: * fix[n] => 0, 1 * * Bit Reference---Returns the <em>n</em>th bit in the binary * representation of <i>fix</i>, where <i>fix</i>[0] is the least * significant bit. * * a = 0b11001100101010 * 30.downto(0) do |n| print a[n] end * * <em>produces:</em> * * 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.
/* * call-seq: * fix ^ other => integer * * 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
/* * call-seq: * fix.abs -> aFixnum * * Returns the absolute value of <i>fix</i>. * * -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.
/* * call-seq: * fix / numeric => numeric_result * fix.div(numeric) => numeric_result * * Performs division: the class of the resulting object depends on * the class of <code>numeric</code> 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.
/* * call-seq: * fix.divmod(numeric) => array * * See <code>Numeric#divmod</code>. */ 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 true if fix is an even number.
/* * call-seq: * fix.even? -> true or false * * Returns <code>true</code> if <i>fix</i> is an even number. */ static VALUE fix_even_p(VALUE num) { if (num & 2) { return Qfalse; } return Qtrue; }
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124 654321.quo(13731.24) #=> 47.6519964693647
/* * call-seq: * fix.quo(numeric) => float * fix.fdiv(numeric) => float * * Returns the floating point result of dividing <i>fix</i> by * <i>numeric</i>. * * 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 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"
/* * call-seq: * fix.id2name -> string or nil * * Returns the name of the object whose symbol id is <i>fix</i>. If * there is no symbol in the symbol table with this value, returns * <code>nil</code>. <code>id2name</code> has nothing to do with the * <code>Object.id</code> method. See also <code>Fixnum#to_sym</code>, * <code>String#intern</code>, and class <code>Symbol</code>. * * symbol = :@inst_var #=> :@inst_var * id = symbol.to_i #=> 9818 * id.id2name #=> "@inst_var" */ static VALUE fix_id2name(fix) VALUE fix; { const 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.
/* * call-seq: * fix % other => Numeric * fix.modulo(other) => Numeric * * Returns <code>fix</code> modulo <code>other</code>. * See <code>Numeric.divmod</code> 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 true if fix is an odd number.
/* * call-seq: * fix.odd? -> true or false * * Returns <code>true</code> if <i>fix</i> is an odd number. */ static VALUE fix_odd_p(VALUE num) { if (num & 2) { return Qtrue; } return Qfalse; }
Returns the floating point result of dividing fix by numeric.
654321.quo(13731) #=> 47.6528293642124 654321.quo(13731.24) #=> 47.6519964693647
/* * call-seq: * fix.quo(numeric) => float * fix.fdiv(numeric) => float * * Returns the floating point result of dividing <i>fix</i> by * <i>numeric</i>. * * 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
/* * call-seq: * fix.size -> fixnum * * Returns the number of <em>bytes</em> in the machine representation * of a <code>Fixnum</code>. * * 1.size #=> 4 * -1.size #=> 4 * 2147483647.size #=> 4 */ static VALUE fix_size(fix) VALUE fix; { return INT2FIX(sizeof(long)); }
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"
/* * call-seq: * fix.to_s( base=10 ) -> aString * * Returns a string containing the representation of <i>fix</i> radix * <i>base</i> (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
/* * call-seq: * fix.to_sym -> aSymbol * * Returns the symbol whose integer value is <i>fix</i>. See also * <code>Fixnum#id2name</code>. * * 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.
/* * call-seq: * fix.zero? => true or false * * Returns <code>true</code> if <i>fix</i> is zero. * */ static VALUE fix_zero_p(num) VALUE num; { if (FIX2LONG(num) == 0) { return Qtrue; } return Qfalse; }
Bitwise OR.
/* * call-seq: * fix | other => integer * * Bitwise OR. */ static VALUE fix_or(x, y) VALUE x, y; { long val; if (!FIXNUM_P(y = fix_coerce(y))) { return rb_big_or(y, x); } val = FIX2LONG(x) | FIX2LONG(y); return LONG2NUM(val); }