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:28]
mithat [Mixing numeric types]
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 ====
Line 100: Line 100:
 </​code>​ </​code>​
  
 +The result of the above is that ''​%%z%%''​ holds the value 1.1 (or a value very close to that).
 ==== Integer division ==== ==== 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.+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.
  
  
Line 123: Line 132:
  
 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