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 value into the inclusive range [lo, hi].

  • # fn floor(x: Float) -> Float extern

    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 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

    base raised to exp; :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 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 extern

    Square root; :math.sqrt/1.