SOURCE FILE: 5.13.precision.cpp


#include <iostream.h>  // input/output functions
#include <iomanip.h>   // for formatting directives
#include <math.h>      // math constants and functions
#include <limits.h>    // integer-oriented limits.
#include <float.h>     // float-oriented limits like FLT_EPSILON

int main ()
{
  double x ;

  cout << "This example shows that translation from" << endl ;
  cout << "binary to decimal is not perfect, so numbers" << endl ;
  cout << "are displayed incorrectly at times.  Also," << endl ;
  cout << "translation from decimal to binary is not" << endl ;
  cout << "perfect and binary numbers can be stored only" << endl ;
  cout << "with so many bits of precision, so numbers" << endl ;
  cout << "that \"should\" test out equal do not test" << endl ;
  cout << "out equal." << endl << endl ;

  cout << setprecision(7)
       << setiosflags(ios::showpoint | ios::fixed) ;

  cout << "FLT_EPSILON is: " << FLT_EPSILON << endl << endl ;

  int i, j ;
  for (i=1; i<100; i++)
     for (j=1; j<i; j++)
     {
       x = float(j)/i ;
//       cout << j << "/" << i << " is: " << x << endl ;
       cout << "x is float(" << j << ")/" << i 
            << " = " << x << " and " 
            << i << "*x should be " << j ;
       if (float(j) != i*x) cout << " ==> they do NOT test equal!" ;
       cout  << endl ;
     }

  return 0 ;
}