To contents Next section

5.1 Arithmetic operators

The arithmetic operators are the simplest ones, since they work just like you remember from math in school. The arithmetic operators are:

Function Syntax Identifier Returns
Addition a + b `+ the sum of a and b
Subtraction a - b `- b subtracted from a
Negation - a `- minus a
Multiplication a * b `* a multiplied by b
Division a / b `/ a divided by b
Modulo a % b `% the remainder of a division between a and b

The third column, "Identifier" is the name of the function that actually evaluates the operation. For instance, a + b can also be written as `+(a, b). I will show you how useful this can be at the end of this chapter.

When applied to integers or floats these operators do exactly what they are supposed to do. The only operator in the list not known from basic math is the modulo operator. The modulo operator returns the remainder from an integer division. It is the same as calculating a - floor(a / b) * b. floor rounds the value down to closest lower integer value. Note that the call to floor isn't needed when operating on integers, since dividing two integers will return the result as an integer and it is always rounded down. For instance, 8 / 3 would return 2.

If all arguments to the operator are integers, the result will also be an integer. If one is a float and the other is an integer, the result will be a float. If both arguments are float, the result will of course be a float.

However, there are more types in Pike than integers and floats. Here is the complete list of combinations of types you can use with these operators:

Operation Returned type Returned value
int + int int the sum of the two values
float + int
int + float
float + float
 float the sum of the two values
string + string
int + string
float + string
string + int
string + float
 string In this case, any int or float is first converted to a string. Then the two strings are concatenated and the resulting string is returned.
array + array array The two arrays are concatenated into a new array and that new array is returned.
mapping + mapping mapping A mapping with all the index-value pairs from both mappings is returned. If an index is present in both mappings the index-value pair from the right mapping will be used.
multiset + multiset multiset A multiset with all the indices from both multisets is returned.
int - int int The right value subtracted from the left.
float - int
int - float
float - float
 float The right value subtracted from the left.
string - string string A copy of the left string with all occurrences of the right string removed.
array - array array A copy of the left array with all elements present in the right array removed. Example: ({2,1,4,5,3,6,7}) - ({3,5,1}) will return ({2,4,6,7}).
mapping - mapping mapping A new mapping with all index-value pairs from the left mapping, except those indices that are also present in the right mapping.
multiset - multiset multiset A copy of the left multiset without any index present in the left multiset.
int int Same as 0 - int.
float float Same as 0 - float.
int * int int the product of the two values
float * int
int * float
float * float
 float the product of the two values
array(string) * string string All the strings in the array are concatenated with the string on the right in between each string. Example: ({"foo","bar"})*"-" will return "foo-bar".
array(array) * array array All the arrays in the left array are concatenated with the array on the right in between each array. Example: ({ ({"foo"}) ,({"bar"})})*({"-"}) will return ({ "foo","-","bar" }).
string * int string This operation will concatenate the string N times. Example: "foo"*3 will return "foofoofoo".
array * int string This operation will concatenate the array N times. Example: ({"foo"})*3 will return ({"foo","foo","foo"}).
int / int int The right integer divided by the left integer rounded towards minus infinity.
float / int
int / float
float / float
 float The right value divided by the left value.
string / string array(string) In symmetry with the multiplication operator, the division operator can split a string into pieces. The right string will be split at every occurrence of the right string and an array containing the results will be returned. Example: "foo-bar"/"-" will return ({"foo","bar"})
string / int array(string) This will split the string into pieces. The size of the pieces is given by the integer. Only complete pieces will be included in the result, the 'reminder' is discarded. Example: "foo-bar"/2 will return ({"fo","o-","ba"})
string / float array(string) This is similar to dividing a string with an integer, but it allows fraction-sized segments and the reminder will always be included. Example: "foo-bar"/2.5 will return ({"fo","o-b","ar"})
array / int array(array) This is similar to dividing a string with an integer, but splits an array. Example: ({1,2,3,4,5,6,7})/2 will return ({({1,2}),({3,4}),({5,6})})
array / float array(array) You should be able to predict what this does by now. :) Example: ({1,2,3,4,5,6,7,8})/2.5 will return ({({1,2}),({3,4,5}),({6,7}),({8})})
int % int int The remainder of a division. If a and b are integers, a%b is the same as a-(a/b)*b
float % float
int % float
float % int
 float The remainder of a division. If a and b are floats, a%b is the same as a-floor(a/b)*b
string % int string The remainder of a string division. Example: "foo-bar"%2 will return "r"
array % int string The remainder of an array division. Example: ({1,2,3,4,5,6,7})%2 will return ({7})

To contents Next section