Nat! bio photo

Nat!

Senior Mull.

Twitter RSS

Github

Doing the stupid - with varying results

#include <stdio.h>

main()
{
  long long  i, j;

  for( i = -1; i <= 1; i++)
     for( j = -1; j <= 1; j++)
         printf( "%lld/%lld=%lld %lld%%%lld=%lld\n", 
               i, j, i/j, i, j, i%j); 
  return( 0);
} 
-1/-1=1 -1%-1=0
-1/0=0 -1%0=0
-1/1=-1 -1%1=0
0/-1=0 0%-1=0
0/0=0 0%0=0
0/1=0 0%1=0
1/-1=-1 1%-1=0
1/0=0 1%0=0
1/1=1 1%1=0

replacing long long with int, and %lld with %d yields

-1/-1=1 -1%-1=0
-1/0=0 -1%0=-1
-1/1=-1 -1%1=0
0/-1=0 0%-1=0
0/0=0 0%0=0
0/1=0 0%1=0
1/-1=-1 1%-1=0
1/0=0 1%0=1
1/1=1 1%1=0


Of course division and modulo by zero are undefined... yet...