In Files

  • complex.c
  • numeric.c
  • rational.c

Float

Float objects represent real numbers using the native architecture's double-precision floating point representation.

Public Instance Methods

flt % other => float click to toggle source

Return the modulo after division of flt by other.

6543.21.modulo(137)      #=> 104.21
6543.21.modulo(137.24)   #=> 92.9299999999996
 
               static VALUE
flo_mod(VALUE x, VALUE y)
{
    double fy, mod;

    switch (TYPE(y)) {
      case T_FIXNUM:
        fy = (double)FIX2LONG(y);
        break;
      case T_BIGNUM:
        fy = rb_big2dbl(y);
        break;
      case T_FLOAT:
        fy = RFLOAT_VALUE(y);
        break;
      default:
        return rb_num_coerce_bin(x, y, '%');
    }
    flodivmod(RFLOAT_VALUE(x), fy, 0, &mod);
    return DBL2NUM(mod);
}
            
float * other => float click to toggle source

Returns a new float which is the product of float and other.

 
               static VALUE
flo_mul(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
        return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
      case T_BIGNUM:
        return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
      case T_FLOAT:
        return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
      default:
        return rb_num_coerce_bin(x, y, '*');
    }
}
            
flt ** other => float click to toggle source

Raises float the other power.

 
               static VALUE
flo_pow(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
        return DBL2NUM(pow(RFLOAT_VALUE(x), (double)FIX2LONG(y)));
      case T_BIGNUM:
        return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y)));
      case T_FLOAT:
        return DBL2NUM(pow(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
      default:
        return rb_num_coerce_bin(x, y, rb_intern("**"));
    }
}
            
float + other => float click to toggle source

Returns a new float which is the sum of float and other.

 
               static VALUE
flo_plus(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
        return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
      case T_BIGNUM:
        return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
      case T_FLOAT:
        return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
      default:
        return rb_num_coerce_bin(x, y, '+');
    }
}
            
float + other => float click to toggle source

Returns a new float which is the difference of float and other.

 
               static VALUE
flo_minus(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
        return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
      case T_BIGNUM:
        return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
      case T_FLOAT:
        return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
      default:
        return rb_num_coerce_bin(x, y, '-');
    }
}
            
-float => float click to toggle source

Returns float, negated.

 
               static VALUE
flo_uminus(VALUE flt)
{
    return DBL2NUM(-RFLOAT_VALUE(flt));
}
            
float / other => float click to toggle source

Returns a new float which is the result of dividing float by other.

 
               static VALUE
flo_div(VALUE x, VALUE y)
{
    long f_y;
    double d;

    switch (TYPE(y)) {
      case T_FIXNUM:
        f_y = FIX2LONG(y);
        return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
      case T_BIGNUM:
        d = rb_big2dbl(y);
        return DBL2NUM(RFLOAT_VALUE(x) / d);
      case T_FLOAT:
        return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
      default:
        return rb_num_coerce_bin(x, y, '/');
    }
}
            
flt < other => true or false click to toggle source

true if flt is less than other.

 
               static VALUE
flo_lt(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
        b = (double)FIX2LONG(y);
        break;

      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;

      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;

      default:
        return rb_num_coerce_relop(x, y, '<');
    }
    if (isnan(a)) return Qfalse;
    return (a < b)?Qtrue:Qfalse;
}
            
flt <= other => true or false click to toggle source

true if flt is less than or equal to other.

 
               static VALUE
flo_le(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
        b = (double)FIX2LONG(y);
        break;

      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;

      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;

      default:
        return rb_num_coerce_relop(x, y, rb_intern("<="));
    }
    if (isnan(a)) return Qfalse;
    return (a <= b)?Qtrue:Qfalse;
}
            
flt <=> numeric => -1, 0, +1 click to toggle source

Returns -1, 0, or +1 depending on whether flt is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.

 
               static VALUE
flo_cmp(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    if (isnan(a)) return Qnil;
    switch (TYPE(y)) {
      case T_FIXNUM:
        b = (double)FIX2LONG(y);
        break;

      case T_BIGNUM:
        if (isinf(a)) {
            if (a > 0.0) return INT2FIX(1);
            else return INT2FIX(-1);
        }
        b = rb_big2dbl(y);
        break;

      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        break;

      default:
        if (isinf(a) && (!rb_respond_to(y, rb_intern("infinite?")) ||
                         !RTEST(rb_funcall(y, rb_intern("infinite?"), 0, 0)))) {
            if (a > 0.0) return INT2FIX(1);
            return INT2FIX(-1);
        }
        return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
    }
    return rb_dbl_cmp(a, b);
}
            
