Open ktye opened 1 year ago
modulo is euclidean now, but idiv is still truncated
encode of negative numbers has to be fixed as well.
8/8\-234
350
and division by zero (e.g. may trap on wasm)
https://stackoverflow.com/a/29319743
+----+----+-----------+---------+-----------+-----------+---------+-----------+
| x mod y | quotient 'q' | remainder 'r' |
| x | y | truncated | floored | Euclidean | truncated | floored | Euclidean |
+----+----+-----------+---------+-----------+-----------+---------+-----------+
| 5 | 3 | 1 | 1 | 1 | 2 | 2 | 2 |
| -5 | 3 | -1 | -2 | -2 | -2 | 1 | 1 |
| 5 | -3 | -1 | -2 | -1 | 2 | -1 | 2 |
| -5 | -3 | 1 | 1 | 2 | -2 | -2 | 1 |
+----+----+-----------+---------+-----------+-----------+---------+-----------+
^^ ^^
currently:
5 -5 5 -5%3 3 -3 -3
1 -1 -1 1
3 3 -3 -3!'5 -5 5 -5
2 1 2 -5
this does all 4 cases (but not division by 0)
func div(x, y int) int {
r := x % y
if r < 0 {
r += abs(y)
}
return (x - r) / y
}
func mod(x, y int) int { return x - div(x, y)*y }
{r:!0;while[~y?0 -1;r,:x!y;y:y%x;|r]} /atomic x y
3/-3+!7 0 -2 -1 0 1 2 0
should be 0 1 2 0 1 2 0