当前位置:网站首页>[C language] Floating point number rounding
[C language] Floating point number rounding
2022-08-10 10:04:00 【Ann passed _】
目录
一、Floating point numbers implement rounding
二、补充:ceil,floor,强制类型转换,round,Comparison of rounding methods and their application scenarios
一、Floating point numbers implement rounding
以保留3位数为例
1.直接实现【Do not use library functions】
int (x*1000+0.5)/1000.0;【特别注意,Here must be divided by yes1000.0】
2.间接实现【利用math函数库中的round】
二、补充:ceil,floor,强制类型转换,round,Comparison of rounding methods and their application scenarios
#include <stdio.h>
#include <math.h>
int main()
{
double x=3.863479, ret;
//①ceil
ret = ceil(x);
printf("%lf\n", ret);
//②floor
ret = floor(x);
printf("%lf\n", ret);
//③强制类型转化——Implicit conversions are involved here
ret = (int)x;
printf("%.0lf\n", ret);
//④round
ret = round(x);
printf("%lf\n", ret);
//⑤四舍五入,比如说保留2位小数——两种实现方法
//(1)round函数
ret = round(x * 100) / 100.0;
printf("%.2lf\n", ret);
//(2)加减+强制类型转换
ret =(int)(x*100+0.5) / 100.0;
printf("%.2lf\n", ret);
return 0;
}
Let's take a look at the running results~
很显然,The following conclusions can be drawn
重要结论1
操作 | 效果 |
ceil函数 | Find the largest integer greater than or equal to the current value(浮点表示形式) |
floor函数 | Finds the largest integer that does not exceed the current value(浮点表示形式) |
round函数 | Find the integer closest to the current value |
强制类型转换 | drop precision【Actually function with floor相似,但有区别】 |
四舍五入 | 浮点数四舍五入【The same idea as in mathematics】 |
另外:这是cpluscplus对于ceil函数的解释,很容易看出,The return value and parameters are both floats,But its function is the largest integer greater than or equal to the current value
同理,floor和round也是类似的
重要结论2
floorThe difference between functions and casts——The result obtained is an integer,The other is a floating point type
再叨叨几句:
1.ceilFind the largest integer that does not exceed the current value,返回值为浮点数
2.floorand mandatory typesintThe conversion evaluates to the largest integer not greater than the current value,The return value is a float and an integer
3.roundFind the closest integer value to the current value
4.There are casts and rounding of floating point numbersroundThere are two solutions to the function
边栏推荐
- 哈希表,哈希桶的实现
- 【Software Exam System Architect】System Reliability Analysis and Design ① System Reliability Analysis
- keepalived:双主配置
- 大连理工&鹏城&UAE提出用于伪装目标检测的混合尺度三重网络ZoomNet,性能SOTA!
- ESP8266-Arduino编程实例-MQ-7一氧化碳传感器驱动
- 第三章 搜索与图论(三)
- 亚信AntDB数据库有啥业务应用场景和应用案例?
- 序列化技术ProtoBuf
- 多元线性回归分析(Stata)
- CSDN 21 Days Learning Challenge - Polymorphism (05)
猜你喜欢
支付 x 聚合 x 分账 - 回流平台“二清”风险规避之路
Payment x Aggregation x Ledger Separation - The Way to Avoid Risk of "Erqing" on the Return Platform
裸辞→自我放松→闭关→复习→斩获Offer
「应用架构」TOGAF建模:应用程序迁移图
Swin Transformer作者曹越加入智源,开展视觉基础模型研究
The Generation of Matlab Symbolic Functions and the Calculation of Its Function Values
CentOS和Ubantu的Mysql主从配置
LCD DRM驱动框架分析二
【数据架构】概念数据模型和逻辑数据模型有什么区别
态势丨黑客侵扰加剧,靶场为网络安全架设“防御盾”
随机推荐
04 【计算属性 侦听器】
效率开发目录
Development environment variable record under win
数据库的约束
Lasso regression (Stata)
90.(cesium之家)cesium高度监听事件
「技术选型」工作流引擎哪家强?首席架构帮你挑
Oracle rac所在的网络要割接,停掉其中一个rac节点,这种方案可行吗?
LeetCode Algorithm 914. 卡牌分组
jq封装树形下拉选择框组件
LCD DRM驱动框架分析一
对话陈赐靓:哪吒要让高端产品大众化
亚信AntDB数据库有啥业务应用场景和应用案例?
Relearn bubble sort
08 【Props 组件事件】
Thrift -- 跨语言RPC 框架
VBA: Inputbox Function and Inputbox Method
C语言题解:倒置字符串
跨公网环境,路由策略,进行设备的访问
PTA 7-2 方阵对角线元素求和及计数 题解