According to the C standard:

        If the quotient  a/b  is  representable,  the
        expression (a/b)*b + a%b shall equal a.

In your example, dividing x by -1 would make x +2147483648,
which is not representable in int32_t. May be this is
why -2147483648/-1 also exceptions out?

Nitpicking: C's % is a remainder operator, not modulus.

> On Feb 12, 2019, at 4:23 PM, Nigel Tao <nigel...@golang.org> wrote:
> 
> On Mon, Feb 4, 2019 at 5:34 AM Miki Tebeka <miki.teb...@gmail.com> wrote:
>> A bit of profiling shows that the modulo operator takes most of the time:
> 
> C's modulo operator is faster, but can crash if you look at it funny.
> 
> $ cat x.c
> #include <stdio.h>
> 
> int main(int argc, char** argv) {
>  int x = -2147483648;
>  int y = -1;
>  printf("x=%d\n", x);
>  printf("y=%d\n", y);
>  printf("m=%d\n", x % y);
>  return 0;
> }
> $ gcc x.c && ./a.out
> x=-2147483648
> y=-1
> Floating point exception
> 
> Compare it to the output of https://play.golang.org/p/Yj2RZmB7ZRI
> 
> Yes, both the Go code and the C code will panic if y is zero. Still,
> "-2147483648 % -1" has a sensible mathematical definition (zero), and
> C fails to calculate it.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to