4.8 Floating-Point Numbers

I l @ ve RuBoard

4.8 Floating-Point Numbers

Real numbers are numbers that have a fractional part. Because of the way they are stored internally, real numbers are also known as floating-point numbers . The numbers 5.5, 8.3, and -12.6 are all floating-point numbers. C++ uses the decimal point to distinguish between floating-point numbers and integers, so a number such as 5.0 is a floating-point number while 5 is an integer. Floating-point numbers must contain a decimal point. Numbers such as 3.14159, 0.5, 1.0, and 8.88 are floating-point numbers.

Although it is possible to omit digits before the decimal point and specify a number as .5 instead of 0.5, the extra 0 makes it clear that you are using a floating-point number. A similar rule applies to 12. versus 12.0. Floating-point zero should be written as 0.0.

Additionally, a floating-point number may include an exponent specification of the form e ± exp . For example, 1.2e34 is shorthand for 1.2x10 34 .

The form of a floating-point declaration is:

 float   variable   ;   //   comment   

Again, there is a limit on the range of floating-point numbers the computer can handle. The range varies widely from computer to computer. Floating-point accuracy is discussed further in Chapter 19.

Floating-point numbers may be output using std::cout . For example:

 std::cout << "The answer is " << (1.0 / 3.0) << "\n"; 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net