comparisons of floating point variables

T

Tagore

consider following program:
#include<stdio.h>
int main(void)
{
float f=0.0f;
int i;
for(i=0;i<10;i++)
f=f+0.1f;
if(f==1.0f)
printf("f is equal to 1.0");
else
printf("f is not equal to 1.0");
return 0;
}

when I run above program it prints: f is not equal to 1.0
I guessed it should print f is equal to 1.0 as after coming out of for
loop its value is 1.0 only.
Please help!
 
J

John Bode

consider following program:
#include<stdio.h>
int main(void)
{
   float f=0.0f;
   int i;
   for(i=0;i<10;i++)
         f=f+0.1f;
   if(f==1.0f)
         printf("f is equal to 1.0");
   else
         printf("f is not equal to 1.0");
    return 0;

}

when I run above program it prints: f is not equal to 1.0
I guessed it should print f is equal to 1.0 as after coming out of for
loop its value is 1.0 only.
Please help!

Read this first: http://www.c-faq.com/fp/fpequal.html

The problem is that not all floating point values can be exactly
represented in a finite number of bits; 0.1 is one such value. What
actually gets stored is a number slightly under or slightly over that
value (something like 0.09999996423721 or 0.10000002384186, for
example), so a little roundoff error creeps into every calculation.
After 10 additions, f is not quite equal to 1.0.

Read the link given above; it will tell you how to compare floating
point values.
 
A

Andrew Oakley

Tagore wrote:
[f is the result of a computation whose value is 1]
I inserted this line of code

printf("\n\n(f - 1.0f) is %e\n", f - 1.0f);

prior to the return statement, and got this output:

(f - 1.0f) is 1.192093e-007
Loss of precision is always a problem with floating point numbers - you
simply cannot store 0.1 in an IEEE-754 float, instead you get approx.
9.9999994e-2 which is the closest representable number.

You might want to take a look at
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
for some ideas on how to compare floats.
 
U

user923005

consider following program:
#include<stdio.h>
int main(void)
{
   float f=0.0f;
   int i;
   for(i=0;i<10;i++)
         f=f+0.1f;
   if(f==1.0f)
         printf("f is equal to 1.0");
   else
         printf("f is not equal to 1.0");
    return 0;

}

when I run above program it prints: f is not equal to 1.0
I guessed it should print f is equal to 1.0 as after coming out of for
loop its value is 1.0 only.

From the C-FAQ:

14.1: When I set a float variable to, say, 3.1, why is printf printing
it as 3.0999999?

A: Most computers use base 2 for floating-point numbers as well as
for integers. Although 0.1 is a nice, polite-looking fraction
in base 10, its base-2 representation is an infinitely-repeating
fraction (0.0001100110011...), so exact decimal fractions such
as 3.1 cannot be represented exactly in binary. Depending on
how carefully your compiler's binary/decimal conversion routines
(such as those used by printf) have been written, you may see
discrepancies when numbers not exactly representable in base 2
are assigned or read in and then printed (i.e. converted from
base 10 to base 2 and back again). See also question 14.6.

14.5: What's a good way to check for "close enough" floating-point
equality?

A: Since the absolute accuracy of floating point values varies, by
definition, with their magnitude, the best way of comparing two
floating point values is to use an accuracy threshold which is
relative to the magnitude of the numbers being compared. Rather
than

double a, b;
...
if(a == b) /* WRONG */

use something like

#include <math.h>

if(fabs(a - b) <= epsilon * fabs(a))

where epsilon is a value chosen to set the degree of "closeness"
(and where you know that a will not be zero).

References: Knuth Sec. 4.2.2 pp. 217-8.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top