Floating point imprecisions.

Hey all,

I'm working with floating points and I'm worried about equalities not triggering due to precision. This time around, I think I'm safe, but I don't know how/where to check. There are no calculations taking place; I need to know if assignment will always be precise with itself:

1
2
3
4
5
6
7
  double checkValue(someErrorConstantDefinedElsewhere);
  for (someMassiveAmountofIterations) {
    if (someConditionFulfilled) {
      checkValue = someCalculation();
    }
  }
  if (checkValue != someErrorConstantDefinedElsewhere) { doSomething(); }


As you can see, the check can only trigger if NOTHING happened to checkValue, thus it still holds its original value, which was assigned directly from the control constant.

Will this always work, or will imprecisions sneak into the assignment and comparison too? According to my understanding of floating point magicmath I would say it will be correct, but I also realize that this would be an incredibly annoying bug to track considering of its location and the results of the check triggering or not.

If it's not a straightforward answer, can someone point me to where I could find the info for my system & compiler?
closed account (zb0S216C)
There's no guarantee that comparing a floating-point value twice for equality will always yield true. It's common practice to compare against an epsilon of some value. [Note: the value of the epsilon should depend on the value of the floating-point --end note] For instance:

1
2
3
4
5
6
7
8
9
10
int main( )
{
  float const EPSILON_SMALL( 0.0003f );
  float const EPSILON_LAGRE( 0.0006f );

  float Value( 1.2f );
  float CompareValue( 1.2f );
  std::cout << ( std::abs( Value - CompareValue ) <= EPSILON_LARGE );
  return( 0 );
}

Wazzak
Last edited on
Hm, good thing I checked then. Thanks for the response!
Topic archived. No new replies allowed.