Numeric Data Types
- The integer (int) datatype
- This represents a subset of the integers.
- Probably -231 to 231-1
- Or -263 to 263-1
- Or -215 to 215-1
- The C/C++ standard does not give a size, that is determined by the hardware/compiler implementer.
- operations include +,-,*,/ and %
- % is modulus.
- It gives the remainder
- 5 % 1 = 0 because 5 = 1 * 5 + 0
- 5 % 2 = 1 because 5 = 2 * 2 + 1
- 5 % 3 = 2 because 5 = 3 * 1 + 2
- 5 % 4 = 1 because 5 = 4 * 1 + 1
- This turns out to be very useful
- If we try to compute a number that is larger than can be stored, integer overflow occurs.
- Input, Output
- cin >> intVar
- Skips all white space
- Reads as much of an integer as it can
- Stops when it hits a noninteger
- stores the results in the variable
- Be careful, if an integer starts with a 0, it is assumed to be in octal.
- This is just one of a family of integer types.
- The char type is actually an integer.
- This is the smallest integer class.
- I/O on char is different.
- It is not common to use a char as an integer type.
- short is the next class
- It is at least as big as a char, but may be larger.
- int is next
- It is at least as big as a short but may be larger.
- long is next
- It is at least as big as a int but may be larger.
- This would be used for computing with large numbers.
- Each of these may be modified by unsigned
- This means only zero and positive values.
- The range is shifted so that it starts at 0.
- And goes through 2n -1
- It is rare to use an unsigned
- It is rare to use anything other than an int.
- Reading Exercise: change.C
- The floating point types
- Floating point numbers are numbers in scientific notation.
- 1.23x10100 is a very large number.
- 1.23x10-100 is a very small number.
- 1.23x100 is a number very close to 1.
- float is the most common type.
- but double and long double are also available.
- The later two will be much slower to compute with.
- But will provide more accuracy.
- Floating point constants can be
- 1
- 1.3
- -1.342
- 0.000002345
- 3E5 -> 3x105
- 4.234E-12 -> 4.234x10-12
- Floating point numbers may not be accurately represented by the computer.
- More on this later.
- I/O of floats
- Input: as with ints, try to read as much of the data from the stream as possible.
- Output, more details at the end of the chapter.
- Expressions with numeric data types
- For the most part, equations are as you expect.
- But the right hand side of the equation is evaluated and assigned to the left hand side.
- This is different from math where the two sides are the same.
- The order of operations is as you learned, and is listed in appdendix B.
- We use () as you would expect to change the order of operations.
- Reading Exercise: burger.C