User Tools

Site Tools


ch04-getting-stuff-done-i

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ch04-getting-stuff-done-i [2017/08/30 22:00]
mithat [Statements]
ch04-getting-stuff-done-i [2017/09/27 23:26] (current)
mithat
Line 31: Line 31:
 $12 + 30$ $12 + 30$
  
-the operator, represented by the plus symbol, indicates what you want to do with the operands ''​%%12%%''​ and ''​%%30%%''​. The concept of an operation in imperative languages is taken directly from the concept in mathematics--although the notation and details can and do differ.+the operator, represented by the plus symbol, indicates what you want to do with the operands ''​%%12%%''​ and ''​%%30%%''​. The concept of an operation in imperative languages is taken directly from the concept in mathematics---although the notation and details can and do differ.
  
 ==== Arithmetic operators ==== ==== Arithmetic operators ====
Line 40: Line 40:
 |''​%%+%%'' ​ |addition ​     |''​%%a + b%%''​| |''​%%+%%'' ​ |addition ​     |''​%%a + b%%''​|
 |''​%%-%%'' ​ |subtraction ​  ​|''​%%b - d%%''​| |''​%%-%%'' ​ |subtraction ​  ​|''​%%b - d%%''​|
 +|''​%%-%%'' ​ |negation ​     |''​%%-b%%''​|
 |''​%%*%%'' ​ |multiplication|''​%%c * d%%''​| |''​%%*%%'' ​ |multiplication|''​%%c * d%%''​|
 |''​%%/​%%'' ​ |division ​     |''​%%d / c%%''​| |''​%%/​%%'' ​ |division ​     |''​%%d / c%%''​|
Line 55: Line 56:
 foo + bar foo + bar
 </​code>​ </​code>​
 +
 === Subtraction === === Subtraction ===
  
-TODO+The ''​%%-%%''​ operator is used for subtraction. The two operands must be (or evaluate to) numeric values. 
 + 
 +<code java> 
 +foo - 5 
 +foo - bar 
 +</​code>​ 
 + 
 +=== Negation === 
 + 
 +The ''​%%-%%''​ operator is also used for numeric negation. It operates on one operand that must be (or evaluate to) a number. It evaluates to the numeric inverse of the operand. 
 + 
 +<code java> 
 +-foo 
 +-(2 * foo) 
 +</​code>​
  
 === Multiplication === === Multiplication ===
  
-TODO+The ''​%%*%%''​ operator is used for multiplication. The two operands must be (or evaluate to) numeric values.
  
-=== Integer versus floating-point division. ===+<code java> 
 +foo * 5 
 +foo * bar 
 +</​code>​
  
-TODO+=== Division ===
  
-=== modulo ===+The ''​%%/​%%''​ operator is used for division. The two operands must be (or evaluate to) numeric values. The behavior of the ''​%%/​%%''​ operator varies depending on whether the operands are integer or floating-point types.
  
-TODO+== Floating-point division ==
  
-==== Assignment ​====+When at least one of the operands in a ''​%%/​%%''​ operation is a floating-point type, the result will also be a floating-point type and have the expected value to the extent possible by the limited precision of floating-point math. (Computers do not have infinite precision, so in general small errors will occur.) 
 + 
 +<​code>​ 
 +float x 2.2; 
 +int y 2; 
 +double z; 
 + 
 +x / y; 
 +</​code>​ 
 + 
 +The result of the above is that ''​%%z%%''​ holds the value 1.1 (or a value very close to that). 
 + 
 +== Integer division == 
 + 
 +When both operands in a ''​%%/​%%''​ operation are integer types, the result will be an integer type and truncating division will take place. In truncating division, the fractional part of the result (i.e., the stuff after the decimal point) will be thrown away. The result will not be rounded to the nearest integer value; whatever the fractional part happens to be will simply be eliminated. 
 + 
 +<​code>​ 
 +int x = 5; 
 +int y = 3; 
 +int z; 
 + 
 +z = x / y; 
 +</​code>​ 
 + 
 +The result of the above is that ''​%%z%%''​ holds the value 1. 
 + 
 +<WRAP center round box 80%> 
 +=== Geekbreak: WTF division? === 
 +Who on earth thought that truncating division would ever be a good idea? 
 + 
 +In the early days of computing, floating-point arithmetic was a very costly proposition. You either needed to tie up the central processing unit with complicated algorithms to churn out floating-point results or you needed to install dedicated floating-point math hardware (typically referred to as floating-point units). These were often expensive and power hungry beasts. So, it was natural in programming languages to specify two ways of doing the most costly of the floating-point operations: division. Today, all but the most basic processors have built-in floating-point units, so floating-point math isn't nearly as big an issue as it used to be. But it still takes many processors less time to perform integer math operations than it does floating-point operations. 
 + 
 +Some modern languages, like Python 3, define //all// division as floating-point division by default. If you want to perform truncating division, you have to explicitly ask for it. JavaScript takes this one step further: it doesn'​t differentiate between floating-point and integer types at all; that is, in JavaScript there is only one numeric type. 
 + 
 +The potential performance advantage of integer math is particularly important in real-time processes---including animations. Therefore, it's a good idea in Processing to be in the habit of not using floating-point variables and operations unless you have a good reason to do so. 
 +</​WRAP>​ 
 + 
 +=== Modulus === 
 + 
 +The ''​%%%%%''​ operator is used for the modulus operator. The modulus operator operates on two operands that must be (or evaluate to) integer (not just numeric) values. ​ Despite appearances,​ this operation has nothing to do with percentages. The operation produces the //​remainder//​ that results from the division of the two operands. 
 + 
 +<​code>​ 
 +int x = 5; 
 +int y = 3; 
 +int z; 
 + 
 +z = x % y; 
 +</​code>​ 
 + 
 +The result of the above is that ''​%%z%%''​ holds the value 2. ((To be pedantic, there is a difference between modulus and remainder, but it only applies to negative numbers. FIXME)) 
 + 
 +=== Assignment ​===
  
 Assignment is an operation on two operands that sets the value of one of the operands (typically a variable) to the value of the other operand. In Processing, the equals symbol is used to indicate assignment. In the example below, the value 8 is assigned to ''​%%foo%%'',​ then twice the value of ''​%%baz%%''​ is assigned to ''​%%bar%%''​. Assignment is an operation on two operands that sets the value of one of the operands (typically a variable) to the value of the other operand. In Processing, the equals symbol is used to indicate assignment. In the example below, the value 8 is assigned to ''​%%foo%%'',​ then twice the value of ''​%%baz%%''​ is assigned to ''​%%bar%%''​.
Line 79: Line 149:
 bar = 2 * baz; bar = 2 * baz;
 </​code>​ </​code>​
-The assignment operation in Processing looks like the "​equals"​ relationship in math, and this is can be a source of confusion to new programmers//They are not the same thing.// "​Equals"​ is a statement of fact: two things are the same. Assignment is an operation: it does something--specifically,​ it copies the value of whatever is on the right into what is on the left.+ 
 +The assignment operation in Processing looks like the "​equals"​ relationship in math. This is can be a source of confusion to new programmers, but //they are not the same thing!// "​Equals"​ is a statement of fact: two things are the same. Assignment is an operation: it does something---specifically,​ it copies the value of whatever is on the right into what is on the left.
  
 Assignment is one way that the state of a program can be changed. Assignment is one way that the state of a program can be changed.
 +
 +-------------------------------------
  
 ==== Mixing numeric types ==== ==== Mixing numeric types ====
  
 +FIXME TODO
 ==== Logical operators ==== ==== Logical operators ====
  
-TODO+FIXME TODO
  
 ==== The number of operands ==== ==== The number of operands ====
Line 100: Line 174:
  
 Theoretically,​ it's possible for a language to define an operator with an arbitrary number of operands, but I am not aware of any operators that take more than three operands in Processing. Theoretically,​ it's possible for a language to define an operator with an arbitrary number of operands, but I am not aware of any operators that take more than three operands in Processing.
 +
 +----------------------------------
  
 ===== Expressions ===== ===== Expressions =====
Line 105: Line 181:
 Expressions are made of up one or more operations and return a value. Expressions are made of up one or more operations and return a value.
  
-TODO+FIXME TODO
  
 ==== Order of precedence ==== ==== Order of precedence ====
  
-TODO+FIXME TODO
  
 ==== Whitespace ==== ==== Whitespace ====
  
-TODO+FIXME TODO
  
 (Spaces, tabs, and the invisible characters that mark the end of a line. In short, whitespace in Processing (and Java, C++, and C) is interchangeable. In other words, from Processing’s point of view, a space is the same thing as a tab is the same thing as the end of a line.) (Spaces, tabs, and the invisible characters that mark the end of a line. In short, whitespace in Processing (and Java, C++, and C) is interchangeable. In other words, from Processing’s point of view, a space is the same thing as a tab is the same thing as the end of a line.)
  
  
ch04-getting-stuff-done-i.1504130430.txt.gz · Last modified: 2017/08/30 22:00 by mithat