stdlib
stdlib copied to clipboard
Add float.modulo
Should close #525 by adding the float.modulo function.
I also tried implementing the float.remainder function as:
pub fn modulo(dividend: Float, by divisor: Float) -> Result(Float, Nil) {
case divisor {
0.0 -> Error(Nil)
_ -> Ok(dividend -. round(dividend /. divisor) *. divisor)
}
}
But round returns an Int and I could not find an easy way to convert it back to Float. A little bit off-topic, but why does it return an Int it should return a Float right?
A little bit off-topic, but why does it return an Int it should return a Float right?
If round returned a float how would you round a float to an int?
If round returned a float how would you round a float to an int?
We could explicit convert float to int by implementing int.to_float and float.to_int (if not already implemented). Then we could also expand the float.round function to accept how many decimals places we want to round the float.
Finally, we would have 3 options to round a float to int: float.floor, float.ceiling, float.round(x, 0). And the user would be responsible to choose which one is more suitable.
Calling float.to_int would essentially cut out the decimal places.
If round returned a float how would you round a float to an int?
We could explicit convert float to int by implementing
int.to_floatandfloat.to_int(if not already implemented). Then we could also expand thefloat.roundfunction to accept how many decimals places we want to round the float.Finally, we would have 3 options to round a float to int:
float.floor,float.ceiling,float.round(x, 0). And the user would be responsible to choose which one is more suitable.Calling
float.to_intwould essentially cut out the decimal places.
This is how I've implemented it in the the community math lib :) I guess the question is also related to this closed issue: https://github.com/gleam-lang/stdlib/issues/625#issue-2341689253
Hello @lpil and @inoas, sorry for being away for so long.
I wish to merge this PR, what are the remaining steps?
The comments from my review haven't been addressed yet!