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
Unary minus on 'numeric' inputs like '-1' #9443
Comments
From @epaCreated by @epaThis was a bit surprising: % perl -E 'say -"-1"' On checking perlop it says: Unary "-" performs arithmetic negation if the operand is numeric. If That makes some sense, then, but what is the meaning of 'numeric'? I had expected that reading the string '-1' from standard input and Perhaps the word 'numeric' in this manual page has a clearly defined I would expect to see % perl -E 'say - -1' To me, that matches the documentation better than the current behaviour. Perl Info
|
From renee.baecker@smart-websolutions.deHi,
The documentation is a bit inconsistent: perlop says "Otherwise, if the string starts with a plus or minus, a Your first example is a string. So the current behaviour seems to be OTOH, perlnumber says "The binary operators + - * / % == != > < >= ... The attached patch adds the conversion if the value of the SV Nevertheless, the documentation should be fixed, no matter what See also the discussion in #36675 Cheers, |
From renee.baecker@smart-websolutions.de57706.patch--- pp.c.orig 2008-11-10 16:17:24.584644839 +0100
+++ pp.c 2008-11-10 15:57:47.053094661 +0100
@@ -2398,6 +2398,11 @@
SV * const sv = sv_2num(TOPs);
const int flags = SvFLAGS(sv);
SvGETMAGIC(sv);
+
+ if( looks_like_number( sv ) ){
+ SvIV_please( sv );
+ }
+
if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
/* It's publicly an integer, or privately an integer-not-float */
oops_its_an_int:
--- t/op/negate.t.orig 2008-11-10 16:12:23.035094284 +0100
+++ t/op/negate.t 2008-11-10 16:16:42.186535842 +0100
@@ -18,8 +18,8 @@
is(-"10", -10, "Negation of a positive string to negative");
is(-"10.0", -10, "Negation of a positive decimal sting to negative");
is(-"10foo", -10, "Negation of a numeric-lead string returns negation of numeric");
-is(-"-10", "+10", 'Negation of string starting with "-" returns a string starting with "+" - numeric');
-is(-"-10.0", "+10.0", 'Negation of string starting with "-" returns a string starting with "+" - decimal');
+is(-"-10", 10, 'Negation of string starting with "-" returns a string starting with "+" - numeric');
+is(-"-10.0", 10.0, 'Negation of string starting with "-" returns a string starting with "+" - decimal');
is(-"-10foo", "+10foo", 'Negation of string starting with "-" returns a string starting with "+" - non-numeric');
is(-"xyz", "-xyz", 'Negation of a negative string adds "-" to the front');
is(-"-xyz", "+xyz", "Negation of a negative string to positive");
|
The RT System itself - Status changed from 'new' to 'open' |
From @obraOn Sun 26.Sep'10 at 14:18:48 -0700, Father Chrysostomos wrote:
And now you have it. I support the change. Thanks! -j |
From @cpansproutOn Mon Nov 10 07:34:21 2008, renee.baecker@smart-websolutions.de wrote:
Thank you. Applied as a5b9289.
I tweaked the perlop description in commit da2f94c. |
From [Unknown Contact. See original ticket]On Mon Nov 10 07:34:21 2008, renee.baecker@smart-websolutions.de wrote:
Thank you. Applied as a5b9289.
I tweaked the perlop description in commit da2f94c. |
@cpansprout - Status changed from 'open' to 'resolved' |
From @cpansproutOn Mon Oct 04 08:43:03 2010, sprout wrote:
Except that patch did not take magic variables into account. However, no numeric ops fully accounted for those. This is now fixed for magic variables in e38171c, which relies on
-- Father Chrysostomos |
From [Unknown Contact. See original ticket]On Mon Oct 04 08:43:03 2010, sprout wrote:
Except that patch did not take magic variables into account. However, no numeric ops fully accounted for those. This is now fixed for magic variables in e38171c, which relies on
-- Father Chrysostomos |
Migrated from rt.perl.org#57706 (status was 'resolved')
Searchable as RT57706$
The text was updated successfully, but these errors were encountered: