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
sqrt without EXPR and Math::Complex always 0 #9620
Comments
From frank.wiegand@gmail.comThis is a bug report for perl from frank.wiegand@gmail.com, perldoc -f sqrt says: | Return the square root of EXPR. If EXPR is omitted, returns square Let's try: % perl -MMath::Complex -E '$_=+1; say sqrt' The EXPR form works fine: % perl -MMath::Complex -E 'say sqrt +1' Without -MMath::Complex everything works fine, according to the docs. Thanks, Frank Flags: Site configuration information for perl 5.10.0: Configured by Debian Project at Thu Jan 1 15:40:00 UTC 2009. Summary of my perl5 (revision 5 version 10 subversion 0) configuration: Locally applied patches: @INC for perl 5.10.0: Environment for perl 5.10.0: PATH=/sbin:/home/fw/bin:/home/fw/bin:/usr/local/bin:/usr/bin:/bin:/usr/games |
From wyp3rlx02@sneakemail.comHi Frank, thanks for the bug report, Frank Wiegand wrote:
This is because Math::Complex defines a new sqrt() function that can I did *not* check all other Math::Complex functions for the same defect. Best regards, |
From wyp3rlx02@sneakemail.com0001-Quick-fix-for-Math-Complex-sqrt-not-falling-back.patchFrom 0e83e9816665b6a5a66f31456bd554f8e0417544 Mon Sep 17 00:00:00 2001
From: Steffen Mueller <smueller@cpan.org>
Date: Fri, 16 Jan 2009 09:16:57 +0100
Subject: [PATCH] Quick fix for Math::Complex::sqrt() not falling back to $_ (RT #62412)
---
lib/Math/Complex.pm | 2 +-
lib/Math/Complex.t | 10 ++++++++++
2 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/lib/Math/Complex.pm b/lib/Math/Complex.pm
index 7d05a44..6c5b3b4 100644
--- a/lib/Math/Complex.pm
+++ b/lib/Math/Complex.pm
@@ -672,7 +672,7 @@ sub arg {
# Therefore if you want the two solutions use the root().
#
sub sqrt {
- my ($z) = @_;
+ my ($z) = @_ ? @_ : $_;
my ($re, $im) = ref $z ? @{$z->_cartesian} : ($z, 0);
return $re < 0 ? cplx(0, CORE::sqrt(-$re)) : CORE::sqrt($re)
if $im == 0;
diff --git a/lib/Math/Complex.t b/lib/Math/Complex.t
index ee02341..99514fc 100755
--- a/lib/Math/Complex.t
+++ b/lib/Math/Complex.t
@@ -106,6 +106,16 @@ push(@script, <<'EOT');
(Im($z) + 1 ) < $eps;
EOT
push(@script, qq(print "ok $test\\n"}\n));
+
+ $test++;
+ push @script, <<'TEST_FOR_DEFAULT_ARG';
+{
+ local $_ = cplx( 4, 0);
+ my $res = sqrt();
+ print "# $test $_=cplx(4, 0); sqrt() == 2\n";
+ print 'not ' if abs($res - 2) > $eps;
+TEST_FOR_DEFAULT_ARG
+ push(@script, qq(print "ok $test\\n"}\n));
}
test_mutators();
--
1.5.4.3
|
The RT System itself - Status changed from 'new' to 'open' |
From wyp3rlx02@sneakemail.comHi Andrew, hi Jarkko, the Porting/Maintainers.pl document in core now has a field that The attached patch fixes a minor issue in Math::Complex and adds a test. Best regards, |
From wyp3rlx02@sneakemail.comHi Frank, thanks for the bug report, Frank Wiegand wrote:
This is because Math::Complex defines a new sqrt() function that can I did *not* check all other Math::Complex functions for the same defect. Best regards, |
From wyp3rlx02@sneakemail.com0001-Quick-fix-for-Math-Complex-sqrt-not-falling-back.patchFrom 0e83e9816665b6a5a66f31456bd554f8e0417544 Mon Sep 17 00:00:00 2001
From: Steffen Mueller <smueller@cpan.org>
Date: Fri, 16 Jan 2009 09:16:57 +0100
Subject: [PATCH] Quick fix for Math::Complex::sqrt() not falling back to $_ (RT #62412)
---
lib/Math/Complex.pm | 2 +-
lib/Math/Complex.t | 10 ++++++++++
2 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/lib/Math/Complex.pm b/lib/Math/Complex.pm
index 7d05a44..6c5b3b4 100644
--- a/lib/Math/Complex.pm
+++ b/lib/Math/Complex.pm
@@ -672,7 +672,7 @@ sub arg {
# Therefore if you want the two solutions use the root().
#
sub sqrt {
- my ($z) = @_;
+ my ($z) = @_ ? @_ : $_;
my ($re, $im) = ref $z ? @{$z->_cartesian} : ($z, 0);
return $re < 0 ? cplx(0, CORE::sqrt(-$re)) : CORE::sqrt($re)
if $im == 0;
diff --git a/lib/Math/Complex.t b/lib/Math/Complex.t
index ee02341..99514fc 100755
--- a/lib/Math/Complex.t
+++ b/lib/Math/Complex.t
@@ -106,6 +106,16 @@ push(@script, <<'EOT');
(Im($z) + 1 ) < $eps;
EOT
push(@script, qq(print "ok $test\\n"}\n));
+
+ $test++;
+ push @script, <<'TEST_FOR_DEFAULT_ARG';
+{
+ local $_ = cplx( 4, 0);
+ my $res = sqrt();
+ print "# $test $_=cplx(4, 0); sqrt() == 2\n";
+ print 'not ' if abs($res - 2) > $eps;
+TEST_FOR_DEFAULT_ARG
+ push(@script, qq(print "ok $test\\n"}\n));
}
test_mutators();
--
1.5.4.3
|
From @AbigailOn Fri, Jan 16, 2009 at 09:41:12AM +0100, Steffen Mueller wrote:
That will not pick up any lexical $_. It would if you'd used a '_' prototype: sub sqrt (_) { Abigail |
From wyp3rlx02@sneakemail.comHi, Abigail abigail-at-abigail.be |newsgroups6| wrote:
doh, of course. Thanks for catching that. If I recall correctly, the _ In any case, please disregard my patch. Best regards, |
From @AbigailOn Fri, Jan 16, 2009 at 11:33:08AM +0100, Steffen Mueller wrote:
Indeed. Both the '_' prototype and lexical $_ are new in 5.10. Abigail |
From @jhiAbigail wrote:
Which means without BEGIN + eval games that can't be in M::C.
|
From @nwc10On Fri, Jan 16, 2009 at 07:58:00AM -0500, Jarkko Hietaniemi wrote:
I don't think that an eval is needed - simply assign one of the alternatives Or alternatively, a Corelist_pm.PL that has the two alternatives, and (Hmm. Without XS, I can't see any way to change the prototype of a function Nicholas Clark |
From @rgs2009/1/16 Nicholas Clark <nick@ccl4.org>:
Scalar::Util::set_prototype() ? |
From @jhiSteffen Mueller wrote:
I would prefer the patches being sent to us (jhi or zefram).
|
From wyp3rlx02@sneakemail.comHi, Jarkko Hietaniemi jhi-at-iki.fi |newsgroups6| wrote:
Thanks for the info, I updated Porting/Maintainers.pl accordingly. Best regards, |
From @jhiRafael Garcia-Suarez wrote:
Okay, call me dim, but how do I write an interface that (either accepts |
From @AbigailOn Sun, Jan 18, 2009 at 02:05:12PM -0500, Jarkko Hietaniemi wrote:
This works for me: #!/usr/bin/perl use strict; use Scalar::Util 'set_prototype'; $_ = "python"; sub hello { BEGIN { { __END__ With the appropriate line uncommented, it prints "Hello, world" for me Abigail |
From @jhi
No eval, true. But "no warnings 'syntax'", if that's left in I get these: Prototype mismatch: sub Math::Complex::abs (_) vs none ... Yuck. |
From @AbigailOn Sun, Jan 18, 2009 at 09:04:16PM -0500, Jarkko Hietaniemi wrote:
Ah, I didn't realize that. (I always have 'syntax' warnings disabled). But when I enable syntax warnings, I only get the warning if I do: sub hello; If I set the prototype after the sub has been defined, all seems to be well. Abigail |
From @nwc10On Mon, Jan 19, 2009 at 03:22:01AM +0100, Abigail wrote:
That's a valid warning.
Indeed. The facetious version: I have a patch. It removes code (that's always good) Inline Patchdiff --git a/cpan/Math-Complex/lib/Math/Complex.pm b/cpan/Math-Complex/lib/Math/
index 8475a2b..afec359 100644
--- a/cpan/Math-Complex/lib/Math/Complex.pm
+++ b/cpan/Math-Complex/lib/Math/Complex.pm
@@ -73,20 +73,6 @@ use Scalar::Util qw(set_prototype);
use warnings;
no warnings 'syntax'; # To avoid the (_) warnings.
-BEGIN {
- # For certain functions that we override, in 5.10 or better
- # we can set a smarter prototype that will handle the lexical $_
- # (also a 5.10+ feature).
- if ($] >= 5.010000) {
- set_prototype \&abs, '_';
- set_prototype \&cos, '_';
- set_prototype \&exp, '_';
- set_prototype \&log, '_';
- set_prototype \&sin, '_';
- set_prototype \&sqrt, '_';
- }
-}
-
my $i;
my %LOGN;
1: All those calls to set_prototype aren't achieving anything It's necessary to set the prototype *after* the subroutine is defined Otherwise, it turns out that what happens is: 1: sub is stubbed and has prototype added by the call (in that BEGIN block) And as the body has no prototype, that (lack of) prototype wins. Compare: $ ./perl -Ilib -MScalar::Util=set_prototype -lwe 'BEGIN {set_prototype \&foo, "_"}; print prototype \&foo; sub foo {}' $ ./perl -Ilib -MScalar::Util=set_prototype -lwe 'sub foo {} BEGIN {set_prototype \&foo, "_"}; print prototype \&foo' I don't have a patch to offer. Moving the BEGIN block later doesn't cause (And I'm more interested in why something else I'm trying doesn't quite work, Nicholas Clark |
From @nwc10On Wed, Nov 17, 2010 at 10:19:21PM +0000, Nicholas Clark wrote:
Specifically, by "later" I had it at the end, so that the code of the Inline Patchdiff --git a/cpan/Math-Complex/lib/Math/Complex.pm b/cpan/Math-Complex/lib/Math/
index 8475a2b..201b4fd 100644
--- a/cpan/Math-Complex/lib/Math/Complex.pm
+++ b/cpan/Math-Complex/lib/Math/Complex.pm
@@ -806,7 +806,7 @@ sub Im {
# Return or set rho(w).
#
sub rho {
- Math::Complex::abs(@_);
+ &Math::Complex::abs(@_);
}
#
@@ -874,7 +874,7 @@ sub log {
#
# Alias for log().
#
-sub ln { Math::Complex::log(@_) }
+sub ln { &Math::Complex::log(@_) }
#
# log10
Nicholas Clark |
From @dcollinsnThis was fixed in 5.10.1. -- |
@dcollinsn - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#62412 (status was 'resolved')
Searchable as RT62412$
The text was updated successfully, but these errors were encountered: