Elementary Programming in C++: Numeric Data Types and Operations

C++ has nine numeric types for integers and floating-point numbers with operators +, -, *, /, and %.

1. Numeric Types

Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. C++ provides primitive data types for numeric values, characters, and Boolean values. This section introduces numeric data types and operations.

Table 2.1 lists the numeric data types with their typical ranges and storage sizes. Table 2.1 Numeric Data Types.

C++ uses three types for integers: short, int, and long. Each integer type comes in two flavors: signed and unsigned. Half of the numbers represented by a signed int are negative and the other half are non-negative. All the numbers represented by an unsigned int are non-negative. Because you have the same storage size for both, the largest number you can store in an unsigned int is twice as big as the largest positive number you can store in a signed int. If you know the value stored in a variable is always nonnegative, declare it as unsigned.

C++ uses three types for floating-point numbers: float, double, and long double. The double type is usually twice as big as float. So, the double is known as double precision, while float is single precision. The long double is even bigger than double. For most
applications, using the double type is desirable.

For convenience, C++ defines constants INT_MIN, INT_MAX, LONG_MIN, LONG_MAX, FLT_MIN, FLT_MAX, DBL_MIN, and DBL_MAX in the <limits> header file. These constants are useful in programming. Run the following code in Listing 2.5 and see what constant values are defined by your compiler:

Listing 2.5   LimitsDemo.cpp

1       #include <iostream>

2       #include <limits>

3       using namespace std;

4

4       int main()

5        {

6               cout <<  “INT_MIN is ” <<   INT_MIN  << endl;

7               cout << “INT_MAX is  ” << INT_MAX  << endl;

8               cout << “LONG_MIN is “

9               cout << “LONG_MAX is ” << LONG_MAX << endl;

10              cout << “FLT_MIN  is “

11              cout << “FLT_MIN  is ” << FLT_MAX  << endl;

12              cout << “DBL_MIN  is “

13              cout << “DBL_MIN is  ” << DBL_MAX  << endl;

15

14              return 0;

15        }

Note these constants may not be defined in some old compilers.

The size of the data types may vary depending on the compiler and computer you are using. Typically, int and long have the same size. On some systems, long requires 8 bytes.

You can use the sizeof function to find the size of a type or a variable on your machine. Listing 2.6 gives an example that displays the size of int, long, and double, and variables age and area on your machine.

Listing 2.6  SizeDemo.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    cout << “The size of int: ” << sizeof(int) << ” bytes” << endl;

7    cout << “The size of long: ” << sizeof(long) << ” bytes” << endl;

8    cout << “The size of double: ” << sizeof(double)

9      << ” bytes” << endl;

10

11    double area = 5.4;

12    cout << “The size of variable area: ” << sizeof(area)

13      << ” bytes” << endl;

14

15    int age = 31;

16    cout << “The size of variable age: ” << sizeof(age)

17      << ” bytes” << endl;

18

19    return 0;

20 }

Invoking sizeof(int), sizeof(long), and sizeof(double) (lines 6-8) return the number of bytes allocated for the int, long, and double types, respectively. Invoking sizeof(area) and sizeof(age) return the number of bytes allocated for the variables area and age, respectively.

2. Numeric Literals

 A literal is a constant value that appears directly in a program. In the following statements, for example, 34 and 0.305 are literals:

int i = 34;

double footToMeters = 0.305;

By default, an integer literal is a decimal integer number. To denote an octal integer literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a leading 0x or 0X (zero x). For example, the following code displays the decimal value 65535 for hexadecimal number FFFF and decimal value 8 for octal number 10.

cout << OxFFFF << ” ” << 010;

Hexadecimal numbers, binary numbers, and octal numbers are introduced in Appendix D, “Number Systems.”

Floating-point literals can be written in scientific notation in the form of a X 10b. For example, the scientific notation for 123.456 is 1.23456 X 102 and for 0.0123456 it’s 1.23456 X 10-2. A special syntax is used to write scientific notation numbers. For exam­ple, 1.23456 X 102 is written as 1.23456E2 or 1.23456E+2 and 1.23456 X 10-2 as 1.23456E-2. E (or e) represents an exponent and can be in either lowercase or uppercase.

3. Numeric Operators

The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), and remainder (%), as shown in Table 2.2. The operands are the values operated by an operator.

When both operands of a division are integers, the result of the division is the quotient and the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and -5 / 2 yields -2, not – 2.5. To perform regular mathematical division, one of the operands must be a floating­point number. For example, 5.0 / 2 yields 2.5.

The % operator, known as modulo or remainder operator, works only with integer operands and yields the remainder after division. The left-hand operand is the dividend and the right- hand operand the divisor. Therefore, 7 % 3 yields 1, 3 % 7 yields 3, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.

The % operator is often used with positive integers but also can be used with negative integers. The behavior of the % operator involving negative integers is compiler-dependent. In C++, the % operator is for integers only.

Modulus is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression:

The program in Listing 2.7 obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.

Listing 2.7   DisplayTime.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    // Prompt the user for input

7    int seconds;

8    cout << “Enter an integer for seconds: “;

9    cin >> seconds;

10   int minutes = seconds / 60;

11   int remainingSeconds = seconds % 60;

12   cout << seconds << ” seconds is ” << minutes <<

13   ” minutes and ” << remainingSeconds << ” seconds ” << endl;

14

15   return 0;

16 }

Line 9 reads an integer for seconds. Line 10 obtains the minutes using seconds / 60. Line 11 (seconds % 60) obtains the remaining seconds after taking away minutes.

The + and – operators can be both unary and binary. A unary operator has only one oper­and; a binary operator has two. For example, the – operator in -5 is a unary operator to negate number 5, whereas the – operator in 4 – 5 is a binary operator for subtracting 5 from 4.

4. Exponent Operations

The pow(a, b) function can be used to compute ab. pow is a function defined in the cmath library. The function is invoked using the syntax pow(a, b) (i.e., pow(2.0, 3)) that returns the result of ab (23). Here, a and b are parameters for the pow function and numbers 2.0 and 3 are actual values used to invoke the function. For example,

cout << pow(2.0, 3) << endl; // Display 8.0

cout << pow(4.0, 0.5) << endl; // Display 2.0

cout << pow(2.5, 2) << endl; // Display 6.25

cout << pow(2.5, -2) << endl; // Display 0.16

Note that some C++ compilers require that either a or b in pow(a, b) be a decimal value. Here we use 2.0 rather than 2.

More details on functions will be introduced in Chapter 6. For now, it’s sufficient to know how to invoke the pow function to perform the exponent operation.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *