Question about comparision

This is a discussion on Question about comparision within the C Programming forums, part of the General Programming Boards category; here d is defined like this way: Code: d = dx*dx + dy*dy + dz*dz; if (d <1.0) { do ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    Question about comparision

    here d is defined like this way:
    Code:
    d = dx*dx + dy*dy + dz*dz;
    if (d <1.0)
    {
        do something
    }
    when d = 1.0 my program should execute the things inside the if condition but it does. I am not figure it why. Can anyone please help me out?

    Thanx in advance

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by curious View Post
    here d is defined like this way:
    Code:
    d = dx*dx + dy*dy + dz*dz;
    if (d <1.0)
    {
        do something
    }
    when d = 1.0 my program should execute the things inside the if condition but it does.
    What?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    my program should execute the things inside the if condition but it does
    I think you missed a negative there... but I'm assuming that you think the code shouldn't execute, and that the code does in fact execute. If d does indeed equal 1.0, the code would not execute. However, because of the way floating point numbers are calculated on a computer, a calculation that in reality yields 1.0, may be represented as 0.99999. That's my guess. Of course since you obviously have a typo in your question - maybe I interpreted it backwards...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If your program should execute the if when d is 1.0, then "d < 1.0" seems pretty silly.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    55
    d, dx, dy, dz are all defined as double. the program calculates the value of d using above given formula and compares with 1.0. if value of d is less than 1.0 then the program execute the things inside the if statement. But when i checked the value of d was 1.000000 and the program was executing the things inside the if condition. Since d =1.00000 so d not less than 1.0 so it shouldn't do so.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    55
    any help?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    39
    A you trying numerical differentiation..if so.

    I would test that with some tolerance level:

    tolerance a very small number

    enter desired tolerance

    ...
    while (tolExceded && something)

    //check if within desired tolerance

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    First of all, don't bump your threads. Much less after only 6 minutes. Second, I'd refer you to this thread, which, no doubt, you are familiar with. Is that not the same problem with floating-point precision?

    a problem with if

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    55
    Sorry, that was from last year. I totally forgot. But my professor didn't suggest me to use that way. Anyway thanx for reminding.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    the condition depends on what u want????
    where d belongs is it ]-infinite,1.0] or ]-infinite,1.0[
    if u want the 1st one so ur cond. must be if(d<=1.0)
    and if u want the 2nd on the cond. must be if(d<1.0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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