Skip to content

-'-10' eq '+10' #8045

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
p5pRT opened this issue Jul 27, 2005 · 8 comments
Closed

-'-10' eq '+10' #8045

p5pRT opened this issue Jul 27, 2005 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 27, 2005

Migrated from rt.perl.org#36675 (status was 'resolved')

Searchable as RT36675$

@p5pRT
Copy link
Author

p5pRT commented Jul 27, 2005

From pfusik@op.pl

This is a bug report for perl from pfusik@​op.pl,
generated with the help of perlbug 1.35 running under perl v5.8.7.


The behavior of the unary minus operator is inconsistent between
strings representing positive and negative numbers​:

perl -le 'print for -"10",-"10.0",-"10foo",-"-10",-"-10.0",-"-10foo",-" -10",-" -10.0",-" -10foo"'
-10
-10
-10
+10
+10.0
+10foo
10
10
10

Perl guesses whether the argument of '-' is a number.
I think that all strings in the above example represent numbers.
Note that strings starting with a minus are not treated as numbers
-- I think they should be. However, with a leading space, they _are_
treated as numbers.



Flags​:
  category=core
  severity=medium


Site configuration information for perl v5.8.7​:

Configured by ActiveState at Sat Jun 4 07​:30​:27 PDT 2005.

Summary of my perl5 (revision 5 version 8 subversion 7) configuration​:
  Platform​:
  osname=linux, osvers=2.2.17, archname=i686-linux-thread-multi
  uname='linux gimlet 2.2.17 #1 sun jun 25 09​:24​:41 est 2000 i686 unknown '

config_args='-ders -Dcc=gcc -Dusethreads -Duseithreads -Ud_sigsetjmp -Uinstallusrbinperl -Ulocincpth= -Uloclibpth= -Accflags=-DUSE_S
ITECUSTOMIZE -Duselargefiles -Accflags=-DNO_HASH_SEED -Uusemallocwrap -Dinc_version_list=5.8.6/$archname 5.8.6 5.8.4/$archname 5.8.4
5.8.3/$archname 5.8.3 5.8.2/$archname 5.8.2 5.8.1/$archname 5.8.1 5.8.0/$archname
5.8.0 -Duseshrplib -Dprefix=/opt/ActivePerl-5.8 -Accflags=-DBUILT_BY_ACTIVESTATE -Dcf_by=ActiveState -Dcf_email=support@​ActiveState.
com'
  hint=recommended, useposix=true, d_sigaction=define
  usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
  useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
  use64bitint=undef use64bitall=undef uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
  Compiler​:
  cc='gcc', ccflags
='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DUSE_SITECUSTOMIZE -DNO_HASH_SEED -DBUILT_BY_ACTIVESTATE -fno-strict-aliasing -pip
e -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  optimize='-O2',

cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DUSE_SITECUSTOMIZE -DNO_HASH_SEED -DBUILT_BY_ACTIVESTATE -fno-strict-alias
ing -pipe'
  ccversion='', gccversion='2.95.2 20000220 (Debian GNU/Linux)', gccosandvers=''
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
  alignbytes=4, prototype=define
  Linker and Libraries​:
  ld='gcc', ldflags =''
  libpth=/lib /usr/lib /usr/local/lib
  libs=-lnsl -lndbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc -lposix
  perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc -lposix
  libc=/lib/libc-2.1.3.so, so=so, useshrplib=true, libperl=libperl.so
  gnulibc_version='2.1.3'
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef,
ccdlflags='-Wl,-E -Wl,-rpath,/opt/ActivePerl-5.8/lib/5.8.7/i686-linux-thread-multi/CORE'
  cccdlflags='-fpic', lddlflags='-shared'

