Focus on C ++ details-use the built-in arithmetic type

Source: Internet
Author: User

 

The integer number in C ++ is a bit confusing. Like C, C ++ is designed to allow programs to process hardware directly when necessary, so integers are defined to meet the needs of a variety of hardware features. Most programmers can (should) Ignore these complexities by limiting the types actually used.

In fact, many people use integer data to count. For example, a program often calculates the number of elements in a data structure such as a vector or an array. In fact, the standard library defines a set of types used to measure the size of objects. Therefore, when counting these elements, the type defined by the standard library is always correct. In other cases, it is wise to use the unsigned type to avoid the possibility that the result is negative if the value is out of bounds.

Short Type is rarely used when integer arithmetic operations are performed. In most programs, using the short type may cause an error where the value assignment is out of bounds. The consequences of this error will depend on the machine in use. A typical case is that the value is "truncated" so that it becomes a huge negative number because of the cross-border. In the same way, although the char type is an integer, the char type is usually used to store characters instead of for calculation. In fact, in some implementations, the char type is treated as the signed type, and in other implementations, it is considered as the unsigned type. Therefore, it is easy to use the char type as the computing type.

In most computing technologies, using int type for integer calculation is not prone to errors. Technically, the int type is represented in 16 bits -- this is too small for most applications. In practice, most general machines use 32-bit long as long to represent the int type. During integer calculation, A 32-bit expression of the int type and a 64-bit expression of the long type will cause difficulties in selecting the int type or the long type. (Of course, this is not a problem for ACMer and can be instantly determined. ). On these machines, the running price of long TYPE calculation is much higher than that of int type calculation, therefore, before selecting a type, you must first understand the program details and compare the actual runtime performance cost of the long and int types.

It is much easier to decide which floating point type to use: using the double type is basically not wrong. In the float type, the loss of implicit precision cannot be ignored, while the cost of Double precision calculation can be ignored as opposed to single precision. In fact, on some machines, the double type is much faster than the float type. The precision provided by the long double type is usually unnecessary, and additional operation costs are required.



Author Ling Feng

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.