4.0 Blah! TWO! TWO MATHMATICAL OPERATORS!! MUHAHAHAHAH!!
Doing math is often used as a logical test as much as … doing any math. You can do just so many useful things with simple mathmatical formulae. Some are obvious, some a little strange. Its just a question of getting used to the tools you are using.
4.1 The Basics
Addition, subtraction, division and multiplication are general deal with the obvious symbols; “+”, “-“, “/” and “*” respectivly. In some languages, such as pascal, if you want to only do INTERGER division, you have to use the word “DIV” instead. In other languages, such as C/C++, it is controled by what memeory variables you are using.
4.2 Conquering the Divisions
Division is important concept in many cases, because of the potential problems involved. When you divide floating-point numbers, you will introduce a rounding error in the result depending on how big the number you are using (look up at memory variable sections again). Another part is that computers get really upset when you try to divide by zero. Hardware and software are a lot better now to catch this thing, but you should always check to make sure if your divisor is zero if there is the remote posibilty of it being zero.
4.3 Other functions
Modulus is the operation that tells you the remainder of dividing two numbers. So 10 modulus 5 would be zero, but 12 modulus 5 would be 2. In C/C++ and Java this is done buy the percent symbol “%” but in languages like pascal and basic, the word “MOD” is used.
Raising a number to a power or taking the square root of any number is general done with functions. Most languages will have an available math library to supply this functionality.
Equality and Assignment are often confused. For C/C++ and Java, the symbol to test if two values are equal is always two equal signs “==” and to assign one value to another value is only one equal sign “=”. This is often a source of confusion. Languages like Pascal use a sepcial “:=” symbol to show assignment and a single “=” to show equality. Languages like basic use “=” for both equality and assignment.
In some very rare cases you will need to shift the bits in a variable. If you look back to the “encoding” section, you can see that everything is stored in bit form, usually in groups of eight bits. But if you look at the four bit list up there….say the number 4, is 0100. Lets say you move all the bits over left by just one, you’d get 1000, or 8. What did that just do? Yep, multplied the number by 2. What if you moved the bits over right by one, you’d get 0010 or 2. What did that do? Divided the number by 2. So what “Shift Bits Left” and “Shift Bits Right” does is to either multply or divide the bits by 2. This has the unique benefit of being very VERY fast. In most languages the command is shl for left and shr for right.
4.4 Enough rope to…play jumprope
One of the quirks to many languages, like C/C++, is the fact that they give you a ton-o-power..yet basically a lot of “rope to accidentally hang yourself with” as the cliché goes. There are some tricks and such for C/C++ that work, but should be used carefully.
If statements are very common and often only deal with simple statements. If this, then assign this and so forth. If you have a simple if condition and want to assign some value or another based upon that condition, then you can use this instead:
( (conditional) ? (true value) : (false value) )
Basically, if the first parameter (which MUST be a boolean result, 0 or 1—true or false), resolves to be true, then the 2nd parameter is returned. If the result is false, then the 3rd parameter is returned.
Another actual useful command is the pre and post incrementor. If you want to increment or decriment a variable by just one, simply add the ++ or -- to the variable. “x++” or “q--“ are perfect examples.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment