当前位置:网站首页>Advanced C language: data storage (floating point)
Advanced C language: data storage (floating point)
2022-07-22 14:03:00 【Tiger Taro's inheritance】
Preface
This article is a supplement to the storage of data in memory , Explore floating point .
Floating point storage in memory
Common floating point numbers :
3.14159
1E10
The family of floating-point numbers includes :float、double、long double type .
The range represented by floating point numbers :float.h In the definition of
The representation of floating-point numbers inside a computer :
According to international standards , Any binary floating point number V It can be expressed in the following form :
( -1 ) ^ S * M * 2 ^ E
( -1 ) ^ S The sign bit , When S=0 ,V Is a positive number ; When S=1, V It's a negative number .
M Represents a significant number , Greater than or equal to 1, Less than 2 .
2^E Indicates the index bit .
You can compare the scientific counting method of decimal system
-152.45 Namely - 1.5245 * 10 ^ 2
Example :
#include<stdio.h>
int main()
{
int n = 9;
float* pFloat = (float*)&n;
printf("n The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
*pFloat = 9.0;
printf("nume The value of is :%d\n", n);
printf("*pFloat The value of is :%f\n", *pFloat);
return 0;
}
num and *pFloat It's the same number in memory , Why are the interpretation results of floating-point numbers and integers so different ?
Another example :
Decimal 5.0, Written as binary is 101.0, amount to 1.01x2^2.
that , According to the above V The format of , You can get S=0,M=1.01,E=2.
Decimal -5.0, Written as binary is -101.0, amount to -1.01x2^2.
that ,S=1.M=1.01,E=2.
IEEE 754 Regulations :
about 32 Floating point number of bits , The highest 1 Bits are sign bits s, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.
about 64 Floating point number of bits , The highest 1 Bits are sign bits S, And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M.
IEEE 754 For significant figures M And the index E, There are some special rules .
As I said before ,1≤M<2, in other words ,M It can be written. 1.xxxxxx In the form of , among xxxxxx Represents the fractional part .
IEEE 754 Regulations , Keep it in the computer M when , By default, the first digit of this number is always 1, So it can be discarded , Save only the back xxxxxx part . For example preservation 1.01 When , Save only 01, Wait until you read , Put the first 1 Add . The purpose of this , It's saving 1 Significant digits . With 32 For example, a floating-point number , Leave to M Only 23 position , Will come first 1 After giving up , It's equivalent to being able to save 24 Significant digits .
As for the index E, The situation is more complicated .
First ,E For an unsigned integer (unsigned int)
It means , If E by 8 position , Its value range is 0255; If E by 11 position , Its value range is 02047. however , We know , In scientific counting E You can have negative numbers , therefore IEEE 754 Regulations , In memory E The true value of must be added with an intermediate number , about 8 Bit E, The middle number is 127; about 11 Bit E, The middle number is 1023. such as ,2^10 Of E yes 10, So save it as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
then , Index E Fetching from memory can be further divided into three cases :
E Not all for 0 Or not all of them 1
At this time , Floating point numbers are represented by the following rules , The index E The calculated value of minus 127( or 1023), Get the real value , And then the significant number M Add the first 1.
for instance :
0.5 The binary form of is 0.1 , Since it is stipulated that the positive part must be 1 , That is to move the decimal point to the right 1 position , Then for
1.0 * 2 ^ ( - 1) ,S by 0 ; E by -1+127=126 , Expressed as 01111110 ; And the mantissa M = 1.0 Remove the integer part and make it 0 , A filling 0 To 23 position
E All for 0
At this time , The exponent of a floating point number E be equal to 1-127( perhaps 1-1023) That's the true value , Significant figures M No more first 1, It's reduced to 0.xxxxxx Decimals of . This is to show that ±0, And close to 0 A very small number of .
E All for 1
At this time , If the significant number M All for 0, Express ± infinity ( It depends on the sign bit s);
边栏推荐
- Is it safe for Huatai Securities to open an account? Can the handling charge be in case?
- NVIDIA programmable inference accelerator tensorrt learning notes (I) -- start
- Auto. JS learning note 18: sub thread and timer are used together (an example is at the end)
- [ManageEngine] seven ways to strengthen the security of enterprise privileged access
- OpenCV 常用的API总结(迅速查询)
- Network counting -- network layer
- 看完这个,还不会DVMA,请你吃瓜
- esp8266模拟输入(ADC)检测问题
- Best practices for three node hybrid deployment
- 异常检测 and 自编码器(2)
猜你喜欢
JUC-并发包
NVIDIA可编程推理加速器TensorRT学习笔记(一)——开始
The undeclared identifier "MainWindow" is used in the QT code of Kirin system“
文件操作下(C语言)
寻找斐波那契数
PADS画板框
ASM学习系列(二)
Super dry! Thoroughly understand the differences and connections between simplex, half duplex and full duplex
Cyberspace mapping
Tidb high concurrency write scenario best practices
随机推荐
JUC memory
Idea decompiles the entire jar package source code
Android interview question: tell me the difference between pendingintent and intent
Restore the word form of the sentence
基于SSM+JSP+MYSQL+H-UI 实现的火锅店点餐系统
Machine learning - Integrated Learning
Wheel 5: qcustomplot common classes
MySQL grant之后一定要跟随flush privileges么?
多线程与高并发—— Synchronized 加锁解锁流程
Rare earth Developers Conference | streamnational Zhai Jia and Liu Dezhi share the road of cloud native technology transformation
Esp8266 analog input (ADC) detection problem
PADS画板框
Kubernetes调度概念与工作流程
Notes and Thoughts on the red dust of the sky (I) all results are conditional possibilities
异常检测 and 自编码器(2)
Interview 3 (multiple processes call the same dynamic library)
Read data nearby under the three data centers
Do you have to follow flush privileges after MySQL grant?
易基因 | 简化基因组DNA甲基化测序(RRBS)实验怎么做?
Implementing DDD based on ABP -- domain service, application service and dto practice