当前位置:网站首页>C语言中浮点数据类型(你学废了吗)
C语言中浮点数据类型(你学废了吗)
2022-07-28 13:26:00 【鱼找水需要时间】
1. 处理带小数的数值
类似于1.234567,0.00001,这类非整数的数据。int能不能装这些数据?
#include <stdio.h>
int main() {
int a = 1.234567;
int b = 0.00001;
int c = 365.12345;
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
return 0;
}
小数部分都丢失了,这时候就需要引入新的类型了——浮点类型。
2. 浮点类型:float
把代码中的整型 int ,用单精度浮点型 float 替代。之后,再将 printf("%d\n",a); 中的占位符是 %d ,所以我们用 %f 替换。( %d 占位符用于整型, %f占位符用于浮点型)
#include <stdio.h>
int main() {
float a = 1.234567;
float b = 0.00001;
float c = 365.12345;
printf("%f\n", a);
printf("%f\n", b);
printf("%f\n", c);
return 0;
}
将 int 替换成 float 之后,大部分的数据都是正确的。但是 365.12345 变成了 365.123444 ,很明显精度出现了误差。
这是因为,浮点数并不能表示无限的精确,它会存在着一定的误差。
C标准规定,float类型必须至少能表示6位有效数字,并且取值范围至少是10^-37~10+37。
所以,使用float来装365.12345时,前面六位数值是准确的,但是后面的数值略有误差。
3. 浮点类型:double
double叫做双精度浮点型,是比float精度更高的类型。把上面代码中的 float 换成 double 。
printf函数 中使用的占位符不需要修改,float和double均可以使用 %f 来作为占位符。
#include <stdio.h>
int main() {
double a = 1.234567;
double b = 0.00001;
double c = 365.12345;
printf("%f\n", a);
printf("%f\n", b);
printf("%f\n", c);
return 0;
}
这次 365.12345 也是正确的了。
注意: double 类型也是有精度范围的。如果是更高精度的数据, double 也会出现误差。
日常的应用中,不会苛求一个精度完美的数值,是会允许存在一定误差范围的。但是,如果涉及高精度领域的计算时,需要额外使用特殊的方法进行数值计算,以尽量减少误差。
4. 浮点类型所占字节大小
之前对整型的经验,越大范围的整型类型所占的空间越大。
那么对于浮点类型来说,越高精度、越大范围的浮点类型,应该也会占用越大的空间。
用sizeof来测量一下float和double分别占用多大的空间。
#include <stdio.h>
int main() {
printf("sizeof float = %d\n", sizeof(float));
printf("sizeof double = %d\n", sizeof(double));
return 0;
}
float , double 分别为4,8个字节。证明:对于越大范围,越高精度的类型所占空间越大。
边栏推荐
- Power amplifier and matching network learning
- [utils] fastdfs tool class
- Clickhouse distributed cluster construction
- 超好用的手机录屏软件推荐
- MeterSphere--开源持续测试平台
- 【Utils】CookieUtil
- On websocket
- 走进音视频的世界——FLV视频封装格式
- Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
- Four ways to create thread pools
猜你喜欢

离散对数问题(DLP) && Diffie-Hellman问题(DHP)

IP黑白名单

zabbix分布式

redis哨兵机制

阿里、京东、抖音:把云推向产业心脏

天气这么热太阳能发电不得起飞喽啊?喽啊个头……

Security assurance is based on software life cycle -psp application
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)

MVC model: calendar system

83. (cesium home) how the cesium example works
随机推荐
HCIP第十二天
Understand the principle behind the virtual list, and easily realize the virtual list
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
数据库系统概论(第5版)补充习题——第一章 绪论
JMeter installation tutorial and login add token
Alibaba, jd.com, Tiktok: push cloud to the heart of industry
Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
2022年熔化焊接与热切割考题及在线模拟考试
指针和数组(7)
jenkins
Security assurance is based on software life cycle - networkpolicy application
Intersectionobserver
[ecmascript6] iterator and generator
Detailed explanation of C language student achievement management system [easy to understand]
Several efficient APIs commonly used in inventory operation URL
【Utils】CookieUtil
多级缓存方案
[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)
Multi level cache scheme
数据库优化 理解这些就够了



