Comparison of double,long double and floats

hello. i want to compare the equallity of two doubles a nd b.
A friend told me i should not test
`if(a==b)`
but
`if(a-b<1e-9)` i.e. a-b<10^-9
Is this the right way to do it? also if i wanted to compare floats or long doubles what is the threshold(a-b<threshold).
Thanks
> Is this the right way to do it?

It would be a right way in most cases; there is no one and only right way.

See: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
First of all most decimal numbers translates into infinite binary number. Like 1/3 is 0.33333.... in decimal.
Second: Computer cannot store infinite numbers. for example we have decimal number, which can store only two digitsafter period, then 1/3 will become 0.33.
Now what happens if we take our example decimal computer and try to compare (1/3)*3 == 1;
Mathematically it should be true, but what will be actual results?
1/3 = 0.33; 0.33 * 3 = 0.99; 0.99 == 1 is false.
The same applies to real computers, save for binary format.
@JLBorges Thanks a lot! I needed to read an article!
@MiiNiPaa i dont see your point
Topic archived. No new replies allowed.