Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Arithmetic

arithmetic.livemd

Arithmetic

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Overview

Arithmetic is a branch of mathematics that involves the manipulation of numbers. Often functional programming languages like Elixir get a reputation for being very math-heavy. The same goes for programming in general, but especially functional programming.

Let’s dispel that myth. While some programming fields require advanced math skills, many do not. Generally, web development only requires simple arithmetic (adding, subtracting, dividing, multiplying, etc), and even then, we let the computers do most of the heavy lifting!

Rest assured, this will not be a math course, nor will most real-world applications demand advanced math knowledge.

Now, with that said, computers were originally built to perform computations that humans could not perform easily or at all. So out of the box, we can perform mathematical calculations.

Humans have been building mathematical machines for ages and the computers that run our programs are by far the most powerful!

Integers

Integers are whole numbers, that also include negative numbers.

flowchart LR
... --> -5 --> -4 --> -3 --> -2 --> -1 --> 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> 6[...]

You can use integers directly in your programs.

1
1023
1235460
-1
-100
-123512

Often you’ll use integers for representing ages, days, years, cash values, and more.

Your Turn

In the Elixir cell below, replace 0 the biggest integer you can think of (until you get bored).

We’ll show you how many digits the number has when you re-evaluate the Elixir cell.

If you’ve used any other programming language, you might be surprised! Integers in Elixir are unbounded, meaning they have no maximum size.

Contrast that with a language like JavaScript which has a maximum safe integer size of 9007199254740991. Elixir makes that look tiny!

biggest_integer = 0

Utils.visual(:biggest_integer, biggest_integer)

You can see unbounded integers in action in the following animation. Watch how the number of digits constantly grows! Move on to the next section when you’re satisfied that integers are truly unbounded.

To avoid distorting your screen we use mathematical notation $10^{n}$ after 80 digits.

Utils.animate(:biggest_integer)

Floats

Floats refer to floating-point numbers.

Floats are used to represent decimal numbers in your program like 1.22, 2.3, 1.0, and even 0.0.

flowchart LR
  ... --> -0.0001 --> -0.001 --> -0.01 --> -0.1 --> 0.0 --> 0.1 --> 0.01 --> 0.001 --> 0.0001 --> a[...]

You can use floats directly in your program just like with integers.

0.0
3.5
12123.123123123

Floats can have varying precision. The precision of a float is the number of digits after the decimal point.

For example, the precision of the float 1.2 is one. the precision of the float 1.22 is two.

You’ll often use floats for representing percentages. For example, you might want to represent a progress bar as a percentage.

Your Turn

In the Elixir cell below, try changing the float percentage to any float between 0.0 and 100.0 and see how the loading bar changes! Replace 50.0 with your answer and reevaluate the cell.

percentage = 50.0

Utils.visual(:loading_bar, percentage)

Floating-Point Errors

Take great care when performing calculations with floats.

Computers are limited to how precise they can be when representing floating-point numbers. Therefore, it’s possible to have unexpected results!

For example, what do you think 1.2 + 1.4 returns? Probably 2.6 right? Nope! You’ll see the actual answer is 2.5999999999999996 in the Elixir cell below.

1.2 + 1.4

Why do computers have these errors? Well that requires a fairly in-depth understanding of how the computer hardware works under the hood. For our purposes it’s enough to be aware that it happens.

If you are curious, Computerphile has an excellent video on why

Kino.YouTube.new("https://www.youtube.com/watch?v=PZRI1IfStY0")

Due to these errors, do not use floats to represent money even though it might seem intuitive to translate $1.29 into 1.29.

Instead, you’ll often use integers to represent pennies so $1.29 becomes 129.

Alternatively, the Money library provide utilities for working with money.

Your Turn

In the Elixir cell below, enter some floating-point addition that returns an unexpected result.

Arithmetic Operators

Using arithmetic operators, we can add +, subtract -, multiply * and divide /. We also have two special arithmetic operators, div/2 for integer division and rem/2 for remainder.

Arithmetic operators manipulate both positive and negative integers and floats.

As previously mentioned in the lesson on floats, arithmetic operations on floats may result in floating-point errors.

Addition

7 + 5

You can create entire equations by writing operators one after the other. So long as there is a valid number (integer or float) on the operator’s left and right hand side.

4 + 3 + 2 + 1

The following code will crash because there is a dangling + operator. The error says TokenMissingError and expression is incomplete because we did not complete the mathematical expression. Whenever our code crashes, Elixir lets you know why with an error message.

2 +

As mentioned, adding floats together may result in floating point errors, so only use them when a small amount of innaccuracy isn’t an issue.

1.2 + 1.4

