Advertisement
omgmonkey

Untitled

Mar 13th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. Math Expressions
  2.  
  3. In your last homework, you did some research into terms we use to describe how computers do math, and help explain what computer math is not quite the same as human math. We need to practice wit this a bit, even before we start using a real programming language.
  4.  
  5. Expressions
  6.  
  7. You should be familiar with these things. You take a bunch of “terms” and place math “operators” between them to explain what kind of math you want. Here are some examples:
  8.  
  9. 5 + 2
  10. 3.0 / 2
  11. 2 + 2 + 2 / 3
  12. The question we need to explore is what do all those “expressions” “evaluate” to? Each one needs to be run through our computer’s calculator to see what final number we get.
  13.  
  14. The huge new thing we need to understand is that we are using two completely different kinds of numbers here:
  15.  
  16. Integer numbers - which cannot have a fractional part
  17. Floating point numbers - which do (well, might) have a fractional part
  18. These two kinds of numbers do not mix, and cannot be combined together in a math operation without converting things to the same kind of number.
  19.  
  20. Da Rulz
  21.  
  22. Integer only expressions never produce floating point results!
  23. Here is a classic example:
  24.  
  25. What is 5 / 2?
  26. Most of you will say 2.5. WRONG! The answer is 2!
  27.  
  28. Why?
  29.  
  30. There is no fraction in the result, because both numbers are integers. Those are displayed with no decimal point. They are called “whole numbers”. This is the math you learned in elementary school:
  31.  
  32. How many times does 2 fit into 5?
  33.  
  34. The answer is 2. And you have one left over!
  35.  
  36. Note The “remainder after division is actually used in computing. The operator that gives you that remainder is called the “modulus” or “mod” operator. We will explore that later.
  37. More Rulz
  38.  
  39. Different types of numbers do not mix!
  40. What is 5.0 / 2?
  41.  
  42. Here, we have a floating point number divided by an integer. What should the computer do? The answer is to convert the integer to a floating point number, then do the math. This time we get 2.5, like you thought you would earlier!
  43.  
  44. Operator Precedence
  45.  
  46. There is one aspect of computer math that takes some practice to get right.
  47.  
  48. Every operator has a “precedence” level, which determines which operator to apply first. In normal, four operator math, the multiplication and division operators have a higher precedence than addition and subtraction. Here are the levels:
  49.  
  50. multiply and divide are equal
  51. add and subtract are equal
  52. Here is how we process a more complex expression:
  53.  
  54. Scan the expression left to right.
  55. Find the highest precedence operator in the expression (left-most if several are equal)
  56. “reduce” the expression by applying that operator to the two numbers on either side.
  57. Rescan left to right and repeat the process.
  58. Here is an example:
  59.  
  60. 5 / 3 + 2 * 3 - 5 / 2
  61. The steps in reducing this to one number follow:
  62.  
  63. 1 + 2 * 3 - 5 / 2
  64. 1 + 6 - 5 / 2
  65. 1 + 6 - 2
  66. 7 - 2
  67. 5
  68. By the way. If any of those numbers was a floating point number, the whole thing becomes a floating point problem and we get a fractional result! Phew!
  69.  
  70. One More Rule!
  71.  
  72. Parentheses! You remember these things. They let you group a part of an expression. We can use those to force things to be processed the way we want, not the way operator precedence ends up doing things!
  73.  
  74. WHat would this produce?
  75.  
  76. 5 / ( 3 + 2 ) * ( 3 - 5 ) / 2
  77. Same numbers, same operators, but a completely different result.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement