In Files

  • numeric.c

Parent

Class/Module Index [+]

Quicksearch
toggle debugging

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. Any attempt to add a singleton method to a Fixnum object will raise a TypeError.

Public Instance Methods

int % other → real click to toggle source
modulo(other) → real
%

Returns int modulo other.

See Numeric#divmod for more information.

 
               static VALUE
fix_mod(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        if (FIX2LONG(y) == 0) rb_num_zerodiv();
        return rb_fix_mod_fix(x, y);
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        x = rb_int2big(FIX2LONG(x));
        return rb_big_modulo(x, y);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y)));
    }
    else {
        return rb_num_coerce_bin(x, y, '%');
    }
}
            
int * numeric → numeric_result click to toggle source

Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.

 
               static VALUE
fix_mul(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        return rb_fix_mul_fix(x, y);
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_mul(y, x);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y));
    }
    else if (RB_TYPE_P(y, T_COMPLEX)) {
        return rb_nucomp_mul(y, x);
    }
    else {
        return rb_num_coerce_bin(x, y, '*');
    }
}
            
int + numeric → numeric_result click to toggle source

Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.

 
               static VALUE
fix_plus(VALUE x, VALUE 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;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_plus(y, x);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
    }
    else if (RB_TYPE_P(y, T_COMPLEX)) {
        return rb_nucomp_add(y, x);
    }
    else {
        return rb_num_coerce_bin(x, y, '+');
    }
}
            
int - numeric → numeric_result click to toggle source

Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.

 
               static VALUE
fix_minus(VALUE x, VALUE 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;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        x = rb_int2big(FIX2LONG(x));
        return rb_big_minus(x, y);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return DBL2NUM((double)FIX2LONG(x) - RFLOAT_VALUE(y));
    }
    else {
        return rb_num_coerce_bin(x, y, '-');
    }
}
            
int / numeric → numeric_result click to toggle source

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result. It may return a Bignum.

 
               static VALUE
fix_div(VALUE x, VALUE y)
{
    return fix_divide(x, y, '/');
}
            
int < real → true or false click to toggle source

Returns true if the value of int is less than that of real.

 
               static VALUE
fix_lt(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        if (FIX2LONG(x) < FIX2LONG(y)) return Qtrue;
        return Qfalse;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_cmp(y, x) == INT2FIX(+1) ? Qtrue : Qfalse;
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return rb_integer_float_cmp(x, y) == INT2FIX(-1) ? Qtrue : Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y, '<');
    }
}
            
int <= real → true or false click to toggle source

Returns true if the value of int is less than or equal to that of real.

 
               static VALUE
fix_le(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        if (FIX2LONG(x) <= FIX2LONG(y)) return Qtrue;
        return Qfalse;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_cmp(y, x) != INT2FIX(-1) ? Qtrue : Qfalse;
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        VALUE rel = rb_integer_float_cmp(x, y);
        return rel == INT2FIX(-1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y, idLE);
    }
}
            
int == other → true or false click to toggle source

Return true if int equals other numerically. Contrast this with Integer#eql?, which requires other to be a Integer.

1 == 2      #=> false
1 == 1.0    #=> true
 
               static VALUE
fix_equal(VALUE x, VALUE y)
{
    if (x == y) return Qtrue;
    if (FIXNUM_P(y)) return Qfalse;
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_eq(y, x);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return rb_integer_float_eq(x, y);
    }
    else {
        return num_equal(x, y);
    }
}
            
int == other → true or false click to toggle source

Return true if int equals other numerically. Contrast this with Integer#eql?, which requires other to be a Integer.

1 == 2      #=> false
1 == 1.0    #=> true
 
               static VALUE
fix_equal(VALUE x, VALUE y)
{
    if (x == y) return Qtrue;
    if (FIXNUM_P(y)) return Qfalse;
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_eq(y, x);
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return rb_integer_float_eq(x, y);
    }
    else {
        return num_equal(x, y);
    }
}
            
int > real → true or false click to toggle source

Returns true if the value of int is greater than that of real.

 
               static VALUE
fix_gt(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        if (FIX2LONG(x) > FIX2LONG(y)) return Qtrue;
        return Qfalse;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_cmp(y, x) == INT2FIX(-1) ? Qtrue : Qfalse;
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        return rb_integer_float_cmp(x, y) == INT2FIX(1) ? Qtrue : Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y, '>');
    }
}
            
int >= real → true or false click to toggle source

Returns true if the value of int is greater than or equal to that of real.

 
               static VALUE
fix_ge(VALUE x, VALUE y)
{
    if (FIXNUM_P(y)) {
        if (FIX2LONG(x) >= FIX2LONG(y)) return Qtrue;
        return Qfalse;
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
        return rb_big_cmp(y, x) != INT2FIX(+1) ? Qtrue : Qfalse;
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
        VALUE rel = rb_integer_float_cmp(x, y);
        return rel == INT2FIX(1) || rel == INT2FIX(0) ? Qtrue : Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y, idGE);
    }
}
            
next → integer click to toggle source
succ → integer

Returns the Integer equal to int + 1.

1.next      #=> 2
(-1).next   #=> 0
1.succ      #=> 2
(-1).succ   #=> 0
 
               static VALUE
fix_succ(VALUE num)
{
    long i = FIX2LONG(num) + 1;
    return LONG2NUM(i);
}
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.