Programming Ruby
The Pragmatic Programmer's Guide
class Float
Parent:
Numeric
Version:
1.6
Index:
Arithmetic operations
<=>
ceil
finite?
floor
infinite?
nan?
round
to_f
to_i
to_s
Float objects represent real numbers using the native
architecture's double-precision floating point representation.
instance methods
Performs various arithmetic operations on
flt.
flt
+
aNumeric
Addition
flt
--
aNumeric
Subtraction
flt
*
aNumeric
Multiplication
flt
/
aNumeric
Division
flt
%
aNumeric
Modulo
flt
**
aNumeric
Exponentiation
<=>
flt <=>
aNumeric
-> -1, 0, +1
Returns -1, 0, or +1 depending on whether
flt is less
than, equal to, or greater than
aNumeric. This is the
basis for the tests in
Comparable.
ceil
flt.ceil ->
anInteger
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
finite?
flt.finite? ->
true or
false
Returns
true if
flt is a valid IEEE floating point
number (it is not infinite, and
nan? is
false).
floor
flt.floor ->
anInteger
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
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
nan?
flt.nan? ->
true or
false
Returns
true if
flt is an invalid IEEE
floating point number.
a = -1.0
サ
-1.0
a.nan?
サ
false
a = Math.log(a)
サ
NaN
a.nan?
サ
true
round
flt.round ->
anInteger
Rounds
flt to the nearest integer. Equivalent to:
def round
return floor(self+0.5) if self > 0.0
return ceil(self-0.5) if self < 0.0
return 0.0
end
1.5.round
サ
2
(-1.5).round
サ
-2
Returns
flt.
to_i
flt.to_i ->
anInteger
Returns
flt truncated to an
Integer.
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''.
Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"
Copyright
©
2001 by Addison Wesley Longman, Inc. This material may
be distributed only subject to the terms and conditions set forth in
the Open Publication License, v1.0 or later (the latest version is
presently available at
http://www.opencontent.org/openpub/)).
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.