float equality

so so at so.so
Mon Feb 21 10:00:19 PST 2011


On Sat, 19 Feb 2011 14:06:38 +0200, spir <denis.spir at gmail.com> wrote:

> Hello,
>
> What do you think of this?
>
> unittest {
>      assert(-1.1 + 2.2 == 1.1);          // pass
>      assert(-1.1 + 2.2 + 3.3 == 4.4);    // pass
>      assert(-1.1 + 3.3 + 2.2 == 4.4);    // fail
>      assert(-1.1 + 3.3 == 2.2);          // fail
> }
>
> There is approxEquals in stdlib, right; but shouldn't builtin "==" be  
> consistent anyway?
>
> Denis

Strange no one mentioned this.
Problem is not the floating point format in your example. I can do the  
same with integral numbers, how?

int(5) / int(2) => int(2) or int(3)? And why?
Answer depends on the rounding mode, if you don't know the given rounding  
mode for a (machine interpreted) number system you can't say anything.
You know the answer because you know the rule. I can say same for the  
floating points since i know how they work.


More information about the Digitalmars-d mailing list