{"id":2595,"date":"2015-06-19T10:36:00","date_gmt":"2015-06-19T02:36:00","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=2595"},"modified":"2015-06-23T10:13:24","modified_gmt":"2015-06-23T02:13:24","slug":"%e3%80%90delphi%e3%80%91%e7%8e%a9%e8%bd%ac%e6%b5%ae%e7%82%b9%e6%95%b0%e8%bd%ac%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=2595","title":{"rendered":"\u3010Delphi\u3011\u73a9\u8f6c\u6d6e\u70b9\u6570\u8f6c\u6574\u6570"},"content":{"rendered":"<p>Delphi\u4e2d\u63d0\u4f9b\u4e86\u4e09\u4e2a\u51fd\u6570:<\/p>\n<p>Trunc: \u5c06\u6d6e\u70b9\u6570\u7684\u6574\u6570\u90e8\u5206\u8fd4\u56de\u3002<br \/>\nRound: \u5c06\u6d6e\u70b9\u6570\u56db\u820d\u4e94\u5165\u540e\u8fd4\u56de\u6574\u6570\u90e8\u5206\u3002<br \/>\nInt: \u5c06\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u90e8\u5206\u53bb\u6389\uff0c\u8fd4\u56de\u53ea\u4fdd\u7559\u4e86\u6574\u6570\u90e8\u5206\u7684Extended\u578b\u3002<\/p>\n<p>\u5728D2007 + Win7 + Intel\u5e73\u53f0\u4e0b\u6d4b\u8bd5\uff0c \u7531\u4e8eInt\u8fd4\u56de\u7684\u5e76\u975e\u6574\u578b\uff0c\u6211\u4eec\u628a\u5b83\u6392\u9664\u6389\u3002\u6d4b\u8bd5\u7ed3\u679c\u662f Round \u6027\u80fd\u9ad8\u51faTrunc\u597d\u51e0\u500d\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48Trunc\u6162\u4e86\u5462\uff0c\u67e5\u770b\u4e86System\u4e2d\u7684ASM\u4ee3\u7801\uff0cTrunc\u670911\u884c\u6307\u4ee4\uff0cRound\u5219\u53ea\u67095\u884c\u3002<\/p>\n<p>\u51fa\u4e8e\u597d\u5947\uff0c\u901a\u8fc7\u7f51\u7edc\u7814\u7a76\u4e0bTrunc\u7684\u7b97\u6cd5\uff0c\u77e5\u9053\u4e86\u5728 IEEE 754 \u91cc\u89c4\u5b9a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u662f 64 \u4f4d\uff0c\u800c\u5176\u4e2d\u7b26\u53f7\u4f4d\u5360 1 \u4f4d\uff0c\u6307\u6570\u90e8\u5206\u662f 11 \u4f4d\uff0c\u90a3\u4e48\u5c3e\u6570\u90e8\u5206\u5c31\u662f 52 \u4f4d\uff0c\u90a3\u4e48 1 * 2^52 = $10000000000000\u3002\u8bbe\u8981\u53d6\u6574\u7684\u6d6e\u70b9\u6570\u4e3aV\uff0c \u90a3\u4e48\u901a\u8fc7\u7b26\u70b9\u6570\u8fd0\u7b97\u7684\u5bf9\u9636\u6cd5\uff0c\u00a0PInteger(V + $10000000000000 \u7684\u7ed3\u679c)^ \u5c31\u662f\u8ba1\u7b97\u7ed3\u679c\u4e86\u3002<\/p>\n<p>\u4e8e\u662f\u6709\u4e86\u4e0b\u9762\u7684\u51fd\u6570\uff1a<\/p>\n<pre class=\"toolbar:2 lang:delphi decode:true  \">function MyTrunc(const V: Double): Integer; inline;\r\nvar\r\n\u00a0 D: Double;\r\nbegin\r\n\u00a0 if V &gt; 0 then\r\n\u00a0\u00a0\u00a0 D := V - 0.499999999999 + $18000000000000\r\n\u00a0 else\r\n\u00a0\u00a0\u00a0 D := V + 0.499999999999 + $18000000000000;\r\n\u00a0 Result := PInteger(@D)^;\r\nend;<\/pre>\n<p>\u518d\u6765\u6d4b\u8bd5\u770b\u770b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"toolbar:2 lang:delphi decode:true \">procedure TForm1.Button6Click(Sender: TObject);\r\nconst\r\nC = 100000000;\r\nvar\r\nu: Extended;\r\nI: Integer;\r\nv, v1, v2: Integer;\r\nt, t1, t2: Int64;\r\nbegin\r\nu := Now;\r\n\r\nt := GetTimestamp;\r\nfor I := 0 to C - 1 do\r\nv := Trunc(u);\r\nt := GetTimestamp - t;\r\n\r\nt1 := GetTimestamp;\r\nfor I := 0 to C - 1 do\r\nv1 := Round(u);\r\nt1 := GetTimestamp - t1;\r\n\r\nt2 := GetTimestamp;\r\nfor I := 0 to C - 1 do\r\nv2 := MyTrunc(u);\r\nt2 := GetTimestamp - t2;\r\n\r\nShowMessage(Format('Trunc: %dms, Round: %dms, MyTrunc: %dms'#13'%d, %d, %d',\r\n[t, t1, t2, v, v1, v2]));\r\nend;<\/pre>\n<p>&nbsp;<\/p>\n<p>\/\/ \u6ce8:\u00a0GetTimestamp \u4e3a\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u5668\u8fd4\u56de\u5355\u4f4d\u4e3ams\uff08\u4e0eQWorker\u4e2d\u7684\u8fd4\u56de0.1ms\u6709\u6240\u4e0d\u540c\uff09\uff0c\u53ef\u6362\u6210GetTickCount\u3002<\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2596\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2015\/06\/QQ\u622a\u56fe20150619103148.bmp\" alt=\"QQ\u622a\u56fe20150619103148\" width=\"366\" height=\"135\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u54c8\u54c8\uff0c\u5c45\u7136\u6210\u4e86\u6700\u5feb\u7684\u4e86\u3002<\/p>\n<p>\u8981\u6ce8\u610f\u7684\u662f\uff0c MyTrunc \u6211\u8fd9\u91cc\u5e76\u6ca1\u6709\u50cfSystem\u7684Trunc\u90a3\u6837\u5199\u6c47\u7f16\u4ee3\u7801\uff0c\u4e00\u662f\u90a3\u6837\u4e0d\u80fdinline\u4e86\uff0c\u4e8c\u662f\u901a\u8fc7\u89c2\u6d4b\u7f16\u8bd1\u7ed3\u679c\u53d1\u73b0\u5df2\u7ecf\u662f\u5f88\u7b80\u5355\u7684\u51e0\u884c\u6307\u4ee4\u4e86\u3002<\/p>\n<p>\u901a\u8fc7\u5bf9MyTrunc\u7b80\u5355\u4e2a\u4fee\u6539\u4e0b\uff0c\u6210\u4e86MyRound:<\/p>\n<pre class=\"toolbar:2 lang:delphi decode:true \">function MyRound(const V: Double): Integer; inline;\r\nvar\r\n  D: Double;\r\nbegin\r\n  D := V + $18000000000000;\r\n  Result := PInteger(@D)^;\r\nend;\r\n<\/pre>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p><a href=\"http:\/\/www.360doc.com\/content\/14\/0308\/22\/3520047_358884576.shtml\">http:\/\/www.360doc.com\/content\/14\/0308\/22\/3520047_358884576.shtml<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delphi\u4e2d\u63d0\u4f9b\u4e86\u4e09\u4e2a\u51fd\u6570: Trun [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2595","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":7912,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2595"}],"version-history":[{"count":5,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2595\/revisions"}],"predecessor-version":[{"id":2605,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2595\/revisions\/2605"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}