Overloading float operators

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Dec 11 23:00:25 UTC 2017


On Monday, December 11, 2017 19:28:47 rumbu via Digitalmars-d-learn wrote:
> Is there any way to overload specific floating point operators?
> https://dlang.org/spec/expression.html#floating-point-comparisons

If those haven't been deprecated yet, they almost certainly will be. It was
decided that they were a mistake, and AFAIK, pretty much no one uses them.
opEquals and opCmp are all that there is for overloading comparison
operators.

> I'm using a decimal data type (a struct) and one of the possible
> values is NaN, that's why I need these operators.
>
> I know also that this also was discussed, but is there any way to
> separately implement == and !=, so both return true or false in
> the same time?
>
> The reason is the same: NaN == NaN = false and NaN != NaN = false
> in the same time.

You cannot overload == and != separately, but why would you need to? There's
no reason for a != b to not be the same as !(a == b) even with floating 
point
comparisons. e.g.

import std.stdio;

void main()
{
    float f;
    writeln(f == f);
    writeln(f != f);
}

prints

false
true

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list