class Float
include Comparable[Float]
A double-precision (IEEE 754 binary64) floating-point number.
Unlike integer arithmetic, a Float's raises nothing: it gives an
infinity on overflow or division by zero, and NAN for an undefined
result. Converting either to an integer (to_i, floor, round)
raises RangeError. inspect and to_s write the shortest decimal
that reads back as the same Float.
0.1 + 0.2 # => 0.30000000000000004
1.0 / 0 # => Infinity
(0.0 / 0.0).nan? # => true
Float::INFINITY.to_i # raises RangeError
<prelude>:880
Constants
INFINITY: Float
Positive infinity, above every finite Float; -INFINITY is below
them all.
<prelude>:886
NAN: Float
Not a number, the result of 0.0 / 0.0: ==, <, <=, > and >=
are false with it, NAN == NAN included, so test for it with nan?.
<=> puts it after every other number, so it sorts last.
<prelude>:890
EPSILON: Float
The difference between 1.0 and the next Float above it, 2**-52.
<prelude>:892
MAX: Float
The largest finite Float; the most negative one is -MAX.
<prelude>:894
MIN: Float
The smallest positive normal Float (not the most negative one,
which is -MAX): subnormals go further down, to
0.0.next_float.
<prelude>:898
DIG: Int
The number of decimal digits a Float always keeps: 15.
<prelude>:900
Methods
def +(other: Float) -> Float
The sum.
<prelude>:903
def +(other: Int) -> Float
The sum.
<prelude>:905
def -(other: Float) -> Float
The difference.
<prelude>:907
def -(other: Int) -> Float
The difference.
<prelude>:909
def *(other: Float) -> Float
The product.
<prelude>:911
def *(other: Int) -> Float
The product.
<prelude>:913
def /(other: Float) -> Float
The quotient: dividing by zero gives INFINITY, -INFINITY or NAN
and raises nothing.
7.5 / 2 # => 3.75
-1.0 / 0 # => -Infinity
<prelude>:921
def /(other: Int) -> Float
The quotient: dividing by zero gives INFINITY, -INFINITY or NAN
and raises nothing.
7.5 / 2 # => 3.75
-1.0 / 0 # => -Infinity
<prelude>:923
def %(other: Float) -> Float
The modulo, with the sign of other as Int#%; NaN when other is
zero.
7.5 % 2 # => 1.5
-7.5 % 2 # => 0.5
7.5 % -2 # => -0.5
<prelude>:932
def %(other: Int) -> Float
The modulo, with the sign of other as Int#%; NaN when other is
zero.
7.5 % 2 # => 1.5
-7.5 % 2 # => 0.5
7.5 % -2 # => -0.5
<prelude>:934
def **(other: Float) -> Float
self raised to the power other; NaN for a negative self and a
fractional other (where Ruby gives a complex number).
<prelude>:937
def **(other: Int) -> Float
self raised to the power other; NaN for a negative self and a
fractional other (where Ruby gives a complex number).
<prelude>:939
def -@ -> Float
The negation.
<prelude>:941
def +@ -> Float
self.
<prelude>:943
def <=>(other: Float) -> Int
-1, 0 or 1 as self is less than, equal to or greater than other.
This is a total order, where Ruby gives nil for NaN: NaN comes after
every other number, infinity included, and is equal to itself
(NAN <=> NAN is 0). So sorting puts NaN last, min ignores it
unless every element is NaN, and max gives NaN when there is one.
The operators <, <=, > and >= keep IEEE 754's comparisons
instead: each is false when either side is NaN. -0.0 and 0.0 are
equal.
1.0 <=> 2 # => -1
Float::NAN <=> Float::INFINITY # => 1
Float::NAN <=> Float::NAN # => 0
[3.0, Float::NAN, 1.0].sort # => [1.0, 3.0, NaN]
[3.0, Float::NAN, 1.0].min # => 1.0
[3.0, Float::NAN, 1.0].max # => NaN
Float::NAN > 1.0 # => false
<prelude>:963
def <=>(other: Int) -> Int
-1, 0 or 1 as self is less than, equal to or greater than other.
This is a total order, where Ruby gives nil for NaN: NaN comes after
every other number, infinity included, and is equal to itself
(NAN <=> NAN is 0). So sorting puts NaN last, min ignores it
unless every element is NaN, and max gives NaN when there is one.
The operators <, <=, > and >= keep IEEE 754's comparisons
instead: each is false when either side is NaN. -0.0 and 0.0 are
equal.
1.0 <=> 2 # => -1
Float::NAN <=> Float::INFINITY # => 1
Float::NAN <=> Float::NAN # => 0
[3.0, Float::NAN, 1.0].sort # => [1.0, 3.0, NaN]
[3.0, Float::NAN, 1.0].min # => 1.0
[3.0, Float::NAN, 1.0].max # => NaN
Float::NAN > 1.0 # => false
<prelude>:965
def ==(other: Object) -> Bool
Whether other is a Float or an Int of the same value
(1.0 == 1 is true; an Integer is never equal). NaN equals
nothing, itself included; 0.0 == -0.0 is true.
<prelude>:969
def !=(other: Object) -> Bool
Whether other is not equal to self (!(self == other)): true for
NaN.
<prelude>:972
def <(other: Float) -> Bool
Whether self is less than other; false when either is NaN
(although <=> orders NaN last).
<prelude>:975
def <=(other: Float) -> Bool
Whether self is less than or equal to other; false when either is
NaN.
<prelude>:978
def >(other: Float) -> Bool
Whether self is greater than other; false when either is NaN
(although <=> orders NaN last).
<prelude>:981
def >=(other: Float) -> Bool
Whether self is greater than or equal to other; false when either
is NaN.
<prelude>:984
def <(other: Int) -> Bool
Whether self is less than other; false when self is NaN.
<prelude>:986
def <=(other: Int) -> Bool
Whether self is less than or equal to other; false when self is
NaN.
<prelude>:989
def >(other: Int) -> Bool
Whether self is greater than other; false when self is NaN.
<prelude>:991
def >=(other: Int) -> Bool
Whether self is greater than or equal to other; false when self
is NaN.
<prelude>:994
def abs -> Float
The absolute value (0.0 for -0.0).
<prelude>:996
def negative? -> Bool
Whether self is less than 0: false for -0.0 and for NaN.
<prelude>:998
def positive? -> Bool
Whether self is greater than 0: false for NaN.
<prelude>:1000
def zero? -> Bool
Whether self is 0.0 or -0.0.
<prelude>:1002
def nan? -> Bool
Whether self is NaN.
<prelude>:1004
def infinite? -> Bool
Whether self is INFINITY or -INFINITY. A Bool, where Ruby
gives 1, -1 or nil.
<prelude>:1007
def finite? -> Bool
Whether self is neither an infinity nor NaN.
<prelude>:1009
def floor -> Int
The largest Int not above self.
Raises
FloatDomainError for NaN or an infinity, OverflowError beyond the
Int range.
3.7.floor # => 3
-3.2.floor # => -4
Float::NAN.floor # raises RangeError
<prelude>:1022
def ceil -> Int
The smallest Int not below self; FloatDomainError for NaN or an
infinity, OverflowError beyond the Int range.
<prelude>:1025
def round -> Int
The nearest Int, a half away from zero (not to even);
FloatDomainError for NaN or an infinity, OverflowError beyond the
Int range.
2.5.round # => 3
3.5.round # => 4
-2.5.round # => -3
2.49.round # => 2
<prelude>:1036
def round(digits: Int) -> Float
self rounded to digits decimal places (to tens, hundreds, ... for
a negative digits), a half away from zero; NaN and the infinities
are returned as they are.
As in Ruby, places after the point are those of the decimal form
inspect shows, the shortest that reads back as self: 1.005 rounds
to 1.01, though the binary number is a little below 1.005. Tens,
hundreds, ... round the integer part exactly. The same holds for
floor, ceil and truncate with digits, whose result is never on
the wrong side of self; with more digits than self shows, self
comes back.
3.14159.round(2) # => 3.14
1.005.round(2) # => 1.01
1234.5.round(-2) # => 1200.0
<prelude>:1054
def truncate -> Int
The Int toward zero; FloatDomainError for NaN or an infinity,
OverflowError beyond the Int range.
<prelude>:1057
def floor(digits: Int) -> Float
self rounded toward negative infinity to digits decimal places
(to tens, hundreds, ... for a negative digits), as a Float: use
floor without digits for an Int. NaN and the infinities are
returned as they are.
3.14159.floor(2) # => 3.14
-3.14159.floor(2) # => -3.15
15.5.floor(0) # => 15.0
15.5.floor(-1) # => 10.0
<prelude>:1069
def ceil(digits: Int) -> Float
self rounded toward positive infinity to digits decimal places,
as a Float, as #floor(digits) rounds down: 15.5.ceil(-1) is
20.0. Use ceil without digits for an Int.
<prelude>:1073
def truncate(digits: Int) -> Float
self rounded toward zero to digits decimal places, as a Float,
as #floor(digits) rounds down: -15.5.truncate(-1) is -10.0. Use
truncate without digits for an Int.
<prelude>:1077
def divmod(other: Float) -> [Int, Float]
[(self / other).floor, self % other], the modulo with the sign of
other. Unlike /, ZeroDivisionError when other is zero.
7.5.divmod(2) # => [3, 1.5]
(-7.5).divmod(2) # => [-4, 0.5]
7.5.divmod(0.0) # raises ZeroDivisionError
<prelude>:1086
def divmod(other: Int) -> [Int, Float]
[(self / other).floor, self % other], the modulo with the sign of
other. Unlike /, ZeroDivisionError when other is zero.
7.5.divmod(2) # => [3, 1.5]
(-7.5).divmod(2) # => [-4, 0.5]
7.5.divmod(0.0) # raises ZeroDivisionError
<prelude>:1088
def fdiv(other: Float) -> Float
The quotient, as /.
<prelude>:1090
def fdiv(other: Int) -> Float
The quotient, as /.
<prelude>:1092
def next_float -> Float
The next Float above self: INFINITY after MAX, the smallest
subnormal after 0.0.
1.0.next_float # => 1.0000000000000002
<prelude>:1099
def prev_float -> Float
The next Float below self.
<prelude>:1101
def step(limit: Float, step: Float, &: (Float) -> Void) -> Void
Calls the block with self, self + step, self + 2 * step and so on
while not past limit (above it for a positive step, below it for a
negative one); ArgumentError when step is 0.0. The number of steps
is counted first, as in Ruby, so that rounding errors do not lose the
last one: 0.0.step(1.0, 0.1) calls the block 11 times.
<prelude>:1107
def integer? -> Bool
False: a Float is not an integer, even a whole one.
<prelude>:1109
def to_i -> Int
The Int toward zero (-3.9.to_i is -3); FloatDomainError for NaN
or an infinity, OverflowError beyond the Int range.
<prelude>:1112
def to_f -> Float
self.
<prelude>:1114
def to_s -> String
The shortest decimal that reads back as self, with .0 for a whole
number, in scientific notation below 0.0001 and from 1e16 on;
"NaN", "Infinity" or "-Infinity" for those.
100.0.to_s # => "100.0"
1e16.to_s # => "1.0e+16"
0.00001.to_s # => "1.0e-05"
(-0.0).to_s # => "-0.0"
<prelude>:1125
def ms -> Duration
A Duration of self milliseconds, to the nearest nanosecond.
<prelude>:1127
def milliseconds -> Duration
A Duration of self milliseconds, to the nearest nanosecond.
<prelude>:1129
def second -> Duration
A Duration of self seconds, to the nearest nanosecond.
1.5.seconds # => 1500ms
0.25.minutes # => 15s
<prelude>:1136
def seconds -> Duration
A Duration of self seconds, to the nearest nanosecond.
1.5.seconds # => 1500ms
0.25.minutes # => 15s
<prelude>:1138
def minute -> Duration
A Duration of self minutes, to the nearest nanosecond.
<prelude>:1140
def minutes -> Duration
A Duration of self minutes, to the nearest nanosecond.
<prelude>:1142