L-21 MCS 260 Mon 8 Oct 2001

In this lecture we continued our exploration of the fundamental data types into the floating-point types. Below are some programs used as supplement.
/* L-21 MCS 260 Mon 8 Oct 2001 : size of fundamental data types 

The command sizeof() is useful for dynamic memory allocation,
as we shall see later.                                            */

#include<stdio.h>

int main(void)
{
   printf("\nSize of fundamental char and int types:\n");
   printf("  number of bytes in char : %d\n", sizeof(char));
   printf("  number of bytes in short int : %d\n", sizeof(short int));
   printf("  number of bytes in long int : %d\n", sizeof(long int));
   printf("  number of bytes in long long int : %d\n", sizeof(long long int));
   printf("\nSize of floating-point data types :\n");
   printf("  number of bytes in float : %d\n", sizeof(float));
   printf("  number of bytes in double : %d\n", sizeof(double));
   printf("  number of bytes in long double : %d\n\n", sizeof(long double));

   return 0;
}
Here is the output on a sun machine :
Size of fundamental char and int types:
  number of bytes in char : 1
  number of bytes in short int : 2
  number of bytes in long int : 4
  number of bytes in long long int : 8

Size of floating-point data types :
  number of bytes in float : 4
  number of bytes in double : 8
  number of bytes in long double : 16

Floating-point types have just as the integer types a range. In addition, there is the precision.
/* L-21 MCS 260 Mon 8 Oct 2001 : check precision of float types 

The program below illustrates the range of precision : what is the
smallest number you can add to one such that the result is different
from one?   Execute the program to see the values for your machine.
Observe the conversion characters in the printf and the f,l,L in the
constants: 1.0f, 1.0l, and 1.0L are all different from each other.  */

#include<float.h>
#include<stdio.h>

int main(void)
{
   float f;
   double x;
   long double ld;

   printf("\nTesting the precision of float, double, and long double : \n");
   f = 1.0f + 1.0e-7;
   printf("  1.0 + 1.0e-7 = %.10f\n", f);
   f = 1.0f + 1.0e-8;
   printf("  1.0 + 1.0e-8 = %.10f\n", f);
   x = 1.0l + 1.0e-15;
   printf("  1.0 + 1.0e-15 = %.20lf\n", x);
   x = 1.0l + 1.0e-16;
   printf("  1.0 + 1.0e-16 = %.20lf\n", x);
   ld = 1.0L + 1.0e-34;
   printf("  1.0 + 1.0e-34 = %.40Lf\n", ld);
   ld = 1.0L + 1.0e-35;
   printf("  1.0 + 1.0e-35 = %.40Lf\n", ld);

   printf("\nThe experiment above is explained by constants from float.h :\n");
   printf("  precision of float : %e\n", FLT_EPSILON);
   printf("  precision of double : %.15le\n", DBL_EPSILON);
   printf("  precision of long double : %.30Le\n", LDBL_EPSILON);

   printf("\nThe largest and smallest numbers we can represent are : \n");
   printf("  largest float : %e\n", FLT_MAX);
   printf("  largest double : %.15le\n", DBL_MAX);
   printf("  largest long double : %.30Le\n", LDBL_MAX);
   printf("  smallest float : %e\n", FLT_MIN);
   printf("  smallest double : %.15le\n", DBL_MIN);
   printf("  smallest long double : %.30Le\n", LDBL_MIN);

   printf("\n");

   return 0;
}
Here is the output on a sun machine :
Testing the precision of float, double, and long double : 
  1.0 + 1.0e-7 = 1.0000001192
  1.0 + 1.0e-8 = 1.0000000000
  1.0 + 1.0e-15 = 1.00000000000000111022
  1.0 + 1.0e-16 = 1.00000000000000000000
  1.0 + 1.0e-34 = 1.0000000000000000000000000000000001925930
  1.0 + 1.0e-35 = 1.0000000000000000000000000000000000000000

The experiment above is explained by constants from float.h :
  precision of float : 1.192093e-07
  precision of double : 2.220446049250313e-16
  precision of long double : 1.925929944387235853055977942585e-34

The largest and smallest numbers we can represent are : 
  largest float : 3.402823e+38
  largest double : 1.797693134862316e+308
  largest long double : 1.189731495357231765085759326628e+4932
  smallest float : 1.175494e-38
  smallest double : 2.225073858507201e-308
  smallest long double : 3.362103143112093506262677817322e-4932