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

Zero to the zeroeth power: Complex numbers not consistent with others. #5504

Closed
p6rt opened this issue Jul 31, 2016 · 6 comments
Closed

Zero to the zeroeth power: Complex numbers not consistent with others. #5504

p6rt opened this issue Jul 31, 2016 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jul 31, 2016

Migrated from rt.perl.org#128785 (status was 'resolved')

Searchable as RT128785$

@p6rt
Copy link
Author

p6rt commented Jul 31, 2016

From @smls

Perl 6 agrees with pretty much all programming languages that 0**0 == 1, and does so consistently for the Int, int, Rat, FatRat, and Num types.

Just the Complex type doesn't play along​:

  ➜ say (0+0i) ** 0;
  NaN+NaN\i

  ➜ say (0+0i) ** (0+0i);
  0+0i

I think these should both of these print 1+0i.

------ Mathematical background​: ------

The value of "Zero to the power of zero" seems to be a historically controversial topic in mathematics​: https://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_power_of_zero

From that wiki article I gather that (0**0 == 1) is popular among those working with practical formulae and actual values, and (0**0 == undefined) popular among those focusing theoretical considerations about limits. Unsurprisingly, programming languages have sided with the former definition...

In any case, I don't see a reason to treat Complex differently from the other numeric types.
In fact, isn't the whole idea of complex numbers, to be a generalization of real numbers so that as long as the imaginary part is 0, you get the exact same results as with real numbers?

Then again, I'm not a mathematician. Input from an expert would be appreciated.

------ Other programming languages​: ------

The ones I checked, seem to agree with my line of thought​:

Perl 5​:

  $ perl -E 'use Math​::Complex; say cplx(0,0) ** 0;'
  1
  $ perl -E 'use Math​::Complex; say cplx(0,0) ** cplx(0,0);'
  1

Ruby​:

  $ ruby -e 'puts Complex(0,0) ** 0'
  1+0i
  $ ruby -e 'puts Complex(0,0) ** Complex(0,0)'
  1+0i

Python​:

  $ python
  >>> print((0+0j) ** 0)
  (1+0j)
  >>> print((0+0j) ** (0+0j))
  (1+0j)

For more examples, see​: http://rosettacode.org/wiki/Zero_to_the_zero_power

@p6rt
Copy link
Author

p6rt commented Jul 31, 2016

From @smls

As a counter-point, the C++ standard library seems to think that 0+0i ** 0+0i == NaN+NaN\i​:
http://rosettacode.org/wiki/Zero_to_the_zero_power#C.2B.2B

So maybe there *are* reasons for not extending the (practically convenient but theoretically dubious) 0⁰=1 definition to complex numbers?

Still couldn't find any language or source to back up Perl 6's (0+0i ** 0+0i == 0+0i) though.
The reasonable choices seem to be 1 or NaN.

@p6rt
Copy link
Author

p6rt commented Sep 11, 2016

From @smls

Comparison table of the different cases of "zero to the zeroeth power" in Rakudo​:

  n n**0 0**n n**n
  ------------- --------- --------- ---------
  0 1 1 1
  0.0 1 1 1
  FatRat.new(0) 1 1 1
  0e0 1 1 1
  0+0i NaN+NaN\i NaN+NaN\i 0+0i

I think that's a pretty clear indication that the Complex case is in need of fixing.
I'm just not sure which way​:

- either make all entries in the bottom row produce 1+0i (like Perl 5, Python, Ruby)
- or make all entries in the bottom row produce NaN+NaN\i (like C++)

Again, input from someone well versed in math programming would be great.

@p6rt
Copy link
Author

p6rt commented Nov 17, 2016

From @zoffixznet

On Sat, 10 Sep 2016 23​:17​:33 -0700, smls75@​gmail.com wrote​:

Comparison table of the different cases of "zero to the zeroeth power"
in Rakudo​:

n n**0 0**n n**n
------------- --------- --------- ---------
0 1 1 1
0.0 1 1 1
FatRat.new(0) 1 1 1
0e0 1 1 1
0+0i NaN+NaN\i NaN+NaN\i 0+0i

I think that's a pretty clear indication that the Complex case is in
need of fixing.
I'm just not sure which way​:

- either make all entries in the bottom row produce 1+0i (like
Perl 5, Python, Ruby)
- or make all entries in the bottom row produce NaN+NaN\i (like
C++)

Again, input from someone well versed in math programming would be
great.

Thanks for the report.

The 0+0i case was returned because the case that does "zero to any power is zero" did not consider the power of zero. The NaNs in our case (and I suspect in C's case) are due to intermediary math operations involving logs and log(0) is a -Inf, which the rest of operations mangled further into a NaN

Fixed for all cases to return 1+0i in​: rakudo/rakudo@7f322436c9
Tests added in​: Raku/roast@6cf2e012c2

@p6rt
Copy link
Author

p6rt commented Nov 17, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Nov 17, 2016

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Nov 17, 2016
@p6rt p6rt added the math label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant