C++ gives you a lot of ways to do math, built into the language directly. Unfortunately, it can get complex – so we’re going to focus on the simple stuff.
The order of operation in C++ is the same as in algebra, so that is easy to remember.
- + is addition
- – is subtraction
- * is multiplication
- / is division
- % is modulus – getting the remainder of two numbers.
- pow(a,b) is a function in the cmath library which can be used to determine the value of a number raised to a power.
Math Example:
#include <iostream>
using namespace std;
int main()
{
int start = 10; // from a simulated seconds counter
int end = 356; // from a simulated seconds counter
int duration = end - start;
int minutes = duration / 60;
int remainingSeconds = duration % 60;
cout << "With a duration of " << duration << " seconds, or if you prefer,"
<< endl << minutes << " minutes and " << remainingSeconds << " seconds."
<< endl;
return 0;
}
Note: One thing you have to be careful for in C++ is division. Division between two integers will give an integer value, even if it shouldn’t. Consider the following:
cout << 9 / 5 << endl; // prints 1 - because both numbers are integers
cout << 9.0 / 5 << endl; // prints 1.8 - because 9.0 becomes a float
Augmented Assignments
Augmented assignments is a form of shortcuts we can take to make writing code a little faster. They were found in C, and most languages which are based off of C, or one of it’s derivatives have them.
Consider the code i = i + 3;
. There is a lot of redundancy in that line of code. If you wanted a short cut, you could write i += 3;
and it has the exact same meaning.
In fact, with any of the math operators, you can specify the operator and then the assignment equal, and skip having to specify the initial variable name again.
length *= 2; // doubles the length
indexPoint /= 2; // cuts the value in half
randomCounter += 8; // adds 8 to randomCounter
finalValue -= 3; // subtracts 3 from finalValue
otherVariableName %= 2; // even works with modulus
For simple variable names it may not seem like much, but in complicated programs with longer variable names, it does matter.
Increment and Decrement Operators
If you need to increment, or decrement, a variable by one, there is even a faster short cut. Sure you could specify i = i + 1;
or even i += 1;
, however, you can also simply specify i++;
(Notice the double +.)
It’s fast to write, in some versions of compilers it’s faster to run, and readily recognizable.
The decrement version is i--;
, assuming that i is the variable you are using. (Notice that it’s a double -.)
These are often found in counting loops as you’ll see in future lessons.
One of the things that can confuse people however, is that the increment, or decrement, can either precede or be after the variable, and this changes the meaning, especially when used as part of a larger equation such as is found below:
int i, j;
i = 10;
j = i++; // j = 10
i = 10;
j = ++i; // j = 11
If the ++ or the — is before the variable, it is done first, before any other assignment or mathematical operation. If it is after the variable name, it happens after everything else has completed.
Casting Number Types
Since C++ is specific about it’s data types, we have to cast numbers if we need to move an int into a double or vice versa. The compiler will do implicit casting for you automatically. You can also use static_cast<data type> to explicitly change data types.
C++ Math was originally found on Access 2 Learn