December 2, 200520 yr 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
December 2, 200520 yr 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
December 2, 200520 yr 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
December 2, 200520 yr 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
December 2, 200520 yr 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
December 2, 200520 yr 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!
December 2, 200520 yr 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
December 2, 200520 yr 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
December 3, 200520 yr 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
December 3, 200520 yr 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
December 3, 200520 yr >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
December 3, 200520 yr 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,
December 3, 200520 yr Moderator >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,>><123 / 2 = 61.5, therefore:>If I wanted to extract the 1, and place it on the stack,>before your explanation I would have used,>>123 100 % rBut I am not "extract(ing) the 1" in my example. That isn't the actual purpose of the modulus, it just happens to be a side effect that occasionally happens when base ten is used as the divisor! Worse, still, 123 100 % will not be equal to 1! (see below) ;)>You see, I don't have the hang of this,Let me try simply explaining in simpler terms then...Q: What is the "leftover" after dividing one number(A) by another (:(, then multiplying the divisor (:( by the whole part of the result©, then subtracting that number from the dividend (A)?A: Take at look at this...A / B = C.xxxxB * C = DA - D = Remainder!364 / 41 = 8.9780487drop the fraction and you get this41 * 8 = 328364 - 328 = 36 remainder///////////////////////////Let's look at the previous example again, in the same way then:123 2 %123 / 2 = 61.561 * 2 = 122123 - 122 = 1 remainder///////////////////////////123 100 %123 / 100 = 1.23100 * 1 = 100123 - 100 = 23 remainder Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
December 3, 200520 yr 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 FS RTWR SHRS F-111 JoinFS Little Navmap
Create an account or sign in to comment