SIMPOL Documentation

Arithmetic Operators

Arithmetic operators are commonly used to apply mathematical calculations to the items on either side of the operator and to return some result. In SIMPOL, the arithmetic operators do not necessarily need to apply to numbers or integers. There are a number of useful ways that strings can be used together with arithmetic operators as will be seen from the documentation that follows.

Unary Minus (–)

Operand count

1

Types

.nul, .inf, blob, boolean, integer, number, string

Description

Negates a value

Behavior by type
Type name Result when unary minus () is applied to this type
.nulThe result will be .nul
.infThe result will be .inf
blob The result is the reverse of the operand, ie. the bytes of the result are the bytes of the input operand but in the opposite order
boolean The result is the same as the operand, ie. –.true is .true and –.false is .false
integerThe result is the negation of the operand
numberThe result is the negation of the operand
string The result is the reverse of the operand, ie. the characters of the result are the characters of the input operand but in the opposite order

Subtraction (–)

Operand count

2

Types

.nul, .inf, boolean, integer, number, string

Description

Subtracts one operand from the other. The operands can be of any type, but an error will result if string, integer, number or boolean types are mixed.

Behavior by type
Type name Result when subtraction () is applied to this type
.nulIf any operand is .nul then the result will be .nul
.inf If any operand is .inf and no operand is .nul then the result will be .inf
boolean If the operands are the same then the result is .false, otherwise it is .true
integerThe result is the difference between the operands
numberThe result is the difference between the operands
string The result is the first string with all copies of the second string removed, for example "abcdefabc" - "bc" is "adefa"

Addition (+)

Operand count

2 or more

Types

.nul, .inf, blob, boolean, integer, number, string

Description

Calculates the sum of the operands. The operands can be of any type, but an error will result if string, blob, integer, number or boolean types are mixed.

Behavior by type
Type name Result when addition (+) is applied to this type
.nulIf any operand is .nul then the result will be .nul
.inf If any operand is .inf and no operand is .nul then the result will be .inf
blobIf all operands are blobs then the result is the concatenation of them
boolean If all the operands are boolean then the result is .true if any operand is .true, otherwise the result is .false
integerIf all operands are integers then the result is their sum
numberIf all operands are integers or numbers then the result is their sum
stringIf all operands are strings then the result is the concatenation of them

Multiplication (*)

Operand count

2 or more

Types

.nul, .inf, blob, boolean, integer, number, string

Description

Calculates the product of the operands. The operands can be of any type, but an error will result if more than one operand is a string or a blob.

Behavior by type
Type name Result when multiplication (*) is applied to this type
.nulIf any operand is .nul then the result will be .nul
.inf If any operand is .inf and no operand is .nul then the result will be .inf
blob Only one operand can be a blob. If one of the operands is a blob then the result will be a blob. If all the other operands are integers or numbers or the .true boolean value then the result is the blob operand replicated the number of times given by the product of the integer and number operands. If the product of the integer operands is negative then the blob result is reversed.
boolean If all operands are boolean then the result will be boolean. If any of the operands are .false then the result will be .false, the empty string or zero. Any operands which are .true do not affect the result unless all operands are .true, in which case the result will be .true
integer If there are no string, number, .nul or .inf operands, and there is at least one integer operand, then the result will be an integer. The result of multiplying integer operands is their numerical product.
number If there are no string, number, .nul or .inf operands, and there is at least one number operand, then the result will be an integer or a number. The result of multiplying integer or number operands is their numerical product.
string Only one operand can be a string. If one of the operands is a string then the result will be a string. If all the other operands are integers or numbers or the .true boolean value then the result is the string operand replicated the number of times given by the product of the integer and number operands. If the product of the integer operands is negative then the string result is reversed.

Division (/)

Operand count

2

Types

.nul, .inf, integer, number

Description

Divides one operand by another.

Behavior by type
Type name Result when division (/) is applied to this type
.nulIf either operand is .nul then the result will be .nul
.inf Dividing any number or .inf by .inf gives the result .nul. Dividing .inf by any number except .nul gives the result .inf.
integerThe result is the quotient of the operands
numberThe result is the quotient of the operands

Modulus (mod)

Operand count

2

Types

.nul, .inf, integer, number

Description

Calculate the remainder when one number is divided by another.

Behavior by type
Type name Result when modulus (mod) is applied to this type
.nulIf either operand is .nul then the result will be .nul
.inf Using modulus on any number or .inf by .inf gives the result .nul. Using modulus on .inf with any number except .nul gives the result .inf.
integer The result is the remainder when the largest possible number of second operands are taken from the first operand. If the result is non-zero then it will be of the same sign as the first operand.
number The result is the remainder when the largest possible number of second operands are taken from the first operand. If the result is non-zero then it will be of the same sign as the first operand.