批处理文件乘法正变量返回负数

时间:2016-07-25 14:39:46

标签: batch-file multiplication area

我一直在研究批量多边形区域计算器,但我遇到了问题。 我需要将2个变量相乘,但如果两个正变量很大,有时会返回负数。

以下是一个示例:999999*999999返回-729379967

代码如下:

REM Calc square area
:PolySqu
Cls
Echo                                        Polygon Area Calculator 
For /L %%P In (1,1,57) Do Echo.
Set /P "InputPolygonCalSqu=Enter one of the line's length in cm :"


Set /A SquArea=InputPolygonCalSqu * InputPolygonCalSqu


Cls
Echo                                        Polygon Area Calculator
For /L %%P In (1,1,57) Do Echo.
Echo The area of this square is %SquArea% cm2.
Pause
Goto :PolygonCal

似乎是命令

Set /A SquArea="InputPolygonCalSqu * InputPolygonCalSqu

无法正确计算。

4 个答案:

答案 0 :(得分:1)

批处理使用32位整数来存储数字。这使它们的最大尺寸为2 ^ 31 - 1 = 2,147,483,647。

999,999 * 999,999 = 999,998,000,001,大于2,147,483,647因此它“包裹”并从否定开始。

这是批量限制,尽管有一些workarounds

答案 1 :(得分:1)

这可能很有用

Can batch files not process large numbers?

批次最多可以是2 ^ 31 - 1 = 2,147,483,647。

所以,它再次从消极开始,并给你答案..

答案 2 :(得分:1)

正如其他人已经指出的那样,本身仅支持32位有符号整数算术。

以下代码构成了使用纯命令将非负数乘以大于2 32 - 1 = 2147483647的限制的解决办法(让我们称之为{ {1}}):

multiply.bat

使用它提供两个数字作为命令行参数相乘;例如:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define arguments here:
set "NUM1=%~1"
set "NUM2=%~2"
set "NUM3=%~3"
set "NUM4=%~4"
if defined NUM1 set "NUM1=%NUM1:"=""%
if defined NUM2 set "NUM2=%NUM2:"=""%
if defined NUM3 set "NUM3=%NUM3:"=%
call :VAL_ARGS NUM1 NUM2 NUM4 || exit /B 1

rem // Define constants here:
set /A "DIG=4" & set "PAD="
setlocal EnableDelayedExpansion
for /L %%J in (1,1,%DIG%) do set "PAD=!PAD!0"
endlocal & set "PAD=%PAD%"

rem // Determine string lengths:
call :STR_LEN LEN1 NUM1
call :STR_LEN LEN2 NUM2
set /A "LEN1=(LEN1-1)/DIG*DIG"
set /A "LEN2=(LEN2-1)/DIG*DIG"
set /A "LIM=LEN1+LEN2+DIG"
for /L %%I in (0,%DIG%,%LIM%) do set /A "RES[%%I]=0"

rem // Perform block-wise multiplication:
setlocal EnableDelayedExpansion
for /L %%J in (0,%DIG%,%LEN2%) do (
    for /L %%I in (0,%DIG%,%LEN1%) do (
        set /A "IDX=%%I+%%J"
        if %%I EQU 0 (set "AUX1=-%DIG%") else (
            set /A "AUX1=%DIG%+%%I" & set "AUX1=-!AUX1!,-%%I"
        )
        if %%J EQU 0 (set "AUX2=-%DIG%") else (
            set /A "AUX2=%DIG%+%%J" & set "AUX2=-!AUX2!,-%%J"
        )
        for /F "tokens=1,2" %%M in ("!AUX1! !AUX2!") do (
            set "AUX1=!NUM1:~%%M!" & set "AUX2=!NUM2:~%%N!"
        )
        call :NO_LEAD0 AUX1 !AUX1!
        call :NO_LEAD0 AUX2 !AUX2!
        set /A "RES[!IDX!]+=AUX1*AUX2"
        set /A "NXT=IDX+DIG, DIT=DIG*2"
        for /F "tokens=1,2,3" %%M in ("!IDX! !NXT! !DIT!") do (
            set "AUX=!RES[%%M]:~-%%O,-%DIG%!"
            set /A "RES[%%N]+=AUX"
            set "RES[%%M]=!RES[%%M]:~-%DIG%!"
            call :NO_LEAD0 RES[%%M] !RES[%%M]!
        )
    )
)

rem // Build resulting product:
set "RES=" & set "AUX="
for /L %%I in (0,%DIG%,%LIM%) do (
    set /A "RES[%%I]+=AUX"
    set /A "NXT=%%I+DIG"
    for /L %%J in (!NXT!,%DIG%,!NXT!) do (
        set "AUX=!RES[%%I]:~-%%J,-%DIG%!"
    )
    set "RES[%%I]=%PAD%!RES[%%I]!"
    set "RES=!RES[%%I]:~-%DIG%!!RES!"
)
endlocal & set "RES=%RES%"
call :NO_LEAD0 RES %RES%

rem // Return resulting product:
echo(%RES%
if defined NUM3 (
    endlocal
    set "%NUM3%=%RES%"
) else (
    endlocal
)
exit /B


:NO_LEAD0  rtn_var  val_num
rem // Remove leading zeros from a number:
for /F "tokens=* delims=0" %%Z in ("%~2") do (
    set "%~1=%%Z" & if not defined %~1 set "%~1=0"
)
exit /B 0


:STR_LEN  rtn_length  ref_string
rem // Retrieve length of string:
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if defined STR (
        set "INT=!STR:~%%L!"
        if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
    )
)
endlocal & set "%~1=%LEN%"
exit /B 0


:VAL_ARGS  ref_arg1  ref_arg2  ref_arg3
rem // Check arguments for validity:
if not defined %~1 >&2 echo ERROR: too few arguments given! & exit /B 1
if not defined %~2 >&2 echo ERROR: too few arguments given! & exit /B 1
if defined %~3 >&2 echo ERROR: too many arguments given! & exit /B 1
(call echo "%%%~1%%" | > nul findstr /R /C:"^\"[0-9][0-9]*\" $") || (
    >&2 echo ERROR: argument 1 is not purely numeric! & exit /B 1
)
(call echo "%%%~2%%" | > nul findstr /R /C:"^\"[0-9][0-9]*\" $") || (
    >&2 echo ERROR: argument 2 is not purely numeric! & exit /B 1
)
exit /B 0

生成的产品将在控制台上返回:

multiply.bat 999999 999999

如果提供第三个参数,则将产品分配给具有该名称的变量;例如:

999998000001

这会将变量multiply.bat 999999 999999 SquArea 设置为结果值。后者仍然在控制台上返回 要在没有任何其他控制台输出的情况下静默分配变量,请将其重定向到SquArea设备:

nul

答案 3 :(得分:0)

我注意到使用纯批处理,实现支持超出32位整数限制的数字的操作会有些困难。所以相反,我限制了数字,所以它们在乘以时不会溢出。

REM Calc Square Area
:PolySqu
Cls
Echo                       Polygon Area Calculator
For /L %%P In (1,1,57) Do Echo.
Set /P "InputPolygonCalSqu=Enter one of the line's length in cm [Less then 40000] :"
If %InputPolygonCalSqu% GTR 40000 Goto :PolySqu

Set /A SquArea="InputPolygonCalSqu * InputPolygonCalSqu


Cls
Echo                       Polygon Area Calculator
For /L %%P In (1,1,57) Do Echo.
Echo The area of this square is %SquArea% cm2.
Pause
Goto :PolygonCal