class 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):Math::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):Math::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):2.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):Math.sqrt(2.0) # => 1.4142135623730951
-
Even a simple computation can introduce imprecision:
x = 0.1 + 0.2 # => 0.30000000000000004 y = 0.3 # => 0.3 x == y # => false
See:
-
github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub ys-floats-imprecise
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:
-
A floating-point literal.
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 aFloatandself -
divmod: Returns a 2-element array containing the quotient and remainder results of dividingselfby the given value. -
fdiv: Returns theFloatresult of dividingselfby the given value. -
floor: Returns the greatest number smaller than or equal toself. -
next_float: Returns the next-larger representableFloat. -
prev_float: Returns the next-smaller representableFloat. -
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.
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):Math::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):Math::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):2.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):Math.sqrt(2.0) # => 1.4142135623730951
-
Even a simple computation can introduce imprecision:
x = 0.1 + 0.2 # => 0.30000000000000004 y = 0.3 # => 0.3 x == y # => false
See:
-
github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#-why-are-rub ys-floats-imprecise
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:
-
A floating-point literal.
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 aFloatandself -
divmod: Returns a 2-element array containing the quotient and remainder results of dividingselfby the given value. -
fdiv: Returns theFloatresult of dividingselfby the given value. -
floor: Returns the greatest number smaller than or equal toself. -
next_float: Returns the next-larger representableFloat. -
prev_float: Returns the next-smaller representableFloat. -
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.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 283
def %: (Integer) -> Float
| (Float) -> Float
| (Rational) -> Float
| (Numeric) -> Numeric
Returns self modulo other as a Float.
For float f and real number r, these expressions are equivalent:
f % r f-r*(f/r).floor f.divmod(r)[1]
See Numeric#divmod.
Examples:
10.0 % 2 # => 0.0 10.0 % 3 # => 1.0 10.0 % 4 # => 2.0 10.0 % -2 # => 0.0 10.0 % -3 # => -2.0 10.0 % -4 # => -2.0 10.0 % 4.0 # => 2.0 10.0 % Rational(4, 1) # => 2.0
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1343
def *: (BigDecimal) -> BigDecimal
| ...
Returns a new Float which is the product of self and other:
f = 3.14 f * 2 # => 6.28 f * 2.0 # => 6.28 f * Rational(1, 2) # => 1.57 f * Complex(2, 0) # => (6.28+0.0i)
Returns the numeric product of self and other:
f = 3.14 f * 2 # => 6.28 f * 2.0 # => 6.28 f * Rational(1, 2) # => 1.57 f * Complex(2, 0) # => (6.28+0.0i)
Returns the numeric product of self and other:
f = 3.14 f * 2 # => 6.28 f * 2.0 # => 6.28 f * Rational(1, 2) # => 1.57 f * Complex(2, 0) # => (6.28+0.0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 316
def **: (Complex) -> Complex
| (Numeric) -> Float
Returns self raised to the power exponent:
f = 3.14 f ** 2 # => 9.8596 f ** -2 # => 0.1014239928597509 f ** 2.1 # => 11.054834900588839 f ** Rational(2, 1) # => 9.8596 f ** Complex(2, 0) # => (9.8596+0i)
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1358
def +: (BigDecimal) -> BigDecimal
| ...
Returns a new Float which is the sum of self and other:
f = 3.14 f + 1 # => 4.140000000000001 f + 1.0 # => 4.140000000000001 f + Rational(1, 1) # => 4.140000000000001 f + Complex(1, 0) # => (4.140000000000001+0i)
Returns the sum of self and other; the result may be inexact (see Float):
3.14 + 0 # => 3.14 3.14 + 1 # => 4.140000000000001 -3.14 + 0 # => -3.14 -3.14 + 1 # => -2.14 3.14 + -3.14 # => 0.0 -3.14 + -3.14 # => -6.28 3.14 + Complex(1, 0) # => (4.140000000000001+0i) 3.14 + Rational(1, 1) # => 4.140000000000001
Returns the sum of self and other; the result may be inexact (see Float):
3.14 + 0 # => 3.14 3.14 + 1 # => 4.140000000000001 -3.14 + 0 # => -3.14 -3.14 + 1 # => -2.14 3.14 + -3.14 # => 0.0 -3.14 + -3.14 # => -6.28 3.14 + Complex(1, 0) # => (4.140000000000001+0i) 3.14 + Rational(1, 1) # => 4.140000000000001
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1373
def -: (BigDecimal) -> BigDecimal
| ...
Returns a new Float which is the difference of self and other:
f = 3.14 f - 1 # => 2.14 f - 1.0 # => 2.14 f - Rational(1, 1) # => 2.14 f - Complex(1, 0) # => (2.14+0i)
Returns the difference of self and other:
f = 3.14 f - 1 # => 2.14 f - 1.0 # => 2.14 f - Rational(1, 1) # => 2.14 f - Complex(1, 0) # => (2.14+0i)
Returns the difference of self and other:
f = 3.14 f - 1 # => 2.14 f - 1.0 # => 2.14 f - Rational(1, 1) # => 2.14 f - Complex(1, 0) # => (2.14+0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 364
def -@: () -> Float
Returns self, negated:
-3.14 # => -3.14 -(-3.14) # => 3.14 -0.0 # => -0.0
(BigDecimal) → BigDecimal
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/sig/big_decimal.rbs, line 1328
def /: (BigDecimal) -> BigDecimal
| ...
Returns a new Float which is the result of dividing self by other:
f = 3.14 f / 2 # => 1.57 f / 2.0 # => 1.57 f / Rational(2, 1) # => 1.57 f / Complex(2, 0) # => (1.57+0.0i)
Returns the quotient of self and other:
f = 3.14 f / 2 # => 1.57 f / 2.0 # => 1.57 f / Rational(2, 1) # => 1.57 f / Complex(2, 0) # => (1.57+0.0i)
Returns the quotient of self and other:
f = 3.14 f / 2 # => 1.57 f / 2.0 # => 1.57 f / Rational(2, 1) # => 1.57 f / Complex(2, 0) # => (1.57+0.0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 396
def <: (Numeric) -> bool
Returns whether the value of self is less than the value of other; other must be numeric, but may not be Complex:
2.0 < 3 # => true 2.0 < 3.0 # => true 2.0 < Rational(3, 1) # => true 2.0 < 2.0 # => false
Float::NAN < Float::NAN returns an implementation-dependent value.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 414
def <=: (Numeric) -> bool
Returns whether the value of self is less than or equal to the value of other; other must be numeric, but may not be Complex:
2.0 <= 3 # => true 2.0 <= 3.0 # => true 2.0 <= Rational(3, 1) # => true 2.0 <= 2.0 # => true 2.0 <= 1.0 # => false
Float::NAN <= Float::NAN returns an implementation-dependent value.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 445
def <=>: (Numeric) -> Integer?
Compares self and other.
Returns:
-
-1, ifselfis less thanother. -
0, ifselfis equal toother. -
1, ifselfis greater thanother. -
nil, if the two values are incommensurate.
Examples:
2.0 <=> 2.1 # => -1 2.0 <=> 2 # => 0 2.0 <=> 2.0 # => 0 2.0 <=> Rational(2, 1) # => 0 2.0 <=> Complex(2, 0) # => 0 2.0 <=> 1.9 # => 1 2.0 <=> 'foo' # => nil
Float::NAN <=> Float::NAN returns an implementation-dependent value.
Class Float includes module Comparable, each of whose methods uses Float#<=> for comparison.
(untyped) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 463
def ==: (untyped) -> bool
Returns true if other has the same value as self, false otherwise:
2.0 == 2 # => true 2.0 == 2.0 # => true 2.0 == Rational(2, 1) # => true 2.0 == Complex(2, 0) # => true
Float::NAN == Float::NAN returns an implementation-dependent value.
Related: Float#eql? (requires other to be a Float).
(untyped) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 478
def ===: (untyped) -> bool
Returns true if other has the same value as self, false otherwise:
2.0 == 2 # => true 2.0 == 2.0 # => true 2.0 == Rational(2, 1) # => true 2.0 == Complex(2, 0) # => true
Float::NAN == Float::NAN returns an implementation-dependent value.
Related: Float#eql? (requires other to be a Float).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 494
def >: (Numeric) -> bool
Returns true if self is numerically greater than other:
2.0 > 1 # => true 2.0 > 1.0 # => true 2.0 > Rational(1, 2) # => true 2.0 > 2.0 # => false
Float::NAN > Float::NAN returns an implementation-dependent value.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 511
def >=: (Numeric) -> bool
Returns true if self is numerically greater than or equal to other:
2.0 >= 1 # => true 2.0 >= 1.0 # => true 2.0 >= Rational(1, 2) # => true 2.0 >= 2.0 # => true 2.0 >= 2.1 # => false
Float::NAN >= Float::NAN returns an implementation-dependent value.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 523
def abs: () -> Float
Returns the absolute value of self:
(-34.56).abs # => 34.56 -34.56.abs # => 34.56 34.56.abs # => 34.56
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 528
def angle: ...
Returns 0 if self is positive, Math::PI otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 597
def ceil: () -> Integer
| (int digits) -> (Integer | Float)
Returns a numeric that is a βceilingβ value for self, as specified by the given ndigits, which must be an integer-convertible object. When ndigits is positive, returns a Float with ndigits decimal digits after the decimal point (as available, but no fewer than 1): f = 12345.6789 f.ceil(1) # => 12345.7 f.ceil(3) # => 12345.679 f.ceil(30) # => 12345.6789 f = -12345.6789 f.ceil(1) # => -12345.6 f.ceil(3) # => -12345.678 f.ceil(30) # => -12345.6789 f = 0.0 f.ceil(1) # => 0.0 f.ceil(100) # => 0.0
When ndigits is non-positive, returns an Integer based on a computed granularity: * The granularity is 10 ** ndigits.abs. * The returned value is the largest multiple of the granularity that is less than or equal to self. Examples with positive self: ndigits|Granularity|12345.6789.ceil(ndigits) ββ-|ββββ|ββββββββ 0| 1| 12346 -1| 10| 12350 -2| 100| 12400 -3| 1000| 13000 -4| 10000| 20000 -5| 100000| 100000 Examples with negative self: ndigits|Granularity|-12345.6789.ceil(ndigits) ββ-|ββββ|ββββββββ- 0| 1| -12345 -1| 10| -12340 -2| 100| -12300 -3| 1000| -12000 -4| 10000| -10000 -5| 100000| 0 When self is zero and ndigits is non-positive, returns Integer zero: 0.0.ceil(0) # => 0 0.0.ceil(-1) # => 0 0.0.ceil(-2) # => 0
Note that the limited precision of floating-point arithmetic may lead to surprising results: (2.1 / 0.7).ceil #=> 4 # Not 3 (because 2.1 / 0.7 # => 3.0000000000000004, not 3.0)
Related: Float#floor.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 614
def coerce: (Numeric) -> [ Float, Float ]
Returns a 2-element array containing other converted to a Float and self:
f = 3.14 # => 3.14 f.coerce(2) # => [2.0, 3.14] f.coerce(2.0) # => [2.0, 3.14] f.coerce(Rational(1, 2)) # => [0.5, 3.14] f.coerce(Complex(1, 0)) # => [1.0, 3.14]
Raises an exception if a type conversion fails.
() → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 624
def denominator: () -> Integer
Returns the denominator (always positive). The result is machine dependent.
See also Float#numerator.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 650
def divmod: (Integer | Float | Rational) -> [ Integer, Float ]
| (Numeric) -> [ Numeric, Numeric ]
Returns a 2-element array [q, r], where
q = (self/other).floor # Quotient r = self % other # Remainder
Examples:
11.0.divmod(4) # => [2, 3.0] 11.0.divmod(-4) # => [-3, -1.0] -11.0.divmod(4) # => [-3, 1.0] -11.0.divmod(-4) # => [2, -3.0] 12.0.divmod(4) # => [3, 0.0] 12.0.divmod(-4) # => [-3, 0.0] -12.0.divmod(4) # => [-3, -0.0] -12.0.divmod(-4) # => [3, -0.0] 13.0.divmod(4.0) # => [3, 1.0] 13.0.divmod(Rational(4, 1)) # => [3, 1.0]
(untyped) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 671
def eql?: (untyped) -> bool
Returns true if other is a Float with the same value as self, false otherwise:
2.0.eql?(2.0) # => true 2.0.eql?(1.0) # => false 2.0.eql?(1) # => false 2.0.eql?(Rational(2, 1)) # => false 2.0.eql?(Complex(2, 0)) # => false
Float::NAN.eql?(Float::NAN) returns an implementation-dependent value.
Related: Float#== (performs type conversions).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 682
def fdiv: (Complex) -> Complex
| (Numeric) -> Float
Returns the quotient from dividing self by other:
f = 3.14 f.quo(2) # => 1.57 f.quo(-2) # => -1.57 f.quo(Rational(2, 1)) # => 1.57 f.quo(Complex(2, 0)) # => (1.57+0.0i)
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 701
def finite?: () -> bool
Returns true if self is not Infinity, -Infinity, or NaN, false otherwise:
f = 2.0 # => 2.0 f.finite? # => true f = 1.0/0.0 # => Infinity f.finite? # => false f = -1.0/0.0 # => -Infinity f.finite? # => false f = 0.0/0.0 # => NaN f.finite? # => false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 763
def floor: () -> Integer
| (int digits) -> (Integer | Numeric)
Returns a float or integer that is a βfloorβ value for self, as specified by ndigits, which must be an integer-convertible object. When self is zero, returns a zero value: a float if ndigits is positive, an integer otherwise: f = 0.0 # => 0.0 f.floor(20) # => 0.0 f.floor(0) # => 0 f.floor(-20) # => 0
When self is non-zero and ndigits is positive, returns a float with ndigits digits after the decimal point (as available): f = 12345.6789 f.floor(1) # => 12345.6 f.floor(3) # => 12345.678 f.floor(30) # => 12345.6789 f = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679 f.floor(30) # => -12345.6789
When self is non-zero and ndigits is non-positive, returns an integer value based on a computed granularity: * The granularity is 10 ** ndigits.abs. * The returned value is the largest multiple of the granularity that is less than or equal to self. Examples with positive self: ndigits|Granularity|12345.6789.floor(ndigits) ββ-|ββββ|ββββββββ- 0| 1| 12345 -1| 10| 12340 -2| 100| 12300 -3| 1000| 12000 -4| 10000| 10000 -5| 100000| 0 Examples with negative self: ndigits|Granularity|-12345.6789.floor(ndigits) ββ-|ββββ|βββββββββ 0| 1| -12346 -1| 10| -12350 -2| 100| -12400 -3| 1000| -13000 -4| 10000| -20000 -5| 100000| -100000 -6| 1000000| -1000000 Note that the limited precision of floating-point arithmetic may lead to surprising results: (0.3 / 0.1).floor # => 2 # Not 3, (because (0.3 / 0.1) # => 2.9999999999999996, not 3.0)
Related: Float#ceil.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 774
def hash: () -> Integer
Returns the integer hash value for self.
See also Object#hash.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 797
def infinite?: () -> Integer?
Returns:
-
1, if
selfisInfinity. -
-1 if
selfis-Infinity. -
nil, otherwise.
Examples:
f = 1.0/0.0 # => Infinity f.infinite? # => 1 f = -1.0/0.0 # => -Infinity f.infinite? # => -1 f = 1.0 # => 1.0 f.infinite? # => nil f = 0.0/0.0 # => NaN f.infinite? # => nil
Returns a string containing a representation of self; depending of the value of self, the string representation may contain:
-
A fixed-point number. 3.14.to_s # => "3.14"
-
A number in "scientific notation" (containing an exponent). (10.1**50).to_s # => "1.644631821843879e+50"
-
'Infinity'. (10.1**500).to_s # => "Infinity"
-
'-Infinity'. (-10.1**500).to_s # => "-Infinity"
-
'NaN' (indicating not-a-number). (0.0/0.0).to_s # => "NaN"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 851
def modulo: (Numeric) -> Float
Returns self modulo other as a Float.
For float f and real number r, these expressions are equivalent:
f % r f-r*(f/r).floor f.divmod(r)[1]
See Numeric#divmod.
Examples:
10.0 % 2 # => 0.0 10.0 % 3 # => 1.0 10.0 % 4 # => 2.0 10.0 % -2 # => 0.0 10.0 % -3 # => -2.0 10.0 % -4 # => -2.0 10.0 % 4.0 # => 2.0 10.0 % Rational(4, 1) # => 2.0
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 864
def nan?: () -> bool
Returns true if self is a NaN, false otherwise.
f = -1.0 #=> -1.0 f.nan? #=> false f = 0.0/0.0 #=> NaN f.nan? #=> true
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 872
def negative?: () -> bool
Returns true if self is less than 0, false otherwise.
() → Float
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 917
def next_float: () -> Float
Returns the next-larger representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f and for the corresponding f.next_float:
f = 0.0 # 0x0000000000000000 f.next_float # 0x0000000000000001 f = 0.01 # 0x3f847ae147ae147b f.next_float # 0x3f847ae147ae147c
In the remaining examples here, the output is shown in the usual way (result to_s):
0.01.next_float # => 0.010000000000000002 1.0.next_float # => 1.0000000000000002 100.0.next_float # => 100.00000000000001 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.next_float }
Output:
0 0x1.47ae147ae147bp-7 0.01
1 0x1.47ae147ae147cp-7 0.010000000000000002
2 0x1.47ae147ae147dp-7 0.010000000000000004
3 0x1.47ae147ae147ep-7 0.010000000000000005
f = 0.0; 100.times { f += 0.1 }
f # => 9.99999999999998 # should be 10.0 in the ideal world.
10-f # => 1.9539925233402755e-14 # the floating point error.
10.0.next_float-10 # => 1.7763568394002505e-15 # 1 ulp (unit in the last place).
(10-f)/(10.0.next_float-10) # => 11.0 # the error is 11 ulp.
(10-f)/(10*Float::EPSILON) # => 8.8 # approximation of the above.
"%a" % 10 # => "0x1.4p+3"
"%a" % f # => "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
Related: Float#prev_float
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 931
def numerator: () -> Integer
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595 d = 0.3.denominator #=> 18014398509481984 n.fdiv(d) #=> 0.3
See also Float#denominator.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 938
def polar: () -> [ Float, Integer | Float ]
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 946
def positive?: () -> bool
Returns true if self is greater than 0, false otherwise.
() → Float
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 982
def prev_float: () -> Float
Returns the next-smaller representable Float.
These examples show the internally stored values (64-bit hexadecimal) for each Float f and for the corresponding f.pev_float:
f = 5e-324 # 0x0000000000000001 f.prev_float # 0x0000000000000000 f = 0.01 # 0x3f847ae147ae147b f.prev_float # 0x3f847ae147ae147a
In the remaining examples here, the output is shown in the usual way (result to_s):
0.01.prev_float # => 0.009999999999999998 1.0.prev_float # => 0.9999999999999999 100.0.prev_float # => 99.99999999999999 f = 0.01 (0..3).each_with_index {|i| printf "%2d %-20a %s\n", i, f, f.to_s; f = f.prev_float }
Output:
0 0x1.47ae147ae147bp-7 0.01 1 0x1.47ae147ae147ap-7 0.009999999999999998 2 0x1.47ae147ae1479p-7 0.009999999999999997 3 0x1.47ae147ae1478p-7 0.009999999999999995
Related: Float#next_float.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 996
def quo: (Complex) -> Complex
| (Numeric) -> Float
Returns the quotient from dividing self by other:
f = 3.14 f.quo(2) # => 1.57 f.quo(-2) # => -1.57 f.quo(Rational(2, 1)) # => 1.57 f.quo(Complex(2, 0)) # => (1.57+0.0i)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1013
def rationalize: (?Numeric eps) -> Rational
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1015
def rect: () -> [ Float, Numeric ]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1017
def remainder: (Numeric) -> Float
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1072
def round: (?half: :up | :down | :even) -> Integer
| (int digits, ?half: :up | :down | :even) -> (Integer | Float)
Returns self rounded to the nearest value with a precision of ndigits decimal digits.
When ndigits is non-negative, returns a float with ndigits after the decimal point (as available):
f = 12345.6789 f.round(1) # => 12345.7 f.round(3) # => 12345.679 f = -12345.6789 f.round(1) # => -12345.7 f.round(3) # => -12345.679
When ndigits is negative, returns an integer with at least ndigits.abs trailing zeros:
f = 12345.6789 f.round(0) # => 12346 f.round(-3) # => 12000 f = -12345.6789 f.round(0) # => -12346 f.round(-3) # => -12000
If keyword argument half is given, and self is equidistant from the two candidate values, the rounding is according to the given half value:
-
:upornil: round away from zero:2.5.round(half: :up) # => 3 3.5.round(half: :up) # => 4 (-2.5).round(half: :up) # => -3
-
:down: round toward zero:2.5.round(half: :down) # => 2 3.5.round(half: :down) # => 3 (-2.5).round(half: :down) # => -2
-
:even: round toward the candidate whose last nonzero digit is even:2.5.round(half: :even) # => 2 3.5.round(half: :even) # => 4 (-2.5).round(half: :even) # => -2
Raises and exception if the value for half is invalid.
Related: Float#truncate.
Source
# File vendor/bundle/ruby/4.0.0/gems/bigdecimal-4.1.2/lib/bigdecimal/util.rb, line 50 def to_d(precision=0) BigDecimal(self, precision) end
Returns the value of float as a BigDecimal. The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also Kernel.BigDecimal.
Returns the value of float as a BigDecimal. The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also Kernel.BigDecimal.
Returns the value of float as a BigDecimal. The precision parameter is used to determine the number of significant digits for the result. When precision is set to 0, the number of digits to represent the float being converted is determined automatically. The default precision is 0.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also Kernel.BigDecimal.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1081
def to_f: () -> Float
Returns self (which is already a Float).
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1097
def to_i: () -> Integer
Returns self truncated to an Integer.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
Returns self truncated to an Integer.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
(?JSON::State? state) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/stdlib/json/0/json.rbs, line 1296
def to_json: (?JSON::State? state) -> String
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1132
def to_r: () -> Rational
Returns the value as a rational.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1) 0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isnβt the same as β0.3β.to_r. The latter is equivalent to β3/10β.to_r, but the former isnβt so.
0.3.to_r == 3/10r #=> false "0.3".to_r == 3/10r #=> true
See also Float#rationalize.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1156
def to_s: () -> String
Returns a string containing a representation of self; depending of the value of self, the string representation may contain:
-
A fixed-point number. 3.14.to_s # => "3.14"
-
A number in "scientific notation" (containing an exponent). (10.1**50).to_s # => "1.644631821843879e+50"
-
'Infinity'. (10.1**500).to_s # => "Infinity"
-
'-Infinity'. (-10.1**500).to_s # => "-Infinity"
-
'NaN' (indicating not-a-number). (0.0/0.0).to_s # => "NaN"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1192
def truncate: () -> Integer
| (Integer ndigits) -> (Integer | Float)
Returns self truncated (toward zero) to a precision of ndigits decimal digits.
When ndigits is positive, returns a float with ndigits digits after the decimal point (as available):
f = 12345.6789 f.truncate(1) # => 12345.6 f.truncate(3) # => 12345.678 f = -12345.6789 f.truncate(1) # => -12345.6 f.truncate(3) # => -12345.678
When ndigits is negative, returns an integer with at least ndigits.abs trailing zeros:
f = 12345.6789 f.truncate(0) # => 12345 f.truncate(-3) # => 12000 f = -12345.6789 f.truncate(0) # => -12345 f.truncate(-3) # => -12000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
Related: Float#round.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.1/core/float.rbs, line 1201
def zero?: () -> bool
Returns true if self is 0.0, false otherwise.