Floating point in the type system

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 12 09:39:13 PDT 2015


On Saturday 12 September 2015 18:08, Robert wrote:

> It's unusual, because `float.nan != float.nan`, so one might 
> expect that `typeof(Foo!(float.nan) != Foo!(float.nan))`, whereas 
> this is clearly not the case, even with both the static assert 
> and runtime assert failing. I'm just curious to understand the 
> reasoning behind this, whether it's intentional, and whether it 
> matters at all.

I don't know what the compiler actually does, but it looks like the 
comparison of template value arguments doesn't use equality, but something 
more akin to `is` instead (bitwise equality).

If that's right, then `is(Foo!(float.nan) == Foo!(float.nan))` holds because 
`float.nan is float.nan` holds.

Same behavior with a struct instead of float:
----
struct S
{
    bool opEquals(S other) {return false;}
}
struct Foo(S s)
{
}
static assert(S.init != S.init); /* not equal */
static assert(S.init is S.init); /* but bit for bit identical */
static assert(is(Foo!(S.init) == Foo!(S.init)));
----



More information about the Digitalmars-d mailing list