« Mac OS X Server - A critical flaw | Main | C# .NET 3D Frameworks - Part IV - RANT »

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...

About

This page contains a single entry from the blog posted on May 22, 2009 6:40 PM.

The previous post in this blog was Mac OS X Server - A critical flaw.

The next post in this blog is C# .NET 3D Frameworks - Part IV - RANT.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 4.25