C ++ Basic Data Types
I remember that when I first learned the C language, there were various confusions about the double, float, long, and unsigned int types, which were basically used casually and there was no overall framework for the data types. I recently learned a lot about <C ++ primer plus>. here we will record the knowledge about C ++ basic data types in the form of Reading Notes.
Generally, there are only two types of C ++ data: integer and floating point.
I,Integer:
Is the number without decimal part (in ascending order of width except bool). It includes:
- Char
- Short
- Int
- Long
- Long
- Bool
Among them, each type except bool has 11 types of signed versions and unsigned versions.
Two Special Cases
- Char is most commonly used to process characters. However, since all characters have their numerical encoding (such as the ASCII character set), char can be considered as an integer smaller than short.
- Bool: the literal value "true" can be converted to 1, and "false" can be converted to 0. In addition, any numeric value can be converted to "bool". The non-zero numeric value is true, and "0" is false.
Unsigned and signed:
When the value is not negative, such as the life value and population quantity, you can use the unsigned type to increase the maximum storage value of the variable.
For example, if the short value ranges from-32768 to + 32767, the unsigned version is 0-65535.
How to select the integer type if there are so many integer types:
- If there is not enough reason to use other types, use int. Because int is usually set to the most natural length for the computer, that is, the most efficient length.
- If the value indicated by the variable cannot be negative, you can use the unsigned type to express a larger value.
- If you know the maximum possible value of a variable that represents a value greater than a 16-bit integer, use long (even if some system int Is 32-bit, ensure program portability)
- If the storage value exceeds 2 billion, use long
- If short is smaller than int, you can use short to save memory. (short must be at least 16 bits in C ++, and int must be at least as long as short ).
- If memory saving is very important, use shoRt
2. Floating Point Number:
Numbers with decimal digits
Floating point:
For numbers such as 2.5 and 3.15159, the computer stores them in two parts. One part indicates the value, and the other part is used to scale the value.
For example, 34.125 and 3412.5 have the same number except decimal place. we can see the first number as 0.34125 (reference value) to 100 times, and the second number as 0.34125 to 10000 times. the zoom factor is used to move the decimal point.
In C ++, there are three floating point types:
The three valid digits can be the same. Generally, float is 32 bits, double is 64 bits, and long double is 80, 96, or 128 bits.
How to select the floating point type:
- When the precision is high, the double. double type has a high precision. The valid number is 16 bits, and the float precision is 6 bits or 7 bits.
- Do not use dual-precision when using single-precision data, saving memory. (double consumes two times of the memory float, and the double operation speed is much slower than float)
- Add:
In the code, the floating point data type is directly ==,<=, >= ,! =. The correct method should be to compare the difference or the decimal number.
Usually compared with 10 ^ 6, for example, floating point and zero:
Equal to 0: fabs (I) <= 1e-6
Relationship greater than 0: I> 1e-6
Relationship less than 0: I <1e-6