/* ur.c
** Mth 351 Fall 2005 Bent Petersen
**
** This simple program computes the unit round for the three
** c floating point data types. Compile without optimization.
** Use my code or write your own better code. If you can't
** compile a program, try a spreadsheet.
**
** Typical compilation commands:
**
** Microsoft C/C++    cl /W4 ur.c
** GNU GCC C/C++      gcc ur.c -o ur -lm -Wall
*/

#include <stdio.h>
#include <math.h>

int main (void) {
  double ur;
  float temp1 = 2.0;
  double temp2 = 2.0;
  long double temp3 = 2.0;

char msg[] = "Logical length of mantissa";

/*** FLOAT ***/

ur = 1.0;
while ( temp1 > 1.0 ) 
  {ur /= 2.0; temp1 = (float)(1.0 + ur);} 
ur *= 2.0;

printf(
  "\nFloat unit round\t\t= %14.7e\n%s\t= %3d bits",
  ur, msg, (int)(1.05-log(ur)/log(2.0)) );
printf("\nSize of float\t\t\t= %3d bytes", sizeof(temp1));

/*** DOUBLE ***/

ur = 1.0;
while ( temp2 > 1.0 ) 
  {ur /= 2.0; temp2 = 1.0 + ur; } 
ur *= 2.0;

printf( 
  "\n\nDouble unit round\t\t= %14.7e\n%s\t= %3d bits",
  ur, msg, (int)(1.05-log(ur)/log(2.0)) );
printf("\nSize of double\t\t\t= %3d bytes", sizeof(temp2));

/*** LONG DOUBLE ***/

ur = 1.0;
while ( temp3 > 1.0 ) 
  {ur /= 2.0; temp3 = (long double)(1.0 + ur); } 
ur *= 2.0;

printf(
  "\n\nLong double unit round\t\t= %14.7e\n%s\t= %3d bits",
  ur, msg, (int)(1.05-log(ur)/log(2.0)) );
printf("\nSize of long double\t\t= %3d bytes\n", sizeof(temp3));

return 0;
}

// end
