Introduction to Programming and C++

Contents Previous Topic Next Topic

If I assign an integer to a double, the compiler does the natural thing: it converts it to the corresponding double value, as is shown in Program 1. This is called type promotion.

Program 1. Promoting an integer to a double.

#include <iostream>

using namespace std;

int main() {
	double x = 3;   // 3 is an integer

	cout << x << endl;

	x = x + 0.1;

	cout << x << endl;

	return 0;
}

A similar process occurs with the promotion of practically anything to a Boolean value, using the following rule:

Any non-zero value is interpreted (promoted) to the value of true while any zero is interpreted as false.

This can be seen in Program 2 which reads a double from the user input.

Program 2. Promoting a double to a Boolean value.

#include <iostream>

using namespace std;

int main() {
	double x = 0;

	cout << "Please enter a real number: ";
	cin >> x;

	if ( x ) {
		cout << "You entered " << x << " which is not zero." << endl;
	} else {
		cout << "You entered zero." << endl;
	}

	return 0;
}

In the if statement, it looks at the value of x and if it is a non-zero value, the first clause is executed. If the value is 0, the else clause is executed.

The justification for this is that each time any instruction is performed on the CPU, if the result is 0, a special bit is set. As a result, an if statement simply checks the value of that one bit.1

Promotion does not always occur autmoatically: for example, a double is not immediately interpreted as an integer. If you try to compile Program 3, you will get a warning.

Program 3. Promoting a double to a Boolean value.

#include <iostream>

using namespace std;

int main() {
	double x = 3.1415;
	int n = x;

	cout << x << " != " << n << endl; 

	return 0;
}

The compiler will not give you an error, however, it will give a warning. To avoid such a warning, you must explicitly tell the compiler "Yes, I want to convert this double into an integer." This is a process called casting. Program 4 gives the necessary syntax.

Program 4. Promoting a double to a Boolean value.

#include <iostream>

using namespace std;

int main() {
	double x = 3.1415;
	int n = static_cast<int>( x );

	cout << x << " != " << n << endl; 

	return 0;
}

When you compile this, you will not get a warning. If you are familiar with C, do not use the old C-style of casting.

Time of Casting

Examine Program 5 and try to guess what the output will be.

Program 5. Integer division and type promotion.

#include <iostream>

using namespace std;

int main() {
	int m = 5;
	int n = 2;
	double x = m/n;

	cout << m << "/" << n << " == " << x << endl; 

	return 0;
}

If your guess is 5/2 = 2.5, guess again. When the compiler sees the division of two integers, it uses integer division (as described in built-in data types) and only once the division is performed does it promote the result to a double. To get the expected output, see Program 6.

Program 6. Appropriate modifications to Program 5.

#include <iostream>

using namespace std;

int main() {
	int m = 5;
	int n = 2;
	double x = static_cast<double>( m ) / static_cast<double>( n );

	cout << m << "/" << n << " == " << x << endl; 

	return 0;
}

At this point, the result will be the expected value of 2.5.


Questions

1. Write a program which prompts the user for two nine-digit integers and stores them as int (simply tell the user that they should enter nine-digit numbers -- you don't have to check this). Next, assign a double the product of the two integers. What happens if you don't use casting (as in Program 5) and what happens if you do use casting (as in Program 6).

2. In Program 6, we could replace the line:

	double x = static_cast<double>( m ) / static_cast<double>( n );

with the line:

	double x = static_cast<double>( m ) / n;

This would still work, because the compiler would determine that the numerator is a double, and therefore, we must interpret the denominator as a double. What are some of the possible negative effects of the second form?

3. Compile Program 6 both with both forms suggested in Question 2. Compare the size of the resulting executable programs.


1. Technically, -0.0 is different from 0.0 (these are stored as 0x8000000000000000 and 0x0000000000000000, respectively), however, both of these are interpreted as false. This, and because a double occupies 8 bytes instead of 4 implies that more work than a single instruction is required to promote a double to a bool.


Contents Previous Topic Top Next Topic