gcalc

A set of algebraic operations for Gleam programs

Types

MathError represents an error that can occur when performing a mathematical operation

pub type MathError {
  DivisionByZero
  ValueOutOfRange
  UnsupportedOperation
}

Constructors

  • DivisionByZero
  • ValueOutOfRange
  • UnsupportedOperation

Functions

pub fn abs(n: Float) -> Float

Returns the absolute value of the argument

pub fn cbrt(n: Float) -> Result(Float, MathError)

Returns a result containing the cube root of the argument

Note: This function only supports non-negative values

pub fn factorial(n: Float) -> Result(Float, MathError)

Returns a result containing the factorial of the argument or an error if the argument is negative

Note: This function only supports non-negative values

pub fn pow(base: Float, power: Float) -> Float

Returns the exponentiation of the two arguments (i.e. the first argument raised to the power of the second argument)

Example:

let base = 2.0
let power = 3.0
pow(base, power) // 8.0
pub fn pow10(n: Float) -> Result(Float, MathError)

Returns a result containing 10^n where n is the argument

pub fn pow2(n: Float) -> Result(Float, MathError)

Returns a result containing 2^n where n is the argument

pub fn sqrt(n: Float) -> Result(Float, MathError)

Returns a result containing the square root of the argument or an error if the argument is negative

Note: This function only supports non-negative values

Example:

sqrt(4.0)             // Ok(2.000000000000002)
|> result.unwrap(0.0) // 2.000000000000002
|> float.floor        // 2.0
Search Document