Data types of floating point numbers for getting started with C + +

Source: Internet
Author: User
Tags constant square root

There are 3 kinds of data types for floating-point numbers, which are:

FLOAT: single-precision floating-point numbers

Double: double-precision floating-point numbers

Long double: extended precision floating-point number

The term "precision" here refers to the number of digits in the mantissa. The precision of the above data types is increased progressively from top to bottom, with the lowest number of digits in the mantissa and the number of digits in long double. Note that precision only determines the number of digits in the mantissa. The range of values represented by a type is primarily determined by the possible range of indices.

The ANSI standard for C + + does not describe the precision and range of values, so the precision and numerical range of these types is determined by the compiler, which usually maximizes the use of the floating-point functions provided by the computer. In general, a long double provides a precision greater than or equal to the double type, and the double provides a precision greater than or equal to the float type.

Typically, the float type provides 7-bit precision, the double type provides 15-bit precision, and the long double provides 19-bit precision, but the double and long double types have the same precision on several compilers. In addition to increased precision, the range of values for double and long double is widening.

On a PC, the range of values represented by a floating-point number type is shown in table 2-11.

Table 2-11

Obviously, these types can represent 0, but they cannot represent values between the lower bounds of the 0 and the positive and negative ranges, so these lower limits are the smallest of 0 values.

By default, floating-point literals are double, and here's how to define variables of this type. You can use the keyword double to specify the floating-point number variable, as shown in the following statement:

Double inches_to_mm=25.4;

This statement declares the variable inches_to_mm as a double and initializes its value to 25.4. If you declare a floating-point number variable, you can also use const, which you can do when you need a floating-point number constant. If you want to modify the value of a variable, the declaration statement should look like the following:

const double inches_to_mm=25.4; Define a constant conversion factor

If you do not need the precision and range of values provided by the double variable, you can choose to declare the floating-point number variable by using the keyword float. For example:

float pi = 3.14159f;

This statement defines a variable pi and sets its initial value to 3.14159. The literal tail of F indicates that this is a float type. If there is no F, the literal is the double type, which does not matter, but the compiler emits a warning message. You can also use the capital letter F to indicate that the floating-point literal is a float type.

To specify the literal amount of type long, you should add uppercase or lowercase letters l at the end of the value. You can declare and initialize a variable of this type with the following statement:

Long double root2 = 1.4142135623730950488L; Square Root of 2

The use of floating-point variables is simple, but the experience of not using this variable is not very good, so here's an example of this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.