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

Example:

let n = 27.0
cbrt(n) // 3.0
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 fma(x: Float, y: Float, z: Float) -> Float

Returns the result of x*y+z without losing precision in any intermediate result

Example:

let x = 10.0
let y = 20.0
let z = 30.0
fma(x,y,z) // 230.0
pub fn gcd(a: Int, b: Int) -> Int

Returns the greatest common divisor of a and b using the Euclidean Algorithm

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

Example:

let n = 2.0
pow10(n) // Ok(100.0)
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