Learn how to use javascript's floating point precision _ javascript skills

Source: Internet
Author: User
I want to learn about the precision of javascript floating-point numbers, so that you can understand the real precision of JavaScript floating-point numbers, you can refer to the several numeric data types in most programming languages, but JavaScript has only one type. You can use the typeof operator to view the numeric type. JavaScript classifies integers and floating-point numbers as numbers.

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

In fact, all numbers in JavaScript are double-precision floating-point numbers. This is a 64-bit encoded number developed by the IEEE754 Standard-"doubles ". If this fact puzzles you about how JavaScript represents integers, remember that double-precision floating-point numbers perfectly represent up to 53-bit integers. 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. Therefore, although JavaScript lacks an obvious Integer type, it can be used for integer operations.
Most arithmetic operators can be calculated using integers, real numbers, or a combination of the two.

0.1 * 0.9; //0.19-99 + 100; //121- 12.3; //8.72.5 /5; //0.521%8; //5

However, bitwise Arithmetic Operators are special. JavaScript does not directly use the operand as a floating point number, but implicitly converts it to a 32-bit integer. (To be exact, they are converted to the 2's complement representing the 32-bit big-endian .) Take the bitwise OR expression as an example:

8|1; //9

A seemingly simple expression actually requires several steps to complete the computation. As mentioned above, numbers 8 and 1 in JavaScript are double-precision floating point numbers. But they can also be expressed as 32-bit integers, namely the 32-Bit 0, 1 sequence. The 32-bit binary sequence of integer 8 is as follows:


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

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

The toString parameter specifies the conversion base. In this example, the base number is 2 (Binary. The result values omit the extra 0 (BITs) of the Left end because they do not affect the final value.
The integer 1 represents a 32-bit binary as follows:


Merge two bits in a bitwise OR expression. If any one of the two bits involved in the operation is 1, the bitwise of the calculation result is 1. The result in bitwise mode is as follows:


This sequence represents an integer of 9. You can use the standard library function parseInt for verification, which also uses 2 as the base:

parseInt("1000", 2); //9

(Likewise, leading 0 bits are unnecessary because they do not affect the operation results .)
All bitwise operators work in the same way. They convert the operand to an integer, then use the integer mode for calculation, and finally convert the result to a standard JavaScript floating point number. In general, the JavaScript engine needs to do additional work for these conversions. Because a 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 and even variables can only use integers for computation. The optimization compiler can sometimes infer these situations and store numbers as integers to avoid extra conversions.

The last warning about floating point numbers is that you should always be vigilant against them. Floating point numbers seem familiar, but they are notoriously inaccurate. Even some seemingly simplest arithmetic operations produce incorrect results.

0.1 + 0.2; 0.300000000000004

Although the 64-bit precision is already quite high, the double-precision floating point number can only represent a limited number, not all real number sets. Floating-point operations can only generate approximate results, rounded to the nearest real number that can be expressed. When you perform a series of operations, the calculation results become increasingly inaccurate as the rounding error accumulates. Rounding will also lead to some unexpected deviations from our usual law of arithmetic operations. For example, the real number satisfies the combination law, which means that for any real number x, y, z, it always satisfies (x + y) + z = x + (y + z ).

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

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

Floating Point Numbers weigh both precision and performance. When we are concerned about precision, we should be careful with the limitations of floating point numbers. An effective solution is to use integer operations as much as possible, because integers do not need to be rounded during representation. When performing a currency-related calculation, programmers usually convert the value to the smallest monetary unit in proportion and then perform the calculation, so that the calculation can be performed as an integer. For example, if the above calculation is in USD, we can convert it to the cent represented by an integer for calculation.

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

You don't have to worry about Rounding Errors for integer operations, but you should be careful that all calculations only apply to-253 ~ An integer of 253.


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

The above is the javascript floating point number. We should always pay attention to the precision traps in floating point operations. I hope this article will help you learn more.

Related Article

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.