Difference between revisions of "Arithmetic Operators"

From Pinguino
Jump to: navigation, search
(Addition: Addition of two pointers does not exist.)
m (Addition)
Line 56: Line 56:
  
 
=== Addition ===
 
=== Addition ===
  x = 10 + 7;      // On integer
+
  x = 10 + 7;      // On integers
  y = 1.23 + 3.45;  // On float
+
  y = 1.23 + 3.45;  // On floats
 
  p = p + 1;        // One pointer and one integer used as a byte offset
 
  p = p + 1;        // One pointer and one integer used as a byte offset
  

Revision as of 10:14, 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;
y = 3.45 - 1.23;
pointer_c = pointer_a - pointer_b;

Memory pointers can be added and subtracted.

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