Module
pooka/float
src/pooka/float.pookaFloating-point numbers measure with a small, honest ruler.
Expose BEAM floating-point operations and fallible numeric conversions. Parsing, square roots and powers keep failure in Result; rounding operations return Int. These are floating-point calculations, so decimal identities need not be exact.
import pooka/float
assert float.floor(-1.2) == -2
API reference
class ParseError(text: String)
A float failure carrying the details needed by its caller. Keep this value in Result until a boundary can explain it.
text: String
String
fn(String) -> pooka/float.ParseErrorclass MathError(message: String)
A float failure carrying the details needed by its caller. Keep this value in Result until a boundary can explain it.
message: String
String
fn(String) -> pooka/float.MathErrorparse(text: String) -> Result(Float, ParseError)
fn(String) -> Result(Float, pooka/float.ParseError)Parse a floating-point value, retaining failure as ParseError.
import pooka/float
assert float.parse("3.0") == Ok(3.0)
to_string(value: Float) -> String
fn(Float) -> StringRender a floating-point value using the library's textual representation.
import pooka/float
let text = float.to_string(3.0)
abs(value: Float) -> Float
fn(Float) -> FloatReturn the nonnegative magnitude.
import pooka/float
assert float.abs(-3.0) == 3.0
min(a: Float, b: Float) -> Float
fn(Float, Float) -> FloatReturn the smaller value.
import pooka/float
assert float.min(2.0, 3.0) == 2.0
max(a: Float, b: Float) -> Float
fn(Float, Float) -> FloatReturn the larger value.
import pooka/float
assert float.max(2.0, 3.0) == 3.0
clamp(value: Float, lower: Float, upper: Float) -> Float
fn(Float, Float, Float) -> FloatKeep a value between the bounds; reversed bounds are normalized first.
import pooka/float
assert float.clamp(3.0, -2.0, 2.0) == 2.0
floor(value: Float) -> Int
fn(Float) -> IntRound downward toward negative infinity and return Int.
import pooka/float
assert float.floor(-1.2) == -2
ceil(value: Float) -> Int
fn(Float) -> IntRound upward toward positive infinity and return Int.
import pooka/float
assert float.ceil(-1.2) == -1
round(value: Float) -> Int
fn(Float) -> IntRound to the nearest integer using the runtime's rounding rule.
import pooka/float
assert float.round(1.6) == 2
truncate(value: Float) -> Int
fn(Float) -> IntDiscard the fractional part toward zero.
import pooka/float
assert float.truncate(-1.8) == -1
sqrt(value: Float) -> Result(Float, MathError)
fn(Float) -> Result(Float, pooka/float.MathError)Compute the square root, returning MathError for an unsupported domain.
import pooka/float
assert float.sqrt(9.0) == Ok(3.0)
pow(base: Float, exponent: Float) -> Result(Float, MathError)
fn(Float, Float) -> Result(Float, pooka/float.MathError)Compute a floating-point power, keeping runtime domain failures in Result.
import pooka/float
assert float.pow(2.0, 3.0) == Ok(8.0)
is_finite(value: Float) -> Bool
fn(Float) -> BoolApply the library's finite-value predicate to a BEAM float.
import pooka/float
assert float.is_finite(1.0)
is_nan(value: Float) -> Bool
fn(Float) -> BoolTest whether the value fails self-equality.
import pooka/float
assert !float.is_nan(1.0)