Numerical Data Types

• Integers
• Integers are whole numbers, no fractional part
• There are a entire set of integer types
• char, short, int, long
• Each is at least as small as the one that follows.
• sizeof function
• Takes a type.
• returns number of bytes for the type.
• Can be used in other ways as well.
• Size depends on c++ implementation, which in turn depends on the architecture.
• ints are usually 4 bytes, or 32 bits.
• They are numbers in twos compliment between -231 and 231-1
• Things can be signed or unsigned.
• An unsigned int would be between 0 and 232-1
• declarations of constants and variables
```	    const int X = 3;
const unsigned short  Y = 5;
...

long grossPay;
int bonusPay = -4;
unsigned char netPay = 3;
```
• Usually, unless you have reasons to do otherwise, you use an int.
• If an integer literal begins with a 0, the number is assumed to be in base 8.
• If an integer literal begins with an 0x the number is assumed to be in base 16
```int x=0x1F;

cout << "X is " << x << endl;
x = 010;
cout << "X is " << x << endl;

output:
X is 31
X is 8
```
• The file /usr/include/limits.h has some predefined sizes
```#include <limits.h>

cout << "The maximum integer is " << INT_MAX << endl;
```
• If you try to add two really big numbers, (or really small for that matter), you will produce overflow.
• The compiler may warn you if it occurs (and it can figure out that it will occur)
• The operating system may do so as well when it runs
```#include <iostream>
#include <limits.h>

using namespace std;

int main () {
int x;

cout << "The maximum integer is " << INT_MAX << endl;
x = INT_MAX + 1;
cout << "one more is " << x << endl;
return -1;
}

WHEN COMPILED:

foo.C: In function `int main()':
foo.C:12: warning: integer overflow in expression

PRODUCES:
The maximum integer is 2147483647
one more is -2147483648

```
• Warnings
• Warnings are issued when the compiler finds a problem, but can go on anyway.
• There are very few times when you should allow a warning to exist without fixing it. (NEVER?)
• Floating Point Numbers
• Think of these as numbers in scientific notation.
• Numbers with a decimal point.
• types : float, double, longdouble
• Find the sizes with sizeof
• /usr/include/math.h for some predefined constants.
• Declarations are the same as ints, chars, and strings.
• Literals
• {+|-}digit*{.}digit*{E{+|-}digit+}
```
float x;

x = -1;
cout << "X is " << x << endl;
x = .2;
cout << "X is " << x << endl;
x = -.3E22;
cout << "X is " << x << endl;
x = -1.2E-12;
cout << "X is " << x << endl;

Output:
X is -1
X is 0.2
X is -3e+21
X is -1.2e-12

```
• Floats are much slower to use than integers. Use integers when you can.
• We can not represent many numbers
• This is known as roundoff error.
• Actually, we can only represent a finite number of numbers, and there are infinately many real numbers.
• We will see more of this later.