Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math/cmplx: Log and Phase are defined on the whole complex plane #9074

Closed
gopherbot opened this issue Nov 8, 2014 · 4 comments
Closed

math/cmplx: Log and Phase are defined on the whole complex plane #9074

gopherbot opened this issue Nov 8, 2014 · 4 comments
Milestone

Comments

@gopherbot
Copy link

by gunnarthormagnusson:

The Log and Phase functions in the math/cmplx library are too permissive. They should be
defined on the standard branch of the complex logarithm, but are in fact defined on the
whole punctured complex plane. Details follow.

> What does 'go version' print?

go version go1.3.3 linux/amd64

> What steps reproduce the problem?
> If possible, include a link to a program on play.golang.org.

Calculate the complex logarithm of 1, i, -1 and -1. See here:

    http://play.golang.org/p/imVbDZGdMw


> What happened?

From what I understand of the comments in the source, the Phase function (used in Log)
should return the arguments of a complex number in the range [-Pi,Pi]. It can not do
this for purely mathematical reasons (see the first paragraph of
http://en.wikipedia.org/wiki/Complex_logarithm#Branches_of_the_complex_logarithm).

> What should have happened instead?

I'm guessing that the authors wanted to define the principal branch of the complex
logarithm; the one that excludes the negative real line. Then

    cmplx.Log(complex(-1,0))

should have returned an error or some kind of 'not a number' type.

> Please provide any additional information below.

The Wikipedia article on the complex logarithm:
http://en.wikipedia.org/wiki/Complex_logarithm

Any introductory textbook or course on complex analysis will have all the details.

I'd like to stress that this is really a problem. Since Go has complex numbers and
functions to deal with them built in, users will be inclined to trust and use them. That
the logarithm and phase functions do not have explicit branches selected and in fact
accept all arguments can and will cause very subtle and difficult to find bugs in the
programs of people who will want to use Go for calculations with complex numbers. (Julia
and Mandelbrot fractals are one example of applications of such calculations.) Those
people *will* blame Go for these bugs when they arise.

A possible solution is to decide and say explicitly that the library functions for
complex numbers in Go use the principal branch, and then modify the functions to return
errors when given purely negative real numbers. Users can then roll their own custom
branch logarithms if they want.

Thanks,
gthm
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.5.

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014
@rsc rsc removed the repo-main label Apr 14, 2015
@ALTree
Copy link
Member

ALTree commented May 10, 2015

The following c program:

#include <stdio.h>
#include <math.h>
#include <complex.h>

int main(void)
{
    double complex z = clog(1 - 0*I); 
    printf("log() = %.1f%+fi\n", creal(z), cimag(z));

    z = clog(0 + I); 
    printf("log() = %.1f%+fi\n", creal(z), cimag(z));

    z = clog(-1 + 0*I); 
    printf("log() = %.1f%+fi\n", creal(z), cimag(z));

    z = clog(0 - 1*I); 
    printf("log() = %.1f%+fi\n", creal(z), cimag(z));
}

Compiled with gcc (Debian 4.9.2-10) 4.9.2 gives no error and the same results of the linked Go playground code:

$ gcc -std=c11 -o clogs clogs.c 
$ ./clogs 
    log() = 0.0-0.000000i
    log() = 0.0+1.570796i
    log() = 0.0+3.141593i
    log() = 0.0-1.570796i

So Go behaviour is similar to the C/C++ one.

@dspezia
Copy link
Contributor

dspezia commented May 10, 2015

Wolfram Alpha gives also the same result as Go and C.
https://www.wolframalpha.com/input/?i=log%28-1%29

@rsc
Copy link
Contributor

rsc commented Jun 29, 2015

I don't know enough to know what is "correct" here, but it seems unlikely that both C and Wolfram Alpha are wrong, and Go matches them.

@rsc rsc closed this as completed Jun 29, 2015
@golang golang locked and limited conversation to collaborators Jun 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants