Sampi

Home Older Versions Documentation Changelog Learn


Math

Sampi can also do math.


OperationExampleResult
Addition1 + 12
Subtraction3 - 12
Multiplication2 * 48
Division8 / 24
Exponentiation3^29
Negation~3-3

Math in Sampi is a littel strange. ~3 gives -3. You can't simply use -3. The biggest change is the loss of the order of operations. Normally, 2 + 3 * 4 will return 14 (2 + (3 * 4)), but in Sampi, it just goes left to right, so 2 + 3 * 4 is 20 ((2 + 3) * 4). This is very important when chaining conditions. These are the comparisons and how to chain them.


ComparisonExampleResult
Equal1 = 1true
Greater than2 > 1true
Less than2 < 3true
Greater than or equal to1 >= 1true
Less than or equal to1 <= 2true
Not equal1 != 2true

OperationExampleResult
Andtrue and falsefalse
Ortrue or falsetrue
Notnot truefalse

Condition chaining is when left to right evaluation can be annoying. If you type:


say(1 = 1 and 2 = 2)
        

You will get an error! But why? It is because instead of doing it like this: (1 = 1) and (2 = 2), Sampi evaluates it this way: ((1 = 1) and 2) = 2. It tries to see if 1 = 1 and 2, which is invalid. The fix is to run:


say(1 = 1 and (2 = 2))
        

Now it evaluates the code: (1 = 1) and (2 = 2), which is what we want! When chaining comparisons, always use parentheses.


Previous Next