Simple math in C++: Difference between revisions

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>