On this page
End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Float
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:
You can create a Float object explicitly with:
You can convert certain objects to Floats with:
-
Method Float.
What’s Here
First, what’s elsewhere. Class Float:
-
Inherits from class Numeric.
Here, class Float provides methods for:
Querying
finite?-
Returns whether
selfis finite.
hash-
Returns the integer hash code for
self.
infinite?-
Returns whether
selfis infinite.
nan?-
Returns whether
selfis a NaN (not-a-number).
Comparing
- <
-
Returns whether
selfis less than the given value.
- <=
-
Returns whether
selfis less than or equal to the given value.
- <=>
-
Returns a number indicating whether
selfis less than, equal to, or greater than the given value.
- >
-
Returns whether
selfis greater than the given value.
- >=
-
Returns whether
selfis greater than or equal to the given value.
Converting
*-
Returns the product of
selfand the given value.
- **
-
Returns the value of
selfraised to the power of the given value.
+-
Returns the sum of
selfand the given value.
--
Returns the difference of
selfand the given value.
- /
-
Returns the quotient of
selfand 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
selfby 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
selfby the given value.
round-
Returns
selfrounded to the nearest value, to a given precision.
truncate-
Returns
selftruncated to a given precision.
Instance methods 48
The minimum number of significant decimal digits in a double-precision floating point.
Usually defaults to 15.
The difference between 1 and the smallest double-precision floating point number greater than 1.
Usually defaults to 2.2204460492503131e-16.
An expression representing positive infinity.
The number of base digits for the double 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 10 raised 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 floating point.
Usually defaults to 2.2250738585072014e-308.
If the platform supports denormalized numbers, there are numbers between zero and Float::MIN. 0.0.next_float returns the smallest positive floating point number including denormalized numbers.
The smallest negative exponent in a double-precision floating point where 10 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 to represent the number.
Usually defaults to 2 on most systems, which would represent a base-10 decimal.