/ / Math from JavaScript Math

Math from JavaScript Math

In JavaScript, the Math object does not need to be created,it exists as a compact mathematical browser coprocessor. Math is always available anywhere in the JavaScript code. It includes basic constants in the form of properties and popular mathematical functions in the form of methods.

JavaScript Math

JavaScript Math is a traditional math, everyday necessary, accessible and easy to use.

Math Properties

Basic mathematical constants:

  • the base of the natural logarithm (E);
  • natural logarithm (LN2, LN10);
  • logarithm of the number (LOG2E, LOG10E);
  • the Pi number (PI);
  • square roots (SQRT1_2, SQRT2).

To access the property of the global JavaScript Math object, you must specify the object name and the name of the required property. For example,

  • var exp = Math.E; // constant e ~ 2.718
  • var n314 = Math.PI; // the number Pi ~ 3.1415

javascript math object
Usually, variables do not describe in advance, butuse the properties of the JavaScript Math object directly in the expression. However, the algorithm may require its own approach. Mathematical constants in the form of properties of the Match object work in any syntactic JavaScript constructs.

A positive number and a string

If you want to get the absolute value of a number, use "abs", a function that makes any number positive.

  • Math.abs ("- 65"); // = 65
  • Math.abs (19); // = 19
  • Math.abs (0); // = 0

math round javascript
To convert a number to a string of characters from a specific base, the toString (rdx) function is applied directly to the variable. For example,

  • var iPos = (a / b) .toString (10);
  • var iPos = (a / b) .toString (2);
  • var iPos = (a / b) .toString (8);
  • var iPos = (a / b) .toString (16).

Here the parameter "rdx" is the base of the number system, respectively, decimal, binary, octal and hexadecimal. Instead of the expression "(a / b)", you can specify the name of the variable.

Conversion to the base of the number system -very useful function. When you need to generate a color code (write the desired sequence of hexadecimal digits) programmatically, it is simply indispensable.

Many CSS rules or styles of specific elements have to be formed on the fly or during the work of the site. The use of the JavaScript Math object can be found even on the simplest site.

Important. Expression:

  • var iPos = Math.abs ("- 1,3");

will not work as it should.

The result will be "NaN". Separation of the whole and fractional part in JavaScript is denoted by a period, not a comma. But the expression:

  • var iPos = ("100,33"). toString ();

will give the result: "100.33". In this case, it is written in the JavaScript Math style, but the result is formed as a string into a string.

In JavaScript code, serious errors are often not caused by the developer’s specific flaw in the algorithm, but by improperly taking into account the specific features of the language. cut off all unnecessary that is not provided or does not fit into the syntax.

The rounding functions of real numbers

Возможности объекта JavaScript Math по округлению The values ​​and results of evaluating expressions are very important. They are not as widely represented here as in other languages, but their functionality is quite enough for almost any task.

It is well known that in the CSS style rules,properties of DOM objects and other elements require integer arguments. In some cases, values ​​with a fractional part of one or more digits are required. In addition to the usual (mathematical) rounding - the Math.round function, JavaScript offers two more options: up (ceil) and down (floor).

As a rule, numerical data should beinteger or have a specific number of digits after the decimal point. When it is necessary to adjust the position of the decimal point in a number, it is convenient to use the classical idea of ​​dividing (multiplying) the desired number by 10, 100, 1000 ...

  • var x = Math.round (20.5); // = 21
  • var x = Math.ceil (1.1); // = 2
  • var x = Math.floor (1.1); // = 1
  • var x = Math.floor (1.11 * 10); // = 11

In the last example, the capabilities of the math functionThe floor javascript code uses somewhat incorrectly. Although much depends on the logic of a specific task. Adjusting the position of the decimal point by multiplying or dividing by 10, 100, 1000, ... is most convenient after rounding, rather than in front of it.

math floor javascript

Important.When using numbers as arguments, errors often occur precisely because the result of a calculation, for example, coordinates or block sizes, has a real or a string value that is not converted to a number.

Mathematical functions

In addition to the trigonometric functions: calculating sine (sin), cosine (cos) and tangent (tan), the object Math offers arcsine (asin), arc cosine (acos) and arctangent (atan).

You can also calculate the exponent (exp) - "e" to the degree, calculate the natural logarithm of the number (log), extract the square root of the number (sqrt) and raise the number to the desired degree (pow).

The use of math functions is similar to round, floor and ceil, except for the function pow, which has two arguments: the first is a number, the second is the degree to which it needs to be raised.

Of course, trigonometric functions are good.component of the JavaScript Math object, but do not get carried away. JavaScript is not a mathematical machine, but a browser language, the concern of which is to serve pages, DOM objects, to perform a lot of routine work.

Цель объекта лежит в предоставлении достаточного mathematical apparatus to perform specific work, and not to calculate integrals, fly to the moon and create a unique animation. Many sites that are fond of mathematics, work very slowly.

Minimum, maximum and random values

The implementation of the min and max functions implies a different number of compared numbers.

Both functions take a number of arguments: not necessarily only two arguments. The result of min is the minimum of the numbers transferred to the function, the result of max is the maximum.

If at least one argument is not specified, the result is "NaN".

The random function generates a pseudo-random number from 0 to 1. Its use is incredibly wide.

The author's example is not a picture

It uses the padl () function, whichadds a number to zero if it contains one digit. As a result of the viuq () call, a relatively random number will be received, but unique enough not to be repeated within one day.

If greater uniqueness is needed, you can call the function twice and connect the obtained values ​​or calculate the number with regard to seconds.

It is very fast and convenient to give any element of the page a file or picture name, a visitor's session, to control security using the random function.