Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO Off
- ::Bigmath.bat
- ECHO %~1|FIND "?"||if not "%~2"=="" GOTO BEGIN
- :help
- echo.&echo "%~nx0" A NT Command script to circumvent large number limitations and the lack of
- echo floating point returned by set /a in situations where speed is not critical.&echo.
- echo Usage:&echo "%~nx0" number1 operator number2 [outputvariable]&echo.
- echo Where numberx is your input numbers to be calculated and operator is a valid operator
- echo.&echo Valid operators are:&echo - Subtract.&echo + Add.&echo * Multiply.&echo / Divide.
- echo %% Modulo&echo BD - Bin to decimal&echo DB - Devimal to bin&echo.
- echo Output variable is optional - if present the output will be set to the variable name
- echo provided rather than be printed to screen.&echo.&echo.
- pause
- goto :eof
- :begin
- SETLOCAL ENABLEDELAYEDEXPANSION
- set optr=%~2
- set inpnum1=%1
- set inpnum2=0
- set dplace=
- set "varout=%~3"
- if /i not "%~2"=="bd" IF /I NOT "%~2"=="DB" (
- IF "%~3"=="" ENDLOCAL&GOTO HELP
- SET "VAROUT=%~4"
- set inpnum2=%3
- )
- if "%inpnum1%"=="%inpnum1:.=%" if "%inpnum2%"=="%inpnum2:.=%" goto FLOATSKIP
- if "%inpnum1:~0,1%"=="." set inpnum1=0%inpnum1%
- if "%inpnum2:~0,1%"=="." set inpnum2=0%inpnum2%
- if "%inpnum1:~-1%"=="." set inpnum1=%inpnum1%0
- if "%inpnum2:~-1%"=="." set inpnum2=%inpnum2%0
- SET DP1=0
- SET DP2=0
- FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM1%") DO (
- SET DPT=%%A
- FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP1=%%B&&GOTO BL1
- )
- :BL1
- FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM2%") DO (
- SET DPT=%%A
- FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP2=%%B&&GOTO BL2
- )
- :BL2
- SET DPLACE=%DP1%
- IF NOT !DP1! EQU !DP2! (
- IF !DP1! LSS !DP2! (
- SET DPLACE=%dp2%
- SET /A DPT=DP2-DP1
- FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM1=!INPNUM1!0
- ) ELSE (
- set dlace=%dp1%
- SET /A DPT=DP1-DP2
- FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM2=!INPNUM2!0
- )
- )
- SET INPNUM1=%INPNUM1:.=%
- SET INPNUM2=%INPNUM2:.=%
- :FLOATSKIP
- :dzer01
- if not "!inpnum1:~1!"=="" if "!inpnum1:~0,1!"=="0" (
- set inpnum1=!inpnum1:~1!
- goto dzer01
- )
- :dzer02
- if not "!inpnum2:~1!"=="" if "!inpnum2:~0,1!"=="0" (
- set inpnum2=!inpnum2:~1!
- goto dzer02
- )
- if "!optr!"=="-" goto subtract
- if "!optr!"=="+" goto add
- if "!optr!"=="*" goto multiply
- if "!optr!"=="/" (
- if !inpnum2! lss 1000000 (
- goto fastdiv
- ) else (
- goto divide
- )
- )
- IF /I "!optr!"=="BD" GOTO BINTODEC
- IF /I "!optr!"=="DB" GOTO DECTOBIN
- if "!optr!"=="%%" goto mod
- goto help
- :subtract
- for %%a in (stotal stten SUB1 SUB2 neg) do set %%a=
- if /I "%~1%~2"=="internal" (
- set sub1=!%3!
- set sub2=!%4!
- ) else (
- set sub1=!inpNUM1!
- set sub2=!inpNUM2!
- )
- set subsub1=!sub2!
- set subsub2=!sub1!
- :sloop
- set sc1=
- set sc2=
- set sstrore=
- if defined sub1 (
- if defined sub2 (
- set sc1=%sub1:~-1%
- set sc2=%sub2:~-1%
- set sub1=%sub1:~0,-1%
- set sub2=%sub2:~0,-1%
- ) else (
- if defined stten (
- set sub2=!stten!
- set stten=
- set sc1=!sub1:~-1!
- set sc2=!sub2:~-1!
- set sub1=!sub1:~0,-1!
- set sub2=!sub2:~0,-1!
- ) else (
- set stotal=%sub1%%stotal%
- call :finish stotal
- goto soutput
- )
- )
- ) else (
- for %%a in (sub2 stten) do (
- if defined %%a (
- set sub1=%subsub1%
- set sub2=%subsub2%
- set neg=-
- set stotal=
- set stten=
- goto sloop
- )
- )
- call :finish stotal
- goto soutput
- )
- set sazc=
- set sazcnt=
- :asttend
- if !sc1!==0 if !sc2! gtr 0 if defined sub1 (
- set sazc=!sub1:~-1!
- set /a sazcnt+=1
- set sub1=!sub1:~0,-1!
- if !sazc!==0 goto asttend
- set /a sazc-=1
- set sc1=10
- set sub1=!sub1!!sazc!
- for /l %%a in (2,1,!sazcnt!) do set sub1=!sub1!9
- )
- if defined stten (
- set /a sc1-=1
- if !sc1! lss 0 (
- set /a stten+=1
- set /a sc1+=10
- ) else (
- set stten=
- )
- )
- set /a sstrore=%sc1%-%sc2%
- if !sstrore! lss 0 (
- set stten=1
- set /a sstrore+=10
- set stten=!stten:-=!
- )
- set stotal=%sstrore%%stotal%
- goto sloop
- :soutput
- if /I "%~1%~2"=="internal" (
- set %3=%stotal%
- goto :eof
- ) else (
- if not defined varout (
- echo %stotal%
- ENDLOCAL
- ) else (
- endlocal&set %varout%=%stotal%
- )
- )
- goto :eof
- :add
- if /I "%~1%~2"=="internal" (
- set add1=!%3!
- set add2=!%4!
- ) else (
- set add1=!inpNUM1!
- set add2=!inpNUM2!
- )
- set atotal=
- set astore=
- set acarry=
- :aloop
- if not defined add1 (
- if defined acarry (
- set add1=%acarry%
- set acarry=
- ) else (
- set atotal=%add2%%atotal%
- call :finish atotal
- goto aoutput
- )
- )
- if not defined add2 (
- if defined acarry (
- set add2=%acarry%
- set acarry=
- ) else (
- set atotal=%add1%%atotal%
- call :finish atotal
- goto aoutput
- )
- )
- if not defined acarry (
- set /a astore=%add1:~-1%+%add2:~-1%
- ) else (
- set /a astore=%add1:~-1%+%add2:~-1%+%acarry%
- set acarry=
- )
- if !astore! lss 10 (
- set atotal=%astore%%atotal%
- ) else (
- set atotal=%astore:~-1%%atotal%
- set acarry=%astore:~0,-1%
- )
- set astore=
- set add1=%add1:~0,-1%
- set add2=%add2:~0,-1%
- goto aloop
- :aoutput
- if /I "%~1%~2"=="internal" (
- set %3=%atotal%
- ) else (
- if not defined varout (
- echo %atotal%
- ENDLOCAL
- ) else (
- endlocal&set %varout%=%atotal%
- )
- )
- goto :eof
- :MULTIPLY
- SET MTOTAL=0
- set /a ph=dplace*2
- set dplace=
- set mten=
- set mult1=!inpnum1!
- set mult2=!inpnum2!
- IF !mult1! equ 0 set mtotal=0&&goto moutput
- IF !mult2! equ 0 set mtotal=0&&goto moutput
- IF !mult2! equ 1 (
- set mtotal=%mult1%
- set dplace=!ph!
- call :finish mtotal
- goto moutput
- ) else (
- IF !mult1! equ 1 (
- set mtotal=%mult2%
- set dplace=!ph!
- call :finish mtotal
- goto moutput
- )
- )
- set m1=!mult1!
- for /l %%a in (2,1,9) do (
- call :add inte rnal m1 mult1
- set m%%a=!m1!
- )
- set m1=!mult1!
- :MPLY
- if defined mult2 (
- set mby=!mult2:~-1!
- set mult2=!mult2:~0,-1!
- ) else (
- set dplace=!ph!
- call :finish mtotal
- goto moutput
- )
- if !mby!==0 (
- set mten=%mten%0
- goto mply
- )
- set madvar=!m%mby%!%mten%
- call :add inte rnal mtotal madvar
- set mten=%mten%0
- goto mply
- :moutput
- if not defined varout (
- echo %mtotal%
- ENDLOCAL
- ) else (
- endlocal&set %varout%=%mtotal%
- )
- goto :eof
- :divide
- if !inpnum2! equ 1 (
- set dtotal=%inpnum1%
- call :finish dtotal
- goto doutput
- )
- for %%a in (din1 din2 diff dten DCNT) do set %%a=
- set dtotal=0
- set dplace=10
- set inpnum1=!inpnum1!0000000000
- set div1=!inpnum1!
- set div2=!inpnum2!
- if !div2!==0 echo Divide by 0^^!>&2&&exit /b 2
- :din1
- if defined inpnum1 (
- set /a din1+=1
- set inpnum1=%inpnum1:~1%
- goto din1
- )
- :din2
- if defined inpnum2 (
- set /a din2+=1
- set inpnum2=%inpnum2:~1%
- goto din2
- )
- if !din2! gtr !din1! (
- set dtotal=0
- goto doutput
- )
- set /a diff=din1-din2
- for /l %%a in (1,1,%diff%) do set dten=!dten!0
- set ph=!dplace!
- set dplace=
- :div
- set dtmp=%div1%
- set daddvar=%div2%%dten%
- call :subtract inte rnal div1 daddvar
- if defined neg (
- set div1=!dtmp!
- set neg=
- if defined dten (
- set dten=!dten:~1!
- goto div
- ) else (
- set dplace=!ph!
- call :finish dtotal
- goto doutput
- )
- ) else (
- set daddvar=1%dten%
- call :add inte rnal dtotal daddvar
- if !div1! equ 0 (
- set dplace=!ph!
- call :finish dtotal
- goto doutput
- )
- goto div
- )
- :doutput
- if not defined varout (
- echo %dtotal%
- ENDLOCAL
- ) else (
- endlocal&set %varout%=%dtotal%
- )
- goto :eof
- :fastdiv
- set fccnt=
- set dplace=10
- set fdiv=%inpnum1%0000000000
- set ftotal=
- :fsplit
- if not defined fdiv goto floop
- if !fdiv! gtr 999 (
- set /a fccnt+=1
- set fchunk!fccnt!=%fdiv:~-4%
- set fdiv=%fdiv:~0,-4%
- goto fsplit
- ) else (
- set /a fccnt+=1
- set fchunk!fccnt!=!Fdiv!
- )
- :floop
- if defined fchunk%fccnt% (
- if "!fchunk%fccnt%:~0,1!"=="0" (
- set fchunk%fccnt%=!fchunk%fccnt%:~1!
- goto floop
- )
- ) else (
- set fchunk%fccnt%=0
- goto fpad
- )
- set fvcnt=0
- :fsubloop
- If !fchunk%fccnt%! geq !inpnum2! (
- If !fchunk%fccnt%! geq !inpnum2!000 (
- set /a fchunk%fccnt%-=!inpnum2!000
- set /a fvcnt+=1000
- goto fsubloop
- ) else (
- If !fchunk%fccnt%! geq !inpnum2!00 (
- set /a fchunk%fccnt%-=!inpnum2!00
- set /a fvcnt+=100
- goto fsubloop
- ) else (
- If !fchunk%fccnt%! geq !inpnum2!0 (
- set /a fchunk%fccnt%-=!inpnum2!0
- set /a fvcnt+=10
- goto fsubloop
- ) else (
- set /a fchunk%fccnt%-=!inpnum2!
- set /a fvcnt+=1
- goto fsubloop
- )
- )
- )
- )
- :fpad
- if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
- if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
- if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
- set ftotal=%ftotal%%Fvcnt%
- set fvcnt=0
- set ftmpcnt=%fccnt%
- set /a fccnt-=1
- if !fchunk%ftmpcnt%! gtr 0 (
- set fcarry=!fchunk%ftmpcnt%!
- set fchunk!fccnt!=!fcarry!!fchunk%fccnt%!
- )
- if %fccnt% equ 0 (
- call :finish ftotal
- goto foutput
- )
- goto floop
- :foutput
- if not defined varout (
- echo %ftotal%
- ENDLOCAL
- ) else (
- endlocal&set %varout%=%ftotal%
- )
- goto :eof
- :dectobin
- IF "!INPNUM1:0=!"=="" (
- if defined varout (endlocal&set "%varout%=0") else ECHO 0
- endlocal
- GOTO :EOF
- )
- IF DEFINED DPLACE (
- 1>&2 ECHO ONLY WHOLE NUMBERS ARE SUPPOTED FOR BINARY CONVERSION.
- PAUSE
- ENDLOCAL
- EXIT /B 1
- )
- set dB=%inpnum1%
- set DBbin=
- :DBloop
- call "%~F0" %DB% / 2 DBtest
- for /f "tokens=1,2 delims=." %%a in ("%DBtest%") do (
- set dB=%%a
- if "%%b"=="" (
- set DBbin=0%DBbin%
- ) else (
- set DBbin=1%DBbin%
- )
- )
- if not %DB%==0 goto DBloop
- if defined varout (
- endlocal&set "%varout%=%Dbbin%"
- ) else (
- echo %dbbin%
- endlocal
- )
- exit /b
- GOTO :EOF
- :BINTODEC
- set BDbin=%INPNUM1%
- set bdtest=%bdbin:0=%
- if defined bdtest if not "%bdtest:1=%"=="" (
- 1>&2 echo bad input for a binary number - "!bdbin!"
- endlocal
- exit /b 1
- )
- set BD=0
- set BDbase=1
- :BDloop
- if not defined BDbin (
- if defined varout (
- endlocal&set "%varout%=%BD%"
- ) else (
- echo %bd%
- endlocal
- )
- exit /b
- )
- if "%BDbin:~-1%"=="1" call :ADD INTE RNAL BD BDBase
- call "%~F0" %BDbase% * 2 BDbase
- set BDbin=%BDbin:~0,-1%
- goto BDloop
- :finish
- if !dplace! equ 0 set dplace=
- if defined dplace if "!%1:~%dplace%,1!"=="" set %1=0!%1!&&goto finish
- if defined dplace set %1=!%1:~0,-%dplace%!.!%1:~-%dplace%!
- :killzerodec
- if defined dplace (
- if "!%1:~-1!"=="0" (
- set %1=!%1:~0,-1!
- goto killzerodec
- ) else (
- if "!%1:~-1!"=="." (
- set %1=!%1:~0,-1!
- )
- )
- )
- :killzerowhle
- if "!%1:~0,1!"=="0" (
- set %1=!%1:~1!
- goto killzerowhle
- ) else (
- if "!%1:~0,1!"=="." set %1=0!%1!
- )
- IF NOT DEFINED %1 SET %1=0
- set %1=%neg%!%1!
- goto :eof
- :mod
- if "!inpnum2!"=="0" (
- echo modulo by zero
- goto :eof
- )
- if "!inpnum1!"=="!inpnum2!" (
- if defined varout (
- endlocal&set "%varout%=0"
- ) else (
- echo 0
- endlocal
- )
- goto :eof
- )
- if not "!inpnum1:-=!"=="!inpnum1!" (
- echo Negitive Modulo not supported
- goto :eof
- )
- if not "!inpnum2:-=!"=="!inpnum2!" (
- echo Negitive Modulo not supported
- goto :eof
- )
- set holder=!dplace!
- set dplace=
- set neg=
- set mtotal=0
- set mod1=!inpnum1!
- set mod2=!inpnum2!
- set 10x=0000000000000000000000000000000000
- :mdloop
- set mtotal=%mod1%
- set mdsub=!mod2!!10x!
- call :subtract inte rnal mod1 mdsub
- if not defined neg goto mdloop
- if not "!10x!"=="" (
- set neg=
- set 10x=!10x:~1!
- set mod1=!mtotal!
- goto mdloop
- )
- set dplace=!holder!
- set neg=
- call :finish mtotal
- if defined varout (
- endlocal&set "%varout%=%mtotal%"
- ) else (
- echo %mtotal%
- endlocal
- )
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement