In flash, the math class is a very common class. Through mathematical computation, we can achieve a lot of wonderful results. Here we will start from the most basic learning and add a math knowledge.
Let's take a look at the math class provided in flash:
Math. Abs () calculates the absolute value.
Math. ACOs () calculates the arccosine value.
Math. asin () calculates the arcsin value.
Math. atan () calculates the arc tangent value.
Math. atan2 () calculates the point-to-point angle from the X axis.
Math. Ceil () rounds up the number to the nearest integer.
Math. Cos () calculates the cosine value.
Math. exp () calculates the exponent value.
Math. Floor () rounds down the number to the nearest integer.
Math. Log () is used to calculate the natural logarithm.
Math. Max () returns one of the two integers.
Math. Min () returns a smaller integer.
Math. Pow () is used to calculate the Power Y of X.
Math. Random () returns a pseudo-random number between 0.0 and 1.0.
Math. Round () is rounded to the nearest integer.
Math. Sin () calculates the sine value.
Math. SQRT () calculates the square root.
Math. Tan () calculates the tangent.
The following describes the 18 basic functions ~!
Let's start with a few simple things and get rid of them ~!
1. Math. Abs () is used to calculate the absolute value formula. What is the absolute value? This should not be a problem. We all know that numbers have positive and negative numbers, so we can calculate positive and negative values. Through this function, we can convert negative values to positive values without changing them. we can verify it using the following statement:
X = 5
Y =-5
X = math. Abs (X)
Trace (X)
// Output 5
Y = math. Abs (y)
Trace (y)
// Output 5
2. Math. Ceil () gets the integer of the upper limit of the specified number or expression, that is, the integer greater than the specified number. The following code can be used for verification:
X = 5.45
X = math. Ceil (X)
Trace (x) // output 6
Note: For negative numbers, the greater the absolute value, the smaller the value. Therefore, the integer obtained using math. Ceil () is the negative integer smaller than the absolute value of the number. The following code describes:
X =-5.45
X = math. Ceil (X)
Trace (x) // output-5
3. Math. floor () and math. the opposite is Ceil (). When an integer or an integer with a lower limit of a number or expression is obtained, the obtained integer is smaller than the specified number. Note: The method for obtaining negative numbers is the same as that for math. Ceil (), but it is only an integer with an absolute value. The following can be verified:
X = 5.45
X = math. Floor (X)
Trace (x) // output 5
X =-5.45
X = math. Floor (X)
Trace (x) // output-6
4. Math. Round (): the method used by the function should be the most familiar to us. It is rounded to the nearest integer to an expression or number. Let's use the code to verify it ~!
X = 5.45
X = math. Round (X)
Trace (x) // output 5
X =-5.45
X = math. Round (X)
Trace (x) // output-5
X =-5.55
X = math. Round (X)
Trace (x) // output-6
But here we find a problem that requires attention. If
X = 5.50
X = math. Round (X)
Trace (x) // output 6
However, if X =-5.50
X =-5.50
X = math. Round (X)
Trace (x) // output-5 Note ~!
5. the math. Random () random function obtains a decimal point of 14 digits ranging from 0 to 1, such as 0 <= x <1. You can use it to obtain numbers of different digits. Next we use him and the function we have studied above to obtain a random four-digit number.
X = math. Random ()
X = 10000 * x
X = math. Round (X)
Trace (x) // output 4478
Of course, flash also provides the random function random (value), which can also obtain a random integer between 0 and value-1, there are a lot of detailed instructions on the Internet, so I will not talk about it here. I will use a piece of code to verify it ~!
X = random (5)
Trace (X)
// The output is any value of 0, 1, 2, 3, 4 ~!
6. Math. Max () calculates the maximum values of two numbers or expressions and returns this value.
X = math. Max (5, 4)
Trace (x) // The output is 5
X = 10
Y = 20
X = math. Max (x, y)
Trace (x) // The output is 20
7. Math. Min () returns the minimum value of two numbers or expressions.
X = math. Min (5, 4)
Trace (x) // The output is 4
X = 10
Y = 20
X = math. Min (x, y)
Trace (x) // The output is 10.
8. The math. Pow () parameter is X, and Y uses math. Pow (x, y) to calculate the Power Y of X, and the calculation result is returned.
X = math. Pow (2, 3) // calculate the 3rd power of 2
Trace (x) // The output is 8
X = 3
Y = 4
Z = math. Pow (x, y) // calculate the yth power of X
Trace (z) // output 81
9. Math. SQRT () calculates the square root of a number or expression. Note that the expression here should be a number greater than or equal to 0.
X = math. SQRT (2) // calculate the start of 2
Trace (x) // output 1.4142135623731
X = 3
X = math. SQRT (x) // calculate the square of X
Trace (x) // output 1.73205080756888
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.