# Modulus?

## 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 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?>cheers,>nickHi,Modulus divides by its divisor and takes the rest123 2 % gives 5 --> 123 /2 = 61.5 then takes the 5Tom

##### 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 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 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 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 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 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 on other sites

What does XML use?

##### 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 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 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 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 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 on other sites

Thanks Bill,I'll tinker some more and it will sink in.I'm thinking at present that by chosing the modulus correctly, the required number can be extracted from a much longer value.Would I be correct in this? I use multiples of 10 myself to extract certain individual digits, but using the correct value of modulus could extract any required value. How would you define the ultimate use of modulus in XML coding. A worded definition would be useful.BTW, >Worse, still, 123 100 % will not be equal to 1!I don't think you saw the 'r' in the line to reverse the stack to obtain a value of 1.cheers,nick

##### Share on other sites

Hi Ron, how does one obtain a oopy of Jet Test? I've searched here and the 'other place'nick

##### Share on other sites

>Hi Ron, how does one obtain a oopy of Jet Test? I've searched>here and the 'other place'>nick 'Jet Test' is an XML 'gauge' I started before FS9 came out. I have slowly added calculations and modified the two Display windows as time goes on. I'm afraid I'd never understand the current version if I hadn't worked it out, bit by bit. It displays many of the things AFSD does, but is faster, and one can modify the Display windows by editing the strings. Or, bring up just the window that displays what he is interested in. However, to display some flight parameters, such as Drag, it requires an 'Aircraft XXX.ini' file that has to be edited with a few lines of data based on the AIR file and aircraft.cfg. While AFSD reads these files directly. I have a simpler 'Prop Test', which has the code and display lines in one xml file. It displays %HP, Prop Thrust, Efficiency, Range, and more basic parameters. I've sent various versions of Jet Test to a few individuals I know. I don't recommend it in general, due to the complexity and the need to set the 'Aircraft ...ini' file. Further, the main display shows quite a few parameters with names only an engineer might recognize. ;) Email me if you are still interested. ;) Ron

##### Share on other sites

> Email me if you are still interested. ;)Hi Ron,if you don't mind, i'm interested in it as well :DMarco

##### Share on other sites

>> Email me if you are still interested. ;)>>Hi Ron,>if you don't mind, i'm interested in it as well :D>Marco You can email me directly by clicking on my name. I've archived a recent version of Jet Test with my B707-320C FD files and a modified Lear.cfg panel which shows how to set the 'gauges'. Ron

##### Share on other sites

>>BTW, >Worse, still, 123 100 % will not be equal to 1!>I don't think you saw the 'r' in the line to reverse the stack>to obtain a value of 1.>cheers,>nick123 100 % r should give 0 or the value existent in the stack before 123, because both 123 and 100 values are extracted from the stack by the % operator.BTW, one form of "imitating" modulus in XML could be:123 2 % = 123 d 2 / int 2 * - Indeed seems easier to use %...Tom

##### Share on other sites

>123 100 % r should give 0Are you sure? Basically, 23 is 'stripped out' and placed on top of the stack, and the 1 is pushed down the stack, so a stack reversal should reinstate the 1?cheers,nick

##### Share on other sites

>>123 100 % r should give 0>Are you sure? Basically, 23 is 'stripped out' and placed on>top of the stack, and the 1 is pushed down the stack, so a>stack reversal should reinstate the 1?>cheers,>nickI am sure :-)Whatever operation you do between two values placed on the stack will mean both being extracted an replaced by the result of the operation.ie 2 3 Stack=3 22 3 * Stack=65 2 3 * Stack=6 5 5 2 3 * - Stack=-12 3 * r Stack=0 65 2 3 * r Stack=5 6 etc, etc,Tom

##### Share on other sites

123 100 % r is indeterminate on its own because it will return whatever was on the top of the stack before it was executed. Consider the following states of the stack, the top of the stack (<-)first:X (<-initially)100 (<-immediately before executing %)123X23 (<-the modulus after executing %)XX (<-after executing r)23 Incidentally what does XML do if the stack is empty and an attempt is made to access the top (nonexistent) value? In general, what does XML do in other similar error situations, such as using non-integer values for the % operators. C/C++ would throw an exception or a complile time error.

##### Share on other sites

>123 100 % r is indeterminate on its ownHowever seems so, it is not indeterminated but equals 0>because it will return whatever was on the top of the stack before >it was executedThat is correct. BUT, an empty stack will return 0 any time you command an extraction from it, either through a (>L:Var) assignment, a calculation (ie + + ), etc.Some examples (>L:Var1,number) (>L:Var2,number) (>L:Var3,number)is the same as0 (>L:Var1,number) 0 (>L:Var2,number) 0 (>L:Var3,number)+ + - (>L:Var1,number)is the same as 0 (>L:Var1,number) / (>L:Var1,number) 0 / (>L:Var1,number)1 / (>L:Var1,number)1 0 / (>L:Var1,number)ALL are the same as 0 (>L:Var1,number). And yes, even a division by 0 gives no error but 0 !!! >>Incidentally what does XML do if the stack is empty and an>attempt is made to access the top (nonexistent) value? In>general, what does XML do in other similar error situations,>such as using non-integer values for the % operators. C/C++>would throw an exception or a complile time error. There is no error here, as noted on the lines above.TomEDIT: modulus in XML will work with integer, non integer and negativevalues without any trouble.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

• Tom Allensworth,
Founder of AVSIM Online

• ### Hot Spots

• Flight Simulation's Premier Resource!

AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!