Question: / / This program demonstrates how to safely test a floating - point number / / to see if it is , for all practical

// This program demonstrates how to safely test a floating-point number
// to see if it is, for all practical purposes, equal to some value.
#include
#include
using namespace std;
int main()
{
double result =.666667*6.0;
//2/3 of 6 should be 4 and, if you print result, 4 is displayed.
cout << "result ="<< result << endl;
// However, internally result is NOT precisely equal to 4.
// So test to see if it is "close" to 4.
if (result ==4.0)
cout << "result DOES equal 4!"<< endl;
else
cout << "result DOES NOT equal 4!"<< endl;
return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!