LanX has asked for the wisdom of the Perl Monks concerning the following question:
OK another case where symmetry is broken
(continuing Re^3: Converting a list of numbers to use a range operator)
this works
DB<159> $a=inf
=> "inf"
DB<160> --$a
=> "inf"
DB<161> ++$a
=> "inf"
DB<162> ++$a
=> "inf"
this doesn't
DB<156> $a=inf
=> "inf"
DB<157> ++$a
=> "ing"
DB<158> ++$a
=> "inh"
any clean way to avoid this?
I suppose any internal "numeric flag" or "dual var slot" is only set after (a clear) numeric operation...
Cheers Rolf
( addicted to the Perl Programming Language)
Update
Stupid workaround proving my idea
DB<174> $a=inf+0
=> "inf"
DB<175> ++$a
=> "inf"
DB<176> ++$a
=> "inf"
IMHO still a bug!
tested in 5.10
could someone plz test in newer versions and reply?
perl -e '$a=inf; print ++$a,"\n"'
ing
Re: Incrementing "Infinity" bug
by Athanasius (Bishop) on Mar 24, 2013 at 14:56 UTC
|
0:52 >perl -E "$a = inf; print ++$a, qq[\n];"
ing
0:52 >perl -E "$a = inf + 0; print ++$a, qq[\n];"
1.#INF
0:52 >perl -Mbigint -E "$a = inf; print ++$a, qq[\n];"
inf
0:52 >perl -v
This is perl 5, version 16, subversion 0 (v5.16.0) built for MSWin32-x
+86-multi-thread-64int
(with 1 registered patch, see perl -V for more detail)
Hope that helps,
| [reply] [d/l] |
Re: Incrementing "Infinity" bug (numification, perlnumber, magic increment decrement)
by Anonymous Monk on Mar 24, 2013 at 22:30 UTC
|
This is not a bug
Perl has magic increment operator, it works on strings
Perl not have magic decrement operator, so string is turned into a number
inf looks like a string so it is a string, not a number, use int to force numification
$ perl -MData::Dump -e " $f = inf; dd $f; $f--; dd $f; "
"inf"
1.#INF
$ perl -MData::Dump -e " dd int inf "
1.#INF
$ perl -MData::Dump -e " dd int Infinity "
1.#INF
perlnumber, perldata, 2010 :) Infinity and Inf? | [reply] [d/l] |
|
DB<100> use constant inf => inf+0
=> 0
DB<101> $a=inf
=> "inf"
DB<102> ++$a
=> "inf"
DB<103> ++$a
=> "inf"
So why doesn't Perl do this per default?
do we really need to use hacks like 1e9999 to create inf?
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] [d/l] [select] |
|
It's a bug or at least a design flow.
How do you figure? I think it isn't
So why doesn't Perl do this per default?
You know why, tradition (backwards compatibility ). "barewords" have always been strings
Also style, lowercase constants are unbecoming :p
do we really need to use hacks like 1e9999 to create inf?
We never needed that, use 0+'inf' or int '-inf' ...
But hey, one of the math modules could export ... or even use feature 'infinity'; could happen though I don't see the benefit, but what do I know
$ perl -le " for(1..4){ print for \int 'infinity', \int'-infinity' }
+"
SCALAR(0x99bc74)
SCALAR(0x99bc64)
SCALAR(0x99bc74)
SCALAR(0x99bc64)
SCALAR(0x99bc74)
SCALAR(0x99bc64)
SCALAR(0x99bc74)
SCALAR(0x99bc64)
update: nans, infs, and vomit, How to create nan/inf says bigint/bigrat export inf that stringifies to inf
Also note Re^3: nans, infs, and vomit (underlying c-runtime and strtod, atof) | [reply] [d/l] [select] |
|
|
|
Re: Incrementing "Infinity" bug
by kcott (Bishop) on Mar 25, 2013 at 06:28 UTC
|
G'day Rolf,
"tested in 5.10
could someone plz test in newer versions and reply?"
Same result in 5.14.2:
$ perl -e '$a=inf; print ++$a,"\n"'
ing
$ perl -v
This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-th
+read-multi-2level
As you were probably aware, warnings alert you and strictures disallow it altogether:
$ perl -Mwarnings -e '$a=inf; print ++$a,"\n"'
Unquoted string "inf" may clash with future reserved word at -e line 1
+.
ing
$ perl -Mstrict -e '$a=inf; print ++$a,"\n"'
Bareword "inf" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
| [reply] [d/l] [select] |
|
Hi Ken!
> As you were probably aware, warnings alert you and strictures disallow it altogether:
Yes, I became aware after posting the first time.
I expected inf and it's brothers² to be constants because of their magic behavior when being numified.
Introducing constants¹ for inf, -inf and NaN (even all uppercase like INF) would help clarifying the situation.
Cheers Rolf
( addicted to the Perl Programming Language)
¹) like I demonstrated here
²) every string starting with inf (no matter if upper or lower case) numifies to inf:
DB<106> $a="Infinity"+0
=> "inf"
DB<109> $a="Inftyddssssd"+0
=> "inf"
DB<110> $a="Inf"+0
=> "inf"
DB<111> $a="InF"+0
=> "inf"
UPDATE: line 109 with"Inftyddssssd" fails with activated warnings, anything else with lc($else) ~~ ['inf','infinity'] won't
DB<123> use warnings; $a='InFiNiTy'+0
=> "inf"
DB<125> use warnings;$a="Inftyddssssd"+0
Argument "Inftyddssssd" isn't numeric in addition (+) at (eval 74)[mul
+ti_perl5db.pl:644] line 2.
| [reply] [d/l] [select] |
|
I came across Inf (and friends) some years ago, decided they all looked somewhat flakey, and haven't used them since. Your posting has (somewhat) piqued my interest again. Here's some interesting results from Scalar::Util's looks_like_number() function.
$ perl -E '
use Scalar::Util qw{looks_like_number};
my @infs = (inf, Inf, INF, infinity, Infinity, INFINITY,
-inf, -Inf, -INF, -infinity, -Infinity, -INFINITY,
Inftyddssssd, infighting, Infighting,
-Inftyddssssd, -infighting, -Infighting);
say "$_ : ", looks_like_number($_) for @infs;
'
inf : 20
Inf : 20
INF : 20
infinity : 20
Infinity : 20
INFINITY : 20
-inf : 28
-inf : 28
-inf : 28
-inf : 28
-inf : 28
-inf : 28
Inftyddssssd : 0
infighting : 0
Infighting : 0
-Inftyddssssd : 0
-infighting : 0
-Infighting : 0
I also noted that the documentation for this function links to perlapi - looks_like_number, which may be of interest ot you.
| [reply] [d/l] [select] |
|
|
|