gleam_community/maths/predicates


Predicates: A module containing functions for testing various mathematical properties of numbers.

Functions

pub fn all_close(xarr: List(Float), yarr: List(Float), rtol: Float, atol: Float) -> Result(
  List(Bool),
  String,
)

Determine if a list of values are close to or equivalent to a another list of reference values.

Example:
 import gleeunit/should
 import gleam/list
 import gleam_community/maths/tests

 pub fn example () {
   let val: Float = 99.
   let ref_val: Float = 100.
   let xarr: List(Float) = list.repeat(val, 42)
   let yarr: List(Float) = list.repeat(ref_val, 42)
   // We set 'atol' and 'rtol' such that the values are equivalent
   // if 'val' is within 1 percent of 'ref_val' +/- 0.1
   let rtol: Float = 0.01
   let atol: Float = 0.10
   tests.all_close(xarr, yarr, rtol, atol)
   |> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
     case zarr {
       Ok(arr) ->
         arr
         |> list.all(fn(a: Bool) -> Bool { a })
         |> Ok
       _ -> Nil |> Error
     }
   }
   |> should.equal(Ok(True))
 }
pub fn is_close(a: Float, b: Float, rtol: Float, atol: Float) -> Bool

Determine if a given value $$a$$ is close to or equivalent to a reference value $$b$$ based on supplied relative $$r_{tol}$$ and absolute $$a_{tol}$$ tolerance values. The equivalance of the two given values are then determined based on the equation:

\[ |a - b| \leq (a_{tol} + r_{tol} \cdot |b|) \]

True is returned if statement holds, otherwise False is returned.

Example
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example () {
   let val: Float = 99.
   let ref_val: Float = 100.
   // We set 'atol' and 'rtol' such that the values are equivalent
   // if 'val' is within 1 percent of 'ref_val' +/- 0.1
   let rtol: Float = 0.01
   let atol: Float = 0.10
   floatx.is_close(val, ref_val, rtol, atol)
   |> should.be_true()
 }
pub fn is_even(x: Int) -> Bool

A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is even.

Example:
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example() {
   tests.is_even(-3)
   |> should.equal(False)
 
   tests.is_even(-4)
   |> should.equal(True)
 }
pub fn is_fractional(x: Float) -> Bool

Determine if a given value is fractional.

True is returned if the given value is fractional, otherwise False is returned.

Example
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example () {
   tests.is_fractional(0.3333)
   |> should.equal(True)
   
   tests.is_fractional(1.0)
   |> should.equal(False)
 }
pub fn is_odd(x: Int) -> Bool

A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is odd.

Example:
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example() {
   tests.is_odd(-3)
   |> should.equal(True)
 
   tests.is_odd(-4)
   |> should.equal(False)
 }
pub fn is_perfect(n: Int) -> Bool

A function that tests whether a given integer value $$n \in \mathbb{Z}$$ is a perfect number. A number is perfect if it is equal to the sum of its proper positive divisors.

Details

For example:

  • $$6$$ is a perfect number since the divisors of 6 are $$1 + 2 + 3 = 6$$.
  • $$28$$ is a perfect number since the divisors of 28 are $$1 + 2 + 4 + 7 + 14 = 28$$
Example:
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example() {
   tests.is_perfect(6)
   |> should.equal(True)

   tests.is_perfect(28)
   |> should.equal(True)
 }
pub fn is_power(x: Int, y: Int) -> Bool

A function that tests whether a given integer value $$x \in \mathbb{Z}$$ is a power of another integer value $$y \in \mathbb{Z}$$.

Example:
 import gleeunit/should
 import gleam_community/maths/tests

 pub fn example() {
   // Check if 4 is a power of 2 (it is)
   tests.is_power(4, 2)
   |> should.equal(True)

   // Check if 5 is a power of 2 (it is not)
   tests.is_power(5, 2)
   |> should.equal(False)
 }
Search Document