123456789_123456789_123456789_123456789_123456789_

Class: Float

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Numeric
Instance Chain:
Inherits: Numeric
Defined in: numeric.c,
complex.c,
rational.c

Overview

Float objects represent inexact real numbers 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:

Constant Summary

  • DIG =

    The minimum number of significant decimal digits in a double-precision floating point.

    Usually defaults to 15.

    # File 'numeric.c', line 4146
    INT2FIX(DBL_DIG)
  • EPSILON =

    The difference between 1 and the smallest double-precision floating point number greater than 1.

    Usually defaults to 2.2204460492503131e-16.

    # File 'numeric.c', line 4198
    DBL2NUM(DBL_EPSILON)
  • INFINITY =

    An expression representing positive infinity.

    # File 'numeric.c', line 4202
    DBL2NUM(INFINITY)
  • MANT_DIG =

    The number of base digits for the double data type.

    Usually defaults to 53.

    # File 'numeric.c', line 4139
    INT2FIX(DBL_MANT_DIG)
  • MAX =

    The largest possible integer in a double-precision floating point number.

    Usually defaults to 1.7976931348623157e+308.

    # File 'numeric.c', line 4191
    DBL2NUM(DBL_MAX)
  • MAX_10_EXP =

    The largest positive exponent in a double-precision floating point where 10 raised to this power minus 1.

    Usually defaults to 308.

    # File 'numeric.c', line 4174
    INT2FIX(DBL_MAX_10_EXP)
  • MAX_EXP =

    The largest possible exponent value in a double-precision floating point.

    Usually defaults to 1024.

    # File 'numeric.c', line 4160
    INT2FIX(DBL_MAX_EXP)
  • MIN =

    :MIN. 0.0.next_float returns the smallest positive floating point number including denormalized numbers.

    # File 'numeric.c', line 4185
    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_10_EXP =

    The smallest negative exponent in a double-precision floating point where 10 raised to this power minus 1.

    Usually defaults to -307.

    # File 'numeric.c', line 4167
    INT2FIX(DBL_MIN_10_EXP)
  • MIN_EXP =

    The smallest posable exponent value in a double-precision floating point.

    Usually defaults to -1021.

    # File 'numeric.c', line 4153
    INT2FIX(DBL_MIN_EXP)
  • NAN =

    An expression representing a value which is “not a number”.

    # File 'numeric.c', line 4206
    DBL2NUM(NAN)
  • RADIX =

    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.

    # File 'numeric.c', line 4133
    INT2FIX(FLT_RADIX)
  • ROUNDS =

    Represents the rounding mode for floating point addition.

    Usually defaults to 1, rounding to the nearest number.

    Other modes include:

    -1

    Indeterminable

    0

    Rounding towards zero

    1

    Rounding to the nearest number

    2

    Rounding towards positive infinity

    3

    Rounding towards negative infinity

    # File 'numeric.c', line 4126
    INT2FIX(FLT_ROUNDS)

Instance Attribute Summary

::Numeric - Inherited

#integer?

Returns true if num is an ::Integer (including ::Fixnum and ::Bignum).

#nonzero?

Returns self if num is not zero, nil otherwise.

#real

Returns self.

#real?

Returns true if num is a Real number.

#zero?

Returns true if num has a zero value.

Instance Method Summary

::Numeric - Inherited

#%

x.modulo(y) means x-y*(x/y).floor.

#+@

Unary Plus—Returns the receiver's value.

#-@

Unary Minus—Returns the receiver's value, negated.

#<=>

Returns zero if number equals other, otherwise nil is returned if the two values are incomparable.

#abs

Returns the absolute value of num.

#abs2

Returns square of self.

#angle

Alias for Numeric#arg.

#arg

Returns 0 if the value is positive, pi otherwise.

#ceil

Returns the smallest possible ::Integer that is greater than or equal to num.

#coerce

If a +numeric is the same type as num, returns an array containing numeric and num.

#conj

Returns self.

#conjugate

Alias for Numeric#conj.

#denominator

Returns the denominator (always positive).

#div

Uses #/ to perform division, then converts the result to an integer.

#divmod

Returns an array containing the quotient and modulus obtained by dividing num by numeric.

#eql?

Returns true if num and numeric are the same type and have equal values.

#fdiv

Returns float division.

#floor

Returns the largest integer less than or equal to num.

#i

