Math object in JS

Source: Internet
Author: User
Tags mathematical functions
Math object
Javascript provides a wealth of mathematical processing tools, which are much more useful than most script developers who do not have background knowledge in computer science or mathematics. But every real Program Design languages require these features, which programmers will use to make windows more vivid on the screen.

The math object contains all these capabilities. This object is different from other JavaScript objects because you cannot generate copies of the objects in use. Instead, the script aggregates the attributes and methods of a simple math object. The math object actually exists in every window and framework, but it has no impact on the script. Programmers call this fixed object a static object. Math objects (starting with an upper case m) are part of these attributes and method references. Math object attributes are usually constants, such as the square root of π and 2:

VaR pivalue = math. Pi
VaR rootoftwo = math. sqrt2

Math object methods include a wide range of trigonometric functions, and other mathematical functions that have been defined in the script to process values. For example, you can find the largest of the two values:

VaR larger = math. Max (value1, value2)

Or you can get a 10th power of the number:

VaR result = math. Pow (value1, 10)

Perhaps more commonly used is to round a value to the nearest integer:

VaR result = math. Round (value1)

Another common requirement for math objects is random numbers. Although this feature is abandoned by the windows and Macintosh versions of navigator 2, it will be included in all versions later. The math. Random () method returns a floating point number between 0 and 1. If you design a script for a card game, you need 1 ~ A random number between 52, or a dice game. The range of each dice is 1 ~ 6. To generate a random integer between 0 and any upper limit, use the following format:

Math. Floor (math. Random () * (n + 1 ))

Here N is the upper limit, and math. Floor returns the integer part of any floating point number. To generate a random number between any number, use the following format:

Math. Floor (math. Random () * n) + 1

Here N is equal to the upper limit of the range. For a dice game, the rule for each dice is:

Newdievalue = math. Floor (math. Random () * 6) + 1

Except ie5.5 and nn6, JavaScript does not provide a method to specify the number format. floating point can display more than a dozen digits after the decimal point. Furthermore, the operation results are affected by the specific floating point numbers on each operating system platform, especially in earlier versions of the script browser. For versions earlier than ie5.5 and nn6, you must program your own script to format each number, such as USD and cents.

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.