Author Message

Hi All

I would like to do complex math with Pascal (Borland Pascal 7.0)

I've thought about several solutions. A C++ friend of mine says you
can use objects and redefine the operators (+ - * / =  etc.) so that
you can type: a = b + c + d, where a,b,c,d are complex numbers. In C++
you use "operator-overload" ... is something similar possible in
object-pascal? Reading the manuals didn't give us much help. In delphi
(which my friend is using) the online help mentioned "override" - but
didn't give us any examples on operators.

So far I have found the following ways to implement complex math, but
they're not as direct as the above mentioned method.

use procedures + a global complex type for returning the results
(complex being of record type)

use procedures and specify the var you want the result put into
(eg. procedure add(a,b : complex; var c : complex)

use function and let the function return a pointer to the result
(complex being a pointer to a complex number eg. of type record)

The problem with the above mentioned method is, that if you want to
add n numbers and put them into a n+1 number you will have to build
up nested add-functions. The procedure-methods are even worse. Building
up complicated formulas becomes impossible to read!

Simply writing a = b + c + .... is far more elegant.

In C++ you write (excuse me, if this is not compilable - my friend is a
C++ rookie, not me):

class complex
private
realpart
imagpart
public
complex(float r, float i)
.
.
.

complex operator + (complex & k)
{
complex temp;
temp.realpart := k.realpart + realpart;
temp.imagpart := k.imagpart + imagpart;

Quote:
}

--

Best regards,
Claus Futtrup

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

>Hi All
>I would like to do complex math with pascal (Borland Pascal 7.0)
>I've thought about several solutions. A C++ friend of mine says you
>can use objects and redefine the operators (+ - * / =  etc.) so that
>you can type: a = b + c + d, where a,b,c,d are complex numbers. In C++
>you use "operator-overload" ... is something similar possible in
>object-pascal? Reading the manuals didn't give us much help. In Delphi
>(which my friend is using) the online help mentioned "override" - but
>didn't give us any examples on operators.

No dialect of Pascal of which I'm aware allows this (Including any
Borlandish dialect). This IS a feature of Ada however, and it's one
that I've considered SHOULD be available in Extended Pascal.

Quote:
>So far I have found the following ways to implement complex math, but
>they're not as direct as the above mentioned method.
>use procedures + a global complex type for returning the results
>  (complex being of record type)
>use procedures and specify the var you want the result put into
>  (eg. procedure add(a,b : complex; var c : complex)
>use function and let the function return a pointer to the result
>  (complex being a pointer to a complex number eg. of type record)

This is the cannonical way of adding "operations" on new types in
Pascal.

Quote:
>The problem with the above mentioned method is, that if you want to
>add n numbers and put them into a n+1 number you will have to build
>up nested add-functions. The procedure-methods are even worse. Building
>up complicated formulas becomes impossible to read!
>Simply writing a = b + c + .... is far more elegant.

Absolutely correct, and the standard arguements for allowing operator

Quote:

Depending on which platform you're working on, you may be able to find
a free or reasonably priced Ada compiler, which will give you mostly
rather more complicatedly) you could write a pre-processor that would
convert your "operators" into appropriate procedure/function calls.
Otherwise, AFAIK, you're fresh out of luck.

Quote:
>--
>Best regards,
>Claus Futtrup

HTH

Stephen Posey

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

> I've thought about several solutions. A C++ friend of mine says you
> can use objects and redefine the operators (+ - * / =  etc.) so that
> you can type: a = b + c + d, where a,b,c,d are complex numbers. In C++
> you use "operator-overload" ... is something similar possible in
> object-pascal?

No, but in the PXSC standard, implemented in GNU Pascal:
http://home.pages.de/~GNU-Pascal
--
Frank Heckenbach, Erlangen, Germany

Turbo Pascal:   http://www.mi.uni-erlangen.de/~heckenb/programs.htm

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

>>So far I have found the following ways to implement complex math, but
>>they're not as direct as the above mentioned method.

>>use procedures + a global complex type for returning the results
>>  (complex being of record type)

>>use procedures and specify the var you want the result put into
>>  (eg. procedure add(a,b : complex; var c : complex)

>>use function and let the function return a pointer to the result
>>  (complex being a pointer to a complex number eg. of type record)

>This is the cannonical way of adding "operations" on new types in
>Pascal.

There is a slightly more elegant way of doing this in TP. Make your
Complex type a string (length depends on the real type you use), for
example:

type Complex       : string[16];
ComplexStruc  : record
length : byte;
re, im : double;
end;

var a : Complex;

This way you can at least return complex results from functions. Within
the function you use a variable of type ComplexStruc set absolute to the
variable of type Complex, to access the fields, for example:

function MakeComplex (a, b : double) : Complex;

var c : Complex;
r : ComplexStruc absolute c;

begin
with r do
begin
length := 16;
re     := a;
im     := b;
end;
MakeComplex := c;
end;

This is not as good as overloading the mathematical functions, but at
least allows you to say something like c := AddComplex(a, b);

Note however that it uses two non-standard features of TP, namely the
absolute statement and the string datatype, which is considered a simple
type in TP.

Wed, 18 Jun 1902 08:00:00 GMT

[Complex numbers ...]

It follows the PXSC Standard which has this, too, but I have never
seen a working PXSC compiler.  (If you know one, please inform the
GNU Pascal mailing list, gpc AT hut DOT fi.

Hope this helps,

Peter

Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
Reply e-mail to "uni-essen"   http://home.pages.de/~peter.gerwinski/ [970201]
Sorry for the inconvenience.  http://home.pages.de/~gnu-pascal/ [970125]

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

>[Complex numbers ...]
>It follows the PXSC Standard which has this, too, but I have never
>seen a working PXSC compiler.  (If you know one, please inform the
>GNU Pascal mailing list, gpc AT hut DOT fi.
>Hope this helps,

Any idea where I can get an on-line look at the PXSC Standard?  I'd be
real curious to see it.  Barring that could you post or Email me the
basic syntax for the operator overloading feature; I'd like to see how
it compares to, say, Ada's implementation.

Quote:
>    Peter
> Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
>Reply e-mail to "uni-essen"   http://home.pages.de/~peter.gerwinski/ [970201]
>Sorry for the inconvenience.  http://home.pages.de/~gnu-pascal/ [970125]

Regards,

Stephen Posey

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

Thank God, lets hope it becomes the standard.

handled with assembly programming (the usual way to get around
restrictions)?

Thanks,
--

Best regards,
Claus Futtrup

Wed, 18 Jun 1902 08:00:00 GMT

Quote:

> Thank God, lets hope it becomes the standard.

It would be enough to thank the gpc developers... :-)

Well, it is standard. Operator overloading is part of the PXSC standard,
complex numbers are part of the extended Pascal standard. But there doesn't
seem to be a compiler but gpc that supports both (and much more, of course),
and gpc has good chances to become the "standard Pascal compiler" just like

Quote:
> I only have Borland Pascal, though - could operator overloading be
> handled with assembly programming (the usual way to get around
> restrictions)?

No, since operator overloading is merely a syntactical issue. You can do
anything you want without operator overloading, the code just doesn't look so
nice (a.init(1); b.init(2); a.add(b); writeln('1+2=', a.tostring) - you know
what I mean...).  Asm code can't change TP's syntax...

--
Frank Heckenbach, Erlangen, Germany

Turbo Pascal:   http://www.mi.uni-erlangen.de/~heckenb/programs.htm