Constants

EPSILON

Difference between 1.0 and the next representable value of Float.

IO.println(Float.EPSILON) #> 2.2204460492503e-16

INFINITY

Positive infinity, noted \(\infty\).

IO.println(Float.INFINITY) #> inf

See also: NAN, is_infinity

LOWEST

Lowest finite value of Float.

IO.println(Float.LOWEST) #> -1.7976931348623e+308

See also: MAX

MAX

Maximum finite value of Float.

IO.println(Float.MAX) #> 1.7976931348623e+308

See also: LOWEST

MIN

Minimum, normalized, positive value of Float.

IO.println(Float.MIN) #> 2.2250738585072e-308

See also: TRUE_MIN

NAN

IO.println(Float.NAN) #> nan

See also: INFINITY, is_nan

TRUE_MIN

Minimum positive value of Float.

IO.println(Float.TRUE_MIN) #> 4.9406564584125e-324

See also: MIN

Static Methods

almost_equals(x,y), almost_equals(x, y, abs, rel)

Check if x and y are almost equals. abs is the absolute error allowed and rel is the relative error allowed between x and y. The first version of the function sets abs and rel to Float.EPSILON.

IO.println(Float.almost_equals(42.0, 42.00000000000001)) #> true
IO.println(Float.almost_equals(0.0, Float.MIN))          #> true
IO.println(Float.almost_equals(0.0, Float.EPSILON))      #> true

Methods

clone()

Get a copy of the float.

IO.println(42.0.clone()) #> 42

fractional

Get the fractional part of a float i.e. the part after any decimal point. The returned value has the same sign as the number.

IO.println(4.2.fractional)    #> 0.2
IO.println((-6.9).fractional) #> -0.9

See also: integral

hash

Compute a hash for the float.

IO.println(Float.INFINITY.hash) #> 2638638862653029785

integral

Get the integral part of a float i.e. the part before any decimal point. The returned value has the same sign as the number.

IO.println(4.2.integral)    #> 4
IO.println((-6.9).integral) #> -6

See also: fractional

is_infinity

Check if a float is positive infinity or negative infinity.

IO.println(Float.INFINITY.is_infinity) #> true
IO.println(42.0.is_infinity) #> false

See also: is_nan, INFINITY

is_nan

Check if a float is "Not a Number".

IO.println(Float.NAN.is_nan) #> true
IO.println(42.0.is_nan) #> false

See also: is_infinity, NAN

to_i

Transform the float to an Int by truncating towards zero.

IO.println(4.2.to_i == 4)     #> true
IO.println((-6.9).to_i == -6) #> true

to_s

Transform the float to a String.

IO.println(4.2.to_s == "4.2")     #> true
IO.println((-6.9).to_s == "-6.9") #> true

Operators

+, - operators

Unary + return the float number itself. Unary - return the opposite of the float number.

IO.println(+4.2) #> 4.2
IO.println(-4.2) #> -4.2

+(other), -(other), *(other), /(other) operators

Compute binary operation using usual arithmetic operators on float numbers. other must be a Float or an Int, there is an implicit conversion from Int.

IO.println(4.2 + 6.9) #> 11.1
IO.println(4.2 - 6.9) #> -2.7
IO.println(4.2 * 6.9) #> 28.98
IO.println(4.2 / 6.9) #> 0.60869565217391

%(other)

Compute the float modulo operation. If \(x\) is this and \(y\) is other, return value is \(x - n \times y\), where \(n\) is the quotient of \(x \div y\), rounded toward zero to an integer (like fmod in C). other must be a Float or an Int, there is an implicit conversion from Int.

IO.println(6.9 % 4.2) #> 2.7

==(other), !=(other) operators

Test the float number with other for equality or inequality. other must be a Float, there is no implicit conversion from Int.

IO.println(4.2 == 6.9) #> false
IO.println(4.2 != 6.9) #> true

<(other), <=(other), >(other), >=(other) operators

Compare the float number with other. other must be a Float, there is no implicit conversion from Int.

IO.println(4.2 < 6.9)  #> true
IO.println(4.2 <= 6.9) #> true
IO.println(4.2 > 6.9)  #> false
IO.println(4.2 >= 6.9) #> false