Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JavaScript perform ordinary arithmetic tasks

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "JavaScript how to perform ordinary math tasks", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "JavaScript how to perform ordinary math tasks" this article.

JavaScript Math object

The purpose of the Math object is to perform common arithmetic tasks.

The Math object provides a variety of arithmetic value types and functions. There is no need to define this object before using it.

Arithmetic value

JavaScript provides eight arithmetic values that can be accessed by Math objects:

Constant

Pi

The square root of 2

The square root of 1 prime 2

Natural logarithm of 2

The natural logarithm of 10

The logarithm of e with base 2

The logarithm of e with a base of 10

This is how these values are used in Javascript (one-to-one corresponding to the arithmetic values above):

Math.E

Math.PI

Math.SQRT2

Math.SQRT1_2

Math.LN2

Math.LN10

Math.LOG2E

Math.LOG10E

Arithmetic method

In addition to the arithmetic values that can be accessed by the Math object, there are several functions (methods) that can be used.

The following example uses the round method of the Math object to round a number.

[xss_clean] (Math.round (4.7)) / / the code output is: 5

The following example uses the random () method of the Math object to return a random number between 0 and 1:

[xss_clean] (Math.random ()) / / the code output is: 0.9370844220218102

The following uses the floor () method of the Math object and random () to return a random number between 0 and 10:

[xss_clean] (Math.floor (Math.random () * 11)) / / the code output is: 3

Round () rounds the number to the nearest integer:

[xss_clean] (Math.round 0.60) + ") [xss_clean] (Math.round 0.50 +") [xss_clean] (Math.round 0.49 + ") [xss_clean] (Math.round (- 4.40) +") [xss_clean] (Math.round (- 4.60))

Random () returns a random number between 0 and 1:

[xss_clean] (Math.random ()) / / for example, 0.2788411558140069 is returned

Max () returns the larger of the two given numbers:

[xss_clean] (Math.max (5pjm 7) + ") [xss_clean] (Math.max (- 3helm 5) +") [xss_clean] (Math.max (- 3mam Mehran 5) + ") [xss_clean] (Math.max (7.25pr 7.30))

Min () returns the smaller of two given numbers:

[xss_clean] (Math.min (5pjm 7) + ") [xss_clean] (Math.min (- 3helm 5) +") [xss_clean] (Math.min (- 3mam Mehran 5) + ") [xss_clean] (Math.min (7.25pr 7.30))

Properties and description of Math object

E / / returns the arithmetic constant e, the base of the natural logarithm (about 2.718). LN2 / / returns the natural logarithm of 2 (about 0.693). LN10 / / returns the natural logarithm of 10 (about 2.302). LOG2E / / returns the logarithm of e with a base of 2 (about 1.414). LOG10E / / returns the logarithm of e with a base of 10 (about 0.434). PI / / returns pi (about 3.14159). SQRT1_2 / / returns the reciprocal of the square root of 2 (about 0.707). SQRT2 / / returns the square root of 2 (about 1.414).

Math object method and description

The absolute value of the number returned by abs (x) / /. Acos (x) / / the inverse cosine of the return number. The arcsine of the number returned by asin (x) / /. Atan (x) / / returns the arc tangent of x as a value between-PI/2 and PI/2 radians. Atan2 (y PI/2 x) / / returns the angle from the x axis to the point (x focus y) (between-and PI/2 radians). Ceil (x) / / logarithm is rounded. Cos (x) / / the cosine of the returned number. Exp (x) / / returns the exponent of e. Floor (x) / / logarithmic rounding. The natural logarithm of the log (x) / / return number (base e). Max (xPowery) / / returns the highest values of x and y. Min (xPowery) / / returns the lowest values in x and y. Pow (xPowery) / / returns the y power of x. Random () / / returns a random number between 0 and 1. Round (x) / / rounds the number to the nearest integer. Sin (x) / / the sine of the returned number. Sqrt (x) / / the square root of the number returned. Tan (x) / / the tangent of the return angle. ToSource () / / returns the source code of the object. ValueOf () / / returns the original value of the Math object. These are all the contents of the article "how to perform ordinary arithmetic tasks in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report