C++ brings some order to its basic types by classifying them into families. Types signed char, short, int, and long are termed signed integer types. C++11 adds long long to that list. The unsigned versions are termed unsigned integer types. The bool, char, wchar_t, signed integer, and unsigned integer types together are termed integral types or integer types. C++11 adds char16_t and char32_t to that list. The float, double, and long double types are termed floating-point types. Integer and floating-point types are collectively termed arithmetic types.

C++ Arithmetic Operators

Perhaps you have warm memories of doing arithmetic drills in grade school. You can give that same pleasure to your computer. C++ uses operators to do arithmetic. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus. Each of these operators uses two values (called operands) to calculate a final answer. Together, the operator and its operands constitute an expression. For example, consider the following statement:

int wheels = 4 + 2;

The values 4 and 2 are operands, the + symbol is the addition operator, and 4 + 2 is an expression whose value is 6.

Here are C++’s five basic arithmetic operators:

• The + operator adds its operands. For example, 4 + 20 evaluates to 24.

• The - operator subtracts the second operand from the first. For example, 12 - 3 evaluates to 9.

• The * operator multiplies its operands. For example, 28 * 4 evaluates to 112.

• The / operator divides its first operand by the second. For example, 1000 / 5 evaluates to 200. If both operands are integers, the result is the integer portion of the quotient. For example, 17 / 3 is 5, with the fractional part discarded.

• The % operator finds the modulus of its first operand with respect to the second. That is, it produces the remainder of dividing the first by the second. For example, 19 % 6 is 1 because 6 goes into 19 three times, with a remainder of 1. Both operands must be integer types; using the % operator with floating-point values causes a compile-time error. If one of the operands is negative, the sign of the result satisfies the following rule: (a/b)*b + a%b equals a.

Of course, you can use variables as well as constants for operands. Listing 3.10 does just that. Because the % operator works only with integers, we’ll leave it for a later example.

Listing 3.10. arith.cpp

// arith.cpp -- some C++ arithmetic

#include

int main()

{

    using namespace std;

    float hats, heads;

    cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

    cout << "Enter a number: ";

    cin >> hats;

    cout << "Enter another number: ";

    cin >> heads;

    cout << "hats = " << hats << "; heads = " << heads << endl;

    cout << "hats + heads = " << hats + heads << endl;

    cout << "hats - heads = " << hats - heads << endl;

    cout << "hats * heads = " << hats * heads << endl;

    cout << "hats / heads = " << hats / heads << endl;

    return 0;

}

As you can see in the following sample output from the program in Listing 3.10, you can trust C++ to do simple arithmetic:

Enter a number: 50.25

Enter another number: 11.17

hats = 50.250000; heads = 11.170000

hats + heads = 61.419998

hats - heads = 39.080002

hats * heads = 561.292480

hats / heads = 4.498657

Well, maybe you can’t trust it completely. Adding 11.17 to 50.25 should yield 61.42, but the output reports 61.419998. This is not an arithmetic problem; it’s a problem with the limited capacity of type float to represent significant figures. Remember, C++ guarantees just six significant figures for float. If you round 61.419998 to six figures, you get 61.4200, which is the correct value to the guaranteed precision. The moral is that if you need greater accuracy, you should use double or long double.

Order of Operation: Operator Precedence and Associativity

Can you trust C++ to do complicated arithmetic? Yes, but you must know the rules C++ uses. For example, many expressions involve more than one operator. That can raise questions about which operator gets applied first. For example, consider this statement:

int flyingpigs = 3 + 4 * 5;  // 35 or 23?

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги