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-20 14:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Joint Participation] [Challenge Ideas] [Batch Processing Floating-Point Operations] DigestI View 41,152 Replies 136
Floor 31 Posted 2006-10-06 00:38 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
The code on the 15th floor has been updated to integer/floating-point segmented calculation
Everyone is welcome to test...
Floor 32 Posted 2006-10-06 01:11 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
The code on the 10th floor is wrong!
Please enter operand one:132165.134646454
Please enter operand two:132654.6546546
-2
132165.134646454+132654.6546546=-2?
Floor 33 Posted 2006-10-06 03:38 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
It was already stated on the 10th floor: the decimal part can calculate up to 8 digits. But your decimal part has 9 digits, so it overflowed...

Found several defects in the code on the 15th floor; calculation error, adding 1 too many. The zero problem has not been fully considered: (111+111=223), (0.23+1.08=2.31)...

Moderator still needs to improve the code. Specifically, both the code on the 10th floor and the code on the 4th floor solved it by adding 1 to the frontmost digit (this is also why the decimal parts in the code on the 10th and 4th floors can only calculate 8 digits).


Notes about the code on the 10th floor (integer/floating-point merged calculation):

The integer part can calculate up to 8 digits, and the decimal part can calculate up to 8 digits.
The decimal part and integer part together can calculate up to 9 digits
Otherwise it will overflow or be an invalid number...


[ Last edited by pengfei on 2006-10-6 at 04:05 ]
Floor 34 Posted 2006-10-06 03:41 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Also, after improvement the code on the 4th floor is already general-purpose, and is even more advantageous than the code on the 10th floor.

Specifically, its calculation range is larger; decimal plus integer can calculate up to 17 digits. The code is more concise and has better fault tolerance!

Just written, so there may still be some problems not yet considered. Looking forward to everyone doing comprehensive testing...
Floor 35 Posted 2006-10-06 07:43 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
I just checked it; it turns out I typed set/a n1+=1 as set n1+=1, so an error occurred.
The 15th floor has already been updated! The decimal part can have up to 9 digits, the integer part can calculate up to 9 digits, and it supports input such as A 136.987 236, as well as entering A and then entering according to the prompts!
Everyone is welcome to test...

[ Last edited by qwe1234567 on 2006-10-7 at 03:45 ]
Floor 36 Posted 2006-10-06 07:50 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
There is an error in the code above on the 4th floor. If there is 0 before the integer, it errors; the later decimal digits are not aligned!
a 008.64 36.6
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
.70
请按任意键继续 . . .
a 8.64 36.6
44.70

[ Last edited by qwe1234567 on 2006-10-6 at 07:54 ]
Floor 37 Posted 2006-10-06 09:52 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Moderator, this kind of a 008.64 should belong to the category of erroneous input, right? I had not considered this. Let’s still test the various situations that may occur in floating-point calculations! I did not find the error tested on the 36th floor...

The moderator said that if there is 0 in front it errors; I’m afraid not, right? The digit-by-digit loop zero-removal method can remove all zeros at the highest digit.

As for later decimal digits not being aligned, that is a formatted output problem and also was not considered; the code is only for testing whether the calculation result is right or wrong. If one insists on formatted output, adding corresponding code can also be considered.

Everyone is welcome to test the calculation defects of the code on the 4th and 10th floors...

[ Last edited by pengfei on 2006-10-6 at 10:16 ]
Floor 38 Posted 2006-10-06 19:29 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
I mean I wrote the code above on the 4th floor (the file whose input is %1 and %2) into the A.BAT file, then entered in CMD:
a 008.64 36.6
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
.70
请按任意键继续 . . .
a 8.64 36.6
44.70
Floor 39 Posted 2006-10-06 19:42 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Hehe~ That section of my code is only limited to calculating inside the batch file and cannot accept input from variables %1, %2. The moderator should know as soon as he looks at it.

Congratulations, your code has already added the digit-by-digit loop zero-removal method and the code for adding 1 before the decimal part for calculation, solving this series of problems. It also adds code for use under CMD ^_^

Currently the code on the 4th floor, 10th floor, and moderator qwe1234567’s code all test normally...
Floor 40 Posted 2006-10-06 23:34 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Then where do the %1 and %2 in the 4th floor’s code accept variables from? (The code below is the one above on your 4th floor, please look!)
@echo off
setlocal enabledelayedexpansion
for %%a in (%1,%2) do (
set /a num+=1
for /f "tokens=1,2 delims=." %%i in ("%%a") do (
if "!num!"=="1" (set one=%%i) else (set one_=%%i)
if "!num!"=="1" (set two=%%j) else (set two_=%%j)
)
)
set /a integer=%one%+%one_%
set /a decimal=%two%+%two_%
if %decimal:~0,1% lss %two:~0,1% (
set /a integer=%integer%+1
set /a decimal=%decimal:~1%
) else (
if %decimal:~0,1% lss %two_:~0,1% (
set /a integer=%integer%+1
set /a decimal=%decimal:~1%
)
)
echo %integer%.%decimal%
pause

