Skip to content
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

wrapping negation -% should work on unsigned ints #1770

Closed
thejoshwolfe opened this issue Nov 21, 2018 · 3 comments · Fixed by #8599
Closed

wrapping negation -% should work on unsigned ints #1770

thejoshwolfe opened this issue Nov 21, 2018 · 3 comments · Fixed by #8599
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@thejoshwolfe
Copy link
Contributor

This is used in C in these algorithms: http://www.pcg-random.org/posts/bounded-rands.html (search for "-range").

Semantics:

const U = u32;
const I = i32;
var u: U = foo();
assert(-%u == @bitCast(U, -%@bitCast(I, u)));
assert(-%u == ~u +% 1);

I'm trying to port this code to Zig, and the workarounds with @bitCast or ~ obscure the origin and intent of the code.

@thejoshwolfe thejoshwolfe added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 21, 2018
@andrewrk andrewrk added this to the 0.5.0 milestone Nov 21, 2018
@andrewrk
Copy link
Member

I'm not convinced. Everywhere else in Zig, operations such as negation refer to the mathematical meaning, not a bitwise interpretation. Negation of an unsigned integer does not make sense mathematically. The @bitCast code above makes it clear.

I will re-open this issue if a counter-argument is provided.

@daurnimator
Copy link
Contributor

Negation of an unsigned integer does not make sense mathematically

-%u should be equivalent to 0 -% u.

@andrewrk
Copy link
Member

OK, when you put it that way, it makes sense, and I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants