Skip to content

Math

Eventra does its sums inside math(…). Anything with +, -, * or / goes in there, so you can always spot where a calculation happens.

get answer = math(_2 + _3) // your calculation goes inside the brackets
run:
get apples = _5
get pears = _3
system.log:message("Fruit:", math($apples + $pears))
system.log:message("Half:", math($apples / _2))
close:
What it prints
Fruit: 8
Half: 2.5

Inside math(), Eventra works things out in the order you learned at school: brackets first, then * and /, then + and -. Brackets change the order:

run:
system.log:message(math(_2 + _3 * _4))
system.log:message(math((_2 + _3) * _4))
close:
What it prints
14
20

** raises a number to a power, so _2 ** _3 is 2 × 2 × 2. It’s worked out before * and /. When there are two, the right one goes first:

run:
system.log:message(math(_2 ** _3))
system.log:message(math(_2 ** _3 ** _2))
close:
What it prints
8
512

Put - in front of a number or a variable to make it negative:

run:
get temperature = _3
system.log:message(math(-$temperature - _5))
close:
What it prints
-8

To change a variable by an amount, use += and its friends. coins += _5 is short for coins = math($coins + _5):

run:
get coins = _10
coins += _5
system.log:message("After +=", $coins)
coins -= _3
system.log:message("After -=", $coins)
coins *= _2
system.log:message("After *=", $coins)
coins /= _4
system.log:message("After /=", $coins)
close:
What it prints
After += 15
After -= 12
After *= 24
After /= 6.0

math.round gives the nearest whole number. Give it a second number, the step, and it rounds to the nearest multiple of that instead: the nearest 25, or the nearest 0.01.

run:
system.log:message(math.round(_7.6))
system.log:message(math.round(_237, _25))
system.log:message(math.round(_3.14159, _0.01))
close:
What it prints
8
225
3.14

Three more work the same way, each going a different direction:

  • math.floor always goes down,
  • math.ceil always goes up,
  • math.trunc goes towards zero, so it just drops what’s after the point.

math.trunc works like math.floor on positive numbers, and like math.ceil on negative ones. Negative numbers are where they’re easiest to mix up:

run:
system.log:message(math.floor(-_7.5), math.ceil(-_7.5), math.trunc(-_7.5), math.round(-_7.5))
close:
What it prints
-8 -7 -7 -8

math.random(low, high) picks a whole number from low to high, both included, so math.random(_1, _6) is a dice roll: 1, 2, 3, 4, 5 or 6. It’s random, so each run is different. The panel here shows one roll. Yours will be whatever comes up.

run:
get roll = math.random(_1, _6)
system.log:message("You rolled", $roll)
close:
What it prints
You rolled 4

Keeping sums inside math() means a stray + can’t sneak into a line by accident: a calculation always looks like one. If you do write one outside, Eventra doesn’t guess what you meant. It stops before running anything and tells you where to put the brackets.

Whole numbers stay whole with +, - and *, and / always gives a decimal. Rounding is how you get back to a whole number when you need one, like a score or a number of lives.

Arithmetic outside math(). It’s the most common one.

run:
get apples = _5
system.log:message($apples + _1)
close:
What Eventra says
Eventra syntax error (line 3): arithmetic has to go inside math(...) — write math(... + ...) instead

Fix: system.log:message(math($apples + _1)).

A typo in a variable’s name.

run:
get apples = _5
system.log:message(math($aples + _1))
close:
What Eventra says
Eventra runtime error (line 3): variable 'aples' has not been declared — write 'get aples = ...' before using $aples

Fix: spell it the way it was declared, $apples.

Dividing by zero.

run:
get people = _0
system.log:message(math(_10 / $people))
close:
What Eventra says
Eventra runtime error (line 3): division by zero

Fix: check first, with CheckIf($people != _0) then do (see Conditions).

Writing **=. There’s a +=, but no **=.

run:
get size = _2
size **= _3
close:
What Eventra says
Eventra syntax error (line 3): Eventra has no '**=' — write it out: size = math($size ** _3)

Fix: the message shows you what to write instead.

A number that’s too big. Eventra’s whole numbers go up to 9223372036854775807.

run:
get stars = _9223372036854775808
close:
What Eventra says
Eventra syntax error (line 2): 9223372036854775808 is too big for a whole number — the biggest is 9223372036854775807. For a bigger number, write it as a decimal: 9223372036854775808.0

Fix: if you really need a number that big, the message shows you how to write it.

Next: Conditions →, doing something only when it’s true.