// Tell me about this sutation, may be it is a bug?

import std.math;
import std.stdio;

struct Vector(int size)
{
        union
        {
                float[size] array = 0;
                struct
                {
                        static if (size == 2) float x, y;
                        static if (size == 3) float x, y, z;
                        static if (size == 4) float x, y, z, w;
                }
        }
        
        @property float lengthsqr()
        {
                static if (size == 2) return x*x + y*y;
                static if (size >= 3) return x*x + y*y + z*z;
        }

        @property float length() { return sqrt(lengthsqr()); }
@property float length2() { float tmp = sqrt(lengthsqr()); return tmp; }
}

void main()
{
        auto a = Vector!4([1, 2, 3, 1]);
        auto a3 = Vector!3([1, 2, 3]);
        assert (a.lengthsqr == 14);
        
        auto alen = a.length; auto a3len = a3.length;
        
        // all of this prints the same number: 0x1.deea2p+1
        writefln("%a, %a", alen, a3len);
        writefln("%a, %a", a.length, a3.length);
        writefln("%a, %a", a.length2, a3.length2);
        
        // passes
        assert (alen == a3len);
        assert (a.length2 == a3.length2);
        assert (cast(real)a.length == cast(real)a3.length);
        
        // all of this fails!!!
        assert (a.length == a.length); // This is really shocking
        assert (a.length == a3.length);
}

Reply via email to