Skip to content

mod #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ktye opened this issue Dec 29, 2022 · 5 comments
Open

mod #30

ktye opened this issue Dec 29, 2022 · 5 comments

Comments

@ktye
Copy link
Owner

ktye commented Dec 29, 2022

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

should be
0 1 2 0 1 2 0

@ktye ktye closed this as completed in ffa361f Nov 11, 2023
@ktye
Copy link
Owner Author

ktye commented Nov 11, 2023

modulo is euclidean now, but idiv is still truncated

@ktye
Copy link
Owner Author

ktye commented Nov 12, 2023

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 ktye reopened this Nov 12, 2023
@ktye
Copy link
Owner Author

ktye commented Nov 12, 2023

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
Copy link
Owner Author

ktye commented Nov 12, 2023

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
Copy link
Owner Author

ktye commented Nov 12, 2023

{r:!0;while[~y?0 -1;r,:x!y;y:y%x;|r]}    /atomic x y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant