# Thread: Floating point - check for divide-by-zero

1. ## Floating point - check for divide-by-zero

I need to know how could I check for a floating point number being "equal to" or "close enough" to 0.0f.
I have this:
Code:
```buttonWidth = buttonRect.right - buttonRect.left;
buttonHeight = buttonRect.top - buttonRect.bottom;

// If button height or width is zero
...```
Would using
Code:
` buttonWidth > FLT_EPSILON`
be okay?
Thanks

2. You can directly compare a floating point number with zero. That's one instance it's actually okay.

3. zero should be fine to compare with 0.0, as it's not going to give you a divide by zero exception for any number not matching exactly zero. [Although if you divide a large number by a very small number, e.g 1E300 / 1E-100, the result will be an overflow].

However, comparing to epsilon should be fine too.

--
Mats

4. Originally Posted by citizen
You can directly compare a floating point number with zero. That's one instance it's actually okay.
I concur;
Especially if top, left bottom, and right are integer values.

5. if the quantity is not precisely zero, you won't get a divide by zero. Just check if it's zero.

6. Originally Posted by iMalc
I concur;
Especially if top, left bottom, and right are integer values.
All the variables involved are float.

I, finally, used the epsilon check.

7. Originally Posted by pankaj401
All the variables involved are float.

I, finally, used the epsilon check.
Okay, so you ask for advice, and 4 people say to do one thing and you do the opposite. So why did you even bother to ask!?

8. Originally Posted by iMalc
Okay, so you ask for advice, and 4 people say to do one thing and you do the opposite. So why did you even bother to ask!?
It's the right thing to do. Comparing to 0.0 *might* work, but it's not guaranteed. While not strictly necessary, a comparison to epsilon is always safe, so that should be the done thing. My advice on floats: don't use them!

QuantumPete

Popular pages Recent additions