C語言編程深入理解取整取余取模問題示例分析
1. 取整問題
1.0向取整(C語言默認的取整方案)
#include<stdio.h> #include<windows.h> int main() { //本質(zhì)是向0取整 //trunc()函數(shù)也有這種作用,不過返回值是浮點數(shù),而且必須引用math.h頭文件 int i = -2.9; int j = 2.9; printf("%d\n", i); //結(jié)果是:-2 printf("%d\n", j); //結(jié)果是:2 system("pause"); return 0; }
2.地板取整(向負無窮的方向取整)
#include <stdio.h> #include <math.h> //因為使用了floor函數(shù),需要添加該頭文件 #include <windows.h> int main() { //本質(zhì)是向-∞取整,注意輸出格式要不然看不到結(jié)果 printf("%.1f\n", floor(-2.9)); //-3 printf("%.1f\n", floor(-2.1)); //-3 printf("%.1f\n", floor(2.9)); //2 printf("%.1f\n", floor(2.1)); //2 system("pause"); return 0; }
?注意:
使用floor()函數(shù)需要引頭文件,參數(shù)為double 類型。返回值也同樣為double類型。
同時不要忘了引math.h頭文件。
?注意:python默認的取整方案就是地板取整,后面也正是因為這個原因,才出現(xiàn)了取模的不同!
3.天花板取整(向+無窮的方向取整)
#include <stdio.h> #include <math.h> #include <windows.h> int main() { //本質(zhì)是向+∞取整,注意輸出格式要不然看不到結(jié)果 printf("%.1f\n", ceil(-2.9)); //-2 printf("%.1f\n", ceil(-2.1)); //-2 printf("%.1f\n", ceil(2.9)); //3 printf("%.1f\n", ceil(2.1)); //3 system("pause"); return 0; }
注意:
使用ceil()函數(shù)需要引頭文件,參數(shù)為double 類型。
返回值也同樣為double類型。同時不要忘了引math.h頭文件。
4.四舍五入取整
#include <stdio.h> #include <math.h> #include <windows.h> int main() { //本質(zhì)是四舍五入 printf("%.1f\n", round(2.1)); printf("%.1f\n", round(2.9)); printf("%.1f\n", round(-2.1)); printf("%.1f\n", round(-2.9)); system("pause"); return 0; }
注意:使用round()函數(shù)需要引頭文件,參數(shù)為double 類型。返回值也同樣為double類型。同時不要忘了引math.h頭文件。
匯總例子
#include <stdio.h> #include <math.h> #include <windows.h> int main() { const char* format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n"; printf("value\tround\tfloor\tceil\ttrunc\n"); printf("-----\t-----\t-----\t----\t-----\n"); printf(format, 2.3, round(2.3), floor(2.3), ceil(2.3), trunc(2.3)); printf(format, 3.8, round(3.8), floor(3.8), ceil(3.8), trunc(3.8)); printf(format, 5.5, round(5.5), floor(5.5), ceil(5.5), trunc(5.5)); printf(format, -2.3, round(-2.3), floor(-2.3), ceil(-2.3), trunc(-2.3)); printf(format, -3.8, round(-3.8), floor(-3.8), ceil(-3.8), trunc(-3.8)); printf(format, -5.5, round(-5.5), floor(-5.5), ceil(-5.5), trunc(-5.5)); system("pause"); return 0; }
2.取模問題?
1.余數(shù)的定義
余數(shù)的定義:如果a和d是兩個自然數(shù),d非零,可以證明存在兩個唯一的整數(shù) q 和 r,滿足 a = q*d + r , q 為整數(shù),且0 ≤ |r|< |d|。其中,q 被稱為商,r 被稱為余數(shù)。
注意:余數(shù)并不一定都是正數(shù),大家一定要牢記這個概念!
2.兩種余數(shù)
由定義可知:
-10%3=-1------>-10/3=-3------->3*(-3)+(-1)=(-10)(C語言中是這樣的)
-10%3=2------->-10/3=-4------->4*(-3)+ 2=(-10)(python環(huán)境中是這樣的)
解釋C: -10 = (-3) * 3 + (-1)(負余數(shù))
解釋Python:-10 = (?)* 3 + 2,其中,可以推到出來,'?'必須是-4
即-10 = (-4)* 3 + 2,才能滿足定義。(正余數(shù))
所以,在不同語言,同一個計算表達式,負數(shù)“取模”結(jié)果是不同的。我們可以稱之為分別叫做正余數(shù)和負余數(shù)。
3.為什么會有這種現(xiàn)象?
由上面的例子可以看出,具體余數(shù)r的大小,本質(zhì)是取決于商q的。
而商,又取決誰呢?取決于除法計算的時候,取整規(guī)則。
C語言中默認是0向取整,python中默認是-無窮的方向取整。
3.區(qū)分取余與取模
1.取余與與取模的本質(zhì)區(qū)別
取余:盡可能讓商,進行向0取整。
取模:盡可能讓商,向負無窮方向取整。
所以:
C中%,本質(zhì)其實是取余。
Python中%,本質(zhì)其實是取模。
2.理解鏈
對任何一個大于0的數(shù),對其進行0向取整和負無窮取整,取整方向是一致的。故取模等價于取余。其實這也是為什么我們常常會認為取模以取余是一碼事的原因所在。
對任何一個小于0的數(shù),對其進行0向取整和負無窮取整,取整方向是相反的。故取模不等價于取余。
3.同符號與不同符號
1.同符號:
同符號數(shù)據(jù)相除,得到的商,一定是正數(shù),即大于0! 故,在對其商進行取整的時候,取模等價于取余。(倘若從數(shù)學上理解,就是簡單的在負數(shù)的前面加一個絕對值即可)
2.不同符號
#include<stdio.h> #include <windows.h> int main() { printf("%d\n", -10 / 3); //結(jié)果:-3 printf("%d\n\n", -10 % 3); //結(jié)果:-1 為什么? -10=(-3)*3+(-1) printf("%d\n", 10 / -3); //結(jié)果:-3 printf("%d\n\n", 10 % -3); //結(jié)果:1 為什么?10=(-3)*(-3)+1 system("pause"); return 0; }
從上面可以看出:
被除數(shù)為負數(shù)時,取余后為負號。
除數(shù)為負數(shù)時,取余后為正數(shù)。
不同符號在C語言中雖然也有一定的規(guī)律,但我并不希望大家利用這個規(guī)律,而是利用定義老老實實的計算,畢竟這這是針對C語言的結(jié)論,在python中就不適用了,因為二者的取整方式是不同的。
以上就是C語言編程深入理解取整取余取模問題示例分析的詳細內(nèi)容,更多關(guān)于C語言取整取余取模問題的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入探討POJ 2312 Battle City 優(yōu)先隊列+BFS
本篇文章是對優(yōu)先隊列+BFS進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++利用 _findfirst與_findnext查找文件的方法
這篇文章主要給大家介紹了關(guān)于C++利用 _findfirst與_findnext查找文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-06-06