标题: [数值计算] 批处理如何实现超大数除法运算? [打印本页]
作者: strolley 时间: 2014-2-19 16:08 标题: 批处理如何实现超大数除法运算?
本帖最后由 pcl_test 于 2016-9-28 22:06 编辑
各位前辈:
在DOS下,想通过BAT写一个简单的除法运算,但是DOS只支持32位运算,超过32位就会有问题。
希望BAT实现此功能,最好精确到小数点后两位,请前辈、高手指教,谢谢啦!!!
如下为我在运行效果:
C:\Documents and Settings\user>set /a tmp=998555947008/1000202272768
无效数字。数字精确度限为 32 位。
作者: terse 时间: 2014-2-19 17:07
代码比较长- @echo off&setlocal enabledelayedexpansion
- set str1=998555947008
- set str2=1000202272768
- set u=2
- for %%i in (str1 str2) do if "!%%i:~,1!" == "-" set /a d+=1
- if "%d%" == "1" (set d=-) else set "d="
- set l=00000000&for /l %%i in (1 1 7) do set "l=!l!!l!"
- set "var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1"
- for /l %%i in (1 1 2) do (
- set "str%%i=!str%%i:-=!"
- set /a "n=str%%i_2=0"
- for %%a in (!str%%i:.^= !) do (
- set /a n+=1
- set s=s%%a&set str%%i_!n!=0
- for %%b in (%var%) do if "!S:~%%b!" neq "" set/a str%%i_!n!+=%%b&set "S=!S:~%%b!"
- set /a len%%i+=str%%i_!n!
- )
- set str%%i=!str%%i:.=!
- )
- if !str1_2! gtr !str2_2! (set /a len2+=str1_2-str2_2) else set /a len1+=str2_2-str1_2
- for /l %%i in (1 1 2) do (
- set str%%i=!str%%i!!l!
- for %%j in (!len%%i!) do set " str%%i=!str%%i:~,%%j!"
- )
- for /f "tokens=* delims=0" %%i in ("!str2!") do set s=%%i&set "str2=0%%i"
- set len2=1
- for %%j in (%var%) do if "!S:~%%j!" neq "" set/a len2+=%%j&set "S=!S:~%%j!"
- set /a len=len2+1
- if !len1! lss !len2! set len1=!len2!&set "str1=!l:~-%len2%,-%len1%!!str1!"
- set /a len1+=u&set str1=0!str1!!l:~,%u%!
- set str=!str1:~,%len2%!
- set "i=0000000!str2!"&set /a Len_i=Len2+7
- for /l %%i in (1 1 9) do (
- set "T=0"
- for /l %%j in (8 8 !Len_i!) do (
- set /a "T=1!i:~-%%j,8!*%%i+T"
- set Num%%i=!T:~-8!!Num%%i!&set /a "T=!T:~,-8!-%%i"
- )
- set Num%%i=!T!!Num%%i!
- set "Num%%i=0000000!Num%%i:~-%Len%!"
- )
- for /L %%a in (!len2! 1 !Len1!) do (
- set "str=!L!!str!!str1:~%%a,1!"
- set "str=!str:~-%Len%!"
- if "!str!" geq "!str2!" (
- set M=1&set i=0000000!str!
- for /l %%i in (2 1 9) do if "!i!" geq "!Num%%i!" set "M=%%i"
- set sun=!sun!!M!&set str=&set T=0
- for %%i in (!M!) do (
- for /l %%j in (8 8 !Len_i!) do (
- set /a "T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2"
- set "str=!T:~1!!str!"
- )
- )
- ) else set sun=!sun!0
- )
- set sun=!sun:~,-%u%!.!sun:~-%u%!
- echo %d%!sun!
- pause
复制代码
作者: strolley 时间: 2014-2-19 17:30
回复 2# terse
谢谢您,功能实现了!太厉害了!!!!
作者: strolley 时间: 2014-2-19 17:34
回复 2# terse
前辈,能否把这算法做成一个方法(公函),用的时候,只需要调用就可以~~~
如:call :Get_XXXX bcs cs jg (bcs被除数、cs除数、jg结果)
作者: Batcher 时间: 2014-2-19 17:42
http://bbs.bathome.net/thread-3372-2-1.html
这个里面有封装好的
作者: terse 时间: 2014-2-19 18:55
回复 4# strolley - @echo off&setlocal enabledelayedexpansion
-
- call:division 998555947008 1000202272768 quot 2
-
- echo %quot%
-
- pause&exit
- :division
- setlocal
- set str1=%1
- set str2=%2
- if "%~4" neq "" set u=%4
- for %%i in (str1 str2) do if "!%%i:~,1!" == "-" set /a d+=1
- if "%d%" == "1" (set d=-) else set "d="
- set l=00000000&for /l %%i in (1 1 7) do set "l=!l!!l!"
- set "var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1"
- for /l %%i in (1 1 2) do (
- set "str%%i=!str%%i:-=!"
- set /a "n=str%%i_2=0"
- for %%a in (!str%%i:.^= !) do (
- set /a n+=1
- set s=s%%a&set str%%i_!n!=0
- for %%b in (%var%) do if "!S:~%%b!" neq "" set/a str%%i_!n!+=%%b&set "S=!S:~%%b!"
- set /a len%%i+=str%%i_!n!
- )
- set str%%i=!str%%i:.=!
- )
- if !str1_2! gtr !str2_2! (set /a len2+=str1_2-str2_2) else set /a len1+=str2_2-str1_2
- for /l %%i in (1 1 2) do (
- set str%%i=!str%%i!!l!
- for %%j in (!len%%i!) do set " str%%i=!str%%i:~,%%j!"
- )
- for /f "tokens=* delims=0" %%i in ("!str2!") do set s=%%i&set "str2=0%%i"
- set len2=1
- for %%j in (%var%) do if "!S:~%%j!" neq "" set/a len2+=%%j&set "S=!S:~%%j!"
- set /a len=len2+1
- if !len1! lss !len2! set len1=!len2!&set "str1=!l:~-%len2%,-%len1%!!str1!"
- set /a len1+=u&set str1=0!str1!!l:~,%u%!
- set str=!str1:~,%len2%!
- set "i=0000000!str2!"&set /a Len_i=Len2+7
- for /l %%i in (1 1 9) do (
- set "T=0"
- for /l %%j in (8 8 !Len_i!) do (
- set /a "T=1!i:~-%%j,8!*%%i+T"
- set Num%%i=!T:~-8!!Num%%i!&set /a "T=!T:~,-8!-%%i"
- )
- set Num%%i=!T!!Num%%i!
- set "Num%%i=0000000!Num%%i:~-%Len%!"
- )
- for /L %%a in (!len2! 1 !Len1!) do (
- set "str=!L!!str!!str1:~%%a,1!"
- set "str=!str:~-%Len%!"
- if "!str!" geq "!str2!" (
- set M=1&set i=0000000!str!
- for /l %%i in (2 1 9) do if "!i!" geq "!Num%%i!" set "M=%%i"
- set sun=!sun!!M!&set str=&set T=0
- for %%i in (!M!) do (
- for /l %%j in (8 8 !Len_i!) do (
- set /a "T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2"
- set "str=!T:~1!!str!"
- )
- )
- ) else set sun=!sun!0
- )
- if defined u if "%u%" gtr "0" set sun=!sun:~,-%u%!.!sun:~-%u%!
- endlocal&set %3=%d%%sun%
复制代码
作者: strolley 时间: 2014-2-20 14:22
回复 6# terse
谢谢,非常感谢您!
现在想在for循环体内调用division方法,对各磁盘使用率进行计算,并显示各磁盘的使用率,但在调用的时候有点问题,不知调用是否需要for循环实现??请高手指点!
@echo off
setlocal enabledelayedexpansion
echo 11111111111
for /f "skip=1 tokens=1,2,3 delims= " %%i in ('wmic logicaldisk where "drivetype=3" get name^,size^,freespace') do (
set /a n+=1
echo 盘符 %%j 剩余:%%i 空间%%k
set AAA=%%i
set BBB=%%k
rem 循环取值
call:division !AAA! !BBB! quot 2 &echo 盘符%%j 剩余为%%j 使用率为%quot%
)
pause
作者: terse 时间: 2014-2-20 15:40
回复 7# strolley
不清楚你的工作环境 我这里 XP win7 测试通过- @echo off
- setlocal enabledelayedexpansion
- echo 11111111111
- for /f "skip=1 tokens=1,2,3 delims= " %%i in ('wmic logicaldisk where "drivetype=3" get name^,size^,freespace') do (
- for /f %%a in ("%%i") do (
- set /a n+=1
- echo 盘符 %%j 剩余:%%i 空间%%k
- set AAA=%%i
- set BBB=%%k
- rem 循环取值
- call:division !AAA! !BBB! quot 2
- echo 盘符%%j 剩余为%%i 使用率为!quot!
- )
- )
- pause&exit
复制代码
作者: PowerShell 时间: 2014-2-20 16:50
本帖最后由 PowerShell 于 2014-2-20 16:52 编辑
powershell代码复制代码
获取卷信息,还可以再次基础上进一步计算占有率和剩余率
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ------------- ----
C NTFS Fixed Healthy 18.59 GB 46 GB
Z NTFS Removable Healthy 37.74 GB 62.08 GB
K NTFS Removable Healthy 47.64 GB 58.16 GB
D FAT32 Fixed Healthy 26.79 GB 30.99 GB
E NTFS Fixed Healthy 194.42 GB 401.08 GB
作者: winload 时间: 2016-1-27 13:48
求解释 此大数除法函数的 算法思路 只解释正整数除法思路就行 不用小数除法
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |