Class: Fixnum

Inherits:
Integer show all
Includes:
Precision
Defined in:
numeric.c

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

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

#between?

Class Method Details

.induced_from(obj) ⇒ Fixnum

Convert obj to a Fixnum. Works with numeric parameters. Also works with Symbols, but this is deprecated.

Returns:



# File 'numeric.c'

static VALUE
rb_fix_induced_from(klass, x)
    VALUE klass, x;
{
    return rb_num2fix(x);
}

Instance Method Details

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

Returns fix modulo other. See Numeric.divmod for more information.

Overloads:



# File 'numeric.c'

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);
}

#&(other) ⇒ Integer

Bitwise AND.

Returns:



# File 'numeric.c'

static VALUE
fix_and(x, y)
VALUE x, y;
{
long val;

if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_and(y, x);
}

#*(numeric) ⇒ Object

Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result.



# File 'numeric.c'

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));
}

#**(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

Returns:



# File 'numeric.c'

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);
}

#+(numeric) ⇒ Object

Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result.



# File 'numeric.c'

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;
}

#-(numeric) ⇒ Object

Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result.



# File 'numeric.c'

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;
}

#-Integer

Negates fix (which might return a Bignum).

Returns:



# File 'numeric.c'

static VALUE
fix_uminus(num)
    VALUE num;
{
    return LONG2NUM(-FIX2LONG(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.



# File 'numeric.c'

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);
}

#<(other) ⇒ Boolean

Returns true if the value of fix is less than that of other.

Returns:

  • (Boolean)


# File 'numeric.c'

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;
}

#<<(count) ⇒ Integer

Shifts fix left count positions (right if count is negative).

Returns:



# File 'numeric.c'

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);
}

#<=(other) ⇒ Boolean

Returns true if the value of fix is less thanor equal to that of other.

Returns:

  • (Boolean)


# File 'numeric.c'

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;
}

#<=>(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.

Returns:

  • (-1, 0, +1)


# File 'numeric.c'

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);
}

#==(other) ⇒ Object

Return true if fix equals other numerically.

1 == 2      #=> false
1 == 1.0    #=> true


# File 'numeric.c'

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);
}

#>(other) ⇒ Boolean

Returns true if the value of fix is greater than that of other.

Returns:

  • (Boolean)


# File 'numeric.c'

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;
}

#>=(other) ⇒ Boolean

Returns true if the value of fix is greater than or equal to that of other.

Returns:

  • (Boolean)


# File 'numeric.c'

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;
}

#>>(count) ⇒ Integer

Shifts fix right count positions (left if count is negative).

Returns:



# File 'numeric.c'

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);
}

#[](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

Returns:

  • (0, 1)


# File 'numeric.c'

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);
}

#^(other) ⇒ Integer

Bitwise EXCLUSIVE OR.

Returns:



# File 'numeric.c'

static VALUE
fix_xor(x, y)
VALUE x, y;
{
long val;

if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_xor(y, x);
}

#absaFixnum

Returns the absolute value of fix.

-12345.abs   #=> 12345
12345.abs    #=> 12345

Returns:

  • (aFixnum)


# File 'numeric.c'

static VALUE
fix_abs(fix)
    VALUE fix;
{
    long i = FIX2LONG(fix);

    if (i < 0) i = -i;

    return LONG2NUM(i);
}

#/(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.



# File 'numeric.c'

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);
}

#divmod(numeric) ⇒ Array

See Numeric#divmod.

Returns:



# File 'numeric.c'

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));
}

#id2nameString?

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"

Returns:



# File 'numeric.c'

static VALUE
fix_id2name(fix)
    VALUE fix;
{
    char *name = rb_id2name(FIX2UINT(fix));
    if (name) return rb_str_new2(name);
    return Qnil;
}

#%(other) ⇒ Numeric #modulo(other) ⇒ Numeric

Returns fix modulo other. See Numeric.divmod for more information.

Overloads:



# File 'numeric.c'

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);
}

#quo(numeric) ⇒ Float

Returns the floating point result of dividing fix by numeric.

654321.quo(13731)      #=> 47.6528293642124
654321.quo(13731.24)   #=> 47.6519964693647

Returns:



# File 'numeric.c'

static VALUE
fix_quo(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y));
}

#sizeFixnum

Returns the number of bytes in the machine representation of a Fixnum.

1.size            #=> 4
-1.size           #=> 4
2147483647.size   #=> 4

Returns:



# File 'numeric.c'

static VALUE
fix_size(fix)
    VALUE fix;
{
    return INT2FIX(sizeof(long));
}

#to_fFloat

Converts fix to a Float.

Returns:



# File 'numeric.c'

static VALUE
fix_to_f(num)
    VALUE num;
{
    double val;

    val = (double)FIX2LONG(num);

    return rb_float_new(val);
}

#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"

Returns:

  • (aString)


# File 'numeric.c'

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);
}

#to_symaSymbol

Returns the symbol whose integer value is fix. See also Fixnum#id2name.

fred = :fred.to_i
fred.id2name   #=> "fred"
fred.to_sym    #=> :fred

Returns:

  • (aSymbol)


# File 'numeric.c'

static VALUE
fix_to_sym(fix)
VALUE fix;
{
ID id = FIX2UINT(fix);

if (rb_id2name(id)) {
return ID2SYM(id);
}

#zero?Boolean

Returns true if fix is zero.

Returns:

  • (Boolean)


# File 'numeric.c'

static VALUE
fix_zero_p(num)
VALUE num;
{
if (FIX2LONG(num) == 0) {
return Qtrue;
}

#|(other) ⇒ Integer

Bitwise OR.

Returns:



# File 'numeric.c'

static VALUE
fix_or(x, y)
VALUE x, y;
{
long val;

if (!FIXNUM_P(y = fix_coerce(y))) {
return rb_big_or(y, x);
}

#~Integer

One's complement: returns a number where each bit is flipped.

Returns:



# File 'numeric.c'

static VALUE
fix_rev(num)
    VALUE num;
{
    long val = FIX2LONG(num);

    val = ~val;
    return LONG2NUM(val);
}