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
Comments
From dankogai@gmail.comOops, 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
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)
% irb irb(main):001:0> require 'cmath' % perl -MMath::Complex -dE 1 Loading DB routines from perl5db.pl version 1.49 Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): 1 So here is the patch that defines sqrt in sqrt. Dan the Complex Perl6 Newbie src/core/Complex.pm | 6 ++++-- Inline Patchdiff --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 @lizmat
Pull request 667 has been merged, this ticket can be closed. Liz |
The RT System itself - Status changed from 'new' to 'open' |
From @usev6On Sat Jan 02 15:19:11 2016, elizabeth wrote:
Thanks! I'm closing this ticket as 'resolved'. |
@usev6 - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#127126 (status was 'resolved')
Searchable as RT127126$
The text was updated successfully, but these errors were encountered: