fsharp / fsharp/fslang-suggestions
Addition of a correct modulus operator
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Addition of a correct modulus operator
The current remainder operator `%` can be surprising in that the sign of the result follows the sign of the dividend, so does not output periodic results for a fixed divisor.
Changing the current behavior of `%` has been declined in https://github.com/fsharp/fslang-suggestions/issues/417, so another way forward would be to add a new operator,
which, like a true modulus operator, [would output positive results regardless of its inputs](https://math.stackexchange.com/q/2686378).
By always outputting positive results, we would achieve predictability and ease of use for periodic computations.
From F#'s OCaml roots, `mod` is keyword for an infix operator already, and is currently undefined in FSharp.Core:
```fs
let inline (mod) D d =
let r = D % d
if r >= LanguagePrimitives.GenericZero then r
elif d >= LanguagePrimitives.GenericZero then r + d
else r - d
// Usage: -5 mod 3
```
The existing way of approaching this problem in F# is to include such a definition everytime we use the operator, or bite ourselves when trying to use `%` on negative dividends.
## Pros and Cons
The advantages of making this adjustment to F# are
1. Raise awareness of the true modulus operator
2. Less duplicated code across projects
3. Assigning novel uses to existing available operators
The disadvantages of making this adjustment to F# are that the presence of both `%` and `mod` can be confusing. However, upon realisation that `%` stands for the remainder operation instead of modulus, and recognising the difference between the two operations, this can be solved.
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): XS
Related suggestions:
- https://github.com/fsharp/fslang-suggestions/issues/417
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.