Discussion:
leading zero
(too old to reply)
Tom Del Rosso
2021-03-07 03:19:37 UTC
Permalink
This is curious.

set x=09
set /a y=x

results in the answer zero, but it should give the invalid number error.

set /a y=%x%
does give the expected error.

BTW I like to use
set /a y=(1%x%-100) ...*/-+...
as a compact way to remove the zero.
--
Defund the Thought Police
JJ
2021-03-07 12:43:51 UTC
Permalink
Post by Tom Del Rosso
This is curious.
set x=09
set /a y=x
results in the answer zero, but it should give the invalid number error.
set /a y=%x%
does give the expected error.
BTW I like to use
set /a y=(1%x%-100) ...*/-+...
as a compact way to remove the zero.
Numbers with leading zero(es) are treated as Octal numbers. So, "8" and "9"
number are invalid Octal numbers.

When SET /A is used with variables only (i.e. no constant), it doesn't
display any error message when it encounters an error. Instead, it simply
set the ERRORLEVEL, then sets the result of the calculation to zero. i.e. a
silent error.

e.g. this causes an error.

set/a 08

This causes a silent error.

dir>nul
echo %errorlevel%
rem above should output: 0

set x=07
set/a x
rem above should output: 7
echo %errorlevel%
rem above should output: 0

set x=08
set/a x
rem above should output: 0
echo %errorlevel%
rem above should output non zero
Tom Del Rosso
2021-03-07 14:28:16 UTC
Permalink
Post by JJ
Numbers with leading zero(es) are treated as Octal numbers. So, "8"
and "9" number are invalid Octal numbers.
When SET /A is used with variables only (i.e. no constant), it doesn't
display any error message when it encounters an error. Instead, it
simply set the ERRORLEVEL, then sets the result of the calculation to
zero. i.e. a silent error.
e.g. this causes an error.
set/a 08
[snip for grc quote rule]

But this doesn't explain (it seems to me) why the variable without
percents has a different result. SET/A is supposed to behave the same
with or without the percents.
--
Defund the Thought Police
JJ
2021-03-08 02:21:03 UTC
Permalink
Post by Tom Del Rosso
But this doesn't explain (it seems to me) why the variable without
percents has a different result. SET/A is supposed to behave the same
with or without the percents.
With below command...

set /a y=%x%

The reason why it causes an error is because CMD performs syntax check then
expands any expandable variable references (which uses the percent sign),
THEN it executes the command.

So, if x variable contain 09, the command line would be like below before
it's executed.

set /a y=09

Meaning, the given value is a constant, rather than a variable name, from
the perspective of the SET /A command.
Tom Del Rosso
2021-03-08 19:54:43 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
But this doesn't explain (it seems to me) why the variable without
percents has a different result. SET/A is supposed to behave the same
with or without the percents.
With below command...
set /a y=%x%
The reason why it causes an error is because CMD performs syntax
check then expands any expandable variable references (which uses the
percent sign), THEN it executes the command.
So, if x variable contain 09, the command line would be like below
before it's executed.
set /a y=09
Meaning, the given value is a constant, rather than a variable name,
from the perspective of the SET /A command.
I know that, but it should do the same with

set /a y=x

As it says in the set help, "Any non-numeric strings in the expression
are treated as environment variable names whose values are converted to
numbers before using them."

So the conversion is done incorrectly, producing zero as if the variable
was not denied at all.
--
Defund the Thought Police
Tom Del Rosso
2021-03-08 19:57:06 UTC
Permalink
Post by Tom Del Rosso
So the conversion is done incorrectly, producing zero as if the
variable was not denied at all.
not defined at all

I don't know how that happened.


--
JJ
2021-03-09 06:22:18 UTC
Permalink
Post by Tom Del Rosso
I know that, but it should do the same with
set /a y=x
As it says in the set help, "Any non-numeric strings in the expression
are treated as environment variable names whose values are converted to
numbers before using them."
So the conversion is done incorrectly, producing zero as if the variable
was not denied at all.
You're right. More like it's the error handling which is not done
incorrectly.

It's probably too late to fix the bug, as administrators/programmers have
accustomed to this bug. Cause fixing the bug would cause the zero value not
be assigned to the variable by SET/A when there's an error. That would
affect existing batch files' program logic.
Herbert Kleebauer
2021-03-09 14:48:52 UTC
Permalink
Post by Tom Del Rosso
This is curious.
set x=09
set /a y=x
results in the answer zero, but it should give the invalid number error.
Seems the "set /a" routine stops parsing a variable when the next character
would make the number incorrect and uses the value till this character.

set x=42a
set /a n=1+x+10
echo %n%

results in 53
Post by Tom Del Rosso
set /a y=%x%
does give the expected error.
Because here the "set /a" routine sees "set /a y=09"
and therefore doesn't parse a variable like above.
mokomoji
2021-05-14 15:37:47 UTC
Permalink
Post by Tom Del Rosso
This is curious.
set x=09
set /a y=x
results in the answer zero, but it should give the invalid number error.
set /a y=%x%
does give the expected error.
BTW I like to use
set /a y=(1%x%-100) ...*/-+...
as a compact way to remove the zero.
--
Defund the Thought Police
Word variable
Numeric variable

word
set x="09"
num
set x=9
num 8
set x=09
num16
set x=0x9

and none type
set "x=09"


Change from simple "word" to "decimal numeric"

set "x=09"
set "y=1%x%"
set /a "y=%y%-100"
mokomoji
2021-05-14 15:48:18 UTC
Permalink
Post by Tom Del Rosso
This is curious.
set x=09
set /a y=x
results in the answer zero, but it should give the invalid number error.
set /a y=%x%
does give the expected error.
BTW I like to use
set /a y=(1%x%-100) ...*/-+...
as a compact way to remove the zero.
--
Defund the Thought Police
etc. Zero String strip.
Reference document - korean language
https://blog.naver.com/mokomoji/222120528528

Loading...