0

I want to test if float value is equal to 0.00 or 0.0 and I wanted to know how to correctly test my variables so please correct me if I'm wrong :

int main()
{
    float x1 = -105.00;
    float x2 = 7.00;
    float x3 = 2.00;
    if((x1 == 0.0f || x1 == 0.0))
    {
        if((x2 == 0.0f || x2 == 0.0))
        {
            if((x3 == 0.0f || x3 == 0.0))
            {
                printf("full null\r\n");
            }
        }
    }
}

So I wanted to know if it is the best way to test float in c.

Clément
  • 37
  • 5
  • 1
    What indication(s) do you have that this is wrong? If you don't have any, why are you here? – Scott Hunter Oct 12 '21 at 14:05
  • 3
    The check of `0.0` (that is without `f`) is completely redundant. Your variables are `float` and checking with a `float` value is sufficient. `if (x1==0.0f && x3==0.0f && x3==0.0f) {..}` should do the whole check. Note, that this will work for zero, but not for "general" floating point value, which might not be exactly representable. – Eugene Sh. Oct 12 '21 at 14:05
  • 1
    Note that "Care must be taken when comparing floating-point values for equality, because the results of many operations cannot be represented exactly and must be rounded. In practice, floating-point numbers are usually compared allowing for the difference of one or more units of the last place." - https://en.cppreference.com/w/c/language/operator_comparison – Fred Larson Oct 12 '21 at 14:08
  • If the values are results of some calculation you might want to check if the absolute value is below some very small number instead of checking if it is equal to 0.0f. – Bodo Oct 12 '21 at 14:10
  • 3
    Do you wish to compare if it's _exactly_ zero or very close to zero? In case of the former, the code is correct. In case of the latter, check duplicate https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison – Lundin Oct 12 '21 at 14:15
  • 1
    worth a read to understand why exact floating point comparisons can be problematic: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – yano Oct 12 '21 at 14:22

3 Answers3

3

To test whether a float x is zero, use x == 0.f. This will evaluate to 1 if and only if the value of x is zero. (Due to C’s various automatic conversions, x == 0. and x == 0 will have the same effect.)

If x is a floating-point number derived from previous calculations that may have rounding errors, then x may be non-zero even though the “ideal” result1 would be zero or vice-versa. In this case, whether it is possible to use x to make a decision about the real result depends on circumstances. For example, if x is farther away from zero than the rounding errors could possibly account for, then the ideal result cannot be zero either. However, if x is close to zero, then it may be impossible to determine from x whether the ideal result is zero or not.

Footnote

1 By “ideal” result, I mean the result of performing the same calculations using real-number arithmetic with no rounding errors, instead of floating-point arithmetic.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

For this specific example, i.e. comparing if all floats are zero, all you need is

int main()
{
    float x1 = -105.00;
    float x2 = 7.00;
    float x3 = 2.00;
    if (x1 == 0 && x2 == 0 && x3 == 0)
    {
        printf("full null\r\n");
    }
}

No magic, no strange things happening, just plain compares.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

I think it's not a very good idea to compare the floating point numbers. You can check if the value is really very close to the zero: https://floating-point-gui.de/errors/comparison/

ctasdemir
  • 383
  • 4
  • 13