Where do the %1 and %2 in it accept variables from?
Floor 41 Posted 2006-10-06 23:35 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
This section of script is the original one; many problems were not solved. Isn’t there another section below on the 4th floor?
Floor 42 Posted 2006-10-07 03:08 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 43 Posted 2006-10-08 00:13 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
New-idea calculation method:
Before calculation, leading zeros in the decimal part are automatically removed, trailing zeros are automatically removed, and leading zeros in the integer part are automatically removed. The largest result is: 2147483647.2147483647 (the largest result for the decimal part is .2147483647, and the largest result for the integer part is .2147483647)
If the decimal parts of the addends both have N or more leading 0s, the decimal part digit count can be more than N+9 digits!
Everyone is welcome to test!
321.00000000065496546+13.000000000046541346=334.000000000701506806
321.1105496546+13.1041541346=334.2147037892
@echo off
if not %1*==* (set number=%1&set number_=%2&goto bj)
set /p number=Please enter operand one:
set /p number_=Please enter operand two:
:bj
echo %number% | find "." >nul || set number=%number%.0
echo %number_% | find "." >nul || set number_=%number_%.0
if %number:~0,1%==. set number=0%number%
if %number_:~0,1%==. set number_=0%number_%
if "%number:~-1%"=="." set number=%number%0
if "%number_:~-1%"=="." set number_=%number_%0
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=." %%a in ("%number%.%number_%") do (
set one=%%a
set two=%%b
set one_=%%c
set two_=%%d
)
call :go %two%
set two=%ls%
set num1=%wish%
call :go %two_%
set two_=%ls%
set num2=%wish%
if %num1% lss %num2% (
set note=%num2%
set /a result=%num2%-%num1%
call :fine !result! %two%
set two=!incept!
) else (
set note=%num1%
set /a result=%num1%-%num2%
call :fine !result! %two_%
set two_=!incept!
)
:1
if "!one:~0,1!"=="0" set one=%one:~1%&&goto 1
:11
if "!two:~0,1!"=="0" set two=%two:~1%&&goto 11
if %two%*==* set two=0
:2
if "!one_:~0,1!"=="0" set one_=!one_:~1!&&goto 2
:22
if "!two_:~0,1!"=="0" set two_=!two_:~1!&&goto 22
if %two_%*==* set two_=0
set/a num2=%two%+%two_%
set/a n1=0!num2:~0,-%note%!
if %num2%==0 set num2=&&goto go3
set num2=!num2:~-%note%!
set ls=%num2%
:go1
if not %ls%*==* (
set ls=!ls:~1!
set/a go+=1
goto go1
)
:go2
if not %go%*==%note%* (
set/a go+=1
set num2=0%num2%
goto go2
)
:go3
set /a num1=%one%+%one_%+%n1%
:3
if !num2:~-1!==0 set num2=!num2:~0,-1!&&goto 3
if %num2%*==* (echo %number%+%number_%=%num1%) ELSE echo %number%+%number_%=%num1%.%num2%
PAUSE
goto :eof

:go
set remove=%1
set wish=0
:q0
if %remove:~-1%*==0* (
set remove=!remove:~0,-1!
goto q0
)
set ls=%remove%
:go_
if not %remove%*==* (
set /a wish+=1
set remove=!remove:~1!
goto go_
)
goto :eof
:fine
set take=%1
set incept=%2
:fine_
if not "%take%"=="0" (
set/a take-=1
set incept=!incept!0
goto fine_
)
goto :eof


[ Last edited by qwe1234567 on 2006-10-8 at 05:20 ]
Floor 44 Posted 2006-10-08 04:47 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
After looking at the moderator’s code, there are two improvements:
1. When reading the integer and decimal parts of the two operands, you put them together to extract them. Nice idea ^_^
2. If the first digit of the decimal part is zero, remove the zero for calculation. (Here leading zeros in the decimal part do not affect calculation; this is the same as integer calculation.)

Although this section of code has not found an error in decimal calculation for the time being, it cannot calculate integers (the reason lies in removing the leading zero of the decimal part; if there is only one zero, then there is no operand. Even the several later lines of code will error). This problem can be solved.

Previously I felt that adding 1 before all decimals was simple and convenient, but it could only calculate 8 digits. Hehe~ I still feel that a larger calculation range is more important; the code is being updated...
Floor 45 Posted 2006-10-08 05:22 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
The problem of removing the leading zero in the decimal part, where if there is only one zero there is no operand, and even the several later lines of code will error, has already been solved. Updated on the 43rd floor.
Everyone is welcome to test!
Forum Jump: