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-07-14 23:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Solutions to Extra-large Number Operations View 2,243 Replies 13
Original Poster Posted 2006-11-19 02:20 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Everyone knows that in batch processing, it is impossible to perform operations on overly large numbers. So how can we break through this limitation? We hope everyone will post and discuss the solutions...

I here provide one of my ideas, which I call the "column vertical form method" because it is very similar to the "column vertical form" in primary school. That is, respectively multiply each digit in the following number by the data above, and then add up all the data to get the answer. This method is based on this...

Test code:

@echo off
setlocal enabledelayedexpansion
echo Calculating...
echo.
set str1=123456789101112131415161718192021222324252627282930
set str2=123456789101112131415161718192021222324252627282930
echo %str1%
echo ×
echo %str2%
call :test %str1% "" ex
call :test %str2% _ ex_

for /l %%a in (0 1 %ex%) do (
for /l %%b in (0 1 %ex_%) do (
set /a mult=!num%%a!*!num%%b_!
set /a sum=%%a+%%b+1
rem Add up all data with the same number of digits: for example, add the digits in the tens place and the digits in the tens place;
set /a result!sum!+=!mult!
))

for /l %%a in (10000 -1 1) do (
if defined result%%a (
rem Define the variable for the previous digit
set /a last=%%a-1
set var=!result%%a!
rem If the number is greater than 10, carry over;
if !result%%a! GEQ 10 set /a result!last!+=!var:~0,-1!
set result=!var:~-1!!result!
)
)
echo.
echo =%result%

pause>nul
:test
for /l %%i in (0 1 100) do (
set var=%1
if "!var:~%%i,1!"=="" set /a %3=%%i-1 & goto :eof
rem Obtain the digits of thousands, millions, etc. respectively;
set /a num%%i%2=!var:~%%i,1!)


Explanation: The above code currently supports the multiplication of numbers within 100 digits; for the above code, there are at least other improvement solutions that can improve the running efficiency, that is, I mentioned in another post "Using Batch Processing to Do Interesting Math Problems" before, the segmented calculation method. It is recommended to divide into segments of 5 digits. In this way, a large number of repeated and frequent calculations can be avoided, thereby improving the running efficiency. As for the latter calculation method, it is still in the testing stage.

The above is just one of my solution ideas. I hope everyone will criticize and correct it!
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +2 2006-11-19 03:36
Floor 2 Posted 2006-11-19 02:35 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Remember that challenge, and these are all mentioned in batch operations
Floor 3 Posted 2006-11-19 03:06 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
It seems that I, a newbie, have opened the wrong post again!
The original has already had discussions!
http://www.cn-dos.net/forum/viewthread.php?tid=23568&fpage=1&highlight=%E5%8F%82%E4%B8%8E

However, the above calculation method has drawbacks (temporarily does not support floating-point number calculation), but it also has advantages. When processing particularly large numbers, the running efficiency is much higher.
Under the same circumstances, the code provided in the above post takes a long time to calculate 123456789101112131415161718192021222324252627282930*123456789101112131415161718192021222324252627282930, but the above code can finish it in two or three seconds
Floor 4 Posted 2006-11-19 04:00 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
If you change it to division, can you calculate pi? PI = 3.14...
Floor 5 Posted 2006-11-19 04:49 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 6 Posted 2006-11-19 06:48 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
There's no problem here for me!



As for the calculation of pi, using batch processing, it's difficult~ I think so.
Floor 7 Posted 2006-11-19 06:48 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Directly simulating assembly, using bitwise operations may be a good idea

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 8 Posted 2006-11-19 06:58 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
It turns out there was an error in set /a num%%i%2=!var:~%%i,1!, it should be: set /a num%%i%~2=!var:~%%i,1!
Floor 9 Posted 2006-11-19 07:11 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
I can't figure out why "set /a num%%i%~2=!var:~%%i,1! " can prevent some errors?
Floor 10 Posted 2006-11-19 22:49 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Because the second parameter in call :test %str1% "" ex is "", set /a num%%i%~2=!var:~%%i,1! will automatically remove the "" in %2. Otherwise, set /a num%%i%2=!var:~%%i,1! is equivalent to set /a num%%i""=!var:~%%i,1!, can this not go wrong?
Floor 11 Posted 2006-11-19 22:54 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
In addition, the carry should start from the lowest bit, otherwise it will also go wrong, because sometimes there will be multiple consecutive carries, then your algorithm will go wrong
Floor 12 Posted 2007-04-05 11:27 ·  中国 湖北 潜江 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
65536 multiplied by 65536 will truncate each digit.
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 13 Posted 2007-04-05 22:15 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
I have written a division in the 26th floor here; The dividend can theoretically be arbitrarily large; the number of decimal places can theoretically be arbitrarily many; but it really doesn't work when trying 1 million digits; it will prompt that the output is too long; However, the situation where the divisor is greater than 10 to the ninth power has never been solved

http://www.cn-dos.net/forum/viewthread.php?tid=29097&fpage=5&highlight=&page=2
Floor 14 Posted 2007-04-05 23:36 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, indeed. Simulating assembly will calculate faster. The method is also simpler.
知,不觉多。不知,乃求知
Forum Jump: