stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Add float.modulo

Open dalprahcd opened this issue 1 year ago • 3 comments
trafficstars

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?

dalprahcd avatar May 09 '24 10:05 dalprahcd

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?

lpil avatar May 10 '24 08:05 lpil

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.

ghost avatar May 12 '24 09:05 ghost

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.

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

NicklasXYZ avatar Jun 23 '24 11:06 NicklasXYZ

Hello @lpil and @inoas, sorry for being away for so long.

I wish to merge this PR, what are the remaining steps?

ghost avatar Jul 08 '24 09:07 ghost

The comments from my review haven't been addressed yet!

lpil avatar Jul 10 '24 21:07 lpil