Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

leading zero

36 views
Skip to first unread message

Tom Del Rosso

unread,
Mar 6, 2021, 10:19:45 PM3/6/21
to
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

unread,
Mar 7, 2021, 7:44:07 AM3/7/21
to
On Sat, 6 Mar 2021 22:19:37 -0500, Tom Del Rosso wrote:
> 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

unread,
Mar 7, 2021, 9:28:18 AM3/7/21
to
JJ wrote:
>
> 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.

JJ

unread,
Mar 7, 2021, 9:21:20 PM3/7/21
to
On Sun, 7 Mar 2021 09:28:16 -0500, Tom Del Rosso wrote:
>
> 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

unread,
Mar 8, 2021, 2:54:45 PM3/8/21
to
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.

Tom Del Rosso

unread,
Mar 8, 2021, 2:57:11 PM3/8/21
to
Tom Del Rosso wrote:
>
> 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

unread,
Mar 9, 2021, 1:22:35 AM3/9/21
to
On Mon, 8 Mar 2021 14:54:43 -0500, Tom Del Rosso wrote:
>
> 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

unread,
Mar 9, 2021, 9:48:54 AM3/9/21
to
On 07.03.2021 04:19, Tom Del Rosso wrote:
> 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


> 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

unread,
May 14, 2021, 11:37:48 AM5/14/21
to
2021년 3월 7일 일요일 오후 12시 19분 45초 UTC+9에 Tom Del Rosso님이 작성한 내용:
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

unread,
May 14, 2021, 11:48:19 AM5/14/21
to
2021년 3월 7일 일요일 오후 12시 19분 45초 UTC+9에 Tom Del Rosso님이 작성한 내용:
etc. Zero String strip.
Reference document - korean language
https://blog.naver.com/mokomoji/222120528528

0 new messages