0

Possible Duplicate:
Floating point comparison

I have some simple C code and the output seems to be unexpected, at least to me:

#include <stdio.h>

int main(void) {
    float f1 = 1.0;
    double f2 = 1.0;

    if (f1 == f2)
        puts("equal");
    else
        puts("unequal");

    return 0;
}

Since float and double have different precision, I expected the output to be unequal but I instead get equal. Why is that the case?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 3
    Here we go again... Please read the following document before attempting any more programming with floating point: http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html – Paul R Oct 12 '11 at 12:20
  • Please link it to duplicate question – Amit Singh Tomar Oct 12 '11 at 12:24
  • @PaulR What i have asked is bit different!! – Amit Singh Tomar Oct 12 '11 at 12:28
  • Not really - it's just the corollary of the problem and this is covered in the answers to the **many** duplicate/related questions already. – Paul R Oct 12 '11 at 12:29
  • I have to disagree with that dupe. It's the question that matters, not the answers. That's because people (and the search engine when you're asking) will search to see if their _question_ has been asked before. I'm not expecting too many people to agree though, I've had this argument before :-) – paxdiablo Oct 12 '11 at 12:42
  • 1
    @paxdiablo: please don't make me trawl through SO looking for all the other dupes... ;-) – Paul R Oct 12 '11 at 13:31

3 Answers3

8

Precision only matters when the number can't be represented exactly. Since both floats and doubles (being IEEE 754 single and double precision values) can represent 1.0 exactly, precision doesn't come into it.

1.0 is basically a zero sign bit, all exponent bits except the highest set to 1, and no mantissa bits set. In single precision, that's binary:

0-01111111-00000000000000000000000

and, for double precision:

0-01111111111-0000000000000000000000000000000000000000000000000000

Not all numbers are exactly representable in IEEE 754 - for example, the 1.1 you mention in a comment is actually stored as 1.100000023841858 in single precision.

Have a look at this answer for an example of decoding a floating point value.

Harald Schmidt's online single-precision converter is an excellent site to play around with if you want to understand the formats. I liked it so much, I made a desktop version in case it ever disappeared (and was capable of doing double precision as well).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

It's not the type, it is the value what it compares, and in this particular case no approximation error occurs, therefore these are equal. Float has a single precision IEEE754 format and double has the double precision format, and in both the format the approximated values are identical, and no error occurs. You can take some fractional value which cannot be stored by float but by double, and test it with your piece of code. Such comparisons should not be done in real code, as overflows and approximation errors would branch the execution unexpectedly (for example float comparison in a loop).

phoxis
  • 60,131
  • 14
  • 81
  • 117
1

Try to do the same with for example .3. Problems appears when the numbers are periodic in the base 2 representation. In any case comparing double and/or float is uncorrect, but I suppose from your question you are already aware.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115