当前位置:网站首页>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 ).
边栏推荐
- Idea properties file display \u solution of not displaying Chinese
- [NPP installation plug-in]
- Concurrent programming, do you really understand?
- Digital filter design matlab
- Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
- C language implementation of strncpy
- Saltstack advanced
- 11. Learn MySQL union operator
- Advanced notes (Part 2)
- Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
猜你喜欢

There is a 'single quotation mark' problem in the string when Oracle inserts data

editor.md中markdown编辑器的实现

Information management system and games based on C language

String中常用的API

Article translation software - batch free translation software supports major translation interfaces

Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022

Edge detection and connection of image segmentation realized by MATLAB

A chip company fell in round B

Deploy LNMP automatically with saltstack

Tencent cloud deployment lamp_ Experience of building a station
随机推荐
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Implementation of markdown editor in editor.md
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Return and job management of saltstack
Cloud computing notes part.1 - system management
There are five certificates in the soft examination advanced examination, which is more worth taking?
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Leetcode day3 find duplicate email addresses
[wechat applet development] page navigation and parameter transmission
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
C language pointer and two-dimensional array
Leetcode Day1 score ranking
Can China make a breakthrough in the future development of the meta universe and occupy the highland?
Integration and implementation of login click graphic verification code in personal blog system
基于C语言的信息管理系统和小游戏
How navicate modifies the database name
Implementation of strcat in C language
爬取IP
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
中国能否在元宇宙的未来发展中取得突破,占领高地?