11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

32 Part I: Introduction to C++ ProgrammingCountingYou cannot use floating-point variables in applications where counting is important.This includes C++ constructs, which requires counting ability. C++ can’tverify which whole number value is meant by a given floating-point number.For example, it’s clear that 1.0 is 1. But what about 0.9 or 1.1? Should these alsobe considered as 1? C++ simply avoids the problem by insisting on using intvalues when counting is involved.Calculation speedHistorically, a computer processor can process integer arithmetic quicker thanit can floating-point arithmetic. Thus, while a processor can add 1 million integernumbers in a given amount of time, the same processor may be able toperform only 200,000 floating-point calculations during the same period. (Notsurprisingly, I couldn’t even get around to reading the first value.)Calculation speed is becoming less of a problem as microprocessors increasetheir capabilities. Most modern processors contain special calculation circuitryfor performing floating-point calculations almost as fast as integercalculations.Loss of accuracyFloating-point variables cannot solve all computational problems. Floatingpointvariables have a limited precision of about 6 digits — an extra-economysize, double-strength version of float can handle some 15 significant digits withroom left over for lunch.To evaluate the problem, consider that 13 is expressed as 0.333 . . . in a continuingsequence. The concept of an infinite series makes sense in math, butnot to a computer. The computer has a finite accuracy. Average 1, 2, and 2(for example), and you get 1.666667.C++ can correct for many forms of round-off error. For example, in output, C++can determine that instead of 0.999999, that the user really meant 1. In othercases, even C++ cannot correct for round-off error.Not-so-limited rangeAlthough the double data type has a range much larger than that of an integer,it’s still limited. The maximum value for an int is a skosh more than 2 billion.The maximum value of a double variable is roughly 10 to the 38th power.That’s 1 followed by 38 zeroes; it eats 2 billion for breakfast. (It’s even morethan the national debt, at least at the time of this writing.)Only the first 13 digits or so have any meaning; the remaining 25 digits sufferfrom floating-point round-off error.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!