473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

portable complex numbers between C, C++

I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif
How portable is this?

Oct 20 '05 #1
6 2538
In article <11**********************@g47g2000cwa.googlegroups .com>,
<ma**@borgerding.net> wrote:
I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif
How portable is this?


The porability of the raw code above is not problem
in and of itself, however, it does imply a C99 implemenation
and/or compatibility across complex's which is not necessarily
there. I'm not sure if that's what you're getting at or not.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #2
ma**@borgerding.net wrote:
I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif
How portable is this?


Not portable at all. If you want to pass any information between C and
C++, the data structure has to be the same, it has to be 'struct' and you
need to include the same definition in all modules. There is no "float
complex" type in C++, and therefore its internals or its behaviour is
unknown. If somehow you discover that std::complex<float> behaves the
same as "float complex" with your C++ and C compilers, its a pure
coincidence AFA languages are concerned.

V
Oct 20 '05 #3
Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;

Oct 20 '05 #4
<ma**@borgerding.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;


We've added that requirement, IIRC. The data layouts should
be compatible.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 20 '05 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:HG*******************@newsread1.mlpsca01.us.t o.verio.net...
ma**@borgerding.net wrote:
I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif
How portable is this?


Not portable at all. If you want to pass any information between C and
C++, the data structure has to be the same, it has to be 'struct' and you
need to include the same definition in all modules. There is no "float
complex" type in C++, and therefore its internals or its behaviour is
unknown. If somehow you discover that std::complex<float> behaves the
same as "float complex" with your C++ and C compilers, its a pure
coincidence AFA languages are concerned.


Not really. Both languages (now) require that the data layout be
the same as float[2], which is akin to the ancient Fortran
requirement. Note also that TR1 reconciles the C99 and C++
complex libraries.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 20 '05 #6
In article <PK******************************@giganews.com>,
"P.J. Plauger" <pj*@dinkumware.com> wrote:
<ma**@borgerding.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;


We've added that requirement, IIRC. The data layouts should
be compatible.


Actually we haven't yet:

http://www.open-std.org/jtc1/sc22/wg...ctive.html#387

still Open <sigh>. But P.J.'s words are close enough. The data layout
compatibility with C99 has strong support, and if nothing else from 387
passes, that part still will (imho). Plus I know of no C++
implementations where the layout compatibility is not already there.

-Howard
Oct 25 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
by: seia0106 | last post by:
Hello, I have an array X=, whose even and odd indices should represent real and imaginary parts of complex numbers. This I want to use in a routine that uses the typedef double Cx; for storing...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
7
by: schaefer.mp | last post by:
To compute the absolute value of a negative base raised to a fractional exponent such as: z = (-3)^4.5 you can compute the real and imaginary parts and then convert to the polar form to get...
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.