flt == obj => true or false click to toggle source

Returns true only if obj has the same value as flt. Contrast this with Float#eql?, which requires obj to be a Float.

1.0 == 1   #=> true
 
               static VALUE
flo_eq(VALUE x, VALUE y)
{
    volatile double a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
        b = FIX2LONG(y);
        break;
      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;
      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;
      default:
        return num_equal(x, y);
    }
    a = RFLOAT_VALUE(x);
    if (isnan(a)) return Qfalse;
    return (a == b)?Qtrue:Qfalse;
}
            
flt == obj => true or false click to toggle source

Returns true only if obj has the same value as flt. Contrast this with Float#eql?, which requires obj to be a Float.

1.0 == 1   #=> true
 
               static VALUE
flo_eq(VALUE x, VALUE y)
{
    volatile double a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
        b = FIX2LONG(y);
        break;
      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;
      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;
      default:
        return num_equal(x, y);
    }
    a = RFLOAT_VALUE(x);
    if (isnan(a)) return Qfalse;
    return (a == b)?Qtrue:Qfalse;
}
            
flt > other => true or false click to toggle source

true if flt is greater than other.

 
               static VALUE
flo_gt(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
        b = (double)FIX2LONG(y);
        break;

      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;

      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;

      default:
        return rb_num_coerce_relop(x, y, '>');
    }
    if (isnan(a)) return Qfalse;
    return (a > b)?Qtrue:Qfalse;
}
            
flt >= other => true or false click to toggle source

true if flt is greater than or equal to other.

 
               static VALUE
flo_ge(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
        b = (double)FIX2LONG(y);
        break;

      case T_BIGNUM:
        b = rb_big2dbl(y);
        break;

      case T_FLOAT:
        b = RFLOAT_VALUE(y);
        if (isnan(b)) return Qfalse;
        break;

      default:
        return rb_num_coerce_relop(x, y, rb_intern(">="));
    }
    if (isnan(a)) return Qfalse;
    return (a >= b)?Qtrue:Qfalse;
}
            
abs => float click to toggle source

Returns the absolute value of flt.

(-34.56).abs   #=> 34.56
-34.56.abs     #=> 34.56
 
               static VALUE
flo_abs(VALUE flt)
{
    double val = fabs(RFLOAT_VALUE(flt));
    return DBL2NUM(val);
}
            
angle → 0 or float click to toggle source

Returns 0 if the value is positive, pi otherwise.

 
               static VALUE
float_arg(VALUE self)
{
    if (isnan(RFLOAT_VALUE(self)))
        return self;
    return rb_call_super(0, 0);
}
            
arg → 0 or float click to toggle source

Returns 0 if the value is positive, pi otherwise.

 
               static VALUE
float_arg(VALUE self)
{
    if (isnan(RFLOAT_VALUE(self)))
        return self;
    return rb_call_super(0, 0);
}
            
ceil => integer click to toggle source

Returns the smallest Integer greater than or equal to flt.

1.2.ceil      #=> 2
2.0.ceil      #=> 2
(-1.2).ceil   #=> -1
(-2.0).ceil   #=> -2
 
               static VALUE
flo_ceil(VALUE num)
{
    double f = ceil(RFLOAT_VALUE(num));
    long val;

    if (!FIXABLE(f)) {
        return rb_dbl2big(f);
    }
    val = f;
    return LONG2FIX(val);
}
            
coerce(p1) click to toggle source

MISSING: documentation

 
               static VALUE
flo_coerce(VALUE x, VALUE y)
{
    return rb_assoc_new(rb_Float(y), x);
}
            
denominator() click to toggle source
 
               static VALUE
float_denominator(VALUE self)
{
    double d = RFLOAT_VALUE(self);
    if (isinf(d) || isnan(d))
        return INT2FIX(1);
    return rb_call_super(0, 0);
}
            
divmod(numeric) => array click to toggle source

See Numeric#divmod.

 
               static VALUE
