-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Add float.modulo #582
Conversation
If round returned a float how would you round a float to an int? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
/// ```gleam | ||
/// modulo(-13.0, by: -3.0) | ||
/// // -> Ok(-1.0) | ||
/// ``` |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
test/gleam/float_test.gleam
Outdated
|> should.equal(Ok(-2.0)) | ||
|
||
float.modulo(-13.0, by: -3.0) | ||
|> should.equal(Ok(-1.0)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
We could explicit convert float to int by implementing Finally, we would have 3 options to round a float to int: Calling |
This is how I've implemented it in the the community math lib :) |
The comments from my review haven't been addressed yet! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Should close #525 by adding the
float.modulo
function.I also tried implementing the
float.remainder
function as:But
round
returns anInt
and I could not find an easy way to convert it back toFloat
. A little bit off-topic, but why does it return anInt
it should return aFloat
right?