Learn javascript floating point precision with me_javascript skills
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
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
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
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 thetoString 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
(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
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
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.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.