flo_divmod(VALUE x, VALUE y)
{
    double fy, div, mod;
    volatile VALUE a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
        fy = (double)FIX2LONG(y);
        break;
      case T_BIGNUM:
        fy = rb_big2dbl(y);
        break;
      case T_FLOAT:
        fy = RFLOAT_VALUE(y);
        break;
      default:
        return rb_num_coerce_bin(x, y, rb_intern("divmod"));
    }
    flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
    a = dbl2ival(div);
    b = DBL2NUM(mod);
    return rb_assoc_new(a, b);
}
            
eql?(obj) => true or false click to toggle source

Returns true only if obj is a Float with the same value as flt. Contrast this with Float#==, which performs type conversions.

1.0.eql?(1)   #=> false
 
               static VALUE
flo_eql(VALUE x, VALUE y)
{
    if (TYPE(y) == T_FLOAT) {
        double a = RFLOAT_VALUE(x);
        double b = RFLOAT_VALUE(y);

        if (isnan(a) || isnan(b)) return Qfalse;
        if (a == b) return Qtrue;
    }
    return Qfalse;
}
            
fdiv(p1) click to toggle source
 
               static VALUE
flo_quo(VALUE x, VALUE y)
{
    return rb_funcall(x, '/', 1, y);
}
            
finite? → true or false click to toggle source

Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).

 
               static VALUE
flo_is_finite_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

#if HAVE_FINITE
    if (!finite(value))
        return Qfalse;
#else
    if (isinf(value) || isnan(value))
        return Qfalse;
#endif

    return Qtrue;
}
            
floor => integer click to toggle source

Returns the largest integer less than or equal to flt.

1.2.floor      #=> 1
2.0.floor      #=> 2
(-1.2).floor   #=> -2
(-2.0).floor   #=> -2
 
               static VALUE
flo_floor(VALUE num)
{
    double f = floor(RFLOAT_VALUE(num));
    long val;

    if (!FIXABLE(f)) {
        return rb_dbl2big(f);
    }
    val = f;
    return LONG2FIX(val);
}
            
hash => integer click to toggle source

Returns a hash code for this float.

 
               static VALUE
flo_hash(VALUE num)
{
    double d;
    int hash;

    d = RFLOAT_VALUE(num);
    /* normalize -0.0 to 0.0 */
    if (d == 0.0) d = 0.0;
    hash = rb_memhash(&d, sizeof(d));
    return INT2FIX(hash);
}
            
infinite? → nil, -1, +1 click to toggle source

Returns nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity.

(0.0).infinite?        #=> nil
(-1.0/0.0).infinite?   #=> -1
(+1.0/0.0).infinite?   #=> 1
 
               static VALUE
flo_is_infinite_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

    if (isinf(value)) {
        return INT2FIX( value < 0 ? -1 : 1 );
    }

    return Qnil;
}
            
abs => float click to toggle source

Returns the absolute value of flt.

(-34.56).abs   #=> 34.56
-34.56.abs     #=> 34.56
 
               static VALUE
flo_abs(VALUE flt)
{
    double val = fabs(RFLOAT_VALUE(flt));
    return DBL2NUM(val);
}
            
modulo(other) => float click to toggle source

Return the modulo after division of flt by other.

6543.21.modulo(137)      #=> 104.21
6543.21.modulo(137.24)   #=> 92.9299999999996
 
               static VALUE
flo_mod(VALUE x, VALUE y)
{
    double fy, mod;

    switch (TYPE(y)) {
      case T_FIXNUM:
        fy = (double)FIX2LONG(y);
        break;
      case T_BIGNUM:
        fy = rb_big2dbl(y);
        break;
      case T_FLOAT:
        fy = RFLOAT_VALUE(y);
        break;
      default:
        return rb_num_coerce_bin(x, y, '%');
    }
    flodivmod(RFLOAT_VALUE(x), fy, 0, &mod);
    return DBL2NUM(mod);
}
            
nan? → true or false click to toggle source

Returns true if flt is an invalid IEEE floating point number.

a = -1.0      #=> -1.0
a.nan?        #=> false
a = 0.0/0.0   #=> NaN
a.nan?        #=> true
 
               static VALUE
flo_is_nan_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

    return isnan(value) ? Qtrue : Qfalse;
}
            
numerator() click to toggle source
 
               static VALUE
float_numerator(VALUE self)
{
    double d = RFLOAT_VALUE(self);
    if (isinf(d) || isnan(d))
        return self;
    return rb_call_super(0, 0);
}
            
phase → 0 or float click to toggle source

