Skip to content
View in the app

A better way to browse. Learn more.

The AVSIM Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Modulus?

Featured Replies

Hi all,I understand modulus to a degree, like123 10 % strips out the 3 and places it on the stack.what does, say, 2 % do?cheers,nick

  • Replies 63
  • Views 6.6k
  • Created
  • Last Reply

Top Posters In This Topic

  • Author

>Hi all,>>I understand modulus to a degree, like>>123 10 % strips out the 3 and places it on the stack.>>what does, say, 2 % do?>cheers,>nickHi,Modulus divides by its divisor and takes the rest123 2 % gives 5 --> 123 /2 = 61.5 then takes the 5Tom

  • Moderator

>Hi all,>>I understand modulus to a degree, like>>123 10 % strips out the 3 and places it on the stack.>>what does, say, 2 % do?The "result" is only obvious whenever you use 10 as a divisor, because it effectively bitshifts the decimal, and the remainder is simply the decimal portion! ;)With other numbers as a divisor however, the operation is more opaque... :( The modulus, or remainder, operator divides number1 by number2 and returns only the remainder as result. The sign of result is the same as the sign of number1. The value of result is between 0 and the absolute value of number2.For example, in the following expression, A (which is result) equals 5.6. A = 19 6.7 %This is equivalent to:19 / 6.7 = 2.8358208We throw away the fraction leaving 2 as the resultNow we have:A = 19 - (6.7 * 2)Therefore A = 5.6The above is an example where the result is certainly not obvious! :( The "%" (or "mod") operator in computer languages is simply the remainder. For example, 17 3 % = 2because 17 / 3 = 5 rem 2which in turn means 17 = 3 * 5 + 2There are some tricky issues when negative numbers are used, but that shouldn't ordinarily be necessary.In math (number theory), the term is used a little differently. The "modulus" is actually not the remainder, but the number you are dividing by; and "mod" is not an operator, but a label telling "in what sense two quantities are considered congruent, or equal." For example, we would say 17 = 11 (mod 3)(read as "17 is congruent to 11, modulo 3"), meaning that 17 and 11 both leave the SAME remainder when divided by 3. You probably won't see this usage if you are only reading about programming, but it's worth being aware of if you look deeper into the math behind it.The expression a = b (% n) means that n is a divisor of a - b. Thissentence is read, "a is congruent to b modulo n." It is somethinglike a remainder, because if you subtract a remainder from thedividend, the divisor will go into the result evenly.Examples: 100 = 86 (% 7), because 100 - 86 = 14 has 7 as a divisor.On the other hand, if you divide 100 by 7, the quotient is 14 andremainder is 2, and 100 = 2 (% 7), too.Now, back to your original question, what would 123 2 % be?A = 123 2 %123 / 2 = 61.5, therefore:A = 123 - (61 * 2)A = 1I hope that this is helpful! It's probably a lot more than you wanted to know... ;)

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
  • Moderator

>Modulus divides by its divisor and takes the rest>>123 2 % gives 5 --> 123 /2 = 61.5 then takes the 5Erm... nope. See above... ;-)

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

In C "The % operator cannot be applied to float or double" (K & R).That certainly applies to Borland C++Builder which won't let me evaluate 19 % 6.7

Gerry Howard

  • Author

Ooops! thank you Bill, I always thought it was just the fractional rest. Lucky I, like seems to be Nick's case, only needed to use it for decimal moudulus so far...Your explanation was VERY helpful indeed :-)TomPD: Never is too late to learn basic things!

  • Moderator

>In C "The % operator cannot be applied to float or double" (K>& R).>>That certainly applies to Borland C++Builder which won't let>me evaluate 19 % 6.7That is correct. If either of the operands is a float, then you'd need to use the fmod function:Example// crt_fmod.c/* This program displays a floating-point remainder. */#include #include int main( void ){ double w = -10.0, x = 3.0, z; z = fmod( w, x ); printf( "The remainder of %.2f / %.2f is %fn", w, x, z );}OutputThe remainder of -10.00 / 3.00 is -1.000000

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
  • Moderator

>Ooops! thank you Bill, I always thought it was just the>fractional rest. Lucky I, like seems to be Nick's case, only>needed to use it for decimal moudulus so far...>>Your explanation was VERY helpful indeed :-)>PD: Never is too late to learn basic things!No problem! Quite honestly, I had to go back and refresh my own, now decades old memory, so it was a useful exercise for me as well... ;)

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator
  • Moderator

>What does XML use?Nick's first question cited the XML syntax:123 10 %or generally:x y % (>L:MyModulus,number)

Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

I realised that. What I should have asked is the XML % the same as C % or C fmod(), ie is it integer only or not?

Gerry Howard

>Ooops! thank you Bill, I always thought it was just the>fractional rest. Lucky I, like seems to be Nick's case, only>needed to use it for decimal moudulus so far...>>Your explanation was VERY helpful indeed :-)>Tom Tom, Note I use '%' in my Jet Test.xml file. I count up by one every dos tick and then do a modulo divide on the count by different constants I defined. When the result == 0, there is no remainder. So, dividing by '5' generates a bool strobe every 5 ticks. I then created if{ blocks of long blocks of XML code that are executed only when StrobeX is True. A: Vars, etc. that don't need to be read every tick, and some associated calculations are in the slower if{ blocks. At the top of the Display windows, I show Count, and a string of bools. Such as '011001', That was so I could see that the stobes were operating. The right most one only flashes every 7 seconds or so. Used to Strobe my Fuel Tank Temperature, which has a lot of transendentals and doesn't need to be calculated very often. In fact, every one minute would be plenty fast. Ages ago, module division was commonly used in calculating row and column to write directly to the display. Nowadays 3D cards do a lot of processing internally, and who knows the inner details. One can also get a decimal result for the quotient of a pair of integers with only integer coding. Something like doing 'long division', where one continues to get and use the remainder of the previous calculation. Hardly needed nowadays, since all PC's have Floating Point hardware in the CPU. Ron

Thanks Bill, more than I needed but very interesting. It's good to know the foundations that the logic is based on.Ref the example and your logic,

Modulus was also taught as a precursor to fractions and decimals in elementary education. 2nd to 3rd grades. We young ones just knew it as a "remainder". No flame on intelligenece here,, just memory of it from many many years back. Regards,Romanhttp://forums.avsim.net/user_files/134768.jpg

20AUG21_Avsim_Sig.png?dl=1  FS RTWR   SHRS F-111   JoinFS   Little Navmap 
 

 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.