473,804 Members | 2,079 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 8, 12:48 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Richard Tobin said:
In article <LoqdncSPdoMrcC TbnZ2dnUVZ8s3in ...@bt.com>,
Richard Heathfield <r...@see.sig.i nvalidwrote:
>>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.
It cannot, functions calls evaluate either to void or rvalue
expressions, that was my point.

Robert Gamble

Aug 8 '07 #21
those who know me have no need of my name <no************ ****@usa.netwri tes:
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.
I wrote the quoted text ("creal() and cimag() are functions, ...").
Please don't snip attribution lines.

--
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 8 '07 #22
In article <11************ *********@w3g20 00hsg.googlegro ups.com>,
Robert Gamble <rg*******@gmai l.comwrote:
>Good point. I cannot, then, see any way that a function may return a
modifiable lvalue.
>It cannot, functions calls evaluate either to void or rvalue
expressions, that was my point.
I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 8 '07 #23
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <11************ *********@w3g20 00hsg.googlegro ups.com>,
Robert Gamble <rg*******@gmai l.comwrote:
>>Good point. I cannot, then, see any way that a function may return a
modifiable lvalue.
>>It cannot, functions calls evaluate either to void or rvalue
expressions , that was my point.

I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
Except that no standard library function is defined to return an
lvalue.

I suppose, theoretically, that such a function could be defined in a
future version of the standard, but I sincerely hope that doesn't
happen (unless a mechanism is added to allow user-defined functions to
return lvalues).

<OT>Perhaps the OP who claimed that functions can return modifiable
lvalues was thinking of C++?</OT>

--
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 8 '07 #24
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
>Except that no standard library function is defined to return an
lvalue.
Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8. 0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.

Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 8 '07 #25
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>>I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
>>Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8. 0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.

Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).
Exactly. Making it a macro would make sense (and I'd argue for
calling it CREAL rather than creal). Making it a "function" that
behaves in a way that no user-defined function can possibly behave
would be silly.

--
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 9 '07 #26
On Wed, 08 Aug 2007 23:38:45 +0000, Richard Tobin wrote:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>>I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
>>Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8. 0;
Where will we stop? What about atan(myvar) = 1 to set myvar to
tan(1)? :-)
One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.
That would sound great, but for consistency with the rest of the
language I'd prefer setreal(z, x).
Much like ferror() which doesn't return a lvalue (but, depending
on how the FILE object is made, it could be very straightforward
to write a macro which does), and we have to use clearerr() to set
it to zero (and have no way to set it to one).
Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).

-- Richard
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 9 '07 #27
Richard Tobin wrote:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>>I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
>Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8. 0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.
This is EXACTLY how it is implemented in lcc-win32.
This makes access to the parts of a complex number VERY
efficient.

The correct way is it however:

double _Complex z1;

....

z1 = 22.0+cimag(z1)* I; // Sets the real part to 22
or
z1 = creal(z1)+22*I; // Sets the imaginary part to 22

Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).

-- Richard
Aug 9 '07 #28

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
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...
0
10350
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
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,...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.