Recommended Posts

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

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

>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

Share this post


Link to post
Share on other sites

>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... ;)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

>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

Share this post


Link to post
Share on other sites

>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... ;)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

>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

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this