LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2016, 03:06 PM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Rep: Reputation: Disabled
Trouble with double complex numbers in C++


It is Ubuntu 14.04. The compiler is g++.

I need to operate with complex<double> values. When I include

Code:
#include <cmath>
#include <complex>
I expect to get full range of complex numbers in my code. Instead this definition

Code:
typedef complex<double> dcomp;
gives me an error on compilation: complex is not a template.

This website, however, shows that it is a correct usage:

http://stackoverflow.com/questions/1...-number-i-in-c

Also this website

http://stackoverflow.com/questions/1...t-defined-in-c

suggests this construction, that allegedly allows to use complex<double>:

template <typename T, typename U>
inline std::complex<T> operator*(std::complex<T> lhs, const U& rhs)
{
return lhs *= rhs;
}

It does not even compile. It gives me two errors:

complex in namespace std does not name a type

If I remove std::

I get another error:

Complex is not a template

Why is it so difficult to use double in complex numbers in C++?

Thanks, - Alex
 
Old 01-23-2016, 04:56 PM   #2
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Also this nuisance:

In the website
http://en.cppreference.com/w/cpp/numeric/complex

Code:
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
 
int main()
{
    using namespace std::literals;
    std::cout << std::fixed << std::setprecision(1);
 
    std::complex<double> z1 = 1i * 1i;     // imaginary unit squared
    std::cout << "i * i = " << z1 << '\n';
 
    std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
    std::cout << "pow(i, 2) = " << z2 << '\n';
 
    double PI = std::acos(-1);
    std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
    std::cout << "exp(i, pi) = " << z3 << '\n';
 
    std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
    std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
}
Trying to compile with g++ I get a pageful of errors:

Quote:
g++ -std=c++11 test1.cpp
test1.cpp: In function ‘int main()’:
test1.cpp:8:26: error: ‘literals’ is not a namespace-name
using namespace std::literals;
^
test1.cpp:8:34: error: expected namespace-name before ‘;’ token
using namespace std::literals;
^
test1.cpp:11:31: error: unable to find numeric literal operator ‘operator"" i’
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
^
test1.cpp:11:36: error: unable to find numeric literal operator ‘operator"" i’
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
^
test1.cpp:14:40: error: unable to find numeric literal operator ‘operator"" i’
std::complex<double> z2 = std:ow(1i, 2); // imaginary unit squared
^
test1.cpp:18:40: error: unable to find numeric literal operator ‘operator"" i’
std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
^
test1.cpp:21:36: error: unable to find numeric literal operator ‘operator"" i’
std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
^
test1.cpp:22:43: error: ‘z5’ was not declared in this scope
std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
^
alex@alex-HP-ProBook-6555b:~/GFORTRAN-APPS/SPHERE-DESIGN/SPHERE-CPP$
Why do I have so much trouble with complex numbers in C++?

Thanks.
 
Old 01-23-2016, 05:05 PM   #3
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
And how about this? I have the following function header:

Code:
public : void legendreTransform (int NN,complex TotFourierTransf[][NN],complex GaussLegTransf[][NN],PhiLimits phiBounds)
  {
This is what I get on compilation:

Quote:
g++ -std=c++11 sphere_perspective.cpp
sphere_perspective.cpp:551:72: error: use of parameter outside function body before ] token
public : void legendreTransform (int NN,complex TotFourierTransf[][NN],complex GaussLegTransf[][NN],PhiLimits phiBounds)
^
sphere_perspective.cpp:551:73: error: expected ) before , token
public : void legendreTransform (int NN,complex TotFourierTransf[][NN],complex GaussLegTransf[][NN],PhiLimits phiBounds)
^
sphere_perspective.cpp:551:74: error: variable or field complex declared void
public : void legendreTransform (int NN,complex TotFourierTransf[][NN],complex GaussLegTransf[][NN],PhiLimits phiBounds)
^
sphere_perspective.cpp:551:74: error: expected ; at end of member declaration
sphere_perspective.cpp:551:82: error: GaussLegTransf does not name a type
public : void legendreTransform (int NN,complex TotFourierTransf[][NN],complex GaussLegTransf[][NN],PhiLimits phiBounds)
What is wrong with my compiler? Am I making mistakes?

Thanks.
 
Old 01-23-2016, 10:21 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Hi Alex,

Post #1: Use:
Code:
typedef std::complex<double> dcomp
Post #2: http://en.cppreference.com/w/cpp/num...mplex#Literals Notice that operator""i is a c++14 feature. (-std=c++14)

Post #3: The compiler expects a member declaration to be terminated with a semicolon. You have an opening brace.
Code:
sphere_perspective.cpp:551:74: error: expected ; at end of member declaration
 
