473,804 Members | 2,141 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
27 2594
On Aug 7, 9:59 am, Keith Thompson <ks...@mib.orgw rote:
jacob navia <ja...@jacob.re mcomp.frwrites:
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.
It's useful to know this sort of thing, for when
you have to maintain someone else's code.
Aug 6 '07 #11
Old Wolf <ol*****@inspir e.net.nzwrites:
On Aug 7, 9:59 am, Keith Thompson <ks...@mib.orgw rote:
>jacob navia <ja...@jacob.re mcomp.frwrites:
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.

It's useful to know this sort of thing, for when
you have to maintain someone else's code.
A fair point. It's pretty obvious what '5.778 + 45.87i' means, but I
suppose not everyone would recognize that it's an extension.

--
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 7 '07 #12
David Marsh wrote:
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

The correct syntax must be:

z1 = 1.2 + cimag(z1)*I;
Aug 7 '07 #13
in comp.lang.c i read:
>creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.
that is not a general restriction on functions, which may certainly return
a modifiable lvalue. though creal and cimag do not.

--
a signature
Aug 8 '07 #14
On Aug 8, 10:35 am, those who know me have no need of my name <not-a-
real-addr...@usa.net wrote:
in comp.lang.c i read:
creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.

that is not a general restriction on functions, which may certainly return
a modifiable lvalue.
Can you provide such an example?

Robert Gamble

Aug 8 '07 #15
Robert Gamble said:
On Aug 8, 10:35 am, those who know me have no need of my name <not-a-
real-addr...@usa.net wrote:
>in comp.lang.c i read:
>creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.

that is not a general restriction on functions, which may certainly
return a modifiable lvalue.

Can you provide such an example?
#include <stdio.h>

char *foo(char *s)
{
printf("%s\n", s);
return s;
}

int main(void)
{
char array_of_modifi able_lvalues[] = "Hello World";
*foo(array_of_m odifiable_lvalu es) = 'C';
puts(array_of_m odifiable_lvalu es);
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 8 '07 #16
In article <Lo************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.
>>that is not a general restriction on functions, which may certainly
return a modifiable lvalue.
>Can you provide such an example?
>char *foo(char *s)
{
printf("%s\n", s);
return s;
}
The value returned by foo() is a pointer to a modifiable lvalue, not a
modifiable lvalue.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 8 '07 #17
Richard Heathfield wrote:
Robert Gamble said:
>On Aug 8, 10:35 am, those who know me have no need of my name <not-a-
real-addr...@usa.net wrote:
>>in comp.lang.c i read:

creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.

that is not a general restriction on functions, which may certainly
return a modifiable lvalue.

Can you provide such an example?

#include <stdio.h>

char *foo(char *s)
{
printf("%s\n", s);
return s;
}

int main(void)
{
char array_of_modifi able_lvalues[] = "Hello World";
*foo(array_of_m odifiable_lvalu es) = 'C';
puts(array_of_m odifiable_lvalu es);
return 0;
}
foo does not return a lvalue, especially a modifiable one. It returns a
value ("rvalue") of type pointer to char. You can use this value to
construct a lvalue, but if that is good enough to claim the function itself
returns a lvalue, what's preventing foo in

int foo(void) {
return 0;
}
int main(void) {
int arr[] = { 1 };
arr[foo()] = 0;
return arr[0];
}

from being considered as returning a lvalue?
Aug 8 '07 #18
Richard Tobin said:
In article <Lo************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>>creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.
>>>that is not a general restriction on functions, which may certainly
return a modifiable lvalue.
>>Can you provide such an example?
>>char *foo(char *s)
{
printf("%s\n", s);
return s;
}

The value returned by foo() is a pointer to a modifiable lvalue, not a
modifiable lvalue.
Good point. I cannot, then, see any way that a function may return a
modifiable lvalue. I await education with eager delight.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 8 '07 #19
On Aug 8, 12:24 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Robert Gamble said:
On Aug 8, 10:35 am, those who know me have no need of my name <not-a-
real-addr...@usa.net wrote:
in comp.lang.c i read:
creal() and cimag() are functions, which therefore do not yield
lvalues. You can't assign to them.
that is not a general restriction on functions, which may certainly
return a modifiable lvalue.
Can you provide such an example?

#include <stdio.h>

char *foo(char *s)
{
printf("%s\n", s);
return s;

}

int main(void)
{
char array_of_modifi able_lvalues[] = "Hello World";
*foo(array_of_m odifiable_lvalu es) = 'C';
puts(array_of_m odifiable_lvalu es);
return 0;

}
As I fully suspect you already know, foo does not return a modifiable
lvalue, it returns an rvalue which when dereferenced produces an
lvalue.

Robert Gamble

Aug 8 '07 #20

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

Similar topics

21
4148
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
2263
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
3681
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
2068
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
2383
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
6359
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
9735
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
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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...
1
10351
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
10096
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
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4311
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
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.