Tutorial: Math

Basic Math

Math Operators

Operation Operator
addition +
subtraction -
multiplication *
division /
mod (remainder) %
Increment ++var | var++
Decrement --var | var--
#include <iostream>
 
using namespace std;
 
int main()
{
    cout << endl;
    cout << 10 + 3 << endl;   // addition
    cout << 10 - 3 << endl;   // subtraction
    cout << 10 * 3 << endl;   // multiplication
    cout << 10 / 3 << endl;   // integer division
    cout << 10 / 3.0 << endl; // floating point division
    return 0;
}

Integer vs. Floating Point Division

  • Division using only integers always results in an integer.
  • Division using at least one floating point number results in a floating point number.

Storing Calculations

Calculations can be stored in variables.

#include <iostream>
using namespace std;
 
int main()
{
    // declare variables to hold results of calculations
 
    int add_result;
    int sub_result;
    int mul_result;
    int int_div_result;
    float flo_div_result;
 
    add_result = 10 + 3;
    sub_result = 10 - 3;
    mul_result = 10 * 3;
    int_div_result = 10 / 3;
    flo_div_result = 10 / 3.0;
 
    cout << " add_result : " << add_result << endl;
    cout << " sub_result : " << sub_result << endl;
    cout << " mul_result : " << mul_result << endl;
    cout << " int_div_result : " << int_div_result << endl;
    cout << " flo_div_result : " << flo_div_result << endl;
 
    return 0;
}

Incrementing and Decrementing

To increment a number is to add 1 to it.

To decrement a number is to subtract 1 from it.

#include <iostream>
using namespace std;
 
int main()
{
    int my_var = 9;
    cout << " Starting Value   : " << my_var << endl;
 
    my_var = my_var + 1; //increment my_var
    cout << " Incremented      : " << my_var << endl;
 
    my_var = my_var - 1; //decrement my var
    cout << " Then Decremented : " << my_var << endl;
    return 0;
}

Incrementing and decrementing is so common in C++ that operators exist to do both operations.
If you have the inrement/decrement operator in front of the variable it returns what the value was after the increment/decrement.
But if you have the inrement/decrement operator behind the variable it returns what the value was before the increment/decrement.

In the example bellow the operator is used after the variable.

#include <iostream>
using namespace std;
 
int main()
{
    int my_var = 9;
    cout << " Starting Value   : " << my_var << endl;
 
    my_var++; //increment my_var
    cout << " Incremented      : " << my_var << endl;
 
    my_var--; //decrement my var
    cout << " Then Decremented : " << my_var << endl;
    return 0;
}

In the example bellow the operator is used before the variable.

#include <iostream>
using namespace std;
 
int main()
{
    int my_var = 9;
    cout << " Starting Value   : " << my_var << endl;
    cout << " Incremented      : " << ++my_var << endl;
    cout << " Then Decremented : " << --my_var << endl;
    return 0;
}

Order of Operations

Positive and Negative (Sign)

Numbers can be either positive or negative.

The sign shows whether a number is positive or negative.

If positive, the sign is not shown.

If negative, the sign is -.

Checking If a Number is Positive or Negative

abs() can be used to check if a number is positive or negative.

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int a_num = 5;
 
    if ( abs(a_num) == a_num)
    {
        cout << "a_num is positive." << endl;
    }
    else
    {
        cout << "a_num is negative." << endl;
    }
    return 0;
}

Math Functions

  • math.h cheat sheet lists the math functions available in math.h
Categories: Beginner Tutorials : Tutorials
page tags: _wip
page_revision: 27, last_edited: 1246723986|%e %b %Y, %H:%M %Z (%O ago)