当前位置:网站首页>Javescript 0.1 + 0.2 = = 0.3 problem
Javescript 0.1 + 0.2 = = 0.3 problem
2022-07-03 02:21:00 【weixin_ forty-nine million thirty-five thousand four hundred an】
According to the definition of double precision floating point number ,Number The valid integer range in type is -0x1fffffffffffff to 0x1fffffffffffff, therefore Number Can't represent integers outside this range exactly .
Again, according to the definition of floating point numbers , Non integer Number Type unavailable ==(=== Not good either. ) To compare , A famous piece of code , This is also the problem of our third question , Why is it JavaScript in ,0.1+0.2 You can't =0.3:
console.log( 0.1 + 0.2 == 0.3);
The output here is false, Explain that the two sides are not equal , This is the characteristic of floating point operation , It is also the source of many students' doubts , The precision problem of floating-point operation leads to that the left and right results of the equation are not strictly equal , It's a tiny difference .
So actually , The wrong conclusion here is not , It's a method of comparison , The right way to compare is to use JavaScript The minimum accuracy value provided :
console.log( Math.abs(0.1 + 0.2 - 0.3) <= Number.EPSILON);
Check whether the absolute value of the difference between the left and right sides of the equation is less than the minimum accuracy , Is the right way to compare floating-point numbers . The result of this code is true 了 .
边栏推荐
- GBase 8c系统表-pg_amop
- Socket编程
- Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
- Thread safe singleton mode
- Coroutinecontext in kotlin
- Deep learning notes (constantly updating...)
- The use of Flink CDC mongodb and the implementation of Flink SQL parsing complex nested JSON data in monggo
- awk从入门到入土(2)认识awk内置变量和变量的使用
- stm32F407-------ADC
- stm32F407-------ADC
猜你喜欢
《上市风云》荐书——唯勇气最可贵
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
Use go language to realize try{}catch{}finally
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
The use of Flink CDC mongodb and the implementation of Flink SQL parsing complex nested JSON data in monggo
stm32F407-------DMA
Detailed introduction to the deployment and usage of the Nacos registry
MySQL学习03
Memory pool (understand the process of new developing space from the perspective of kernel)
随机推荐
Deep learning notes (constantly updating...)
Leetcode(540)——有序数组中的单一元素
机器学习笔记(持续更新中。。。)
Pytorch convolution network regularization dropblock
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
Ni visa fails after LabVIEW installs the third-party visa software
[Yu Yue education] China Ocean University job search OMG reference
Apple releases MacOS 11.6.4 update: mainly security fixes
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
GBase 8c系统表pg_database
【教程】chrome关闭跨域策略cors、samesite,跨域带上cookie
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
What are MySQL locks and classifications
GBase 8c 函数/存储过程参数(一)
Create + register sub apps_ Define routes, global routes and sub routes
力扣(LeetCode)183. 从不订购的客户(2022.07.02)
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
Startup mode and scope builder of collaboration in kotlin
[Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
GBase 8c系统表-pg_conversion