The arithmetic operation module

Saturday, May 31, 2008

The "form" is a mathematical operation available on virtually all programming languages (I used the first time with Basic on the Commodore 64) . May appear as a simple operator, as in the case of Actionscript or Javascript that both use the character "percent" (a% b), or as a method or education. Developer may be useful in contexts apparently different. Gskinner spoke recently, showing some classic but interesting examples.

Simplistically, the modulo of two numbers returns the rest of their division. For completeness, we say that the transaction module is a very broad topic, which I treated in the RSA encryption! This time, however, do not talk of codes or ciphers, but useful and much simpler.

Divisible by ... Alternative

The first use that we can make the transaction module is to determine whether a number is divisible by a number b! If risultatto of a% b (% I will use here the notation to indicate the operation of the module) is zero, a is divisible by b. I remember when I had to do with a problem like I did not know the transaction module. Commodore 64 I needed to know if a number was divisible by 2. At the time I used this syntax (I found the code ORIGINAL - year 1983):

CODE:
  1. 10 INPUT A
  2. : IF B = INT ( B ) THEN PRINT "OK IL NUMERO " A " E' PARI" :GOTO 10 12 B = A / 2: IF B = INT (B) THEN PRINT "THE NUMBER OK" A "AND 'EQUAL": GOTO 10
  3. A " E' DISPARI" :GOTO 10 14 PRINT "THE NUMBER" A "and 'Odd": GOTO 10

BASIC on the Commodore 64, however, did not have the transaction form - at least that I remember!

Basically what I was doing exactly that by its nature makes the operation of the form: Verify that the division by 2 did not rest, in this case, check that the result did not contain a decimal IF B = INT(B) If the operation of division is equal to its full, then the number is equal!

If at a value that we have our own control, the same thing can be resolved by:

ActionScript
  1. a % 2 ) == 0 ) { if ((a% 2) == 0) (
  2. / / Equal
  3. )

Or, for the 3, by:

ActionScript
  1. a % 3 ) == 0 ) { if ((a% 3) == 0) (
  2. / / Divisible by 3
  3. )

Little note: thanks to the track if you have to do with the powers of 2 is annche a faster way to check whether a number is even or odd. In binary notation, in fact, the even numbers all have the first bit to zero! Thus, using the logical operators, you can know whether a number is odd or even simply "Protestant" the first bit:

ActionScript
  1. ! ( a & 1 ) ) { if ((v & 1)) (
  2. / / Equal
  3. )

In the Assembly, for example, this is our daily bread! It is even more immediate and simple (the Motorola 68020, for example, had an instruction BTST that "tested" just a single bit ;) ). Logical operations (AND, OR, NOT, XOR, etc ...), in fact, are known to be faster at running and thus the "time machine"! (a & 1) performs the logical operation of AND & - multiplying bitwise) between our value a and 1 (called mask). If a = 7 (00000111), for example:

CODE:
  1. 00000111 AND
  2. 00000001 =
  3. 00000001; odd

If a = 8:

CODE:
  1. 00001000 AND
  2. 00000001 =
  3. 00000000; equal

Not only can we verify the even numbers, ie the first bit to 0, and therefore divisible by 2, but can also occur if a number is divisible by 4, 8, 16, 32, 64, etc ... using the appropriate forms:

ActionScript
  1. ! ( a & 3 ) ) { if ((a & 3)) (
  2. / / Divisible by 4 (= 3 mask = 00000011) since 4 is 00000100)
  3. )

If the last two bits are zero the number is divisible by 4! If the last 3 bits (mask 00000111) are at zero the number is divisible by 8! And so :)

Gskinner shows the use of the module in case of generation of alternative values. Classic example is to change the background of a list of elements: a white and gray. Gskinner proposes:

ActionScript
  1. rowIndex % 2 == 0 ) { if (rowIndex% 2 == 0) (
  2. rowColor = 0xFFFFFF;
  3. { ) Else (
  4. rowColor = 0xCCCCCC;
  5. )

First of all the code above can be solved with a single line of code:

ActionScript
  1. 2 == 0 ) ? 0xFFFFFF : 0xCCCCCC ; rowColor = (rowIndex% 2 == 0)? 0xFFFFFF: 0xCCCCCC;

Moreover, in this particular case, I would avoid actually performing a "heavy" operation module. This particular situation is often engaged in loop, and thus would be "right" not to waste clock cycles machine. In these cases I like the cleaner (and logical) solution:

ActionScript
  1. fooIndex == false ) ) ? 0xFFFFFF : 0xCCCCCC ; rowColor = (fooIndex = (fooIndex == false))? 0xFFFFFF: 0xCCCCCC;

