Jump to content


Floating-point model?


10 replies to this topic

#1 Apocalypse

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 14 April 2006 - 02:54 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?

// 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
Apocalypse, the End of the World.

#2 Wernaeh

    Senior Member

  • Members
  • PipPipPipPip
  • 368 posts

Posted 14 April 2006 - 02:58 PM

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

#3 .oisyn

    DevMaster Staff

  • Moderators
  • 1822 posts

Posted 14 April 2006 - 03:01 PM

Note that the correct term is "less than", not "lower than" :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#4 Apocalypse

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 14 April 2006 - 03:10 PM

.oisyn said:

Note that the correct term is "less than", not "lower than" ;)

Oooops.... :lol: I don't know what was I thinking?
Apocalypse, the End of the World.

#5 Axel

    Valued Member

  • Members
  • PipPipPip
  • 119 posts

Posted 14 April 2006 - 08:01 PM

Apocalypse said:

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.

#6 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 14 April 2006 - 08: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.

#7 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 15 April 2006 - 04:57 AM

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. :)
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#8 Apocalypse

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 15 April 2006 - 08: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!
Apocalypse, the End of the World.

#9 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 893 posts

Posted 15 April 2006 - 10:09 AM

Than, just make sure the loop will not run past the end of the array. Pretty standard.

#10 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 15 April 2006 - 03:01 PM

> 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();
}

#11 Apocalypse

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 18 April 2006 - 12:14 PM

Thanks guys :yes:
Apocalypse, the End of the World.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users