網(wǎng)站首頁 編程語言 正文
前言
這篇博客就帶大家深度理解以下什么是取整。我提到的"深度"就可以看出來這篇文章對(duì)取整講解的還是很詳細(xì)的并且是肯定有些知識(shí)點(diǎn)內(nèi)容在里面的,但是不要覺得深度就是設(shè)計(jì)到很多很困難的知識(shí)點(diǎn)。實(shí)際上都是一些相對(duì)且容易的一些知識(shí)點(diǎn),那么廢話不多說進(jìn)入正題
取整
關(guān)于"取整"這個(gè)詞似乎我們既熟悉又會(huì)感到陌生,熟悉是因?yàn)槲覀冊(cè)诰幊痰臅r(shí)候經(jīng)常會(huì)用到取整。陌生是因?yàn)橛譀]有好好深度理解過這取整,那么接下來就圍繞取整作為一個(gè)探討。
取整?取整字面意思非常好理解無非就是對(duì)整數(shù)取整像2.5是一個(gè)浮點(diǎn)數(shù)我們對(duì)其進(jìn)行取整的話就是2的整數(shù)。??代碼示例如下↓
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
int a = 2.99;
int b = -2.99;
printf("a=%2d\n", a);
printf("b=%2d\n", b);
return 0;
}
運(yùn)行結(jié)果??
a =? 2
b = -2
??說明?在上述代碼當(dāng)中我們可以知道并不是按照我們數(shù)學(xué)意義上的四舍五入而是不管你的小數(shù)位多大都的取整,這也是為什么我變量小數(shù)點(diǎn)取怎么大的原因也就是想讓大家能非常直觀的理解這個(gè)概念。一句話:直接把小數(shù)點(diǎn)后面的數(shù)字給省略了。
??總結(jié)?「C語言」取整默認(rèn)采用的是"0向取整"。
⒈trunc - 0向取整
trunc?的頭文件是 ? #include<math.h>
??拓展知識(shí)點(diǎn)?對(duì)于「C語言」來說它實(shí)際上是有一個(gè)取整函數(shù)的相信很多小伙伴們是不知道「C語言」有這個(gè)trunc取整函數(shù)的。
trunc?參數(shù)如下↓
double trunc ( double x);
float truncf ( float x);
long double truncl (long double x);
??代碼示例如下↓
#include<stdio.h>
#include<math.h>
int main(void)
{
printf("%2f\n", trunc(2.99));
printf("%2f\n", trunc(-2.99));
printf("%2d\n", (int)trunc(2.99));
printf("%2d\n", (int)trunc(-2.99));
return 0;
}
運(yùn)行結(jié)果??
?2
-2
?2
-2
??注?這里我們需要注意函數(shù)的返回值都是浮點(diǎn)類型,如果你是用整形打印的話你需要把類型進(jìn)行強(qiáng)轉(zhuǎn)成(int)
??說明?在C語言當(dāng)中默認(rèn)采用的是0向取整的方式來進(jìn)行的。
⒉floor?-地板取整
floor?的頭文件是 ? #include<math.h>
floor?參數(shù)如下↓
double floor (double x);
??代碼示例如下↓
#include<stdio.h>
#include<math.h>
int main(void)
{
printf("%2d\n", (int)floor(2.99));
printf("%2d\n", (int)floor(-2.99));
printf("%2d\n", (int)floor(2.99));
printf("%2d\n", (int)floor(-2.99));
return 0;
}
運(yùn)行結(jié)果??
?2
-3
?2
-3
??注?地板取整我們需要記住它是往-∞當(dāng)中靠近的,從上述代碼當(dāng)中的運(yùn)行結(jié)果相信你也可以看的出來都是往-∞當(dāng)中靠近的。
⒊ceil-無窮大取整
ceil 的頭文件是 ? #include<math.h>
ceil?參數(shù)如下↓
double ceil (double x);
??代碼示例如下↓
#include<stdio.h>
#include<math.h>
int main(void)
{
printf("%2d\n", (int)ceil(2.99));
printf("%2d\n", (int)ceil(-2.99));
printf("%2d\n", (int)ceil(2.99));
printf("%2d\n", (int)ceil(-2.99));
return 0;
}
運(yùn)行結(jié)果??
?3
-2
?3
-2
??注?我們需要記住它是往+∞當(dāng)中靠近的,從上述代碼當(dāng)中的運(yùn)行結(jié)果相信你也可以看的出來都是往+∞當(dāng)中靠近的。
⒋round-四舍五入
round 的頭文件是 ? #include<math.h>
round?參數(shù)如下↓
double round (double x);
float roundf (float x);
long double roundl (long double x);
??代碼示例如下↓
#include<stdio.h>
#include<math.h>
int main(void)
{
printf("%2d\n", (int)round(2.99));
printf("%2d\n", (int)round(-2.01));
printf("%2d\n", (int)round(2.01));
printf("%2d\n", (int)round(-2.99));
return 0;
}
運(yùn)行結(jié)果??
?3
-3
?2
-3
??注?這就是在我們數(shù)學(xué)當(dāng)中的四舍五入的方式。
??
總結(jié)
最終采用哪種取決方式是根據(jù)你的應(yīng)用場(chǎng)景來進(jìn)行使用的~
原文鏈接:https://blog.csdn.net/weixin_52632755/article/details/125660435
相關(guān)推薦
- 2023-08-01 ref 和 reactive 函數(shù)標(biāo)注類型解析
- 2022-04-12 Git:解決Git向碼云中push文件報(bào)錯(cuò):! [rejected] master -
- 2024-04-08 SpringBoot使用Mybatis-Plus中分頁出現(xiàn)total=0的情況解決
- 2022-04-03 python中如何利用matplotlib畫多個(gè)并列的柱狀圖_python
- 2022-09-27 Kotlin示例講解標(biāo)準(zhǔn)函數(shù)with與run和apply的使用_Android
- 2022-06-12 C#自定義特性(Attribute)詳解_C#教程
- 2022-04-24 C語言時(shí)間函數(shù)之strftime()詳解_C 語言
- 2022-07-18 Kotlin 正確退出 foreach、foreachIndexed 循環(huán)函數(shù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支