Class: Float
Overview
******************************************************************
A \Float object represents a sometimes-inexact real number using the native
architecture's double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number.
So you should know its esoteric system. See following:
- https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rubys-floats-imprecise
- https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
You can create a \Float object explicitly with:
- A {floating-point literal}[rdoc-ref:syntax/literals.rdoc@Float+Literals].
You can convert certain objects to Floats with:
- \Method #Float.
== What's Here
First, what's elsewhere. \Class \Float:
- Inherits from
{class Numeric}[rdoc-ref:Numeric@What-27s+Here]
and {class Object}[rdoc-ref:Object@What-27s+Here].
- Includes {module Comparable}[rdoc-ref:Comparable@What-27s+Here].
Here, class \Float provides methods for:
- {Querying}[rdoc-ref:Float@Querying]
- {Comparing}[rdoc-ref:Float@Comparing]
- {Converting}[rdoc-ref:Float@Converting]
=== Querying
- #finite?: Returns whether +self+ is finite.
- #hash: Returns the integer hash code for +self+.
- #infinite?: Returns whether +self+ is infinite.
- #nan?: Returns whether +self+ is a NaN (not-a-number).
=== Comparing
- #<: Returns whether +self+ is less than the given value.
- #<=: Returns whether +self+ is less than or equal to the given value.
- #<=>: Returns a number indicating whether +self+ is less than, equal
to, or greater than the given value.
- #== (aliased as #=== and #eql?): Returns whether +self+ is equal to
the given value.
- #>: Returns whether +self+ is greater than the given value.
- #>=: Returns whether +self+ is greater than or equal to the given value.
=== Converting
- #% (aliased as #modulo): Returns +self+ modulo the given value.
- #*: Returns the product of +self+ and the given value.
- #**: Returns the value of +self+ raised to the power of the given value.
- #+: Returns the sum of +self+ and the given value.
- #-: Returns the difference of +self+ and the given value.
- #/: Returns the quotient of +self+ and the given value.
- #ceil: Returns the smallest number greater than or equal to +self+.
- #coerce: Returns a 2-element array containing the given value converted to a \Float
and +self+
- #divmod: Returns a 2-element array containing the quotient and remainder
results of dividing +self+ by the given value.
- #fdiv: Returns the \Float result of dividing +self+ by the given value.
- #floor: Returns the greatest number smaller than or equal to +self+.
- #next_float: Returns the next-larger representable \Float.
- #prev_float: Returns the next-smaller representable \Float.
- #quo: Returns the quotient from dividing +self+ by the given value.
- #round: Returns +self+ rounded to the nearest value, to a given precision.
- #to_i (aliased as #to_int): Returns +self+ truncated to an Integer.
- #to_s (aliased as #inspect): Returns a string containing the place-value
representation of +self+ in the given radix.
- #truncate: Returns +self+ truncated to a given precision.
Constant Summary collapse
- RADIX =
The base of the floating point, or number of unique digits used to represent the number.
Usually defaults to 2 on most systems, which would represent a base-10 decimal.
INT2FIX(FLT_RADIX)
- MANT_DIG =
The number of base digits for the
double
data type.Usually defaults to 53.
INT2FIX(DBL_MANT_DIG)
- DIG =
The minimum number of significant decimal digits in a double-precision floating point.
Usually defaults to 15.
INT2FIX(DBL_DIG)
- MIN_EXP =
The smallest possible exponent value in a double-precision floating point.
Usually defaults to -1021.
INT2FIX(DBL_MIN_EXP)
- MAX_EXP =
The largest possible exponent value in a double-precision floating point.
Usually defaults to 1024.
INT2FIX(DBL_MAX_EXP)
- MIN_10_EXP =
The smallest negative exponent in a double-precision floating point where 10 raised to this power minus 1.
Usually defaults to -307.
INT2FIX(DBL_MIN_10_EXP)
- MAX_10_EXP =
The largest positive exponent in a double-precision floating point where 10 raised to this power minus 1.
Usually defaults to 308.
INT2FIX(DBL_MAX_10_EXP)
- MIN =
:MIN. 0.0.next_float returns the smallest positive floating point number including denormalized numbers.
The smallest positive normalized number in a double-precision floating point. Usually defaults to 2.2250738585072014e-308. If the platform supports denormalized numbers, there are numbers between zero and Float
- MAX =
The largest possible integer in a double-precision floating point number.
Usually defaults to 1.7976931348623157e+308.
DBL2NUM(DBL_MAX)
- EPSILON =
The difference between 1 and the smallest double-precision floating point number greater than 1.
Usually defaults to 2.2204460492503131e-16.
DBL2NUM(DBL_EPSILON)
- INFINITY =
An expression representing positive infinity.
DBL2NUM(HUGE_VAL)
- NAN =
An expression representing a value which is “not a number”.
DBL2NUM(nan(""))
Instance Method Summary collapse
-
#%(other) ⇒ Float
Returns
self
moduloother
as a float. -
#*(other) ⇒ Numeric
Returns a new Float which is the product of
self
andother
:. -
#**(other) ⇒ Numeric
Raises
self
to the power ofother
:. -
#+(other) ⇒ Numeric
Returns a new Float which is the sum of
self
andother
:. -
#-(other) ⇒ Numeric
Returns a new Float which is the difference of
self
andother
:. -
#/(other) ⇒ Numeric
Returns a new Float which is the result of dividing
self
byother
:. -
#<(other) ⇒ Boolean
Returns
true
ifself
is numerically less thanother
:. -
#<=(other) ⇒ Boolean
Returns
true
ifself
is numerically less than or equal toother
:. -
#<=>(other) ⇒ -1, ...
Returns a value that depends on the numeric relation between
self
andother
:. - #== ⇒ Object
- #=== ⇒ Object
-
#>(other) ⇒ Boolean
Returns
true
ifself
is numerically greater thanother
:. -
#>=(other) ⇒ Boolean
Returns
true
ifself
is numerically greater than or equal toother
:. -
#arg ⇒ 0, Math::PI
Returns 0 if
self
is positive, Math::PI otherwise. -
#arg ⇒ 0, Math::PI
Returns 0 if
self
is positive, Math::PI otherwise. -
#ceil(*args) ⇒ Object
:markup: markdown.
-
#coerce(other) ⇒ Array
Returns a 2-element array containing
other
converted to a Float andself
:. -
#denominator ⇒ Integer
Returns the denominator (always positive).
-
#divmod(other) ⇒ Array
Returns a 2-element array
[q, r]
, where. - #eql? ⇒ Boolean
-
#quo(other) ⇒ Numeric
Returns the quotient from dividing
self
byother
:. -
#finite? ⇒ Boolean
Returns
true
ifself
is notInfinity
,-Infinity
, orNaN
,false
otherwise:. -
#floor(*args) ⇒ Object
:markup: markdown.
-
#hash ⇒ Integer
Returns the integer hash value for
self
. -
#infinite? ⇒ -1, ...
Returns:.
-
#%(other) ⇒ Float
Returns
self
moduloother
as a float. -
#nan? ⇒ Boolean
Returns
true
ifself
is a NaN,false
otherwise. -
#next_float ⇒ Float
Returns the next-larger representable Float.
-
#numerator ⇒ Integer
Returns the numerator.
-
#arg ⇒ 0, Math::PI
Returns 0 if
self
is positive, Math::PI otherwise. -
#prev_float ⇒ Float
Returns the next-smaller representable Float.
-
#quo(other) ⇒ Numeric
Returns the quotient from dividing
self
byother
:. -
#rationalize([eps]) ⇒ Object
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|).
-
#round(ndigits = 0, half: :up) ⇒ Integer, Float
Returns
self
rounded to the nearest value with a precision ofndigits
decimal digits. -
#to_i ⇒ Integer
Returns
self
truncated to an Integer. -
#to_i ⇒ Integer
Returns
self
truncated to an Integer. -
#to_r ⇒ Object
Returns the value as a rational.
-
#to_s ⇒ String
(also: #inspect)
Returns a string containing a representation of
self
; depending of the value ofself
, the string representation may contain:. -
#truncate(ndigits = 0) ⇒ Float, Integer
Returns
self
truncated (toward zero) to a precision ofndigits
decimal digits.
Methods inherited from Numeric
#-@, #abs, #abs2, #clone, #div, #i, #magnitude, #negative?, #nonzero?, #polar, #positive?, #rect, #rectangular, #remainder, #singleton_method_added, #step, #to_c, #zero?
Methods included from Comparable
Instance Method Details
#%(other) ⇒ Float
Returns self
modulo other
as a float.
For float f
and real number r
, these expressions are equivalent:
f % r
f-r*(f/r).floor
f.divmod(r)[1]
See Numeric#divmod.
Examples:
10.0 % 2 # => 0.0
10.0 % 3 # => 1.0
10.0 % 4 # => 2.0
10.0 % -2 # => 0.0
10.0 % -3 # => -2.0
10.0 % -4 # => -2.0
10.0 % 4.0 # => 2.0
10.0 % Rational(4, 1) # => 2.0
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 |
# File 'numeric.c', line 1386
static VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (FIXNUM_P(y)) {
fy = (double)FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
fy = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}
|
#*(other) ⇒ Numeric
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
# File 'numeric.c', line 1207
VALUE
rb_float_mul(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
}
else if (RB_BIGNUM_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
}
else if (RB_FLOAT_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '*');
}
}
|
#**(other) ⇒ Numeric
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 |
# File 'numeric.c', line 1480
VALUE
rb_float_pow(VALUE x, VALUE y)
{
double dx, dy;
if (y == INT2FIX(2)) {
dx = RFLOAT_VALUE(x);
return DBL2NUM(dx * dx);
}
else if (FIXNUM_P(y)) {
dx = RFLOAT_VALUE(x);
dy = (double)FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
dx = RFLOAT_VALUE(x);
dy = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
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));
}
|
#+(other) ⇒ Numeric
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'numeric.c', line 1146
VALUE
rb_float_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
}
else if (RB_BIGNUM_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
}
else if (RB_FLOAT_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '+');
}
}
|
#-(other) ⇒ Numeric
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 |
# File 'numeric.c', line 1177
VALUE
rb_float_minus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
}
else if (RB_BIGNUM_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
}
else if (RB_FLOAT_TYPE_P(y)) {
return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '-');
}
}
|
#/(other) ⇒ Numeric
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 |
# File 'numeric.c', line 1262
VALUE
rb_float_div(VALUE x, VALUE y)
{
double num = RFLOAT_VALUE(x);
double den;
double ret;
if (FIXNUM_P(y)) {
den = FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
den = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
den = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '/');
}
ret = double_div_double(num, den);
return DBL2NUM(ret);
}
|
#<(other) ⇒ Boolean
Returns true
if self
is numerically less than other
:
2.0 < 3 # => true
2.0 < 3.0 # => true
2.0 < Rational(3, 1) # => true
2.0 < 2.0 # => false
Float::NAN < Float::NAN
returns an implementation-dependent value.
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 |
# File 'numeric.c', line 1808
static VALUE
flo_lt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_INTEGER_TYPE_P(y)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return RBOOL(-FIX2LONG(rel) < 0);
return Qfalse;
}
else if (RB_FLOAT_TYPE_P(y)) {
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 RBOOL(a < b);
}
|
#<=(other) ⇒ Boolean
Returns true
if self
is numerically less than or equal to other
:
2.0 <= 3 # => true
2.0 <= 3.0 # => true
2.0 <= Rational(3, 1) # => true
2.0 <= 2.0 # => true
2.0 <= 1.0 # => false
Float::NAN <= Float::NAN
returns an implementation-dependent value.
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 |
# File 'numeric.c', line 1851
static VALUE
flo_le(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_INTEGER_TYPE_P(y)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return RBOOL(-FIX2LONG(rel) <= 0);
return Qfalse;
}
else if (RB_FLOAT_TYPE_P(y)) {
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 RBOOL(a <= b);
}
|
#<=>(other) ⇒ -1, ...
Returns a value that depends on the numeric relation between self
and other
:
-
-1, if
self
is less thanother
. -
0, if
self
is equal toother
. -
1, if
self
is greater thanother
. -
nil
, if the two values are incommensurate.
Examples:
2.0 <=> 2 # => 0
2.0 <=> 2.0 # => 0
2.0 <=> Rational(2, 1) # => 0
2.0 <=> Complex(2, 0) # => 0
2.0 <=> 1.9 # => 1
2.0 <=> 2.1 # => -1
2.0 <=> 'foo' # => nil
This is the basis for the tests in the Comparable module.
Float::NAN <=> Float::NAN
returns an implementation-dependent value.
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 |
# File 'numeric.c', line 1670
static VALUE
flo_cmp(VALUE x, VALUE y)
{
double a, b;
VALUE i;
a = RFLOAT_VALUE(x);
if (isnan(a)) return Qnil;
if (RB_INTEGER_TYPE_P(y)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return LONG2FIX(-FIX2LONG(rel));
return rel;
}
else if (RB_FLOAT_TYPE_P(y)) {
b = RFLOAT_VALUE(y);
}
else {
if (isinf(a) && !UNDEF_P(i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0))) {
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);
}
|
#== ⇒ Object
#=== ⇒ Object
#>(other) ⇒ Boolean
Returns true
if self
is numerically greater than other
:
2.0 > 1 # => true
2.0 > 1.0 # => true
2.0 > Rational(1, 2) # => true
2.0 > 2.0 # => false
Float::NAN > Float::NAN
returns an implementation-dependent value.
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 |
# File 'numeric.c', line 1723
VALUE
rb_float_gt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_INTEGER_TYPE_P(y)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return RBOOL(-FIX2LONG(rel) > 0);
return Qfalse;
}
else if (RB_FLOAT_TYPE_P(y)) {
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 RBOOL(a > b);
}
|
#>=(other) ⇒ Boolean
Returns true
if self
is numerically greater than or equal to other
:
2.0 >= 1 # => true
2.0 >= 1.0 # => true
2.0 >= Rational(1, 2) # => true
2.0 >= 2.0 # => true
2.0 >= 2.1 # => false
Float::NAN >= Float::NAN
returns an implementation-dependent value.
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 |
# File 'numeric.c', line 1766
static VALUE
flo_ge(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_BIGNUM_TYPE_P(y)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return RBOOL(-FIX2LONG(rel) >= 0);
return Qfalse;
}
else if (RB_FLOAT_TYPE_P(y)) {
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 RBOOL(a >= b);
}
|
#arg ⇒ 0, Math::PI
Returns 0 if self
is positive, Math::PI otherwise.
2453 2454 2455 2456 2457 2458 2459 2460 2461 |
# File 'complex.c', line 2453
static VALUE
float_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);
}
|
#arg ⇒ 0, Math::PI
Returns 0 if self
is positive, Math::PI otherwise.
2453 2454 2455 2456 2457 2458 2459 2460 2461 |
# File 'complex.c', line 2453
static VALUE
float_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);
}
|
#ceil(*args) ⇒ Object
:markup: markdown
call-seq:
ceil(ndigits = 0) -> float or integer
Returns a numeric that is a “ceiling” value for ‘self`, as specified by the given `ndigits`, which must be an [integer-convertible object](implicit_conversion.rdoc@Integer-Convertible+Objects).
When ‘ndigits` is positive, returns a Float with `ndigits` decimal digits after the decimal point (as available, but no fewer than 1):
“‘ f = 12345.6789 f.ceil(1) # => 12345.7 f.ceil(3) # => 12345.679 f.ceil(30) # => 12345.6789 f = -12345.6789 f.ceil(1) # => -12345.6 f.ceil(3) # => -12345.678 f.ceil(30) # => -12345.6789 f = 0.0 f.ceil(1) # => 0.0 f.ceil(100) # => 0.0 “`
When ‘ndigits` is non-positive, returns an Integer based on a computed granularity:
-
The granularity is ‘10 ** ndigits.abs`.
-
The returned value is the largest multiple of the granularity that is less than or equal to ‘self`.
Examples with positive ‘self`:
| ndigits | Granularity | 12345.6789.ceil(ndigits) | |——–:|————:|————————-:| | 0 | 1 | 12346 | | -1 | 10 | 12350 | | -2 | 100 | 12400 | | -3 | 1000 | 13000 | | -4 | 10000 | 20000 | | -5 | 100000 | 100000 |
Examples with negative ‘self`:
| ndigits | Granularity | -12345.6789.ceil(ndigits) | |——–:|————:|————————–:| | 0 | 1 | -12345 | | -1 | 10 | -12340 | | -2 | 100 | -12300 | | -3 | 1000 | -12000 | | -4 | 10000 | -10000 | | -5 | 100000 | 0 |
When ‘self` is zero and `ndigits` is non-positive, returns Integer zero:
“‘ 0.0.ceil(0) # => 0 0.0.ceil(-1) # => 0 0.0.ceil(-2) # => 0 “`
Note that the limited precision of floating-point arithmetic may lead to surprising results:
“‘ (2.1 / 0.7).ceil #=> 4 # Not 3 (because 2.1 / 0.7 # => 3.0000000000000004, not 3.0) “`
Related: Float#floor.
2301 2302 2303 2304 2305 2306 |
# File 'numeric.c', line 2301
static VALUE
flo_ceil(int argc, VALUE *argv, VALUE num)
{
int ndigits = flo_ndigits(argc, argv);
return rb_float_ceil(num, ndigits);
}
|
#coerce(other) ⇒ Array
1120 1121 1122 1123 1124 |
# File 'numeric.c', line 1120
static VALUE
flo_coerce(VALUE x, VALUE y)
{
return rb_assoc_new(rb_Float(y), x);
}
|
#denominator ⇒ Integer
Returns the denominator (always positive). The result is machine dependent.
See also Float#numerator.
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 |
# File 'rational.c', line 2099
VALUE
rb_float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (!isfinite(d))
return INT2FIX(1);
r = float_to_r(self);
return nurat_denominator(r);
}
|
#divmod(other) ⇒ Array
Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient
r = self % other # Remainder
Examples:
11.0.divmod(4) # => [2, 3.0]
11.0.divmod(-4) # => [-3, -1.0]
-11.0.divmod(4) # => [-3, 1.0]
-11.0.divmod(-4) # => [2, -3.0]
12.0.divmod(4) # => [3, 0.0]
12.0.divmod(-4) # => [-3, 0.0]
-12.0.divmod(4) # => [-3, -0.0]
-12.0.divmod(-4) # => [3, -0.0]
13.0.divmod(4.0) # => [3, 1.0]
13.0.divmod(Rational(4, 1)) # => [3, 1.0]
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 |
# File 'numeric.c', line 1441
static VALUE
flo_divmod(VALUE x, VALUE y)
{
double fy, div, mod;
volatile VALUE a, b;
if (FIXNUM_P(y)) {
fy = (double)FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
fy = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
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);
}
|
#eql? ⇒ Boolean
#quo(other) ⇒ Numeric
1300 1301 1302 1303 1304 |
# File 'numeric.c', line 1300
static VALUE
flo_quo(VALUE x, VALUE y)
{
return num_funcall1(x, '/', y);
}
|
#finite? ⇒ Boolean
Returns true
if self
is not Infinity
, -Infinity
, or NaN
, false
otherwise:
f = 2.0 # => 2.0
f.finite? # => true
f = 1.0/0.0 # => Infinity
f.finite? # => false
f = -1.0/0.0 # => -Infinity
f.finite? # => false
f = 0.0/0.0 # => NaN
f.finite? # => false
1992 1993 1994 1995 1996 1997 1998 |
# File 'numeric.c', line 1992
VALUE
rb_flo_is_finite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
return RBOOL(isfinite(value));
}
|
#floor(*args) ⇒ Object
:markup: markdown
call-seq:
floor(ndigits = 0) -> float or integer
Returns a float or integer that is a “floor” value for ‘self`, as specified by `ndigits`, which must be an [integer-convertible object](implicit_conversion.rdoc@Integer-Convertible+Objects).
When ‘self` is zero, returns a zero value: a float if `ndigits` is positive, an integer otherwise:
“‘ f = 0.0 # => 0.0 f.floor(20) # => 0.0 f.floor(0) # => 0 f.floor(-20) # => 0 “`
When ‘self` is non-zero and `ndigits` is positive, returns a float with `ndigits` digits after the decimal point (as available):
“‘ f = 12345.6789 f.floor(1) # => 12345.6 f.floor(3) # => 12345.678 f.floor(30) # => 12345.6789 f = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679 f.floor(30) # => -12345.6789 “`
When ‘self` is non-zero and `ndigits` is non-positive, returns an integer value based on a computed granularity:
-
The granularity is ‘10 ** ndigits.abs`.
-
The returned value is the largest multiple of the granularity that is less than or equal to ‘self`.
Examples with positive ‘self`:
| ndigits | Granularity | 12345.6789.floor(ndigits) | |——–:|————:|————————–:| | 0 | 1 | 12345 | | -1 | 10 | 12340 | | -2 | 100 | 12300 | | -3 | 1000 | 12000 | | -4 | 10000 | 10000 | | -5 | 100000 | 0 |
Examples with negative ‘self`:
| ndigits | Granularity | -12345.6789.floor(ndigits) | |——–:|————:|—————————:| | 0 | 1 | -12346 | | -1 | 10 | -12350 | | -2 | 100 | -12400 | | -3 | 1000 | -13000 | | -4 | 10000 | -20000 | | -5 | 100000 | -100000 | | -6 | 1000000 | -1000000 |
Note that the limited precision of floating-point arithmetic may lead to surprising results:
“‘ (0.3 / 0.1).floor # => 2 # Not 3, (because (0.3 / 0.1) # => 2.9999999999999996, not 3.0) “`
Related: Float#ceil.
2216 2217 2218 2219 2220 2221 |
# File 'numeric.c', line 2216
static VALUE
flo_floor(int argc, VALUE *argv, VALUE num)
{
int ndigits = flo_ndigits(argc, argv);
return rb_float_floor(num, ndigits);
}
|
#hash ⇒ Integer
Returns the integer hash value for self
.
See also Object#hash.
1620 1621 1622 1623 1624 |
# File 'numeric.c', line 1620
static VALUE
flo_hash(VALUE num)
{
return rb_dbl_hash(RFLOAT_VALUE(num));
}
|
#infinite? ⇒ -1, ...
Returns:
-
1, if
self
isInfinity
. -
-1 if
self
is-Infinity
. -
nil
, otherwise.
Examples:
f = 1.0/0.0 # => Infinity
f.infinite? # => 1
f = -1.0/0.0 # => -Infinity
f.infinite? # => -1
f = 1.0 # => 1.0
f.infinite? # => nil
f = 0.0/0.0 # => NaN
f.infinite? # => nil
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 |
# File 'numeric.c', line 1962
VALUE
rb_flo_is_infinite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
if (isinf(value)) {
return INT2FIX( value < 0 ? -1 : 1 );
}
return Qnil;
}
|
#%(other) ⇒ Float
Returns self
modulo other
as a float.
For float f
and real number r
, these expressions are equivalent:
f % r
f-r*(f/r).floor
f.divmod(r)[1]
See Numeric#divmod.
Examples:
10.0 % 2 # => 0.0
10.0 % 3 # => 1.0
10.0 % 4 # => 2.0
10.0 % -2 # => 0.0
10.0 % -3 # => -2.0
10.0 % -4 # => -2.0
10.0 % 4.0 # => 2.0
10.0 % Rational(4, 1) # => 2.0
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 |
# File 'numeric.c', line 1386
static VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (FIXNUM_P(y)) {
fy = (double)FIX2LONG(y);
}
else if (RB_BIGNUM_TYPE_P(y)) {
fy = rb_big2dbl(y);
}
else if (RB_FLOAT_TYPE_P(y)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}
|
#nan? ⇒ Boolean
Returns true
if self
is a NaN, false
otherwise.
f = -1.0 #=> -1.0
f.nan? #=> false
f = 0.0/0.0 #=> NaN
f.nan? #=> true
1931 1932 1933 1934 1935 1936 1937 |
# File 'numeric.c', line 1931
static VALUE
flo_is_nan_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
return RBOOL(isnan(value));
}
|
#next_float ⇒ Float
Returns the next-larger representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.next_float
:
f = 0.0 # 0x0000000000000000
f.next_float # 0x0000000000000001
f = 0.01 # 0x3f847ae147ae147b
f.next_float # 0x3f847ae147ae147c
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.next_float # => 0.010000000000000002
1.0.next_float # => 1.0000000000000002
100.0.next_float # => 100.00000000000001
f = 0.01
(0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.next_float }
Output:
0 0x1.47ae147ae147bp-7 0.01
1 0x1.47ae147ae147cp-7 0.010000000000000002
2 0x1.47ae147ae147dp-7 0.010000000000000004
3 0x1.47ae147ae147ep-7 0.010000000000000005
f = 0.0; 100.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.
Related: Float#prev_float
2053 2054 2055 2056 2057 |
# File 'numeric.c', line 2053
static VALUE
flo_next_float(VALUE vx)
{
return flo_nextafter(vx, HUGE_VAL);
}
|
#numerator ⇒ Integer
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595
d = 0.3.denominator #=> 18014398509481984
n.fdiv(d) #=> 0.3
See also Float#denominator.
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 |
# File 'rational.c', line 2079
VALUE
rb_float_numerator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (!isfinite(d))
return self;
r = float_to_r(self);
return nurat_numerator(r);
}
|
#arg ⇒ 0, Math::PI
Returns 0 if self
is positive, Math::PI otherwise.
2453 2454 2455 2456 2457 2458 2459 2460 2461 |
# File 'complex.c', line 2453
static VALUE
float_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);
}
|
#prev_float ⇒ Float
Returns the next-smaller representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f
and for the corresponding f.pev_float
:
f = 5e-324 # 0x0000000000000001
f.prev_float # 0x0000000000000000
f = 0.01 # 0x3f847ae147ae147b
f.prev_float # 0x3f847ae147ae147a
In the remaining examples here, the output is shown in the usual way (result to_s
):
0.01.prev_float # => 0.009999999999999998
1.0.prev_float # => 0.9999999999999999
100.0.prev_float # => 99.99999999999999
f = 0.01
(0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.prev_float }
Output:
0 0x1.47ae147ae147bp-7 0.01
1 0x1.47ae147ae147ap-7 0.009999999999999998
2 0x1.47ae147ae1479p-7 0.009999999999999997
3 0x1.47ae147ae1478p-7 0.009999999999999995
Related: Float#next_float.
2094 2095 2096 2097 2098 |
# File 'numeric.c', line 2094
static VALUE
flo_prev_float(VALUE vx)
{
return flo_nextafter(vx, -HUGE_VAL);
}
|
#quo(other) ⇒ Numeric
1300 1301 1302 1303 1304 |
# File 'numeric.c', line 1300
static VALUE
flo_quo(VALUE x, VALUE y)
{
return num_funcall1(x, '/', y);
}
|
#rationalize([eps]) ⇒ Object
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10)
1.333.rationalize #=> (1333/1000)
1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r.
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 |
# File 'rational.c', line 2288
static VALUE
float_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;
}
|
#round(ndigits = 0, half: :up) ⇒ Integer, Float
Returns self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is non-negative, returns a float with ndigits
after the decimal point (as available):
f = 12345.6789
f.round(1) # => 12345.7
f.round(3) # => 12345.679
f = -12345.6789
f.round(1) # => -12345.7
f.round(3) # => -12345.679
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789
f.round(0) # => 12346
f.round(-3) # => 12000
f = -12345.6789
f.round(0) # => -12346
f.round(-3) # => -12000
If keyword argument half
is given, and self
is equidistant from the two candidate values, the rounding is according to the given half
value:
-
:up
ornil
: round away from zero:2.5.round(half: :up) # => 3 3.5.round(half: :up) # => 4 (-2.5).round(half: :up) # => -3
-
:down
: round toward zero:2.5.round(half: :down) # => 2 3.5.round(half: :down) # => 3 (-2.5).round(half: :down) # => -2
-
:even
: round toward the candidate whose last nonzero digit is even:2.5.round(half: :even) # => 2 3.5.round(half: :even) # => 4 (-2.5).round(half: :even) # => -2
Raises and exception if the value for half
is invalid.
Related: Float#truncate.
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 |
# File 'numeric.c', line 2559
static VALUE
flo_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);
if (ndigits > 14) {
/* In this case, pow(10, ndigits) may not be accurate. */
return rb_flo_round_by_rational(argc, argv, num);
}
f = pow(10, ndigits);
x = ROUND_CALL(mode, round, (number, f));
return DBL2NUM(x / f);
}
return num;
}
|
#to_i ⇒ Integer
Returns self
truncated to an Integer.
1.2.to_i # => 1
(-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 |
# File 'numeric.c', line 2651
static VALUE
flo_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);
}
|
#to_i ⇒ Integer
Returns self
truncated to an Integer.
1.2.to_i # => 1
(-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 |
# File 'numeric.c', line 2651
static VALUE
flo_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);
}
|
#to_r ⇒ Object
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 equivalent to “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 Float#rationalize.
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 |
# File 'rational.c', line 2203
static VALUE
float_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
}
|
#to_s ⇒ String Also known as: inspect
Returns a string containing a representation of self
; depending of the value of self
, the string representation may contain:
-
A fixed-point number.
3.14.to_s # => "3.14"
-
A number in “scientific notation” (containing an exponent).
(10.1**50).to_s # => "1.644631821843879e+50"
-
‘Infinity’.
(10.1**500).to_s # => "Infinity"
-
‘-Infinity’.
(-10.1**500).to_s # => "-Infinity"
-
‘NaN’ (indicating not-a-number).
(0.0/0.0).to_s # => "NaN"
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'numeric.c', line 1029
static VALUE
flo_to_s(VALUE flt)
{
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
enum {float_dig = DBL_DIG+1};
char buf[float_dig + roomof(decimal_mant, 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);
free(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;
}
|
#truncate(ndigits = 0) ⇒ Float, Integer
Returns self
truncated (toward zero) to a precision of ndigits
decimal digits.
When ndigits
is positive, returns a float with ndigits
digits after the decimal point (as available):
f = 12345.6789
f.truncate(1) # => 12345.6
f.truncate(3) # => 12345.678
f = -12345.6789
f.truncate(1) # => -12345.6
f.truncate(3) # => -12345.678
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789
f.truncate(0) # => 12345
f.truncate(-3) # => 12000
f = -12345.6789
f.truncate(0) # => -12345
f.truncate(-3) # => -12000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
Related: Float#round.
2697 2698 2699 2700 2701 2702 2703 2704 |
# File 'numeric.c', line 2697
static VALUE
flo_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);
}
|