[問題] math.h vs cmath

看板C_and_CPP (C/C++)作者 (偷偷)時間13年前 (2012/12/10 17:37), 編輯推噓0(0014)
留言14則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) <math.h>,<cmath> 問題(Question): 我以前對cmath的認知是它是包含math.h且更powerful的header 但像pow(int a,int b)若是include<math.h> compiled會過 反而include<cmath>就會有error 後來修正為pow(double a,int b) or pow(int a,double b)則compiled OK 雖然能用但是不太懂為什麼的感覺不是很好 有人能稍微講解一下這兩種使用上的比較和觀念嗎? 謝謝!! --

12/10 18:14, , 1F
因為pow(int, int)會不知道要轉成pow(double, double)
12/10 18:14, 1F

12/10 18:15, , 2F
還是pow(float, float)吧 因為C++的pow有function over
12/10 18:15, 2F

12/10 18:15, , 3F
loading, 在C的時候用float的pow要call powf
12/10 18:15, 3F

12/10 18:17, , 4F
有這種事嗎?你是不是跟modf()搞混了?
12/10 18:17, 4F

12/10 18:18, , 5F
另外應該也還有long double的powl~~
12/10 18:18, 5F

12/10 18:19, , 6F
1F 說反了,math.h 裡傳入 pow(int,int),會將兩個引數轉成
12/10 18:19, 6F

12/10 18:20, , 7F
double,即使 pow(int,double)/pow(double,int)在c也行.
12/10 18:20, 7F

12/10 18:20, , 8F
唯c++,cmath多載了三份,所以 pow(int,double)會不知用哪份
12/10 18:20, 8F

12/10 18:22, , 9F
更正最後一句,pow(int,double) 會呼叫 pow(double,double)
12/10 18:22, 9F

12/10 18:48, , 10F
http://goo.gl/WvgZy pow(double,int) 有 direct match,
12/10 18:48, 10F

12/10 18:49, , 11F
pow(int,double) 會用 pow(double,double)(唯一符合)
12/10 18:49, 11F

12/10 18:49, , 12F
pow(int,int) 會卡在第一個要 double 還是 long double
12/10 18:49, 12F

12/10 19:27, , 13F
我1F是說在C++裡面阿 XD
12/10 19:27, 13F

12/10 20:38, , 14F
原來如此,是我意會錯了.
12/10 20:38, 14F
看了連結和說明後明瞭多了 感謝各位大大解答~~ ※ 編輯: Jeanime 來自: 140.112.41.30 (12/10 21:50)
文章代碼(AID): #1GnQrghP (C_and_CPP)
文章代碼(AID): #1GnQrghP (C_and_CPP)