473,796 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operations on complex numbers in C99

I understand that C99 supports a complex type and complex
arithmetic. There is nothing about it in the FAQ and online
searches turned up very little except synopses. Can anyone point
me toward sources or give examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers

Thanks,
David
Aug 5 '07 #1
27 2592
David Marsh wrote:
I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up very
little except synopses. Can anyone point me toward sources or give
examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers
The only examples I have seen are in Annex G of the standard, not the
best (being in standards speak!), but they should be enough to get you
going.

--
Ian Collins.
Aug 6 '07 #2
David Marsh <dm****@mail.co mwrites:
I understand that C99 supports a complex type and complex
arithmetic. There is nothing about it in the FAQ and online searches
turned up very little except synopses. Can anyone point me toward
sources or give examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers
The latest draft of the standard is
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 6 '07 #3
David Marsh wrote:
>
I understand that C99 supports a complex type and complex
arithmetic. There is nothing about it in the FAQ and online
searches turned up very little except synopses. Can anyone point
me toward sources or give examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers
Try N1124 and/or N869. You can get N869 in BZ2'd text form from:

<http://cbfalconer.home .att.net/download/>

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."

--
Posted via a free Usenet account from http://www.teranews.com

Aug 6 '07 #4
Ian Collins wrote:
David Marsh wrote:
>>I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up very
little except synopses. Can anyone point me toward sources or give
examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers

The only examples I have seen are in Annex G of the standard, not the
best (being in standards speak!), but they should be enough to get you
going.
Sheesh! Much easier to write my own library than to make sense of
that gobbledygook. But thanks anyway.

David
Aug 6 '07 #5
David Marsh wrote:
I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up very
little except synopses. Can anyone point me toward sources or give
examples which show how to:

-declare a complex variable
double _Complex myvar = 5.778 + 45.87*I;
(standard notation)

Some compilers like lcc-win32 or gcc use
double _Complex myvar = 5.778 + 45.87i;
but that is not standard.
-assign the real and imaginary parts
creal(myvar)=8. 0;
cimag(myvar)=6. 9;
-perform the basic +,-,*,/ operations on complex numbers
(myvar+2)/(myvar-4)

Nothing special
>
Thanks,
David
Aug 6 '07 #6
David Marsh wrote:
Ian Collins wrote:
>David Marsh wrote:
>>I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up very
little except synopses. Can anyone point me toward sources or give
examples which show how to:

-declare a complex variable
-assign the real and imaginary parts
-perform the basic +,-,*,/ operations on complex numbers

The only examples I have seen are in Annex G of the standard, not the
best (being in standards speak!), but they should be enough to get you
going.

Sheesh! Much easier to write my own library than to make sense of that
gobbledygook. But thanks anyway.

David
See my answer elsethread. It is very easy.
Aug 6 '07 #7
jacob navia wrote:
David Marsh wrote:
>I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up
very little except synopses. Can anyone point me toward sources or
give examples which show how to:

-declare a complex variable


double _Complex myvar = 5.778 + 45.87*I;
(standard notation)

Some compilers like lcc-win32 or gcc use
double _Complex myvar = 5.778 + 45.87i;
but that is not standard.
>-assign the real and imaginary parts


creal(myvar)=8. 0;
cimag(myvar)=6. 9;
>-perform the basic +,-,*,/ operations on complex numbers


(myvar+2)/(myvar-4)

Nothing special
Thanks, Jacob, but I'm having a problem with the assignments
using creal() and cimag(). It works OK if the commented out lines
are used instead. What am I doing wrong?

David

#include <stdio.h>
#include <complex.h>

int main(void)
{
//double _Complex z1 = 1.2 + .5 * I;
//double _Complex z2, z3;
double _Complex z1, z2, z3;

creal(z1) = 1.2; /* error */
cimag(z1) = .5; /* error */
z2 = csqrt(z1);
z3 = z1 + z2;
printf("sqrt = %g %gi\n", creal(z2), cimag(z2));
printf("z1+z2 = %g %gi\n", creal(z3), cimag(z3));

return 0;
}

C:\WINDOWS\Desk top\data>lcc cxtest.c
Error cxtest.c: 10 the left hand side of the assignment can't be
assigned to
Error cxtest.c: 11 the left hand side of the assignment can't be
assigned to
2 errors, 0 warnings
Aug 6 '07 #8
On Mon, 06 Aug 2007 16:00:22 +0200, jacob navia wrote:
David Marsh wrote:
>I understand that C99 supports a complex type and complex arithmetic.
There is nothing about it in the FAQ and online searches turned up very
little except synopses. Can anyone point me toward sources or give
examples which show how to:

-declare a complex variable

double _Complex myvar = 5.778 + 45.87*I;
(standard notation)
<snip>
>-assign the real and imaginary parts

creal(myvar)=8. 0;
cimag(myvar)=6. 9;
I don't understand the last two. Isn't creal a function returning a
double? How can that yield an lvalue?.
To set just the real part of myvar, wouldn't you have to write something
like
myvar = 8.0 + cimag(myvar)*I;
?
(For what its worth I much prefer gcc's __real__ and __imag__ operators;
with them you can write
__real__ myvar = 0.0;
)
Aug 6 '07 #9
jacob navia <ja***@jacob.re mcomp.frwrites:
David Marsh wrote:
>I understand that C99 supports a complex type and complex
arithmetic. There is nothing about it in the FAQ and online searches
turned up very little except synopses. Can anyone point me toward
sources or give examples which show how to:
-declare a complex variable

double _Complex myvar = 5.778 + 45.87*I;
(standard notation)
If you have '#include <complex.h>' (which is required for 'I' anyway),
you can use 'complex' rather than '_Complex'.
Some compilers like lcc-win32 or gcc use
double _Complex myvar = 5.778 + 45.87i;
but that is not standard.
and is therefore irrelevant unless you want to make your code
gratuitously non-portable.
>-assign the real and imaginary parts

creal(myvar)=8. 0;
cimag(myvar)=6. 9;
creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.
>-perform the basic +,-,*,/ operations on complex numbers

(myvar+2)/(myvar-4)
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 6 '07 #10

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

Similar topics

21
4143
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 cmplx, temp;
17
3169
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 able to CRASH the backend, there is a bug here somewhere... test=# select version(); version
6
2261
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
3677
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 complex numbers. I want to declare an array of complex numbers(with 8 elements) of this type(Cx), such that even and odd indices of array X are loaded as real
3
2065
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 instance for "x" above, I'd like to make the format string apply to an element or elements of the instance. Can I somehow overload the "%" operator for that? Thanks.
7
2380
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 the correct value: real_part = ( 3^-4.5 ) * cos( -4.5 * pi ) imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )
16
6358
by: Gianmaria Iaculo - NVENTA | last post by:
Hi there, I'm so new to python (coming from .net so excuse me for the stupid question) and i'm tring to do a very simple thing,with bytes. My problem is this: i've a byte that naturally is composed from 2 nibbles hi&low, and two chars.. like A nd B. What i wonna do is to write A to the High nibble and B to the the lower nibble. Or an other example can be i've 2 numbers.. like 7 and 8 and whant to do the
25
9732
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 instance double _Complex m = 2+3*I; printf("%Zg\n",m);
9
9952
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 = 1.0f; --------
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.