On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Float
A Float object stores a real number using the native architecture’s double-precision floating-point representation.
Float Imprecisions
Some real numbers can be represented precisely as Float objects:
37.5 # => 37.5 98.75 # => 98.75 12.3125 # => 12.3125
Others cannot; among these are the transcendental numbers, including:
-
Pi, π: in mathematics, a number of infinite precision: 3.1415926535897932384626433… (to 25 places); in Ruby, it is of limited precision (in this case, to 16 decimal places):
ExampleMath::PI # => 3.141592653589793
-
Euler’s number, e: in mathematics, a number of infinite precision: 2.7182818284590452353602874… (to 25 places); in Ruby, it is of limited precision (in this case, to 15 decimal places):
ExampleMath::E # => 2.718281828459045
Some floating-point computations in Ruby give precise results:
1.0/2 # => 0.5 100.0/8 # => 12.5
Others do not:
-
In mathematics, 2/3 as a decimal number is an infinitely-repeating decimal: 0.666… (forever); in Ruby,
2.0/3is of limited precision (in this case, to 16 decimal places):Example2.0/3 # => 0.6666666666666666
-
In mathematics, the square root of 2 is an irrational number of infinite precision: 1.4142135623730950488016887… (to 25 decimal places); in Ruby, it is of limited precision (in this case, to 16 decimal places):
ExampleMath.sqrt(2.0) # => 1.4142135623730951
-
Even a simple computation can introduce imprecision:
Examplex = 0.1 + 0.2 # => 0.30000000000000004 y = 0.3 # => 0.3 x == y # => false
See:
Note that precise storage and computation of rational numbers is possible using Rational objects.
Creating a Float
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 and class Object.
-
Includes module Comparable.
Here, class Float provides methods for:
Querying
-
finite?: Returns whetherselfis finite. -
hash: Returns the integer hash code forself. -
infinite?: Returns whetherselfis infinite. -
nan?: Returns whetherselfis a NaN (not-a-number).
Comparing
-
<: Returns whetherselfis less than the given value. -
<=: Returns whetherselfis less than or equal to the given value. -
<=>: Returns a number indicating whetherselfis less than, equal to, or greater than the given value. -
==(aliased as===andeql?): Returns whetherselfis equal to the given value. -
>: Returns whetherselfis greater than the given value. -
>=: Returns whetherselfis greater than or equal to the given value.
Converting
-
*: Returns the product ofselfand the given value. -
**: Returns the value ofselfraised to the power of the given value. -
+: Returns the sum ofselfand the given value. -
-: Returns the difference ofselfand the given value. -
/: Returns the quotient ofselfand the given value. -
ceil: Returns the smallest number greater than or equal toself. -
coerce: Returns a 2-element array containing the given value converted to a Float andself -
divmod: Returns a 2-element array containing the quotient and remainder results of dividingselfby the given value. -
fdiv: Returns the Float result of dividingselfby the given value. -
floor: Returns the greatest number smaller than or equal toself. -
next_float: Returns the next-larger representable Float. -
prev_float: Returns the next-smaller representable Float. -
quo: Returns the quotient from dividingselfby the given value. -
round: Returnsselfrounded to the nearest value, to a given precision. -
to_i(aliased asto_int): Returnsselftruncated to anInteger. -
to_s(aliased asinspect): Returns a string containing the place-value representation ofselfin the given radix. -
truncate: Returnsselftruncated to a given precision.
Instance methods 47
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.