{"id":1370,"date":"2016-07-30T16:49:24","date_gmt":"2016-07-30T08:49:24","guid":{"rendered":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/?p=1370"},"modified":"2019-02-12T20:11:44","modified_gmt":"2019-02-12T12:11:44","slug":"lua%e5%b0%8f%e5%9d%91%e4%b9%8b%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/2016\/07\/30\/lua%e5%b0%8f%e5%9d%91%e4%b9%8b%e6%95%b4%e6%95%b0\/","title":{"rendered":"Lua\u5c0f\u5751\u4e4b\u6574\u6570"},"content":{"rendered":"<p>\u5728\u6700\u8fd1\u7684\u4e00\u6b21\u5f00\u53d1\u4e2d\uff0c\u9700\u8981\u5c06\u4e24\u4e2a\u6574\u6570\u505a\u9664\u6cd5\uff0c\u4f8b\u5982\u4ee5\u4e0b\u7b80\u5355\u8bed\u53e5\uff1a<\/p>\n<pre>c=a\/b<\/pre>\n<p>\u7531\u4e8ea\u548cb\u90fd\u662f\u6574\u6570\uff0c\u56e0\u6b64\u6211\u53c8\u60f3\u5f53\u7136\u5730\u8ba4\u4e3ac\u4e5f\u662f\u6574\u6570\uff0c\u800c\u5b9e\u9645\u4e0aLua\u5c06c\u8f6c\u6362\u6210\u6d6e\u70b9\u6570\uff0c\u5bfc\u81f4\u540e\u7eed\u903b\u8f91\u5224\u65ad\u548c\u5904\u7406\u90fd\u51fa\u9519\u4e86\u3002\u8fd9\u53c8\u662fLua\u8ba9\u4eba\u610f\u5916\u7684\u4e00\u4e2a\u5730\u65b9\uff0c\u6211\u5f88\u56f0\u60d1\u8bbe\u8ba1\u8005\u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u8bbe\u8ba1\u3002\u5728Lua 5.3\u7684reference manual\u6587\u6863\u4e2d\uff0c\u5bf9\u6b64\u6709\u660e\u786e\u8bf4\u660e\uff1a<\/p>\n<pre>Exponentiation and float division always convert integer operands to floats.<\/pre>\n<p>\u636e\u8bf45.3\u7248\u672c\u4e4b\u524d\uff0cLua\u751a\u81f3\u90fd\u4e0d\u652f\u6301\u6574\u6570\uff0c\u53ea\u6709\u6d6e\u70b9\u6570\u3002\u800c\u6211\u4eec\u7684\u7a0b\u5e8f\u901a\u5e38\u53ea\u6709\u4e24\u79cd\u7c7b\u578b\uff1a\u6574\u6570\u548c\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u91cd\u65b0\u5c06\u8ba1\u7b97\u7ed3\u679c\u8f6c\u6362\u56de\u6574\u6570\u662f\u4e2a\u6025\u9700\u89e3\u51b3\u7684\u95ee\u9898\u3002\u5728\u7ffb\u770bmanual\u6587\u6863\u65f6\uff0c\u770b\u5230math.tointeger\u63a5\u53e3\uff0c\u8bd5\u4e86\u4e00\u4e0b\uff0c\u7ed3\u679c\u6781\u4e3a\u60b2\u50ac\uff0c\u8fd4\u56de\u4e86nil\u503c\uff01\u8fd9\u662f\u641e\u4e2a\u6bdb\u7ebf\u554a\uff1f<\/p>\n<p>\u5e78\u8fd0\u5730\u662flua\u5bf9C\u5f88\u53cb\u597d\uff0c\u56e0\u6b64\u53ea\u597d\u624b\u5de5\u6253\u9020\u4e00\u4e2a\u63a5\u53e3\u7ed9Lua\u7a0b\u5e8f\u7528\uff1a<\/p>\n<pre>static int bluaToInt(lua_State *L)\r\n{\r\n assert(NULLP!=L);\r\n int intVal = 0;\r\n int type = lua_type(L,1);\r\n switch( type )\r\n {\r\n case LUA_TNUMFLT: \r\n intVal = (int)luaL_checknumber(L,1);\r\n break;\r\n \r\n case LUA_TNUMINT:\r\n intVal=(int)luaL_checkinteger(L, 1);\r\n break;\r\n\r\n case LUA_TSTRING:\r\n intVal = BclStrToInt(luaL_checkstring(L,1));\r\n break;\r\n \r\n default:\r\n assert(0);\r\n break;\r\n }\r\n\r\n lua_pushinteger(L, intVal);\r\n return 1;\r\n}<\/pre>\n<p>\u5c06\u4e0a\u8ff0\u51fd\u6570\u6ce8\u518c\u5230Lua\u4e2d\uff0c\u5b9a\u4e49\u4e3a\u201cToInt\u201d\uff0c\u7136\u540e\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u5373\u53ef\u3002\u7531\u6b64\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u53d8\u4e3a\uff1a<\/p>\n<pre>c=ToInt(a\/b)<\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u786e\u4fddc\u4e3a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6700\u8fd1\u7684\u4e00\u6b21\u5f00\u53d1\u4e2d\uff0c\u9700\u8981\u5c06\u4e24\u4e2a\u6574\u6570\u505a\u9664\u6cd5\uff0c\u4f8b\u5982\u4ee5\u4e0b\u7b80\u5355\u8bed\u53e5\uff1a c=a\/b \u7531\u4e8ea\u548cb\u90fd\u662f\u6574\u6570\uff0c\u56e0\u6b64\u6211\u53c8\u60f3\u5f53\u7136\u5730\u8ba4\u4e3ac\u4e5f\u662f\u6574\u6570\uff0c\u800c\u5b9e\u9645\u4e0aLua\u5c06c\u8f6c\u6362\u6210\u6d6e\u70b9\u6570\uff0c\u5bfc&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.myvoipapp.com\/blogs\/yxh\/2016\/07\/30\/lua%e5%b0%8f%e5%9d%91%e4%b9%8b%e6%95%b4%e6%95%b0\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[565,564,562,563],"class_list":["post-1370","post","type-post","status-publish","format-standard","hentry","category-6","tag-5-3","tag-integer","tag-562","tag-563"],"_links":{"self":[{"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/posts\/1370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/comments?post=1370"}],"version-history":[{"count":2,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/posts\/1370\/revisions"}],"predecessor-version":[{"id":1372,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/posts\/1370\/revisions\/1372"}],"wp:attachment":[{"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/media?parent=1370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/categories?post=1370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myvoipapp.com\/blogs\/yxh\/wp-json\/wp\/v2\/tags?post=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}