当前位置:网站首页>Floating point memory storage problem
Floating point memory storage problem
2022-07-29 15:39:00 【Goku doesn't buy groceries】
Let's first look at the storage of integers
比如int i = 1,Take a look at its actual memory storage
Then let's take a look at the storage of floating point numbers
Floating point numbers are not directly stored in memory in binary form like integers,After parsing it according to a standard,stored in memory
For example, a floating point number is19.625
Its binary form is:10011.101
Its memory distribution is not as follows
Generally, we will convert this floating point number into the following form:
That is, it is stored in memory in the form of an index.
那么19.625In the form of the above, it is
Or look at its binary first:10011.101
The decimal point four left:1.0011101 * 2 ^4
This is stored in memory,So how exactly is it stored in bits?
先来看一下,A Bitwise Storage Rule for Single and Double Precision Floating Point Numbers
大致意思就是
for this number:1.0011101 * 2 ^4
符号位:0,Because it's a positive number
尾数位:0011101,The number of digits after the decimal point,This before the decimal point1呢,Just give up,哈哈
Here's the code:
阶码 = 阶数 + 偏移量
阶数 = 指数,Here the exponent is4,二进制就是100
To calculate the offset first we must know the following two information
1.Number of exponents in single precision 8
2.Double precision exponent digits11
We must know the number of exponents,To calculate the offset,So how to calculate the offset?
2^e-1 - 1,这里eis the number of digits.
Then the single-precision offset is:2^7 - 1 = 128 -1 =127,The double-precision offset is:2^11 - 1= 1024 - 1 = 1023
综上所述:code here = 4 + 127(Here I treat it as a single-precision floating-point number) = 131 ,二进制就是:10000011
所以19.625The actual memory distribution of the form is
OK,Then how do we store it in memory??
when considering storage,Still have to pay attention to the low byte stored in the low byte,High-order bits are stored in high-order bytes,还要注意,数据的存储顺序(Low order from left to right)with parsing order(从右往左解析)
上代码,Let's take a look at the memory
#include <stdio.h>
#include <stdlib.h>
void main()
{
float num = 19.625f;
printf("%p", &num);
system("pause");
return 0;
}
go to memory
Next I talk about the storage of negative floating point numbers,还是拿19.625来说
定义一个数据类型float num = -19.625
直接上代码,然后看内存
#include <stdio.h>
#include <stdlib.h>
void main()
{
float num = -19.625f;
printf("%p", &num);
system("pause");
return 0;
}
内存:
how did it becomec1了?
Let's take a look at the memory distribution map
换句话说,Negative floating point numbers are to store the source code directly.
Here's another question,精度缺失问题,比如10.6,this fractional part0.6,We're going to be binary
0.6 * 2 = 1.2
0.2 * 2 = 0.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6....开始循环1001
对于内存来说,It's not allowed to keep looping like this,所以 ,In limited space accordingly,Only certain data can be stored,The latter will be lost directly,所以,This is also the reason why decimals cause certain errors
边栏推荐
- NLP自然语言处理-机器学习和自然语言处理介绍(三)
- 兆骑科创赛事活动承办,项目路演,人才引进平台
- 自动化配置SSH免密登录和取消SSH免密配置脚本
- 药物研发---信息部门考核办法
- Linux安装MySQL(超详细)
- Guangzhou fire: high temperature weather frequent fire fire safety should not be ignored
- I quit my job after cutting the brothers, and turned to do a small clerk
- ES6 from entry to master # 11: the Map data type
- AOP实现企业级API访问接口监控(通过Google Guava缓存数据)
- 【yolov7系列二】正负样本分配策略
猜你喜欢
机器学习的3大“疑难杂症”,因果学习是突破口 | 重庆大学刘礼
如何创建NFT(还在创作中ing)
文档贡献与写作必读-OpenHarmony开发者文档风格指南
[yolov7 series two] positive and negative sample allocation strategy
Micro combat | centralized configuration service center Config asymmetric encryption and security management
如何获取本地json
NLP自然语言处理-机器学习和自然语言处理介绍(三)
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
【深度学习】深度学习刷SOTA的一堆trick
分析Nacos配置及源码
随机推荐
Linux环境 redis完整配置及启动命令
数据库管控平台-awr报告采集(mysql/oracle)
File management: logical structure
云原生Meetup·广州站举行,共话云原生时代的企业数字化转型
【 LeetCode 】 217. Duplicate elements
数据分析(一)
网络知识大集合(最详细)与网络通信过程
回放线上流量利器-GoReplay
Why does APP use the JSON protocol to interact with the server: serialization related knowledge
深度学习-神经网络
Google Play 政策更新 | 2022 年 7 月
极市直播丨严彬-Unicorn:走向目标跟踪的大一统(ECCV2022 Oral)
ES6 从入门到精通 # 11:Map 数据类型
Face key point prediction and normalization
Linux安装MySQL(超详细)
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
浮点数内存存储问题
【 LeetCode 】 1. The sum of two Numbers
ES6 from entry to master # 10: Set the Set data type
gateway基本使用