Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add float.modulo #582

Merged
merged 3 commits into from Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/gleam/float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,55 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
@external(javascript, "../gleam_stdlib.mjs", "random_uniform")
pub fn random() -> Float

/// Computes the modulo of an float division of inputs as a `Result`.
///
/// Returns division of the inputs as a `Result`: If the given divisor equals
/// `0`, this function returns an `Error`.
///
/// ## Examples
///
/// ```gleam
/// modulo(3.0, 2.0)
/// // -> Ok(1.0)
/// ```
///
/// ```gleam
/// modulo(1.0, 0.0)
/// // -> Error(Nil)
/// ```
///
/// ```gleam
/// modulo(10.0, -1.0)
/// // -> Ok(0.0)
/// ```
///
/// ```gleam
/// modulo(13.0, by: 3.0)
/// // -> Ok(1.0)
/// ```
///
/// ```gleam
/// modulo(-13.0, by: 3.0)
/// // -> Ok(2.0)
/// ```
///
/// ```gleam
/// modulo(13.0, by: -3.0)
/// // -> Ok(-2.0)
/// ```
///
/// ```gleam
/// modulo(-13.0, by: -3.0)
/// // -> Ok(-1.0)
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have fewer examples please? It's a little overwhelming I think

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments were copied from the int.modulo function, they explain all the possible behaviours.

  • When divisor is non-zero and when it is zero.
  • That the modulo follows the sign of the divisor.

Can we keep the examples?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is too much information here, please reduce it. The examples section is not suitable for an exhaustive description of the behaviour.

///
pub fn modulo(dividend: Float, by divisor: Float) -> Result(Float, Nil) {
case divisor {
0.0 -> Error(Nil)
_ -> Ok(dividend -. floor(dividend /. divisor) *. divisor)
}
}

/// Returns division of the inputs as a `Result`.
///
/// ## Examples
Expand Down
23 changes: 23 additions & 0 deletions test/gleam/float_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,29 @@ pub fn random_test() {
|> should.be_true
}

pub fn modulo_test() {
float.modulo(3.0, 2.0)
|> should.equal(Ok(1.0))

float.modulo(1.0, 0.0)
|> should.equal(Error(Nil))

float.modulo(10.0, -1.0)
|> should.equal(Ok(0.0))

float.modulo(13.0, by: 3.0)
|> should.equal(Ok(1.0))

float.modulo(-13.0, by: 3.0)
|> should.equal(Ok(2.0))

float.modulo(13.0, by: -3.0)
|> should.equal(Ok(-2.0))

float.modulo(-13.0, by: -3.0)
|> should.equal(Ok(-1.0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these are whole numbers. Let's have tests for other numbers too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, could you help me solve this failing test?

Failures:

  1) gleam/float_test.modulo_test
     Values were not equal
     expected: Ok(1.3)
          got: Ok(1.3000000000000007)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check https://hexdocs.pm/gleam_stdlib/gleam/float.html#loosely_equals maybe

But then I believe I would need to import the result library to unwrap the value before, right? Is it ok adding this dependency?

}

pub fn divide_test() {
float.divide(1.0, 1.0)
|> should.equal(Ok(1.0))
Expand Down
Loading