Doing MOD calculations in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rondinardo
Posts: 1
Joined: 14 Aug 2007 08:56

Doing MOD calculations in batch

#1 Post by rondinardo » 14 Aug 2007 09:05

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?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 18 Aug 2007 18:12

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 ;)

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Doing MOD calculations in batch??

#3 Post by pp8771 » 10 Jul 2018 08:03

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Doing MOD calculations in batch??

#4 Post by Squashman » 10 Jul 2018 09:03

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.

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Doing MOD calculations in batch??

#5 Post by pp8771 » 18 Jul 2018 04:44

Now working

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Doing MOD calculations in batch??

#6 Post by Aacini » 18 Jul 2018 05:57

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

juztsteve
Posts: 1
Joined: 01 Mar 2022 15:20

Re: Doing MOD calculations in batch

#7 Post by juztsteve » 01 Mar 2022 15:30

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Doing MOD calculations in batch

#8 Post by jeb » 04 Mar 2022 00:43

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Doing MOD calculations in batch

#9 Post by Compo » 04 Mar 2022 11:31

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"

Post Reply