当前位置:网站首页>2. Floating point number, the difference between float and double in C language and how to choose them
2. Floating point number, the difference between float and double in C language and how to choose them
2022-07-28 20:05:00 【A programmer who loves playing badminton】
- First we need to know ,float,double stay C The language means The set of real Numbers , And decimals in C In language The default is double type .
- float It belongs to single precision floating-point data ,double It belongs to double precision floating-point data . The meaning of this sentence is double The accuracy of higher than float.float Occupy 4 Bytes of memory , and double Occupy 8 Bytes .
- float Only available 6 Significant digits , Generally speaking, absolutely 6 position , Probably 7 It should be 6~7 position ,double Can provide 16 Significant digits .
- Generally speaking ,CPU Processing single precision floating-point numbers is faster than processing double precision floating-point numbers , If you don't declare , The default decimal is double type , So if you want to use float Words , It has to be forced to turn ,eg:float a = 1.24546f;
float a = 1.24324556; double b = 1.24324556; float c = 1.24324556; double d = 1.24324556; printf("a=%f\n", a); printf("b=%f\n", b); printf("c=%.8f\n", c); printf("d=%.8f\n", d);
1. We go through a and b The output of can be seen ,%f perhaps %lf Indicates that the default output is six decimal places , Less than six to 0 A filling , More than six digits are rounded off .
2. adopt c and d The output of can be seen %.8f It means to output eight decimal places , and float type Of C Because it can only be kept after the decimal point 6~7 position , So it is rounded , and double Type of d It can output accurately .
How to select and use float still double Well ?
For programmers ,double and float Is the difference between the double High precision , Significant figures 16 position ,float precision 7 position . but double Memory consumption is float Twice as many ,double The speed of computing is faster than float Much slower ,C The name of mathematical function in language double and float Different , Don't write wrong , Don't use double precision when you can use single precision ( To save memory , Speed up the calculation ).
边栏推荐
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- Const pointer of C language and parameter passing of main function
- JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
- adb remount of the / superblock failed: Permission denied
- [C language] summary of methods for solving the greatest common divisor
- [network] cross area network communication learning classification and calculation of IPv4 address
- Machine learning -- model evaluation, selection and verification
- Digital filter design matlab
- Implementation of strstr in C language
- Leetcode day2 连续出现的数字
猜你喜欢

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

基于 MinIO 对象存储保障 Rancher 数据

Cdga | how can the industrial Internet industry do a good job in data governance?

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

editor.md中markdown编辑器的实现
![[C language] string reverse order implementation (recursion and iteration)](/img/c3/02d0a72f6026df8a67669293e55ef2.png)
[C language] string reverse order implementation (recursion and iteration)

一文读懂如何部署具有外部数据库的高可用 K3s
随机推荐
Machine learning -- model evaluation, selection and verification
Const pointer of C language and parameter passing of main function
How many types of rain do you know?
English translation Arabic - batch English translation Arabic tools free of charge
A chip company fell in round B
Overcome the "fear of looking at teeth", and we use technology to change the industry
中国能否在元宇宙的未来发展中取得突破,占领高地?
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
个人博克系统登录点击图形验证码的集成与实现
【NPP安装插件】
[C language] summary of methods for solving the greatest common divisor
Two methods to judge the size end
Deploy LNMP automatically with saltstack
爬取IP
基于 MinIO 对象存储保障 Rancher 数据
Use of strtok and strError
Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
通信网络基础知识01
[网络]跨区域网络的通信学习IPv4地址的分类和计算