当前位置:网站首页>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个字节。证明:对于越大范围,越高精度的类型所占空间越大。
边栏推荐
- 如何有效进行回顾会议(上)?
- 草料二维码--在线二维码生成器
- LeetCode 0143. 重排链表
- Clickhouse分布式集群搭建
- webSocket聊天
- [translation] how to choose a network gateway for your private cloud
- LeetCode 1331.数组序号转换
- Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
- Minitest -- applet automation testing framework
- 数据库系统概论(第5版)补充习题——第一章 绪论
猜你喜欢

Recommended super easy-to-use mobile screen recording software

Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱

MVC model: calendar system

Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions

These three online PS tools should be tried

Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence

多级缓存方案

Leetcode 0142. circular linked list II

手机滚动截屏软件推荐

Minitest -- applet automation testing framework
随机推荐
Security assurance is based on software life cycle -istio authentication mechanism
一文读懂如何部署具有外部数据库的高可用 K3s
MySql5.5之后的默认存储引擎为InnoDB。
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
Niuke multi school link with level editor i- (linear DP)
Duplicate data in leetcode (442) array
VOS3000如何呼入送到OKCC
解决uniapp微信小程序canvas不能引入字体的问题
Custom Configuration Sections
在centos中安装mysql5.7.36
软件测试技术之如何编写测试用例
MVC模型:日历系统
超好用的手机录屏软件推荐
AFNetworking速成教程
Clickhouse分布式集群搭建
As a programmer, how to manage time efficiently?
【Utils】CookieUtil
Master closures and consolidate basic skills
协同办公工具:在线白板初起步,在线设计已红海
[ecmascript6] proxy and reflection



