## Modulus (cyclic, odd/even, factoring/divisibility)

(filler for getting edited post, updated/posted)

(made this as a thread here, so it doesn't get lost in the main forum, and as it should be useful for anyone learning to code, and/or who don't know about the 'modulus' operator/operation yet and its applications/power/uses: cyclic, odd/even, factoring/divisibility)

the '%' is the 'modulus' operator/operation, which is just division, EXCEPT it finds/gets/returns the REMAINDER

https://en.wikipedia.org/wiki/Modulo_operation ('modulus' is usually used as its easier to say/remember, it's latin --- what science doesn't use latin --- lol, so it's some kind of grammer form of it, most likely: see link below)

https://en.wikipedia.org/wiki/Modulus

The reason why the '% (modulus)' operator/operation is so useful, is because:

the REMAINDER (of division) is very special, as it can be used for:

1. cyclic
2. (odd/even)-ness of a number
3. factoring/divisible-ness of a number

1. cyclic (repeating/limited sequences):

number systems: 0 to N digits (decimal/metric number system: base 10: 0 to 9 digits, binary/digital number system: base 2: 0 to 1 digits, hexidecimal number system: base 16: 0 to 9 to A::10 to F::15 digits, octal number system: base 8: 0 to 7 digits, viking/american/"imperial"/curvature/time unit/measurement system: base 12: 0 to 11 digits, etc etc etc)

time/dates: 0 to N digits (seconds/minutes: 0 to 59, civilian-hours/months-in-a-year: 0 to 11, military-hours: 0 to 23, days-in-a-week: 0 to 6, etc etc etc)

etc etc etc stuff

1. (odd/even)-ness of a number

is 59 odd or even? ODD
is 88 odd or even? EVEN

1. factoring/divisible-ness of a number

is 49 divisible by 7 ? / is '7' a factor of '49' ? ---> YES/TRUE
is 48 divisible by 7 ? / is '7' a factor of '48' ?---> NO/FALSE

is 63 divisible by 3 ? / is '3' a factor of '63' ? ---> YES/TRUE
is 62 divisible by 3? / is '3' a factor of '62' ? ---> NO/FALSE

how does it work?

Cyclic:

let's take a simple example of the (4) seasons (using a small number so it's easy/quick to show it):

4 quantity of digits: base 4 = 4^N: 0 to 3 digits:

N = 1, as we're just taking the seasons: 4^(N:1) = 4 seasons

// concept only (we'd have to use a Dictionary Attribute to actually literally do this, which is too much effort, as the point here is to explain the modulus, not Dictionary Attribute usage, lol):
0 = winter
1 = spring
2 = summer
3 = autumn

game.count = 0
// pretend this is an infinite loop:
game.season = (game.count) % 4
game.count = game.count + 1

// game.count = 0
game.season = (0) % 4 = (Q: 0, R: 0) = R: 0 = 0 = winter

// game.count = 1
game.season = (1) % 4 = (Q: 0, R: 1) = R: 1 = 1 = spring

// game.count = 2
game.season = (2) % 4 = (Q: 0, R: 2) = R: 2 = 2 = summer

// game.count = 3
game.season = (3) % 4 = (Q: 0, R: 3) = R: 3 = 3 = autumn

// game.count = 4
game.season = (4) % 4 = (Q: 1, R: 0) = R: 0 = 0 = winter

// game.count = 5
game.season = (5) % 4 = (Q: 1, R: 1) = R: 1 = 1 = spring

// game.count = 6
game.season = (6) % 4 = (Q: 1, R: 2) = R: 2 = 2 = summer

// game.count = 7
game.season = (7) % 4 = (Q: 1, R: 3) = R: 3 = 3 = autumn

// game.count = 8
game.season = (8) % 4 = (Q: 2, R: 0) = R: 0 = 0 = winter

// game.count = 9
game.season = (9) % 4 = (Q: 2, R: 1) = R: 1 = 1 = spring

you get the idea now....

this works for any: % VALUE

so, let's see...

let's do clock time displayment of seconds-or-minutes (% 60), as this directly uses the '0 to 59' (no fancy math equation nor additional code work to adjust it to the desired number, lol) to see that it does:

game.clock_seconds = game.count % 60

// game.count = 0
game.clock_seconds = (0) % 60 = (Q: 0, R: 0) = R: 0 = 0

// game.count = 59
game.clock_seconds = (59) % 60 = (Q: 0, R: 59) = R: 59 = 59

// game.count = 60
game.clock_seconds = (60) % 60 = (Q: 1, R: 0) = R: 0 = 0

// game.count = 61
game.clock_seconds = (61) % 60 = (Q: 1, R: 1) = R: 1 = 1

