Floating point - check for divide-by-zero

This is a discussion on Floating point - check for divide-by-zero within the C Programming forums, part of the General Programming Boards category; I need to know how could I check for a floating point number being "equal to" or "close enough" to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24

    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. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    You can directly compare a floating point number with zero. That's one instance it's actually okay.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by citizen View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    if the quantity is not precisely zero, you won't get a divide by zero. Just check if it's zero.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Quote Originally Posted by iMalc View Post
    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. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by pankaj401 View Post
    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!?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by iMalc View Post
    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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a fast way to check if a point is within a line.
    By mike_g in forum Game Programming
    Replies: 5
    Last Post: 08-05-2008, 12:24 PM
  2. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  3. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  4. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM
  5. Floating point numbers
    By bulsquare in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21