Floating point in the type system

Robert via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 12 09:08:30 PDT 2015


On Saturday, 12 September 2015 at 15:49:23 UTC, Atila Neves wrote:
> On Saturday, 12 September 2015 at 15:13:27 UTC, Robert wrote:
>> Hi all,
>>
>> I came across this example, and wondered what your thoughts on 
>> it are:
>>
>>
>> ```
>> void main(string[] args)
>> {
>>     struct Foo(float f) {
>>         alias VAL = f;
>>         float getF() {
>>             return f;
>>         }
>>     }
>>
>>     Foo!(float.nan) f;
>>     Foo!(float.nan) f2;
>>
>>     // This will fail at compile time
>>     static assert(f.VAL == f2.VAL);
>>
>>     // This will fail at run time
>>     assert(f.getF() == f2.getF());
>>
>>     // But this is ok
>>     f = f2;
>> }
>> ```
>>
>> It seems a little unusual to me.
>>
>> Robert
>
> What do think is unusual?
>
> Atila

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.


More information about the Digitalmars-d mailing list