Class: Float
- Includes:
- Precision
- Defined in:
- numeric.c,
numeric.c
Overview
******************************************************************
<code>Float</code> objects represent real numbers using the native
architecture's double-precision floating point representation.
Constant Summary collapse
- ROUNDS =
INT2FIX(FLT_ROUNDS)
- RADIX =
INT2FIX(FLT_RADIX)
- MANT_DIG =
INT2FIX(DBL_MANT_DIG)
- DIG =
INT2FIX(DBL_DIG)
- MIN_EXP =
INT2FIX(DBL_MIN_EXP)
- MAX_EXP =
INT2FIX(DBL_MAX_EXP)
- MIN_10_EXP =
INT2FIX(DBL_MIN_10_EXP)
- MAX_10_EXP =
INT2FIX(DBL_MAX_10_EXP)
- MIN =
rb_float_new(DBL_MIN)
- MAX =
rb_float_new(DBL_MAX)
- EPSILON =
rb_float_new(DBL_EPSILON)
Class Method Summary collapse
-
.induced_from(obj) ⇒ Float
Convert
obj
to a float.
Instance Method Summary collapse
-
#% ⇒ Object
Return the modulo after division of
flt
byother
. -
#*(other) ⇒ Float
Returns a new float which is the product of
float
andother
. -
#**(other) ⇒ Float
Raises
float
theother
power. -
#+(other) ⇒ Float
Returns a new float which is the sum of
float
andother
. -
#+(other) ⇒ Float
Returns a new float which is the difference of
float
andother
. -
#- ⇒ Float
Returns float, negated.
-
#/(other) ⇒ Float
Returns a new float which is the result of dividing
float
byother
. -
#<(other) ⇒ Boolean
true
ifflt
is less thanother
. -
#<=(other) ⇒ Boolean
true
ifflt
is less than or equal toother
. -
#<=>(numeric) ⇒ -1, ...
Returns -1, 0, or +1 depending on whether flt is less than, equal to, or greater than numeric.
-
#==(obj) ⇒ Boolean
Returns
true
only if obj has the same value as flt. -
#>(other) ⇒ Boolean
true
ifflt
is greater thanother
. -
#>=(other) ⇒ Boolean
true
ifflt
is greater than or equal toother
. -
#abs ⇒ Float
Returns the absolute value of flt.
-
#ceil ⇒ Integer
Returns the smallest
Integer
greater than or equal to flt. -
#coerce ⇒ Object
MISSING: documentation.
-
#divmod(numeric) ⇒ Array
See
Numeric#divmod
. -
#eql?(obj) ⇒ Boolean
Returns
true
only if obj is aFloat
with the same value as flt. -
#finite? ⇒ Boolean
Returns
true
if flt is a valid IEEE floating point number (it is not infinite, andnan?
isfalse
). -
#floor ⇒ Integer
Returns the largest integer less than or equal to flt.
-
#hash ⇒ Integer
Returns a hash code for this float.
-
#infinite? ⇒ nil, ...
Returns
nil
, -1, or 1 depending on whether flt is finite, -infinity, or infinity. -
#modulo ⇒ Object
Return the modulo after division of
flt
byother
. -
#nan? ⇒ Boolean
Returns
true
if flt is an invalid IEEE floating point number. -
#round ⇒ Integer
Rounds flt to the nearest integer.
-
#to_f ⇒ Object
As
flt
is already a float, returns self. -
#to_i ⇒ Object
Returns flt truncated to an
Integer
. -
#to_int ⇒ Object
Returns flt truncated to an
Integer
. -
#to_s ⇒ String
Returns a string containing a representation of self.
-
#truncate ⇒ Object
Returns flt truncated to an
Integer
. -
#zero? ⇒ Boolean
Returns
true
if flt is 0.0.
Methods included from Precision
included, #prec, #prec_f, #prec_i
Methods inherited from Numeric
#+@, #div, #initialize_copy, #integer?, #nonzero?, #quo, #remainder, #singleton_method_added, #step
Methods included from Comparable
Class Method Details
.induced_from(obj) ⇒ Float
Convert obj
to a float.
1873 1874 1875 |
# File 'numeric.c', line 1873 static VALUE rb_flo_induced_from(klass, x) VALUE klass, x; |
Instance Method Details
#%(other) ⇒ Float #modulo(other) ⇒ Float
Return the modulo after division of flt
by other
.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
686 687 688 |
# File 'numeric.c', line 686 static VALUE flo_mod(x, y) VALUE x, y; |
#*(other) ⇒ Float
Returns a new float which is the product of float
and other
.
602 603 604 |
# File 'numeric.c', line 602 static VALUE flo_mul(x, y) VALUE x, y; |
#**(other) ⇒ Float
Raises float
the other
power.
756 757 758 |
# File 'numeric.c', line 756 static VALUE flo_pow(x, y) VALUE x, y; |
#+(other) ⇒ Float
Returns a new float which is the sum of float
and other
.
554 555 556 |
# File 'numeric.c', line 554 static VALUE flo_plus(x, y) VALUE x, y; |
#+(other) ⇒ Float
Returns a new float which is the difference of float
and other
.
578 579 580 |
# File 'numeric.c', line 578 static VALUE flo_minus(x, y) VALUE x, y; |
#- ⇒ Float
Returns float, negated.
539 540 541 |
# File 'numeric.c', line 539 static VALUE flo_uminus(flt) VALUE flt; |
#/(other) ⇒ Float
Returns a new float which is the result of dividing float
by other
.
626 627 628 |
# File 'numeric.c', line 626 static VALUE flo_div(x, y) VALUE x, y; |
#<(other) ⇒ Boolean
true
if flt
is less than other
.
1003 1004 1005 |
# File 'numeric.c', line 1003 static VALUE flo_lt(x, y) VALUE x, y; |
#<=(other) ⇒ Boolean
true
if flt
is less than or equal to other
.
1039 1040 1041 |
# File 'numeric.c', line 1039 static VALUE flo_le(x, y) VALUE x, y; |
#<=>(numeric) ⇒ -1, ...
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
.
899 900 901 |
# File 'numeric.c', line 899 static VALUE flo_cmp(x, y) VALUE x, y; |
#==(obj) ⇒ Boolean
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
829 830 831 |
# File 'numeric.c', line 829 static VALUE flo_eq(x, y) VALUE x, y; |
#>(other) ⇒ Boolean
true
if flt
is greater than other
.
932 933 934 |
# File 'numeric.c', line 932 static VALUE flo_gt(x, y) VALUE x, y; |
#>=(other) ⇒ Boolean
true
if flt
is greater than or equal to other
.
968 969 970 |
# File 'numeric.c', line 968 static VALUE flo_ge(x, y) VALUE x, y; |
#abs ⇒ Float
Returns the absolute value of flt.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
1117 1118 1119 |
# File 'numeric.c', line 1117 static VALUE flo_abs(flt) VALUE flt; |
#ceil ⇒ Integer
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
1256 1257 1258 |
# File 'numeric.c', line 1256 static VALUE flo_ceil(num) VALUE num; |
#coerce ⇒ Object
MISSING: documentation
525 526 527 |
# File 'numeric.c', line 525 static VALUE flo_coerce(x, y) VALUE x, y; |
#divmod(numeric) ⇒ Array
See Numeric#divmod
.
716 717 718 |
# File 'numeric.c', line 716 static VALUE flo_divmod(x, y) VALUE x, y; |
#eql?(obj) ⇒ Boolean
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
1078 1079 1080 |
# File 'numeric.c', line 1078 static VALUE flo_eql(x, y) VALUE x, y; |
#finite? ⇒ Boolean
Returns true
if flt is a valid IEEE floating point number (it is not infinite, and nan?
is false
).
1200 1201 1202 |
# File 'numeric.c', line 1200 static VALUE flo_is_finite_p(num) VALUE num; |
#floor ⇒ Integer
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
1229 1230 1231 |
# File 'numeric.c', line 1229 static VALUE flo_floor(num) VALUE num; |
#hash ⇒ Integer
Returns a hash code for this float.
861 862 863 |
# File 'numeric.c', line 861 static VALUE flo_hash(num) VALUE num; |
#infinite? ⇒ nil, ...
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
1177 1178 1179 |
# File 'numeric.c', line 1177 static VALUE flo_is_infinite_p(num) VALUE num; |
#%(other) ⇒ Float #modulo(other) ⇒ Float
Return the modulo after division of flt
by other
.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
686 687 688 |
# File 'numeric.c', line 686 static VALUE flo_mod(x, y) VALUE x, y; |
#nan? ⇒ Boolean
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
1156 1157 1158 |
# File 'numeric.c', line 1156 static VALUE flo_is_nan_p(num) VALUE num; |
#round ⇒ Integer
Rounds flt to the nearest integer. Equivalent to:
def round
return (self+0.5).floor if self > 0.0
return (self-0.5).ceil if self < 0.0
return 0
end
1.5.round #=> 2
(-1.5).round #=> -2
1287 1288 1289 |
# File 'numeric.c', line 1287 static VALUE flo_round(num) VALUE num; |
#to_f ⇒ Object
As flt
is already a float, returns self.
1099 1100 1101 |
# File 'numeric.c', line 1099 static VALUE flo_to_f(num) VALUE num; |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
1313 1314 1315 |
# File 'numeric.c', line 1313 static VALUE flo_truncate(num) VALUE num; |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
1313 1314 1315 |
# File 'numeric.c', line 1313 static VALUE flo_truncate(num) VALUE num; |
#to_s ⇒ String
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
”.
491 492 493 |
# File 'numeric.c', line 491 static VALUE flo_to_s(flt) VALUE flt; |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
1313 1314 1315 |
# File 'numeric.c', line 1313 static VALUE flo_truncate(num) VALUE num; |
#zero? ⇒ Boolean
Returns true
if flt is 0.0.
1133 1134 1135 |
# File 'numeric.c', line 1133 static VALUE flo_zero_p(num) VALUE num; |