Wikiversity
C++/Simple Math
< C++
Simple C++ Math
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
#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;
[Link]();//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
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
Now that we have the C math library let's use some neat functions.
Square Root
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 [Link]();
14 [Link]();
15 [Link]();
16
17 return 0;
18 }
Powers
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 [Link]();
16 [Link]();
17 [Link]();
18
19 return 0;
20 }
Trigonometry
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) << endl;
cout << "cos(" << myFloat << ") = " << cos(myFloat) << endl;
cout << "tan(" << myFloat << ") = " << tan(myFloat) << endl;
[Link]();
[Link]();
[Link]();
return 0;
}
Where To Go Next
Topics in C++
Beginners Data Structures Advanced
Lesson 1: Introduction to C++ Lesson 8: Pointers Lesson 12: The STL
Lesson 2: Variables and User Input Lesson 9: Classes and Inheritance Lesson 13: STL Algorithms
Lesson 3: Simple Math Lesson 10: Templates 1
Lesson 4: Conditional Statements Lesson 11: Templates 2
Lesson 5: Loops
Lesson 6: Functions and Recursion
Lesson 7: More Functions
Part of the School of Computer Science
Retrieved from "[Link]
Last edited 2 months ago by an anonymous user
Content is available under CC BY-SA 3.0 unless otherwise noted.