Skip to content

[doc] perlnumber: "minus plus number" looks like number #18584

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

Closed
rsFalse opened this issue Feb 15, 2021 · 5 comments · Fixed by #18595
Closed

[doc] perlnumber: "minus plus number" looks like number #18584

rsFalse opened this issue Feb 15, 2021 · 5 comments · Fixed by #18595

Comments

@rsFalse
Copy link

rsFalse commented Feb 15, 2021

Where

https://perldoc.perl.org/perlnumber

Description

Quoted "minus plus number" behaves like negative number in numeric context.
Seems undocumented. Usually numbers start with either + or - or none.

(perl v5.32.0)

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $A = "-+3";
my $B = " -+3";
my $C = "- +3";
my $D = "-+ 3";

print map "[$_]", map $_ + 1,
	$A, $B, $C, $D,
	;

OUTPUT:

Argument "-+3" isn't numeric in addition (+) at minus_plus_number_1.pl line 13.
Argument " -+3" isn't numeric in addition (+) at minus_plus_number_1.pl line 13.
Argument "- +3" isn't numeric in addition (+) at minus_plus_number_1.pl line 13.
Argument "-+ 3" isn't numeric in addition (+) at minus_plus_number_1.pl line 13.
[-2][-2][-2][1]
@Grinnz
Copy link
Contributor

Grinnz commented Feb 15, 2021

Everything behaves like a number in numeric context. As you can see from the warnings, they don't "look" like numbers. If you're suggesting it's a bug that only the last one gets truncated to 0 in numeric context, perhaps.

@karenetheridge
Copy link
Member

Behaviour changed between 5.29.1 and 5.29.2, but I don't see anything relevant in the perldeltas. (Bisection within this range will likely give an answer though.)

: [ether@jaeger ~]$; perlbrew use 29.1@std
: [ether@jaeger ~]$; perl -wle'print "-+3" + 0'
Argument "-+3" isn't numeric in addition (+) at -e line 1.
0
: [ether@jaeger ~]$; perlbrew use 29.2@std
: [ether@jaeger ~]$; perl -wle'print "-+3" + 0'
Argument "-+3" isn't numeric in addition (+) at -e line 1.
-3

@karenetheridge
Copy link
Member

Perhaps this is it (found in perl5300delta):

       •   A new function ""my_strtod"" in perlapi or its synonym, Strtod(), is now available with the same signature as the libc strtod().  It provides strotod() equivalent behavior on all
           platforms, using the best available precision, depending on platform capabilities and Configure options, while handling locale-related issues, such as if the radix character should be a
           dot or comma.

@t-a-k
Copy link
Contributor

t-a-k commented Feb 18, 2021

This seems to be because perl's string-to-number conversion function (Perl_my_atof3) passes a string after the first (optional) sign to strtod, so that sign symbols will be parsed twice.

I found an another symptom where a hexadecimal notation is sometimes (unexpectedly) recognized:

% perl -wle 'print "+0x123" + 0'
Argument "+0x123" isn't numeric in addition (+) at -e line 1.
0
% perl -wle 'print "+ 0x123" + 0'
Argument "+ 0x123" isn't numeric in addition (+) at -e line 1.
291

@sisyphus
Copy link
Contributor

Just to point out that it was stated in #17062 that hexadecimal strings should numify to zero ... so I guess this erroneous handling of "+ 0x123" ought to be fixed.

t-a-k added a commit to t-a-k/perl5 that referenced this issue Feb 18, 2021
…mber

Perl_my_atof3 used to pass a substring after the first (optional) sign
to (S_)strtod, which causes wrong numifications for strings like "-+3"
or "+ 0x123" (for the latter case, while Perl_my_atof3 already had
the code to block "0x" prefixes, this string will slip through due to
the space character in it).

For GH Perl#18584.
khwilliamson pushed a commit that referenced this issue Jul 25, 2021
…mber

Perl_my_atof3 used to pass a substring after the first (optional) sign
to (S_)strtod, which causes wrong numifications for strings like "-+3"
or "+ 0x123" (for the latter case, while Perl_my_atof3 already had
the code to block "0x" prefixes, this string will slip through due to
the space character in it).

For GH #18584.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants