compare double value to 0 - unreliable comparisons warning

This is a discussion on compare double value to 0 - unreliable comparisons warning within the C Programming forums, part of the General Programming Boards category; Hey - Quick one for ya... I get the warning message below at compile time because i'm trying to compare ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    17

    compare double value to 0 - unreliable comparisons warning

    Hey - Quick one for ya...

    I get the warning message below at compile time because i'm trying to compare a double value to 0.

    What should I use to compare it? I'm working on a matrix multiplier so thought a cast would be a slow, if not accuracy losing method of doing it.

    warning #1572: floating-point equality and inequality comparisons are unreliable
    Code:
    double alpha;
    ...
     if ((m == 0) || (n == 0) || (((alpha == 0) || (k == 0)) && (beta == 1)))
    Elementary stuff no doubt but in my defence it's getting late....

    Thanks,

    Colly.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When you use floats and doubles, because of the way they work, the variable does not store the exact value. So 3 / 2 may not be exactly 1.5, it might be 1.50000001 or something. This is why inequality and equality comparisons are unreliable with them. I'm afraid an alternative doesn't come to mind. It's been a loong time since I worked with matrices.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To further the point, when using floating point numbers, don't compare to an integer. Compare to a float. Not that it's really going to help this, but for correctness sake, you want this instead:
    Code:
    if( myfloat == 0.0 )
    The only way to really have accuracy is to skip floating point numbers all together.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The biggest project I've worked on: ASCIIpOrtal
    By guesst in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 07-21-2009, 05:42 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  4. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  5. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 03:54 PM

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