Locally applied patches​:
  ACTIVEPERL_LOCAL_PATCHES_ENTRY
  # if !defined(PERL_DARWIN)
  Iin_load_module moved for compatibility with build 806
  # endif
  # if defined(__hpux)
  Avoid signal flag SA_RESTART for older versions of HP-UX
  # endif
  PerlEx hacks for CGI​::Carp
  Less verbose ExtUtils​::Install and Pod​::Find
  instmodsh upgraded from ExtUtils-MakeMaker-6.25
  24699 ICMP_UNREACHABLE handling in Net​::Ping
  21540 Fix backward-compatibility issues in if.pm


@​INC for perl v5.8.7​:
  /opt/ActivePerl-5.8/lib/5.8.7/i686-linux-thread-multi
  /opt/ActivePerl-5.8/lib/5.8.7
  /opt/ActivePerl-5.8/lib/site_perl/5.8.7/i686-linux-thread-multi
  /opt/ActivePerl-5.8/lib/site_perl/5.8.7
  /opt/ActivePerl-5.8/lib/site_perl
  .


Environment for perl v5.8.7​:
  HOME=/home/fox
  LANG=en_US.UTF-8
  LANGUAGE (unset)
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)
  PATH=/opt/ActivePerl-5.8/bin​:/usr/local/bin​:/usr/bin​:/bin​:/usr/X11R6/bin​:/usr/java/j2sdk1.4.2_01/bin​:/home/fox/bin
  PERL_BADLANG (unset)
  SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Dec 16, 2005

From @smpeters

[pfusik@​op.pl - Wed Jul 27 15​:43​:24 2005]​:

This is a bug report for perl from pfusik@​op.pl,
generated with the help of perlbug 1.35 running under perl v5.8.7.

-----------------------------------------------------------------

The behavior of the unary minus operator is inconsistent between
strings representing positive and negative numbers​:

perl -le 'print for
-"10",-"10.0",-"10foo",-"-10",-"-10.0",-"-10foo",-" -10",-" -10.0",-"
-10foo"'
-10
-10
-10
+10
+10.0
+10foo
10
10
10

Perl guesses whether the argument of '-' is a number.
I think that all strings in the above example represent numbers.
Note that strings starting with a minus are not treated as numbers
-- I think they should be. However, with a leading space, they _are_
treated as numbers.

Let's see what Scalar​::Util​::looks_like_number() says...

steve@​kirk​:~/perl-current$ perl -MScalar​::Util=looks_like_number -
le 'for(-"10" \
,-"10.0",-"10foo",-"-10",-"-10.0",-"-\ 10foo",-" -10",-" -10.0",-" -
10foo") \
{ print; print looks_like_number($_) ? "YES" : "NO"}'
-10
YES
-10
YES
-10
YES
+10
YES
+10.0
YES
+ 10foo
NO
10
YES
10
YES
10
YES

Note that strings starting with a minus are not treated as numbers

Perl certainly does treat them like numbers when they appear to be
numbers. In your particular example, however, Perl doesn't believe
that the string "-\ 10foo" is a number, and I really have to agree with
it.

@p5pRT
Copy link
Author

p5pRT commented Dec 16, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Dec 16, 2005

@smpeters - Status changed from 'open' to 'resolved'

@p5pRT p5pRT closed this as completed Dec 16, 2005
@p5pRT
Copy link
Author

p5pRT commented Dec 17, 2005

From BQW10602@nifty.com

The behavior of the unary minus operator is inconsistent between
strings representing positive and negative numbers​:

perl -le 'print for
-"10",-"10.0",-"10foo",-"-10",-"-10.0",-"-10foo",-" -10",-" -10.0",-"
-10foo"'
-10
-10
-10
+10
+10.0
+10foo
10
10
10

Perl guesses whether the argument of '-' is a number.
I think that all strings in the above example represent numbers.
Note that strings starting with a minus are not treated as numbers
-- I think they should be. However, with a leading space, they _are_
treated as numbers.

It is documented in perlop.pod that unary minus opposes the sign
at the start of a string. (try not-a-number, like -"-xyz")

  Unary ``-''
  .... Otherwise, if the string starts with a plus or minus,
  a string starting with the opposite sign is returned.

