{"id":2905,"date":"2025-03-18T00:21:50","date_gmt":"2025-03-17T16:21:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2905"},"modified":"2025-03-18T00:21:50","modified_gmt":"2025-03-17T16:21:50","slug":"python-math-%e6%a8%a1%e5%9d%97%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/18\/python-math-%e6%a8%a1%e5%9d%97%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python math \u6a21\u5757\u8be6\u89e3"},"content":{"rendered":"\n<p>Python \u7684 <code>math<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u6db5\u76d6 <strong>\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u3001\u5bf9\u6570\u3001\u4e09\u89d2\u51fd\u6570\u3001\u5e42\u8fd0\u7b97\u3001\u9636\u4e58\u3001\u7279\u6b8a\u6570\u5b66\u5e38\u6570<\/strong> \u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd <code>math<\/code> \u6a21\u5757\u7684\u5e38\u7528\u51fd\u6570\u53ca\u5e94\u7528\u573a\u666f\uff0c\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5bfc\u5165 <code>math<\/code> \u6a21\u5757<\/strong><\/h2>\n\n\n\n<p>\u5728\u4f7f\u7528 <code>math<\/code> \u6a21\u5757\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>math<\/code> \u6a21\u5757\u7684\u6570\u5b66\u5e38\u91cf<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e38\u91cf<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.pi<\/code><\/td><td>\u5706\u5468\u7387 \u03c0\uff0c\u7ea6 3.141592653589793<\/td><\/tr><tr><td><code>math.e<\/code><\/td><td>\u81ea\u7136\u5bf9\u6570\u7684\u5e95\u6570 e\uff0c\u7ea6 2.718281828459045<\/td><\/tr><tr><td><code>math.tau<\/code><\/td><td>\u5706\u5e38\u6570 \u03c4\uff0c\u7b49\u4e8e 2\u03c0\uff0c\u7ea6 6.283185307179586<\/td><\/tr><tr><td><code>math.inf<\/code><\/td><td>\u6b63\u65e0\u7a77\u5927\uff08<code>float('inf')<\/code>\uff09<\/td><\/tr><tr><td><code>-math.inf<\/code><\/td><td>\u8d1f\u65e0\u7a77\u5927<\/td><\/tr><tr><td><code>math.nan<\/code><\/td><td>\u975e\u6570\u503c NaN\uff08<code>float('nan')<\/code>\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\nprint(math.pi)   # 3.141592653589793\nprint(math.e)    # 2.718281828459045\nprint(math.tau)  # 6.283185307179586\nprint(math.inf)  # inf\nprint(math.nan)  # nan<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u57fa\u672c\u6570\u5b66\u8fd0\u7b97<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u53d6\u6574\u51fd\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.ceil(x)<\/code><\/td><td>\u5411\u4e0a\u53d6\u6574\uff0c\u8fd4\u56de\u4e0d\u5c0f\u4e8e <code>x<\/code> \u7684\u6700\u5c0f\u6574\u6570<\/td><\/tr><tr><td><code>math.floor(x)<\/code><\/td><td>\u5411\u4e0b\u53d6\u6574\uff0c\u8fd4\u56de\u4e0d\u5927\u4e8e <code>x<\/code> \u7684\u6700\u5927\u6574\u6570<\/td><\/tr><tr><td><code>math.trunc(x)<\/code><\/td><td>\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u53d6\u6574\u6570\u90e8\u5206\uff08\u5411 0 \u53d6\u6574\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.ceil(4.3))   # 5\nprint(math.floor(4.9))  # 4\nprint(math.trunc(-3.9)) # -3<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u5e42\u6b21\u4e0e\u6839\u53f7<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.pow(x, y)<\/code><\/td><td>\u8fd4\u56de <code>x<\/code> \u7684 <code>y<\/code> \u6b21\u5e42\uff08\u6d6e\u70b9\u6570\uff09<\/td><\/tr><tr><td><code>math.sqrt(x)<\/code><\/td><td>\u8fd4\u56de <code>x<\/code> \u7684\u5e73\u65b9\u6839<\/td><\/tr><tr><td><code>math.exp(x)<\/code><\/td><td>\u8fd4\u56de <code>e^x<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.pow(2, 3))   # 8.0\nprint(math.sqrt(16))    # 4.0\nprint(math.exp(2))      # 7.38905609893065<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u5bf9\u6570\u8fd0\u7b97<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.log(x, base)<\/code><\/td><td>\u4ee5 <code>base<\/code> \u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u9ed8\u8ba4 <code>base=e<\/code><\/td><\/tr><tr><td><code>math.log10(x)<\/code><\/td><td>\u4ee5 10 \u4e3a\u5e95\u7684\u5bf9\u6570<\/td><\/tr><tr><td><code>math.log2(x)<\/code><\/td><td>\u4ee5 2 \u4e3a\u5e95\u7684\u5bf9\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.log(8, 2))   # 3.0\nprint(math.log10(100))  # 2.0\nprint(math.log2(16))    # 4.0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u7edd\u5bf9\u503c\u4e0e\u7b26\u53f7\u5224\u65ad<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.fabs(x)<\/code><\/td><td>\u53d6 <code>x<\/code> \u7684\u7edd\u5bf9\u503c\uff08\u6d6e\u70b9\u6570\uff09<\/td><\/tr><tr><td><code>math.copysign(x, y)<\/code><\/td><td>\u53d6 <code>x<\/code> \u7684\u7edd\u5bf9\u503c\uff0c\u5e76\u8d4b\u4e88 <code>y<\/code> \u7684\u7b26\u53f7<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.fabs(-4.5))       # 4.5\nprint(math.copysign(3, -7))  # -3.0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.5 \u6700\u5927\u516c\u7ea6\u6570\u4e0e\u6700\u5c0f\u516c\u500d\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.gcd(a, b)<\/code><\/td><td>\u8ba1\u7b97 <code>a<\/code> \u548c <code>b<\/code> \u7684\u6700\u5927\u516c\u7ea6\u6570<\/td><\/tr><tr><td><code>math.lcm(a, b)<\/code><\/td><td>\u8ba1\u7b97 <code>a<\/code> \u548c <code>b<\/code> \u7684\u6700\u5c0f\u516c\u500d\u6570\uff08Python 3.9+\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.gcd(48, 18))  # 6\nprint(math.lcm(4, 6))    # 12<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.6 \u9636\u4e58<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.factorial(n)<\/code><\/td><td>\u8ba1\u7b97 <code>n!<\/code>\uff08n \u7684\u9636\u4e58\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.factorial(5))  # 120 (5*4*3*2*1)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4e09\u89d2\u51fd\u6570<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u89d2\u5ea6\u4e0e\u5f27\u5ea6\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.degrees(x)<\/code><\/td><td>\u5f27\u5ea6\u8f6c\u6362\u4e3a\u89d2\u5ea6<\/td><\/tr><tr><td><code>math.radians(x)<\/code><\/td><td>\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.degrees(math.pi))  # 180.0\nprint(math.radians(180))      # 3.141592653589793<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u5e38\u7528\u4e09\u89d2\u51fd\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.sin(x)<\/code><\/td><td>\u6b63\u5f26\u51fd\u6570\uff08\u5f27\u5ea6\uff09<\/td><\/tr><tr><td><code>math.cos(x)<\/code><\/td><td>\u4f59\u5f26\u51fd\u6570\uff08\u5f27\u5ea6\uff09<\/td><\/tr><tr><td><code>math.tan(x)<\/code><\/td><td>\u6b63\u5207\u51fd\u6570\uff08\u5f27\u5ea6\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>angle = math.radians(30)  # 30\u00b0 \u8f6c\u5f27\u5ea6\nprint(math.sin(angle))  # 0.5\nprint(math.cos(angle))  # 0.86602540378\nprint(math.tan(angle))  # 0.57735026919<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u53cd\u4e09\u89d2\u51fd\u6570<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.asin(x)<\/code><\/td><td>\u53cd\u6b63\u5f26\u51fd\u6570\uff0c\u8fd4\u56de\u5f27\u5ea6<\/td><\/tr><tr><td><code>math.acos(x)<\/code><\/td><td>\u53cd\u4f59\u5f26\u51fd\u6570\uff0c\u8fd4\u56de\u5f27\u5ea6<\/td><\/tr><tr><td><code>math.atan(x)<\/code><\/td><td>\u53cd\u6b63\u5207\u51fd\u6570\uff0c\u8fd4\u56de\u5f27\u5ea6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.degrees(math.asin(0.5)))  # 30.0\nprint(math.degrees(math.acos(0.5)))  # 60.0\nprint(math.degrees(math.atan(1)))    # 45.0<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u7279\u6b8a\u6570\u5b66\u51fd\u6570<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>math.isfinite(x)<\/code><\/td><td>\u662f\u5426\u4e3a\u6709\u9650\u6570<\/td><\/tr><tr><td><code>math.isinf(x)<\/code><\/td><td>\u662f\u5426\u4e3a\u65e0\u7a77\u5927<\/td><\/tr><tr><td><code>math.isnan(x)<\/code><\/td><td>\u662f\u5426\u4e3a NaN<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(math.isfinite(10))      # True\nprint(math.isinf(math.inf))   # True\nprint(math.isnan(math.nan))   # True<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p><code>math<\/code> \u6a21\u5757\u4e3a\u6570\u5b66\u8ba1\u7b97\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\uff0c\u5e38\u7528\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u57fa\u672c\u8fd0\u7b97<\/strong>\uff08\u53d6\u6574\u3001\u5e42\u3001\u6839\u53f7\uff09<\/li>\n\n\n\n<li><strong>\u5bf9\u6570\u8fd0\u7b97<\/strong><\/li>\n\n\n\n<li><strong>\u6700\u5927\u516c\u7ea6\u6570\/\u6700\u5c0f\u516c\u500d\u6570<\/strong><\/li>\n\n\n\n<li><strong>\u4e09\u89d2\u51fd\u6570\u4e0e\u89d2\u5ea6\u8f6c\u6362<\/strong><\/li>\n\n\n\n<li><strong>\u6570\u5b66\u5e38\u91cf\uff08\u03c0\u3001e\u3001\u221e\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u7279\u6b8a\u6570\u503c\u5224\u65ad<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u5de5\u7a0b\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\uff0c<code>math<\/code> \u6a21\u5757\u662f\u4e00\u4e2a\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7684 math \u6a21\u5757\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u6db5\u76d6 \u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u3001\u5bf9\u6570\u3001\u4e09\u89d2\u51fd\u6570\u3001\u5e42\u8fd0\u7b97\u3001\u9636\u4e58\u3001\u7279\u6b8a\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2906,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2905"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2905\/revisions"}],"predecessor-version":[{"id":2907,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2905\/revisions\/2907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2906"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}