Integers

Bear these considerations in mind when using integers:

Check for integer division. When you're using integers, 7/10 does not equal 0.7. It usually equals 0, or minus infinity, or the nearest integer, or—you get the picture. What it equals varies from language to language. This applies equally to intermediate results. In the real world 10 * (7/10) = (10*7) / 10 = 7. Not so in the world of integer arithmetic. 10 * (7/10) equals 0 because the integer division (7/10) equals 0. The easiest way to remedy this problem is to reorder the expression so that the divisions are done last: (10*7) / 10.

Check for integer overflow. When doing integer multiplication or addition, you need to be aware of the largest possible integer. The largest possible unsigned integer is often 232-1 and is sometimes 216-1, or 65,535. The problem comes up when you multiply two numbers that produce a number bigger than the maximum integer. For example, if you multiply 250 * 300, the right answer is 75,000. But if the maximum integer is 65,535, the answer you'll get is probably 9464 because of integer overflow (75,000 - 65,536 = 9464). Table 12-1 shows the ranges of common integer types.

Table 12-1. Ranges for Different Types of Integers

Integer Type

Range

Signed 8-bit

−128 through 127

Unsigned 8-bit

0 through 255

Signed 16-bit

−32,768 through 32,767

Unsigned 16-bit

0 through 65,535

Signed 32-bit

−2,147,483,648 through 2,147,483,647

Unsigned 32-bit

0 through 4,294,967,295

Signed 64-bit

−9,223,372,036,854,775,808 through 9,223,372,036,854,775,807

Unsigned 64-bit

0 through 18,446,744,073,709,551,615

The easiest way to prevent integer overflow is to think through each of the terms in your arithmetic expression and try to imagine the largest value each can assume. For example, if in the integer expression m = j * k, the largest expected value for j is 200 and the largest expected value for k is 25, the largest value you can expect for m is 200 * 25 = 5,000. This is OK on a 32-bit machine since the largest integer is 2,147,483,647. On the other hand, if the largest expected value for j is 200,000 and the largest expected value for k is 100,000, the largest value you can expect for m is 200,000 * 100,000 = 20,000,000,000. This is not OK since 20,000,000,000 is larger than 2,147,483,647. In this case, you would have to use 64-bit integers or floating-point numbers to accommodate the largest expected value of m.

Also consider future extensions to the program. If m will never be bigger than 5,000, that's great. But if you expect m to grow steadily for several years, take that into account.

Check for overflow in intermediate results. The number at the end of the equation isn't the only number you have to worry about. Suppose you have the following code:

Example 12-1. Java Example of Overflow of Intermediate Results

int termA = 1000000;
int termB = 1000000;
int product = termA * termB / 1000000;
System.out.println( "( " + termA + " * " + termB + " ) / 1000000 = " + product );

If you think the Product assignment is the same as (1,000,000*1,000,000) / 1,000,000, you might expect to get the answer 1,000,000. But the code has to compute the intermediate result of 1,000,000*1,000,000 before it can divide by the final 1,000,000, and that means it needs a number as big as 1,000,000,000,000. Guess what? Here's the result:

( 1000000 * 1000000 ) / 1000000 = -727

If your integers go to only 2,147,483,647, the intermediate result is too large for the integer data type. In this case, the intermediate result that should be 1,000,000,000,000 is -727,379,968, so when you divide by 1,000,000, you get -727, rather than 1,000,000.

You can handle overflow in intermediate results the same way you handle integer overflow, by switching to a long-integer or floating-point type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.4