vectoradagnat-gps

How to add vectors in Ada


I have a vector of the form (x,y,x) representing coordinates. I want to be able to do something like (x,y,z) + (x2,y2,z2) to produce a new set of coordinates. Ada says it cant use '+' for composite types, but surely there is a way I can do this?


Solution

  • If you have

    type Vector is record
       X : Float;
       Y : Float;
       Z : Float;
    end record;
    

    you can define + as

    function "+" (L, R : Vector) return Vector is
      (L.X + R.X, L.Y + R.Y, L.Z + R.Z);
    

    Be careful when you define - similarly to use - throughout! that error is very hard to spot.