Detailed explanation of js floating point calculation problems
Analysis
JavaScript has only one number type Number
, and all numbers in Javascript are expressed in the IEEE-754 standard format. The precision problem of floating point numbers is not unique to JavaScript, because some decimals have infinite digits when expressed in binary:
0.1 0.0001100110011001100110011001100110011001100110011001101 0.2 0.001100110011001100110011001100110011001100110011001101 0.3 0.010011001100110011001100110011001100110011001100110011 0.5 0.1 0.6 0.10011001100110011001100110011001100110011001100110011
So for example 1.1
, the program actually cannot It really represents '1.1', but it can only be accurate to a certain extent. This is an inevitable loss of precision:
1.09999999999999999
In JavaScript, the problem is more complicated, here is just Give some test data in Chrome:
输入 输出1.0-0.9 == 0.1 False1.0-0.8 == 0.2 False1.0-0.7 == 0.3 False1.0-0.6 == 0.4 True1.0-0.5 == 0.5 True1.0-0.4 == 0.6 True1.0-0.3 == 0.7 True1.0-0.2 == 0.8 True1.0-0.1 == 0.9 True
Solution
So how to avoid this type of ` 1.0-0.9 != 0.1 ` non-bug type problems? The following is a commonly used solution. The calculation result is reduced in precision before judging the floating point operation result, because the precision reduction process will always be automatically rounded:
(1.0-0.9).toFixed(digits) // toFixed() 精度参数须在 0 与20 之间(1.0-0.9).toFixed(10)== 0.1 // 结果为True(1.0-0.8).toFixed(10)== 0.2 // 结果为True(1.0-0.7).toFixed(10)== 0.3 // 结果为True(11.0-11.8).toFixed(10) == -0.8 // 结果为TrueparseFloat((1.0-0.9).toFixed(10)) === 0.1 // 结果为TrueparseFloat((1.0-0.8).toFixed(10)) === 0.2 // 结果为TrueparseFloat((1.0-0.7).toFixed(10)) === 0.3 // 结果为TrueparseFloat((11.0-11.8).toFixed(10)) === -0.8 // 结果为True
Method refining
// 通过isEqual工具方法判断数值是否相等function isEqual(number1, number2, digits){ digits = digits || 10; // 默认精度为10return number1.toFixed(digits) === number2.toFixed(digits); } isEqual(1.0-0.7, 0.3); // return true// 原生扩展方式,更喜欢面向对象的风格Number.prototype.isEqual = function(number, digits){ digits = digits || 10; // 默认精度为10return this.toFixed(digits) === number.toFixed(digits); } (1.0-0.7).isEqual(0.3); // return true
<br>
The above is the detailed content of Detailed explanation of js floating point calculation problems. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Title: Is Tencent’s main programming language Go: An in-depth analysis. As China’s leading technology company, Tencent has always attracted much attention in its choice of programming languages. In recent years, some people believe that Tencent mainly adopts Go as its main programming language. This article will conduct an in-depth analysis of whether Tencent's main programming language is Go, and give specific code examples to support this view. 1. Application of Go language in Tencent Go is an open source programming language developed by Google. Its efficiency, concurrency and simplicity are loved by many developers.

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,
