Arithmetic

From GCHSWiki
Jump to: navigation, search

Arithmetic Expression An expression is a combination of one or more operands and their operators. Arithmetic expression compute numeric result and make use of the arithmetic operators.

An arithmetic expression is a combination of one or more operands and their operations. Arithmetic operators are to be followed by the order of operations (P.E.M.D.A.S.). When there are two or more operators of different priority, the highest priority operator is done first. If there are some operators of the same priority do the operators from left to right. When there are sets of parentheses inside parentheses the innermost set of parentheses is worked out first. To find the remainder of a division, the % (modulus) sign will return what is remaining after the second number goes into the first one as many times as possible.

- Addition ( + )
- Subtraction ( - )
- Multiplication ( * )
- Division( / )
- Remainder ( % )
(modulus)

Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer.

( the fractional part is discarded).
Examples:
14 / 3 = 4
8 / 12 = 0
★Division = decimal always drops (no decimal) unless:

Say you wanted to divide two numbers so that you would get the exact answer instead of the just the whole number with the fractional part discarded. If one or two of the operands to the division operator ( / ) are doubles (meaning it is a decimal number), then the result is also a double.

Examples:
7.0 / 2 = 3.5
4.0 / 2 = 2.0

Or, a more tedious way to do the same thing is simply inserting a cast in front of the first number. If you want the answer to be more precise and have a decimal, then cast a double by inserting (double) in front of the first part of the equation. If you want the answer to be less precise and have an integer answer, then insert (int) in front.

Examples:
(double) 7 / 2 = 3.5
(int) 7.5 / 2 = 3

Remainders- The remainder is similar to division, but obviously will give a different answer. It is called with the % (modulus) sign and instead of returning how many times a certain number will go into another one, it will return what is left over after the second number goes into the first one as many times as possible.

5 % 7 = 5
7 goes into 5 zero times and the remainder is 5 so the answer is 5.
2 % 1 = 0
★ To prevent the decimal from being dropped, one should make one of the integers a double.

Operator Procedure (Order of Operations)

Always follow P.E.M.D.A.S.
Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction .

1.Parentheses and Brackets -- Simplify the inside of parentheses and brackets before you deal with the exponent (if any) of the set of parentheses or remove the parentheses. 2.Exponents -- Simplify the exponent of a number or of a set of parentheses before you multiply, divide, add, or subtract it. 3.Multiplication and Division -- Simplify multiplication and division in the order that they appear from left to right. 4.Addition and Subtraction -- Simplify addition and subtraction in the order that they appear from left to right.

For example, how should the expression

5 + 6 * 7

be evaluated? Do we add 5 and 6, getting 11, then multiply by 7 , getting 77? Or do we first multiply 6 and 7 and add the product to 5, getting 47? We do the multiplication first because of the order of operation that multiplication has precedence over addition.

As another example, consider the expression

10 - 7 - 2

Two subtractions are indicated. Which is performed first? By Order of Operation, if two consecutive operators have equal precedence, they are performed left-to-right, so the correct result is 1, not 5.

What is 10 - (7 - 2)?

The above problem, while looking similar to the one before it, is in fact different. Using the order of operations, whatever is inside the parentheses comes first. After you follow the instructions in the parentheses, you have 10 - (5) which then would give you the answer of 5.

What if you have more than one set of parentheses? Example: 12 + (7 + (4 + 5))

When you have a parentheses inside a set of parentheses, always make sure that you satisfy the innermost parentheses first. In the above problem, you would first do 4 + 5 to get 9. Then, you add 7 to 9 to get 16, and finally 12 to 16 for the final answer of 28.

Rounding Sometimes, we might have a number that has a lot of numbers after the decimal point, such as "3.14159". However, we may not need all those numbers. We may only need two or three decimal places, like if we are dealing with cash. Say we want to round 3.14159 to simply 3.14. There is an easy way to do this with a method called Math.round(). To round to 3.14 we would do this:

Pi = 3.14159; roundedPi = Math.round(Pi * 100) /100d; Make sure both the numbers in bold are the same

Make sure to have the "d" after the second number or this won't work. If you want to round to 3 places, it would be:

Pi = 3.14159 roundedPi = Math.round(Pi * 1000) /1000d; This rounds to 3.142

Keep going up by factors of ten to round to more decimal places. The number of zeros will tell you how many decimal places you will be rounding to.

There is also another rounding method that works with Java & this is: public static double round(double val, int places) {

   long factor = (long)Math.pow(10,places);
 
   // Shift the decimal the correct number of places
   // to the right.
   val = val * factor;
 
   // Round to the nearest integer.
   long tmp = Math.round(val);
 
   // Shift the decimal the correct number of places
   // back to the left.
   return (double)tmp / factor;

}


Where all you need is to insert this section of code, then call the method & insert the value you wish to round as val & the number of places you wish to round to as places.

Example: private double x; x = 3.14159; round( x, 2 );

this would return 3.14