Greetings.
I’m going to paste here a post I made on the zig forums and I want to check if a comment that was made there is true or not.
Hello there.
I’m currently developing a game in Zig and ran into an issue using the % operator with isize. The compiler recommended using either @mod or @rem. Out of curiosity, I looked up the equivalent operators in Odin, since I have more experience with it.
According to the Odin documentation:
% modulo (truncated) integers
%% remainder (floored) integers
However, when I tested them, I noticed that the results were swapped compared to what I expected from Zig.
//zig
@mod(-1, 50) == 49
@rem(-1, 50) == -1
//odin
-1 % 50 == -1
-1 %% 50 == 49
Can someone clear things up?
And this is one of the replies that I got and want to check if it’s true:
Odin made a mistake, it’s that simple.
Odin might also have deliberately made a mistake to correspond with C’s common nickname.
In the C standard , % is explicitly defined as the remainder:
The result of the / operator is the quotient from the division of the first operand by the
second; the result of the % operator is the remainder. Inboth operations, if the value of
the second operand is zero, the behavior is undefined.
However, the % in C language is widely and incorrectly referred to as ‘mod’.
Odin might have deliberately reversed their meanings to go along with this mistake.
1 Like
They are not swapped and we are a different language to Zig, and there is no standard naming convention for the differences between modulo and remainders, even in mathematics. However % in Odin is akin to how it works in C and have kept the general naming conventions of C.
So there is no “mixed” naming convention, per se, but rather there is no real convention of what is meant by remainder or modulo, as much as people would like it to be.
3 Likes
gingerBill:
So there is no “mixed” naming convention, per se, but rather there is no real convention of what is meant by remainder or modulo, as much as people would like it to be.
Interesting I never knew that…
Thank for the reply.
1 Like