Returns 0 if the value is positive, pi otherwise.

 
               static VALUE
float_arg(VALUE self)
{
    if (isnan(RFLOAT_VALUE(self)))
        return self;
    return rb_call_super(0, 0);
}
            
quo(p1) click to toggle source
 
               static VALUE
flo_quo(VALUE x, VALUE y)
{
    return rb_funcall(x, '/', 1, y);
}
            
round([ndigits]) => integer or float click to toggle source

Rounds flt to a given precision in decimal digits (default 0 digits). Precision may be negative. Returns a a floating point number when ndigits is more than one.

1.5.round      #=> 2
(-1.5).round   #=> -2
 
               static VALUE
flo_round(int argc, VALUE *argv, VALUE num)
{
    VALUE nd;
    double number, f;
    int ndigits = 0, i;
    long val;

    if (argc > 0 && rb_scan_args(argc, argv, "01", &nd) == 1) {
        ndigits = NUM2INT(nd);
    }
    number  = RFLOAT_VALUE(num);
    f = 1.0;
    i = abs(ndigits);
    while  (--i >= 0)
        f = f*10.0;

    if (isinf(f)) {
        if (ndigits < 0) number = 0;
    }
    else {
        if (ndigits < 0) number /= f;
        else number *= f;
        number = round(number);
        if (ndigits < 0) number *= f;
        else number /= f;
    }

    if (ndigits > 0) return DBL2NUM(number);

    if (!FIXABLE(number)) {
        return rb_dbl2big(number);
    }
    val = number;
    return LONG2FIX(val);
}
            
to_f => flt click to toggle source

As flt is already a float, returns self.

 
               static VALUE
flo_to_f(VALUE num)
{
    return num;
}
            
to_i => integer click to toggle source
to_int => integer

Returns flt truncated to an Integer.

 
               static VALUE
flo_truncate(VALUE num)
{
    double f = RFLOAT_VALUE(num);
    long val;

    if (f > 0.0) f = floor(f);
    if (f < 0.0) f = ceil(f);

    if (!FIXABLE(f)) {
        return rb_dbl2big(f);
    }
    val = f;
    return LONG2FIX(val);
}
            
to_int => integer click to toggle source

Returns flt truncated to an Integer.

 
               static VALUE
flo_truncate(VALUE num)
{
    double f = RFLOAT_VALUE(num);
    long val;

    if (f > 0.0) f = floor(f);
    if (f < 0.0) f = ceil(f);

    if (!FIXABLE(f)) {
        return rb_dbl2big(f);
    }
    val = f;
    return LONG2FIX(val);
}
            
to_r() click to toggle source
 
               static VALUE
float_to_r(VALUE self)
{
    VALUE f, n;

    float_decode_internal(self, &f, &n);
    return f_to_r(f_mul(f, f_expt(INT2FIX(FLT_RADIX), n)));
}
            
to_s => string click to toggle source

Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return “NaN'', “Infinity'', and “-Infinity''.

 
               static VALUE
flo_to_s(VALUE flt)
{
    char buf[32];
    double value = RFLOAT_VALUE(flt);
    char *p, *e;

    if (isinf(value))
        return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
    else if(isnan(value))
        return rb_usascii_str_new2("NaN");

    snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
    if (!(e = strchr(buf, 'e'))) {
        e = buf + strlen(buf);
    }
    if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
        snprintf(buf, sizeof(buf), "%#.14e", value);
        if (!(e = strchr(buf, 'e'))) {
            e = buf + strlen(buf);
        }
    }
    p = e;
    while (p[-1]=='0' && ISDIGIT(p[-2]))
        p--;
    memmove(p, e, strlen(e)+1);
    return rb_usascii_str_new2(buf);
}
            
truncate => integer click to toggle source

Returns flt truncated to an Integer.

 
               static VALUE
flo_truncate(VALUE num)
{
    double f = RFLOAT_VALUE(num);
    long val;

    if (f > 0.0) f = floor(f);
    if (f < 0.0) f = ceil(f);

    if (!FIXABLE(f)) {
        return rb_dbl2big(f);
    }
    val = f;
    return LONG2FIX(val);
}
            
zero? → true or false click to toggle source

Returns true if flt is 0.0.

 
               static VALUE
flo_zero_p(VALUE num)
{
    if (RFLOAT_VALUE(num) == 0.0) {
        return Qtrue;
    }
    return Qfalse;
}