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

Decent sqrt() - makes sqrt(-1+0i) == i #4981

Closed
p6rt opened this issue Jan 2, 2016 · 5 comments
Closed

Decent sqrt() - makes sqrt(-1+0i) == i #4981

p6rt opened this issue Jan 2, 2016 · 5 comments
Labels

Comments

@p6rt
Copy link

@p6rt p6rt commented Jan 2, 2016

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

Searchable as RT127126$

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

From dankogai@gmail.com

Oops, the previous message does not contain the patch so I am submitting again. Use the patch below or just pull rakudo/rakudo#667 .

====

I am glad perl6 supports complex numbers natively. What I am not glad is that its definition of i does not agree with mathematics​:

% perl6

sqrt(-1+0i)
6.12323399573677e-17+1i

Though (-1+0i)**0.5 != 1i for most other platforms, they still get sqrt(-1+0i) right​:

% python

  Python 2.7.10 (default, Oct 23 2015, 18​:05​:06)
  [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.

from cmath import *
sqrt(-1+0j)
  1j
(-1+0j)**0.5
  (6.123233995736766e-17+1j)
^D

% irb

  irb(main)​:001​:0> require 'cmath'
  => true
  irb(main)​:002​:0> CMath.sqrt(-1)
  => (0+1.0i)
  irb(main)​:003​:0> ^D

% perl -MMath​::Complex -dE 1

  Loading DB routines from perl5db.pl version 1.49
  Editor support available.

  Enter h or 'h h' for help, or 'man perldebug' for more help.

  main​::(-e​:1)​: 1
  DB<1> p sqrt(-1+0*i)
  i
  DB<2> p (-1+0*i)**0.5
  6.12323399573677e-17+i

So here is the patch that defines sqrt in sqrt.

Dan the Complex Perl6 Newbie


src/core/Complex.pm | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

Inline Patch
diff --git a/src/core/Complex.pm b/src/core/Complex.pm
index 8314025..74b1d5b 100644
--- a/src/core/Complex.pm
+++ b/src/core/Complex.pm
@@ -80,8 +80,10 @@ my class Complex is Cool does Numeric {
     }
 
     method sqrt(Complex:D:) {
-        my Num ($mag, $angle) = self.polar;
-        $mag.sqrt.unpolar($angle/2);
+        my Num $abs = self.abs;
+        my Num $re = (($abs + self.re)/2).sqrt;
+        my Num $im = (($abs - self.re)/2).sqrt;
+        Complex.new($re, self.im < 0 ?? -$im !! $im);
     }
 
     multi method exp(Complex:D:) {
-- 
2.5.4 (Apple Git-61)
@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

From @lizmat

On 02 Jan 2016, at 20​:54, Dan Kogai (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Dan Kogai
# Please include the string​: [perl #​127126]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=127126 >

Oops, the previous message does not contain the patch so I am submitting again. Use the patch below or just pull rakudo/rakudo#667 .

====

I am glad perl6 supports complex numbers natively. What I am not glad is that its definition of i does not agree with mathematics​:

% perl6

sqrt(-1+0i)
6.12323399573677e-17+1i

Though (-1+0i)**0.5 != 1i for most other platforms, they still get sqrt(-1+0i) right​:

% python

Python 2.7.10 (default, Oct 23 2015, 18​:05​:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

from cmath import *
sqrt(-1+0j)
1j
(-1+0j)**0.5
(6.123233995736766e-17+1j)
^D

% irb

irb(main)​:001​:0> require 'cmath'
=> true
irb(main)​:002​:0> CMath.sqrt(-1)
=> (0+1.0i)
irb(main)​:003​:0> ^D

% perl -MMath​::Complex -dE 1

Loading DB routines from perl5db.pl version 1.49
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main​::(-e​:1)​: 1
DB<1> p sqrt(-1+0*i)
i
DB<2> p (-1+0*i)**0.5
6.12323399573677e-17+i

So here is the patch that defines sqrt in sqrt.

Dan the Complex Perl6 Newbie

---
src/core/Complex.pm | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/core/Complex.pm b/src/core/Complex.pm
index 8314025..74b1d5b 100644
--- a/src/core/Complex.pm
+++ b/src/core/Complex.pm
@​@​ -80,8 +80,10 @​@​ my class Complex is Cool does Numeric {
}

method sqrt\(Complex&#8203;:D&#8203;:\) \{

- my Num ($mag, $angle) = self.polar;
- $mag.sqrt.unpolar($angle/2);
+ my Num $abs = self.abs;
+ my Num $re = (($abs + self.re)/2).sqrt;
+ my Num $im = (($abs - self.re)/2).sqrt;
+ Complex.new($re, self.im < 0 ?? -$im !! $im);
}

multi method exp\(Complex&#8203;:D&#8203;:\) \{

--
2.5.4 (Apple Git-61)

Pull request 667 has been merged, this ticket can be closed.

Liz

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 2, 2016

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

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 9, 2016

From @usev6

On Sat Jan 02 15​:19​:11 2016, elizabeth wrote​:

On 02 Jan 2016, at 20​:54, Dan Kogai (via RT) <perl6-bugs-
followup@​perl.org> wrote​:

# New Ticket Created by Dan Kogai
# Please include the string​: [perl #​127126]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=127126 >

Oops, the previous message does not contain the patch so I am
submitting again. Use the patch below or just pull
rakudo/rakudo#667 .

====

I am glad perl6 supports complex numbers natively. What I am not glad
is that its definition of i does not agree with mathematics​:

% perl6

sqrt(-1+0i)
6.12323399573677e-17+1i

Though (-1+0i)**0.5 != 1i for most other platforms, they still get
sqrt(-1+0i) right​:

% python

Python 2.7.10 (default, Oct 23 2015, 18​:05​:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.

from cmath import *
sqrt(-1+0j)
1j
(-1+0j)**0.5
(6.123233995736766e-17+1j)
^D

% irb

irb(main)​:001​:0> require 'cmath'
=> true
irb(main)​:002​:0> CMath.sqrt(-1)
=> (0+1.0i)
irb(main)​:003​:0> ^D

% perl -MMath​::Complex -dE 1

Loading DB routines from perl5db.pl version 1.49
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main​::(-e​:1)​: 1
DB<1> p sqrt(-1+0*i)
i
DB<2> p (-1+0*i)**0.5
6.12323399573677e-17+i

So here is the patch that defines sqrt in sqrt.

Dan the Complex Perl6 Newbie

---
src/core/Complex.pm | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/core/Complex.pm b/src/core/Complex.pm
index 8314025..74b1d5b 100644
--- a/src/core/Complex.pm
+++ b/src/core/Complex.pm
@​@​ -80,8 +80,10 @​@​ my class Complex is Cool does Numeric {
}

method sqrt(Complex​:D​:) {
- my Num ($mag, $angle) = self.polar;
- $mag.sqrt.unpolar($angle/2);
+ my Num $abs = self.abs;
+ my Num $re = (($abs + self.re)/2).sqrt;
+ my Num $im = (($abs - self.re)/2).sqrt;
+ Complex.new($re, self.im < 0 ?? -$im !! $im);
}

multi method exp(Complex​:D​:) {
--
2.5.4 (Apple Git-61)

Pull request 667 has been merged, this ticket can be closed.

Liz

Thanks! I'm closing this ticket as 'resolved'.

@p6rt
Copy link
Author

@p6rt p6rt commented Jan 9, 2016

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

@p6rt p6rt closed this Jan 9, 2016
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.