C++/Simple Math
< C++
Simple C++ Math
editMath 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 executed.
}
//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
editThe C++ math library is actually C's math library. It is easy to use and is accessed by including cmath.
#include <cmath>
Math functions
editNow that we have the C math library let's use some neat functions.
Square Root
edit#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float myFloat = 0.0f; //the f (requires decimal) tells the compiler to treat this real number as a 32 bit float
//and not as a 64 bit double. this is more of a force of habit than a requirement
cout << "Enter a number. ENTER: ";
cin >> myFloat;
cout << "The square root of " << myFloat << " is " << sqrt(myFloat) << endl;
cin.clear();
cin.sync();
cin.get();
return 0;
}
Powers
edit#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float myFloat = 0.0f;
cout << "Enter a number. ENTER: ";
cin >> myFloat;
cout << myFloat << " in the power of 2 is " << pow(myFloat, 2) << endl;
cout << myFloat << " in the power of 3 is " << pow(myFloat, 3) << endl;
cout << myFloat << " in the power of 0.5 is " << pow(myFloat, 0.5) << endl;
cin.clear();
cin.sync();
cin.get();
return 0;
}
Trigonometry
editNote: 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) << endl;
cout << "cos(" << myFloat << ") = " << cos(myFloat) << endl;
cout << "tan(" << myFloat << ") = " << tan(myFloat) << endl;
cin.clear();
cin.sync();
cin.get();
return 0;
}
Where To Go Next
editTopics in C++ | ||
Beginners | Data Structures | Advanced |
---|---|---|
Template loop detected: C++/Lessons/Beginners |
Template loop detected: C++/Lessons/Data Structures |
Template loop detected: C++/Lessons/Advanced |
Part of the School of Computer Science |