0

So in my C++ code I have the following line of code for debugging purposes:

if(float1 != float2)
{
    std::cout<<float1<<" "<<float2<<std::endl;
}

What's happening is that the program is entering into the if-statement...but when I print out the two float values they are the same. But if they were the same, then it should bypass this if-statement completely. So I'm really confused as to why this is happening.

user1782677
  • 1,963
  • 5
  • 26
  • 48
  • 2
    Duplicate of so many questions, all of which have this as the main answer: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – chris May 20 '13 at 23:07
  • 2
    They're not the same. – Kerrek SB May 20 '13 at 23:08
  • 1
    @chris: Goldberg's paper is probably overkill. The key point is that floating-point values that happen to be displayed the same way are not necessarily equal. Modify the output statement to show more precision and you'll see that they're different. Floating-point arithmetic is inexact. – Keith Thompson May 20 '13 at 23:11
  • @KeithThompson, Yeah, it's a lot, but it's pretty much THE link to give for information. Of course searching for related questions before asking would give some pretty good on-site explanations. – chris May 20 '13 at 23:15
  • @chris Providing only arcane articles to read as an answer to simple questions is how superstitions are born. – Pascal Cuoq May 21 '13 at 06:15

2 Answers2

6

The floats may just have very similar values. By default, the I/O library will truncate the output of floating point values. You can ensure that you get the full precision by calling the precision member function of std::cout:

if(float1 != float2)
{
    std::cout.precision(9);
    std::cout<<float1<<" "<<float2<<std::endl;
}

Now you should see the difference. The value 9 is the number of base-10 digits representable by a IEEE 754 32 bit float (see @EricPostpischil's comment below).

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • `digits10` is the maximum number of digits such that a decimal numeral with that many digits can be converted to the floating-point type and back again without change. It is not the full precision of the type—there may be two floating-point values that are different but yield the same numeral when converted with digits10 digits. The value you need to guarantee observable differences is what the IEEE 754 standard calls Pmin(binary32), which is 9. I do not see a C or C++ feature that provides this value. – Eric Postpischil May 21 '13 at 13:59
  • @EricPostpischil Thanks. I wasn't completely sure that what I'd written was correct. – Joseph Mansfield May 21 '13 at 14:05
1

Floating-point value are typically stored in computer memory in binary format. Meanwhile, values you print through cout are represented in decimal format. The conversion from binary floating-point representation to decimal representation can be lossy, depending on your conversion settings. This immediatelty means that what you print is not necessarily exactly the same as what is actually stored in memory. This explains why the direct comparison between float1 and float2 might say that they are different, while the decimal printout might look identical.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765