# Question about comparision

• 06-11-2009
curious
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?

• 06-11-2009
ಠ_ಠ
Quote:

Originally Posted by curious
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?
• 06-11-2009
sean
Quote:

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...
• 06-11-2009
tabstop
If your program should execute the if when d is 1.0, then "d < 1.0" seems pretty silly.
• 06-11-2009
curious
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.
• 06-11-2009
curious
any help?
• 06-11-2009
ninety3gd
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
• 06-11-2009
sean
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?

http://cboard.cprogramming.com/c-pro...roblem-if.html
• 06-11-2009
curious
Sorry, that was from last year. I totally forgot. But my professor didn't suggest me to use that way. Anyway thanx for reminding.
• 06-12-2009
mostafa faisal
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)