当前位置:网站首页>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
边栏推荐
- ThreadLocal test multithreaded variable instance
- POI create and export Excel based on data
- 12.<tag-链表和常考点综合>-lt.234-回文链表
- General testing technology [1] classification of testing
- Unity Basics
- Test questions and some space wars
- C interface
- Detailed explanation of socket programming send() and recv() functions
- Docker拉取mysql并连接
- Oracle query foreign keys contain comma separated data
猜你喜欢
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
. net webapi access authorization mechanism and process design (header token + redis)
可以接收多种数据类型参数——可变参数
一文了解全面静态代码分析
Visual programming - drawing assignment
C interface
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
Unity games and related interview questions
可以接收多種數據類型參數——可變參數
随机推荐
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
关于idea调试模式下启动特别慢的优化
L3-011 direct attack Huanglong (30 points)
Flink实时数仓项目—DWS层设计与实现
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
TCP three handshakes and four waves
List interface of collection
MySQL query specifies that a row is sorted to the first row
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
可以接收多种数据类型参数——可变参数
C abstract class
Unity knowledge points (ugui 2)
ThreadLocal 测试多线程变量实例
可以接收多種數據類型參數——可變參數
Preview of converting doc and PDF to SWF file
浅学一下I/O流和File类文件操作
2022年做跨境电商五大技巧小分享
Unity games and related interview questions