* Re: Function pointer in Ada?
2002-05-14 17:58 ` Kai Schuelke
@ 2002-05-14 18:01 ` Preben Randhol
2002-05-14 18:24 ` David C. Hoos
2002-05-14 18:25 ` Marin David Condic
2 siblings, 0 replies; 14+ messages in thread
From: Preben Randhol @ 2002-05-14 18:01 UTC (permalink / raw)
On Tue, 14 May 2002 19:58:23 +0200, Kai Schuelke wrote:
> Hello,
>
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:
First why do you want to do this?
Preben
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-14 17:58 ` Kai Schuelke
2002-05-14 18:01 ` Preben Randhol
@ 2002-05-14 18:24 ` David C. Hoos
2002-05-14 20:00 ` Kai Schuelke
2002-05-14 18:25 ` Marin David Condic
2 siblings, 1 reply; 14+ messages in thread
From: David C. Hoos @ 2002-05-14 18:24 UTC (permalink / raw)
----- Original Message -----
From: "Kai Schuelke" <kai.schuelke@gmx.net>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Tuesday, May 14, 2002 12:58 PM
Subject: Re: Function pointer in Ada?
> Hello,
>
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but
I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:
>
> plus_pointer : pointer_type := access Standard."+";
>
Why you would want to do this, I can't imageine, but here's how it can be
done:
First, since you are not allowed to take the 'Access attribute of an
intrinsic
function, I "wrapped" the Standard (intrinsic) function in a user-defined
function, then took the 'Access attribute of that.
The code to demonstrate it is here:
with Ada.Text_IO;
procedure Standard_Function_Access
is
type Dyadic_Float_Access is access
function (Left, Right : Float) return Float;
function "+" (Left, Right : Float) return Float
is
begin
return Standard."+" (Left, Right);
end "+";
Float_Plus_Access : constant Dyadic_Float_Access := "+"'Access;
begin
Ada.Text_IO.Put_Line
("3.1 + 7.2 =" & Float'Image
(Float_Plus_Access (3.1, 7.2)));
end Standard_Function_Access;
I also took the liberty of using "Adaesque" names for the declarations.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-14 18:24 ` David C. Hoos
@ 2002-05-14 20:00 ` Kai Schuelke
0 siblings, 0 replies; 14+ messages in thread
From: Kai Schuelke @ 2002-05-14 20:00 UTC (permalink / raw)
"David C. Hoos" <david.c.hoos.sr@ada95.com> schrieb im Newsbeitrag
news:mailman.1021400702.30533.comp.lang.ada@ada.eu.org...
[..]
> Why you would want to do this, I can't imageine, but here's how it can be
> done:
[..]
That's how I did it. It was for a function-plotter. You put in a String and
you get back a pointer to a mathmatical function.
It looked like this. I hope you get a idea from this ut down version. If
anybody's interessted in it I can send the sources.
type Single_Operation is
Op : Operator_Pointer;
L : Single_Operation_Pointer;
R : Single_Operation_Pointer;
Parameter : FLOAT;
end record;
The mathematical function is represented with a tree of Single_Operation. To
solve the equation you just have to do a recursive call:
function solve(temp : Single_Operation_Pointer) return FLOAT is
begin
if L /= null then
return temp.Op.all(solve(L), solve(R));
else
return Parameter;
end if;
end;
The program works, but it would be more elegant if I could get a pointer to
the Standard"+" without wrapping a extra function around it.
I thought it would be better to use pointers to functions because it is more
memory efficient and more flexible, too.
Have a good night!
Kai Schuelke
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-14 17:58 ` Kai Schuelke
2002-05-14 18:01 ` Preben Randhol
2002-05-14 18:24 ` David C. Hoos
@ 2002-05-14 18:25 ` Marin David Condic
2002-05-15 22:30 ` Kai Schuelke
2 siblings, 1 reply; 14+ messages in thread
From: Marin David Condic @ 2002-05-14 18:25 UTC (permalink / raw)
That seems a sick and twisted thing to want to do! :-) Anyway, off the top
of my head, I'd guess that you can't do that. Ada tends to not let you have
much control over its standard stuff because compilers may want to treat it
very differently. (There may not be an actual function to point to, correct?
The compiler may see 'Standard."+"' and just generate an add instruction.)
However, you could define your own addition function that encapsulates the
standard "+" operator and try getting a pointer to that. It might let you do
what you want to do, but I suspect that whatever you are attempting to do,
there is a better way than with pointers to functions. Don't try to make Ada
look like C or C++ - learn to do it The Ada Way(tm). Perhaps if you describe
the problem you are trying to solve, you might get a better answer here.
MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas www.pacemicro.com
Enabling the digital revolution
e-Mail: marin.condic@pacemicro.com
"Kai Schuelke" <kai.schuelke@gmx.net> wrote in message
news:3ce15027.0@news.unibw-muenchen.de...
> Hello,
>
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but
I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:
>
> plus_pointer : pointer_type := access Standard."+";
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-14 18:25 ` Marin David Condic
@ 2002-05-15 22:30 ` Kai Schuelke
2002-05-16 11:37 ` Marc A. Criley
2002-05-16 12:53 ` Georg Bauhaus
0 siblings, 2 replies; 14+ messages in thread
From: Kai Schuelke @ 2002-05-15 22:30 UTC (permalink / raw)
Hi,
I solved the problem like you suggested but I think that there must be a
better way to do it, too.
I want a program that can solve every mathematical function (e.g.: z = 2*x +
sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
function at a time. My spec looks like:
-- Spec
package math is
procedure Set_Function(Func : in STRING);
function Calculate(Value : in FLOAT) return FLOAT;
end math;
-- Sample program
with math; use math;
procedure sample is
result : FLOAT;
begin
Set_Function("2*x+sin(x)");
result := Calculate(4.0);
end;
What would be the Ada way to do this?
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-15 22:30 ` Kai Schuelke
@ 2002-05-16 11:37 ` Marc A. Criley
2002-05-16 19:06 ` Kai Schuelke
2002-05-16 12:53 ` Georg Bauhaus
1 sibling, 1 reply; 14+ messages in thread
From: Marc A. Criley @ 2002-05-16 11:37 UTC (permalink / raw)
Kai Schuelke wrote:
>
> I want a program that can solve every mathematical function (e.g.: z = 2*x +
> sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
> function at a time. My spec looks like:
>
> -- Spec
> package math is
> procedure Set_Function(Func : in STRING);
> function Calculate(Value : in FLOAT) return FLOAT;
> end math;
>
> -- Sample program
> with math; use math;
> procedure sample is
> result : FLOAT;
> begin
> Set_Function("2*x+sin(x)");
> result := Calculate(4.0);
> end;
>
> What would be the Ada way to do this?
If you're going to pass the function in as a String, as your package
shows, then you will have to parse that string into its constituent
operands and operations.. These would be stored perhaps in a tree
structure, then traverse the tree in the proper sequence, doing an
interpretive evaluation as you go. (And watch for malformed functions!)
Marc A. Criley
Consultant
Quadrus Corporation
www.quadruscorp.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-16 11:37 ` Marc A. Criley
@ 2002-05-16 19:06 ` Kai Schuelke
2002-05-16 19:18 ` Preben Randhol
0 siblings, 1 reply; 14+ messages in thread
From: Kai Schuelke @ 2002-05-16 19:06 UTC (permalink / raw)
I took the String a watched for the least significant Operation (binary
operations only at first), which is the root of my tree. I put the left side
of the String onto the left branch, same on the right side, until I get a
remaining number or a variable name.
y = 5*x + 3 becomes
Op: +
/ \
Op: * 3 Num
/ \
Num: 5 x Var
Therefore I need a record to describe a leaf that consists of
- 2 pointers to the next leafs
- A storage, that indicates what the leaf is: a operation, a constant or a
variable
I used:
type LEAF is record
Left,
Right : operation_pointer := null -- thats why I started to post in
this thread
Con : FLOAT;
Var : BOOLEAN;
end record;
Does anybody see a more ada-like way to do it? Somebody wrote that this is C
style.
Thanks
Kai Schuelke looking forward to learn something new.
PS: This program works just fine right know, but it is intended to learn
something about Ada. So please don't mind my asking for better ways.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Function pointer in Ada?
2002-05-15 22:30 ` Kai Schuelke
2002-05-16 11:37 ` Marc A. Criley
@ 2002-05-16 12:53 ` Georg Bauhaus
1 sibling, 0 replies; 14+ messages in thread
From: Georg Bauhaus @ 2002-05-16 12:53 UTC (permalink / raw)
Kai Schuelke <kai.schuelke@gmx.net> wrote:
:
: I want a program that can solve every mathematical function (e.g.: z = 2*x +
: sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
: function at a time. My spec looks like:
Maybe it's time to look at gnuplot or at a language that has
EVAL or COMPILE.
-- Georg
^ permalink raw reply [flat|nested] 14+ messages in thread