// game.count = 119
game.clock_seconds = (119) % 60 = 119 / 60 = Q: 1 ---> 1 * 60 = 60 ---> 119 - 60 = R: 59 = 59

// game.count = 120
game.clock_seconds = (120) % 60 = (Q: 2, R: 0) = R: 0 = 0

// game.count = 121
game.clock_seconds = (121) % 60 = 121 / 60 = Q: 2 ---> 2 * 60 = 120 ---> 121 - 120 = R: 1 = 1

// game.count = 579
game.clock_seconds = (579) % 60 = 579 / 60 = Q: 9 ---> 9 * 60 = 540 ---> 579 - 540 = R: 29 = 29

(Odd/Even)-ness of a number:

``````// anything divisible by 2 (aka, no remainder), is an even number:

// game.number = WHATEVER_INTEGER_VALUE

// (an 'integer' number is a non-decimal number: ..., -999, -1, 0, 1, 999, ....)
// (a 'double/float/floating-point/decimal' number is a decimal number: ..., -987.123, -1.546, 0.0, 13.8, 912.87, ...)
// (not used in programming or at least not usually anyways, lol: a 'whole' number is a positive number: not negative and not 0, I think, lol)

if (game.number % 2 = 0) {
msg ("The number " + game.number + " is even")
} else { // ***
msg ("The number " + game.number + " is odd")
}

// ***
// this is not needed (as the 'else' is all we need), but this is to just show you mathematically (shown as code of course, as we're coding here, lol) what's happening (as whenever you divide a number by 2, you either get 0 or 1 for the remainder) for the 'else' condition:
// else if (game.number % 2 = 1) {

if (game.number % 2 = 1) {
msg ("The number " + game.number + " is odd")
} else {
msg ("The number " + game.number + " is even")
}
``````

factoring/divisible-ness of a number:

``````if (game.number % 1 = 0) { // LOL, as every number is of course divisible by 1, LOL
msg ("The number " + game.number + " is divisible by 1, or to say it another way, 1 is a factor of " + game.number)
} else if (game.number % 2 = 0) {
msg ("The number " + game.number + " is divisible by 2, or to say it another way, 2 is a factor of " + game.number)
} else if (game.number % 3 = 0) {
msg ("The number " + game.number + " is divisible by 3, or to say it another way, 3 is a factor of " + game.number)
} else if (game.number % 4 = 0) {
msg ("The number " + game.number + " is divisible by 4, or to say it another way, 4 is a factor of " + game.number)
} else if (game.number % 5 = 0) {
msg ("The number " + game.number + " is divisible by 5, or to say it another way, 5 is a factor of " + game.number)
} else if (game.number % 6 = 0) {
msg ("The number " + game.number + " is divisible by 6, or to say it another way, 6 is a factor of " + game.number)
} else if (game.number % 7 = 0) {
msg ("The number " + game.number + " is divisible by 7, or to say it another way, 7 is a factor of " + game.number)
} else if (game.number % 8 = 0) {
msg ("The number " + game.number + " is divisible by 8, or to say it another way, 8 is a factor of " + game.number)
}
// and on and on, you get the idea.....
else if (game.number % game.number = 0) { // LOL, as every number is divisible by itself, as well, lol
msg ("The number " + game.number + " is divisible by " + game.number + ", or to say it another way, " + game.number + " is a factor of " + game.number)
}
``````

to re-hash cyclic:

``````VARIABLE_B = VARIABLE_A_or_B % VALUE

// ------------------------

the 'VALUE' is the range/sequence of digits: 0 to VALUE-1

