Learn JavaScript with me floating-point precision _javascript tips

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators

Most programming languages have several numeric data types, but JavaScript has only one. You can use the TypeOf operator to see the type of numbers. Whether it's an integer or a floating-point number, JavaScript simply categorizes them as numbers.

typeof 17; Number
typeof 98.6;//number
typeof-21.3;//number

In fact, all the digits in JavaScript are double-precision floating-point numbers. This is a 64-digit coded number--"doubles"--developed 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-bit precision. All integers from –9 007 199 254 740 992 (–253) to 9 007 199 254-740 (992) are valid double-precision floating-point numbers. Therefore, although there is a lack of obvious integer types in JavaScript, it is entirely possible to perform integer operations.
Most arithmetic operators can be computed using a combination of integers, real numbers, or both.

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

However, bit arithmetic operators are more special. Instead of manipulating the operand directly as a floating-point number, JavaScript will implicitly convert it to a 32-bit integer and then perform the operation. (specifically, they are converted to integers represented by the 32-bit big-endian (Big-endian) complement of 2.) As an example of a bitwise OR operational expression:

8|1; 9

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

00000000000000000000000000001000

You can also use the number type ToString method to view:

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

The parameters of the ToString method specify its conversion cardinality, which is represented by cardinality 2 (that is, binary). The resulting value omits the extra 0 (bits) of the left end, because they do not affect the final value.
The integer 1 is represented as a 32-bit binary as follows:

00000000000000000000000000000001

Merges two bit sequences by bitwise OR operation expression. As long as any one of the two-bit bits involved in the operation is 1, the result of the operation is 1. The results in bit mode are as follows:

00000000000000000000000000001001

This sequence represents an integer 9. You can use the standard library function parseint validation, also with 2 as the base:

parseint ("1000", 2); 9

(again, leading 0 bits are unnecessary because they do not affect the result of the operation.) )
All bitwise operators work the same way. They convert the operands to integers, then use integer-bit mode to perform the operation, and finally convert the result to a standard JavaScript floating-point number. In general, the JavaScript engine needs to do some extra work to make these transformations. Because the number is stored as floating-point numbers, it must be converted to an integer and then returned to the floating-point numbers. However, in some cases, an arithmetic expression or even a variable can only use integer participation, and the optimization compiler sometimes infers these cases and internally stores the numbers as integers to avoid unwanted conversions.

The last warning about floating point numbers is that you should keep them on your toes. Floating point numbers seem familiar, but they are known to be imprecise. Even some seemingly simplest arithmetic operations will produce incorrect results.

0.1+0.2; 0.300000000000004

Although the 64-bit precision is already quite high, a double-precision floating-point number can only represent a limited set of numbers, not all of the set of real numbers. A floating-point operation can produce only approximate results, rounded to the nearest real number that can be represented. When you perform a series of operations, as rounding errors accumulate, the results of the operations become increasingly imprecise. Rounding also causes some unexpected deviations from the arithmetic laws that we normally expect. For example, the real number satisfies the binding law, which means that for any real x,y,z, it always satisfies (x + y) + z = x + (y + z).

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

Floating-point numbers weigh the precision and performance. When we care about precision, be careful about the limitations of floating-point numbers. An effective solution is to use integer numeric operations as much as possible, because integers do not need to be rounded when they are represented. When currency-related calculations are made, programmers typically convert the numeric value to the smallest unit of money to represent a second calculation, so that it can be evaluated as an integer. For example, if the above calculation is in U.S. dollars, then we can convert it to an integer representation of the cents to calculate.

(10+20) +30;
10+ (20+30);//60

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

Tips

    • The number of JavaScript is double-precision floating-point numbers.
    • Integers in JavaScript are only a subset of double-precision floating-point numbers, not a single data type
    • The bitwise operator treats a number as a 32-bit signed integer.

Above is the introduction of the JavaScript floating-point number, we should always pay attention to the precision of floating-point operations traps, I hope this article for everyone's learning help.

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.