当前位置:网站首页>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 ).
边栏推荐
- How to write the SQL statement of time to date?
- 软考高级考试中有五大证书,哪个更值得考?
- Deploy ZABBIX automatically with saltstack
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- 数字滤波器设计——Matlab
- Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
- Cloud computing notes part.2 - Application Management
- leetcode day5 删除重复的电子邮箱
- 克服“看牙恐惧”,我们用技术改变行业
猜你喜欢

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

English translation Arabic - batch English translation Arabic tools free of charge

KPMG China: insights into information technology audit projects of securities fund management institutions

Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
![[C language] initial C language reflection and summary](/img/21/826d144867f7a73ec2cd8896a5250a.png)
[C language] initial C language reflection and summary

Tencent cloud deployment lamp_ Experience of building a station

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

一文读懂如何部署具有外部数据库的高可用 K3s

String中常用的API

Function fitting based on MATLAB
随机推荐
MySQL command statement (personal summary)
Basic knowledge of C language
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
Rand function generates pseudo-random numbers
Function fitting based on MATLAB
Implementation of strstr in C language
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Getting started with enterprise distributed crawler framework
In the second half of 2022, the system integration project management engineer certification starts on August 20
Cell review: single cell methods in human microbiome research
MySQL8 Encrypting InnoDB Tablespaces
通信网络基础知识01
基于C语言的信息管理系统和小游戏
Redis笔记
中国能否在元宇宙的未来发展中取得突破,占领高地?
Codeignier framework implements restful API interface programming
NetCoreAPI操作Excel表格
[C language] Fibonacci sequence [recursion and iteration]
English Translation Spanish - batch English Translation Spanish tools free of charge