Jump to content

C++/simple Math


ThriNity

Recommended Posts

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     cout << myInt << endl;     cin.get();//Taking one character or waiting after displaying output}
//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
#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
#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

Note: Trigonometric functions in cmath use RADIANS.

#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;}
  • Like 1
  • Love 1
Link to comment
Share on other sites

  • 3 weeks later...
  • 11 months later...
  • 1 month later...
  • 2 weeks later...
  • 3 months later...
  • XorEax locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.

 

AdBlock Extension Detected!

depositphotos_220325684-stock-illustration-hand-holding-mobile-with-ad.jpg

 

Our website is made possible by displaying online Advertisements to our members.

Please disable AdBlock browser Extension first, to be able to use our Community.

You won't be able to access this page.

I've Disabled AdBlock