C語言取模取整的深入理解
一:四大取整
1.1、 0向取整
看代碼:
#include <stdio.h> int main() { //本質是向0取整 int i = -2.9; int j = 2.9; printf("%d\n", i); //結果是:-2 printf("%d\n", j); //結果是:2 printf("%d %d\n", 5 / 2, -5 / 2); // 2 -2 return 0; }
C中默認0向取整,畫圖解釋0向取整:
C語言當中有一個trunc函數(shù),運用的也是0向取整。注意引用頭文件#include<math.h>.示例如下:
#include <stdio.h> #include<math.h> int main() { int i = -2.9; int j = 2.9; printf("%d\n", i); //結果是:-2 printf("%d\n", j); //結果是:2 printf("%d\n", (int)trunc(2.9)); //結果是:2 //結果是:-2 printf("%d\n", (int)trunc(-2.9));//結果是:-2 return 0; }
1.2、 -∞取整( 地板取整)
其實運用的是floor函數(shù),跟上述trunc一樣引用#include<math.h>
看代碼:
#include<stdio.h> #include<math.h> int main() { printf("%.1f\n", floor(-2.9)); // -3.0 printf("%.1f\n", floor(-2.1)); // -3.0 printf("%.1f\n", floor(2.9)); // 2.0 printf("%.1f\n", floor(2.1)); // 2.0 return 0; }
畫圖解釋-∞取整
1.3、 +∞取整
運用的是ceil函數(shù),同樣需要#include<math.h>
看代碼:
#include<stdio.h> #include<math.h> int main() { printf("%.1f\n", ceil(-2.9)); // -2.0 printf("%.1f\n", ceil(-2.1)); // -2.0 printf("%.1f\n", ceil(2.9)); // 3.0 printf("%.1f\n", ceil(2.1)); // 3.0 return 0; }
畫圖解釋+∞取整:
1.4、 四舍五入取整
運用的是round函數(shù),同樣需要#include<math.h>
看代碼:
#include<stdio.h> #include<math.h> int main() { printf("%.1f\n", round(-2.9)); // -3.0 printf("%.1f\n", round(-2.1)); // -2.0 printf("%.1f\n", round(2.9)); // 3.0 printf("%.1f\n", round(2.1)); // 2.0 }
1.5、 例子匯總
#include <stdio.h> #include <math.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)); return 0; }
結論:
浮點數(shù)(整數(shù)/整數(shù)),是有很多的取整方式的。
二:取模 / 取余
2.1、 概念
如果a和d是兩個自然數(shù),d非零,可以證明存在兩個唯一的整數(shù) q 和 r,滿足 a = q*d + r 且0 ≤ r < d。其中,q 被稱為商,r 被稱為余數(shù)。
2.2、 示例(C和Python)
#include <stdio.h> int main() { int a = 10; int d = 3; printf("%d\n", a % d); //結果是1 //因為:a=10,d=3,q=3,r=1 0<=r<d(3) //所以:a = q*d+r -> 10=3*3+1 int m = -10; int n = 3; printf("%d\n", m / n); //C語言中是-3,很好理解 printf("%d\n", m % n); //C語言是-1 return 0; }
在C語言中,我們對-10/3=-3以及-10%3=-1很好理解,沒有爭議
而在Python中,-10/3竟然=-4,而-10%3竟然=2
為什么呢?
結論:很顯然,上面關于取模的定義,并不能滿足語言上的取模運算
解析:
因為在C中,現(xiàn)在-10%3出現(xiàn)了負數(shù),根據(jù)定義:滿足 a = q*d + r 且0 ≤ r < d,C語言中的余數(shù),是不滿足定義的, 因為,r<0了。
故,大家對取模有了一個修訂版的定義: 如果a和d是兩個自然數(shù),d非零,可以證明存在兩個唯一的整數(shù) q 和 r,滿足 a = q*d + r , q 為整數(shù),且0 ≤ |r| < |d|。其中,q 被稱為商,r 被稱為余數(shù)。
有了這個新的定義,那么C中或者Python中的“取模”,就都能解釋了。
解釋C: -10 = (-3) * 3 + (-1)
解釋Python:-10 = (?)* 3 + 2,其中,可以推到出來,'?'必須是-4(后面驗證).即-10 = (-4)* 3 + 2,才能 滿足定義。
所以,在不同語言,同一個計算表達式,負數(shù)“取模”結果是不同的。我們可以稱之為分別叫做正余數(shù) 和 負余數(shù)
是什么決定了這種現(xiàn)象?
由上面的例子可以看出,具體余數(shù)r的大小,本質是取決于商q的。
而商,又取決誰呢?取決于除法計算的時候,取整規(guī)則。
2.3、 取余和取模一樣嗎?
細心的同學,應該看到了,我上面的取模都是帶著""的。說明這兩個并不能嚴格等價(雖然大部分情況差不多) 取余或者取模,都應該要算出商,然后才能得出余數(shù)。
本質 1 取整:
取余:盡可能讓商,進行向0取整。
取模:盡可能讓商,向-∞方向取整。
故:
C中%,本質其實是取余。
Python中%,本質其實是取模。
理解鏈:
對任何一個大于0的數(shù),對其進行0向取整和-∞取整,取整方向是一致的。故取模等價于取余 對任何一個小于0的數(shù),對其進行0向取整和-∞取整,取整方向是相反的。故取模不等價于取余
同符號數(shù)據(jù)相除,得到的商,一定是正數(shù)(正數(shù)vs正整數(shù)),即大于0! 故,在對其商進行取整的時候,取模等價于取余。
本質 2 符號:
參與取余的兩個數(shù)據(jù),如果同符號,取模等價于取余
2.4、 計算數(shù)據(jù)同符號
看代碼:
#include <stdio.h> int main() { printf("%d\n", 10 / 3); // 3 printf("%d\n\n", 10 % 3);// 1 printf("%d\n", -10 / -3); // 3 printf("%d\n\n", -10 % -3); // -1 return 0; }
在Python中,我們發(fā)現(xiàn)運行結果和上述一樣。
結論:通過對比試驗,更加驗證了,參與取余的兩個數(shù)據(jù),如果同符號,取模等價于取余
2.5、 計算數(shù)據(jù)不同符號
看代碼:
#include <stdio.h> int main() { printf("%d\n", -10 / 3); //結果:-3 printf("%d\n\n", -10 % 3); //結果:-1 為什么? -10=(-3)*3+(-1) printf("%d\n", 10 / -3); //結果:-3 printf("%d\n\n", 10 % -3); //結果:1 為什么?10=(-3)*(-3)+1 return 0; }
而在Python中,結果卻不是這樣的。
明顯結論:如果不同符號,余數(shù)的求法,參考之前定義。而余數(shù)符號,與被除數(shù)相同。
為什么呢?
重新看看定義:如果a和d是兩個自然數(shù),d非零,可以證明存在兩個唯一的整數(shù) q 和 r,滿足 a = q*d + r , q 為整數(shù),且0 ≤ |r| < |d|。其中,q 被稱為商,r 被稱為余數(shù)
a = q*d + r 變換成 r = a - q*d 變換成 r = a + (-q*d)
對于:x = y + z,這樣的表達式,x的符號 與 |y|、|z|中大的數(shù)據(jù)一致
而r = a + (-q*d)中,|a| 和 |-q*d|的絕對值誰大,取決于商q的取整方式。
c是向0取整的,也就是q本身的絕對值是減小的。如:
-10/3=-3.333.33 向0取整 -3. a=-10 |10|, -q*d=-(-3)*3=9 |9|
10/-3=-3.333.33 向0取整 -3. a=10 |10|, -q*d=-(-3)*(-3)=-9 |9|
絕對值都變小了
python是向-∞取整的,也就是q本身的絕對值是增大的。
-10/3=-3.333.33 '//'向-∞取整 -4. a=-10 |10|, -q*d=-(-4)*3=12 |12|
10/-3=--3.333.33 '//'向-∞取整 -4. a=10 |10|, -q*d=-(-4)*(-3)=-12 |12|
絕對值都變大了
結論:如果參與取余的兩個數(shù)據(jù)符號不同,在C語言中(或者其他采用向0取整的語言如:C++,Java),余數(shù)符號,與被除數(shù) 相同。
2.6、 總結
(1)浮點數(shù)(或者整數(shù)相除),是有很多的取整方式的。
(2)如果a和d是兩個自然數(shù),d非零,可以證明存在兩個唯一的整數(shù) q 和 r,滿足 a = q*d + r , q 為整數(shù),且0 ≤ |r| < |d|。其中,q 被稱為商,r 被稱為余數(shù)。
(3)在不同語言,同一個計算表達式,“取模”結果是不同的。我們可以稱之為分別叫做正余數(shù) 和 負余數(shù)
(4)具體余數(shù)r的大小,本質是取決于商q的。而商,又取決于除法計算的時候,取整規(guī)則。
(5)取余vs取模: 取余盡可能讓商,進行向0取整。取模盡可能讓商,向-∞方向取整。
(6)參與取余的兩個數(shù)據(jù),如果同符號,取模等價于取余
(7)如果參與取余的兩個數(shù)據(jù)符號不同,在C語言中(或者其他采用向0取整的語言如: C++,Java),余數(shù)符號,與被 除數(shù)相同。(因為采用的向0取整)
到此這篇關于C語言取模取整的深入理解的文章就介紹到這了,更多相關C語言取模取整 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++調用libcurl開源庫實現(xiàn)郵件的發(fā)送功能流程詳解
libcurl是一個免費開源的網(wǎng)絡傳輸庫,支持ftp、ftps、tftp,http、https、telnet、ldap、pop3、smtp等多種協(xié)議,接下來讓我們一起來了解吧2021-11-11