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
Next revision Both sides next revision
ch04-getting-stuff-done-i [2017/08/30 22:23]
mithat [Integer division]
ch04-getting-stuff-done-i [2017/08/30 22:42]
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.)
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).
  
-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.+== 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>​
  
-=== modulo ​===+The result of the above is that ''​%%z%%''​ holds the value 1. 
 + 
 +<WRAP center round box 80%> 
 +=== Geekbreak: WTF division? === 
 +TODO FIXME 
 +</​WRAP>​ 
 + 
 +=== Modulus ​===
  
 TODO TODO
  
-==== 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 123: Line 137:
  
 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 ====
ch04-getting-stuff-done-i.txt · Last modified: 2017/09/27 23:26 by mithat