This shows you the differences between two versions of the page.
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:06] mithat [Assignment] |
ch04-getting-stuff-done-i [2017/08/30 22:42] mithat |
||
---|---|---|---|
Line 56: | 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. |
+ | |||
+ | == 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.) | ||
+ | |||
+ | <code> | ||
+ | float x = 2.2; | ||
+ | int y = 2; | ||
+ | double z; | ||
+ | |||
+ | 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? === | ||
+ | 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 84: | 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 ==== |