{"id":28306,"date":"2023-05-05T12:08:05","date_gmt":"2023-05-05T04:08:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=28306"},"modified":"2023-07-30T23:44:35","modified_gmt":"2023-07-30T15:44:35","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88python%e4%b8%ad%e7%9a%84%e6%95%b4%e9%99%a4%e6%98%af%e5%90%91%e4%b8%8b%e5%8f%96%e6%95%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/28306.html","title":{"rendered":"\u4e3a\u4ec0\u4e48Python\u4e2d\u7684\u6574\u9664\u662f\u5411\u4e0b\u53d6\u6574"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>\u8fd9\u4e2a\u95ee\u9898\u8ba9\u4e00\u4e9b\u4eba\u611f\u5230\u56f0\u6270\uff0c\u4f46\u5176\u5b9e\u662f\u6709\u6570\u5b66\u8003\u8651\u7684\u3002\u6574\u9664\u64cd\u4f5c\uff08\/\/\uff09\u4e0e\u53d6\u6a21\u64cd\u4f5c\uff08%\uff09\u662f\u4e00\u4e2a\u7ec4\u5408\uff0c\u5171\u540c\u5b9e\u73b0\u4e00\u4e2a\u6570\u5b66\u5173\u7cfb\uff08\u6240\u6709\u53d8\u91cf\u90fd\u662f\u6574\u6570\uff09\uff1a# a\/b = q \u4f59 r# b*q + r = a\uff0c\u5176\u4e2d 0 &lt;= r &lt; b\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-28307\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-18.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-18.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-18-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4e3a\u4ec0\u4e48Python\u4e2d\u7684\u6574\u9664\u662f\u5411\u4e0b\u53d6\u6574<\/strong><\/h2>\n<p>\u8fd9\u4e2a\u95ee\u9898\u8ba9\u4e00\u4e9b\u4eba\u611f\u5230\u56f0\u6270\uff0c\u4f46\u5176\u5b9e\u662f\u6709\u6570\u5b66\u8003\u8651\u7684\u3002\u6574\u9664\u64cd\u4f5c\uff08\/\/\uff09\u4e0e\u53d6\u6a21\u64cd\u4f5c\uff08%\uff09\u662f\u4e00\u4e2a\u7ec4\u5408\uff0c\u5171\u540c\u5b9e\u73b0\u4e00\u4e2a\u6570\u5b66\u5173\u7cfb\uff08\u6240\u6709\u53d8\u91cf\u90fd\u662f\u6574\u6570\uff09\uff1a<\/p>\n<p># a\/b = q \u4f59 r# b*q + r = a\uff0c\u5176\u4e2d 0 &lt;= r &lt; b\u3002<\/p>\n<p>\u5f53 a \u4e3a\u8d1f\u6570\uff08b \u4fdd\u6301\u6b63\u6570\uff09\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u9009\u62e9\uff1a\u4e00\u4e2a\u662f\u8ba9 q \u5411\u96f6\u53d6\u6574\uff0c\u4e8e\u662f\u4f59\u6570 r \u4f1a\u53d8\u6210\u8d1f\u6570\uff0c\u5373 0 &lt;= abs(r) &lt; b\uff1b\u53e6\u4e00\u4e2a\u662f\u8ba9 q \u5411\u8d1f\u65e0\u7a77\u53d6\u6574\uff0c\u5219\u4f59\u6570\u7684\u8bbe\u5b9a\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p>\u6570\u5b66\u5bb6\u4eec\u603b\u662f\u503e\u5411\u4e8e\u540e\u4e00\u79cd\u9009\u62e9\u3002<\/p>\n<p>\u800c\u5728 Python \u4e2d\uff0c\u6211\u4e5f\u9009\u62e9\u4e86\u540e\u4e00\u79cd\u65b9\u6848\uff0c\u56e0\u4e3a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b58\u5728\u4e00\u4e9b a \u65e2\u53ef\u80fd\u4e3a\u6b63\u6570\u4e5f\u53ef\u80fd\u4e3a\u8d1f\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5 POSIX \u65f6\u95f4\u6233\uff081970\u5e74\u4ee5\u6765\u7684\u79d2\u6570\uff09\u4e3a\u4f8b\uff0c\u5982\u679c\u6211\u4eec\u8981\u83b7\u53d6\u5f53\u524d\u7684\u5177\u4f53\u65f6\u95f4\uff0c\u7531\u4e8e\u6bcf\u5929\u4e3a 24 * 3600 = 86400 \u79d2\uff0c\u53ea\u9700\u76f4\u63a5\u53d6\u6a21 t % 86400 \u5c31\u53ef\u4ee5\u4e86\u3002\u5047\u5982\u65f6\u95f4\u6233\u4e3a\u8d1f\u6570\uff0c\u5373\u5728 1970 \u5e74\u4e4b\u524d\uff0c\u90a3\u4e48\uff0c\u4e3a\u83b7\u5f97\u6b63\u786e\u7ed3\u679c\uff0c\u5c31\u53ea\u80fd\u7528\u5411\u4e0b\u53d6\u6574\uff0c\u800c\u4e0d\u662f\u5411\u96f6\u53d6\u6574\u3002<\/p>\n<p>\u6211\u80fd\u60f3\u5230\u7684\u53e6\u4e00\u4e2a\u4f8b\u5b50\u662f\u56fe\u50cf\u70b9\u4f4d\u7684\u8ba1\u7b97\u3002\u800c\u4e14\u6211\u5f88\u786e\u5b9a\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u5b83\u4f8b\u5b50\u3002<\/p>\n<p>\u987a\u4fbf\u63d0\u4e00\u4e0b\uff0c\u5982\u679c b \u53d6\u8d1f\u6570\uff0c\u6574\u4e2a\u7b49\u5f0f\u5c31\u53cd\u8fc7\u6765\u4e86\uff1a0 &gt;= r &gt; b\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001Python\u4e2d\u53d6\u6574\u7684\u51e0\u79cd\u65b9\u6cd5\u5c0f\u7ed3<\/strong><\/h2>\n<h3 class=\"wp-block-heading\"><strong>1<\/strong><strong>\u3001\u5411\u4e0b\u53d6\u6574<\/strong><\/h3>\n<p>\u5411\u4e0b\u53d6\u6574\u76f4\u63a5\u7528\u5185\u5efa\u7684 int() \u51fd\u6570\u5373\u53ef\uff1a<\/p>\n<p>&gt;&gt;&gt; a = 3.75<\/p>\n<p>&gt;&gt;&gt; int(a)<\/p>\n<h3 class=\"wp-block-heading\"><strong>2<\/strong><strong>\u3001\u56db\u820d\u4e94\u5165<\/strong><\/h3>\n<p>\u5bf9\u6570\u5b57\u8fdb\u884c\u56db\u820d\u4e94\u5165\u7528 round() \u51fd\u6570\uff1a<\/p>\n<p>&gt;&gt;&gt; round(3.25); round(4.85)<\/p>\n<p>3.0<\/p>\n<p>5.0<\/p>\n<h3 class=\"wp-block-heading\"><strong>3<\/strong><strong>\u3001\u5411\u4e0a\u53d6\u6574<\/strong><\/h3>\n<p>\u5411\u4e0a\u53d6\u6574\u9700\u8981\u7528\u5230 math \u6a21\u5757\u4e2d\u7684 ceil() \u65b9\u6cd5:<\/p>\n<p>&gt;&gt;&gt; import math<\/p>\n<p>&gt;&gt;&gt; math.ceil(3.25)<\/p>\n<p>4.0<\/p>\n<p>&gt;&gt;&gt; math.ceil(3.75)<\/p>\n<p>4.0<\/p>\n<p>&gt;&gt;&gt; math.ceil(4.85)<\/p>\n<p>5.0<\/p>\n<h3 class=\"wp-block-heading\"><strong>4<\/strong><strong>\u3001\u5206\u522b\u53d6\u6574\u6570\u90e8\u5206\u548c\u5c0f\u6570\u90e8\u5206<\/strong><\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5206\u522b\u83b7\u53d6\u6574\u6570\u90e8\u5206\u548c\u5c0f\u6570\u90e8\u5206\uff0c\u8fd9\u65f6\u53ef\u4ee5\u7528 math \u6a21\u5757\u4e2d\u7684 modf() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5c0f\u6570\u90e8\u5206\u548c\u6574\u6570\u90e8\u5206\u7684\u5143\u7ec4\uff1a<\/p>\n<p>&gt;&gt;&gt; import math<\/p>\n<p>&gt;&gt;&gt; math.modf(3.25)<\/p>\n<p>(0.25, 3.0)<\/p>\n<p>&gt;&gt;&gt; math.modf(3.75)<\/p>\n<p>(0.75, 3.0)<\/p>\n<p>&gt;&gt;&gt; math.modf(4.2)<\/p>\n<p>(0.20000000000000018, 4.0)<\/p>\n<p>\u6709\u4eba\u53ef\u80fd\u4f1a\u5bf9\u6700\u540e\u4e00\u4e2a\u8f93\u51fa\u7ed3\u679c\u611f\u5230\u8be7\u5f02\uff0c\u6309\u7406\u8bf4\u5b83\u5e94\u8be5\u8fd4\u56de (0.2, 4.0) \u624d\u5bf9\u3002\u8fd9\u91cc\u6d89\u53ca\u5230\u4e86\u53e6\u4e00\u4e2a\u95ee\u9898\uff0c\u5373\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u8868\u793a\uff0c\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u65e0\u6cd5\u7cbe\u786e\u7684\u8868\u793a\u5c0f\u6570\u7684\uff0c\u81f3\u5c11\u76ee\u524d\u7684\u8ba1\u7b97\u673a\u505a\u4e0d\u5230\u8fd9\u4e00\u70b9\u3002\u4e0a\u4f8b\u4e2d\u6700\u540e\u7684\u8f93\u51fa\u7ed3\u679c\u53ea\u662f 0.2 \u5728\u8ba1\u7b97\u4e2d\u7684\u8fd1\u4f3c\u8868\u793a\u3002Python \u548c C \u4e00\u6837, \u91c7\u7528 IEEE 754 \u89c4\u8303\u6765\u5b58\u50a8\u6d6e\u70b9\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8fd9\u4e2a\u95ee\u9898\u8ba9\u4e00\u4e9b\u4eba\u611f\u5230\u56f0\u6270\uff0c\u4f46\u5176\u5b9e\u662f\u6709\u6570\u5b66\u8003\u8651\u7684\u3002\u6574\u9664\u64cd\u4f5c\uff08\/\/\uff09\u4e0e\u53d6\u6a21\u64cd\u4f5c\uff08%\uff09\u662f\u4e00\u4e2a\u7ec4\u5408\uff0c\u5171\u540c\u5b9e\u73b0\u4e00\u4e2a\u6570\u5b66\u5173 [&hellip;]","protected":false},"author":1,"featured_media":28307,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/28306"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=28306"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/28306\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/28307"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=28306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=28306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=28306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}