Returns the corresponding imaginary number.

#imag

Returns zero.

#imaginary

Alias for Numeric#imag.

#initialize_copy

Numerics are immutable values, which should not be copied.

#magnitude

Alias for Numeric#abs.

#modulo

Alias for Numeric#%.

#numerator

Returns the numerator.

#phase

Alias for Numeric#arg.

#polar

Returns an array; [num.abs, num.arg].

#quo

Returns most exact division (rational for integers, float for floats).

#rect

Returns an array; [num, 0].

#rectangular

Alias for Numeric#rect.

#remainder

x.remainder(y) means x-y*(x/y).truncate.

#round

Rounds num to a given precision in decimal digits (default 0 digits).

#singleton_method_added

Trap attempts to add methods to ::Numeric objects.

#step

Invokes the given block with the sequence of numbers starting at num, incremented by step (defaulted to 1) on each call.

#to_c

Returns the value as a complex.

#to_int

Invokes the child class's #to_i method to convert num to an integer.

#truncate

Returns num truncated to an ::Integer.

::Comparable - Included

#<

Compares two objects based on the receiver's #<=> method, returning true if it returns -1.

#<=

Compares two objects based on the receiver's #<=> method, returning true if it returns -1 or 0.

#==

Compares two objects based on the receiver's #<=> method, returning true if it returns 0.

#>

Compares two objects based on the receiver's #<=> method, returning true if it returns 1.

#>=

Compares two objects based on the receiver's #<=> method, returning true if it returns 0 or 1.

#between?

Returns false if obj #<=> min is less than zero or if anObject #<=> max is greater than zero, true otherwise.

Instance Attribute Details

#finite?Boolean (readonly)

