473,786 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::complex

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
if the default constructor left the values uninitialized.

void f()
{
using namespace std;
complex c[100];
for (int i = 0; i < 100; ++i) c[i] = complex(1, 0);
}

Values of built in types work perfectly without zero initialization, so why
make complex different and cause additional learning trouble?

Best regards,
Marcin
Jul 22 '05 #1
2 3468

"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message
news:cn******** **@korweta.task .gda.pl...
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
if the default constructor left the values uninitialized.
How have you managed to time that, or is it just a guess?

void f()
{
using namespace std;
complex c[100];
for (int i = 0; i < 100; ++i) c[i] = complex(1, 0);
}

Values of built in types work perfectly without zero initialization, so
why make complex different and cause additional learning trouble?


Most people would say that uninitialised variables are what causes learning
trouble, not to mention bugs.

How about rewriting your code like this?

#include <vector>
#include <complex>

void f()
{
using namespace std;
vector<complex> c(100, complex(1, 0));
}

john
Jul 22 '05 #2
>> 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 if the default constructor left the values uninitialized.
How have you managed to time that, or is it just a guess?


I have generated an assembly listing on MSVC .NET 2003 all optimizations
enabled. Here's the function:

#include <complex>
std::complex<fl oat> f()
{
std::complex<fl oat> c[100];
for (int i = 0; i < 100; ++i)
c[i] = std::complex<fl oat>(1, 0);
return c[50];
}

Below is a relevant part of assembly listing for the function f. Note the
loop that is created by line #5 (the definition of the array):

; 5 : std::complex<fl oat> c[100];

lea eax, DWORD PTR _c$[esp+800]
mov ecx, 100 ; 00000064H
xor edx, edx
$L11604:
mov DWORD PTR [eax], edx
mov DWORD PTR [eax+4], edx
add eax, 8
sub ecx, 1
jne SHORT $L11604

; 6 : for (int i = 0; i < 100; ++i)

xor eax, eax
mov ecx, 1065353216 ; 3f800000H
$L10133:

; 7 : c[i] = std::complex<fl oat>(1, 0);

mov DWORD PTR _c$[esp+eax*8+800], ecx
mov DWORD PTR _c$[esp+eax*8+804], edx
add eax, 1
cmp eax, 100 ; 00000064H
jl SHORT $L10133
Best regards,
Marcin

Jul 22 '05 #3

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

Similar topics

8
3661
by: Marc Schellens | last post by:
Does anybody know an easy way to get the atan of a complex number in C++? thanks, marc
4
5454
by: zender | last post by:
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:
9
3436
by: Greg Buchholz | last post by:
/* While writing a C++ version of the Mandelbrot benchmark over at the "The Great Computer Language Shootout"... http://shootout.alioth.debian.org/gp4/benchmark.php?test=mandelbrot&lang=all ....I've come across the issue that complex<double>'s seem quite slow unless compiled with -ffast-math. Of course doing that results in incorrect answers because of rounding issues. The speed difference for
5
3174
by: ma740988 | last post by:
Trying to determine how to get the max element from a complex sequence. Given: int main() { //typedef std::complex < double > C typedef std::vector < std::complex < double > > complex_vec; typedef complex_vec::const_iterator cvect_it; complex_vec x_vec;
2
2829
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 some body show me how to redefine the new operator for an class already defined in the library? Thanks,
4
4335
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 = " << result << std::endl;
1
9516
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 element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
5
6054
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can use it, but std::sort doesn't seem to find it. I have copied a very simple example below. Everything compiles just fine when the line with std::sort function is commented out, but with that line included a whole slew of errors are given like: ...
7
2631
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 returned. I tend to believe that MSVC made a big mistake here!!! But unfortunately I must use it at work, which sucks!!!
0
9647
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
10363
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
10164
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...
0
8989
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
7512
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
6745
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.