-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulusOperatorDetail.py
More file actions
422 lines (348 loc) · 8 KB
/
ModulusOperatorDetail.py
File metadata and controls
422 lines (348 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
print('''
% Modulus - remainder of the division of left operand by the right
x % y (remainder of x/y)
// Floor division - division that results into whole number adjusted
to the left in the number line x // y
''')
print('''
The % (modulo) operator yields the remainder from the division of
the first argument by the second. The numeric arguments are first
converted to a common type. A zero right argument raises the
ZeroDivisionError exception. The arguments may be floating point
numbers, e.g., 3.14%0.7 equals 0.34
(since 3.14 equals 4*0.7 + 0.34.)
The modulo operator always yields a result with the same sign
as its second operand (or zero); the absolute value of the
result is strictly smaller than the absolute value of the second
operand [2].
Taken from http://docs.python.org/reference/expressions.html
The first number is the numerator and the second is the denominator. In our example
2 divided by 6 is 0 remainder 2, therefore the result is 2.
print(2%6)
This is how it's implemented in Python:
a % b = a - b * floor(a/b)
a=2
b=6
print(a % b = a - b * floor(a/b))
''')
print('''
To use floor convertor we have to import from Math
from math import *
''')
from math import *
a=2
b=6
print(a - b * floor(a/b))
print('''
print(2%6)
''')
print(2%6)
print('''
a=12
b=15
print(a - b * floor(a/b))
''')
a=12
b=15
print(a - b * floor(a/b))
print('''
print(12%15)
''')
print(12%15)
print('''
a=12
b=-15
print(a - b * floor(a/b))
''')
a=12
b=-15
print(a - b * floor(a/b))
print('''
print(12%-15)
''')
print(12%-15)
print('''
a=-12
b=-15
print(a - b * floor(a/b))
''')
a=-12
b=-15
print(a - b * floor(a/b))
print('''
print(-12%-15)
''')
print(-12%-15)
print('''
a=6
b=6
print(a - b * floor(a/b))
''')
a=6
b=6
print(a - b * floor(a/b))
print('''
print(6%6)
''')
print(6%6)
print('''
a=1
b=6
print(a - b * floor(a/b))
''')
a=1
b=6
print(a - b * floor(a/b))
print('''
print(1%6)
''')
print(1%6)
print('''
a=-1
b=6
print(a - b * floor(a/b))
''')
print('''
print(-1%6)
''')
print(-1%6)
print('''
a=-1
b=-6
print(a - b * floor(a/b))
''')
a=-1
b=-6
print(a - b * floor(a/b))
print('''
print(-1%-6)
''')
print(-1%-6)
print('''
a=0
b=5
print(a - b * floor(a/b))
''')
print('''
print(0%5)
''')
print(0%5)
print('''
a=2
b=-6
print(a - b * floor(a/b))
''')
a=2
b=-6
print(a - b * floor(a/b))
print('''
print(2%-6)
''')
print(2%-6)
print('''
a=-2
b=6
print(a - b * floor(a/b))
''')
print('''
print(-2%6)
''')
print(-2%6)
print('''
a=-7
b=1
print(a - b * floor(a/b))
''')
a=-7
b=1
print(a - b * floor(a/b))
print('''
print(-7%1)
''')
print(-7%1)
print('''
print(7%0)
will produce error essage
ZeroDivisionError
''')
print('''
Somewhat off topic, the % is also used in string formatting operations
like %= to substitute values into a string:
>>> x = 'abc_%(key)s_'
>>> x %= {'key':'value'}
>>> x
'abc_value_'
Again, off topic, but it seems to be a little documented feature which took me
awhile to track down, and I thought it was related to Pythons modulo
calculation for which this SO page ranks highly.
''')
print('''
d="dog %(bark)s"
d %= {'bark':'run'}
print(d)
''')
d="dog %(bark)s"
d %= {'bark':'run'}
print(d)
print('''
b="bag%(heavy)s"
b %= {'heavy':'light'}
print(b)
''')
b="bag%(heavy)s"
b %= {'heavy':'light'}
print(b)
print('''
The modulus is a mathematical operation, sometimes described
as "clock arithmetic." I find that describing it as simply a
remainder is misleading and confusing because it masks the real
reason it is used so much in computer science. It really is used
to wrap around cycles.
Think of a clock: Suppose you look at a clock in "military" time,
where the range of times goes from 0:00 - 23.59. Now if you wanted
something to happen every day at midnight, you would want the current
time mod 24 to be zero:
if (hour % 24 == 0):
You can think of all hours in history wrapping around a circle of
24 hours over and over and the current hour of the day is that
infinitely long number mod 24. It is a much more profound concept
than just a remainder, it is a mathematical way to deal with cycles
and it is very important in computer science. It is also used to
wrap around arrays, allowing you to increase the index and use
the modulus to wrap back to the beginning after you reach the end
of the array.
This is how it's implemented in Python:
a % b = a - b * floor(a/b)
print("""
divmod(a, b)
Take two (non complex) numbers as arguments and return
a pair of numbers consisting of their quotient and remainder
when using long division.
""")
''')
print('''
from math import *
a=2
b=6
print(a - b * floor(a/b))
print(2%6)
''')
from math import *
a=2
b=6
print(a - b * floor(a/b))
print(2%6)
print('''
a=24
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
''')
a=24
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
print('''
a=-4
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
''')
a=-4
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
print('''
% Modulo operator can be also used for printing strings
(Just like in C) as defined on Google
https://developers.google.com/edu/python/strings.
# % operator
text = ("%d little pigs come out or I'll %s and %s and %s"
% (3, 'huff', 'puff', 'blow down'))
print(text)
This seems to bit off topic but It will certainly help someone.
''')
text = ("%d little pigs come out or I'll %s and %s and %s"
% (3, 'huff', 'puff', 'blow down'))
print(text)
print('''
Floor Division(//) - The division of operands where the result is the quotient
in which the digits after the decimal point are removed. But if one of the
operands is negative, the result is floored, i.e.,
rounded away from zero (towards negative infinity): examples: 9//2 = 4 and
9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
Towards infinity and away from zero are different things.
Example for (-1.2 and 3.4): "flooring" is towards
negative infinity (-2 and 3), "truncating" is towards zero
(-1 and 3), "saturating" is away from zero (-2 and 4),
and "ceiling" is towards positive infinity (-1 and 4).
''')
print('''
% Modulus - remainder of the division of left operand by the right
x % y (remainder of x/y)
// Floor division - division that results into whole number adjusted
to the left in the number line x // y
from math import *
a=-6
b=4
print(floor(a/b))
print(-6//4)
print(-6%4)
print(a - b * floor(a/b))
a=24
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
''')
from math import *
a=-6
b=4
print(floor(a/b))
print(-6//4)
print(-6%4)
print(a - b * floor(a/b))
a=24
b=7
print(divmod(a, b))
print(a/ b)
print(a// b)
print(a% b)
print("""
how does remainder or modulus operator works
a % b = a - b * floor(a/b)
An expression like x % y evaluates to the remainder of x ÷ y.
Precedence is the same as operators / (division) and * (multiplication).
>>> 9 / 2
4
>>> 9 % 2
1
% Modulo operator can be also used for printing strings (Just like in C)
as defined on Google https://developers.google.com/edu/python/strings.
# % operator
text = "%d little pigs come out
or I'll %s and %s and %s" % (3, 'huff', 'puff', 'blow down')
This seems to bit off topic but It will certainly help someone.
Somewhat off topic, the % is also used in string formatting operations
like %= to substitute values into a string:
>>> x = 'abc_%(key)s_'
>>> x %= {'key':'value'}
>>> x
'abc_value_'
Again, off topic, but it seems to be a little documented feature which took
me awhile to track down, and I thought it was related to Pythons modulo
calculation for which this SO page ranks highly.
Also, there is a useful built-in function called divmod:
divmod(a, b)
Take two (non complex) numbers as arguments and return a pair of numbers
consisting of their quotient and remainder when using long division.
""")