Old 01-24-2016, 05:03 PM   #5
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Thank you norobro. I did pay attention to c++14 but the compiler rejected it. I tried this:

Code:
g++ -std=c++14 sphere_perspective.cpp
Where and how can I get C++ 2014 version?

Also, I am sure I had a semicolon at the end of that line. I removed this line of code after posting. I now reentered it and got an error anyway:

Code:
sphere_perspective.cpp:14:9: error: complex in namespace std does not name a type
 typedef std::complex<double> dcomp;
The line of code is now:

Code:
typedef std::complex<double> dcomp;
^
How can I advance my compiler to C++14?

Thanks, - Alex
 
Old 01-24-2016, 05:21 PM   #6
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
I tried clang-3.5. It gave me a copious output but many lines had a proclamation: "note." So, there were errors, warnings and notes. What is the note in this context?

Thanks, - Alex
 
Old 01-24-2016, 06:29 PM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
The only thing that requires c++14 is operator""i. In lieu of using it in that program, you can declare/define a complex variable.

The following compiles on my machine using g++-4.7. Give it a try:
Code:
// g++ -std=c++11 -o test test.cpp

#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
 
int main()
{
    typedef std::complex<double> dcomp;    // your typedef
    //using namespace std::literals;
    std::cout << std::fixed << std::setprecision(1);
    
    dcomp one_i(0.0, 1.0);
    dcomp z1 = one_i * one_i;     // imaginary unit squared
    std::cout << "i * i = " << z1 << '\n';
	
    dcomp z2 = std::pow(one_i, 2); // imaginary unit squared
    std::cout << "pow(i, 2) = " << z2 << '\n';
	
    double PI = std::acos(-1);
    dcomp z3 = std::exp(one_i * PI); // Euler's formula
    std::cout << "exp(i, pi) = " << z3 << '\n';
	
    dcomp two_i(0.0, 2.0);					     
    dcomp z4 = 1. + two_i, z5 = 1. - two_i; // conjugates
    std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
}
If you want to post your code, I'll be happy to take a look.
 
Old 01-24-2016, 11:33 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: Are you you just experiencing, or do you actually want to work with complex numbers? If it is the latter, then use C instead of C++.
 
Old 01-25-2016, 12:51 AM   #9
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by NevemTeve View Post
@OP: Are you you just experiencing, or do you actually want to work with complex numbers? If it is the latter, then use C instead of C++.
why do you think so?

I see no advantage in the C version, it's pretty similar


C++
Code:
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
 
int main()
{
    using namespace std::literals;
    std::cout << std::fixed << std::setprecision(1);
 
    std::complex<double> z1 = 1i * 1i;     // imaginary unit squared
    std::cout << "i * i = " << z1 << '\n';
 
    std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
    std::cout << "pow(i, 2) = " << z2 << '\n';
 
    double PI = std::acos(-1);
    std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
    std::cout << "exp(i, pi) = " << z3 << '\n';
 
    std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
    std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
}

C
Code:
#include <stdio.h>
#include <complex.h>
#include <tgmath.h>
 
int main(void)
{
    double complex z1 = I * I;     // imaginary unit squared
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
 
    double complex z2 = pow(I, 2); // imaginary unit squared
    printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));
 
    double PI = acos(-1);
    double complex z3 = exp(I * PI); // Euler's formula
    printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));
 
    double complex z4 = 1+2*I, z5 = 1-2*I; // conjugates
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4*z5), cimag(z4*z5));
}
 
Old 01-26-2016, 08:49 AM   #10
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
@NevemTeve, I need to work with complex numbers. I can invoke (use) complex numbers all right, it is the double complex that gives me trouble. I need higher precision than float complex. Spherical Harmonics that I have to use are complex functions by definition, I mean all values are complex: a + ib.
 
Old 01-26-2016, 10:00 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Sorry for the delay; actually, I wouldn't use C99 either, instead I'd manually declare a struct of two double (re and im; or perhaps r and phi).

Well, back to your case, please provide a minimal complete example, that shows the problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trouble with Random numbers and C errigour Programming 2 09-18-2013 07:18 PM
LXer: The trouble with rounding floating point numbers LXer Syndicated Linux News 0 08-12-2006 01:54 PM
LVM EXT3 RAID Double Trouble tokehs Fedora 0 04-03-2005 11:37 AM
Double Trouble: Music cd's not loading and video device problems. ObLiViOuS1886 Slackware - Installation 10 05-10-2004 09:51 PM
C# trouble converting from string to double. exodist Programming 2 02-23-2004 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration