Jump to content

Simple math in C++: Difference between revisions

From IdeaWazaWiki
wikademia>Shatner
wikademia>Remi0o
mNo edit summary
Line 78: Line 78:
==Where To Go Next==
==Where To Go Next==
{{Cpp_Lessons}}
{{Cpp_Lessons}}
[[Category:C++]]
[[Category:Mathematics]]
[[Category:Programming]]
[[Category:Computer science]]

Revision as of 19:34, 14 April 2007

Simple C++ Math

Here we are in our third lesson! Finally time to get C++ working for us. Computers are meant for math, and that's what we're going to do here. So, first things first. Lets add the math libraries to our C++ project. That way we have the ability to do useful math functions such as powers, square roots and the like.

#include <math>

There we go! That easy. Now you have made many useful functions available to your program. Now, let's do some math!

Adding, Subtracting, Multiplying and Dividing

These are all very straight forward, and do not even need the math library to work. The syntax for basic math with these operands are as follows:

variable = operand operator operand;

So an example of this could be something like

int myResult;
myResult = 5 + 6;  // A value of 11 will be stored into the integer myResult

Now, C++ allows this all to be super flexible. So you can mix numbers, variables, floats, ints and all sorts of things together.

int five=5, six=6, twelve=12;
float pi=3.14, myResult;
myResult = (float)six * pi + (float)twelve;
myResult = myResult - (float)five;

C++ will perform operations via BEDMAS (or PEDMAS, or however you learnt it in middle school). That means brackets (or paranthesis) will be computed first. Then exponents, then division and multiplication, and then finally addition and subtraction. So the above code will take the number six, and then convert it to a floating point number. Then it will multiply 6 by pi, which will return a floating point number. Then twelve, which has been converted to a float, will be added to the result. So myResult at this point will contain 30.84. Then five will be converted to a float and subtracted from myResult. The result will be 25.84.

Now, this should be pretty straight forward I hope :) Now, take a look at the last line of the above code. Notice that we are taking the variable and adding another value to it, and then dumping it back into itself. You've just had to type the same variable name twice in a row, which could lead to typos, and also takes up time. There is an easier way to do this! Each of the following lines has an equivalent line of code that follows.

myResult = myResult+5;
myResult += 5;
myResult = myResult-5;
myResult -= 5;
myResult = myResult*5;
myResult *= 5;
myResult = myResult/5;
myResult /= 5;

See the pattern? Very cool stuff hey? See, programming is easy! Okay, so we reach another very common situation. There are many instances where you will want to increment a variable by one, or decrement it by one. This is very common in counters and loops. For example, everytime the user hits a button on the keyboard you may want to increment a counter by 1. So, here's an easy way to add one, or subtract one from a variable.

int counter=0;  // Initialize the variable
counter++;      // Adds 1 to the variable counter
counter--;      // Subtracts 1 from the variable counter

Basic Math Library Functions

Now it is time to browse a few of the functions that are available from the math library. We can square root numbers, we can take powers of numbers. The synatax is as follows:

float myValue=25, myResult;
myResult = sqrt(myValue);       // Squareroot the variable myValue, and store it into variable myResult
myResult = pow(my_result,2.00);  // Square the variable myResult, and then store it back into myResult

Let's investigate what this does. First the value 25 is square rooted and stored into myResult. This leaves a 5.00 in myResult. Then this value is squared and stored back into itself. This means 25.00 will be stored in myResult. Notice that I have put 2.00 into the power function. If I was to put just a 2, then the output number would be an integer, and not a float. So, more simply.

pow(myValue,myPowner);    // myValue is the base and myPower is the exponent

End of lesson 3

You should now be able to get C++ to do some simple math for you. In reality, it never gets much more complex than this. Almost everything you will write will be based on just a few simple operators. Thanks for reading this far, and hopefully you have learnt something! If any of this seemed to complicated, let me know! Write something in discussion, or anything like that.

Where To Go Next

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