Author : Mike Dane
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 C++ 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.
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.
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.
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 C++ we use the
* to indicate multiplication and the
/ to indicate division.
cout << 2 * 3 << endl; // Basic Arithmetic: +, -, /, * cout << 10 % 3 << endl; // Modulus Op. : returns remainder of 10/3 cout << 1 + 2 * 3 << endl; // order of operations cout << 10 / 3.0 << endl; // int's and doubles
Above are some common examples of math operation you might use.
C++ will also respect order of operations, so you can group certain expressions using parenthesis to encode more complex math problems.
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 C++ 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.
Aside from all of the basic math operations and the modulo operation, C++ 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 C++!
cout << 2 * 3 << endl; // Basic Arithmetic: +, -, /, * cout << 10 % 3 << endl; // Modulus Op. : returns remainder of 10/3 cout << 1 + 2 * 3 << endl; // order of operations cout << 10 / 3.0 << endl; // int's and doubles int num = 10; num += 100; // +=, -=, /=, *= cout << num << endl; num++; cout << num << endl; // #include <cmath> cout << pow(2, 3) << endl; cout << sqrt(144) << endl; cout << round(2.7) << endl;