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 13:06
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Joint Participation] [Challenge Ideas] [Batch Processing Floating-Point Operations] DigestI View 41,140 Replies 136
Floor 121 Posted 2006-10-23 03:55 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Hehe, it's not explanations, it's just that Chinese labels were used. With Chinese labels, even explanations can be omitted!
Floor 122 Posted 2006-10-23 03:57 ·  中国 甘肃 兰州 城关区 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by qwe1234567 at 2006-10-23 03:55:
Hehe, it's not explanations, it's just that Chinese labels were used. With Chinese labels, even explanations can be omitted!

Chinese works too, huh. Haven't tried it yet, not sure how many Chinese characters are supported at most
Floor 123 Posted 2006-10-23 04:18 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 124 Posted 2006-10-23 04:19 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 125 Posted 2006-10-23 04:31 ·  中国 甘肃 兰州 城关区 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by qwe1234567 at 2006-10-23 04:18:
This is also my first time using Chinese labels. I think there probably isn't a limit on the number

Letters are 8, so I think there should be one. Test it and see
Floor 126 Posted 2006-10-23 04:35 ·  中国 甘肃 兰州 城关区 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Turns out there really is no limit, hehe~ I'll use Chinese for labels from now on
Floor 127 Posted 2006-10-23 04: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 tried it. In CMD, letters aren't limited to just 8 either (I guess there isn't much of a limit, or maybe it's 255). You can test it with the following code
@echo off
:abcdefghi
set /p a=Please enter:
if %a%.==5. (goto abcdefghi) else goto abcdefghi1
:abcdefghi1
echo %a%
pause
Floor 128 Posted 2007-03-04 23:15 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Originally posted by namejm at 2006-10-4 21:08:
 So far, everyone has already found two methods for calculating floating-point numbers under CMD:

  Method 1: Calculate the integer part and the fractional part separately. The process is: the integer parts can just be added directly; the fractional part is a bit more troublesome: if the numbers of digits after the decimal point are not the same, then use the one with the most digits as the standard, pad zeros onto the fractional part of the other number, and then perform integer addition on the fractional parts; if the number of digits in the sum exceeds the longest original fractional length, then carry 1 to the integer part, and the highest digit of the fractional part must be reduced by 1 accordingly;

  Method 2: Recombine the integer part and fractional part into a new number, then perform addition on the new number, and finally insert the decimal point back into its original position;

  The respective advantages and disadvantages of the two methods:

  Method 1: Advantage: the range of floating-point numbers it can handle is very large; as long as the result of the addition is between (2^32-1.2^32-1) and -(2^32-1.2^32-1), it can handle it; disadvantage: most problems appear in the fractional part, for example, if the highest digit of the fractional part is 08 or 09, errors may occur, and these two cases need separate handling; if the digit length of the fractional sum exceeds the longest original length, then the integer part must be incremented by 1 and the highest fractional digit must be decremented by 1; if this is extended to addition of three or more floating-point numbers, the carry problem becomes even more complicated;

  Method 2: Advantage: there is no need to separately handle the 08 and 09 cases, and no need to consider carry problems (after reading pengfei's analysis below, I found that this description is a bit problematic; for details please refer to the explanation below); disadvantage: the range of floating-point numbers it can handle is relatively small; it only succeeds when the range of the new value formed by removing the decimal point from the sum is between (2^32-1.2^32-1) and -(2^32-1.2^32-1).


Excellent!!!!

Most decimals used in daily life are "amounts of money", with only 2 decimal places, so "Method 2" is better!

This thread gave me a lot of inspiration, thanks everyone!!!



[ Last edited by xycoordinate on 2007-3-4 at 10:51 AM ]
Floor 129 Posted 2007-03-10 08:17 ·  中国 湖北 黄冈 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
If this could be changed into a command-line version (when using it, enter the filename and the corresponding parameters), that would be great.
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 130 Posted 2007-03-10 09:02 ·  中国 湖北 潜江 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
Labels can also use Chinese, learned something!
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 131 Posted 2009-04-05 12:29 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@echo off
set aa=%1
set bb=%2

::Call floating-point multiplication function
call :mul cc aa bb %3

echo %cc%
goto :eof

:mul
::Floating-point multiplication function, leading and trailing zeros are allowed, supports products of: 49 integer digits, 49 fractional digits.
::The first parameter is the receiving variable name, the second and third are the names of the multiplicand variables, the fourth specifies how many decimal places to keep
@echo off&setlocal enabledelayedexpansion
set cc=0
set ka=!%2:*.=!
set kb=!%3:*.=!

set maa=!%2:.=!
set mb=!%3:.=!

for /l %%a in (0,1,49) do if not "!maa:~%%a,1!"=="0" (set maa=!maa:~%%a!&goto :maaok)
:maaok

for /l %%a in (0,1,49) do if not "!mb:~%%a,1!"=="0" (set mb=!mb:~%%a!&goto :mbok)
:mbok

if "!kb!."=="!%3!." set kb=
if "!ka!."=="!%2!." set ka=
set kab=!kb!!ka!#
for /l %%a in (0,1,49) do if "!kab:~%%a,1!"=="#" (set kab=%%a&goto :lp1)

:lp1
if defined mb (set al=!mb:~-4!
if "!al:~0,1!"=="0" set/a al=1!al!-10000
set mb=!mb:~0,-4!
) else (goto :endmul)
set ma=!maa!
set zero=!mz!
set mz=!mz!0000

for /l %%a in (1,1,12) do (
if defined ma (set bl=!ma:~-4!
if "!bl:~0,1!"=="0" set/a bl=1!bl!-10000
set/a bl*=al&set ma=!ma:~0,-4!
) else (goto :end1)
set bl=!bl!!zero!
set zero=!zero!0000
call :add cc cc bl
)

:end1
goto :lp1

:endmul
if not %kab%==0 (
set ka=00000000000000000000000000000000000000000000000000%cc%
if "!cc:~0,-%kab%!"=="" (set cc=0.!ka:~-%kab%,%4!) else (set cc=!cc:~0,-%kab%!.!cc:~-%kab%,%4!)
)
endlocal&set %1=%cc%&goto :eof

:add
set xa=%cc%
set xb=%bl%
set cc=
set zz=0
set zy=
for /l %%a in (9,9,81) do (
if "!xa!!xb!"=="" goto :end
set cc=!zy!!cc!
if defined xa (set "aa=000000000!xa:~-9!"&set xa=!xa:~0,-9!) else (set aa=000000000)
if defined xb (set "bb=000000000!xb:~-9!"&set xb=!xb:~0,-9!) else (set bb=000000000)
set/a zz=!zz:~0,-9!+1!bb:~-9!-1000000000+1!aa:~-9!-1000000000
set zy=000000000!zz!
set zy=!zy:~-9!
)
:end
set cc=%zz%%cc%
if not %cc% gtr 0 goto :endok
if "%cc:~0,2%"=="0." goto :endok
for /l %%a in (0,1,36) do if not "!cc:~%%a,1!"=="0" (set cc=!cc:~%%a!&goto :endok)
:endok
goto :eof


[ Last edited by netbenton on 2009-4-5 at 10:30 ]
精简
=> 个人网志
Floor 132 Posted 2009-04-05 14:46 ·  中国 湖北 荆州 洪湖市 电信
初级用户
★★
Credits 161
Posts 114
Joined 2009-02-20 13:00
17-year member
UID 139375
Gender Male
Status Offline
The one on the 4th floor is like a heavenly book...
But why go so deep into floating-point operations? I haven't seen many batch files that actually need floating-point calculations. You experts really are too bored.
Floor 133 Posted 2009-04-07 09:01 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@echo off
set aa=%1
set bb=%2

::Call the floating-point multiplication function
echo %time%
call :mul cc aa bb %3
echo %time%

echo %cc%
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:mul <接收变量名> <被乘数> <乘数>
::
::Floating-point multiplication function; leading and trailing 0s are both allowed, supports a product of: 49 integer digits and 49 decimal digits.
::The first parameter is the receiving variable name, the second and third are the variable names to be multiplied, the fourth specifies how many decimal places to keep
::Calculating the maximum number of digits takes only 12 milliseconds
::Don't let the code length fool you, the speed is definitely not slow
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal enabledelayedexpansion
set cc=0
set ka=!%2:*.=!
set kb=!%3:*.=!

set maa=!%2:.=!
set mb=!%3:.=!

for /l %%a in (0,1,49) do if not "!maa:~%%a,1!"=="0" (set maa=!maa:~%%a!&goto :maaok)
:maaok

for /l %%a in (0,1,49) do if not "!mb:~%%a,1!"=="0" (set mb=!mb:~%%a!&goto :mbok)
:mbok

if "!kb!."=="!%3!." set kb=
if "!ka!."=="!%2!." set ka=
set kab=!kb!!ka!#
for /l %%a in (0,1,49) do if "!kab:~%%a,1!"=="#" (set kab=%%a&goto :lp1)
set mz=
:lp1
if defined mb (set al=!mb:~-4!
if "!al:~0,1!"=="0" set/a al=1!al!-10000
set mb=!mb:~0,-4!
) else (goto :endmul)
set ma=!maa!
set "cl= "
for /l %%a in (1,4,91) do (
if not defined ma (goto :end1)
set bl=!ma:~-4!
if "!bl:~0,1!"=="0" set/a bl=1!bl!-10000
set/a bl=!cl:~0,-%%a!+bl*al
set ma=!ma:~0,-4!
set cl=0000!cl:~-%%a!
set cl=!bl!!cl:~-%%a!
)
:end1
set cl=%cl:~0,-1%%mz%
set mz=!mz!0000

set xa=%cc%
set xb=%cl%
set cc=
set zz=0
set zy=
for /l %%a in (9,9,81) do (
if "!xa!!xb!"=="" goto :end2
set cc=!zy!!cc!
if defined xa (set "aa=000000000!xa:~-9!"&set xa=!xa:~0,-9!) else (set aa=000000000)
if defined xb (set "bb=000000000!xb:~-9!"&set xb=!xb:~0,-9!) else (set bb=000000000)
set/a zz=!zz:~0,-9!+1!bb:~-9!-1000000000+1!aa:~-9!-1000000000
set zy=000000000!zz!
set zy=!zy:~-9!
)
:end2
set cc=%zz%%cc%
if not %cc% gtr 0 goto :lp1
for /l %%a in (0,1,36) do if not "!cc:~%%a,1!"=="0" (set cc=!cc:~%%a!&goto :lp1)
goto :lp1

:endmul
if not %kab%==0 (
set ka=00000000000000000000000000000000000000000000000000%cc%
if "!cc:~0,-%kab%!"=="" (set cc=0.!ka:~-%kab%,%4!) else (set cc=!cc:~0,-%kab%!.!cc:~-%kab%,%4!)
)
endlocal&set %1=%cc%&goto :eof
精简
=> 个人网志
Floor 134 Posted 2009-04-09 00:12 ·  中国 上海 松江区 电信
初级用户
Credits 20
Posts 24
Joined 2009-03-18 05:33
17-year member
UID 141631
Gender Male
Status Offline
What does this line mean??
if "%number1:~0,1%"=="." set number1=0%number1%
Floor 135 Posted 2009-05-23 17:47 ·  中国 广东 广州 电信
新手上路
Credits 5
Posts 5
Joined 2009-05-09 20:05
17-year member
UID 144737
Gender Male
Status Offline
What does this line mean??
if "%number1:~0,1%"=="." set number1=0%number1%
Forum Jump: