## DEV Community is a community of 787,776 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Andrey Frolov

Posted on • Updated on

# Elixir base arithmetic operations π§ͺ

Performing math operations and calculus is a very common thing to do with any programming language. Elixir offers several operators to help us work with numbers

Performing math operations and calculus is a very common thing to do with any programming language.

Elixir offers several operators to help us work with numbers.

• Addition (+)
• Subtraction (-)
• Division (/)
• Remainder (%)
• Multiplication (*)
• Exponentiation (**)

## Addition (+)

`````` 1 + 2
# => 3
``````

But the + operator doesn't serve as string concatenation if you use strings, so pay attention:

``````"asdas" + "asdas"
# => ** (ArithmeticError) bad argument in arithmetic # expression: 'asdas' + 'asdas'
#     :erlang.+('asdas', 'asdas')
``````

## Subtraction (-)

Same as addition.

``````3-2
# => 1
``````

## Division (/)

Returns the quotient of the first operator and the second:

Take a note, the result is a Float type, not an Integer. Operator / always return a Float, but if you want to do Integer division use div function.

``````20 / 5
# => 4.0 (not a 4)

div(20,5)
# => 4
``````

If you try to divide by 0. You'll get an error.

``````1 / 0

# => ** (ArithmeticError) bad argument in arithmetic
# expression: 1 / 0
#    :erlang./(1, 0)
``````

## Remainder (rem)

The remainder is a very useful calculation in many use cases:

``````rem 20, 5
# => 0

rem 20, 7
# => 6

rem 1, 0
# => ** (ArithmeticError) bad argument in arithmetic #expression: rem(1, 0)
#    :erlang.rem(1, 0)
``````

## Multiplication

Multiply two numbers

``````1 * 2 # => 2
-1 * 2 # => -2
``````

## Exponentiation (**)

Raise the first operand to the power second operand. You can use math module

``````:math.pow(2, 3)
# => 8

:math.pow(3, 2)
# => 9
``````

One exception I found is that when raising a base number to an exponent, you probably donβt want to use the :math.pow/2 function.

``````:math.pow(42, 909)
# => ** (ArithmeticError) bad argument in arithmetic # expression
#    (stdlib) :math.pow(42, 909)
``````