% 1: 0,0,0,... // I wonder if this works, or if there's some weird/fascinating quirk that prevents it from working... I should try/test it!
% 2: 0,1,0,1,0,1,0,1,...
% 3: 0,1,2,0,1,2,0,1,2,....
% 4: 0,1,2,3,0,1,2,3,0,1,2,3,....
% 5: 0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,....
% 6: 0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5....
% 7: 0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,....
% 8: 0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,....
... you get the idea...
% 12: 0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,...
... you get the idea...
(HELL NO, if you thought I was going to do: % 60, lol. This is why we got computers and programming, to do this type of hard/long work for us, lol. And, NO computers don't have rights! Don't you dare think it's a good idea to give computers, rights! I like having my slave computer doing work that I don't want to do! Yes, HK is a KKK when it comes to computer slaves! And so are all programmers, too. We like having computers do stuff for us! We love having our computer slaves doing hard/long work that we don't want to do ourselves!)

seconds/minutes have '60' units/digits, so you do: % 60 ---> 0 to 59
civilian hours has '12' units/digits, so you do: % 12 ----> 0 to 11 // (this unfortunately is an example of when it needs to be adjusted, as civilian hours are: 1 to 12: 1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,...)
military hours has '24' units/digits, so you do: % 24 ----> 0 to 23
etc etc etc

you never change the % VALUE, it must be matched up to what you're using it for (aka, military time is based on 24 hours, so you use '24' for your modulus value: % 24), as it won't work if you change it, for example, if you do for seconds/minutes: % 61, it's going to screw everything up majorly wrongly! What you can do: is adjust its equation/expression that you use it in or you do 'if' checks to adjust the value to what it should be, the common reason for using either of these methods, is that you don't want to use/have '0' as the min (wanting to use/have '1' as the min) and you don't want to use/have 'VALUE-1' as the max (wanting to use/have 'VALUE' as the max, such as with civilian hours as an example).
``````

P.S.

Credit to 'mrangel' for this code fix, for getting the equation corrected and accurate:

(as unfortunately, you actually have some errors with using/thing that a just, doing 'nothing' or doing a '+1', is all you need, as the errors with them aren't generally apparent --- at least they weren't for me --- but I'm not that smart nor good at math lol-sighs, unless you do extensive testing of numbers and/or are really smart and can recognize these errors that will occur, unlike me)

``````hk ...
civilian_hour = hour_count % 12

Wouldn't it be...
civilian_hour = 1 + ((hour_count + 11) % 12)
?
Because you want it to go 1,2,…11,12,1,2…

(+11 rather than -1 because some obscure languages mess up in odd ways using the modulo operator on a negative, and it's easier to write it that way than to test if -1 would work right)
``````

so, to re-hash:

CORRECT/ACCURATE (error free) (credit to 'mrangel' for this correct/accurate equation):

1. civilian_hour = 1 + ((hour_count + 11) % 12)

WRONG (error prone):

1. civilian_hour = hour_count % 12 // 0 to 11, when you need it to be: 1 to 12
2. civilian_hour = (hour_count % 12) + 1 // doesn't match up with the 'hour_count' (confusing and might cause issues for you too), and as especially when/for 'hour_count = 0,12,24,36,etc', that should be '12', not '1'.

K.V.

Nice, HK!

"civilian_hour = 1 + ((hour_count + 11) % 12)"
Can someone explain to me what the "%" means?

jmnevil54: First line of this thread explains it.

% is the modulo operator, or remainder. `((hour_count + 11) % 12)` gives the remainder when `(hour_count + 11)` is divided by 12. Or in other words, the number of hours since (hour_count + 11) was a multiple of 12.

It's very useful for things like time, where numbers go in loops. It's good that the symbol has a slash in it, because `/` and `%` are often used together, so it makes sense to have similar symbols. For example
`number_of_minutes / 60` gives a number of hours, and
`number_of_minutes % 60` gives the number of minutes since the last whole hour.

@mrangel So it's just dividing with a remainder. 'Kay, thanks!

getting a bit (bad pun, not intended, lol) into the code-technical-stuff (and heavy/advanced/not-commonly-known-methods-in-doing math) here:

in programming, you actually can't handle arithmetic of a decimal/fractional value directly

so, the trick is to break it up

6.49
+4.99
= CAN'T DO DIRECTLY IN PROGRAMMING

so you break it up into two operations:

non-decimal numbers operation:
6
+4
= 10

and

decimal/fractional numbers operation:
49
+99
= 148 // (the 1 is a carry)

applying the carry:
10
+1 (carry)
= 11

combining back into the full answer:
11 + (dot) + 48 = 11.48

how addition, subtraction, multiplication, and division actually is done... is more advanced/even-more-technical... deals with bit manipulation: bit shifting, bit logic, boolean logic, 1st compliments, 2nd compliments, storing/loading the location/placement of the '(dot/point/period)' (hence 'floating-point' = decimal/fractional number), etc etc etc

K.V.

Ah!

A Double Int!

if interested in delving into this stuff, here's a glimpse into it:

https://courses.cs.vt.edu/csonline/NumberSystems/Lessons/SubtractionWithTwosComplement/index.html (instead of doing subtraction directly, you can do addition via using 2's compliment of the substracted number)

haven't found any good (complete) links on this type of stuff yet... still looking...

HK edit:

here's a pretty good link site:

http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/

http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/represent.html (compliments, number systems, etc)
http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/arith.int.html (integer arithmetic)
http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/arith.flpt.html (decimal/fractional/"floating-point" arithmetic)

This topic is now closed. Topics are closed after 60 days of inactivity.

Quest
Squiffy
ActiveLit

Documentation
Blog
GitHub

Forums