Find out what type my class is being converted to for comparisons

rikki cattermole rikki at cattermole.co.nz
Tue Oct 18 18:59:37 UTC 2022


Well its not a type system issue.

Making u = n, that'll returns true.

So the problem almost certainly lies with IEEE-754.
They are horrible to compare (float/double).

Unfortunately you are stuck calling functions like isClose to compare.

https://dlang.org/phobos/std_math_operations.html#.isClose

Full code extracted from above:

```d
import std;

void main()
{
     auto m = new Matrix!(2)();
     m.data = [1.0, 0.0, 0.0, 1.0].dup;
     auto n = new Matrix!(2)();
     n.data = [2.0, 3.0, 4.0, 5.0].dup;

     auto u = mult(m, n);
     writeln("u.data vs n.data:");
     for (int i = 0; i < u.data.length; ++i)
         writeln(u.data[i], "\t", n.data[i]);

     // using opEquals() directly is working, but it doesn't seem to be 
being used
     //assert(opEquals(u,n),"\"opEquals(u, n)\" is failing."); // this 
works fine
     assert(u == n, "\"u == n\" is failing."); // this fails. Why?
}

class Matrix(size_t X, size_t Y = X)
{
     const size_t x_length = X;
     const size_t y_length = Y;

     double[X * Y] data;

     /// both matrices have to have an identical shape to compare
     /// each element must be identical to match
     bool opEquals(size_t X, size_t Y)(const Matrix!(X, Y) m1, const 
Matrix!(X, Y) m2)
     {
         for (int i = 0; i < m1.data.length; ++i)
             if (m1.data[i] != m2.data[i])
                 return false;
         return true;
     }
}
```


More information about the Digitalmars-d-learn mailing list