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)                              # => 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

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

abs(x: Int) -> Int

 fn abs(x: Int) -> Int

Group tag consumed by Cure.Stdlib.Preload. Absolute value. Implemented as :erlang.abs/1.

ceil(x: Float) -> Float

 fn ceil(x: Float) -> Float

Smallest integer float >= x; :math.ceil/1.

clamp(value: Int, lo: Int, hi: Int) -> Int

 fn clamp(value: Int, lo: Int, hi: Int) -> Int

Clamp value into the inclusive range [lo, hi].

floor(x: Float) -> Float

 fn floor(x: Float) -> Float

Greatest integer float <= x; :math.floor/1.

is_even(x: Int) -> Bool

 fn is_even(x: Int) -> Bool

true when x is even.

is_odd(x: Int) -> Bool

 fn is_odd(x: Int) -> Bool

true when x is odd.

log2(x: Float) -> Float

 fn log2(x: Float) -> Float

Base-2 logarithm; :math.log2/1.

log10(x: Float) -> Float

 fn log10(x: Float) -> Float

Base-10 logarithm; :math.log10/1.

log(x: Float) -> Float

 fn log(x: Float) -> Float

Natural logarithm; :math.log/1.

max(a: Int, b: Int) -> Int

 fn max(a: Int, b: Int) -> Int

Integer maximum.

min(a: Int, b: Int) -> Int

 fn min(a: Int, b: Int) -> Int

Integer minimum.

negate(x: Int) -> Int

 fn negate(x: Int) -> Int

Additive inverse.

pi() -> Float

 fn pi() -> Float

The mathematical constant pi; :math.pi/0.

pow(base: Float, exp: Float) -> Float

 fn pow(base: Float, exp: Float) -> Float

base raised to exp; :math.pow/2.

round(x: Float) -> Int

 fn round(x: Float) -> Int

Round a float to the nearest integer using banker's rounding. Implemented as :erlang.round/1.

safe_div(a: Int, b: Int) -> Int

 fn safe_div(a: Int, b: Int) -> Int

Integer division that returns 0 on divide-by-zero instead of raising. Truncates toward zero otherwise.

sign(x: Int) -> Int

 fn sign(x: Int) -> Int

Sign of x: 1 for positive, 0 - 1 for negative, 0 for zero.

sqrt(x: Float) -> Float

 fn sqrt(x: Float) -> Float

Square root; :math.sqrt/1.