// FP1.CPP
//
// This is the test program that demonstrates
// the problem found when mixing floats and
// doubles.  The add_double() routine works
// properly for the constant 1.12, and for
// the double 1.12.  But when used with a
// float, it fails.
//
// Note that this C++ program was built using
// 1996 vintage C++ compilers. ISO C++ compliant
// compilers will need to modify the header names
// and include the "using namespace std" statement.
//
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

//
// This routine converts a double to the
// ASCII representation needed for storage
// in a database.  After the conversion,
// atof() is used to convert the value back
// to binary representation.  A check is done
// to see if the ASCII and binary represntations
// match up.  If they don't it means that the
// double is going to require more than 2 digits
// of precision to represent properly.
//
int add_double( double d )
{
    char buf[ 81 ];
    sprintf( buf, "%9.2f", d );
    double test = atof( buf );
//
//  Some function here adds buf to the database
//
    if ( test == d )
        return 1; //success
    else
        return 0; //failure
}

main()
{
    cout << "add_double( 1.12 ) returns "
         << ( add_double( 1.12 ) ? "success" : "failure" )
         << "\n";

    cout << "add_double( 1.125 ) returns "
         << ( add_double( 1.125 ) ? "success" : "failure" )
         << "\n";

    double d = 1.12;
    cout << "add_double( d ) returns "
         << ( add_double( d ) ? "success" : "failure" )
         << "\n";

    float f = d;
    cout << "add_double( f ) returns "
         << ( add_double( f ) ? "success" : "failure" )
         << "\n";

    return 1;
}

