Thread: Floating point

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006

    Floating point

    I am doing a program of finding an exocenter of a triangle.

    In one case I want to check if the floating point number is zero or not. I am finding out like this

    if( !fabs(hold)  )    
           return true;
    else return false;
    This detects hold = 0.000000 nicely but fails when hold = -0.000000!!! What should i do now to detect both! I am using MinGW compiler with CodeBlocks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You need to realise that all floats are approximations. So what might appear to damn close to zero as to not really matter, it will still fail such absolute tests.

    Try something like
    if ( fabs( hold ) < 0.0000001 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Still not working!!!!

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    As Salem said floats are not 100&#37; accurate due to imprecision caused by rounding. You should create some float functions that make use of an epsilon value for the comparison.

    And never ever ever rely on equality when it comes to comparing floats. This will get you into a lot of trouble. A==B is not always true when it comes to floating point values.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Are you saying that "fabs()" doesn't return the right thing for negative zero? What does it return?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM