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:20]
mithat [Floating-point division]
ch04-getting-stuff-done-i [2017/09/27 23:26] (current)
mithat
Line 86: Line 86:
 === Division === === Division ===
  
-The ''​%%/​%%''​ operator is used for multiplication. 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.+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.
  
-==== Floating-point division ​====+== Floating-point division ==
  
-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.)+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>​ <​code>​
Line 100: Line 100:
 </​code>​ </​code>​
  
-==== Integer division ====+The result of the above is that ''​%%z%%''​ holds the value 1.1 (or a value very close to that).
  
 +== Integer division ==
  
-=== modulo ===+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.
  
-TODO+<​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 ===
  
 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 119: Line 153:
  
 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 137: 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 142: 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.1504131637.txt.gz · Last modified: 2017/08/30 22:20 by mithat