Merge pull request #11 from sotolf2/main

Implemented euclidian modulo
This commit is contained in:
Nicklas Sindlev Andersen 2024-01-23 14:24:36 +01:00 committed by GitHub
commit 0c01501272
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 0 deletions

View file

@ -96,6 +96,56 @@ fn do_gcd(x: Int, y: Int) -> Int {
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// The function calculates the Euclidian modulo of two numbers
/// The Euclidian_modulo is the modulo that most often is used in maths
/// rather than the normal truncating modulo operation that is used most
/// often in programming through the % operator
/// In contrast to the % operator this function will always return a positive
/// result
///
/// Like the gleam division operator / this will return 0 if one of the
/// parameters are 0 as this is not defined in mathematics
///
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleeunit/should
/// import gleam_community/maths/arithmetics
///
/// pub fn example() {
/// arithmetics.euclidian_modulo(15, 4)
/// |> should.equal(3)
///
/// arithmetics.euclidian_modulo(-3, -2)
/// |> should.equal(1)
///
/// arithmetics.euclidian_modulo(5, 0)
/// |> should.equal(0)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top </small>
/// </a>
/// </div>
///
pub fn euclidian_modulo(x: Int, y: Int) -> Int {
case x % y, x, y {
_, 0, _ -> 0
_, _, 0 -> 0
md, _, _ if md < 0 -> md + int.absolute_value(y)
md, _, _ -> md
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>

View file

@ -21,6 +21,17 @@ pub fn int_gcd_test() {
|> should.equal(6)
}
pub fn euclidian_modulo_test() {
arithmetics.euclidian_modulo(15, 4)
|> should.equal(3)
arithmetics.euclidian_modulo(-3, -2)
|> should.equal(1)
arithmetics.euclidian_modulo(5, 0)
|> should.equal(0)
}
pub fn int_lcm_test() {
arithmetics.lcm(1, 1)
|> should.equal(1)