Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating complex infinity with std::complex<T> in C++

I'm trying to create a complex infinity equal to Inf+Inf*j where j is the complex variable. When I do this :

#include <complex>
#include <limits>
using std;

...

complex<double> attempt1 =
   complex<double>( numeric_limits<double>::infinity(),
                    numeric_limits<double>::infinity() );

returns the complex number (NaN + Inf*j).

But

complex<double> attempt2 =
   complex<double>( numeric_limits<double>::infinity() );

returns the complex number (Inf + 0*j).

Also :

complex<double> attempt_at_imag_inf =
   complex<double>(any_value_here, numeric_limits<double>::infinity());

returns the complex number (NaN + Inf*j).

Does anybody know what's going on here? Any time I attempt to have infinty for the imaginary part, then NaN is written on the real part.

The above only applies to types that support NaN and Infinity of course. I am using g++ v4.6.1. I've looked at the numeric_limits header and there is no indication that the above should happen at all.

To put the above into context, I'm actually doing the above in a partial specialization of numeric_limits for complex. Many thanks for considering this problem.

REVISION TO ORIGINAL POST

I'm providing a complete but short program to illustrate the problem. I've also included some more qualifying information on how the program should be compiled to generate the results.

#include <iostream>
#include <complex>
#include <limits>

using namespace std;

int main(int argc, char* argv[])
{

   complex<double> my_complex_inf =
      complex<double>(numeric_limits<double>::infinity(),
                      numeric_limits<double>::infinity());

   cout << "my_complex_inf = " << my_complex_inf << endl;

   complex<double> attempt2 =
      complex<double>( numeric_limits<double>::infinity() );

   cout << "attempt2 = " << attempt2 << endl;

   double any_value_here = 0;

   complex<double> attempt_at_imag_inf =
      complex<double>(0, numeric_limits<double>::infinity());

   cout << "attempt_at_imag_inf = " << attempt_at_imag_inf << endl;

   return 0;

}

Compiling the above in g++ version 4.6.1 on Ubuntu with the -std=c++0x gives the following results :

my_complex_inf = (nan,inf)
attempt2 = (inf,0)
attempt_at_imag_inf = (nan,inf)

Without the -std=c++0x option the results are :

my_complex_inf = (inf,inf)
attempt2 = (inf,0)
attempt_at_imag_inf = (0,inf)

So the question really is WHY DOES GNU g++ V4.6.1 GIVE THE ANSWERS IT DOES WHEN C++0x IS SPECIFIED?

REVISION 2 TO ORIGINAL POST

I just tried the following in Octave (MATLAB-like numerics package) :

a=inf + j*inf

And the answer is :

a = NaN + Infi

This is exactly what I see in my C++11 code (C++0x). I don't know what Octave is compiled with (it's a combination of C++ and FORTRAN I believe) but if that package returns the result that I am getting, then I assume that this is well-known behaviour.

However, I have looked at the C++11 draft standard and cannot find any mention of this behaviour.

REVISION 3 TO ORIGINAL POST

Adding the following line

my_complex_inf.real(my_complex_inf.imag());

to just after the construction of my_complex_inf return the "correct" answer (inf, inf) when compiled for C++11. Unfortunately, this is now a 2-step process and I am unable to create this kind of complex infinity in a constexpr function.

like image 927
Ender St. John-Olcayto Avatar asked May 14 '12 16:05

Ender St. John-Olcayto


People also ask

How do you make a complex number in C++?

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real() – It returns the real part of the complex number. imag() – It returns the imaginary part of the complex number.

Can infinity be a complex number?

When talking about z=∞, we are referring to something called complex infinity, which can be regarded as a complex number with infinite magnitude and undefined argument.

Can we use imaginary numbers in C++?

We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class. In this example we are creating one complex type class, a function to display the complex number into correct format.


1 Answers

A scalar Inf converted to complex is inf+0 j. Ths is correct above. A scalar Inf offset in the complex plane impliesv aa rotation and, is not calculable, therefore Nan is correct. What is the problem again?

"There be dragons."

like image 96
starbolin Avatar answered Oct 04 '22 20:10

starbolin