```
#include <limits>
using namespace std;
bool is_equal(double a, double b) {
return fabs(a - b) < 2 * numeric_limits<double>::epsilon():
}
```

Solved

Posted on 2009-02-23

Hi,

I'm trying to check if floating point numbers are equal to one another:

float f1 = 0.404;

float f2 = 0.405;

float f3 = 0.404;

if (f1 == f2) {

// no

}

if (f1 == f3) {

// yes

}

I know this comparison does not work at some point, when the number of digits past the decimal place extends "pretty far" - I'm wondering at what point on a 32 bit machine can I trust the == operator with floats? Three digits, four?

Thanks

I'm trying to check if floating point numbers are equal to one another:

float f1 = 0.404;

float f2 = 0.405;

float f3 = 0.404;

if (f1 == f2) {

// no

}

if (f1 == f3) {

// yes

}

I know this comparison does not work at some point, when the number of digits past the decimal place extends "pretty far" - I'm wondering at what point on a 32 bit machine can I trust the == operator with floats? Three digits, four?

Thanks

11 Comments

```
#include <limits>
using namespace std;
bool is_equal(double a, double b) {
return fabs(a - b) < 2 * numeric_limits<double>::epsilon():
}
```

from http://www.experts-exchang

This is related to Java but I believe it would apply to your case.

Found this if you're working with c# http://cplus.about.com/od/learnc/ss/variables_5.htm

Unless you're doing scientific programming with very large or small numbers, you'll only use doubles for greater precision. Floats are good for 6 digits of accuracy but doubles offer 15. http://cplus.about.com/od/

An IEEE 754 floating point number on a 32-bit system (the most common format and the one used by Intel and AMD) is 48 bits of mantissa and 16 bits of sign/magnitude. One would hope that the error in a floating point computation is limited to the lowest order bit, but that's not necessarily true. It does take a fairly unusual set of circumstances for more than the lowest couple of bits to be affected, so most applications can probably trust 45 bits of accuracy. Note that the 45 bits represent the entire number (not just the decimal fraction) so 1,234,567,890.1 requires the same number of significant bits as .12345678901. And 45 bits should be good for about 15 digits.

If you want to read up on the theory, Sun has a very good article here:

http://docs.sun.com/source

Though it is probably longer, more detailed, and more boring than anyone is likely to read. :S

Kent

0.0 to 0.999

for equality. I was lazy and have just been doing == for the time being, all the while knowing that that is not really correct. It's just a game application and it seems to be working ok but I'd rather just patch this part up now. So what should a util function look like for this? (it will be plain C):

bool is_equal(float a, float b)

{

return fabs(a - b) < 2 * 0.00001:

}

Thanks

```
bool is_equal(float a, float b)
{
return fabs(a - b) < 0.001:
}
```

You should keep in mind that floating point values are not precise at all, and should not be used when precision matters. Fixed point values are often a better alternative in that case.

Apologies,

I completely misstated that. :(

A 32 bit floating point value has 23 bits of mantissa, about 7 decimal digits. A 32 bit value has 52 bits of mantissa. The 48 bits I mentioned earlier were a completely different architecture.

Still, only the bottom 2 or 3 bits are generally "at risk".

Kent

If all that matters is 3 digits of precision, fixed point is a definite possibility. Depending on the range you need to cover, an unsigned long might be sufficient for your needs :

```
unsigned long value;
/* and when you want to display, you split it up : */
unsigned long after_the_comma = value % 1000; /* 3 digits precision */
unsigned long before_the_comma = value / 1000;
```

Title | # Comments | Views | Activity |
---|---|---|---|

Frozen keyboard and mouse | 15 | 56 | |

Purchase of laptop with win 7 Professional OS | 8 | 64 | |

wifi turns off automatically in window 10 | 40 | 90 | |

C++ error cannot convert from std::string to const char * | 6 | 35 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**14** Experts available now in Live!