Jump to content

Simple math in C++: Difference between revisions

From IdeaWazaWiki
wikademia>Cybiko123
mNo edit summary
wikademia>Cybiko123
mNo edit summary
Line 7: Line 7:
</pre></code>
</pre></code>


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


=== Adding, Subtracting, Multiplying and Dividing ===
=== 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:
These are all very straightforward, and do not even need the math library to work. The syntax for basic math with these operands are as follows:


<code><pre>
<code><pre>
Line 24: Line 24:
</pre></code>
</pre></code>


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


<code><pre>
<code><pre>
Line 33: Line 33:
</pre></code>
</pre></code>


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.
C++ will perform operations via BEDMAS (or PEDMAS, or however you learned it in middle school). That means brackets (or parantheses) will be computed first. Then exponents, then division and multiplication, and then finally addition and subtraction. 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.
This should be pretty straightforward. 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.


<code><pre>
<code><pre>
Line 48: Line 48:
</pre></code>
</pre></code>


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.
See the pattern? 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.


<code><pre>
<code><pre>

Revision as of 17:46, 4 June 2007

Simple C++ Math

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>

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 straightforward, 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 very 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 learned it in middle school). That means brackets (or parantheses) will be computed first. Then exponents, then division and multiplication, and then finally addition and subtraction. 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.

This should be pretty straightforward. 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? 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