Article ID: 145889
Article Last Modified on 6/10/2005
36068 INFO: IEEE Floating-Point Representation and MS Languages
125056 INFO: Precision and Accuracy in Floating-Point Calculations
/* Compile options needed: none
Value of c is printed with a decimal point precision of 10 and
6 (printf rounded value by default) to show the difference
*/
#include <stdio.h>
#define EPSILON 0.0001 // Define your own tolerance.
#define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON)))
void main()
{
float a,b,c
a=1.345f;
b=1.123f;
c=a+b;
//if (FLOAT_EQ(c, 2.468)) // Remove comment for correct result.
if (c == 2.468) //Comment this line for correct result.
printf("They are equal\n");
else
printf("They are not equal!!The value of c is %13.10f,or %f",c,c);
}
Microsoft Developer Network: C Floating-Point Constants Microsoft
Additional query words: 1.00 1.50 2.00 4.00 8.00 8.00c 9.00 9.10 10.00 floating point
Keywords: kbhowto kbinfo kbcode kbcompiler kbarttypeinf KB145889