Where fooIndex is a value set to precedentemete true or false depending on how we initially set our background color. This approach is more subtle and less immediate, at first, but faster because it performs logical operations of true / false. If fooIndex is true when the statement is executed (fooIndex = (fooIndex == false) ) the condition (fooIndex == false) is false and therefore fooIndex is set from true to false. The second time, however, the condition (fooIndex == false) will be true and then fooIndex is set from false to true! And so on ...

Homework: The behavior to watch

Arithmetic over, that underlies the functions of the form, is often referred to as clock arithmetic. A clock classic clock has a dial divided into 12 hours (12 day plus 12 night!), Sufficient to indicate one of every 24 hours a day. At 08:00 am If you ask what time your watch will score between 18 hours, your answer would be 8 +18 = 26! But the 02:00 at night! Perhaps inadvertently and without knowing it, we perform operations conitnuamente form whenever we deal with the timetable.

In Italy we are used to the system for 24 hours (or mixed). When we point to 16:00 intend quell'orario than in other countries is indicated as 04:00 pm. Who is using postfix am / pm hours Diuna to distinguish from those of the night, running more often the transaction module, in this case form 12! We Italians, and others, however, being accustomed to consider the 24 hours, we perform operations with Form 24!

When are the 11 am (11:00 for all) and think of "between 5 hours", the Italians respond 5 +11 = 16 (four in the afternoon), an alien may also respond 5 +11 = 4 pm. Since 5 + 11, classical arithmetic, is obviously 16, as did the stranger to meet 4 pm? Simply run the module 12 on the result: 5 + 11 = 16 mod 12 = 4. It is not necessary that you take a calculator to verify the accuracy of computation, but these steps are a bit 'all; un'orologio displayed a face of mental attitude and the hands on the 11, then add - always visually - 5 hours and, as by magic you'll actually about 4! That's why four in the afternoon are called 16 :)

In Italy, however, we use a Form 24, in fact: 11 +5 = 16 mod 24 = 16. When I am to 23:00 pm, after 5 hours will not be the 28:00, because 28 modulo 12 = 4! 122514884225 Form 24 = 17! What happens is that the figure of the result modulo 24 never exceeds 24. But the most interesting thing is that the numbers are repeated indefinitely:

CODE:
  1. = 1 1% 24 = 1
  2. = 2 2% 24 = 2
  3. = 3 3% 24 = 3
  4. ...
  5. = 22 22% 24 = 22
  6. = 23 23% 24 = 23
  7. = 0 24% 24 = 0
  8. = 1 25% 24 = 1
  9. = 2 26% 24 = 2
  10. = 3 27% 24 = 3
  11. ...
  12. = 0 48% 24 = 0
  13. = 1 49% 24 = 1
  14. = 2 50% 24 = 2
  15. = 3 51% 24 = 3
  16. ...

This repetition is a comfort disarming. Imagine, for example, that it should have an object, a MovieClip, like a chessboard, in four columns. Create a MovieClip quandrato, 55x55 pixels and panel esportiamolo property with the name Simbolo The following code will have our MovieClip on four columns, and will "wrap" thanks to the operation of the module:

ActionScript
  1. Number = 4 ; var col: Number = 4;
  2. var i= 0 ; i < 12 ; i ++ ) { for (var i = 0; i <12; i + +) (
  3. Sprite = new Simbolo ( ) ; var s: Sprite = new Symbol ();
  4. s ) ; addChild (s);
  5. = ( i % col ) * 60 ; s. x = (i% col) * 60;
  6. = Math . floor ( i / col ) * 60 ; s. y = Math. floor (i / col) * 60;
  7. )

As shown in the code, the variable i of the cycle for growing indefinitely. But the coordinate x of our MovieClip never exceeds a certain value, and repeats indefinitely! Here is a concrete example:

Loading Flash Player ...

Using the two knobs you can vary the number of columns (module) and the total number of elements, using the algorithm of overtaking.

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

One comment to "The arithmetic operation module

  1. getAvatar 1.0

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL