CMD set / a, module and negative numbers

Can't CMD evaluate module of negative numbers with set /a ?

90 % 7 matches 6 batches correctly, however -90 % 7 gives -6 instead of 1.

I thought he could rate -(90 % 7) , but that doesn't seem to be the case since (-90) % 7 also gives -6.

 h:\uprof>set /a -90%7 -6 h:\uprof>set /a (-90)%7 -6 

So, is this a limitation of the set of CMD / module operator?

+6
source share
3 answers

% in CMD, as in other Microsoft environments, is a stop function, not a true modular operation. The rest of this is true for a -6 return for your examples. Using mod in Excel is a true module that returns the expected 1.

Despite the fact that it was written for C #, the article below has a big difference in difference: http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder -vs-modulus.aspx

+4
source

If you want true modulo, you can use this:

 set a=-90 set b=7 set /a (a%b+b)%b 
+3
source

As Jason Wu said, % not a modular operator. But if you want -b mod N, maybe this can help:

 @echo off set /a num1=7 set /a num2=-90 :add if %num2% LSS 0 set /a num2+=num1&goto add echo/%num2% pause 
+2
source

Source: https://habr.com/ru/post/980849/


All Articles