当前位置:网站首页>Why 0.1+0.2=0.3000000000000004
Why 0.1+0.2=0.3000000000000004
2022-06-10 17:40:00 【luoganttcc】


0.1 and 0.2 How to express ?
According to the previous explanation , Decimal system 0.1 Convert to binary decimal , Get is 0.0001100… ( repeat 1100) Such a circular binary decimal , Use IEEE754 It is shown in the figure below :

For this 0.1 Binary encoding of binary data ,
Then cut the code , How to cut , From left to right , find first 1 Just cut it down , Cut such a long number into two ends
| 0001100110011001100110011001100110011001100110011001101 |
|---|
| Index | mantissa |
|---|---|
| 0001 | 100110011001100110011001100110011001100110011001101 |
You can find that there are four people in front of you , yes 0001 , So the exponent of this floating point number is -4 , Why negative numbers , It should be a decimal , It's counting to the right ,
The second picture above shows , The exponent bits of floating point numbers are 8 A bit , It can express 2 8 = 256 2^8=256 28=256 Count ,float A type must express either a very large number or a very small number of absolute values , 0 and 255 Corresponding to two special cases , code 1-254 Then corresponding in sequence Index range -126-127
| Decimal index | Floating point number eight digit exponential decimal encoding | Floating point eight bit exponential binary encoding |
|---|---|---|
| -126 | 1 | 0000 0001 |
| -125 | 2 | 0000 0010 |
| … | … | … |
| -4 | 123 | 0111 1011 |
| 1 | 128 | 1000 0000 |
| 127 | 254 | 1111 1110 |
in other words , chart 2 Inside the binary code by 127+(-4)=123 =0111 1011
| Index | 23 Potential mantissa | Rounding item | Truncate item |
|---|---|---|---|
| 0001 | 10011001100110011001100 | 1100 | 110011001100110011001101 |
| first 1 Division | The last item in the mantissa is 0, But the rounding term is 0.75, Need to enter 1 | 1 ∗ ( 0.5 ) − 1 + 1 ∗ ( 0.5 ) − 2 1 ∗ ( 0.5 ) − 3 1 ∗ ( 0.5 ) − 4 = 0.75 1*(0.5)^{-1}+1*(0.5)^{-2}1*(0.5)^{-3}1*(0.5)^{-4}=0.75 1∗(0.5)−1+1∗(0.5)−21∗(0.5)−31∗(0.5)−4=0.75 | |
| 10011001100110011001101 |
On the rounding of floating point numbers ,IEEE Floating point format defines 4 There are different ways of rounding , As shown in the following table . among , The default rounding method is to round even numbers , The other three can be used to calculate the upper and lower bounds .
Next, let's consider the mantissa , Digit occupancy 23 A bit , If you are directly in the mantissa Intercept 23 A bit , that Floating point numbers will always be less than the number we need to express , You cut off a small tail of a dragon , The dragon must have lost weight . So be sure to add rounding rules , There is no simple rounding
Same method ,0.2 Expressed as a single precision floating-point number is :0.20000000298023223876953125. therefore ,0.1 + 0.2 The result is :0.300000004470348358154296875

use python Code To achieve
# Convert decimal decimals to binary
def dec2bin(x):
x -= int(x)
bins = []
while x:
x *= 2
bins.append(1 if x>=1. else 0)
x -= int(x)
return bins
#print()
#cc=dec2bin(0.1)
0.1 The binary representation of
cc1=dec2bin(0.1)
#cc1=0001100110011001100110011001100110011001100110011001101
0.2 The binary representation of
cc2=dec2bin(0.2)
#cc1=001100110011001100110011001100110011001100110011001101
0.1 Standard floating point count format
cc11=0
for k in range
边栏推荐
猜你喜欢

matplotlib plt.text()的具体用法——画图时给图中的点加标签

Numpy learning notes

What should be done to improve the service level of the park and optimize the business environment

重庆第一个科创板IPO,来了
![[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)](/img/75/a3336aa7314a2dfc9a7a32995793e7.png)
[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)
Example analysis of SQL injection error reporting

Solve the problem that idea is stuck in opening a project
Redis operation set, Zset, hash data types and use of visualization tools

Flood control and drainage monitoring automatic monitoring and telemetering terminal for flood control and drainage

单片机底层通信协议① —— 同步和异步、并行和串行、全双工和半双工以及单工、电平信号和差分信号
随机推荐
How MySQL modifies field type and field length
运筹说 第64期丨动态规划奠基人——理查德·贝尔曼
每日一题-1287. 有序数组中出现次数超过25%的元素
线上交流丨技能网络:解决多任务多模态问题的稀疏模型(青源Talk第19期 唐都钰)
使用KuboardSpray安装kubernetes(v1.23.1)
李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
重庆第一个科创板IPO,来了
For more than 20 years, there are only Durex, Okamoto and jasbon in the condom market
Nat. Rev. Drug Discov. | AI在小分子药物发现中的应用:一个即将到来的浪潮?
Simple site backup shell script
【玩转华为云】手把手带你使用鲲鹏代码迁移工具实现源码迁移
HTML+PHP+Mysql登录注册页面
Fabric.js 锁定背景图,不受缩放和拖拽的影响
路由器实验之serial接口的静态路由配置(补充)
Nacos配置管理
【玩转华为云】鲲鹏DevKit迁移实战
Fabric. JS centered element
蓝桥杯_挑选子串_组合数学_乘法原理_ / 尺取法
成立1年便成独角兽,腾讯滴滴做「靠山」,今年新晋的独角兽不简单
Nat. Commun. | Knowledge integration and decision support for accelerating the discovery of antibiotic resistance genes