cf. PP(pp_negate)
...
  else if (SvPOKp(sv)) {
...
  else if (*s == '+' || *s == '-') {
  sv_setsv(TARG, sv);
  *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
  }

I think the case of (-'-10' eq '+10') is conformable
with the documentation (thus not a bug).
(I guess the case of -" -10.0" is not (clearly?) documented.)

Regards,
SADAHIRO Tomoyuki

@p5pRT
Copy link
Author

p5pRT commented Dec 17, 2005

From @smpeters

[BQW10602@​nifty.com - Fri Dec 16 20​:26​:30 2005]​:

The behavior of the unary minus operator is inconsistent between
strings representing positive and negative numbers​:

perl -le 'print for
-"10",-"10.0",-"10foo",-"-10",-"-10.0",-"-10foo",-" -10",-" -10.0",-"
-10foo"'
-10
-10
-10
+10
+10.0
+10foo
10
10
10

Perl guesses whether the argument of '-' is a number.
I think that all strings in the above example represent numbers.
Note that strings starting with a minus are not treated as numbers
-- I think they should be. However, with a leading space, they _are_
treated as numbers.

It is documented in perlop.pod that unary minus opposes the sign
at the start of a string. (try not-a-number, like -"-xyz")

Unary ``-''
.... Otherwise, if the string starts with a plus or minus,
a string starting with the opposite sign is returned.

cf. PP(pp_negate)
...
else if (SvPOKp(sv)) {
...
else if (*s == '+' || *s == '-') {
sv_setsv(TARG, sv);
*SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
}

I think the case of (-'-10' eq '+10') is conformable
with the documentation (thus not a bug).
(I guess the case of -" -10.0" is not (clearly?) documented.)

No, its not. The following change fixes this. Also, much of the string
handling for arithmetic negation is under- or non-tested. The change
also adds t/op/negate.t to remedy this.

Change 26387 by stevep@​stevep-mccoy on 2005/12/17 15​:35​:45

  Addition documentation explaining aritmetic negation on strings. Also,
  additional test cases based partially on code by Piotr Fusik in
  RT #36675​: -'-10' eq '+10'.

Affected files ...

... //depot/perl/pod/perlop.pod#136 edit
... //depot/perl/t/op/negate.t#1 add

Differences ...

==== //depot/perl/pod/perlop.pod#136 (text) ====

@​@​ -199,7 +199,11 @​@​
concatenated with the identifier is returned. Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
is returned. One effect of these rules is that -bareword is equivalent
-to the string "-bareword".
+to the string "-bareword". If, however, the string begins with a
+non-alphabetic characters, Perl will attempt to convert the string to a
numeric
+and the arithmetic negation is performed. If the string cannot be cleanly
+converted to a numeric, Perl will give the warning B<Argument "the string"
+isn't numeric in negation (-) at ...>.
X<-> X<negation, arithmetic>

Unary "~" performs bitwise negation, i.e., 1's complement. For

@p5pRT
Copy link
Author

p5pRT commented Dec 17, 2005

From @smpeters

[stmpeters - Sat Dec 17 07​:37​:34 2005]​:

[BQW10602@​nifty.com - Fri Dec 16 20​:26​:30 2005]​:

The behavior of the unary minus operator is inconsistent between
strings representing positive and negative numbers​:

perl -le 'print for
-"10",-"10.0",-"10foo",-"-10",-"-10.0",-"-10foo",-" -10",-" -10.0",-"
-10foo"'
-10
-10
-10
+10
+10.0
+10foo
10
10
10

Perl guesses whether the argument of '-' is a number.
I think that all strings in the above example represent numbers.
Note that strings starting with a minus are not treated as numbers
-- I think they should be. However, with a leading space, they _are_
treated as numbers.

It is documented in perlop.pod that unary minus opposes the sign
at the start of a string. (try not-a-number, like -"-xyz")

Unary ``-''
.... Otherwise, if the string starts with a plus or minus,
a string starting with the opposite sign is returned.

cf. PP(pp_negate)
...
else if (SvPOKp(sv)) {
...
else if (*s == '+' || *s == '-') {
sv_setsv(TARG, sv);
*SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
}

I think the case of (-'-10' eq '+10') is conformable
with the documentation (thus not a bug).
(I guess the case of -" -10.0" is not (clearly?) documented.)

No, its not. The following change fixes this. Also, much of the string
handling for arithmetic negation is under- or non-tested. The change
also adds t/op/negate.t to remedy this.

Change 26387 by stevep@​stevep-mccoy on 2005/12/17 15​:35​:45

Addition documentation explaining aritmetic negation on strings\.  Also\,
additional test cases based partially on code by Piotr Fusik in 
RT \#36675&#8203;: \-'\-10' eq '\+10'\.

Affected files ...

... //depot/perl/pod/perlop.pod#136 edit
... //depot/perl/t/op/negate.t#1 add

Differences ...

==== //depot/perl/pod/perlop.pod#136 (text) ====

@​@​ -199,7 +199,11 @​@​
concatenated with the identifier is returned. Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
is returned. One effect of these rules is that -bareword is equivalent
-to the string "-bareword".
+to the string "-bareword". If, however, the string begins with a
+non-alphabetic characters, Perl will attempt to convert the string to a
numeric
+and the arithmetic negation is performed. If the string cannot be
cleanly
+converted to a numeric, Perl will give the warning B<Argument "the
string"
+isn't numeric in negation (-) at ...>.
X<-> X<negation, arithmetic>

Unary "~" performs bitwise negation, i.e., 1's complement. For

Errr... a bit clearer is

Change 26388 by stevep@​stevep-mccoy on 2005/12/17 15​:41​:38

  Fix a typo and add some clarification to change #26387.

Affected files ...

... //depot/perl/pod/perlop.pod#137 edit

Differences ...

==== //depot/perl/pod/perlop.pod#137 (text) ====

@​@​ -200,10 +200,10 @​@​
starts with a plus or minus, a string starting with the opposite sign
is returned. One effect of these rules is that -bareword is equivalent
to the string "-bareword". If, however, the string begins with a
-non-alphabetic characters, Perl will attempt to convert the string to a
numeric
-and the arithmetic negation is performed. If the string cannot be cleanly
-converted to a numeric, Perl will give the warning B<Argument "the string"
-isn't numeric in negation (-) at ...>.
+non-alphabetic character (exluding "+" or "-"), Perl will attempt to
convert
+the string to a numeric and the arithmetic negation is performed. If the
+string cannot be cleanly converted to a numeric, Perl will give the warning
+B<Argument "the string" isn't numeric in negation (-) at ...>.
X<-> X<negation, arithmetic>

Unary "~" performs bitwise negation, i.e., 1's complement. For

@p5pRT
Copy link
Author

p5pRT commented Dec 17, 2005

From @tamias

On Sat, Dec 17, 2005 at 07​:42​:49AM -0800, Steve Peters via RT wrote​:

==== //depot/perl/pod/perlop.pod#137 (text) ====

@​@​ -200,10 +200,10 @​@​
starts with a plus or minus, a string starting with the opposite sign
is returned. One effect of these rules is that -bareword is equivalent
to the string "-bareword". If, however, the string begins with a
-non-alphabetic characters, Perl will attempt to convert the string to a
numeric
-and the arithmetic negation is performed. If the string cannot be cleanly
-converted to a numeric, Perl will give the warning B<Argument "the string"
-isn't numeric in negation (-) at ...>.
+non-alphabetic character (exluding "+" or "-"), Perl will attempt to
convert
+the string to a numeric and the arithmetic negation is performed. If the
+string cannot be cleanly converted to a numeric, Perl will give the warning
+B<Argument "the string" isn't numeric in negation (-) at ...>.
X<-> X<negation, arithmetic>

Nits​: "exluding" should be "excluding"; "numeric" is an adjective, not a
noun, so it should be replaced with "number".

Ronald

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

No branches or pull requests

1 participant