ExDiceRoller v1.0.0-rc.2 ExDiceRoller.Compilers.Math View Source

Handles compiling expressions using common mathematical operators.

iex> {:ok, tokens} = ExDiceRoller.Tokenizer.tokenize("1+x")
{:ok, [{:int, 1, '1'}, {:basic_operator, 1, '+'}, {:var, 1, 'x'}]}
iex> {:ok, parse_tree} = ExDiceRoller.Parser.parse(tokens)
{:ok, {{:operator, '+'}, 1, {:var, 'x'}}}
iex> fun = ExDiceRoller.Compilers.Math.compile(parse_tree)
iex> fun.([x: 2])
3
iex> fun.([x: 2.4])
3.4

ExDiceRoller uses infix notation when working with mathematical operators. Below is the list of operators currently supported by ExDiceRoller:

  • +: adds the values on both sides of the expression
  • -: subtracts the value on the right from the value on the left
  • *: multiplies the values on both sides of the expression
  • /: divides, with the left value as the dividend, the right the divisor
  • %: modulo, with the left the dividend, the right the divisor
  • ^: exponentiation, with the left the base, the right the exponent

Link to this section Summary

Functions

Function used for division calculations

Function used for modulo calculations. Only accepts integer values

Link to this section Functions

Function used for division calculations.

Function used for modulo calculations. Only accepts integer values.