### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: h2pas and C99's complex numbers  (Read 1646 times)

#### tom.schoonjans

• Newbie
• Posts: 1
##### h2pas and C99's complex numbers
« on: November 26, 2018, 03:01:59 pm »
Hi,

I am looking into accessing a C library from pascal that uses C99's 'double complex' variable type. When using h2pas on the header file containing such complex number variable types, I get a syntax error.

I am guessing that this isn't supported by h2pas?

Is there an official way to safely map C99's complex numbers to their pascal counterparts in UCOMPLEX?

Thanks in advance for any help!

#### jamie

• Hero Member
• Posts: 3635
##### Re: h2pas and C99's complex numbers
« Reply #1 on: November 28, 2018, 11:53:29 pm »
I think someone made a lib for complex numbers....

You could do that yourself you know , you need a record with the Real and imaginary fields and a few basic
functions to do the math...

from the help file..
[next] [prev] [prev-tail] [tail] [up]

15.4  Arithmetic operators

Arithmetic operators define the action of a binary operator. Possible operations are:

multiplication

To multiply two types, the
*

division

To divide two types, the
/

+

substraction

To subtract two types, the
-

exponentiation

To exponentiate two types, the
**

Unary minus

is used to take the negative of the argument following it.

Symmetric Difference

To take the symmetric difference of 2 structures, the
><

The definition of an arithmetic operator takes two parameters, except for unary minus, which needs only 1 parameter. The first parameter must be of the type that occurs at the left of the operator, the second parameter must be of the type that is at the right of the arithmetic operator. The result type must match the type that results after the arithmetic operation.

To compile an expression as

var

R : real;

C,Z : complex;

begin

C:=R*Z;

end;

One needs a definition of the multiplication operator as:

Operator * (r : real; z1 : complex) z : complex;

begin

z.re := z1.re * r;

z.im := z1.im * r;

end;

As can be seen, the first operator is a real, and the second is a complex. The result type is
complex.

P.S.
Forgot
include the unit "ucomplex" but I don't know if it supports all of what you need, you may need to add on..

« Last Edit: November 28, 2018, 11:58:50 pm by jamie »
The only true wisdom is knowing you know nothing

#### Kays

• Full Member
• Posts: 215
• Whasup!?
##### Re: h2pas and C99's complex numbers
« Reply #2 on: November 29, 2018, 03:49:59 pm »
[…] Is there an official way to safely map C99's complex numbers […]
In C99's § 6.2.5 (13) (page 46) its says:
Quote
Each complex type has the same representation and alignment requirements as an array type containing exactly two elements of the corresponding real type; the first element is equal to the real part, and the second element to the imaginary part, of the complex number.
So, ucomplex.pp defines complex as a record of two reals back-to-back. real is either a single or double, depending on the platform. So you can either map single complex or double complex, but not both. Now, I'm no specialist about aligning, but that's potentially an issue, too.
Yours Sincerely
Kai Burghardt