ktye / i

interpret
100 stars 17 forks source link

mod #30

Open ktye opened 1 year ago

ktye commented 1 year ago

3/-3+!7 0 -2 -1 0 1 2 0

should be 0 1 2 0 1 2 0

ktye commented 10 months ago

modulo is euclidean now, but idiv is still truncated

ktye commented 10 months ago

encode of negative numbers has to be fixed as well.

 8/8\-234
350

and division by zero (e.g. may trap on wasm)

ktye commented 10 months ago

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
ktye commented 10 months ago

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 }
ktye commented 10 months ago
{r:!0;while[~y?0 -1;r,:x!y;y:y%x;|r]}    /atomic x y