 # Thread: Comparison of Double values

1. bump because kayjay and I crossed posts  Reply With Quote

2. Originally posted by phinds
................anyone has any further quesitons on this subject ... fugeddaboudit !!! I'm sick of the whole thing. I first encounted this problem in about 1963 and I've explained the whole thing .............
1963 Don't think even my mother born then!

Thanx for the time and effort. Though I still don not completely understand I'll make an effort.  Reply With Quote

I dont see any round for converting 5.375  Reply With Quote

4. Originally posted by nemaroller
These are all good explanations, but still, this code raises questions.... the binary equivalents are still equal..
[/Highlight]
Actually the only way to test Phinds theory (which I am in agreement with) would be to do the conversion in the other direction. You will probably find that you can come up with two different binary numbers that both convert to 1.4.

Thanks for the explanation phinds .. but please dont be hard on someone for questioning 1.4<>1.4 = True   Reply With Quote

5. I dont see any round for converting 5.375
which is EXACTLY in agreement with my statement.

5.375 is rational in binary, so as I explained, it will not suffer any rounding error.

You keep setting up strawmen to avoid a conclusion that you apparently don't like.

Do the math on 1.4  Reply With Quote

6. If you do the math on 1.4 yes you do end up with that trailing 1100.
So then the value gets rounded. Right.

The fact remains though that you're still comparing two numbers that would have been rounded the same.
1.4 v 1.4  Reply With Quote

7. Well, obviously Phinds must know what he's talking about...

Its basically the fact that numbers you would think would have an absolute finite value in decimal, like .04, does not mean it is so in binary, because of the way that number system is set up... in fact, 0.4 seems to go on forever in binary when trying to convert that decimal fraction to binary... you just wont get an ending...

The same applies for 1.36..... In my limited runs, it appears only decimal fractional numbers ending with 5, can have a finite representation in binary (.375,.5,.15)  Reply With Quote

8. Here is an example of what's going on. I had to write the program in C to demonstrate. Check out the subtle difference when I dumped the memory where the variables were stored. Then when I print the variables again, they appear equal. In memory, however they are not. The comparison operation checks that all bits are identical. Clearly, these variables appear to hold the same value...and for math calculations it would work fine. But for comparison they are definitely not equal.

f1 = 1.360000
f2 = 0.040000
f3 = 1.400000
f1 + f2 = 1.400000
f1 dump = 0x600000003ff5c28f
f2 dump = 0x400000003fa47ae1
f3 dump = 0x600000003ff66666
f4 dump = 0x6a0000003ff66666
f1 = 1.360000
f2 = 0.040000
f3 = 1.400000
f1 + f2 = 1.400000

Here is the code I ran (for all you C buffs...it's a quick cloodge for a demo.)

Code:
```#include <stdio.h>

int main(void)
{
void *temp;
double f1 = 1.36f, f2 = 0.04f;
double f3 = 1.4f;
double f4;

printf("f1 = %f\n", f1);
printf("f2 = %f\n", f2);
printf("f3 = %f\n", f3);
f4 = f1 + f2;
printf("f1 + f2 = %f\n", f4);

temp = &f1;
printf("f1 dump = 0x%x%x\n", *(int*)temp, *(int*)((int)temp + 4));
temp = &f2;
printf("f2 dump = 0x%x%x\n", *(int*)temp, *(int*)((int)temp + 4));
temp = &f3;
printf("f3 dump = 0x%x%x\n", *(int*)temp, *(int*)((int)temp + 4));
temp = &f4;
printf("f4 dump = 0x%x%x\n", *(int*)temp, *(int*)((int)temp + 4));

printf("f1 = %f\n", f1);
printf("f2 = %f\n", f2);
printf("f3 = %f\n", f3);
printf("f1 + f2 = %f\n", f4);

return 0;
}```  Reply With Quote

9. Actually, it appears only decimal fractions that are all odd and end with 5, will have a finite representation in binary....

http://www.cs.nmsu.edu/~pfeiffer/cla...otes/base.html  Reply With Quote

10. Hell, thats not even true.... I don't know, guess there just isn't a solid rule here....  Reply With Quote

11. But just to cool off this thread... the binary system was initially developed by Gottfried Wilhelm von Leibniz in 1623, but it was discovered the Chineese had actually developed it 700 years earlier.... solid lines are 1's, broken lines are 0's...

http://www.albany.net/~cybernet/Iching.jpg  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured