Class std::Float

Implements Comparable<Float>

Instances of the Float class are floating-point numbers (floats). A float is an approximation of a real number, and can represent whole and fractional numbers, both small and large in magnitude. Internally Float objects are 64-bit IEEE double-precision floating-point numbers, with up to 15 significant digits of accuracy.

class Float(x)
Construct an object of the Float type. Call the _float() method of the argument and return a value equal to the result, provided that it is a float. Of the primitive types, Str and Int objects provide a _float method. String arguments may contain optional blank and tab characters before and after the number. Otherwise, the syntax of the string arguments must follow the Alore float literal syntax.

Operations

Float objects support the following operations (f below refers to a Float value):

f + x (Float + FloatFloat; Float + IntFloat)
Perform a floating point addition operation. The right operand x may be an integer or a float; the result is a float.
f + x (Float + Addable<Float, T> ⇒ T)
If x is neither an integer or a float, return x + n (switch operands).
f * x (Float * FloatFloat; Float * IntFloat)
Perform a floating point multiplication operation. The right operand x may be an integer or a float; the result is a float.
f * x (Float * Multipliable<Float, T> ⇒ T)
If x is neither an integer or a float, return x * n (switch operands).
f - x (Float - FloatFloat; Float - IntFloat)
Perform a subtraction operation. The right operands x may be an integer or a float, and the result is a float.
f / x (Float / FloatFloat; Float / IntFloat)
Perform a division operation. The right operands x may be an integer or a float, and the result is a float.
f div x (Float div FloatInt; Float div IntInt)
Perform integer division. The result is f / x rounded down to the nearest integer represented as a float. The right operand x may be an integer or a float.
7.1 div 3.0     -- 2.0
-4.2 div 3.0    -- -2.0
f mod x (Float mod FloatFloat; Float mod IntFloat)
Perform the modulus operation. The result is chosen so that (f div x) * x + f mod x == f (the equality is only approximate due to the inexact nature of floating point numbers). The right operand x may be an integer or a float, and the result is a float.
f ** x (Float ** FloatFloat; Float ** IntFloat)
Perform the exponentiation (power) operation. The right operand may be an integer or a float, and the result is a float. The operator follows the following rules in special conditions:
  • 0.0 ** 0 == 1.0
  • 0.0 ** 0.0 == 1.0
  • If f < 0 and x is not an integer, raise ArithmeticError.
-f (-FloatFloat)
Return the negation of a float.
f == x (Float == ObjectBoolean)
Floats can be compared for equality with arbitrary objects. A float object is equal to an equivalent float or integer value. If x is neither an integer nor a float, the operation is evaluated as x == f instead.

Note: The last property enables user-defined types to be compared with floats usefully.

f < x (Float < FloatBoolean; Float < IntBoolean)
f > x (Float > FloatBoolean; Float > IntBoolean)
Floats can be compared for order with floats and integers. If x is neither a float nor an integer, the operation is evaluated as x > f (for a < operation) or x < f (for a > operation).

Note: The last property allows user-defined types to be compared with floats usefully.

Str(f)
Convert a float to a string. The result contains up to approximately 10 significant digits.
Repr(f)
Convert a float to a string. The result contains up to approximately 16 significant digits.
Int(f)
Convert a float to an integer, truncating any fraction.
Hash(f)
Return the hash value of a float.