[問題] 01變數求絕對值

看板C_and_CPP (C/C++)作者 (沅沅)時間11年前 (2015/02/02 12:21), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
開發平台(Platform):VS (Ex: VC++, GCC, Linux, ...) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question):我試想利用VS搭配gurobi進行最佳化求解,但在compile的時 後發現求取絕對值的部份一直出錯,後來查了一下,好像abs可使用的型態只有int和 long,這樣還有什麼解決辦法嗎?若使用轉型態會不會佔用太多記憶體? 餵入的資料(Input): void addX(GRBModel*& model){ for(int i = 1; i <= num_of_jobs; i++){ for(int j = 1; j < num_of_machines; j++){ for(int t = 1; t <= Ut; t++){ stringstream ss; ss << "x" << i << j << t; var[ss.str()] = model->addVar(0, 1, 0, GRB_BINARY, ss.str()); } } } } //部分省略 ss << "x" << i << j << t; ss2<< "x" << i << j << t+1; sum = sum + abs(var[ss2.str()] - var[ss.str()]); 預期的正確結果(Expected Output):可以取[xij(t+1)]-[xijt]的絕對值 錯誤結果(Wrong Output): error C2665: 'abs' : 沒有任何 6 多載可以轉換所有引數型別 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\math.h(101): 可能是 'int abs(int)' [使用與引數相關的查閱找到] 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\math.h(494): 或 'double abs(double)' 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\math.h(498): 或 'float abs(float)' 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\math.h(546): 或 'long double abs(long double)' 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\stdlib.h(484): 或 'long abs(long)' 1> C:\Program Files\Microsoft Visual Studio 11.0\VC\include\stdlib.h(488): 或 '__int64 abs(__int64)' 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): 麻煩大家了~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.96.221 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1422850865.A.31C.html

02/02 15:48, , 1F
你的var是什麼啊?
02/02 15:48, 1F

02/02 18:58, , 2F
我覺得應該是 compile error
02/02 18:58, 2F

02/02 21:47, , 3F
你什麼都貼了 最重要的var是啥...只看得出應該是個map
02/02 21:47, 3F

02/02 21:48, , 4F
頂多只能猜出是std::map<std::string, ??????>
02/02 21:48, 4F

02/02 21:49, , 5F
還有錯誤訊息都說了他還能吃double跟float 你那個var
02/02 21:49, 5F

02/02 21:49, , 6F
map出來的東西我猜根本是string吧 你要lexical_cast
02/02 21:49, 6F

02/02 21:50, , 7F
根本就不是數字我猜
02/02 21:50, 7F
文章代碼(AID): #1KplinCS (C_and_CPP)
文章代碼(AID): #1KplinCS (C_and_CPP)