Common Error : Equality Test of Two Floating-Point Values

 Common Error : Equality Test of Two Floating-Point Values


#include<iostream>

using namespace std;


int main()

{

double x = 0.1+0.1+0.1+0.1;

if(x == 0.5)

{

cout<<"x is equal to 0.5";

}

else

{

cout<<"x is not equal to 0.5";

}

return 0;

}


output:-

x is not equal to 0.5

Comments

Popular posts from this blog

Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas: area = radius * radius * π volume = area * length

Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total.

Write a program that prompts the user to enter the min- utes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days