JavaScript type System--math Object

Source: Internet
Author: User
Tags acos arithmetic operators asin cos natural logarithm pow sin square root

xTable of Contents [1] constants [2] before the function

JavaScript uses arithmetic operators to implement basic arithmetic operations, and if more complex arithmetic operations are to be implemented, the constants and functions defined by the Math object need to be implemented. Unlike other objects, math is just a static object and does not have the math () constructor. In fact, math is just an object namespace set by JavaScript for storing mathematical constants and functions. This article describes the Math object in detail

New Math (); // uncaught Typeerror:math is not a constructor

Constant

There are 8 constants in the Math object, including the logarithm, the allocation value and the square root three kinds of

Logarithmic
The base of the 
 MATH.E natural logarithm, which is the value of the constant E (approximately equal to 2. ) The natural logarithm of MATH.LN2 2 (approximately equal to 0.  693  ) The natural logarithm of math.ln10 10 (approximately 2. ) 303  ) math.log2e a logarithm of base e of 2 (approximately equal to 1. ) 443  ) math.log10e a logarithm of base e of 10 (approximately equal to 0. ) 434 ) 
Console.log (MATH.E); // 2.718281828459045Console.log (MATH.LN2); // 0.6931471805599453     Console.log (MATH.LN10); // 2.302585092994046Console.log (MATH.LOG2E); // 1.4426950408889634Console.log (math.log10e); // 0.4342944819032518

Pi Value
            the value of the Math.PI pie (approximately equal to 3.  Console.log (Math.PI); // 3.141592653589793

Square root
         the square root of the Math.sqrt2 2 (approximately equal to 1.  414) math.sqrt1_2       The square root of1/2, which is the reciprocal of the square root of 2 (approximately 0.  707)
Console.log (MATH.SQRT2); // 1.4142135623730951Console.log (math.sqrt1_2); // 0.7071067811865476

Function

There are 18 static functions in the Math object, including maximum, rounding, random number, absolute value, trigonometric function, and the root 6 class.

[note] These functions involve a number () implicit type conversion. If out of range, Nan is returned

Best Value

The min () and Max () methods of the Math object are used to determine the minimum and maximum values in a set of values, both of which can receive any numeric parameter

Math.max ()

Returns the maximum value in a parameter. Returns-infinity if there are no arguments. If either parameter is Nan or is not convertible to a number, Nan is returned

Math.min ()

Returns the minimum value in the parameter. Returns infinity if there are no arguments. If either parameter is Nan or is not convertible to a number, Nan is returned

Console.log (Math.min (1,2,3));//1Console.log (Math.max (1,2,3));//3Console.log (Math.min ());//InfinityConsole.log (Math.max ());//-infinityConsole.log (Math.min (1,2,'3px'));//NaNConsole.log (Math.max (1,2,'3px'));//NaN

"Tips" finds the maximum or minimum value in the array

var values = [1,2,3,4,5,6,7,8 ]; var maxValue = Math.max.apply (math,values); // 8 var minValue = Math.min.apply (math,values); // 1

Rounding

The Math object has a total of three decimal methods that are rounded to integers: Math.ceil (), Math.floor (), and Math.Round ()

Math.ceil ()

Performs an up-rounding operation, which means that it returns the nearest integer greater than or equal to the function parameter

Note Math.ceil () does not turn negative numbers into larger negative numbers, but instead takes them to the direction of 0

Math.floor ()

Performs a down-rounding operation, which means that it returns the nearest integer less than or equal to the function parameter

Note Math.floor () also rounded down the negative numbers, i.e. the number will be smaller

Math.Round ()

Perform rounding and rounding operations

Note Math.Round (x) + Math.Round (-X) = = 0;//true, when x is a number

Console.log (Math.ceil (12.6)); // Console.log (Math.floor (12.6)); // Console.log (Math.Round (12.6)); // Console.log (Math.ceil (-12.6)); // -12Console.log (Math.floor (-12.6)); // -13Console.log (Math.Round (-12.6)); // -13

Random number

Math.random ()

The method returns a random number greater than or equal to 0 less than 1

Console.log (Math.random ()); // 0.590752829178167

Apply the following formula to randomly select a value from an integer range using Math.random ()

Value = Math.floor (Math.random () * Total number of possible values + first possible value)
// returns an integer value between 1-10 var Ten 1)

In most cases, the known condition is the end-to-end value

var 1) + lowervalue);
// Take a random item out of an array var colors = [1,2,3,4,5,6]; var color = Colors[math.floor (Math.random () *colors.length)];

