How to add vectors in Ada

378 views Asked by At

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?

2

There are 2 answers

0
Simon Wright On BEST ANSWER

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.

0
Jim Rogers On

If you define your vectors to contain elements of a floating point type you can use the generic package Ada.Numerics.Generic_Real_Arrays. This package is described in the Ada Language Reference Manual section G.3.1.

If you want to define your vectors to contain elements of a complex number type then you can use the generic package described in section G.3.2 Complex Vectors and Matrices

If you wish to use integer types as your vector components you can write the "+" function for your integer vector type.