Std.Math
View source →Mathematical operations.
A thin veneer over Erlang's :math and :erlang BIFs plus a
handful of small integer helpers written in Cure. Float
functions (sqrt, pow, log, ...) operate on IEEE-754
doubles; integer helpers expect Int.
Examples
use Std.Math
abs(0 - 5) # => 5
sqrt(16.0) # => 4.0
pow(2.0, 10.0) # => 1024.0
clamp(42, 0, 10) # => 10
is_even(8) # => true
use Std.Math
# Compute the length of the hypotenuse of a 3-4 right triangle.
sqrt(pow(3.0, 2.0) + pow(4.0, 2.0)) # => 5.0
Functions
-
# fn __group__() -> Atom
Group tag consumed by
Cure.Stdlib.Preload. -
# fn abs(x: Int) -> Int extern
Absolute value. Implemented as
:erlang.abs/1. -
# fn ceil(x: Float) -> Float extern
Smallest integer float >=
x;:math.ceil/1. -
# fn clamp(value: Int, lo: Int, hi: Int) -> Int
Clamp
valueinto the inclusive range[lo, hi]. -
# fn floor(x: Float) -> Float extern
Greatest integer float <=
x;:math.floor/1. -
# fn is_even(x: Int) -> Bool
truewhenxis even. -
# fn is_odd(x: Int) -> Bool
truewhenxis odd. -
# fn log(x: Float) -> Float extern
Natural logarithm;
:math.log/1. -
# fn log10(x: Float) -> Float extern
Base-10 logarithm;
:math.log10/1. -
# fn log2(x: Float) -> Float extern
Base-2 logarithm;
:math.log2/1. -
# fn max(a: Int, b: Int) -> Int
Integer maximum.
-
# fn min(a: Int, b: Int) -> Int
Integer minimum.
-
# fn negate(x: Int) -> Int
Additive inverse.
-
# fn pi() -> Float extern
The mathematical constant pi;
:math.pi/0. -
# fn pow(base: Float, exp: Float) -> Float extern
baseraised toexp;:math.pow/2. -
# fn round(x: Float) -> Int extern
Round a float to the nearest integer using banker's rounding. Implemented as
:erlang.round/1. -
# fn safe_div(a: Int, b: Int) -> Int
Integer division that returns
0on divide-by-zero instead of raising. Truncates toward zero otherwise. -
# fn sign(x: Int) -> Int
Sign of
x:1for positive,0 - 1for negative,0for zero. -
# fn sqrt(x: Float) -> Float extern
Square root;
:math.sqrt/1.