C ++ entry-level floating point data type

Source: Internet
Author: User

There are three types of floating point data:

● Float: Single-precision floating point number

● Double: double-precision floating point number

● Long double: Floating Point with extended precision

The term "precision" here refers to the number of digits in the tail. The precision of the above data type increases step by step from top to bottom. float has the lowest number of digits in the end and long double has the most. Note that the precision only determines the number of digits in the ending number. The value range of a value of a type is determined by the possible range of the index.

The ANSI standard of C ++ does not describe the precision and Numerical range, so the accuracy and Numerical range of these types are determined by the compiler. The Compiler usually maximizes the use of the floating point function provided by the computer. Generally, the precision of the long double type is greater than or equal to the double type, and that of the double type is greater than or equal to the float type.

Generally, the float type provides 7-bit precision, the double type provides 15-bit precision, and the long double type provides 19-bit precision, however, the double and long double types have the same precision on several compilers. In addition to increasing precision, values of the double and long double types are also expanding.

On PC, the value range of the floating point type is shown in Table 2-11.

Table 2-11

Obviously, all these types can represent 0, but they cannot represent the value between 0 and the lower limit in the positive and negative ranges. Therefore, these lower limits are the minimum values in non-0 values.

By default, the floating point literal is of the double type. Let's take a look at how to define this type of variable. You can use the double keyword to specify the floating point variable, as shown in the following statement:

Double inches_to_mm = 25.4;

This statement declares the variable inches_to_mm as the double type and initializes its value to 25. 4. you can also use const when declaring floating-point variables. This can be done when a floating-point constant is required. If you want to modify the value of a variable, the declaration statement should be as follows:

Const double inches_to_mm = 25.4; // Define a constant conversion factor

If you do not need the precision and value range provided by the double variable, you can use the float keyword to declare the floating point variable. For example:

Float pi = 3.14159f;

This statement defines a variable pi and sets its initial value to 3. 14159. f at the end of the literal volume indicates that this is a float type. If f is not used, the literal is of the double type. This will not cause any problem, but the compiler will issue a warning message. You can also use the upper-case letter F to indicate that the floating point literal is of the float type.

To specify the literal value of the long double type, add an upper or lower-case letter L at the end of the value. Use the following statement to declare and initialize this type of variable:

Long double root2 = 1.4142135623730950488L; // Square root of 2

It is very easy to use floating-point variables, but it is not good to use such variables after all, so the following is an example.

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.