1. BOOL
1. Detailed explanation of C language bool
-
bool
It is a keyword in C language that declares variables of Boolean type. There are only two possible values for the Boolean type:true
andfalse
。 - Before the C99 standard, C language did not have a built-in Boolean type, and people usually use it
int
Type and use0
expressfalse
,No0
(usually1
)expresstrue
。C99The standard has been introducedbool
Type, and header file<>
, it definesbool
、true
andfalse
。
2. Usage and definition of bool
To usebool
Type, first you need to include the header file<>
,
Then you can declare it like you would declare other types of variablesbool
Variables of type:
#include <> bool isHappy = true; bool isSad = false;
3. The basic function of bool
The main function of the bool type is to provide a more intuitive and easy to understand way to represent logical values. When writing conditional statements, usebool
Variables of types can make the code clearer and easier to understand.
4. Conversion between bool and other types
When needed,bool
Values of types can be implicitly converted toint
Type, wherefalse
Convert to0
,true
Convert to1
. However,int
Type conversion tobool
When type,0
Will be converted tofalse
, any non0
The values oftrue
。
5. Things to note when using bool
- Don't put
bool
Variables of types are confused with integer types, although they can be converted. - When writing conditional statements, use them directly
bool
Variables of type are used as conditions, not withtrue
orfalse
Make a comparison, e.g.if (isHappy)
Insteadif (isHappy == true)
。 - In some old C compilers or environments, it may not be supported
bool
type. In this case, you can useint
type and follow the same logical convention.
6. Expansion: Comparison of bool type with its "zero value"
In the semantics of Boolean logic, a zero value is considered "false" (FALSE), and any non-zero value is considered "true" (TRUE). It is worth noting that there is no unified standard for the specific numerical values of TRUE, and it depends on the specific programming language or environment. For example, in Visual C++, TRUE is defined as 1, and in Visual Basic, TRUE is defined as -1.
It is important to understand that the nature of a bool type variable is a Boolean expression that represents the result of a comparison with a "zero value" (i.e. FALSE). Therefore, a variable of type bool is used directly as a conditional expression in an if statement without explicitly comparing it with its "zero value". The correct usage of if statement is as follows:
bool b; // Assume b has been assigned a valueif (b) { // If b is true (non-zero), execute the code here ... } // Use the logical non-operator "!" to check if b is false (zero value)if (!b) { // If b is false (zero value), then execute the code here ... }
2. FLOAT
1. Definition and use of float
The float keyword is used to declare a single-precision floating-point variable that can store numeric values with decimals and has a relatively wide range of values. In C, the float type usually takes up 4 bytes (32 bits) of memory space, with 1 bit used for the sign bit, 8 bits used for the exponent bit, and the remaining 23 bits used for the mantissa bit.
2. Basic use of float
In C language, when defining a variable using the float keyword, you need to add a suffix of "f" or "F" after the variable name to clearly indicate that this is a constant of type float. For example:
float a = 3.14f; //If not added“f”or“F”suffix,The compiler might consider it asdoubleConstants of type,进而可能引发类型不匹配的警告or错误。
3. Float accuracy problem
The fundamental reason for the float accuracy problem is that the computer uses binary to represent floating-point numbers internally, while many decimal decimals cannot be fully expressed in binary. In addition, the float type follows the IEEE 754 standard when storing, which divides 32 bits into sign bits (1 bit), exponent bits (8 bits) and mantissa bits (23 bits). This representation also limits the accuracy of float.
Specifically, the mantissa bit is used to represent the decimal part of a floating point number, but since there are only 23 bits, it can only accurately represent numbers within a certain range. For numbers outside this range, the computer will round up to find the closest representable number, resulting in a loss of accuracy.
4. Examples of accuracy problems
1. 0.1 cannot be expressed accurately:
In decimal, 0.1 is a simple number, but in binary it cannot be expressed accurately. When we try to store 0.1 as float type, the computer converts it to the closest binary fraction, resulting in a loss of precision. For example:
float a = 0.1f; // The output may not be0.1,But similar0.100000024Approximate value of
2. Adding floating point numbers produces accuracy loss:
When we add two floating point numbers, if their fractional parts cannot be expressed accurately, the result of the addition may also have a loss of accuracy. For example:
float b = 0.2f; float c = a + b; // a is 0.1f defined in the above example// cProbably not0.3,But similar0.300000012Approximate value of
3. Adding floating point numbers produces accuracy loss:
When performing loops or conditional judgments, if comparisons of floating-point numbers are involved, logical errors may occur due to precision issues. For example, when judging whether two floating point numbers are equal, due to accuracy loss, use it directly==
Operators are often not the best choice.
5. Expansion: Comparison of float type with "zero value"
float
The "zero value" corresponding to the type is not0.0
, this is because floating point types lose precision. also,float
Type cannot be used==
or!=
For comparison, only<=
and>=
Construct an interval that excludes its "zero value" for comparison.
If only analyzing the theory, in C languageor c++
cfloat
In the file, aFLT_EPSILON
Constant. This constant is1.192092896e-07F
, represents the difference between 1 and the first (or minimum) single-precision floating point number greater than, that is, the theoretical minimum positive single-precision floating point number. Based on this constant, we can write the following statement:
#include<> // c++ for #include<cfloat>... float a = A certain floating point number; ... // is "zero value"if((a >= -FLT_EPSILON) && (a <= FLT_EPSILON)){ ... } // ---- and ---- // Not "zero value"if((a <= -FLT_EPSILON) || (a >= FLT_EPSILON)){ ... }
In addition, the double type also has the same limit constant, which is DBL_EPSILON, which can also be introduced from the or cfloat file, and will not be described here.
In the actual process, specific situation analysis is required, and problems such as measurement errors of readings must be considered, rather than simply using mathematical limits to eliminate "zero values". For example, a floating point reading indicating the measurement of the water level of a bucket. If the water of the bucket is empty, add water to it. At this time, it would not be reasonable if we use FLT_EPSILON to compare.
6. Expansion: Comparison of pointer variables with their "zero value"
The zero value of the pointer variable is "empty" (remembered asNULL
, in C and C++,NULL
The definitions are not the same.
//In C language, it is customary to define NULL as void* pointer value 0:#define NULL (void*)0 //In C++, NULL is clearly defined as an integer constant 0:// The source code that defines NULL#ifndef NULL #ifdef __cplusplus #ifndef _WIN64 #define NULL 0 #else #define NULL 0LL #endif /* W64 */ #else #define NULL ((void *)0) #endif #endif
In versions below C++11, the comparison of pointer variables with their "zero value" is essentially a comparison of pointer variables with NULL. In versions above C++11, due to the introduction of nullptr, the differences between C and C++ are unified, as follows:
//-----------------------------------------------------------------------------------------------------------------------------char* c; if(c == NULL){ ... } // ---- and ---- if(c != NULL){ ... } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------char* c; if(c == nullptr){ ... } // ---- and ---- if(c != nullptr){ ... }
Summarize
This is the end of this article about the usage of bool and float in C. For more related C language bool and float analysis content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!