{"id":966781,"date":"2024-12-27T04:50:44","date_gmt":"2024-12-26T20:50:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966781.html"},"modified":"2024-12-27T04:50:47","modified_gmt":"2024-12-26T20:50:47","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8math%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/966781.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528math\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182403\/0aa60557-daf0-450f-a0d0-1570b5a802f3.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528math\u5e93\" \/><\/p>\n<p><p> Python\u4e2d<strong>\u4f7f\u7528math\u5e93<\/strong>\u4e3b\u8981\u662f\u4e3a\u4e86\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0cmath\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c<strong>\u5982\u5e73\u65b9\u6839\u3001\u5bf9\u6570\u3001\u4e09\u89d2\u51fd\u6570\u3001\u5706\u5468\u7387\u5e38\u91cf\u7b49<\/strong>\u3002\u4f7f\u7528math\u5e93\u7684\u7b2c\u4e00\u6b65\u662f\u5bfc\u5165\u8be5\u5e93\uff0c\u901a\u5e38\u901a\u8fc7<code>import math<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u5bfc\u5165\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528math\u5e93\u4e2d\u7684\u5404\u79cd\u51fd\u6570\u548c\u5e38\u91cf\u6765\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c<code>math.sqrt()<\/code>\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c<code>math.sin()<\/code>\u7528\u4e8e\u8ba1\u7b97\u6b63\u5f26\u503c\uff0c<code>math.pi<\/code>\u8868\u793a\u5706\u5468\u7387\u7b49\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528math\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165MATH\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528math\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\u3002\u5bfc\u5165\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cmath\u5e93\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u5e38\u91cf\u5c31\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u4e86\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cmath\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001MATH\u5e93\u4e2d\u7684\u5e38\u7528\u51fd\u6570<\/p>\n<\/p>\n<p><p>1\u3001\u5e73\u65b9\u6839\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5e73\u65b9\u6839\u662f\u6570\u5b66\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528math\u5e93\u4e2d\u7684<code>math.sqrt()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6307\u6570\u548c\u5bf9\u6570\u51fd\u6570<\/p>\n<\/p>\n<p><p>math\u5e93\u63d0\u4f9b\u4e86\u8ba1\u7b97\u6307\u6570\u548c\u5bf9\u6570\u7684\u51fd\u6570\u3002<code>math.exp(x)<\/code>\u7528\u4e8e\u8ba1\u7b97e^x\u7684\u503c\uff0c\u5176\u4e2de\u662f\u81ea\u7136\u5bf9\u6570\u7684\u5e95\u6570\u3002<code>math.log(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u81ea\u7136\u5bf9\u6570\u3002\u6b64\u5916\uff0c<code>math.log10(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>exp_result = math.exp(2)<\/p>\n<p>log_result = math.log(10)<\/p>\n<p>log10_result = math.log10(100)<\/p>\n<p>print(exp_result)  # \u8f93\u51fa\uff1a7.38905609893065<\/p>\n<p>print(log_result)  # \u8f93\u51fa\uff1a2.302585092994046<\/p>\n<p>print(log10_result)  # \u8f93\u51fa\uff1a2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u4e09\u89d2\u51fd\u6570<\/p>\n<\/p>\n<p><p>math\u5e93\u4e2d\u5305\u542b\u4e86\u8ba1\u7b97\u4e09\u89d2\u51fd\u6570\u7684\u51fd\u6570\uff0c\u5982\u6b63\u5f26\u3001\u4f59\u5f26\u548c\u6b63\u5207\u51fd\u6570\u3002<code>math.sin(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u6b63\u5f26\u503c\uff0c<code>math.cos(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u4f59\u5f26\u503c\uff0c<code>math.tan(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u6b63\u5207\u503c\u3002\u8fd9\u91cc\u7684x\u662f\u5f27\u5ea6\u5236\u7684\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>sin_result = math.sin(math.pi \/ 2)<\/p>\n<p>cos_result = math.cos(math.pi)<\/p>\n<p>tan_result = math.tan(math.pi \/ 4)<\/p>\n<p>print(sin_result)  # \u8f93\u51fa\uff1a1.0<\/p>\n<p>print(cos_result)  # \u8f93\u51fa\uff1a-1.0<\/p>\n<p>print(tan_result)  # \u8f93\u51fa\uff1a1.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001MATH\u5e93\u4e2d\u7684\u5e38\u91cf<\/p>\n<\/p>\n<p><p>1\u3001\u5706\u5468\u7387<\/p>\n<\/p>\n<p><p>math\u5e93\u63d0\u4f9b\u4e86\u5706\u5468\u7387\u7684\u5e38\u91cf<code>math.pi<\/code>\uff0c\u5b83\u5927\u7ea6\u7b49\u4e8e3.141592653589793\u3002\u8fd9\u4e2a\u5e38\u91cf\u5728\u8ba1\u7b97\u5706\u7684\u9762\u79ef\u548c\u5468\u957f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>radius = 5<\/p>\n<p>area = math.pi * radius  2<\/p>\n<p>circumference = 2 * math.pi * radius<\/p>\n<p>print(area)  # \u8f93\u51fa\uff1a78.53981633974483<\/p>\n<p>print(circumference)  # \u8f93\u51fa\uff1a31.41592653589793<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u81ea\u7136\u5bf9\u6570\u7684\u5e95\u6570<\/p>\n<\/p>\n<p><p>\u81ea\u7136\u5bf9\u6570\u7684\u5e95\u6570e\u662f\u4e00\u4e2a\u6570\u5b66\u5e38\u6570\uff0c\u5728math\u5e93\u4e2d\u8868\u793a\u4e3a<code>math.e<\/code>\uff0c\u5927\u7ea6\u7b49\u4e8e2.718281828459045\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>exponential = math.e  2<\/p>\n<p>print(exponential)  # \u8f93\u51fa\uff1a7.3890560989306495<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001MATH\u5e93\u4e2d\u7684\u5176\u4ed6\u6709\u7528\u51fd\u6570<\/p>\n<\/p>\n<p><p>1\u3001\u5411\u4e0a\u548c\u5411\u4e0b\u53d6\u6574<\/p>\n<\/p>\n<p><p><code>math.ceil(x)<\/code>\u51fd\u6570\u7528\u4e8e\u5c06x\u5411\u4e0a\u53d6\u6574\uff0c<code>math.floor(x)<\/code>\u51fd\u6570\u7528\u4e8e\u5c06x\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>ceil_result = math.ceil(4.2)<\/p>\n<p>floor_result = math.floor(4.2)<\/p>\n<p>print(ceil_result)  # \u8f93\u51fa\uff1a5<\/p>\n<p>print(floor_result)  # \u8f93\u51fa\uff1a4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u7edd\u5bf9\u503c\u548c\u7b26\u53f7\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>math.fabs(x)<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97x\u7684\u7edd\u5bf9\u503c\uff0c\u4e0e\u5185\u5efa\u7684<code>abs()<\/code>\u51fd\u6570\u7c7b\u4f3c\u3002<code>math.copysign(x, y)<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56dex\u7684\u7edd\u5bf9\u503c\u5e76\u8d4b\u4e88y\u7684\u7b26\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>abs_result = math.fabs(-10)<\/p>\n<p>copysign_result = math.copysign(10, -1)<\/p>\n<p>print(abs_result)  # \u8f93\u51fa\uff1a10.0<\/p>\n<p>print(copysign_result)  # \u8f93\u51fa\uff1a-10.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u6700\u5927\u516c\u7ea6\u6570\u548c\u6700\u5c0f\u516c\u500d\u6570<\/p>\n<\/p>\n<p><p>Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u7684math\u5e93\u63d0\u4f9b\u4e86\u8ba1\u7b97\u6700\u5927\u516c\u7ea6\u6570\u7684\u51fd\u6570<code>math.gcd(x, y)<\/code>\uff0c\u4ecePython 3.9\u5f00\u59cb\uff0cmath\u5e93\u4e2d\u8fd8\u52a0\u5165\u4e86\u8ba1\u7b97\u6700\u5c0f\u516c\u500d\u6570\u7684\u51fd\u6570<code>math.lcm(x, y)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>gcd_result = math.gcd(48, 180)<\/p>\n<h2><strong>lcm_result = math.lcm(12, 15)  # \u9700\u8981Python 3.9\u4ee5\u4e0a\u7248\u672c<\/strong><\/h2>\n<p>print(gcd_result)  # \u8f93\u51fa\uff1a12<\/p>\n<h2><strong>print(lcm_result)  # \u8f93\u51fa\uff1a60<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001MATH\u5e93\u7684\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>1\u3001\u5f27\u5ea6\u4e0e\u89d2\u5ea6\u7684\u8f6c\u6362<\/p>\n<\/p>\n<p><p>math\u5e93\u4e2d\u7684\u4e09\u89d2\u51fd\u6570\u4f7f\u7528\u7684\u662f\u5f27\u5ea6\u5236\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528\u89d2\u5ea6\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.radians(x)<\/code>\u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\uff0c\u6216\u4f7f\u7528<code>math.degrees(x)<\/code>\u5c06\u5f27\u5ea6\u8f6c\u6362\u4e3a\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>radians = math.radians(180)<\/p>\n<p>degrees = math.degrees(math.pi)<\/p>\n<p>print(radians)  # \u8f93\u51fa\uff1a3.141592653589793<\/p>\n<p>print(degrees)  # \u8f93\u51fa\uff1a180.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6570\u503c\u8303\u56f4\u4e0e\u7cbe\u5ea6<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528math\u5e93\u4e2d\u7684\u51fd\u6570\u65f6\uff0c\u8981\u6ce8\u610f\u51fd\u6570\u7684\u53c2\u6570\u8303\u56f4\u4ee5\u53ca\u8fd4\u56de\u503c\u7684\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c<code>math.sqrt()<\/code>\u51fd\u6570\u8981\u6c42\u53c2\u6570\u4e3a\u975e\u8d1f\u6570\uff0c\u5426\u5219\u4f1a\u5f15\u53d1ValueError\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cmath\u5e93\u662fPython\u4e2d\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u90e8\u5206\u6570\u5b66\u8ba1\u7b97\u9700\u6c42\u3002\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\uff0c\u5e76\u6ce8\u610f\u53c2\u6570\u7684\u8303\u56f4\u548c\u8fd4\u56de\u503c\u7684\u7cbe\u5ea6\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528math\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6570\u5b66\u8ba1\u7b97\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u7684math\u5e93\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u51fd\u6570\uff1f<\/strong><br \/>math\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a<code>math.sqrt()<\/code>\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c<code>math.factorial()<\/code>\u7528\u4e8e\u8ba1\u7b97\u9636\u4e58\uff0c<code>math.sin()<\/code>\u548c<code>math.cos()<\/code>\u7528\u4e8e\u4e09\u89d2\u51fd\u6570\u8ba1\u7b97\u3002\u6b64\u5916\uff0c<code>math.pi<\/code>\u5e38\u91cf\u53ef\u7528\u4e8e\u8868\u793a\u5706\u5468\u7387\uff0c<code>math.e<\/code>\u8868\u793a\u81ea\u7136\u5bf9\u6570\u7684\u5e95\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528math\u5e93\uff1f<\/strong><br \/>\u8981\u4f7f\u7528math\u5e93\uff0c\u9996\u5148\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528<code>import math<\/code>\u8bed\u53e5\u3002\u5bfc\u5165\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>math.<\/code>\u524d\u7f00\u6765\u8c03\u7528\u5e93\u4e2d\u7684\u51fd\u6570\u548c\u5e38\u91cf\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5e73\u65b9\u6839\u53ef\u4ee5\u4f7f\u7528<code>math.sqrt(16)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0cmath\u5e93\u4e0enumpy\u5e93\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>math\u5e93\u4e3b\u8981\u63d0\u4f9b\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u5355\u4e2a\u503c\u7684\u64cd\u4f5c\uff1b\u800cnumpy\u5e93\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u6570\u7ec4\u548c\u77e9\u9635\u7684\u8fd0\u7b97\uff0c\u529f\u80fd\u66f4\u4e3a\u4e30\u5bcc\u3002numpy\u4e2d\u6709\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u5b66\u51fd\u6570\u548c\u64cd\u4f5c\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\u548c\u590d\u6742\u7684\u6570\u503c\u8ba1\u7b97\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u6570\u7ec4\u6570\u636e\uff0cnumpy\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4f7f\u7528math\u5e93\u4e3b\u8981\u662f\u4e3a\u4e86\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0cmath\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u5982\u5e73\u65b9\u6839\u3001\u5bf9\u6570\u3001 [&hellip;]","protected":false},"author":3,"featured_media":966789,"comment_status":"closed","ping_status":"","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\/966781"}],"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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=966781"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966781\/revisions"}],"predecessor-version":[{"id":966791,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966781\/revisions\/966791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966789"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}