当前位置:网站首页>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 了 .
边栏推荐
- Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
- PyTorch 卷积网络正则化 DropBlock
- Unrecognized SSL message, plaintext connection?
- The use of Flink CDC mongodb and the implementation of Flink SQL parsing complex nested JSON data in monggo
- easyPOI
- 我的创作纪念日
- GBase 8c 函数/存储过程定义
- Job object of collaboration in kotlin
- Create + register sub apps_ Define routes, global routes and sub routes
- GBase 8c系统表-pg_amop
猜你喜欢

elastic stack

oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???

微服务组件Sentinel (Hystrix)详细分析

8 free, HD, copyright free video material download websites are recommended

基于线程池的生产者消费者模型(含阻塞队列)

Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux

RestCloud ETL 跨库数据聚合运算

stm32F407-------DMA

MySQL学习03
![[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)](/img/ac/bf83f319ea787c5abd7ac3fabc9ede.jpg)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
随机推荐
PyTorch 卷积网络正则化 DropBlock
GBase 8c系统表pg_cast
oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???
Iptables layer 4 forwarding
The Sandbox阐释对元宇宙平台的愿景
easyPOI
Return a tree structure data
5. File operation
Detailed introduction to the usage of Nacos configuration center
线程安全的单例模式
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
基于线程池的生产者消费者模型(含阻塞队列)
[codeforces] cf1338a - Powered addition [binary]
【教程】chrome關閉跨域策略cors、samesite,跨域帶上cookie
stm32F407-------DMA
GBase 8c系统表pg_database
GBase 8c系统表-pg_class
MySQL learning 03