enforce (i > 0) for i = int.min does not throw

Azi Hassan azi.hassan at live.fr
Wed Jan 31 23:19:32 UTC 2018


On Saturday, 27 January 2018 at 14:13:49 UTC, kdevel wrote:
> I would expect this code
>
> enforce3.d
> ---
> import std.exception;
>
> void main ()
> {
>    int i = int.min;
>    enforce (i > 0);
> }
> ---
>
> to throw an "Enforcement failed" exception, but it doesn't:
>
> $ dmd enforce3.d
> $ ./enforce3
> [nothing]

I wonder if it's caused by a comparison between signed and 
unsigned integers.

import std.stdio;

void main ()
{
     int zero = 0;
     writeln(int.min > 0u);
     writeln(int.min > zero);
}

$ rdmd test.d
true
false

The same behavior can be observed in C :

#include <stdio.h>
#include <limits.h>

int main(void)
{
     int zero = 0;
     printf("%d\n", INT_MIN > 0u);
     printf("%d\n", INT_MIN > zero);
     return 0;
}

$ gcc test.c && ./a.out
1
0


More information about the Digitalmars-d-learn mailing list