You can use floats and integers together in arithmetic expressions.

1.2 + 4
1.5 + 3.5 + 3 + 4.1 + 9

You can also add negative and positive numbers together. Adding a negative number is the same as subtracting a positive number.

8 + -9

Adding negative numbers might feel unintuitive, so it’s rare to write code like this, but it’s useful to know you can.

Your Turn

In the Elixir cell below, add two massive numbers together that you couldn’t do in your head.

Subtraction

We can subtract numbers with the minus - operator.

20 - 12

Like with addition, we can subtract floats and integers in any combination.

2.5 - 10
10 - 2.5 - 2.1

Floating-point errors can still occur.

1.3 - 1.2

Subtracting a negative is the same as adding a positive.

10 - -1

Your Turn

In the Elixir cell below, subtract two massive numbers together.

Multiplication

Computers can efficiently multiply numbers. Under the hood, multiplication is just repeated addition. For example, 5 * 5 is 5 + 5 + 5 + 5 + 5.

10 * 10

Multiplication can still result in floating-point errors.

2.2 * 2.1

You can multiply integers and floats both positive and negative in combination, like with addition and subtraction.

2 * 30 * -2 * 1.4

Your Turn

In the Elixir cell below, multiply two massive numbers together.

Division

We can also divide numbers. However, in Elixir there are two operators for division. There is the / operator, which will always return a float, and the div operator, which will always return an integer (rounded down).

10 / 5

div is a function. You will learn more about functions in the future. For now, it’s enough to know that div allows you to take two numbers and divide them.

div(10, 5)

Remember that div always rounds values down, so 1.5 becomes 1.

div(3, 2)

There’s a limit to how precise numbers can be in any computer program, so you should always take care when using fractions that don’t divide evenly. You can also run into the same floating-point calculation issues discussed in the floats lesson.

For example, notice that 10 / 3 is 3.3333333333333335.

10 / 3

Your Turn

In the Elixir cell below, divide 223 by 71 using / to get a nice slice of pi. Your answer should be close to 3.14.

Divide 100 by 3 using div. Your answer should be 33.

Exponents

The exponent operator in Elixir is **.

That means that 10 ** 2 is 10 to the power of 2. Mathematically that’s the same as 10 * 10.

10 ** 3 means 10 * 10 * 10. If you would like a more in-depth explanation of exponents, there’s a video by Khan Academy.

Kino.YouTube.new("https://www.youtube.com/watch?v=XZRQhkii0h0")
10 ** 2

Your Turn

In the Elixir cell below, find 10 to the power of 214. Replace nil with your answer.

result = nil

Utils.feedback(:exponents, result)

BEDMAS

Calculations in Elixir follow the BEDMAS order of operations. BEDMAS stands for brackets, exponents, division, multiplication, addition, and subtraction.

That means that the following will evaluate 5 * 2 before 5 + 5.

5 + 5 * 2

So it first evaluates into.

5 + 10

To get the result of 15.

15

Here’s the same statement in an Elixir cell so that you can confirm the result is 15.

5 + 5 * 2

Had it been evaluated from left to right, the result would have been 20.

5 + 5 * 2
10 * 2
20

You might notice the B in BEDMAS stands for brackets. You can use brackets in your program override the order of operations.

(5 + 5) * 2
(10) * 2
20

Here’s the same statement in an Elixir cell for you to confirm its result is 20.

(5 + 5) * 2

Your Turn

In the Elixir cell below, Use brackets () to alter the return value of the expression.

answer = 20 + 20 * 20

Utils.feedback(:bedmas, answer)

Remainder

A remainder is the amount left when you divide one number by the other. For example, if you divide 5 by 2, the remainder is 1.

That’s because 2 goes into 5 twice and leaves a remainder of 1.

If you’re not familiar with remainders, here’s a video by Khan Academy.

Kino.YouTube.new("https://www.youtube.com/watch?v=P1qyjdh_sIw")
rem(5, 2)

In programming, we can use remainders to check if a number is evenly divisible by a number. For example, a number is even if rem(number, 2) equals 0.

rem(6, 2)

Where as an odd number will have a remainder of 1.

rem(5, 2)

You’ll also use remainders to cycle through numbers but then reset back to 0 when you hit a limit. You don’t need to know how to do this yet, but it will be helpful in future lessons.

Here we have an animation to show the remainder of a growing number divided by 10 to help you visualize this effect.

Utils.animate(:remainder)

Your Turn

In the Elixir cell below, find the remainder of 10 divided by 3 using rem/2. Replace nil with your answer.

remainder = nil

Utils.feedback(:remainder, remainder)

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish arithmetic section"