Possible Duplicates:

Comparing IEEE floats and doubles for equality

Most effective way for float and double comparison

I'm writing an algorithm which requires testing if the results of large floating point calculations are "equal"; obviously doing such a thing just using a == b is silly since floating point numbers are infamous for their little inaccuracies.

Traditionally I have written the function something like this:

```
isEqual(float a, float b)
{
return abs(a - b) < epsilon; //epsilon = a very small number;
}
```

However, as a friend of mine pointed out this only works properly for certain sizes of floating point numbers, very large numbers need a different epsilon value to very large ones. His suggested fix for this is this:

```
isEqual(float a, float b)
{
return (abs((a - b) / (a + b)) < epsilon);
}
```

I have a feeling you could probably do something clever using bit twiddling, along the lines of this simpler example for integers:

```
isEqual(unsigned byte a, unsigned byte b)
{
return ((!(a ^ b)) & 1) == 0; //all bits except the last 1 have to be equal
}
```

So, what's the best way to do this?

Extra points are allocated for fast solutions, since this is used many times in an algorithm running in real-time in a game, so speed is really rather important!

Final note: my final implementation will be in C#, which can do pretty much anything C/C++ can in terms of floating point numbers, so feel free to go wild with bit twiddling and strange pointer magic if you really want to.