Floats
64-bit floating-point numbers (IEEE 754) with methods for rounding, comparison, and type conversion.
Creating Floats
let pi = 3.14159
let temp = -40.0
let tiny = 0.001
let sci = 2.5e10 # Scientific notation
let typed: Float = 1.5
Rounding Methods
.round / .round(digits)
Round to the nearest integer (0 args) or to a specific number of decimal places.
3.7.round # 4 (returns int)
3.14159.round(2) # 3.14 (returns float)
2.5.round # 3
Predicate Methods
.zero? / .positive? / .negative?
Check if the float is zero, positive, or negative.
0.0.zero? # true
3.14.positive? # true
(-1.5).negative? # true
.finite? / .infinite? / .nan?
Check for special IEEE 754 float states.
3.14.finite? # true
3.14.infinite? # false
3.14.nan? # false
.between?(min, max)
Check if the float is within the given range (inclusive).
3.14.between?(1.0, 5.0) # true
0.5.between?(1.0, 5.0) # false
.clamp(min, max)
Constrain the float to be within the given range.
15.5.clamp(0.0, 10.0) # 10.0
(-5.0).clamp(0.0, 10.0) # 0.0
Conversion Methods
Introspection Methods
.is_a?(type_name)
Check if the value is of the given type. Supports "float", "numeric", and "object".
3.14.is_a?("float") # true
3.14.is_a?("numeric") # true
3.14.is_a?("int") # false
.nil? / .blank? / .present?
Floats are never nil and always present.
3.14.nil? # false
3.14.blank? # false
3.14.present? # true