Course Content#
Mathematical Operations in C Language#
-
-
= Assignment
- Can be combined with other operators: +=, <<=, ...
-
/ Division
- Both sides are integers: integer division, rounding down
- At least one side is a floating-point number: the result is a floating-point number
-
() Increase operation precedence
-
⭐Bitwise Operations, high efficiency
- Computers store different data types uniformly as binary data, i.e., stored by bits
- 8 bits → 1 byte
- The int type is 32 bits, and for signed integers, the maximum representable value is 2^31-1
- In bitwise operations, ① first expand to binary representation, ② only the corresponding bits have relationships
- Bitwise operations do not support floating-point numbers?
- It is meaningless. For a 64-bit floating-point number, the highest bit is the sign bit S, the next 11 bits are the exponent E, and the remaining 52 bits are the significant digits M.
- If bitwise AND is applied, what are the rules for the first 12 bits? What is the significance of the result of the exponent bits being ANDed? This is the essence; there is no mathematical meaning in ANDing two floating-point numbers.
- ^ XOR
- Characteristics: it is the inverse operation of itself; supports commutative and associative laws
- Inverse operation
- Premise: conforms to the commutative law
- Subtraction and division do not conform
- Subtraction is the inverse operation of addition, but not vice versa
- Division is the inverse operation of multiplication, but not vice versa [not strictly, does not consider the multiplier being 0]
- Premise: conforms to the commutative law
-
<< Left Shift >> Right Shift
- Left shift by one bit, multiply by 2
- Right shift by one bit, divide by 2, rounding down, sign unchanged
- The left side continuously fills the sign bit (the highest bit is the sign bit for signed numbers)
- The left side forcibly fills with 0 (for unsigned numbers)
Mathematical Functions in C Language#
Header file: math.h
Note: The abs() function is in stdlib.h
Prototype | double pow(double a, double b); | Exponential function |
---|---|---|
- | double sqrt(double x); | Square root |
- | double ceil(double x); | Ceiling |
- | The return type is still double, as double can represent numbers beyond the range of int | |
- | double floor(double x); | Floor |
- | int abs(int x); | Integer absolute value; stdlib.h |
- | double fabs(double x); | Real number absolute value; f may have previously represented float |
- | double log(double x); | Natural logarithm |
- | double log10(double x); | Base 10 logarithm |
- | Change of base formula can be used to find logarithms with any base: log(2)8 = log(8) / log(2) | |
- | double acos(double x); | Arccosine function: arccos() |
Return value: radian value of the angle ⭐π = acos(-1) |
In-Class Exercises#
-
- The control character for double type is “%lf”
- The control character for scanf must correspond to the type of the parameter, otherwise it will not read in
-
double x;
-
When using scanf, be sure to use “%lf” for correspondence, not “%f”, otherwise:
-
- printf is not as strict; the control character does not have to match the type exactly, there should be type conversion
- Code
-
- Cleverly using pow, the one-third power represents the cube root
- ⭐1/ 3**.0** instead of 1 / 3
- When compiling with gcc, add the math library, -lm
-
-
-
-
- Note that π can be obtained more accurately using acos(-1)
- Code
- Dividing first and then multiplying is safer and less likely to overflow
Highlight Notes#
- The control character for double type is “%lf”
- π can be obtained more accurately using acos(-1)
Code Demonstration#
Exercises#
-
-
Using XOR for swapping is an interesting idea, but it is not actually recommended
- It can only be used for integers to perform XOR
- The speed is not faster than using temp
- Swapping the same variable will result in 0: swap(a, a)
Additional Knowledge Points#
- Operator Precedence and Associativity Table-Baidu
- The operation order of *p-- is ① p's address - 1, ② then use * to get the value at that address
- (*p)-- is ① first get the variable value at p's address, then subtract 1 from the variable's value
- The operation order of *p-- is ① p's address - 1, ② then use * to get the value at that address
- XOR Properties
- a ^ a = 0;
- 0 ^ a = a;
- Can find a number that appears only once in an array where all other numbers appear twice
- Xshell runs C language without warnings
- g++ *.cpp -Wall
- Using this option allows all useful warnings that GCC can provide
- You can also use -W{warning} to specify certain warnings.
-
- g++ *.cpp -Wall
Points for Consideration#
- What are the benefits of using XOR operations for variable swapping?
- Saves memory for a third-party variable
- Principle consideration
- Just perform the inverse operation
- Or
- XORing itself results in 0
* Any variable XORed with 0 remains unchanged - Considering the above principle, can it also be done using +- operations?
- a' = a + b
- b = a' - b = a
- a = a' - b = a' - a = b
- However, this approach does not utilize the efficiency of bitwise operations, and it must ensure that the sum of the two numbers does not overflow~
Tips#
-
C Language Documentation in Chinese-Geek Academy Translation
- The C language has about 29 function libraries
-
Reference textbook chapters
-