This topic can be explained generally and by specific datatypes( ex: doing things with integers is different from doing things with floating point numbers).
Generally there are three terms that need to be defined before you can do anything.
- Expression: Any term or combo of terms, with terms being in this case variables or literals, with them being able to be combined with operators.
- Literal: A specific value
- ex: 2 is a int literal and “This” is a string literal
- Operator: a symbol that does a built in operation
- list of operators
- + addition
- – subtraction (and negative)
- * multiplication
- / division
- % (only works with integers)
- There are also compound operators, bitwise operators, conditional operators logical operators, most of which will be explained elsewhere
- compound operators
- ex: +=, -=, %=, . . .
- basically its one of the arithmetic operates and the ‘=’ sign
- This does combines an operation and an assignment, so it basically means that if you were changing the value of a variable you can shorten the equation
- ex: Instead of ‘b=b+5;’ you can do ‘b+=5;’
- ex: +=, -=, %=, . . .
- compound operators
- list of operators
The expressions in order to be read correctly by the computer must follow the precedence rules Which are:
- ( ) parenthesis
- * / %
- unary ( operators that only need one term to be used )
- ex: – when used to denote negative
- + –
- right to left
It is good practice to always use parenthesis so its easier for you and other programmers to read. In the same vein you should put spaces around operators unless unary so its easier to read.
Integers
For doing Arithmetic expressions with integers, the first thing you need to know is what are the integer types. They are as their name applies integers so when represented numerically they do not have a decimal point.
A list of these are:
- int
- (this is for reference, either 32 or 64 bits unless your using something really old, in which case it might be 16, but that’s unlikely)
- long int
- ( it doubles the number of bits in an int )
- short int
- (its half the number of bites in an int )
- bool
- ( this is true, false basically but false = 0, and true =1 (or well anything but 0 is typically going to convert to true))
- char
- ( all characters have an integer representation, which can be found on an ASCII table)
- hex
- ( this is where you use a base 16 number system to represent a number)
- octal
- (this is where you use a base 8 number system to represent a number)
- bit
- ( this is binary)
- etc
- Generally you can modify a datatype to only have positive integers in it using the unsigned keyword in front of the datatype, (most integer datatypes are signed unless otherwise specified)
- ex: unsigned int
- what happens if you set it to a negative number?
- Well it overflows the datatype so it prints out the maximum number that can be held in it, since it basically flips all the zeros into ones in the computers binary representation(In case you don’t know computers actually work in binary, binary is just annoying to read so we made it translate it into something comprehensible for us)
- Generally you can modify a datatype to only have positive integers in it using the unsigned keyword in front of the datatype, (most integer datatypes are signed unless otherwise specified)
When using integer literals ( as in actual numbers not characters) the default datatype is int. To change this you add a suffix to the end if the integer or you type cast it. For example the suffix L on an integer denotes a long int, which would look like, 135L.
Now in C++ you can divide by zero if and only if its floating point division not integer division. In integer division the quotient will always be a integer, so if the number you are dividing isn’t divisible it will round down, since it basically ignores the remainder. You get the remainder by using modulo (which is why modulo can only be used with integers).
Floating Point
These datatypes are basically a real number containing a decimal point. Their are three datatypes which in order of storage space are float (4 bytes), double (8 bytes) and long double ( 16 bytes).
Now you can divide by zero with floating point numbers. When you do so, a positive number / 0 = inf, a negative number /0 = -inf and 0/0 = NaN (or not a number).
The default for a floating point literal is a double, if you want it to be a float you use the suffix f.
Some other things to note are that very large and very small floating point numbers might be printed using scientific notation and it is a good practice to always have a number before the decimal even if its a ‘0’ so its easier to read.
I will probably go more in depth on integer and/or floating point types noting how to use them or how you should use them in a different post that I will link here.
One thought on “C++ Arithmetic Expressions”
Comments are closed.