Float
objects represent real numbers using the native architecture's double-precision floating point representation.
flt | + |
aNumeric | Addition |
flt | - |
aNumeric | Subtraction |
flt | * |
aNumeric | Multiplication |
flt | / |
aNumeric | Division |
flt | % |
aNumeric | Modulo |
flt | ** |
aNumeric | Exponentiation |
Comparable
. Integer
greater than or equal to flt.
1.2.ceil → 2
2.0.ceil → 2
(-1.2).ceil → -1
(-2.0).ceil → -2
true
or false
true
if flt is a valid IEEE floating point number (it is not infinite, and nan?
is false
). 1.2.floor → 1
2.0.floor → 2
(-1.2).floor → -2
(-2.0).floor → -2
nil
, -1, +1nil
, -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
true
or false
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
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
Integer
.
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.