Arithmetic Operators

From Pinguino
Jump to: navigation, search


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;

x += y;      // equivalent to x = x + y
z = x - y;

x -= y;      // equivalent to x = x - y
z = x * y;

x *= y;      // equivalent to x = x * y
z = x / y;

x /= y;      // equivalent to x = x / y
z = x % y;

x %= y;      // equivalent to x = 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