C++/Simple Math

From Wikiversity
< C++
Jump to: navigation, search

Simple C++ Math[edit]

Math in C++ is very simple. Keep in mind that C++ mathematical operations follow a particular order much the same as high school math. For example, multiplication and division take precedence over addition and subtraction. The order in which these operations are evaluated can be changed using parentheses.

Adding, Subtracting, Multiplying and Dividing[edit]

#include <iostream>

using namespace std;

int main()
{
    int myInt = 100;

    myInt = myInt / 10; //myInt is now 10
    myInt = myInt * 10; //myInt is back to 100
    myInt = myInt + 50; //myInt is up to 150
    myInt = myInt - 50; //myInt is back to where it started

    myInt = myInt + 100 * 2; // myInt is now 300 because multiplication takes precedence over addition
    myInt = (myInt + 100) * 2; // myInt is now 800 because we have changed the precedence using parentheses

    myInt -= 10; // myInt is now 790 because this line is the short-hand for myInt = myInt - 10;
    myInt = myInt % 100; // myInt is now 90 because % is modulus operator

    cout << myInt << endl;

    cin.get();//Taking one character or waiting after displaying output
    
    return 0; //Passing message to the Operating System saying that the code has been successfully execute. 
}


//C++ arithmetic operators
// + (add)
// - (subtract)
// / (divide)
// * (multiply)
// % (modulus division) 4 % 5 = 4 the remainder is returned 6 % 5 = 1
// += (add and assign)
// -= (subtract and assign)
// /= (divide and assign)
// *= (multiply and assign)
// %= (mod and assign)

C++ math library[edit]

The C++ math library is actually C's math library. It is easy to use and is accessed by including cmath.

#include <cmath>

Math functions[edit]

Now that we have the C math library let's use some neat functions.

Square Root[edit]

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     float myFloat = 0.0f; //the f (requires decimal) tells the compiler to treat this real number as a 32 bit float
 9                           //and not as a 64 bit double. this is more of a force of habit than a requirement
10     cout << "Enter a number. ENTER: ";
11     cin >> myFloat;
12     cout << "The square root of " << myFloat << " is " << sqrt(myFloat) << endl;
13     cin.clear();
14     cin.sync();
15     cin.get();
16 
17     return 0;
18 }

Powers[edit]

 1 #include <iostream>
 2 #include <cmath>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     float myFloat = 0.0f;
 9 
10     cout << "Enter a number. ENTER: ";
11     cin >> myFloat;
12     cout << myFloat << " in the power of 2 is " << pow(myFloat, 2) << endl;
13     cout << myFloat << " in the power of 3 is " << pow(myFloat, 3) << endl;
14     cout << myFloat << " in the power of 0.5 is " << pow(myFloat, 0.5) << endl;
15     cin.clear();
16     cin.sync();
17     cin.get();
18 
19     return 0;
20 }

Trigonometry[edit]

Note: Trigonometric functions in cmath use RADIANS.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    float myFloat = 0.0f;

    cout << "enter a number. ENTER: ";
    cin >> myFloat;
    cout << "sin(" << myFloat << ") = " << sin(myFloat) 

    cout << "cos(" << myFloat << ") = " << cos(myFloat) << endl;
    cout << "tan(" << myFloat << ") = " << tan(myFloat) << endl;
    cin.clear();
    cin.sync();
    cin.get();

    return 0;
}

Where To Go Next[edit]

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science