« April 2009 | Main | June 2009 »

May 2009 Archives

May 22, 2009

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 May 2009

This page contains all entries posted to Nat!'s Web Journal in May 2009. They are listed from oldest to newest.

April 2009 is the previous archive.

June 2009 is the next archive.

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

Powered by
Movable Type 4.25