Article directory
Determine whether a number of type double is an integer
Mod 1 == 0 is enough, both Java and js can be used, but there are also precision problems, for example: 1.00000000000000001 % 1, its result is 0.
The above writing method is usually not a big problem in writing, and it can be used. However, in projects in specific fields, please refer to Ali’s recommended writing method to define a variable ɛ that is small enough to meet the actual needs of your project. This value is set to Double.MIN_VALUE in Java, for example, and the absolute value of subtracting two numbers is less than this variables, we consider them equal.
sum of consecutive numbers
The sum of consecutive series with a spacing of 1 (for example: 78,79,80)
and the sum of continuous series: (head+tail)*number of items/2
For example: 78+79+80+81=(81+78)*4/2
Common methods of math
Math is called a mathematical function, and it is also an object data type, mainly used to manipulate numbers
1, Math.abs() to find the absolute value
Math.abs(-1)
2. Math.ceil/Math.floor is rounded up and rounded down Rounded up,
whether it is a positive or negative number, take the largest value
Rounded down, whether it is a positive or negative number, take the smallest value
Math.ceil(1.2)
2
Math.ceil(-1.6)
-1
Math.floor(1.8)
1
Math.floor(-1.1)
-2
3. Math.round() rounds to
a positive number, it is still normal, I understood it before, but if it is a negative number, the critical point must be greater than 5
Math.round(1.5)
2
Math.round(-1.5)
-1
Math.round(-1.51)
-2
4. Math.sqrt() square root
Math.sqrt(9)
3
5. Math.pow(n,m) takes the power
of n to the power of m
Math.pow(3,2) ==> 9
6、Math.PI
Math.PI ===>3.141592653589793
7. Math.max/Math.min get the maximum and minimum values
Math.max(1,2,3)
Math.min(4,5,6)
8. Math.random() obtains a random number between 0 and 1 (greater than or equal to 0 and less than 1)
obtains a random number between n and m:
Math.random()*(m-n)+n;
// Get a random number between 10 and 20
Math.random()*10+10
a.Math.random is the number of [0,1];
b. Use the following formula when taking a random integer of [min,max]:
Math.floor(Math.random().(max-min+1)+min)
When c takes a random integer of [min,max], use the following formula
Math.floor(Math.random().(max-min)+min)
When d takes a random integer of [min,max], use the following formula
Math.floor(Math.random().(max-min)+min+1)