網站首頁 編程語言 正文
前言
這篇博客就帶大家深度理解以下什么是取整。我提到的"深度"就可以看出來這篇文章對取整講解的還是很詳細的并且是肯定有些知識點內容在里面的,但是不要覺得深度就是設計到很多很困難的知識點。實際上都是一些相對且容易的一些知識點,那么廢話不多說進入正題
取整
關于"取整"這個詞似乎我們既熟悉又會感到陌生,熟悉是因為我們在編程的時候經常會用到取整。陌生是因為又沒有好好深度理解過這取整,那么接下來就圍繞取整作為一個探討。
取整?取整字面意思非常好理解無非就是對整數取整像2.5是一個浮點數我們對其進行取整的話就是2的整數。?代碼示例如下↓
#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;
}
運行結果?
a =? 2
b = -2
?說明?在上述代碼當中我們可以知道并不是按照我們數學意義上的四舍五入而是不管你的小數位多大都的取整,這也是為什么我變量小數點取怎么大的原因也就是想讓大家能非常直觀的理解這個概念。一句話:直接把小數點后面的數字給省略了。
?總結?「C語言」取整默認采用的是"0向取整"。
⒈trunc - 0向取整
trunc?的頭文件是 ? #include<math.h>
?拓展知識點?對于「C語言」來說它實際上是有一個取整函數的相信很多小伙伴們是不知道「C語言」有這個trunc取整函數的。
trunc?參數如下↓
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;
}
運行結果?
?2
-2
?2
-2
?注?這里我們需要注意函數的返回值都是浮點類型,如果你是用整形打印的話你需要把類型進行強轉成(int)
?說明?在C語言當中默認采用的是0向取整的方式來進行的。
⒉floor?-地板取整
floor?的頭文件是 ? #include<math.h>
floor?參數如下↓
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;
}
運行結果?
?2
-3
?2
-3
?注?地板取整我們需要記住它是往-∞當中靠近的,從上述代碼當中的運行結果相信你也可以看的出來都是往-∞當中靠近的。
⒊ceil-無窮大取整
ceil 的頭文件是 ? #include<math.h>
ceil?參數如下↓
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;
}
運行結果?
?3
-2
?3
-2
?注?我們需要記住它是往+∞當中靠近的,從上述代碼當中的運行結果相信你也可以看的出來都是往+∞當中靠近的。
⒋round-四舍五入
round 的頭文件是 ? #include<math.h>
round?參數如下↓
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;
}
運行結果?
?3
-3
?2
-3
?注?這就是在我們數學當中的四舍五入的方式。
?
總結
最終采用哪種取決方式是根據你的應用場景來進行使用的~
原文鏈接:https://blog.csdn.net/weixin_52632755/article/details/125660435
相關推薦
- 2022-06-21 分享python?寫?csv?文件的兩種方法_python
- 2022-08-11 C++通過boost.date_time進行時間運算_C 語言
- 2022-08-04 python連接FTP服務器的實現方法_python
- 2023-04-08 Swift中的HTTP模擬測試示例詳解_Swift
- 2022-10-24 C語言進度條的實現原理詳解_C 語言
- 2022-06-08 如何在springboot中使用Thymeleaf
- 2022-09-20 Springboot整合Redis與數據持久化_Redis
- 2022-02-19 數據結構C語言鏈表的實現介紹_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支