I am small and live in Wuhan, do two years of new media, ready to use 6 months time to switch to the front end of the industry.
Lesson Objective for today
Yesterday we learned the basic encoding methods and eval methods in Global objects based on search. Today is a search-based primer on Math object properties and basic methods. It’s a great day for learning. Go, small and !!!!
Today’s Lesson summary
- Math object Properties
- The min () and Max ()
- Round methods (ceil(),round(),floor())
Math object Properties
The property name | Attributes that |
---|---|
Math.E | Euler’s constant, also the base of the natural logarithm, is approximately 2.718. |
Math.LN2 | The natural log of 2 is approximately 0.693. |
Math.LN10 | The natural log of 10 is approximately 2.303. |
Math.LOG2E | Log base 2 of E is about 1.443. |
Math.LOG10E | Log base 10 of E is approximately 0.434. |
Math.PI | PI, the ratio of a circle’s circumference to its diameter, is approximately 3.14159. |
Math.SQRT1_2 | The square root of 1/2, which is also the reciprocal of the square root of 2, is approximately 0.707. |
Math.SQRT2 | The square root of 2 is approximately 1.414. |
The min () and Max ()
min()
Basic grammar
Math.min([value1[,value2, ...]])
Copy the code
Parameters that
- value1, value2, …
A set of numerical
Return Value Description
The smallest number of a given number.
If there are no arguments, the result is Infinity.
If any of the arguments cannot be converted to a numeric value, NaN is returned.
Detailed instructions
Math.min() returns a minimum of zero or more values.
Since min is a static method of Math, it should be used like this: math.min (), not as a method of the Math instance you create (Math is not a constructor).
case
The following example finds the minimum of x and y and assigns it to z
var x = 10, y = - 20;
var z = Math.min(x, y);
Copy the code
max()
Basic grammar
Math.max(value1[,value2, ...] )Copy the code
Parameters that
- value1, value2, …
A set of numerical
Return Value Description
Returns the maximum value of a given set of numbers.
If there are no arguments, the result is -infinity.
NaN is returned if at least one of the given arguments cannot be converted to a number.
Detailed instructions
Math.max() returns the maximum value of a set of numbers.
Since Max is a static method of Math, you should use: math.max () instead of the method of the created Math instance (Math is not a constructor).
case
Math.max(10.20); // 20
Math.max(- 10.- 20); / / - 10
Math.max(- 10.20); // 20
Copy the code
Rounding method
ceil()
Basic grammar
Math.ceil(x)
Copy the code
Parameters that
- x
A number.
Return Value Description
The smallest integer greater than or equal to the given number.
If the given argument cannot be converted to a number, 0 is returned.
Detailed instructions
Math.ceil() returns the smallest integer greater than or equal to a given number.
case
// Closure
(function() {
/ * * * Decimal adjustment of a number.
* * @param {String} type The type of adjustment. * @param {Number} value The number. * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base). * @returns {Number} The adjusted value. * / function decimalAdjust(type, value, exp) { // If the exp is undefined or zero... if (typeof exp === 'undefined' || +exp === 0) { return Math[type](value); } value = +value; exp = +exp; // If the value is not a number or the exp is not an integer... if (isNaN(value) || ! (typeof exp === 'number' && exp % 1= = =0)) { return NaN; } // Shift value = value.toString().split('e'); value = Math[type](+(value[0] + 'e' + (value[1]? (+value[1] - exp) : -exp))); // Shift back value = value.toString().split('e'); return +(value[0] + 'e' + (value[1]? (+value[1] + exp) : exp)); } // Decimal round if (!Math.round10) { Math.round10 = function(value, exp) { return decimalAdjust('round', value, exp); }; } // Decimal floor if (!Math.floor10) { Math.floor10 = function(value, exp) { return decimalAdjust('floor', value, exp); }; } // Decimal ceil if (!Math.ceil10) { Math.ceil10 = function(value, exp) { return decimalAdjust('ceil', value, exp); }; } }) (); // Round Math.round10(55.55.- 1); / / 55.6 Math.round10(55.549.- 1); / / 55.5 Math.round10(55.1); / / 60 Math.round10(54.9.1); / / 50 Math.round10(55.55.- 1); // -55.5 Math.round10(55.551.- 1); // -55.6 Math.round10(- 55.1); / / - 50 Math.round10(55.1.1); / / to 60 // Floor Math.floor10(55.59.- 1); / / 55.5 Math.floor10(59.1); / / 50 Math.floor10(55.51.- 1); // -55.6 Math.floor10(- 51.1); / / to 60 // Ceil Math.ceil10(55.51.- 1); / / 55.6 Math.ceil10(51.1); / / 60 Math.ceil10(55.59.- 1); // -55.5 Math.ceil10(59.1); / / - 50 Copy the code
round()
Basic grammar
Math.round(x)
Copy the code
Parameters that
- x
A number.
Return Value Description
The value of a given number is rounded to the nearest whole number.
Detailed instructions
Math.round() returns the nearest integer rounded to a number.
If the decimal part of the argument is greater than 0.5, the adjacent integers with greater absolute values are rounded. If the decimal part of the argument is less than 0.5, it is rounded to adjacent integers with smaller absolute values. If the decimal part of the argument is exactly 0.5, it is rounded to adjacent integers in the direction of positive infinity (+∞).
Note that unlike the round() function in many other languages, math.round () is not always rounded away from zero (especially if the fractional part of a negative number is exactly 0.5).
Because round() is a static method of Math, you should use math.round () directly, rather than as an instance method of the Math object you create (Math has no constructors).
case
x = Math.round(20.49); / / 20
x = Math.round(20.5); / / 21
x = Math.round(20.5); / / - 20
x = Math.round(20.51); / / - 21
Copy the code
floor()
Basic grammar
Math.floor(x)
Copy the code
Parameters that
- x
A number.
Return Value Description
A number representing the largest integer less than or equal to the specified number.
Detailed instructions
Math.floor() returns the largest integer less than or equal to a given number.
Since floor is a static method of Math, you should always use it math.floor () like this, rather than as a method of a Math object you create (Math is not a constructor).
case
Math.floor( 45.95);
/ / 45
Math.floor( 45.05);
/ / 45
Math.floor( 4 );
/ / 4 Math.floor(45.05); / / - 46 Math.floor(45.95); / / - 46 Copy the code
Summary of today’s lesson
Today the mood
Today I will focus on searching for Math object properties and basic methods, and hope to learn more tomorrow ~~~~
This article is formatted using MDNICE