[問題]有關 round() 的問題

看板C_and_CPP (C/C++)作者 (watson)時間12年前 (2013/10/03 17:45), 編輯推噓0(0012)
留言12則, 2人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 6.0 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 宣告 #include "main.h" #include <time.h> //time_t time() clock_t clock() #include <Mmsystem.h> //timeGetTime() #pragma comment(lib, "Winmm.lib") //timeGetTime() #include <math.h> #define Round(x) ((x)-(int)(x))>=0.5?ceil((x)):floor((x)) 以下進行例行性的程序 SetPulse=round((111.11111*iParam7)); printf("SetPulse=%d\n",SetPulse); for(i=0 ;i<iParam3 ;i++) { Sleep(100); for( j = 1 ; j<=10 ; j++) { HW_SetPositionPulse(2,SetPulse); HW_SetPositionPulse(3,SetPulse); } } 我的compile有過 但是再build的時候出現了 Linking... Main.obj : error LNK2001: unresolved external symbol _round Debug_GMC700K/GMC700K.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. Creating browse info file... GMC700K.exe - 2 error(s), 0 warning(s) 我找不太到原因出在哪 四捨五入還有其他更簡單的寫法嗎!? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.124.249.111 ※ 編輯: watson1988 來自: 140.124.249.111 (10/03 17:51)

10/03 17:51, , 1F
你在搞笑嗎...
10/03 17:51, 1F

10/03 17:53, , 2F
我比較喜歡floor(x+0.5)
10/03 17:53, 2F

10/03 17:55, , 3F
我這就去重寫~~謝謝囉^^
10/03 17:55, 3F

10/03 17:56, , 4F
你太累了, 眼花的話讓眼睛休息一下... 大小寫錯了而已...
10/03 17:56, 4F

10/03 17:59, , 5F
謝謝s大的提醒!! 都快哭了~哈哈XD
10/03 17:59, 5F

10/03 18:19, , 6F
剛剛試了一下,你的寫法在 x 為負數時結果不正確...
10/03 18:19, 6F

10/03 18:23, , 7F
今天才知道強制轉型int的效果是trunc()不是floor()... XD
10/03 18:23, 7F

10/03 18:33, , 8F
如果負值~我這要怎麼改!?
10/03 18:33, 8F
換成下面這一行嗎?! #define ROUND(x) (int)((x) + 0.5 - (double)((x) < 0)) ※ 編輯: watson1988 來自: 140.124.249.111 (10/03 18:38)

10/03 18:38, , 9F
(x)-floor(x)>0.5?
10/03 18:38, 9F

10/03 18:41, , 10F
不然就用2F的方法 #define ROUND(x) floor((x)+0.5)
10/03 18:41, 10F

10/03 18:43, , 11F
了解~這就去改 (((筆記
10/03 18:43, 11F

10/03 18:44, , 12F
謝謝s大 ^^
10/03 18:44, 12F
文章代碼(AID): #1IJJo-Gf (C_and_CPP)
文章代碼(AID): #1IJJo-Gf (C_and_CPP)