Course Content#
Mathematical Operations in C Language#
-
-
= Assignment
- Can be combined with other operators: +=, <<=, ...
-
/ Division
- Both operands are integers: Integer division, rounding down
- At least one operand is a floating-point number: The result is a floating-point number
-
() Increase the priority of operations
-
⭐ Bitwise Operations, high efficiency
- Computers store different data types in binary format, i.e., in bits
- 8 bits → 1 byte
- int type is 32 bits, and the maximum value for a signed int is 2^31-1
- During bitwise operations, ① the binary representation is expanded, ② only the corresponding bits are related
- Bitwise operations do not support floating-point numbers?
- It doesn't make sense. For a 64-bit floating-point number, the highest bit is the sign bit S, followed by 11 bits for the exponent E, and the remaining 52 bits for the significand M.
- What would be the rule for the first 12 bits in bitwise AND? What would be the meaning of the result of the exponent bits being ANDed? This is the essence, bitwise AND of two floating-point numbers has no mathematical meaning.
- ^ XOR
- Characteristics: It is its own inverse operation; supports commutative and associative laws
- Inverse operation
- Premise: Complies with commutative law
- Subtraction and division do not comply
- Subtraction is the inverse operation of addition, but not vice versa
- Division is the inverse operation of multiplication, but not vice versa [Not strictly, not considering the multiplicand being 0]
- Premise: Complies with commutative law
-
<< Left Shift >> Right Shift
- Left shift by one bit is equivalent to multiplying by 2
- Right shift by one bit is equivalent to dividing by 2, rounding down, with the sign unchanged
- The sign bit (the highest bit for signed numbers) is continuously filled on the left
- The left side is forcibly filled with 0 (for unsigned numbers)
Mathematical Functions in C Language#
Header file: math.h
Note: 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 value is still double because double can represent numbers beyond the range of int | |
- | double floor(double x); | Floor |
- | int abs(int x); | Absolute value of integer; stdlib.h |
- | doublefabs(double x); | Absolute value of real number; f may represent float in the past |
- | double log(double x); | Natural logarithm |
- | double log10(double x); | Base 10 logarithm |
- | Change of base formula can be used to calculate logarithms with any base: log(2)8 = log(8) / log(2) | |
- | double acos(double x); | Inverse cosine 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 argument, otherwise it cannot be read
-
double x;
-
When using scanf, you must use "%lf" to correspond, cannot use "%f", otherwise:
-
- printf is not as strict as scanf, the control character does not have to be exactly the same as the type, there may be type conversion
- Code
-
- Cleverly using pow, the power of one-third represents the cube root
- ⭐1/ 3**.0** instead of 1 / 3
- When compiling with gcc, you need to 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
Key Notes#
- The control character for double type is "%lf"
- π can be obtained more accurately using acos(-1)
Code Demonstration#
Exercise#
-
-
Using XOR for swapping variables is interesting, but not recommended
- Only applicable to integers for XOR operation
- Not faster than using a temporary variable
- Swapping the same variable will result in 0: swap(a, a)
Additional Knowledge#
- Operator Precedence and Associativity-Baidu
- The operation order of *p-- is ① the address pointed to by p minus 1, ② then * takes the value at that address
- (*p)-- is ① taking the value of the variable pointed to by p, then ② subtracting 1 from the value of the variable
- The operation order of *p-- is ① the address pointed to by p minus 1, ② then * takes the value at that address
- XOR rules
- a ^ a = 0;
- 0 ^ a = a;
- Can be used to find the number that appears only once in an array where all other numbers appear twice
- xshell does not report warnings when running C language
- g++ *.cpp -Wall
- Use this option to enable all useful warnings that GCC can provide
- You can also use -W{warning} to mark specific warnings.
-
- g++ *.cpp -Wall
Points to Ponder#
- What are the advantages of using XOR operation for variable swapping?
- Saves memory for a third variable
- Principle of thinking
- Inverse operation is sufficient
- Or
- XOR with itself is 0
* XOR with 0 does not change the value of any variable - Thinking based on the principle above, can we use +- operations to do the same thing?
- a' = a + b
- b = a' - b = a
- a = a' - b = a' - a = b
- However, this does not utilize the efficiency of bitwise operations, and it is necessary to ensure that the sum of the two numbers does not overflow~
Tips#
-
C Language Documentation in Chinese-Translated by Geek Academy
- There are about 29 function libraries in C language
-
Reference tool book chapter
-