Float

The Float module provides functions for working with 64-bit floating-point numbers (IEEE 754 double precision). Floats are used for representing decimal and very large or small numbers.

Constants

Float.pi
Float
The mathematical constant pi (approximately 3.14159).
println Float.pi
# => 3.141592653589793
Float.e
Float
Euler's number (approximately 2.71828).
println Float.e
# => 2.718281828459045
Float.nan
Float
Not-a-Number, representing undefined or unrepresentable values.
println (Float.nan? Float.nan)
# => true
Float.infinity / Float.neg-infinity
Float
Positive and negative infinity.
println (Float.infinite? Float.infinity)
# => true

Arithmetic

Float.add
Float -> Float -> Float
Adds two floating-point numbers.
println (Float.add 3.5 2.1)
# => 5.6
Float.sub
Float -> Float -> Float
Subtracts the second float from the first.
println (Float.sub 5.0 2.3)
# => 2.7
Float.mul
Float -> Float -> Float
Multiplies two floating-point numbers.
println (Float.mul 2.5 4.0)
# => 10.0
Float.div
Float -> Float -> Float
Divides the first float by the second.
println (Float.div 10.0 4.0)
# => 2.5
Float.neg
Float -> Float
Negates a floating-point number.
println (Float.neg 3.14)
# => -3.14
Float.abs
Float -> Float
Returns the absolute value.
println (Float.abs -3.14)
# => 3.14

Comparison

Float.eq? / Float.ne?
Float -> Float -> Bool
Tests equality or inequality of two floats.
println (Float.eq? 3.0 3.0)
# => true
Float.lt? / Float.gt? / Float.le? / Float.ge?
Float -> Float -> Bool
Less than, greater than, less than or equal, greater than or equal comparisons.
println (Float.lt? 3.14 3.15)    # => true
println (Float.ge? 3.14 3.14)    # => true
Float.compare
Float -> Float -> Int
Returns -1 if first is less, 0 if equal, 1 if greater.
println (Float.compare 3.0 5.0)
# => -1

Predicates

Float.positive? / Float.negative? / Float.zero?
Float -> Bool
Tests the sign of a floating-point number.
println (Float.positive? 3.14)    # => true
println (Float.negative? -2.5)    # => true
println (Float.zero? 0.0)         # => true
Float.nan?
Float -> Bool
Returns true if the value is NaN (not a number).
println (Float.nan? Float.nan)       # => true
println (Float.nan? 3.14)            # => false
Float.infinite?
Float -> Bool
Returns true if the value is positive or negative infinity.
println (Float.infinite? Float.infinity)    # => true
println (Float.infinite? 1000000.0)         # => false
Float.finite?
Float -> Bool
Returns true if the value is neither infinite nor NaN.
println (Float.finite? 3.14)             # => true
println (Float.finite? Float.infinity)   # => false

Conversions

Float.to-int
Float -> Int
Converts a float to an integer by truncating toward zero.
println (Float.to-int 3.7)     # => 3
println (Float.to-int -3.7)    # => -3
Float.to-string / Float.to-str
Float -> String
Converts a float to its string representation. Float.to-str is an alias.
s = Float.to-string 3.14159
println s
# => "3.14159"
Float.parse
String -> Result Float ParseError
Parses a string as a floating-point number. Returns Ok n on success or Err ParseError on invalid input.
result = Float.parse "3.14159"
println result
# => Ok(3.14159)

result = Float.parse "abc"
println result
# => Err(ParseError)
Float.force
Float -> Float
Forces evaluation of a floating-point value. Useful for strict evaluation in lazy contexts.
n = Float.force 3.14
println n
# => 3.14
Float.to-exp
Float -> String
Converts a float to exponential notation string.
println (Float.to-exp 12345.0)
# => "1.2345e4"
Float.to-exp-n
Float -> Int -> String
Converts a float to exponential notation string with the specified number of decimal places.
println (Float.to-exp-n 12345.6789 2)
# => "1.23e4"
Float.show
Float -> String
Returns a string representation of the float.
println (Float.show 2.718)
# => "2.718"

Rounding

Float.floor
Float -> Int
Rounds down to the nearest integer.
println (Float.floor 3.7)     # => 3
println (Float.floor -3.2)    # => -4
Float.ceil
Float -> Int
Rounds up to the nearest integer.
println (Float.ceil 3.2)     # => 4
println (Float.ceil -3.7)    # => -3
Float.round
Float -> Int
Rounds to the nearest integer.
println (Float.round 3.4)    # => 3
println (Float.round 3.6)    # => 4
Float.trunc
Float -> Int
Truncates toward zero, removing the fractional part.
println (Float.trunc 3.9)     # => 3
println (Float.trunc -3.9)    # => -3

Mathematical Functions

Float.sqrt
Float -> Float
Returns the square root.
println (Float.sqrt 16.0)
# => 4.0
Float.pow
Float -> Float -> Float
Raises the first number to the power of the second.
println (Float.pow 2.0 10.0)
# => 1024.0
Float.exp
Float -> Float
Returns e raised to the given power.
println (Float.exp 1.0)
# => 2.718281828459045
Float.log
Float -> Float
Returns the natural logarithm (base e).
println (Float.log Float.e)
# => 1.0
Float.log10
Float -> Float
Returns the base-10 logarithm.
println (Float.log10 1000.0)
# => 3.0
Float.sin / Float.cos / Float.tan
Float -> Float
Trigonometric functions. Arguments are in radians.
println (Float.sin 0.0)           # => 0.0
println (Float.cos 0.0)           # => 1.0
println (Float.sin Float.pi)      # => ~0.0
Float.asin / Float.acos / Float.atan
Float -> Float
Inverse trigonometric functions. Returns values in radians.
println (Float.asin 1.0)    # => 1.5707963... (pi/2)
println (Float.acos 1.0)    # => 0.0
println (Float.atan 1.0)    # => 0.7853981... (pi/4)
Float.atan2
Float -> Float -> Float
Two-argument arctangent. Computes the angle in radians between the positive x-axis and the point (x, y).
println (Float.atan2 1.0 1.0)    # => 0.7853981... (pi/4)
println (Float.atan2 1.0 0.0)    # => 1.5707963... (pi/2)

Min/Max/Clamp

Float.min
Float -> Float -> Float
Returns the smaller of two floats.
println (Float.min 3.14 2.71)
# => 2.71
Float.max
Float -> Float -> Float
Returns the larger of two floats.
println (Float.max 3.14 2.71)
# => 3.14
Float.clamp
Float -> Float -> Float -> Float
Restricts a value to be within a given range.
println (Float.clamp 0.5 0.0 1.0)    # => 0.5 (within range)
println (Float.clamp -0.5 0.0 1.0)   # => 0.0 (clamped to min)
println (Float.clamp 1.5 0.0 1.0)    # => 1.0 (clamped to max)