Difference between revisions of "Arithmetic Operators"

From Pinguino
Jump to: navigation, search
m (Addition)
(Subtraction: Pointer substraction return an integer)
Line 61: Line 61:
  
 
=== Subtraction ===
 
=== Subtraction ===
  x = 10 - 7;
+
  x = 10 - 7;                     // On integers
  y = 3.45 - 1.23;
+
  y = 3.45 - 1.23;                 // On floats
  pointer_c = pointer_a - pointer_b;
+
  pointer_c = pointer_a - offset;  // Pointer and integer used as an offset
 
+
offset = pointer_b - pointer_a; // Get offset (integer) between two pointers
Memory pointers can be added and subtracted.
 
  
 
=== Multiplication ===
 
=== Multiplication ===

Revision as of 11:17, 20 December 2011


Work in Progress

Operator Names

+ Addition x + y x add y -or- x plus y
- Subtraction x - y x subtract y -or- x minus y
* Multiplication x * y x multiplied by y -or- x times y
/ Division x / y x divided by y
% Modular Division x % y x mod y
- Negation -x negative x
++ Increment ++x or x++ increment x
-- Decrement --x or x-- decrement x

Description

Syntax

z = x + y;

z = x - y;

z = x * y;

z = x / y;

z = x % y;

z = -x;

++x;
x++;

--x;
x--;

Examples

Addition

x = 10 + 7;       // On integers
y = 1.23 + 3.45;  // On floats
p = p + 1;        // One pointer and one integer used as a byte offset

Subtraction

x = 10 - 7;                      // On integers
y = 3.45 - 1.23;                 // On floats
pointer_c = pointer_a - offset;  // Pointer and integer used as an offset
offset = pointer_b - pointer_a;  // Get offset (integer) between two pointers

Multiplication

x = 10 * 7;
y = 3.45 * 1.23;

Memory pointers cannot be multiplied or divided.

Division

x = 10 / 7;
y = 3.45 / 1.23;

Integer division of positive values truncates towards zero, so 10/7 is 1.

Memory pointers cannot be multiplied or divided.

Modular Division

x = 10 % 7;

Modular division returns the remainder produced after performing integer division, so 10%7 is 3.

Negation

z = -x;

Only provides a negative result if the operand, x, is of a signed data type.

Bugs

See also

Comments