Page 1 of 1

Doing MOD calculations in batch

Posted: 14 Aug 2007 09:05
by rondinardo
I would love to do a modulo in batch ... does anyone know how to write it??



I figure it's something simlar to ADDITION:

set /a var1=4 + 2


So I was thinking of the logical choices:

set /a var2=4 % 2

- or -

set /a var2=4 MOD 2


Neither of these work. What's wrong? Am I at least close?

Posted: 18 Aug 2007 18:12
by DosItHelp
rondinardo,

Since the % sign is also used for variablen substitution you'll need to use two of them to make the commandline parser understand it. Try:

Set /a a = 13 %% 5

You should get 3.
DOS IT HELP ;)

Re: Doing MOD calculations in batch??

Posted: 10 Jul 2018 08:03
by pp8771
Set /a a = 13 %% 5
giving error missing operand


OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601


OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601

Re: Doing MOD calculations in batch??

Posted: 10 Jul 2018 09:03
by Squashman
pp8771 wrote:
10 Jul 2018 08:03
Set /a a = 13 %% 5
I guess you misunderstood the previous comments in this thread.

If you are running from a command prompt you use one percent symbol.

Code: Select all

Set /a  a = 13 % 5
If you are running the code in a batch file then use two percent symbols.

Code: Select all

Set /a  a = 13 %% 5
Look at Dave's explanation here.

Re: Doing MOD calculations in batch??

Posted: 18 Jul 2018 04:44
by pp8771
Now working

Re: Doing MOD calculations in batch??

Posted: 18 Jul 2018 05:57
by Aacini
It is working here. This is my test:

Code: Select all

Microsoft Windows [Versión 6.3.9600]
(c) 2013 Microsoft Corporation. Todos los derechos reservados.

C:\Users\Antonio> cd test

C:\Users\Antonio\Test> type test.bat
@echo off

set /a  a = 13 %% 5

echo Result: %a%

C:\Users\Antonio\Test> test
Result: 3
Antonio

Re: Doing MOD calculations in batch

Posted: 01 Mar 2022 15:30
by juztsteve
Kind of late to reply to this but here is a script I wrote that resolved the % issue by using double quotes:

@echo off
title [ Mod Calculator ]
set /p "divnd=Enter a dividend: "
set /a "divnd2=%divnd%"
if %divnd% lss 0 set /a "divnd-=(divnd*2)"
echo:
set /p "divsr=Enter a divisor: "
set /a "qtnt=%divnd%/divsr%"
set /a "mod=%divnd%%%divsr%"
echo:
echo %divnd2% divided by %divsr% = %qtnt%, mod = %mod%
echo:
pause
title
exit /b

Re: Doing MOD calculations in batch

Posted: 04 Mar 2022 00:43
by jeb
Hi juztsteve,

your quote solution is interessting but wrong.

It computes the modulo, but the quotes doesn't escape the percent sign,
instead the complete parsing is broken in your line and works by coincidence.

Code: Select all

set /a "mod=%divnd%%%divsr%"
Is parsed into:
1. set "/a=<the dividend number> %%divsr%"   # The expansion of the first variable
2. set "/a=<the dividend number> %   divsr%"   # Two percent signs are used to create one literal percent sign
3. set "/a=<the dividend number> %   divsr"      # The trailing percent sign is removed, because there are no more percent signs in the line
4. set "/a=<the dividend number> %   <the divisor>"  # set/a is able to replace variable names by itself, without the need of percent/delayed expansion
You can see the full problem by replacing the %divsr% by a fixed number or by inserting spaces

Code: Select all

set /a "mod=%divnd%%16"
set /a "mod=%divnd%  %  %divsr%"
Both cases fails.

Batch is always a source of fun, with unexpected results
jeb

Re: Doing MOD calculations in batch

Posted: 04 Mar 2022 11:31
by Compo
juztsteve wrote:
01 Mar 2022 15:30
<SNIP />
set /a "divnd2=%divnd%"
<SNIP />
set /a "qtnt=%divnd%/divsr%"
set /a "mod=%divnd%%%divsr%"
<SNIP />
You should be using this instead:

Code: Select all

set /a divnd2 = divnd

Code: Select all

set /a qtnt = divnd / divsr

Code: Select all

set /a mod = divnd %% divsr
Or if you want to minimize characters, but still use doublequotes:

Code: Select all

set /a "divnd2=divnd"

Code: Select all

set /a "qtnt=divnd/divsr"

Code: Select all

set /a "mod=divnd%%divsr"