Skip Menu |
Report information
Id: 127126
Status: resolved
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: dankogai [at] gmail.com
Cc:
AdminCc:

Severity: (no value)
Tag: Patch
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Date: Sun, 3 Jan 2016 04:54:44 +0900
Subject: [PATCH] Decent sqrt() - makes sqrt(-1+0i) == i
From: Dan Kogai <dankogai [...] gmail.com>
To: rakudobug [...] perl.org
Download (untitled) / with headers
text/plain 1.8k
Oops, the previous message does not contain the patch so I am submitting again. Use the patch below or just pull https://github.com/rakudo/rakudo/pull/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 Show quoted text
> 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. Show quoted text
>>> from cmath import * >>> sqrt(-1+0j)
1j Show quoted text
>>> (-1+0j)**0.5
(6.123233995736766e-17+1j) Show quoted text
>>> ^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)
From: Elizabeth Mattijsen <liz [...] dijkmat.nl>
To: "Dan Kogai (via RT)" <perl6-bugs-followup [...] perl.org>
Subject: Re: [perl #127126] [PATCH] Decent sqrt() - makes sqrt(-1+0i) == i
Date: Sun, 3 Jan 2016 00:18:25 +0100
Download (untitled) / with headers
text/plain 2.3k
Show quoted text
> 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.perl.org/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 https://github.com/rakudo/rakudo/pull/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
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 2.5k
On Sat Jan 02 15:19:11 2016, elizabeth wrote: Show quoted text
> > 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.perl.org/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 > > https://github.com/rakudo/rakudo/pull/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'.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org