Home > Web Front-end > JS Tutorial > body text

Learn javascript floating point precision with me_javascript skills

WBOY
Release: 2016-05-16 15:32:19
Original
911 people have browsed it

Most programming languages ​​have several numeric data types, but JavaScript has only one. You can use the typeof operator to check the type of a number. Regardless of whether they are integers or floating point numbers, JavaScript simply classifies them as numbers.

typeof 17; //number
typeof 98.6; //number
typeof -21.3; //number
Copy after login

In fact, all numbers in JavaScript are double-precision floating point numbers. These are 64-bit encoded numbers - "doubles" - specified by the IEEE754 standard. If this fact makes you wonder how JavaScript represents integers, remember that double-precision floating point numbers perfectly represent integers up to 53 digits of precision. All integers from –9 007 199 254 740 992 (–253) to 9 007 199 254 740 992 (253) are valid double-precision floating point numbers. So, despite the lack of obvious integer types in JavaScript, integer arithmetic is perfectly possible.
Most arithmetic operators can perform calculations using integers, real numbers, or a combination of both.

0.1 * 0.9; //0.19
-99 + 100; //1
21- 12.3; //8.7
2.5 /5; //0.5
21%8; //5
Copy after login

However, bit arithmetic operators are special. JavaScript does not directly operate on the operand as a floating point number, but implicitly converts it to a 32-bit integer before performing the operation. (To be precise, they are converted to 32-bit big-endian 2’s complement representation of integers.) Take the bitwise OR expression as an example:

8|1; //9
Copy after login

A seemingly simple expression actually requires several steps to complete the operation. As mentioned before, the numbers 8 and 1 in JavaScript are both double-precision floating point numbers. But they can also be represented as 32-bit integers, which are sequences of 32 bits 0 and 1. The integer 8 is represented as a 32-bit binary sequence as follows:

00000000000000000000000000001000

You can also use the toString method of the numeric type to view it yourself:

(8).toString(2) //"1000"

The parameter of the

toString method specifies its conversion base. This example is expressed in base 2 (i.e. binary). The resulting value omits the extra 0 (bits) on the left because they do not affect the final value.
The integer 1 is represented as a 32-bit binary as follows:

00000000000000000000000000000001

Bitwise OR expression combines two bit sequences. As long as any one of the two bits involved in the operation is 1, the bit in the operation result will be 1. The result expressed as a bit pattern is as follows:

00000000000000000000000000001001

This sequence represents the integer 9. You can use the standard library function parseInt to verify, also using base 2:

parseInt("1000", 2); //9
Copy after login

(Again, leading 0 bits are unnecessary as they do not affect the result of the operation.)
All bitwise operators work the same way. They convert the operands to integers, then perform operations using integer bit patterns, and finally convert the result to a standard JavaScript floating point number. Typically, the JavaScript engine needs to do some extra work to perform these conversions. Because the number is stored as a floating point number, it must be converted to an integer and then converted back to a floating point number. However, in some cases, arithmetic expressions or even variables can only be operated with integers, and optimizing compilers can sometimes infer these situations and store numbers as integers internally to avoid redundant conversions.

A final warning about floating point numbers is that you should always be wary of them. Floating point numbers may seem familiar, but they are notoriously imprecise. Even some seemingly simple arithmetic operations can produce incorrect results.

0.1 0.2; 0.300000000000004

Although the precision of 64 bits is quite high, double-precision floating point numbers can only represent a limited set of numbers, but cannot represent the entire set of real numbers. Floating point operations can only produce approximate results, rounded to the nearest representable real number. As you perform a series of operations, the results become less and less accurate as rounding errors accumulate. Rounding can also produce some unexpected deviations from the laws of arithmetic we normally expect. For example, real numbers satisfy the associative law, which means that for any real numbers x, y, z, (x y) z = x (y z) is always satisfied.

However, for floating point numbers, this is not always the case.

(0.1+0.2)+0.3; //0.60000000000000001
0.1+(0.2+ 0.3); //0.6

Copy after login

Floating point numbers trade off precision and performance. When we care about precision, be careful about the limitations of floating point numbers. An efficient solution is to use integer-valued arithmetic whenever possible, since integers are represented without rounding. When performing currency-related calculations, programmers often proportionally convert the value to the smallest monetary unit before performing the calculation, so that the calculation can be performed as an integer. For example, if the above calculation is in US dollars, then we can convert it to an integer representation of cents.

(10+20)+30; //60
10+ (20+30); //60
Copy after login

For integer operations, you don’t have to worry about rounding errors, but you still have to be careful that all calculations only apply to integers from –253 to 253.

Tips

  • JavaScript numbers are all double-precision floating point numbers.
  • Integers in JavaScript are just a subset of double-precision floating point numbers, not a separate data type
  • Bitwise operators treat numbers as 32-bit signed integers.

The above is the introduction of floating point numbers in JavaScript. We must always pay attention to the precision traps in floating point operations. I hope this article will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!