mod of negative number

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Sep 23 20:14:36 UTC 2024


On Monday, September 23, 2024 1:52:02 PM MDT Craig Dillabaugh via Digitalmars-
d-learn 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.

Well, this is what the spec says:

https://dlang.org/spec/expression.html#division

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list