Absolute

Math.Abs ()

The method returns the absolute value of any numeric value

Console.log (Math.Abs (-1)); // 1console.log (math.abs ('1px')); // NaNConsole.log (Math.Abs (1,2,3)); // 1

Trigonometric function

There are 7 functions in the Math object that involve trigonometric functions, namely sine, cosine, tangent, inverse, inverse cosine, arc tangent, and y/x tangent value.

Math.sin (x)

Returns the sine of x with a return value between 1 and 1

Math.Cos (x)

Returns the cosine of x with a return value between 1 and 1

Math.tan (x)

Returns the tangent value of X

[Note that the]x is an angle in radians, and if you want to convert the angle system to radians, you can multiply the value of the angle system by 0.017 (2 pi/360)

Math.asin (x)

Returns the inverse sine of x, with a return value between-pi/2 to pi/2 radians (x must be a number between 1 and 1)

Math.acos (x)

Returns the inverse cosine of x, with a return value between 0 and the number of radians (x must be 1 to 1)

Math.atan (x)

Returns the inverse tangent of x with a return value between-pi/2 to pi/2 radians

Math.atan2 (y,x)

Returns the inverse tangent of the y/x, the return value of which is the y-coordinate at which the faction can regard y as a point, and X as the x-coordinate of the point

[Note that the]y coordinates are in front of the X coordinate

Console.log (Math.sin ( -*math.pi/ the));//0.49999999999999994Console.log (Math.Cos ( -*math.pi/ the));//0.5000000000000001Console.log (Math.tan ( $*math.pi/ the));//0.9999999999999999Console.log (Math.asin (1)* the/MATH.PI);// -Console.log (Math.acos (1)* the/MATH.PI);//0Console.log (Math.atan (1)* the/MATH.PI);// $Console.log (Math.atan2 (1,1)* the/MATH.PI);// $

Square root

The math object involves a total of 4 functions

Math.exp (num)

Returns the NUM power of MATH.E, which is the enum

Console.log (Math.exp (0)); // 1console.log (math.exp (1)); // 2.718281828459045

Math.log (num)

Returns the natural logarithm of NUM, logenum (num must be a number greater than or equal to 0)

Logx = 1og e* logex log2x = Log2e * Logex
function log10 (x) {    return math.log10e * Math.log (x);} function log2 (x) {    return math.log2e * Math.log (x);}
Console.log (Math.log (1)); // 0Console.log (Math.log (MATH.E)); // 1

MATH.SQRT (num)

Returns the square root of num (x must be a number greater than or equal to 0)

Console.log (math.sqrt ()); // TenConsole.log (math.sqrt (1)); // 1

Math.pow (Num,power)

Returns the power sub power of num

Console.log (Math.pow (2)); // Console.log (Math.pow (1/2)); // Ten

Resources

"1" Ruan one peak JavaScript standard reference Tutorial--standard Library--math object http://javascript.ruanyifeng.com/stdlib/math.html
"2" W3school-javascript Advanced Tutorial--math Object http://www.w3school.com.cn/jsref/jsref_obj_math.asp
"3" JavaScript authoritative Guide (6th edition), part III JavaScript core reference
"4" JavaScript Advanced Programming (3rd Edition), chapter 5th reference type

JavaScript type System--math Object

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.