China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-29 08:16
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » About the problem of number bases during numerical operations! View 3,004 Replies 11
Original Poster Posted 2006-09-05 02:07 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
When using SET /A for operations, if the first digit of the character is zero and the value contains 8 or 9, it will cause the value to not be read correctly.

Because except that hexadecimal has the 0x prefix and octal has the 0 prefix, the numeric value is in decimal. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.

Example:
C:\>set /a minute=08+5
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=5+018
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=0029+8
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=125+09
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).

Solutions:

1. When operating constants, if the constant contains 8 or 9, do not add zero in front, in short, the highest digit cannot be zero.

Example: Incorrect (08, 009, 018, 0029)

Correct (8, 9, 18, 29)

2. Prevent the highest digit of the variable from being zero during variable operation;

Algorithm:
First take the first digit of the variable and judge whether it is zero;

If it is true, then take the remainder after the first digit and reassign it to the variable;

Execute the loop until the first digit is not zero;

Finally, use the zero-removed variable for operation.

Code:
@echo off
set mu=0001028
echo Before removing zero=%mu%
:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)
echo After removing zero=%mu%
set /a num=%mu%+154
echo Operation result=%num%
pause

I encountered this problem when writing a batch script today, and finally figured out the reason.

Also found the solution, and in the future, everyone can avoid detours when encountering corresponding problems.

[ Last edited by pengfei on 2006-10-2 at 09:41 ]
Floor 2 Posted 2006-09-05 03:41 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Well, I didn't notice this number system
Floor 3 Posted 2006-09-05 05:17 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, there was a vague mention of handling when the first digit is 0 in the old posts of the forum, but I can't find that post right now.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-09-29 11:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Learning...
You can also use this algorithm to judge the number of spaces in a string.
Hehe`
Floor 5 Posted 2006-09-29 13:08 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Bump
Floor 6 Posted 2006-09-29 22:55 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Just came here again, haha...

:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)

What you wrote here with :again and goto again is really cool!
It continues until the leading 0s are removed until the IF condition naturally fails and jumps out to continue executing the new content :)
Admired~~~
Floor 7 Posted 2006-09-30 00:34 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Inspired by the wonderful application of " :again ... goto again " from the building - owner pengfei, I tried to make a simulation experiment of recursive factorial calculation of N by learning the principle of goto of the building - owner. It is really convenient to use goto well~:)
Thank you again for the building - owner's masterpiece "Regarding the base problem in numerical operations"~:)

Calculate the factorial of N~:)
There is no boundary crossing and no illegal check, just a work for learning the application of goto of the building - owner~:)



@echo off
setlocal ENABLEDELAYEDEXPANSION

set /p n=Please enter N! :
set /a redtek=!n!
if !n!==0 (echo The factorial of 0 is 1 && goto :Eof )

:Start

set /a n-=1
if !n!==0 goto :Ok
set /a redtek*=n
goto :Start

:Ok
echo The factorial is:!redtek!
echo The maximum can only calculate the factorial of 31, and overflow if larger~:)

Floor 8 Posted 2006-09-30 00:44 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
:again
if "%mu:~0,1%"=="0" set mu=%mu:~1%&goto again

if 写成一行也OK
Floor 9 Posted 2006-10-02 09:21 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Well, this is considered the most streamlined method. Give it a thumbs up first.
Floor 10 Posted 2006-10-02 10:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The algorithm I thought of when dealing with this problem at that time was to first extract the first digit and judge whether its value is zero. This is the first step. Then how to write the code? Getting the value and judging zero are easy. Take the character in the set to get the digit, and a single IF judgment can solve it.

The second step, if there are multiple zeros, a loop and re - assignment are needed. When I thought about it, it seemed that in batch processing, the only way to implement such a loop was goto (and then I thought again whether goto would keep looping, but then I saw that the previous IF was not only a judgment of zero but also played a role in conditional judgment). Here, one IF and one goto form a complete algorithm. The code was written quickly...

[ Last edited by pengfei on 2006 - 12 - 7 at 11:58 AM ]
Floor 11 Posted 2006-10-02 12:16 ·  中国 北京 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Here's the translation:
Here's a recursive one:

@echo off
set x=00010280
call :sub %x%
echo %y%
goto :EOF
:sub
set y=%1
if "%y:~0,1%"=="0" call :sub %y:~1%
goto :EOF
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 12 Posted 2006-10-02 19:58 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
If a number starts with 0, the system considers it as octal, and since there are no 8 or 9 in octal, so an error occurs!
Forum Jump: