Thread overview
[Issue 6560] New: Exponentiation operator ^^ doesn't work for complex numbers
Aug 26, 2011
bdsatish@gmail.com
Aug 27, 2011
bdsatish@gmail.com
Aug 27, 2011
bdsatish@gmail.com
Aug 27, 2011
bdsatish@gmail.com
Oct 26, 2011
Don
August 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560

           Summary: Exponentiation operator ^^ doesn't work for complex
                    numbers
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bdsatish@gmail.com


--- Comment #0 from bdsatish@gmail.com 2011-08-26 16:19:41 PDT ---
Hi,

Whereas std.complex; implements +, -, *, etc for complex numbers, there is no exponentiation (power) operator ^^ for complex numbers.

(1+2i)^^(3+4i)  doesn't work.

I think this is so basic that it's absence deters me away from using D :-(

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc
            Version|D1 & D2                     |D1


--- Comment #1 from bearophile_hugs@eml.cc 2011-08-26 17:22:23 PDT ---
(In reply to comment #0)

> I think this is so basic that it's absence deters me away from using D :-(

Built-in complex numbers are going to be deprecated in D2. In D2 you are supposed to use the std.complex module, that supports the ^^ operator too:

https://github.com/D-Programming-Language/phobos/blob/master/std/complex.d#L324

So I restrict this enhancement request to D1 language only. If you are not interested in D1 language then I suggest you to close this enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #2 from bdsatish@gmail.com 2011-08-26 17:41:40 PDT ---
(In reply to comment #1)

> https://github.com/D-Programming-Language/phobos/blob/master/std/complex.d#L324
> 
I'm using DMD v2.051 on Ubuntu 11.04, the following isn't compiling:

import std.stdio;
import std.complex;

void main()
{
    writeln((1+3i)^^(4+6i));
}

Error message is:

complexes.d(6): Error: template std.math.pow(F,G) if (isFloatingPoint!(F) &&
isIntegral!(G)) does not match any function template declaration
complexes.d(6): Error: template std.math.pow(F,G) if (isFloatingPoint!(F) &&
isIntegral!(G)) cannot deduce template function from argument types
!()(cdouble,cdouble)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #3 from bearophile_hugs@eml.cc 2011-08-26 17:46:41 PDT ---
(In reply to comment #2)

> I'm using DMD v2.051 on Ubuntu 11.04, the following isn't compiling:
> 
> import std.stdio;
> import std.complex;
> 
> void main()
> {
>     writeln((1+3i)^^(4+6i));
> }

The deprecation of the built-in complex numbers will cause the deprecation of their nice literals too. This means in your program you are not really using the std.complex module. You have to write it this way:

import std.stdio, std.complex;

void main() {
    writeln(complex(1, 3) ^^ complex(4, 6));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #4 from bdsatish@gmail.com 2011-08-27 03:00:15 PDT ---
(In reply to comment #3)
> The deprecation of the built-in complex numbers will cause the deprecation of their nice literals too. This means in your program you are not really using the std.complex module.
Looks like D is going in the wrong direction. IIUC, there is no longer a builtin imaginary type. D1 agrees with Prof. Kahan but D2 diverges so that it can introduce inadvertent errors (http://www.digitalmars.com/d/2.0/cppcomplex.html).

> You have to write it this way:
> 
> import std.stdio, std.complex;
> 
> void main() {
>     writeln(complex(1, 3) ^^ complex(4, 6));
> }

The above program gives an error:

complexes.d(6): Error: undefined identifier complex, did you mean struct
Complex(T) if (isFloatingPoint!(T))?
complexes.d(6): Error: undefined identifier complex, did you mean struct
Complex(T) if (isFloatingPoint!(T))?

I thought 1,3, etc. are integers, may be, so changing 1 -> 1.0, 3 -> 3.0 etc. it still gives same error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #5 from bearophile_hugs@eml.cc 2011-08-27 04:03:38 PDT ---
(In reply to comment #4)

> D1 agrees with Prof. Kahan but D2 diverges so that it
> can introduce inadvertent errors
> (http://www.digitalmars.com/d/2.0/cppcomplex.html).

Please, show examples of the real problems, so Andrei & Walter will judge the situation.


> The above program gives an error:
> 
> complexes.d(6): Error: undefined identifier complex, did you mean struct
> Complex(T) if (isFloatingPoint!(T))?
> complexes.d(6): Error: undefined identifier complex, did you mean struct
> Complex(T) if (isFloatingPoint!(T))?
> 
> I thought 1,3, etc. are integers, may be, so changing 1 -> 1.0, 3 -> 3.0 etc. it still gives same error.

Recently they have improved the std.complex module. I am using a very recent version of DMD (2.055head) so maybe you have to wait for the release of 2.055 to see the correct code, or you have to download and compile the bleeding edge compiler + Phobos.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #6 from bdsatish@gmail.com 2011-08-27 07:14:06 PDT ---
(In reply to comment #5)

I found a workaround for ^^, looks like this works:

import std.stdio;
import std.complex;

void main()  {
    auto x = Complex!real(1,2);
    auto y = Complex!real(3,4);

    writeln(x^^y);
}

> Please, show examples of the real problems, so Andrei & Walter will judge the situation.
Let's consider the same cases as mentioned in the "Semantics" section of: http://www.digitalmars.com/d/2.0/cppcomplex.html

Well, unfortunately std.complex; in v2.051 is not up-to-date. I wanted to
verify whether the following identities hold:
* sqrt(conj(z)) == conj(sqrt(z)) whenever z takes on negative real values
* conj(log(z)) == log(conj(z)) whenever z takes on negative real values

I couldn't test the above because sqrt( ) and log( ) are not implemented for
complex numbers in 2.051. I do not know the situation in 2.055head. Could you
please verify this, if possible ?

However, I do have a testcase for this one:
* (1 - infinity*i)*i == (infinity + i) but not (infinity + NaN*i)

where i = sqrt(-1), the imaginary constant. The following demonstrates how a "spurious NaN" is generated, as mentioned in the above link.

import std.stdio;
import std.complex;

union INFINITY {
    float inf;
    uint num;
};

union NOT_A_NUMBER {
    float nan;
    uint  num;
};

void main()  {
    INFINITY posinf, neginf;
    NOT_A_NUMBER nan;

    posinf.num = 0x7F800000;     // positive infinity
    neginf.num = 0xFF800000;     // negative infinity
    nan.num = 0x7FC00000;        // Not a Number

    writefln("Positive infinity = %f", posinf.inf);
    writefln("Negative infinity = %f", neginf.inf);
    writefln("Not a number = %f", nan.nan);

    Complex!float i = Complex!float(0.0f, 1.0f);        // i
    Complex!float p = Complex!float(1.0f, neginf.inf);  // 1-inf*i

    writefln("%f",p*i);   // Must be (1-inf*i)*i = inf + 1i
}

The expected answer is (+inf, 1.0f) but the above prints "inf-nani".

I do not know how it was in D1, but in D2 the above produces mathematically incorrect result. Again, I'm unaware how it is in C99/C++, etc. or is it always like this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560



--- Comment #7 from bearophile_hugs@eml.cc 2011-08-27 07:29:09 PDT ---
(In reply to comment #6)

> I couldn't test the above because sqrt( ) and log( ) are not implemented for
> complex numbers in 2.051. I do not know the situation in 2.055head. Could you
> please verify this, if possible ?

Added two days ago: https://github.com/D-Programming-Language/phobos/commit/cc2d7c9703c5b264f9e6aebeec314917b7805bec

I suggest you do dowload the latest std.complex:

https://raw.github.com/D-Programming-Language/phobos/master/std/complex.d

Probably it works with DMD 2.054 compiler too (or it works with tiny changes).


> The expected answer is (+inf, 1.0f) but the above prints "inf-nani".
> 
> I do not know how it was in D1, but in D2 the above produces mathematically incorrect result. Again, I'm unaware how it is in C99/C++, etc. or is it always like this.

I presume that Don or Walter will take a look at this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6560


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
            Version|D1                          |D2


--- Comment #8 from Don <clugdbug@yahoo.com.au> 2011-10-26 00:19:41 PDT ---
There's no way this can be a D1 bug. D1 doesn't have the ^^ operator.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 01, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6560


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@kyllingen.net
         Resolution|                            |WONTFIX


--- Comment #9 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2012-02-01 14:50:32 PST ---
So, to summarise:  In D2, built-in complex numbers are being deprecated and std.complex supports the ^^ operator, while in D1, ^^ doesn't exist.  Seems we can close this, then.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------