Primitive Types

C++ Primitive Types

let’s discuss these basic C++ primitive types ...

bool

char

int (and friends)

double (and friends)

bool: The Boolean Types

bool literals: true, false

bool flag = true;
cout << flag; // 1

flag = false;
cout << flag; // 0

cout << "bool is " << sizeof(bool) << " bytes(s)";
// bool is 1 byte(s)

char: A Character Type

char literals: 'a', '7', '\n', '\'', ...

char letter = 'S';
cout << "letter = " << letter   // letter = S
     << "ASCII value of letter = " << int(letter);
cout << "char is " << sizeof(char) << " bytes(s)";
// char is 1 bytes(s)

int: An Integer Type

int literals: 42, -35, 0, ...

cout << "int is " << sizeof(int) << " bytes(s)"
     // int is 4 bytes(s)

     << "long is " << sizeof(long) << " bytes(s)"
     // long is 4 bytes(s)

     << "long long is " << sizeof(long long) << " bytes(s)";
     // long long is 8 bytes(s)

Division Can be Tricky!

int harmonic1 = 1/1 + 1/2 + 1/3 + 1/4;
cout << "harmonic1 = " << harmonic1;
// 1 (!)

1/1 is 1

1/2 is 0

1/3 is 0

1/4 is 0

double: A Floating Point Type

double literals: 42.0, -3.5, 0.0, 0.00001, 5.04e13...

cout << "float is " << sizeof(float) << " bytes(s)"
     // float is 4 bytes(s)

     << "double is " << sizeof(double) << " bytes(s)"
     // double is 8 bytes(s)

     << "long double is " << sizeof(long double) << " bytes(s)";
     // long double is 12 bytes(s)

Division Can be Tricky!

double harmonic3 = 1/1 + 1.0/2 + 1/3.0 + 1.0/4.0;
cout << "harmonic3 = " << harmonic3 << "\n";
// harmonic3 = 2.08333

1/1 evaluates to an int

1.0/2 evaluates to a double

1/3.0 evaluates to a double

1.0/4.0 evaluates to a double

Division Can be Tricky!

double(1)/2 evaluates to a double (0.5)

double(1/2) evaluates to a double (0.0)

1/double(3) evaluates to a double (0.33333)

double(1)/double(4) evaluates to a double (0.25)

Division Can be Tricky!

division by 0 is undefined in mathematics

but it’s not so simple in C++

Division by 0 (int literals)

cout << "2 / 0 = " << (2 / 0);

compiler error (for us!)

error: division by zero

Division by 0 (int variables)

int n = 2;
int zero = 0;
cout << "n / zero = " << (n / zero);

// Run-time error: Floating point exception (core dumped)

Division by 0 (int variables)

int n = 2;
int five = 5;
cout << "five / (n - n) = " << (five / (n - n));

// compiler error (for us!): error: division by zero

Division by 0.0 (doubles)

cout << "4.0 / 0.0 = " << (4.0 / 0.0);
// 4.0 / 0.0 = inf  (!)

inf is a value representing positive infinity

cout << "0.0 / 0.0 = " << (0.0 / 0.0);
// 0.0 / 0.0 = -nan  (!)

nan is a value representing “not a number”

nan and inf are both doubles and they are not equal to each other

Computer arithmetic can be strange

in mathematics these facts are true

for any real number \(x\)

  • if \(x > 0\) then \(x + 1 > 0\)
  • \(|x| \geq 0\)
  • \(x = x\)

but these are not all true in C++!

Computer Arithmetic Can be Strange

if \(x > 0\) then \(x + 1 > 0\)

int a = 2147483647;
int b = 1;
cout << a + b;
// -2147483648

2147483647 is the max int (\(2^{31} - 1\))

-2147483648 is the min int (\(-2^{31}\))

adding 1 to the max int gives you the min int

Computer Arithmetic Can be Strange

\(|x| \geq 0\)

cout << abs(-2147483648);

-2147483648 is the min int

due to 0, there is one more negative int than positive int

so -2147483648 has no matching positive int

Computer Arithmetic Can be Strange

\(x = x\)

double x = 0.0 / 0.0;
bool same = (x == x);
cout << same;

prints 0, which means x == x is false (!)

0.0 / 0.0 is nan, i.e. “not a number”

nan != nan

Computer Arithmetic Can be Strange

there are infinitely many real numbers, but only a finite number of doubles, so round-off errors are unavoidable

cout << 1.0 / 3.0;
// 0.333333

0.333333 is a little less than the true value \(\frac{1}{3} = 0.3333\ldots\)

Comparing Floating Point Numbers

x == y is often a bad way of comparing two doubles

round-off errors might make x and y not exactly equal

use abs(x - y) < EPSILON, where EPSILON is a small double you define (e.g. maybe 0.000001)

Advice

important floating point calculations must be done with great care

however, we will mostly ignore round-off errors in this course and hope they don’t cause problems