Math

Lesson 9
Author : Afrixi
Last Updated : November, 2017


Computers and numbers are like bread and butter. In fact the term computer actually used to refer to people who's job it was to do calculations and "compute" things all day. Needless to say we've come a long way since then, and modern computers are capable of performing billions of calculations in the time it takes a normal human to even write a number down.

Not to nerd out here too much, but when you get down to it, all computer instructions, all the code that we've written in this course, and all the code that's currently running on your computer, is actually just a collection of complex patterns of 0s & 1s running through your processor. And that same processor that's running everything on your computer is made up of a bunch of ALUs which stands for Arithmetic Logic Unit, basically a digital circuit which does basic math really really fast!

It's because computers are good at math, that you're even able to read this website right now. So as we forge forward and learn to become Ruby developers, it will help to spend some time learning how to leverage the amazing mathematical abilities of this awesome machine we're programming.

Integers vs Floats

The first thing I'd like to discuss is a very fundamental concept which is integral to mathematics as well as computer programming, which is the difference between an integer and a float.

These are two different types of numbers that we can work with in our programs, and it's important that you have a grasp of the differences so you can use them appropriately.

Let's start with integers which are whole numbers, or counting numbers. That means both negative numbers, positive numbers and zero, as long as they don't have decimal places after them.

Integers are often used to store thing that can't be broken up, like the number of minutes in the hour (you wouldn't say it 10:42.5). They can also be used for counting the number of times something has happened. This in particular will come in handy when we look at loops later on in this course.

Unlike integers, floats are any numbers with decimal places. Even if a float is technically a whole number like 42, in order for it to be a float it would have to have a .0 at the end of it 42.0.

Floats are often used when precision is a factor, like in scientific measurements, or in many cases when representing percentages (0.0 - 1.0).

Any time you're working with numbers in your programs, you always want to be aware of the type of number you're using, be it an integer or a float. Failure to do say may result in aspects of your program not performing as intended, or math calculations not giving precise enough answers.

Math Operations

Now that we have a grasp on the different types of numbers, let use them! You can tell a computer to perform any of the 4 basic math operations, addition, subtraction, multiplication and division by simply writing out the problem like you would normally.

Note that in Ruby we use the * to indicate multiplication and the / to indicate division.

Copyputs  2 * 3         # Basic Arithmetic: +, -, /, *
puts  2**3          # Exponent
puts  10 % 3        # Modulus Op. : returns remainder of 10/3
puts  1 + 2 * 3     # order of operations
puts 10 / 3.0       # int's and doubles

Above are some common examples of math operation you might use.

Ruby will also respect order of operations, so you can group certain expressions using parenthesis to encode more complex math problems.

Modulo Operator

If you look in the code above you'll also notice an operator that you might not have seen before, indicated by a percent sign (%). This is what's referred to as the modulo operator and is a pretty common operation that is performed in Ruby programs.

The modulo operator will take the number on the left, divide it by the number on the right and then return the remainder. So if we ask the computer to perform 10 % 3 we would get back a 1, because 10/3 is 3 with a remainder of 1.

In the code above we are not making use of variables, but often times it's extra useful to store numbers in variables because numbers, unlike strings don't give us any context. If I see a random string in my program I can at least try to infer what it's purpose is by reading the text, the same can't always be said about numbers.

Advanced Math

Aside from all of the basic math operations and the modulo operation, Ruby also provides ways of doing more complex and difficult calculations with relative easy. Check out the video above for a full demonstration of advanced math in Ruby!

Video Code

Copyputs  2 * 3         # Basic Arithmetic: +, -, /, *
puts  2**3          # Exponent
puts  10 % 3        # Modulus Op. : returns remainder of 10/3
puts  1 + 2 * 3     # order of operations
puts 10 / 3.0       # int's and doubles


num = 10
num += 100          # +=, -=, /=, *=
puts num

num = -36.8
puts  num.abs()
puts  num.round()

# Math class has useful math methods
puts Math.sqrt(144)
puts Math.log(0)