As we all know, in the basic language, the system provides many standard functions, and "getting the entire function" is a very important function.
1. formats and functions of the "retrieve the entire function.
1. Format: int (X)
2. function: obtain the maximum integer not greater than X.
3. Description: Int Is the function name and cannot be changed. X is the independent variable. It can be a numerical constant, a numerical variable, or a numerical expression.
Example: int (3.1416) = 3
INT (3.8752) = 3
INT (-3.14) =-4
INT (-3.85) =-4
From the above question, we can see that for positive numbers with decimals, int
After the integer is obtained, the fractional part is rounded off without rounding. For a negative number with a decimal number, the integer is not directly rounded off, it is an integer 1 smaller than its integer. Of course, for a real integer, the value after int remains unchanged.
II. Application of "getting the entire function"
1. Rounding numeric values
(1) The integer part of the X value is retained, and the fractional part is rounded to the nearest integer.
Expression: int (x * 100 + 0.5)
For example:
INT (3.1416 + 0.5) = int (3.6416) = 3
INT (3.8572 + 0.5) = int (4.3572) = 4
INT (-3.14 + 0.5) = int (-2.64) =-3
INT (-3.85 + 0.5) = int (-3.35) =-4
By analyzing the above example, we can see that the key to rounding the integer function with int is 0.5. From the perspective of the number axis, we can add 0.5 to a number, it is equivalent to moving the value 0.5 to the right. The first digit after the decimal point is less than 5 or greater than or equal to 5, determines whether the number is an integer during the moving to the right, because the value of the int function is the maximum integer on the left. If an integer is passed, the result is the integer. Otherwise, the result is the same as that of the original number. In this way, the goal may be rounded down.
(2) retain two decimal places for the value of X, rounding the third decimal places
Expression: int (x * 100 + 0.5)/100
For example:
INT (3.1416*100 + 0.5)/100
= Int (314.16 + 0.5)/100
= Int (314.66)/100
= 314*100
= 3.14
INT (3.8572*100 + 0.5)/100
= Int (385.72 + 0.5)/100
= Int (386.22)/100
= 386/100
= 3.86
This rounding reserve is only different from the reservation above 1 in the decimal place. We only need to try to change the decimal place, so we use the method to first increase X by 100 times, then, use the first method to select decimal places, and then reduce the number by 100 times. This will not affect the basic size of the number, but also round the number.
Conclusion 1
N decimal places are retained for the X value. The general expression for rounding n + 1 decimal places is:
INT (x * 10 ^ N + 0.5)/x * 10 ^ n
2. Determine whether a single m can be divisible by N
For example, judge the parity of a number, that is, whether it can be divisible by 2
M = 25 m = 24
M/2 = 12.5 m/2 = 12
INT (M/2) = 12 INT (M/2)
Through the above expression, we can easily conclude that 25 is an odd number, 25/2 <> int (25/2), 24 is an even number, 24/2 = int (24/2 ), the Int function can remove the fractional part. For a number M, M/2 can be equal to int (M/2) only when m can be divisible by 2 ), therefore, the expression of this question can be written:
When M/2 <> int (M/2), M is an odd number
When M/2 = int (M/2), M is an even number
Conclusion 2
Number m can be divisible by number N: M/N = int (M/N)
M cannot count n integer division: M/N <> int (M/N)
Iii. Differences between CINT (X) and fix (X)
3. CINT (x) rounds the decimal part of X to an integer.
Fix (x) truncates the fractional part to get the integer
The following table compares the values of the three functions:
X int (x) CINT (x) fix (X)
3.26 3 3 3
3.76 3 4 3
-3.26-4-3-3
-3.76-4-4-3:
Conclusion 3
When x> = 0, INT (x) has the same value,
When X is <0, the total value of INT (X) is 1;
CINT (X) is an integer rounded to the decimal part of X. Its function is the same as that of INT (x + 0.5 ).