I am running this batch program,

@echo off
set /a answer=2*1073741824
echo %answer%

Output am getting,


Expected Output,


The problem I am facing is the output returns in a negative value, and I don't know what's wrong. What solutions exist to help me resolve this problem using batch?


Calculate large integers in batch with a PowerShell helper

1. Batch

Essentially you can . . .

  1. Set a mathematical equation as a string variable in your batch script such as SET "Calculation=2*1073741824"

  2. Set the MathType string variable value for the operator type i.e. *.

  3. Use the #1 and #2 batch variables and plug those into small PowerShell commands to create a dynamic script with the batch ECHO command.

  4. The PowerShell logic will split apart each numerical portion of the #1 value and delimit it by the math operator (e.g. *) so each can be reused individually to do the large number calculation in PowerShell.

Finally, run the execution of the script through a for /f loop and save that executed result as a batch variable. This allows PowerShell to do the large int arithmetic calculation where it can handle more bytes, but also allows the result to be later used as a string variable in batch.

Batch Script

SET "Calculation=3*1073741824"
SET "MathType=*"

SET PSScript=%temp%\PS~MathTemp.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO $Calculation = "%Calculation%"                                             > "%PSScript%"
ECHO [int]$Calculation.split("%MathType%")[0] %MathType% [int]$Calculation.split("%MathType%")[1] >> "%PSScript%"

FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "nCalc=%%I"

ECHO %nCalc%

2. PowerShell

As suggested by others, I made a simple PowerShell script that will do this by passing it two arguments with those being...

  1. Simple two number calculation as an entire string (e.g. "4 + 4", "823*7", "8 /2", ..., etc.)
  2. The arithmetic operator of *, /, +, or - which should always match the #1 two number calculation arithmetic operator used in the same execution

PowerShell Script

$Calculation = $args[0];
$MathType    = $args[1];
$p1  = [int]$Calculation.split($MathType)[0].Trim();
$p2  = [int]$Calculation.split($MathType)[1].Trim();
$cmd = "$p1 $MathType $p2";
Invoke-Expression -Command $cmd

Execution Example

PS C:\WINDOWS\system32> & "C:\Scripts\PassCalc.ps1" "9 /  5 " "/"

enter image description here

Further Resources

| improve this answer | |
  • Perhaps this can handle more than the "simple" calculation as I stated in the PowerShell section—I kept my testing simple and thus my answer detail as well. – Pimp Juice IT Dec 12 '19 at 3:47

If things fail working in the Microsoft Windows Batch File Preprocessor, you can always use the Microsoft Windows Powershell Integrated Shell-Scripting Environment or the Cygwin Environment.

In the latter, you can use the bc Software Utility. It can perform advanced calculations. You can mix it with a Shell-Scripting Environment such as the Bourne-Again Shell-Scripting Environment and get the results that you want.

dobre@Laptop ~
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

dobre@Laptop ~
| improve this answer | |

Why not just hidden - in your variable %answer:-=%:

@echo off & cls 
set /a answer=2*1073741824
echo=%answer:-=% & pause>nul

rem :: you can remove from variable by :: 
set "answer=%answer:-=%"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.