comp.lang.ada
 help / color / mirror / Atom feed
From: "Jerry" <lanceboyle@qwest.net>
Subject: Why does this work? (overloads)
Date: 6 Feb 2007 20:39:23 -0800
Date: 2007-02-06T20:39:23-08:00	[thread overview]
Message-ID: <1170823163.681564.186260@s48g2000cws.googlegroups.com> (raw)

(1) Why does the following work?
(2) Have I done something stupid or dangerous?

I have written some overloads to +, -, * / to make doing some vector-
matrix
calculations look more algebraic. I have also made some overloads to
allow
some element-by-element operations so that I can do Matlab-like
things,
such as "multiplying" two vectors of the same length and getting a
vector
back.

So here are a few lines of code with a couple declarations from the
G.3 Annex of Ada 2005. Note that the line that I originally wrote to
test
some of these overloads was this, involving some complex entities
  H := Two_Pi / (Two_Pi + j * Two_Pi * f * x);
(j = sqrt(-1.0)) but I think the simplified line for H below captures
the
essence of my question.


Here are a few lines of code.


-- From a-ngrear.ads (Ada 2005, Generic Real Arrays
-- instanciated with Long_Float)...
type Real_Vector is array (Integer range <>) of Real'Base; -- Vectors
function "*" (Left, Right : Real_Vector) return Real'Base; -- Inner
product


-- From some of my own overloads...
function "*" (x, y : Real_Vector) return Real_Vector; -- element-by-
element
function "+" (a : Long_Float; x : Real_Vector) return Real_Vector;


-- Some declarations...
f, x, H : Real_Vector(0 .. NN - 1);
a : Long_Float;


-- And finally, two lines of calculation...
H := 1.0 + f * x; -- Adds scalar, 1.0, to a vector using my overload
above.
a := 1.0 + f * x; -- Adds scalar, 1.0, to another scalar, an inner
product.


I get the results that I expected but I feel a little lucky.

Why when calculating H doesn't the compiler interpret f * x as an
inner
product then generate an error, after adding 1.0, when trying to make
the
assignment to H which is a vector?

Obviously the compiler figures out which of the * operators to use
depending on the assigned-to variable. Is this reliable for more
complicated
calculations? I wonder if it is possible to create an ambiguity that
would
be resolved "in the wrong way" and I would get incorrect results.

Regards,
Jerry

 ___
/o|o\
 (o)




             reply	other threads:[~2007-02-07  4:39 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-07  4:39 Jerry [this message]
2007-02-07  6:45 ` AW: Why does this work? (overloads) Grein, Christoph (Fa. ESG)
2007-02-07  7:07   ` Jerry
2007-02-07  7:26     ` AW: " Grein, Christoph (Fa. ESG)
2007-02-07 20:43       ` Jerry
2007-02-07 19:09   ` AW: " Jeffrey R. Carter
2007-02-07 21:13     ` Jerry
2007-02-07 21:33       ` Ludovic Brenta
2007-02-07 21:36       ` Randy Brukardt
2007-02-07 19:02 ` Jeffrey R. Carter
2007-02-07 19:21   ` Adam Beneschan
2007-02-07 19:25     ` Adam Beneschan
2007-02-08  0:51       ` Jeffrey R. Carter
2007-02-07 23:12     ` Robert A Duff
2007-02-08  0:49     ` Jeffrey R. Carter
2007-02-07 20:53   ` Jerry
2007-02-08  0:53     ` Jeffrey R. Carter
2007-02-08 10:40       ` Jerry
2007-02-08 11:34         ` Georg Bauhaus
2007-02-08 12:05         ` AW: " Grein, Christoph (Fa. ESG)
2007-02-08 18:52         ` Jeffrey R. Carter
2007-02-09  0:56         ` Jerry
2007-02-09  1:27         ` tmoran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox