mod of negative number

Sergey kornburn at yandex.ru
Tue Sep 24 11:43:37 UTC 2024


On Monday, 23 September 2024 at 19:52:02 UTC, Craig Dillabaugh 
wrote:
> Why does the following program:
>
>     \<code>
>         import std.stdio;
>
>         int main(string[] args) {
>             uint Q = 7681;
>             writeln("Val = ", -1 % Q);
>             return 0;
>         }
>     \</code>
>
>     Print
>         Val = 5568
>
>
> Was hoping for 1.
>
> I assume it is an integer promotion issue, but I am unsure how 
> to resolve.  I tried replacing the Q with to!int(Q) but this 
> gave me -1, which is closer but not right either.

Check section "In Programming languages" - 
https://en.wikipedia.org/wiki/Modulo
There are different operations (remainder and modulus) and 
operators (modulo) which could be combined in a different ways


More information about the Digitalmars-d-learn mailing list