Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation problem?

Status
Not open for further replies.

zenox0

Programmer
Mar 28, 2005
14
CA
Hey, im having a problem with a calculation. This is a sample of the code:

Code:
void main()
{
   double a = 1.16 / 1.45 * 1000.0;
   double b = 800.0;

   if ( a == b )
      printf("a equals b");
}

Now.. if you calculate this.. a = 800. and b = 800.
However.... the statement if ( a == b ) returns false.

Can anyone tell me why?
 
comparing doubles is a tricky business.. if even one digit is different, it will not eval to true... you should find a mechanism to set the precision you want.
 
Try to cout a and b as decimals and as hex values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top