Update: stdlib v0.28 & gleam v0.28.3

This commit is contained in:
NicklasXYZ 2023-04-22 21:34:10 +02:00
parent 50f5c2845b
commit cd1f8e6249
12 changed files with 259 additions and 193 deletions

View file

@ -18,7 +18,7 @@ jobs:
- uses: erlef/setup-beam@v1
with:
otp-version: "25.0"
gleam-version: "0.26.1"
gleam-version: "0.28.3"
- uses: actions/setup-node@v3.5.1
with:
node-version: "16.18.1"

View file

@ -6,7 +6,7 @@ description = "A basic maths library"
repository = { type = "github", user = "gleam-community", repo = "maths" }
[dependencies]
gleam_stdlib = "~> 0.25"
gleam_stdlib = "~> 0.28"
[dev-dependencies]
gleeunit = "~> 0.7"

View file

@ -2,10 +2,10 @@
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.25.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "AD0F89928E0B919C8F8EDF640484633B28DBF88630A9E6AE504617A3E3E5B9A2" },
{ name = "gleeunit", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A1170754BF54F5DD6E9EF392FB1DC612528B007CCBE41B52F0C5453254708490" },
{ name = "gleam_stdlib", version = "0.28.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "73F0A89FADE5022CBEF6D6C3551F9ADCE7054AFCE0CB1DC4C6D5AB4CA62D0111" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
]
[requirements]
gleam_stdlib = "~> 0.25"
gleam_stdlib = "~> 0.28"
gleeunit = "~> 0.7"

View file

@ -387,7 +387,7 @@ pub fn round(
) -> Result(Float, String) {
case digits {
option.Some(a) -> {
assert Ok(p) = power(10.0, int.to_float(a))
let assert Ok(p) = power(10.0, int.to_float(a))
// Round the given input x using at the specified digit
do_round(p, x, mode)
}
@ -444,7 +444,7 @@ fn round_to_nearest(p: Float, x: Float) -> Float {
case remainder {
_ if remainder >. 0.5 -> sign(x) *. truncate_float(xabs +. 1.0) /. p
_ if remainder == 0.5 -> {
assert Ok(is_even) = int.modulo(to_int(xabs), 2)
let assert Ok(is_even) = int.modulo(to_int(xabs), 2)
case is_even == 0 {
True -> sign(x) *. xabs_truncated /. p
False -> sign(x) *. truncate_float(xabs +. 1.0) /. p
@ -1158,8 +1158,8 @@ pub fn logarithm(x: Float, base: option.Option(Float)) -> Result(Float, String)
case a >. 0.0 && a != 1.0 {
True -> {
// Apply the "change of base formula"
assert Ok(numerator) = logarithm_10(x)
assert Ok(denominator) = logarithm_10(a)
let assert Ok(numerator) = logarithm_10(x)
let assert Ok(denominator) = logarithm_10(a)
numerator /. denominator
|> Ok
}
@ -1480,7 +1480,7 @@ pub fn square_root(x: Float) -> Result(Float, String) {
"Invalid input argument: x < 0."
|> Error
False -> {
assert Ok(result) = power(x, 1.0 /. 2.0)
let assert Ok(result) = power(x, 1.0 /. 2.0)
result
|> Ok
}
@ -1532,7 +1532,7 @@ pub fn cube_root(x: Float) -> Result(Float, String) {
"Invalid input argument: x < 0."
|> Error
False -> {
assert Ok(result) = power(x, 1.0 /. 3.0)
let assert Ok(result) = power(x, 1.0 /. 3.0)
result
|> Ok
}
@ -1589,7 +1589,7 @@ pub fn nth_root(x: Float, n: Int) -> Result(Float, String) {
False ->
case n >= 1 {
True -> {
assert Ok(result) = power(x, 1.0 /. int.to_float(n))
let assert Ok(result) = power(x, 1.0 /. int.to_float(n))
result
|> Ok
}
@ -1634,9 +1634,9 @@ pub fn nth_root(x: Float, n: Int) -> Result(Float, String) {
/// </div>
///
pub fn hypotenuse(x: Float, y: Float) -> Float {
assert Ok(term1) = power(x, 2.0)
assert Ok(term2) = power(y, 2.0)
assert Ok(h) = square_root(term1 +. term2)
let assert Ok(term1) = power(x, 2.0)
let assert Ok(term2) = power(y, 2.0)
let assert Ok(h) = square_root(term1 +. term2)
h
}
@ -2192,8 +2192,8 @@ fn gamma_lanczos(x: Float) -> Float {
},
)
let t: Float = z +. lanczos_g +. 0.5
assert Ok(v1) = power(2.0 *. pi(), 0.5)
assert Ok(v2) = power(t, z +. 0.5)
let assert Ok(v1) = power(2.0 *. pi(), 0.5)
let assert Ok(v2) = power(t, z +. 0.5)
v1 *. v2 *. exponential(-1.0 *. t) *. x
}
}
@ -2219,7 +2219,7 @@ fn gamma_lanczos(x: Float) -> Float {
pub fn incomplete_gamma(a: Float, x: Float) -> Result(Float, String) {
case a >. 0.0 && x >=. 0.0 {
True -> {
assert Ok(v) = power(x, a)
let assert Ok(v) = power(x, a)
v *. exponential(-1.0 *. x) *. incomplete_gamma_sum(
a,
x,

View file

@ -78,7 +78,7 @@ import gleam/iterator
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
///
/// [1.0, 1.0, 1.0]
/// |> float_list.norm(1.0)
@ -107,11 +107,11 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
|> list.fold(
0.0,
fn(acc: Float, a: Float) -> Float {
assert Ok(result) = floatx.power(float.absolute_value(a), p)
let assert Ok(result) = floatx.power(float.absolute_value(a), p)
result +. acc
},
)
assert Ok(result) = floatx.power(agg, 1.0 /. p)
let assert Ok(result) = floatx.power(agg, 1.0 /. p)
result
}
}
@ -141,7 +141,7 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
///
/// // Empty lists returns 0.0
/// float_list.minkowski_distance([], [], 1.0)
@ -155,7 +155,7 @@ pub fn norm(arr: List(Float), p: Float) -> Float {
/// float_list.minkowski_distance([0.0, 0.0], [0.0, 0.0], -1.0)
/// |> should.be_error()
///
/// assert Ok(result) = float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0)
/// let assert Ok(result) = float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0)
/// result
/// |> floatx.is_close(3.0, 0.0, tol)
/// |> should.be_true()
@ -217,7 +217,7 @@ pub fn minkowski_distance(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
///
/// // Empty lists returns 0.0
/// float_list.euclidean_distance([], [])
@ -227,7 +227,7 @@ pub fn minkowski_distance(
/// float_list.euclidean_distance([], [1.0])
/// |> should.be_error()
///
/// assert Ok(result) = float_list.euclidean_distance([0.0, 0.0], [1.0, 2.0])
/// let assert Ok(result) = float_list.euclidean_distance([0.0, 0.0], [1.0, 2.0])
/// result
/// |> floatx.is_close(2.23606797749979, 0.0, tol)
/// |> should.be_true()
@ -269,7 +269,7 @@ pub fn euclidean_distance(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
///
/// // Empty lists returns 0.0
/// float_list.manhatten_distance([], [])
@ -279,7 +279,7 @@ pub fn euclidean_distance(
/// float_list.manhatten_distance([], [1.0])
/// |> should.be_error()
///
/// assert Ok(result) = float_list.manhatten_distance([0.0, 0.0], [1.0, 2.0])
/// let assert Ok(result) = float_list.manhatten_distance([0.0, 0.0], [1.0, 2.0])
/// result
/// |> floatx.is_close(3.0, 0.0, tol)
/// |> should.be_true()
@ -315,9 +315,9 @@ pub fn manhatten_distance(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, True)
/// assert Ok(result) =
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, True)
/// let assert Ok(result) =
/// float_list.all_close(linspace, [10.0, 20.0, 30.0, 40.0, 50.0], 0.0, tol)
/// result
/// |> list.all(fn(x) { x == True })
@ -390,9 +390,9 @@ pub fn linear_space(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, True, 10.0)
/// assert Ok(result) =
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, True, 10.0)
/// let assert Ok(result) =
/// float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol)
/// result
/// |> list.all(fn(x) { x == True })
@ -419,10 +419,10 @@ pub fn logarithmic_space(
) -> Result(List(Float), String) {
case num > 0 {
True -> {
assert Ok(linspace) = linear_space(start, stop, num, endpoint)
let assert Ok(linspace) = linear_space(start, stop, num, endpoint)
linspace
|> list.map(fn(i: Float) -> Float {
assert Ok(result) = floatx.power(base, i)
let assert Ok(result) = floatx.power(base, i)
result
})
|> Ok
@ -450,9 +450,9 @@ pub fn logarithmic_space(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
/// assert Ok(tol) = floatx.power(-10.0, -6.0)
/// assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, True)
/// assert Ok(result) =
/// let assert Ok(tol) = floatx.power(-10.0, -6.0)
/// let assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, True)
/// let assert Ok(result) =
/// float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol)
/// result
/// |> list.all(fn(x) { x == True })
@ -490,8 +490,8 @@ pub fn geometric_space(
False ->
case num > 0 {
True -> {
assert Ok(log_start) = floatx.logarithm_10(start)
assert Ok(log_stop) = floatx.logarithm_10(stop)
let assert Ok(log_start) = floatx.logarithm_10(start)
let assert Ok(log_stop) = floatx.logarithm_10(stop)
logarithmic_space(log_start, log_stop, num, endpoint, 10.0)
}
False ->
@ -791,7 +791,7 @@ pub fn maximum(arr: List(Float)) -> Result(Float, String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val0) = list.at(arr, 0)
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
@ -846,7 +846,7 @@ pub fn minimum(arr: List(Float)) -> Result(Float, String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val0) = list.at(arr, 0)
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
@ -901,7 +901,7 @@ pub fn arg_maximum(arr: List(Float)) -> Result(List(Int), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(max) =
let assert Ok(max) =
arr
|> maximum()
arr
@ -961,7 +961,7 @@ pub fn arg_minimum(arr: List(Float)) -> Result(List(Int), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(min) =
let assert Ok(min) =
arr
|> minimum()
arr
@ -1021,8 +1021,8 @@ pub fn extrema(arr: List(Float)) -> Result(#(Float, Float), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val_max) = list.at(arr, 0)
assert Ok(val_min) = list.at(arr, 0)
let assert Ok(val_max) = list.at(arr, 0)
let assert Ok(val_min) = list.at(arr, 0)
arr
|> list.fold(
#(val_min, val_max),

View file

@ -438,8 +438,8 @@ pub fn permutation(n: Int, k: Int) -> Result(Int, String) {
1
|> Ok
False -> {
assert Ok(v1) = factorial(n)
assert Ok(v2) = factorial(n - k)
let assert Ok(v1) = factorial(n)
let assert Ok(v2) = factorial(n - k)
v1 / v2
|> Ok
}
@ -520,9 +520,9 @@ pub fn absolute_difference(a: Int, b: Int) -> Int {
/// </div>
///
pub fn is_power(x: Int, y: Int) -> Bool {
assert Ok(value) =
let assert Ok(value) =
floatx.logarithm(int.to_float(x), option.Some(int.to_float(y)))
assert Ok(truncated) = floatx.truncate(value, option.Some(0))
let assert Ok(truncated) = floatx.truncate(value, option.Some(0))
let rem = value -. truncated
rem == 0.0
}
@ -654,7 +654,7 @@ pub fn divisors(n: Int) -> List(Int) {
pub fn find_divisors(n: Int) -> List(Int) {
let nabs: Float = float.absolute_value(to_float(n))
assert Ok(sqrt_result) = floatx.square_root(nabs)
let assert Ok(sqrt_result) = floatx.square_root(nabs)
let max: Int = floatx.to_int(sqrt_result) + 1
list.range(2, max)
|> list.fold(
@ -713,7 +713,7 @@ pub fn do_gcd(x: Int, y: Int) -> Int {
case x == 0 {
True -> y
False -> {
assert Ok(z) = int.modulo(y, x)
let assert Ok(z) = int.modulo(y, x)
do_gcd(z, x)
}
}

View file

@ -71,7 +71,7 @@ import gleam_community/maths/int as intx
/// int_list.manhatten_distance([], [1])
/// |> should.be_error()
///
/// assert Ok(result) = int_list.manhatten_distance([0, 0], [1, 2])
/// let assert Ok(result) = int_list.manhatten_distance([0, 0], [1, 2])
/// result
/// |> should.equal(3)
/// }
@ -337,7 +337,7 @@ pub fn arg_minimum(arr: List(Int)) -> Result(List(Int), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(min) =
let assert Ok(min) =
arr
|> minimum()
arr
@ -397,7 +397,7 @@ pub fn arg_maximum(arr: List(Int)) -> Result(List(Int), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(max) =
let assert Ok(max) =
arr
|> maximum()
arr
@ -457,7 +457,7 @@ pub fn maximum(arr: List(Int)) -> Result(Int, String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val0) = list.at(arr, 0)
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
@ -512,7 +512,7 @@ pub fn minimum(arr: List(Int)) -> Result(Int, String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val0) = list.at(arr, 0)
let assert Ok(val0) = list.at(arr, 0)
arr
|> list.fold(
val0,
@ -567,8 +567,8 @@ pub fn extrema(arr: List(Int)) -> Result(#(Int, Int), String) {
"Invalid input argument: The list is empty."
|> Error
_ -> {
assert Ok(val_max) = list.at(arr, 0)
assert Ok(val_min) = list.at(arr, 0)
let assert Ok(val_max) = list.at(arr, 0)
let assert Ok(val_min) = list.at(arr, 0)
arr
|> list.fold(
#(val_min, val_max),

View file

@ -19,10 +19,14 @@
//// .katex { font-size: 1.1em; }
////</style>
////
//// A module containing general utility functions applying to lists.
//// A module containing general functions applying to lists.
////
//// ---
////
//// * **Combinatorial functions**
//// * [`combination`](#combination)
//// * [`permutation`](#permutation)
//// * [`cartesian_product`](#cartesian_product)
//// * **Miscellaneous functions**
//// * [`trim`](#trim)
@ -31,6 +35,7 @@ import gleam/int
import gleam/float
import gleam/set
import gleam/io
import gleam/iterator
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
@ -138,7 +143,24 @@ pub fn combination(arr: List(a), k: Int) -> List(a) {
/// </a>
/// </div>
///
pub fn permutation(arr: List(a)) -> List(a) {
pub fn permutation(arr: List(a)) -> List(List(a)) {
do_permutation(arr, [], [])
}
fn do_permutation(
arr: List(a),
pick_acc: List(a),
acc: List(a),
) -> List(List(a)) {
// arr
// |> iterator.unfold(fn(xarr: List(a)) -> iterator.Step(List(a), List(a)) {
// case xarr {
// [head, ..tail] ->
// iterator.Next(element: [head, ..tail], accumulator: [head, ..tail])
// _ -> iterator.Done
// }
// })
// |> iterator.to_list()
todo
}

View file

@ -19,7 +19,7 @@ pub fn float_list_all_close_test() {
// 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
let atol: Float = 0.1
float_list.all_close(xarr, yarr, rtol, atol)
|> fn(zarr: Result(List(Bool), String)) -> Result(Bool, Nil) {
case zarr {
@ -36,7 +36,7 @@ pub fn float_list_all_close_test() {
}
pub fn float_list_norm_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// An empty lists returns 0.0
[]
@ -82,7 +82,7 @@ pub fn float_list_norm_test() {
}
pub fn float_list_minkowski_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Empty lists returns 0.0
float_list.minkowski_distance([], [], 1.0)
@ -98,44 +98,47 @@ pub fn float_list_minkowski_test() {
// Check that the function agrees, at some arbitrary input
// points, with known function values
assert Ok(result) = float_list.minkowski_distance([1.0, 1.0], [1.0, 1.0], 1.0)
let assert Ok(result) =
float_list.minkowski_distance([1.0, 1.0], [1.0, 1.0], 1.0)
result
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
assert Ok(result) =
let assert Ok(result) =
float_list.minkowski_distance([0.0, 0.0], [1.0, 1.0], 10.0)
result
|> floatx.is_close(1.0717734625362931, 0.0, tol)
|> should.be_true()
assert Ok(result) =
let assert Ok(result) =
float_list.minkowski_distance([0.0, 0.0], [1.0, 1.0], 100.0)
result
|> floatx.is_close(1.0069555500567189, 0.0, tol)
|> should.be_true()
assert Ok(result) =
let assert Ok(result) =
float_list.minkowski_distance([0.0, 0.0], [1.0, 1.0], 10.0)
result
|> floatx.is_close(1.0717734625362931, 0.0, tol)
|> should.be_true()
// Euclidean distance (p = 2)
assert Ok(result) = float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 2.0)
let assert Ok(result) =
float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 2.0)
result
|> floatx.is_close(2.23606797749979, 0.0, tol)
|> should.be_true()
// Manhatten distance (p = 1)
assert Ok(result) = float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0)
let assert Ok(result) =
float_list.minkowski_distance([0.0, 0.0], [1.0, 2.0], 1.0)
result
|> floatx.is_close(3.0, 0.0, tol)
|> should.be_true()
}
pub fn float_list_euclidean_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Empty lists returns 0.0
float_list.euclidean_distance([], [])
@ -146,14 +149,14 @@ pub fn float_list_euclidean_test() {
|> should.be_error()
// Euclidean distance (p = 2)
assert Ok(result) = float_list.euclidean_distance([0.0, 0.0], [1.0, 2.0])
let assert Ok(result) = float_list.euclidean_distance([0.0, 0.0], [1.0, 2.0])
result
|> floatx.is_close(2.23606797749979, 0.0, tol)
|> should.be_true()
}
pub fn float_list_manhatten_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Empty lists returns 0.0
float_list.manhatten_distance([], [])
@ -164,27 +167,27 @@ pub fn float_list_manhatten_test() {
|> should.be_error()
// Manhatten distance (p = 1)
assert Ok(result) = float_list.manhatten_distance([0.0, 0.0], [1.0, 2.0])
let assert Ok(result) = float_list.manhatten_distance([0.0, 0.0], [1.0, 2.0])
result
|> floatx.is_close(3.0, 0.0, tol)
|> should.be_true()
}
pub fn float_list_linear_space_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
// ---> With endpoint included
assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, True)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, True)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, 20.0, 30.0, 40.0, 50.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
assert Ok(linspace) = float_list.linear_space(10.0, 20.0, 5, True)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, 20.0, 5, True)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, 12.5, 15.0, 17.5, 20.0], 0.0, tol)
result
@ -193,16 +196,16 @@ pub fn float_list_linear_space_test() {
// Try with negative stop
// ----> Without endpoint included
assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, False)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, 50.0, 5, False)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, 18.0, 26.0, 34.0, 42.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
assert Ok(linspace) = float_list.linear_space(10.0, 20.0, 5, False)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, 20.0, 5, False)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, 12.0, 14.0, 16.0, 18.0], 0.0, tol)
result
@ -210,16 +213,16 @@ pub fn float_list_linear_space_test() {
|> should.be_true()
// Try with negative stop
assert Ok(linspace) = float_list.linear_space(10.0, -50.0, 5, False)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, -50.0, 5, False)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, -2.0, -14.0, -26.0, -38.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
assert Ok(linspace) = float_list.linear_space(10.0, -20.0, 5, True)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(10.0, -20.0, 5, True)
let assert Ok(result) =
float_list.all_close(linspace, [10.0, 2.5, -5.0, -12.5, -20.0], 0.0, tol)
result
@ -227,16 +230,16 @@ pub fn float_list_linear_space_test() {
|> should.be_true()
// Try with negative start
assert Ok(linspace) = float_list.linear_space(-10.0, 50.0, 5, False)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(-10.0, 50.0, 5, False)
let assert Ok(result) =
float_list.all_close(linspace, [-10.0, 2.0, 14.0, 26.0, 38.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
assert Ok(linspace) = float_list.linear_space(-10.0, 20.0, 5, True)
assert Ok(result) =
let assert Ok(linspace) = float_list.linear_space(-10.0, 20.0, 5, True)
let assert Ok(result) =
float_list.all_close(linspace, [-10.0, -2.5, 5.0, 12.5, 20.0], 0.0, tol)
// A negative number of points does not work (-5)
@ -245,45 +248,50 @@ pub fn float_list_linear_space_test() {
}
pub fn float_list_logarithmic_space_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
// ---> With endpoint included
// - Positive start, stop, base
assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, True, 10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(1.0, 3.0, 3, True, 10.0)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive start, stop, negative base
assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, True, -10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(1.0, 3.0, 3, True, -10.0)
let assert Ok(result) =
float_list.all_close(logspace, [-10.0, 100.0, -1000.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive start, negative stop, base
assert Ok(logspace) = float_list.logarithmic_space(1.0, -3.0, 3, True, -10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(1.0, -3.0, 3, True, -10.0)
let assert Ok(result) =
float_list.all_close(logspace, [-10.0, -0.1, -0.001], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive start, base, negative stop
assert Ok(logspace) = float_list.logarithmic_space(1.0, -3.0, 3, True, 10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(1.0, -3.0, 3, True, 10.0)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 0.1, 0.001], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive stop, base, negative start
assert Ok(logspace) = float_list.logarithmic_space(-1.0, 3.0, 3, True, 10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(-1.0, 3.0, 3, True, 10.0)
let assert Ok(result) =
float_list.all_close(logspace, [0.1, 10.0, 1000.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
@ -291,8 +299,9 @@ pub fn float_list_logarithmic_space_test() {
// ----> Without endpoint included
// - Positive start, stop, base
assert Ok(logspace) = float_list.logarithmic_space(1.0, 3.0, 3, False, 10.0)
assert Ok(result) =
let assert Ok(logspace) =
float_list.logarithmic_space(1.0, 3.0, 3, False, 10.0)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 46.41588834, 215.443469], 0.0, tol)
result
|> list.all(fn(x) { x == True })
@ -304,29 +313,29 @@ pub fn float_list_logarithmic_space_test() {
}
pub fn float_list_geometric_space_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
// ---> With endpoint included
// - Positive start, stop
assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, True)
assert Ok(result) =
let assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, True)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 100.0, 1000.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive start, negative stop
assert Ok(logspace) = float_list.geometric_space(10.0, 0.001, 3, True)
assert Ok(result) =
let assert Ok(logspace) = float_list.geometric_space(10.0, 0.001, 3, True)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 0.1, 0.001], 0.0, tol)
result
|> list.all(fn(x) { x == True })
|> should.be_true()
// - Positive stop, negative start
assert Ok(logspace) = float_list.geometric_space(0.1, 1000.0, 3, True)
assert Ok(result) =
let assert Ok(logspace) = float_list.geometric_space(0.1, 1000.0, 3, True)
let assert Ok(result) =
float_list.all_close(logspace, [0.1, 10.0, 1000.0], 0.0, tol)
result
|> list.all(fn(x) { x == True })
@ -334,8 +343,8 @@ pub fn float_list_geometric_space_test() {
// ----> Without endpoint included
// - Positive start, stop
assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, False)
assert Ok(result) =
let assert Ok(logspace) = float_list.geometric_space(10.0, 1000.0, 3, False)
let assert Ok(result) =
float_list.all_close(logspace, [10.0, 46.41588834, 215.443469], 0.0, tol)
result
|> list.all(fn(x) { x == True })

View file

@ -10,15 +10,15 @@ pub fn main() {
}
pub fn float_acos_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
assert Ok(result) = floatx.acos(1.0)
let assert Ok(result) = floatx.acos(1.0)
result
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
assert Ok(result) = floatx.acos(0.5)
let assert Ok(result) = floatx.acos(0.5)
result
|> floatx.is_close(1.047197, 0.0, tol)
|> should.be_true()
@ -33,10 +33,10 @@ pub fn float_acos_test() {
}
pub fn float_acosh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
assert Ok(result) = floatx.acosh(1.0)
let assert Ok(result) = floatx.acosh(1.0)
result
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
@ -53,8 +53,8 @@ pub fn float_asin_test() {
floatx.asin(0.0)
|> should.equal(Ok(0.0))
assert Ok(tol) = floatx.power(-10.0, -6.0)
assert Ok(result) = floatx.asin(0.5)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(result) = floatx.asin(0.5)
result
|> floatx.is_close(0.523598, 0.0, tol)
|> should.be_true()
@ -69,7 +69,7 @@ pub fn float_asin_test() {
}
pub fn float_asinh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.asinh(0.0)
@ -82,7 +82,7 @@ pub fn float_asinh_test() {
}
pub fn float_atan_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.atan(0.0)
@ -95,7 +95,7 @@ pub fn float_atan_test() {
}
pub fn math_atan2_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.atan2(0.0, 0.0)
@ -143,15 +143,15 @@ pub fn math_atan2_test() {
}
pub fn float_atanh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
assert Ok(result) = floatx.atanh(0.0)
let assert Ok(result) = floatx.atanh(0.0)
result
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
assert Ok(result) = floatx.atanh(0.5)
let assert Ok(result) = floatx.atanh(0.5)
result
|> floatx.is_close(0.549306, 0.0, tol)
|> should.be_true()
@ -172,7 +172,7 @@ pub fn float_atanh_test() {
}
pub fn float_cos_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.cos(0.0)
@ -189,7 +189,7 @@ pub fn float_cos_test() {
}
pub fn float_cosh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.cosh(0.0)
@ -206,7 +206,7 @@ pub fn float_cosh_test() {
}
pub fn float_exponential_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.exponential(0.0)
@ -223,13 +223,13 @@ pub fn float_exponential_test() {
}
pub fn float_natural_logarithm_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.natural_logarithm(1.0)
|> should.equal(Ok(0.0))
assert Ok(result) = floatx.natural_logarithm(0.5)
let assert Ok(result) = floatx.natural_logarithm(0.5)
result
|> floatx.is_close(-0.693147, 0.0, tol)
|> should.be_true()
@ -241,22 +241,22 @@ pub fn float_natural_logarithm_test() {
}
pub fn float_logarithm_10_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
assert Ok(result) = floatx.logarithm_10(1.0)
let assert Ok(result) = floatx.logarithm_10(1.0)
result
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
assert Ok(result) = floatx.logarithm_10(10.0)
let assert Ok(result) = floatx.logarithm_10(10.0)
result
|> floatx.is_close(1.0, 0.0, tol)
|> should.be_true()
assert Ok(result) = floatx.logarithm_10(50.0)
let assert Ok(result) = floatx.logarithm_10(50.0)
result
|> floatx.is_close(1.698970, 0.0, tol)
|> floatx.is_close(1.69897, 0.0, tol)
|> should.be_true()
// Check that we get an error when the function is evaluated
@ -266,7 +266,7 @@ pub fn float_logarithm_10_test() {
}
pub fn float_logarithm_2_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.logarithm_2(1.0)
@ -275,7 +275,7 @@ pub fn float_logarithm_2_test() {
floatx.logarithm_2(2.0)
|> should.equal(Ok(1.0))
assert Ok(result) = floatx.logarithm_2(5.0)
let assert Ok(result) = floatx.logarithm_2(5.0)
result
|> floatx.is_close(2.321928, 0.0, tol)
|> should.be_true()
@ -405,7 +405,7 @@ pub fn float_nth_root_test() {
}
pub fn float_hypotenuse_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
floatx.hypotenuse(0.0, 0.0)
|> should.equal(0.0)
@ -423,7 +423,7 @@ pub fn float_hypotenuse_test() {
}
pub fn float_sin_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.sin(0.0)
@ -440,7 +440,7 @@ pub fn float_sin_test() {
}
pub fn float_sinh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.sinh(0.0)
@ -457,7 +457,7 @@ pub fn float_sinh_test() {
}
pub fn math_tan_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.tan(0.0)
@ -470,7 +470,7 @@ pub fn math_tan_test() {
}
pub fn math_tanh_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
// Check that the function agrees, at some arbitrary input
// points, with known function values
floatx.tanh(0.0)
@ -491,7 +491,7 @@ pub fn math_tanh_test() {
}
pub fn float_to_degree_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
floatx.to_degree(0.0)
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
@ -502,7 +502,7 @@ pub fn float_to_degree_test() {
}
pub fn float_to_radian_test() {
assert Ok(tol) = floatx.power(-10.0, -6.0)
let assert Ok(tol) = floatx.power(-10.0, -6.0)
floatx.to_radian(0.0)
|> floatx.is_close(0.0, 0.0, tol)
|> should.be_true()
@ -694,26 +694,26 @@ pub fn float_gamma_function_test() {
pub fn math_round_to_nearest_test() {
// Try with positive values
floatx.round(1.50, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(1.5, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(2.0))
floatx.round(1.75, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(2.0))
floatx.round(2.00, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(2.0, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(2.0))
floatx.round(3.50, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(3.5, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(4.0))
floatx.round(4.50, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(4.5, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(4.0))
// Try with negative values
floatx.round(-3.50, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(-3.5, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(-4.0))
floatx.round(-4.50, option.Some(0), option.Some(floatx.RoundNearest))
floatx.round(-4.5, option.Some(0), option.Some(floatx.RoundNearest))
|> should.equal(Ok(-4.0))
// Round 3. digit AFTER decimal point
@ -751,13 +751,13 @@ pub fn math_round_up_test() {
floatx.round(0.45, option.Some(0), option.Some(floatx.RoundUp))
|> should.equal(Ok(1.0))
floatx.round(0.50, option.Some(0), option.Some(floatx.RoundUp))
floatx.round(0.5, option.Some(0), option.Some(floatx.RoundUp))
|> should.equal(Ok(1.0))
floatx.round(0.45, option.Some(1), option.Some(floatx.RoundUp))
|> should.equal(Ok(0.5))
floatx.round(0.50, option.Some(1), option.Some(floatx.RoundUp))
floatx.round(0.5, option.Some(1), option.Some(floatx.RoundUp))
|> should.equal(Ok(0.5))
floatx.round(0.455, option.Some(2), option.Some(floatx.RoundUp))
@ -770,19 +770,19 @@ pub fn math_round_up_test() {
floatx.round(-0.45, option.Some(0), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.0))
floatx.round(-0.50, option.Some(0), option.Some(floatx.RoundUp))
floatx.round(-0.5, option.Some(0), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.0))
floatx.round(-0.45, option.Some(1), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.4))
floatx.round(-0.50, option.Some(1), option.Some(floatx.RoundUp))
floatx.round(-0.5, option.Some(1), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.5))
floatx.round(-0.4550, option.Some(2), option.Some(floatx.RoundUp))
floatx.round(-0.455, option.Some(2), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.45))
floatx.round(-0.5050, option.Some(2), option.Some(floatx.RoundUp))
floatx.round(-0.505, option.Some(2), option.Some(floatx.RoundUp))
|> should.equal(Ok(-0.5))
}
@ -792,45 +792,45 @@ pub fn math_round_down_test() {
floatx.round(0.45, option.Some(0), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.0))
floatx.round(0.50, option.Some(0), option.Some(floatx.RoundDown))
floatx.round(0.5, option.Some(0), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.0))
floatx.round(0.45, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.4))
floatx.round(0.50, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.50))
floatx.round(0.5, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.5))
floatx.round(0.4550, option.Some(2), option.Some(floatx.RoundDown))
floatx.round(0.455, option.Some(2), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.45))
floatx.round(0.5050, option.Some(2), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.50))
floatx.round(0.505, option.Some(2), option.Some(floatx.RoundDown))
|> should.equal(Ok(0.5))
// Try with negative values
floatx.round(-0.45, option.Some(0), option.Some(floatx.RoundDown))
|> should.equal(Ok(-1.0))
floatx.round(-0.50, option.Some(0), option.Some(floatx.RoundDown))
floatx.round(-0.5, option.Some(0), option.Some(floatx.RoundDown))
|> should.equal(Ok(-1.0))
floatx.round(-0.45, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(-0.5))
floatx.round(-0.50, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(-0.50))
floatx.round(-0.5, option.Some(1), option.Some(floatx.RoundDown))
|> should.equal(Ok(-0.5))
floatx.round(-0.4550, option.Some(2), option.Some(floatx.RoundDown))
floatx.round(-0.455, option.Some(2), option.Some(floatx.RoundDown))
|> should.equal(Ok(-0.46))
floatx.round(-0.5050, option.Some(2), option.Some(floatx.RoundDown))
floatx.round(-0.505, option.Some(2), option.Some(floatx.RoundDown))
|> should.equal(Ok(-0.51))
}
pub fn math_round_to_zero_test() {
// Note: Rounding mode "RoundToZero" is an alias for the truncate function
// Try with positive values
floatx.round(0.50, option.Some(0), option.Some(floatx.RoundToZero))
floatx.round(0.5, option.Some(0), option.Some(floatx.RoundToZero))
|> should.equal(Ok(0.0))
floatx.round(0.75, option.Some(0), option.Some(floatx.RoundToZero))
@ -840,16 +840,16 @@ pub fn math_round_to_zero_test() {
|> should.equal(Ok(0.4))
floatx.round(0.57, option.Some(1), option.Some(floatx.RoundToZero))
|> should.equal(Ok(0.50))
|> should.equal(Ok(0.5))
floatx.round(0.4575, option.Some(2), option.Some(floatx.RoundToZero))
|> should.equal(Ok(0.45))
floatx.round(0.5075, option.Some(2), option.Some(floatx.RoundToZero))
|> should.equal(Ok(0.50))
|> should.equal(Ok(0.5))
// Try with negative values
floatx.round(-0.50, option.Some(0), option.Some(floatx.RoundToZero))
floatx.round(-0.5, option.Some(0), option.Some(floatx.RoundToZero))
|> should.equal(Ok(0.0))
floatx.round(-0.75, option.Some(0), option.Some(floatx.RoundToZero))
@ -859,37 +859,37 @@ pub fn math_round_to_zero_test() {
|> should.equal(Ok(-0.4))
floatx.round(-0.57, option.Some(1), option.Some(floatx.RoundToZero))
|> should.equal(Ok(-0.50))
|> should.equal(Ok(-0.5))
floatx.round(-0.4575, option.Some(2), option.Some(floatx.RoundToZero))
|> should.equal(Ok(-0.45))
floatx.round(-0.5075, option.Some(2), option.Some(floatx.RoundToZero))
|> should.equal(Ok(-0.50))
|> should.equal(Ok(-0.5))
}
pub fn math_round_ties_away_test() {
// Try with positive values
floatx.round(1.40, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(1.4, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(1.0))
floatx.round(1.50, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(1.5, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(2.0))
floatx.round(2.50, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(2.5, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(3.0))
// Try with negative values
floatx.round(-1.40, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(-1.4, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(-1.0))
floatx.round(-1.50, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(-1.5, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(-2.0))
floatx.round(-2.00, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(-2.0, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(-2.0))
floatx.round(-2.50, option.Some(0), option.Some(floatx.RoundTiesAway))
floatx.round(-2.5, option.Some(0), option.Some(floatx.RoundTiesAway))
|> should.equal(Ok(-3.0))
// Round 3. digit AFTER decimal point
@ -923,26 +923,26 @@ pub fn math_round_ties_away_test() {
pub fn math_round_ties_up_test() {
// Try with positive values
floatx.round(1.40, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(1.4, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(1.0))
floatx.round(1.50, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(1.5, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(2.0))
floatx.round(2.50, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(2.5, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(3.0))
// Try with negative values
floatx.round(-1.40, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(-1.4, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(-1.0))
floatx.round(-1.50, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(-1.5, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(-1.0))
floatx.round(-2.00, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(-2.0, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(-2.0))
floatx.round(-2.50, option.Some(0), option.Some(floatx.RoundTiesUp))
floatx.round(-2.5, option.Some(0), option.Some(floatx.RoundTiesUp))
|> should.equal(Ok(-2.0))
// Round 3. digit AFTER decimal point
@ -1058,7 +1058,7 @@ pub fn float_is_close_test() {
// 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
let atol: Float = 0.1
floatx.is_close(val, ref_val, rtol, atol)
|> should.be_true()
}

View file

@ -142,7 +142,7 @@ pub fn int_list_manhatten_test() {
int_list.manhatten_distance([], [1])
|> should.be_error()
assert Ok(result) = int_list.manhatten_distance([0, 0], [1, 2])
let assert Ok(result) = int_list.manhatten_distance([0, 0], [1, 2])
result
|> should.equal(3)
}

View file

@ -4,6 +4,8 @@ import gleam/pair
import gleam_community/maths/list as listx
import gleeunit
import gleeunit/should
import gleam/io
import gleam/set
pub fn main() {
gleeunit.main()
@ -30,7 +32,8 @@ pub fn list_cartesian_product_test() {
// Test with some arbitrary inputs
[1, 2, 3]
|> listx.cartesian_product([1, 2, 3])
|> should.equal([
|> set.from_list()
|> should.equal(set.from_list([
#(1, 1),
#(1, 2),
#(1, 3),
@ -40,9 +43,41 @@ pub fn list_cartesian_product_test() {
#(3, 1),
#(3, 2),
#(3, 3),
])
]))
[1.0, 10.0]
|> listx.cartesian_product([1.0, 2.0])
|> should.equal([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)])
|> set.from_list()
|> should.equal(set.from_list([
#(1.0, 1.0),
#(1.0, 2.0),
#(10.0, 1.0),
#(10.0, 2.0),
]))
}
pub fn list_permutation_test() {
io.debug("TODO: Implement tests for 'list.permutation'.")
// // An empty lists returns an empty list
// []
// |> listx.permutation([])
// |> should.equal([[]])
// Test with some arbitrary inputs
// [1, 2]
// |> listx.permutation()
// should.be_error(Ok(1))
// |> should.equal([[1, 2], [2, 1]])
// // Test with some arbitrary inputs
// [1, 2, 3]
// |> listx.permutation()
// |> should.equal([
// [1, 2, 3],
// [2, 1, 3],
// [3, 1, 2],
// [1, 3, 2],
// [2, 3, 1],
// [3, 2, 1],
// ])
}