batch-file Bypass arithmetic limitations in batch files


Batch files allows only 32bit integer calculations , though this can be bypassed with different approaches.

Using powershell

As the powershell is installed by default on every windows system from 7/2008 and above it can be used for more complex calculations:

@echo off
set "expression=(2+3)*10/1000"
for /f %%# in ('"powershell %expression%"') do set result=%%#
echo %result%

Mind the additional double quotes in the for /f which prevent brackets conflicts with the for command syntax.

Potential issue is that powershell is much slower than using wsh/vbscript/jscript due to the loading of the .net framerwork

Using jscript

WSH/JScript is installed on every windows system since NT so using it for more complex calculations makes it pretty portable. JScript is easier for combining it with batch file :

@if (@[email protected]) @then
@echo off

set "expression=2*(2+3)/1000"
for /f %%# in ('cscript //nologo //e:jscript "%~f0" "%expression%"') do set 
echo %result%
:: more batch code

exit /b %errorlevel%

With this approach you can put your whole code in a single file.It is faster than using powershell. Here and here more advanced scripts can be found (which can be used as external files).

Emulating pen and paper calculations, math functions implementations

  1. Here can be found the most comprehensive math library that emulates pen and paper calculations and allows working with bigger numbers.
  2. Here are another examples of pen and paper emulations: ADD , Comparison , Multiply
  3. Some math functions implementations can be found here.