Returns true if float is a valid IEEE floating point number (it is not infinite, and #nan? is false).

#infinite?Boolean (readonly)

Return values corresponding to the value of float:

finite

nil

-Infinity

-1

+Infinity

1

For example:

(0.0).infinite?        #=> nil
(-1.0/0.0).infinite?   #=> -1
(+1.0/0.0).infinite?   #=> 1

#nan?Boolean (readonly)

Returns true if float is an invalid IEEE floating point number.

a = -1.0      #=> -1.0
a.nan?        #=> false
a = 0.0/0.0   #=> NaN
a.nan?        #=> true

#zero?Boolean (readonly)

Returns true if float is 0.0.

Instance Method Details

#%(other) ⇒ Float #modulo(other) ⇒ Float
Also known as: #modulo

Return the modulo after division of float by other.

6543.21.modulo(137)      #=> 104.21
6543.21.modulo(137.24)   #=> 92.9299999999996

#*(other) ⇒ Float

Returns a new float which is the product of float and other.

#**(other) ⇒ Float

Raises float to the power of other.

2.0**3      #=> 8.0

#+(other) ⇒ Float

Returns a new float which is the sum of float and other.

#-(other) ⇒ Float

Returns a new float which is the difference of float and other.

#-Float

Returns float, negated.

#/(other) ⇒ Float

Returns a new float which is the result of dividing float by other.

#<(real) ⇒ Boolean

Returns true if float is less than real.

The result of NaN < NaN is undefined, so the implementation-dependent value is returned.

#<=(real) ⇒ Boolean

Returns true if float is less than or equal to real.

The result of NaN <= NaN is undefined, so the implementation-dependent value is returned.

#<=>(real) ⇒ 1, ...

Returns -1, 0, +1 or nil depending on whether float is less than, equal to, or greater than real. This is the basis for the tests in ::Comparable.

The result of NaN <=> NaN is undefined, so the implementation-dependent value is returned.

nil is returned if the two values are incomparable.

#==(obj) ⇒ Boolean Also known as: #===

Returns true only if obj has the same value as float. Contrast this with #eql?, which requires obj to be a Float.

The result of NaN == NaN is undefined, so the implementation-dependent value is returned.

1.0 == 1   #=> true

#==(obj) ⇒ Boolean #===(obj) ⇒ Boolean

Alias for #==.

#>(real) ⇒ Boolean

Returns true if float is greater than real.

The result of NaN > NaN is undefined, so the implementation-dependent value is returned.

#>=(real) ⇒ Boolean

Returns true if float is greater than or equal to real.

The result of NaN >= NaN is undefined, so the implementation-dependent value is returned.

#absFloat #magnitudeFloat
Also known as: #magnitude

Returns the absolute value of float.

(-34.56).abs   #=> 34.56
-34.56.abs     #=> 34.56

#arg0, Float #angle0, Float #phase0, Float

Alias for #arg.

#arg0, Float #angle0, Float #phase0, Float
Also known as: #angle, #phase

Returns 0 if the value is positive, pi otherwise.

#ceilInteger

Returns the smallest ::Integer greater than or equal to float.

1.2.ceil      #=> 2
2.0.ceil      #=> 2
(-1.2).ceil   #=> -1
(-2.0).ceil   #=> -2

#coerce(numeric) ⇒ Array

Returns an array with both a numeric and a float represented as Float objects.

This is achieved by converting a numeric to a Float.

1.2.coerce(3)       #=> [3.0, 1.2]
2.5.coerce(1.1)     #=> [1.1, 2.5]

#denominatorInteger

Returns the denominator (always positive). The result is machine dependent.

See numerator.

#divmod(numeric) ⇒ Array

See Numeric#divmod.

42.0.divmod 6 #=> [7, 0.0]
42.0.divmod 5 #=> [8, 2.0]

#eql?(obj) ⇒ Boolean

Returns true only if obj is a Float with the same value as float. Contrast this with #==, which performs type conversions.

The result of NaN.eql?(NaN) is undefined, so the implementation-dependent value is returned.

1.0.eql?(1)   #=> false

#fdiv(numeric) ⇒ Float #quo(numeric) ⇒ Float

Alias for #quo.

#floorInteger

Returns the largest integer less than or equal to float.

1.2.floor      #=> 1
2.0.floor      #=> 2
(-1.2).floor   #=> -2
(-2.0).floor   #=> -2

#hashInteger

Returns a hash code for this float.

See also Object#hash.

#to_sString #inspectString

Alias for #to_s.

#absFloat #magnitudeFloat

Alias for #abs.

#%(other) ⇒ Float #modulo(other) ⇒ Float

Alias for #%.

#next_floatFloat

Returns the next representable floating-point number.

Float::MAX.next_float and Float::INFINITY.next_float is INFINITY.

Float::NAN.next_float is NAN.

For example:

p 0.01.next_float  #=> 0.010000000000000002
p 1.0.next_float   #=> 1.0000000000000002
p 100.0.next_float #=> 100.00000000000001

p 0.01.next_float - 0.01   #=> 1.734723475976807e-18
p 1.0.next_float - 1.0     #=> 2.220446049250313e-16
p 100.0.next_float - 100.0 #=> 1.4210854715202004e-14

f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
#=> 0x1.47ae147ae147bp-7 0.01
#   0x1.47ae147ae147cp-7 0.010000000000000002
#   0x1.47ae147ae147dp-7 0.010000000000000004
#   0x1.47ae147ae147ep-7 0.010000000000000005
#   0x1.47ae147ae147fp-7 0.010000000000000007
#   0x1.47ae147ae148p-7  0.010000000000000009
#   0x1.47ae147ae1481p-7 0.01000000000000001
#   0x1.47ae147ae1482p-7 0.010000000000000012
#   0x1.47ae147ae1483p-7 0.010000000000000014
#   0x1.47ae147ae1484p-7 0.010000000000000016
#   0x1.47ae147ae1485p-7 0.010000000000000018
#   0x1.47ae147ae1486p-7 0.01000000000000002
#   0x1.47ae147ae1487p-7 0.010000000000000021
#   0x1.47ae147ae1488p-7 0.010000000000000023
#   0x1.47ae147ae1489p-7 0.010000000000000024
#   0x1.47ae147ae148ap-7 0.010000000000000026
#   0x1.47ae147ae148bp-7 0.010000000000000028
#   0x1.47ae147ae148cp-7 0.01000000000000003
#   0x1.47ae147ae148dp-7 0.010000000000000031
#   0x1.47ae147ae148ep-7 0.010000000000000033

f = 0.0
100.times { f += 0.1 }
p f                            #=> 9.99999999999998       # should be 10.0 in the ideal world.
p 10-f                         #=> 1.9539925233402755e-14 # the floating-point error.
p(10.0.next_float-10)          #=> 1.7763568394002505e-15 # 1 ulp (units in the last place).
p((10-f)/(10.0.next_float-10)) #=> 11.0                   # the error is 11 ulp.
p((10-f)/(10*Float::EPSILON))  #=> 8.8                    # approximation of the above.
p "%a" % f                     #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5.  16 - 5 = 11 ulp.

#numeratorInteger

Returns the numerator. The result is machine dependent.

n = 0.3.numerator    #=> 5404319552844595
d = 0.3.denominator  #=> 18014398509481984
n.fdiv(d)            #=> 0.3

#arg0, Float #angle0, Float #phase0, Float

Alias for #arg.

#prev_floatFloat

Returns the previous representable floatint-point number.

(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.

Float::NAN.prev_float is NAN.

For example:

p 0.01.prev_float  #=> 0.009999999999999998
p 1.0.prev_float   #=> 0.9999999999999999
p 100.0.prev_float #=> 99.99999999999999

p 0.01 - 0.01.prev_float   #=> 1.734723475976807e-18
p 1.0 - 1.0.prev_float     #=> 1.1102230246251565e-16
p 100.0 - 100.0.prev_float #=> 1.4210854715202004e-14

f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
#=> 0x1.47ae147ae147bp-7 0.01
#   0x1.47ae147ae147ap-7 0.009999999999999998
#   0x1.47ae147ae1479p-7 0.009999999999999997
#   0x1.47ae147ae1478p-7 0.009999999999999995
#   0x1.47ae147ae1477p-7 0.009999999999999993
#   0x1.47ae147ae1476p-7 0.009999999999999992
#   0x1.47ae147ae1475p-7 0.00999999999999999
#   0x1.47ae147ae1474p-7 0.009999999999999988
#   0x1.47ae147ae1473p-7 0.009999999999999986
#   0x1.47ae147ae1472p-7 0.009999999999999985
#   0x1.47ae147ae1471p-7 0.009999999999999983
#   0x1.47ae147ae147p-7  0.009999999999999981
#   0x1.47ae147ae146fp-7 0.00999999999999998
#   0x1.47ae147ae146ep-7 0.009999999999999978
#   0x1.47ae147ae146dp-7 0.009999999999999976
#   0x1.47ae147ae146cp-7 0.009999999999999974
#   0x1.47ae147ae146bp-7 0.009999999999999972
#   0x1.47ae147ae146ap-7 0.00999999999999997
#   0x1.47ae147ae1469p-7 0.009999999999999969
#   0x1.47ae147ae1468p-7 0.009999999999999967

#fdiv(numeric) ⇒ Float #quo(numeric) ⇒ Float
Also known as: #fdiv

Returns float / numeric, same as #/.

#rationalize([eps]) ⇒ Rational

Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). if the optional 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 to_r.

#round([ndigits]) ⇒ Integer, Float

Rounds float to a given precision in decimal digits (default 0 digits).

Precision may be negative. Returns a floating point number when ndigits is more than zero.

1.4.round      #=> 1
1.5.round      #=> 2
1.6.round      #=> 2
(-1.5).round   #=> -2

1.234567.round(2)  #=> 1.23
1.234567.round(3)  #=> 1.235
1.234567.round(4)  #=> 1.2346
1.234567.round(5)  #=> 1.23457

34567.89.round(-5) #=> 0
34567.89.round(-4) #=> 30000
34567.89.round(-3) #=> 35000
34567.89.round(-2) #=> 34600
34567.89.round(-1) #=> 34570
34567.89.round(0)  #=> 34568
34567.89.round(1)  #=> 34567.9
34567.89.round(2)  #=> 34567.89
34567.89.round(3)  #=> 34567.89

#to_fself

Since float is already a float, returns self.

#to_iInteger #to_intInteger #truncateInteger
Also known as: #to_int, #truncate

Returns the float truncated to an ::Integer.

Synonyms are #to_i, #to_int, and #truncate.

#to_iInteger #to_intInteger #truncateInteger

Alias for #to_i.

#to_rRational

Returns the value as a rational.

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.

2.0.to_r    #=> (2/1)
2.5.to_r    #=> (5/2)
-0.75.to_r  #=> (-3/4)
0.0.to_r    #=> (0/1)

See rationalize.

#to_sString Also known as: #inspect

Returns a string containing a representation of self. As well as a fixed or exponential form of the float, the call may return NaN, Infinity, and -Infinity.

#to_iInteger #to_intInteger #truncateInteger

Alias for #to_i.