Std. Math
(Cure Standard Library v0.37.2)
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) # => trueuse 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
Summary
Functions
Group tag consumed by Cure.Stdlib.Preload.
Absolute value. Implemented as :erlang.abs/1.
Smallest integer float >= x; :math.ceil/1.
Clamp value into the inclusive range [lo, hi].
Greatest integer float <= x; :math.floor/1.
true when x is even.
true when x is odd.
Base-2 logarithm; :math.log2/1.
Base-10 logarithm; :math.log10/1.
Natural logarithm; :math.log/1.
Integer maximum.
Integer minimum.
Additive inverse.
The mathematical constant pi; :math.pi/0.
base raised to exp; :math.pow/2.
Round a float to the nearest integer using banker's rounding.
Implemented as :erlang.round/1.
Integer division that returns 0 on divide-by-zero instead of
raising. Truncates toward zero otherwise.
Sign of x: 1 for positive, 0 - 1 for negative, 0 for zero.
Square root; :math.sqrt/1.
Functions
fn abs(x: Int) -> Int
Group tag consumed by Cure.Stdlib.Preload.
Absolute value. Implemented as :erlang.abs/1.
fn ceil(x: Float) -> Float
Smallest integer float >= x; :math.ceil/1.
fn clamp(value: Int, lo: Int, hi: Int) -> Int
Clamp value into the inclusive range [lo, hi].
fn floor(x: Float) -> Float
Greatest integer float <= x; :math.floor/1.
fn is_even(x: Int) -> Bool
true when x is even.
fn is_odd(x: Int) -> Bool
true when x is odd.
fn log2(x: Float) -> Float
Base-2 logarithm; :math.log2/1.
fn log10(x: Float) -> Float
Base-10 logarithm; :math.log10/1.
fn log(x: Float) -> Float
Natural logarithm; :math.log/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
The mathematical constant pi; :math.pi/0.
fn pow(base: Float, exp: Float) -> Float
base raised to exp; :math.pow/2.
fn round(x: Float) -> Int
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 0 on divide-by-zero instead of
raising. Truncates toward zero otherwise.
fn sign(x: Int) -> Int
Sign of x: 1 for positive, 0 - 1 for negative, 0 for zero.
fn sqrt(x: Float) -> Float
Square root; :math.sqrt/1.