473,399 Members | 3,603 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,399 software developers and data experts.

Assignment to std::complex number

Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?

Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;
}

zender@ashes:~/c++$ g++ -Wall -g -I${HOME}/include -o tst tst.cc
tst.cc: In function `int main()':
tst.cc:17: error: non-lvalue in assignment

Jul 22 '05 #1
4 5430

<ze****@uci.edu> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?
I don't think so.

Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:
True. They return by value, not by reference.

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;
scalar = std::complex<double>(0, scalar.imag());
}


-Mike
Jul 22 '05 #2
ze****@uci.edu wrote:
Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?


No, but there is support for adding this feature. See
http://www.open-std.org/jtc1/sc22/wg...004/n1589.html.

Jonathan
Jul 22 '05 #3
Thanks for the info!
Making real and imaginary component public and separatly assignable
would simplify many physics codes.

Charlie

Jul 22 '05 #4
<ze****@uci.edu> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com
Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?

Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;
}


For what it is worth VC++.Net offers the following "set function" as a
non-standard extension (presumably courtesy of the Dinkumware library that
it uses):

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real(0.0);
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5

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

Similar topics

8
by: Marc Schellens | last post by:
Does anybody know an easy way to get the atan of a complex number in C++? thanks, marc
2
by: Marcin Kalicinski | last post by:
Hi, I found out that default constructor of std::complex class initializes the value to (0,0). I wonder why is it so? Because of this, the code below is about two times slower than it could be...
12
by: vj | last post by:
Hi! I have a piece of code (shown below) involving complex numbers. The code is not running and giving error ("Invalid floating point operation" and "SQRT:Domain error"). I would be very...
2
by: PengYu.UT | last post by:
Hi, In FFTW (http://www.fftw.org/), it defines the funciton fftw_malloc to allocate memory properly aligned. However, I only want to use new to allocate memory for std::complex<double>. Can...
4
by: Fredy Halter | last post by:
the following code is not working: std::complex<long doublex(1.,1.); std::complex<long doubleresult(0.,0.); result = 1./x; std::cout << "x = " << x << std::endl; std::cout << "r = " <<...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
4
by: Peng Yu | last post by:
Hi, I'm wondering how to get the cubic root for a complex number? It seems that cbrt does not work complex numbers. Thanks, Peng #include <complex> #include <iostream>
4
by: Reshmi | last post by:
Hi, I have this C code which does complex number arithmetic. When I try to write a similar file for C++, it says that "creal’ was not declared in this scope". Can anyone give a better idea to...
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
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,...
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
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...
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...
0
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,...

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.