Notes for Math of functions in javaScript

  • 2020-06-15 07:41:18
  • OfStack

1. You cannot explicitly create an Math object, you can simply use it;

2.Math objects cannot store data, which is different from String and Date objects;

3. We know that the parseInt() function will turn a decimal into an integer (so 24.999 becomes 24) by eliminating the 1 cut behind the decimal point.

Here are the methods of the Math object:

round(): When the decimal is 0.5 or greater than 0.5, go up 1 place;

ceil(): always rounding up, so 23.75 becomes 24, so 23.25;

floor(): always rounding down, so 23.75 becomes 23, so 23.25;


<DOCTYPE html>
<html>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <head>
    <title>Math function </title>
  </head>
  <script type="text/javascript">
    var userInput=prompt(" Please enter the 1 The number of ","");
    document.write("round()=",+Math.round(userInput));
    document.write("ceil()=",+Math.ceil(userInput));
    document.write("floor()=",+Math.floor(userInput));
    
  </script>
  <body>
  </body>
</html>

4. You can use the random() method of the Math object to generate a random decimal that is greater than or equal to 0 but less than 1. Usually in order to take advantage of it, you have to multiply by something, and then use one of the rounding methods.


var diceThrow=Math.round(Math.random()*6)+1;
document.write("You threw a "+diceThrow);

Attach methods for the Math object

1. Discard decimal parts and keep integer parts
parseInt(5/2)
2. If you round up, add 1 to the whole number if you have a decimal
Math.ceil(5/2)
3,4 rounded into 5.
Math.round(5/2)
4. Round it down
Math.floor(5/2)
Methods for the Math object
FF: Firefox, N: Netscape, IE: Internet Explorer
Methods FF N IE were described
abs(x) returns the absolute value of the number 1, 2, 3
acos(x) returns the arccosine of the number 1, 2, 3
asin(x) returns the arcsine of the number 1, 2, 3
atan(x) returns the arctangent value of x 1, 2, 3 with a value between -ES64en /2 and PI/2 radians
atan2(y,x) returns the Angle (between -ES74en /2 and PI/2 radians) from the x axis to the point (x,y) 1, 2, 3
ceil(x) rounds up a number. 1 2 3
cos(x) returns the cosine of the number 1, 2, 3
exp(x) returns the index of e. 1 2 3
floor(x) rounds down a number. 1 2 3
log(x) returns the natural log of the number (base e) 1, 2, 3
max(x,y) returns the highest value of x and y 1, 2, 3
min(x,y) returns the lowest value of x and y 1, 2, 3
pow(x,y) returns the y power of x 1, 2, 3
random() returns a random number between 0 and 1, 1, 2, 3
round(x) rounds a number 4 to 5 as the nearest integer 1, 2, and 3
sin(x) returns the sine of the number 1, 2, 3
sqrt(x) returns the square root of the number 1, 2, 3
tan(x) returns the tangent of 1 Angle, 2, 3
toSource() represents the object's source code 1 4 -
valueOf() returns the original value of 1 Math object

Math.abs(num) : Returns the absolute value of num
Math.acos(num) : Returns the arccosine value of num
Math.asin(num) : Returns the arcsine of num
Math.atan(num) : Returns the arctangent value of num
Math.atan2(y,x) : returns the inverse tangent of the quotient of y divided by x
Math.ceil(num) : Returns the smallest integer greater than num
Math.cos(num) : Returns the cosine of num
8. Math.exp(x) : Returns a number to the power x with a natural number as its base
Math.floor(num) : Returns the largest integer less than num
10.Math.log(num) : Returns the natural log of num
11.Math.max(num1,num2) : returns the larger of num1 and num2
Math.min(num1,num2) : Returns the smaller of num1 and num2
Math.pow(x,y) : Returns the value of x to the y power
Math.random() : Returns 1 random number between 0 and 1
15. Math. round(num) : Returns the value of num4 rounded into 5
Math.sin(num) : Returns the sine of num
Math.sqrt(num) : Returns the square root of num
Math.tan(num) : Returns the tangent of num
19.Math.E: Natural Numbers (2.718281828459045)
Natural logarithm of Math 2:2 (0.6931471805599453)
21.Math.LN10: Natural logarithm of 10 (2.302585092994046)
22. Math. LOG2E: log 2 at the bottom of natural Numbers (1.4426950408889634)
23. Math. LOG10E: log 10 at the bottom of natural Numbers (0.4342944819032518)
24. Math. PI: PI (3.141592653589793)
Math 1_2: square root of 1/2 (0.7071067811865476)
Math.SQRT2: Square root of 2 (1.4142135623730951)

This is the end of this article, I hope you enjoy it.


Related articles: