Float objects represent inexact real numbers usingthe native architecture's double-precision floating pointrepresentation.
Floating point has a different arithmetic and is an inexact number. So youshould know its esoteric system. See following:
The minimum number of significant decimal digits in a double-precisionfloating point.
Usually defaults to 15.
The difference between 1 and the smallest double-precision floating pointnumber greater than 1.
Usually defaults to 2.2204460492503131e-16.
An expression representing positive infinity.
The number of base digits for thedouble
data type.
Usually defaults to 53.
The largest possible integer in a double-precision floating point number.
Usually defaults to 1.7976931348623157e+308.
The largest positive exponent in a double-precision floating point where 10raised to this power minus 1.
Usually defaults to 308.
The largest possible exponent value in a double-precision floating point.
Usually defaults to 1024.
The smallest positive normalized number in a double-precision floatingpoint.
Usually defaults to 2.2250738585072014e-308.
If the platform supports denormalized numbers, there are numbers betweenzero andFloat::MIN. 0.0.next_float returnsthe smallest positive floating point number including denormalized numbers.
The smallest negative exponent in a double-precision floating point where10 raised to this power minus 1.
Usually defaults to -307.
The smallest possible exponent value in a double-precision floating point.
Usually defaults to -1021.
An expression representing a value which is “not a number”.
The base of the floating point, or number of unique digits used torepresent the number.
Usually defaults to 2 on most systems, which would represent a base-10decimal.
Returns the modulo after division offloat
byother
.
6543.21.modulo(137)#=> 104.210000000000046543.21.modulo(137.24)#=> 92.92999999999961
static VALUEflo_mod(VALUE x, VALUE y){ double fy; if (RB_TYPE_P(y, T_FIXNUM)) { fy = (double)FIX2LONG(y); } else if (RB_TYPE_P(y, T_BIGNUM)) { fy = rb_big2dbl(y); } else if (RB_TYPE_P(y, T_FLOAT)) { fy = RFLOAT_VALUE(y); } else { return rb_num_coerce_bin(x, y, '%'); } return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));}
Returns a newFloat which is the product offloat
andother
.
VALUErb_float_mul(VALUE x, VALUE y){ if (RB_TYPE_P(y, T_FIXNUM)) { return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y)); } else if (RB_TYPE_P(y, T_BIGNUM)) { return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y)); } else if (RB_TYPE_P(y, T_FLOAT)) { return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y)); } else { return rb_num_coerce_bin(x, y, '*'); }}
Raisesfloat
to the power ofother
.
2.0**3#=> 8.0
VALUErb_float_pow(VALUE x, VALUE y){ double dx, dy; if (y == INT2FIX(2)) { dx = RFLOAT_VALUE(x); return DBL2NUM(dx * dx); } else if (RB_TYPE_P(y, T_FIXNUM)) { dx = RFLOAT_VALUE(x); dy = (double)FIX2LONG(y); } else if (RB_TYPE_P(y, T_BIGNUM)) { dx = RFLOAT_VALUE(x); dy = rb_big2dbl(y); } else if (RB_TYPE_P(y, T_FLOAT)) { dx = RFLOAT_VALUE(x); dy = RFLOAT_VALUE(y); if (dx < 0 && dy != round(dy)) return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy); } else { return rb_num_coerce_bin(x, y, idPow); } return DBL2NUM(pow(dx, dy));}
Returns a newFloat which is the sum offloat
andother
.
VALUErb_float_plus(VALUE x, VALUE y){ if (RB_TYPE_P(y, T_FIXNUM)) { return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y)); } else if (RB_TYPE_P(y, T_BIGNUM)) { return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y)); } else if (RB_TYPE_P(y, T_FLOAT)) { return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y)); } else { return rb_num_coerce_bin(x, y, '+'); }}
Returns a newFloat which is the difference offloat
andother
.
VALUErb_float_minus(VALUE x, VALUE y){ if (RB_TYPE_P(y, T_FIXNUM)) { return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y)); } else if (RB_TYPE_P(y, T_BIGNUM)) { return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y)); } else if (RB_TYPE_P(y, T_FLOAT)) { return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y)); } else { return rb_num_coerce_bin(x, y, '-'); }}
Returnsfloat
, negated.
VALUErb_float_uminus(VALUE flt){ return DBL2NUM(-RFLOAT_VALUE(flt));}
Returns a newFloat which is the result ofdividingfloat
byother
.
VALUErb_float_div(VALUE x, VALUE y){ double num = RFLOAT_VALUE(x); double den; double ret; if (RB_TYPE_P(y, T_FIXNUM)) { den = FIX2LONG(y); } else if (RB_TYPE_P(y, T_BIGNUM)) { den = rb_big2dbl(y); } else if (RB_TYPE_P(y, T_FLOAT)) { den = RFLOAT_VALUE(y); } else { return rb_num_coerce_bin(x, y, '/'); } ret = double_div_double(num, den); return DBL2NUM(ret);}
Returnstrue
iffloat
is less thanreal
.
The result ofNaN < NaN
is undefined, so animplementation-dependent value is returned.
static VALUEflo_lt(VALUE x, VALUE y){ double a, b; a = RFLOAT_VALUE(x); if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { VALUE rel = rb_integer_float_cmp(y, x); if (FIXNUM_P(rel)) return -FIX2LONG(rel) < 0 ? Qtrue : Qfalse; return Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return rb_num_coerce_relop(x, y, '<'); }#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a < b)?Qtrue:Qfalse;}
Returnstrue
iffloat
is less than or equal toreal
.
The result ofNaN <= NaN
is undefined, so animplementation-dependent value is returned.
static VALUEflo_le(VALUE x, VALUE y){ double a, b; a = RFLOAT_VALUE(x); if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { VALUE rel = rb_integer_float_cmp(y, x); if (FIXNUM_P(rel)) return -FIX2LONG(rel) <= 0 ? Qtrue : Qfalse; return Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return rb_num_coerce_relop(x, y, idLE); }#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a <= b)?Qtrue:Qfalse;}
Returns -1, 0, or +1 depending on whetherfloat
is less than,equal to, or greater thanreal
. This is the basis for thetests in theComparable module.
The result ofNaN <=> NaN
is undefined, so animplementation-dependent value is returned.
nil
is returned if the two values are incomparable.
static VALUEflo_cmp(VALUE x, VALUE y){ double a, b; VALUE i; a = RFLOAT_VALUE(x); if (isnan(a)) return Qnil; if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { VALUE rel = rb_integer_float_cmp(y, x); if (FIXNUM_P(rel)) return LONG2FIX(-FIX2LONG(rel)); return rel; } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y); } else { if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) { if (RTEST(i)) { int j = rb_cmpint(i, x, y); j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1); return INT2FIX(j); } if (a > 0.0) return INT2FIX(1); return INT2FIX(-1); } return rb_num_coerce_cmp(x, y, id_cmp); } return rb_dbl_cmp(a, b);}
Returnstrue
only ifobj
has the same value asfloat
. Contrast this with#eql?, which requiresobj
to be aFloat.
1.0==1#=> true
The result ofNaN == NaN
is undefined, so animplementation-dependent value is returned.
MJIT_FUNC_EXPORTED VALUErb_float_equal(VALUE x, VALUE y){ volatile double a, b; if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { return rb_integer_float_eq(y, x); } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return num_equal(x, y); } a = RFLOAT_VALUE(x);#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a == b)?Qtrue:Qfalse;}
Returnstrue
only ifobj
has the same value asfloat
. Contrast this with#eql?, which requiresobj
to be aFloat.
1.0==1#=> true
The result ofNaN == NaN
is undefined, so animplementation-dependent value is returned.
MJIT_FUNC_EXPORTED VALUErb_float_equal(VALUE x, VALUE y){ volatile double a, b; if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { return rb_integer_float_eq(y, x); } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return num_equal(x, y); } a = RFLOAT_VALUE(x);#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a == b)?Qtrue:Qfalse;}
Returnstrue
iffloat
is greater thanreal
.
The result ofNaN > NaN
is undefined, so animplementation-dependent value is returned.
VALUErb_float_gt(VALUE x, VALUE y){ double a, b; a = RFLOAT_VALUE(x); if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { VALUE rel = rb_integer_float_cmp(y, x); if (FIXNUM_P(rel)) return -FIX2LONG(rel) > 0 ? Qtrue : Qfalse; return Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return rb_num_coerce_relop(x, y, '>'); }#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a > b)?Qtrue:Qfalse;}
Returnstrue
iffloat
is greater than or equal toreal
.
The result ofNaN >= NaN
is undefined, so animplementation-dependent value is returned.
static VALUEflo_ge(VALUE x, VALUE y){ double a, b; a = RFLOAT_VALUE(x); if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) { VALUE rel = rb_integer_float_cmp(y, x); if (FIXNUM_P(rel)) return -FIX2LONG(rel) >= 0 ? Qtrue : Qfalse; return Qfalse; } else if (RB_TYPE_P(y, T_FLOAT)) { b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(b)) return Qfalse;#endif } else { return rb_num_coerce_relop(x, y, idGE); }#if MSC_VERSION_BEFORE(1300) if (isnan(a)) return Qfalse;#endif return (a >= b)?Qtrue:Qfalse;}
Returns the absolute value offloat
.
(-34.56).abs#=> 34.56-34.56.abs#=> 34.5634.56.abs#=> 34.56
#magnitude is an alias for#abs.
VALUErb_float_abs(VALUE flt){ double val = fabs(RFLOAT_VALUE(flt)); return DBL2NUM(val);}
Returns 0 if the value is positive, pi otherwise.
static VALUEfloat_arg(VALUE self){ if (isnan(RFLOAT_VALUE(self))) return self; if (f_tpositive_p(self)) return INT2FIX(0); return rb_const_get(rb_mMath, id_PI);}
Returns 0 if the value is positive, pi otherwise.
static VALUEfloat_arg(VALUE self){ if (isnan(RFLOAT_VALUE(self))) return self; if (f_tpositive_p(self)) return INT2FIX(0); return rb_const_get(rb_mMath, id_PI);}
Returns the smallest number greater than or equal tofloat
with a precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returns a floating point number whenndigits
is positive,otherwise returns an integer.
1.2.ceil#=> 22.0.ceil#=> 2(-1.2).ceil#=> -1(-2.0).ceil#=> -21.234567.ceil(2)#=> 1.241.234567.ceil(3)#=> 1.2351.234567.ceil(4)#=> 1.23461.234567.ceil(5)#=> 1.2345734567.89.ceil(-5)#=> 10000034567.89.ceil(-4)#=> 4000034567.89.ceil(-3)#=> 3500034567.89.ceil(-2)#=> 3460034567.89.ceil(-1)#=> 3457034567.89.ceil(0)#=> 3456834567.89.ceil(1)#=> 34567.934567.89.ceil(2)#=> 34567.8934567.89.ceil(3)#=> 34567.89
Note that the limited precision of floating point arithmetic might lead tosurprising results:
(2.1/0.7).ceil#=> 4 (!)
static VALUEflo_ceil(int argc, VALUE *argv, VALUE num){ int ndigits = 0; if (rb_check_arity(argc, 0, 1)) { ndigits = NUM2INT(argv[0]); } return rb_float_ceil(num, ndigits);}
Returns the denominator (always positive). The result is machinedependent.
See also#numerator.
VALUErb_float_denominator(VALUE self){ double d = RFLOAT_VALUE(self); VALUE r; if (isinf(d) || isnan(d)) return INT2FIX(1); r = float_to_r(self); return nurat_denominator(r);}
SeeNumeric#divmod.
42.0.divmod(6)#=> [7, 0.0]42.0.divmod(5)#=> [8, 2.0]
static VALUEflo_divmod(VALUE x, VALUE y){ double fy, div, mod; volatile VALUE a, b; if (RB_TYPE_P(y, T_FIXNUM)) { fy = (double)FIX2LONG(y); } else if (RB_TYPE_P(y, T_BIGNUM)) { fy = rb_big2dbl(y); } else if (RB_TYPE_P(y, T_FLOAT)) { fy = RFLOAT_VALUE(y); } else { return rb_num_coerce_bin(x, y, id_divmod); } flodivmod(RFLOAT_VALUE(x), fy, &div, &mod); a = dbl2ival(div); b = DBL2NUM(mod); return rb_assoc_new(a, b);}
Returnstrue
only ifobj
is aFloat with the same value asfloat
.Contrast this with Float#==, which performs type conversions.
1.0.eql?(1)#=> false
The result ofNaN.eql?(NaN)
is undefined, so animplementation-dependent value is returned.
MJIT_FUNC_EXPORTED VALUErb_float_eql(VALUE x, VALUE y){ if (RB_TYPE_P(y, T_FLOAT)) { double a = RFLOAT_VALUE(x); double b = RFLOAT_VALUE(y);#if MSC_VERSION_BEFORE(1300) if (isnan(a) || isnan(b)) return Qfalse;#endif if (a == b) return Qtrue; } return Qfalse;}
Returnsfloat / numeric
, same as Float#/.
static VALUEflo_quo(VALUE x, VALUE y){ return num_funcall1(x, '/', y);}
Returnstrue
iffloat
is a valid IEEE floatingpoint number, i.e. it is not infinite and#nan? isfalse
.
VALUErb_flo_is_finite_p(VALUE num){ double value = RFLOAT_VALUE(num);#ifdef HAVE_ISFINITE if (!isfinite(value)) return Qfalse;#else if (isinf(value) || isnan(value)) return Qfalse;#endif return Qtrue;}
Returns the largest number less than or equal tofloat
with aprecision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returns a floating point number whenndigits
is positive,otherwise returns an integer.
1.2.floor#=> 12.0.floor#=> 2(-1.2).floor#=> -2(-2.0).floor#=> -21.234567.floor(2)#=> 1.231.234567.floor(3)#=> 1.2341.234567.floor(4)#=> 1.23451.234567.floor(5)#=> 1.2345634567.89.floor(-5)#=> 034567.89.floor(-4)#=> 3000034567.89.floor(-3)#=> 3400034567.89.floor(-2)#=> 3450034567.89.floor(-1)#=> 3456034567.89.floor(0)#=> 3456734567.89.floor(1)#=> 34567.834567.89.floor(2)#=> 34567.8934567.89.floor(3)#=> 34567.89
Note that the limited precision of floating point arithmetic might lead tosurprising results:
(0.3/0.1).floor#=> 2 (!)
static VALUEflo_floor(int argc, VALUE *argv, VALUE num){ int ndigits = 0; if (rb_check_arity(argc, 0, 1)) { ndigits = NUM2INT(argv[0]); } return rb_float_floor(num, ndigits);}
Returns a hash code for this float.
See alsoObject#hash.
static VALUEflo_hash(VALUE num){ return rb_dbl_hash(RFLOAT_VALUE(num));}
Returnsnil
, -1, or 1 depending on whether the value isfinite,-Infinity
, or+Infinity
.
(0.0).infinite?#=> nil(-1.0/0.0).infinite?#=> -1(+1.0/0.0).infinite?#=> 1
VALUErb_flo_is_infinite_p(VALUE num){ double value = RFLOAT_VALUE(num); if (isinf(value)) { return INT2FIX( value < 0 ? -1 : 1 ); } return Qnil;}
Returns the absolute value offloat
.
(-34.56).abs#=> 34.56-34.56.abs#=> 34.5634.56.abs#=> 34.56
#magnitude is an alias for#abs.
VALUErb_float_abs(VALUE flt){ double val = fabs(RFLOAT_VALUE(flt)); return DBL2NUM(val);}
Returns the modulo after division offloat
byother
.
6543.21.modulo(137)#=> 104.210000000000046543.21.modulo(137.24)#=> 92.92999999999961
static VALUEflo_mod(VALUE x, VALUE y){ double fy; if (RB_TYPE_P(y, T_FIXNUM)) { fy = (double)FIX2LONG(y); } else if (RB_TYPE_P(y, T_BIGNUM)) { fy = rb_big2dbl(y); } else if (RB_TYPE_P(y, T_FLOAT)) { fy = RFLOAT_VALUE(y); } else { return rb_num_coerce_bin(x, y, '%'); } return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));}
Returnstrue
iffloat
is an invalid IEEE floatingpoint number.
a =-1.0#=> -1.0a.nan?#=> falsea =0.0/0.0#=> NaNa.nan?#=> true
static VALUEflo_is_nan_p(VALUE num){ double value = RFLOAT_VALUE(num); return isnan(value) ? Qtrue : Qfalse;}
Returnstrue
iffloat
is less than 0.
static VALUEflo_negative_p(VALUE num){ double f = RFLOAT_VALUE(num); return f < 0.0 ? Qtrue : Qfalse;}
Returns the next representable floating point number.
Float::MAX.next_float and Float::INFINITY.next_float isFloat::INFINITY.
Float::NAN.next_float isFloat::NAN.
For example:
0.01.next_float#=> 0.0100000000000000021.0.next_float#=> 1.0000000000000002100.0.next_float#=> 100.000000000000010.01.next_float-0.01#=> 1.734723475976807e-181.0.next_float-1.0#=> 2.220446049250313e-16100.0.next_float-100.0#=> 1.4210854715202004e-14f =0.01;20.times {printf"%-20a %s\n",f,f.to_s;f =f.next_float }#=> 0x1.47ae147ae147bp-7 0.01# 0x1.47ae147ae147cp-7 0.010000000000000002# 0x1.47ae147ae147dp-7 0.010000000000000004# 0x1.47ae147ae147ep-7 0.010000000000000005# 0x1.47ae147ae147fp-7 0.010000000000000007# 0x1.47ae147ae148p-7 0.010000000000000009# 0x1.47ae147ae1481p-7 0.01000000000000001# 0x1.47ae147ae1482p-7 0.010000000000000012# 0x1.47ae147ae1483p-7 0.010000000000000014# 0x1.47ae147ae1484p-7 0.010000000000000016# 0x1.47ae147ae1485p-7 0.010000000000000018# 0x1.47ae147ae1486p-7 0.01000000000000002# 0x1.47ae147ae1487p-7 0.010000000000000021# 0x1.47ae147ae1488p-7 0.010000000000000023# 0x1.47ae147ae1489p-7 0.010000000000000024# 0x1.47ae147ae148ap-7 0.010000000000000026# 0x1.47ae147ae148bp-7 0.010000000000000028# 0x1.47ae147ae148cp-7 0.01000000000000003# 0x1.47ae147ae148dp-7 0.010000000000000031# 0x1.47ae147ae148ep-7 0.010000000000000033f =0.0100.times {f+=0.1 }f#=> 9.99999999999998 # should be 10.0 in the ideal world.10-f#=> 1.9539925233402755e-14 # the floating point error.10.0.next_float-10#=> 1.7763568394002505e-15 # 1 ulp (unit in the last place).(10-f)/(10.0.next_float-10)#=> 11.0 # the error is 11 ulp.(10-f)/(10*Float::EPSILON)#=> 8.8 # approximation of the above."%a"%10#=> "0x1.4p+3""%a"%f#=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
static VALUEflo_next_float(VALUE vx){ return flo_nextafter(vx, HUGE_VAL);}
Returns the numerator. The result is machine dependent.
n =0.3.numerator#=> 5404319552844595d =0.3.denominator#=> 18014398509481984n.fdiv(d)#=> 0.3
See also#denominator.
VALUErb_float_numerator(VALUE self){ double d = RFLOAT_VALUE(self); VALUE r; if (isinf(d) || isnan(d)) return self; r = float_to_r(self); return nurat_numerator(r);}
Returns 0 if the value is positive, pi otherwise.
static VALUEfloat_arg(VALUE self){ if (isnan(RFLOAT_VALUE(self))) return self; if (f_tpositive_p(self)) return INT2FIX(0); return rb_const_get(rb_mMath, id_PI);}
Returnstrue
iffloat
is greater than 0.
static VALUEflo_positive_p(VALUE num){ double f = RFLOAT_VALUE(num); return f > 0.0 ? Qtrue : Qfalse;}
Returns the previous representable floating point number.
(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is-Float::INFINITY.
Float::NAN.prev_float isFloat::NAN.
For example:
0.01.prev_float#=> 0.0099999999999999981.0.prev_float#=> 0.9999999999999999100.0.prev_float#=> 99.999999999999990.01-0.01.prev_float#=> 1.734723475976807e-181.0-1.0.prev_float#=> 1.1102230246251565e-16100.0-100.0.prev_float#=> 1.4210854715202004e-14f =0.01;20.times {printf"%-20a %s\n",f,f.to_s;f =f.prev_float }#=> 0x1.47ae147ae147bp-7 0.01# 0x1.47ae147ae147ap-7 0.009999999999999998# 0x1.47ae147ae1479p-7 0.009999999999999997# 0x1.47ae147ae1478p-7 0.009999999999999995# 0x1.47ae147ae1477p-7 0.009999999999999993# 0x1.47ae147ae1476p-7 0.009999999999999992# 0x1.47ae147ae1475p-7 0.00999999999999999# 0x1.47ae147ae1474p-7 0.009999999999999988# 0x1.47ae147ae1473p-7 0.009999999999999986# 0x1.47ae147ae1472p-7 0.009999999999999985# 0x1.47ae147ae1471p-7 0.009999999999999983# 0x1.47ae147ae147p-7 0.009999999999999981# 0x1.47ae147ae146fp-7 0.00999999999999998# 0x1.47ae147ae146ep-7 0.009999999999999978# 0x1.47ae147ae146dp-7 0.009999999999999976# 0x1.47ae147ae146cp-7 0.009999999999999974# 0x1.47ae147ae146bp-7 0.009999999999999972# 0x1.47ae147ae146ap-7 0.00999999999999997# 0x1.47ae147ae1469p-7 0.009999999999999969# 0x1.47ae147ae1468p-7 0.009999999999999967
static VALUEflo_prev_float(VALUE vx){ return flo_nextafter(vx, -HUGE_VAL);}
Returnsfloat / numeric
, same as Float#/.
static VALUEflo_quo(VALUE x, VALUE y){ return num_funcall1(x, '/', y);}
Returns a simpler approximation of the value (flt-|eps| <= result <=flt+|eps|). If the optional argumenteps
is not given, itwill be chosen automatically.
0.3.rationalize#=> (3/10)1.333.rationalize#=> (1333/1000)1.333.rationalize(0.01)#=> (4/3)
See also#to_r.
static VALUEfloat_rationalize(int argc, VALUE *argv, VALUE self){ double d = RFLOAT_VALUE(self); VALUE rat; int neg = d < 0.0; if (neg) self = DBL2NUM(-d); if (rb_check_arity(argc, 0, 1)) { rat = rb_flt_rationalize_with_prec(self, argv[0]); } else { rat = rb_flt_rationalize(self); } if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num)); return rat;}
Returnsfloat
rounded to the nearest value with a precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returns a floating point number whenndigits
is positive,otherwise returns an integer.
1.4.round#=> 11.5.round#=> 21.6.round#=> 2(-1.5).round#=> -21.234567.round(2)#=> 1.231.234567.round(3)#=> 1.2351.234567.round(4)#=> 1.23461.234567.round(5)#=> 1.2345734567.89.round(-5)#=> 034567.89.round(-4)#=> 3000034567.89.round(-3)#=> 3500034567.89.round(-2)#=> 3460034567.89.round(-1)#=> 3457034567.89.round(0)#=> 3456834567.89.round(1)#=> 34567.934567.89.round(2)#=> 34567.8934567.89.round(3)#=> 34567.89
If the optionalhalf
keyword argument is given, numbers thatare half-way between two possible rounded values will be rounded accordingto the specified tie-breakingmode
:
:up
ornil
: round half away from zero (default)
:down
: round half toward zero
:even
: round half toward the nearest even number
2.5.round(half: :up)#=> 32.5.round(half: :down)#=> 22.5.round(half: :even)#=> 23.5.round(half: :up)#=> 43.5.round(half: :down)#=> 33.5.round(half: :even)#=> 4(-2.5).round(half: :up)#=> -3(-2.5).round(half: :down)#=> -2(-2.5).round(half: :even)#=> -2
static VALUEflo_round(int argc, VALUE *argv, VALUE num){ double number, f, x; VALUE nd, opt; int ndigits = 0; enum ruby_num_rounding_mode mode; if (rb_scan_args(argc, argv, "01:", &nd, &opt)) { ndigits = NUM2INT(nd); } mode = rb_num_get_rounding_option(opt); number = RFLOAT_VALUE(num); if (number == 0.0) { return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0); } if (ndigits < 0) { return rb_int_round(flo_to_i(num), ndigits, mode); } if (ndigits == 0) { x = ROUND_CALL(mode, round, (number, 1.0)); return dbl2ival(x); } if (isfinite(number)) { int binexp; frexp(number, &binexp); if (float_round_overflow(ndigits, binexp)) return num; if (float_round_underflow(ndigits, binexp)) return DBL2NUM(0); f = pow(10, ndigits); x = ROUND_CALL(mode, round, (number, f)); return DBL2NUM(x / f); } return num;}
Sincefloat
is already aFloat,returnsself
.
static VALUEflo_to_f(VALUE num){ return num;}
Returns thefloat
truncated to anInteger.
1.2.to_i#=> 1(-1.2).to_i#=> -1
Note that the limited precision of floating point arithmetic might lead tosurprising results:
(0.3/0.1).to_i#=> 2 (!)
static VALUEflo_to_i(VALUE num){ double f = RFLOAT_VALUE(num); if (f > 0.0) f = floor(f); if (f < 0.0) f = ceil(f); return dbl2ival(f);}
Returns thefloat
truncated to anInteger.
1.2.to_i#=> 1(-1.2).to_i#=> -1
Note that the limited precision of floating point arithmetic might lead tosurprising results:
(0.3/0.1).to_i#=> 2 (!)
static VALUEflo_to_i(VALUE num){ double f = RFLOAT_VALUE(num); if (f > 0.0) f = floor(f); if (f < 0.0) f = ceil(f); return dbl2ival(f);}
Returns the value as a rational.
2.0.to_r#=> (2/1)2.5.to_r#=> (5/2)-0.75.to_r#=> (-3/4)0.0.to_r#=> (0/1)0.3.to_r#=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn't the same as “0.3”.to_r. The latter is equivalentto “3/10”.to_r, but the former isn't so.
0.3.to_r==3/10r#=> false"0.3".to_r==3/10r#=> true
See also#rationalize.
static VALUEfloat_to_r(VALUE self){ VALUE f; int n; float_decode_internal(self, &f, &n);#if FLT_RADIX == 2 if (n == 0) return rb_rational_new1(f); if (n > 0) return rb_rational_new1(rb_int_lshift(f, INT2FIX(n))); n = -n; return rb_rational_new2(f, rb_int_lshift(ONE, INT2FIX(n)));#else f = rb_int_mul(f, rb_int_pow(INT2FIX(FLT_RADIX), n)); if (RB_TYPE_P(f, T_RATIONAL)) return f; return rb_rational_new1(f);#endif}
Returns a string containing a representation ofself
. As wellas a fixed or exponential form of thefloat
, the call mayreturnNaN
,Infinity
, and-Infinity
.
static VALUEflo_to_s(VALUE flt){ enum {decimal_mant = DBL_MANT_DIG-DBL_DIG}; enum {float_dig = DBL_DIG+1}; char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10]; double value = RFLOAT_VALUE(flt); VALUE s; char *p, *e; int sign, decpt, digs; if (isinf(value)) { static const char minf[] = "-Infinity"; const int pos = (value > 0); /* skip "-" */ return rb_usascii_str_new(minf+pos, strlen(minf)-pos); } else if (isnan(value)) return rb_usascii_str_new2("NaN"); p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e); s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0); if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1; memcpy(buf, p, digs); xfree(p); if (decpt > 0) { if (decpt < digs) { memmove(buf + decpt + 1, buf + decpt, digs - decpt); buf[decpt] = '.'; rb_str_cat(s, buf, digs + 1); } else if (decpt <= DBL_DIG) { long len; char *ptr; rb_str_cat(s, buf, digs); rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2); ptr = RSTRING_PTR(s) + len; if (decpt > digs) { memset(ptr, '0', decpt - digs); ptr += decpt - digs; } memcpy(ptr, ".0", 2); } else { goto exp; } } else if (decpt > -4) { long len; char *ptr; rb_str_cat(s, "0.", 2); rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs); ptr = RSTRING_PTR(s); memset(ptr += len, '0', -decpt); memcpy(ptr -= decpt, buf, digs); } else { goto exp; } return s; exp: if (digs > 1) { memmove(buf + 2, buf + 1, digs - 1); } else { buf[2] = '0'; digs++; } buf[1] = '.'; rb_str_cat(s, buf, digs + 1); rb_str_catf(s, "e%+03d", decpt - 1); return s;}
Returnsfloat
truncated (toward zero) to a precision ofndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with atleastndigits.abs
trailing zeros.
Returns a floating point number whenndigits
is positive,otherwise returns an integer.
2.8.truncate#=> 2(-2.8).truncate#=> -21.234567.truncate(2)#=> 1.2334567.89.truncate(-2)#=> 34500
Note that the limited precision of floating point arithmetic might lead tosurprising results:
(0.3/0.1).truncate#=> 2 (!)
static VALUEflo_truncate(int argc, VALUE *argv, VALUE num){ if (signbit(RFLOAT_VALUE(num))) return flo_ceil(argc, argv, num); else return flo_floor(argc, argv, num);}
This page was generated for Ruby 3.0.0
Generated with Ruby-doc Rdoc Generator 0.42.0.