PDA

View Full Version : Floating-point model?


Apocalypse
04-14-2006, 07:54 AM
Okay, I use Visual C++ 8.0 (2005) and I want to know: do I need the following functions when comparing floating-point values, or the precise floating-point model takes care of this?

// this function tests if two floating-point values are equal
#define EPSILON 0.00001f
inline bool IsSame(float f1, float f2)
{
return fabs(f1-f2) < EPSILON;
}
// this function tests if a floating-point value is lower than another
inline bool IsLowerThan(float f1, float f2)
{
return (f2-f1) > EPSILON;
}
// this function tests if a floating-point value is lower than or equal to another
inline bool IsLowerThanOrEqual(float f1, float f2)
{
return (f1-f2) <= EPSILON;
}
// this function tests if a floating-point value is greater than another
inline bool IsGreaterThan(float f1, float f2)
{
return (f1-f2) > EPSILON;
}
// this function tests if a floating-point value is greater than or equal to another
inline bool IsGreaterThanOrEqual(float f1, float f2)
{
return (f2-f1) <= EPSILON;
}
#undef EPSILON

what about the fast floating-point model? Do I need the above functions if I use the fast floating-point model?

P.S.: I really don't know much about the floating-point model...:huh: sorry if it doesn't have to do with my functions. :wacko:

Thanks

Wernaeh
04-14-2006, 07:58 AM
I'm quite sure the only difference between the floating point models is the intermediate result precision used by the cpu.

Anyways, your functions certainly are not obsolete.

Cheers,
- Wernaeh

.oisyn
04-14-2006, 08:01 AM
Note that the correct term is "less than", not "lower than" ;)

Apocalypse
04-14-2006, 08:10 AM
Note that the correct term is "less than", not "lower than" ;)

Oooops.... :lol: I don't know what was I thinking?

Axel
04-14-2006, 01:01 PM
Okay, I use Visual C++ 8.0 (2005) and I want to know: do I need the following functions when comparing floating-point values, or the precise floating-point model takes care of this?
Short Answer: Yes.

Nils Pipenbrinck
04-14-2006, 01:43 PM
well - it depends.

Wrong answers for floating point compares are sometimes absolutely what you want as long as the result is constant and that some rules like less than != greater than are valid.

A general floating point compare with epsilon solution is crying for problems. It's basically the same thing as comparing without epsilons, just the other way around.

monjardin
04-14-2006, 09:57 PM
So, are you saying that floating point comparison tolerances should be handled on a case by case basis? Just making sure I'm following you. If so, then I agree. :)

Apocalypse
04-15-2006, 01:40 AM
Thanks for the replies, guys! :happy:

It is just that my program goes through an array and stops when a value in the array is greater than a float variable (it's not constant!). So if the last value in the array is 1.599999 and the float variable is currently 1.6, then the loop will go through the next item in the array, which doesn't exist, and the program will crash!

geon
04-15-2006, 03:09 AM
Than, just make sure the loop will not run past the end of the array. Pretty standard.

Nils Pipenbrinck
04-15-2006, 08:01 AM
> So, are you saying that floating point comparison
> tolerances should be handled on a case by case basis?

Yes, basically.

Ignore the fact, that a single eplsilon won't work for all numbers (it' depends on the range you work on). It does imho not make sense to do an epsilon compare on greater/lesser comparisons.

It does however makes sense for IsZero for math-stuff, where you don't want to divide by zero or anything close to it, and for IsEqual, where you just want to know if two floats are really close together or not. Most commonly to avoid mathematical troubles later on where the values (after calculation) result in unlogical logic like this: (x*a > x*b) != (a > b)

Lesser and Greater cry for troubles because if you program them with epsilons simple rules like:

(a > b) != (a < b)

simply not apply anymore, and the entire boolean logic is broken.

Instead I'd rather do:

if (IsClose(a,b))
{
do something
} else {
if (a>b) dosomethingelse();
else dosomedifferent();
}

Apocalypse
04-18-2006, 05:14 AM
Thanks guys :yes: