Thread: Conditional Statements

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool

    nice 1 happy_reaper

    much appreciated

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ((a < 0.00001 || a > -0.000001) && (b < 0.00001 || b > -0.000001))
    always true
    Should be
    Code:
    ((a < 0.00001 && a > -0.000001) && (b < 0.00001 && b > -0.000001))
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    hehe...right
    The funny thing is that I remember correcting someone on that a couple weeks back...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    this...

    Code:
    if ( !a && !b )
    ...seems a lot more intuitive to me.

  5. #20
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    But that would also fall prey to the same floating point problems as the OP was having.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #21
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Quote Originally Posted by Happy_Reaper
    But that would also fall prey to the same floating point problems as the OP was having.
    no it wouldn't

  7. #22
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    no it wouldn't
    Explain, please.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if ( !a && !b )
    Considering this is just
    if ( !(a!=0.0) && !(b!=0.0) )

    you still haven't solved the problem of comparing floats for exact equality or inequality, as described in the first link posted by andor.
    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.

  9. #24
    Logical Aesthetician codepoet's Avatar
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by Salem
    > if ( !a && !b )
    Considering this is just
    if ( !(a!=0.0) && !(b!=0.0) )

    you still haven't solved the problem of comparing floats for exact equality or inequality, as described in the first link posted by andor.
    the original poster wanted to compare variables to absolute zero, not zero within a tolerance (at least that's how i interpreted his code), so i don't understand why you guys are providing solutions that compensate for fuzz... explain, please.

  10. #25
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Namely, he's trying to compare floats to absolute zero. That's not the same as comparing ints with 0.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #26
    Logical Aesthetician codepoet's Avatar
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by Happy_Reaper
    Namely, he's trying to compare floats to absolute zero. That's not the same as comparing ints with 0.
    yes, i know. however, zero of any magnitude is still zero, and unary negation should still fit the bill in this case (!0, !0.0, !0.0000, etc should all still return 1).

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Floating point numbers aren't exact. Assigning "1.23" might result in "1.229999999" being stored in reality. When you're comparing floating point numbers, you have to take this into account.

    Take this loop, for example.
    Code:
    float x;
    
    for(x = 0.0; x != 1.0; x += 0.1) {}
    It might run infinitely; 1.0 can't be represented exactly on some computers at least.

    This is a good read: http://en.wikipedia.org/wiki/Floating_point

    Admittedly, zero is probably different. I imagine that floating point numbers can usually represent zero.
    Last edited by dwks; 11-22-2006 at 05:10 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're missing the point.

    Saying x == 0.0 is just as broken as saying x != 0.0
    If x is even off by some very small fraction then the logic of the test is completely broken.

    If you want repeatability with floating point comparisions, then you have to implement some kind of approximately equal to test.

    Sure it will always work if you just assign a constant, but if you're doing something involving calculations like
    for ( x = 0.0 ; x < 1.0 ; x += 0.1 )
    don't expect x to be reliably 1.0 when the loop finishes.
    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.

  14. #29
    Logical Aesthetician codepoet's Avatar
    Join Date
    Nov 2006
    Posts
    14
    no, i'm not missing the point; i'm afraid both of you are. you can't input 0 into x and have it come out 0.0000001. the inaccuracy of floating point numbers doesn't apply to zero, and the computer doesn't have any problem storing zero, regardless of the magnitude.

    and if x is guaranteed to be zero, then negating it is guaranteed to be 1 and the code snippet i offered will work.

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You missed my edit.
    Quote Originally Posted by dwks
    Admittedly, zero is probably different. I imagine that floating point numbers can usually represent zero.
    Where did you see that?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. problem with floats in conditional statements
    By Desolation in forum C Programming
    Replies: 3
    Last Post: 07-08-2006, 01:56 AM
  3. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 10:52 PM
  4. Using floats in conditional statements
    By DBB in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2001, 07:54 AM