Very strange problem with comparing floating point numbers

monarch_dodra monarchdodra at gmail.com
Sun Sep 30 10:07:41 PDT 2012


On Sunday, 30 September 2012 at 01:29:24 UTC, Ivan Agafonov wrote:
> // Tell me about this sutation, may be it is a bug?
>
> [SNIP]
> 	// all of this fails!!!
> 	assert (a.length == a.length); // This is really shocking
> 	assert (a.length == a3.length);
> [SNIP]

This is just a fact of life regarding how floating point types 
work. Here is a well documented explanation. It pertains to C++, 
but applies.

http://www.parashift.com/c++-faq/floating-point-arith2.html

As a rule of thumb, NEVER use opEqual with floating point types 
aniways. You need to use some sort of comparison with leway for 
error, such as std.math.approxEqual.



More information about the Digitalmars-d-learn mailing list