当前位置:网站首页>JS - accuracy issues
JS - accuracy issues
2022-04-23 03:26:00 【Open the door of the world】
I'm learning today js In the process , A precision problem was found 2^53 -1
To find out
- According to international standards IEEE 754,javascript Floating point 64 Binary bits , From the far left , It's made up of
The first 1 position : Sign bit ,0 It means a positive number ,1 A negative number (1 position )
The first 2 Position to the first 12 position : Storage index section (11 position )
The first 13 Position to the first 64 position : Save decimal part ( It's a significant number 52 position , remove Sign bit and exponent bit )
The sign bit determines the plus or minus of a number , The exponential part determines the size of the number , The decimal part determines the numerical precision

IEEE 754 Regulations , The first significant number by default is always 1, Don't save it in 64 Bit floating point number .
in other words , Significant numbers are always 1.xx...xx In the form of , among xx..xx Part of the is saved in 64 Bit floating point number , The longest possible is 52 position
therefore ,javascript The most significant number provided is 53 Binary bits
The maximum accuracy is 53 Binary bits , Means that the absolute value is less than 2 Of 53 The whole number of the power , namely ±(2^53-1), Can accurately represent
Look at a few demo
0.1+0.2!=0.3
0.1 Expressed as binary
0.0001100110011001100110011001100110011001100110011001101
0.2 Expressed as binary
0.001100110011001100110011001100110011001100110011001101
Add up in the computer , just
0.0100110011001100110011001100110011001100110011001100111
To convert to decimal is 0.30000000000000004
Explain why other situations are similar to 0.9+0.1==1 Well ?
This is because although the added binary number is not exactly equal to 1, But the extra value 1.0000000000000000000000000000000000000000000000000000001
Is too small , It was abandoned
resolvent
If the accuracy requirement is not high , direct (0.1+0.2).toFixed(2) == 0.3
utilize ES6 Number new property EPSILON Math.abs(0.1+0.2-0.3)<Number.EPSILON
Utilization Library mathjs,math.format(math.eval(‘0.1 + 0.2’))
版权声明
本文为[Open the door of the world]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220619451179.html
边栏推荐
- 研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
- socket编程 send()与 recv()函数详解
- 批量下载文件----压缩后再下载
- Optimization of especially slow startup in idea debugging mode
- Use of ADB command [1]
- C interface
- C-11 problem h: treasure chest 2
- 可以接收多種數據類型參數——可變參數
- MySQL keyword group_ Concat, combined connection query
- 2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
猜你喜欢

Supersocket is Use in net5 - concept

Peut recevoir plusieurs paramètres de type de données - paramètres variables

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)

When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director

Unity basics 2

L3-011 direct attack Huanglong (30 points)

Configuration table and page information automatically generate curd operation page

QT dynamic translation of Chinese and English languages

Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
随机推荐
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
QT uses drag and drop picture to control and mouse to move picture
ThreadLocal test multithreaded variable instance
Test questions and some space wars
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Log4net is in Net core usage
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
Detailed explanation of socket programming send() and recv() functions
Advanced sorting - fast sorting
js递归树结构计算每个节点的叶子节点的数量并且输出
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
QT dynamic translation of Chinese and English languages
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Punch in: 4.22 C language chapter - (1) first knowledge of C language - (11) pointer
第四次作业
Scenario Title: how does system a use the page of system B
Use of ADB command [1]
New ORM framework -- Introduction to beetlsql
. net 5 Web custom middleware implementation returns the default picture
. NETCORE sets the API post mode, which can accept parameters directly in parentheses