symmetric signed types

Timon Gehr timon.gehr at gmx.ch
Mon Jan 27 06:13:36 PST 2014


On 01/27/2014 02:46 PM, Dominikus Dittes Scherkl wrote:
> On Saturday, 25 January 2014 at 13:43:25 UTC, Timon Gehr wrote:
>>On 01/25/2014 01:57 PM, Dominikus Dittes Scherkl wrote:
>>> And then comparison cannot be implemented fully correct with the current
>>> operator overloding system of D.
>> Why not?
>>
>> struct S{
>>     auto opCmp(S r){ return float.nan; }
>> }
>>
>> void main(){
>>     S s;
>>     assert(s!<>=s);
>> }
>
> Yes, but only for floatingpoint types - you cannot overload the !<>=
> operator for integral types

I don't get what this is supposed to mean.

> and it will be deprecated anyway.

So? It was the most convenient way to illustrate that I have defined a 
not fully ordered type using opCmp.

> And you cannot opverload opCmp in a way that the new defined integer NaN
> will not compare in some way to the other integer values.

Of course you can. Just return float.nan from opCmp in the case that at 
least one of the arguments is your 'integer NaN'.

float opCmp(sint r){
     if(isNan()||r.isNan()) return float.nan;
     return value<r.value?-1:value>r.value?1:0;
}

> What would be needed is a minimal signed type (2bit with the values -1,
> 0, 1 and NaN) and use that in opCmp.

That's not needed in order to get correct comparison behaviour.


More information about the Digitalmars-d mailing list