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 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  math模块及其应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   <font size=\"4\"><font color=Red>__模块(module)__</font>是 Python 中非常重要的东西,可以把它理解为 Python 的扩展工具,可提供面向特定领域或方向的程序功能。Python 安装好之后,默认安装了一些模块,称为<font color=Red>__“标准库”__</font>,“标准库”中的模块<font color=Red>__不需要安装__</font>,就可以直接使用。  \n",
    "   <font size=\"4\">没有纳入标准库的模块,需要使用 <font color=Red>__“pip install模块名”__</font>安装之后才能使用。</font>\n",
    "  </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在数学运算之中,除了加、减、乘、除运算之外,还有其它更多的运算,比如乘方、开方、对数运算等等,要实现这些运算,可以使用Python 中的<font color=Red>__math模块__</font>。该模块是内置模块,不需要安装,可直接使用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python中导入模块的方法有两种:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 导入模块名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此时,程序可以调用模块名中的<font color=Red>__所有函数__</font>,语法表示如下:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "import  库名\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 调用模块中函数时,需明确指出函数所在的模块的名称,格式如下: "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "模块名.函数名(函数参数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.141592653589793\n",
      "2.23606797749979\n"
     ]
    }
   ],
   "source": [
    "import math   # 导入math模块,引用其中的函数时需要用在函数名前面加“math.”\n",
    "\n",
    "print(math.pi)             # 输出math模块中的pi值3.141592653589793\n",
    "print(math.sqrt(5))        # 调用math模块中的sqrt函数求解5的平方根并输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\">若调用模块中函数时,<font color=Red>__未指出函数所在的模块__</font>的名称,将抛异常。</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math   \n",
    "\n",
    "print(pi)   # 未指明模块名,抛未定义异常"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 直接导入库中的<font color=Red>__函数__</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=\"4\">可以同时引用多个函数,各函数间用逗号分隔,也可以用通配符“*”代替,表示引入该为中的所有函数,语法表示如下:</font> "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "from <库名 > import <函数名,函数名,...,函数名 >\n",
    "from <库名 > import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此时,调用该库的函数时不需要指明函数所在库的名称,格式如下: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<函数名 >(<函数参数 >)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import pi,sqrt  # 导入math中的常数pi和sqrt()函数,直接引用函数名\n",
    "\n",
    "\n",
    "print(pi)             # 输出math模块中的pi值3.141592653589793\n",
    "print(sqrt(5))        # 调用math模块中的sqrt函数求解5的平方根并输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import *    # 导入math中的所有函数,直接引用函数名\n",
    "\n",
    "\n",
    "print(pi)             # 输出math模块中的pi值3.141592653589793\n",
    "print(sqrt(5))        # 调用math模块中的sqrt函数求解5的平方根并输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一般程序较简单时,只引入一个库或所引用的函数仅在一个库中存在时,两种方法都可以使用。  \n",
    "当编写的程序较复杂、<font color=Red>__引用多个库时__</font>,可能在多个库中存在同名函数,而这些同名函数功能可能不同。这时建议使用<font color=Red>__第一种方法__</font>,明确指出所引用的函数来自于哪个库,以免出现错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "math库中包括:  \n",
    "24个数论与表示函数  \n",
    "10个幂和对数函数  \n",
    "9个三角函数  \n",
    "6个双曲函数  \n",
    "2个角度转换函数  \n",
    "4个特殊函数  \n",
    "5个数学常量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 24个数论与表示函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | ceil(x) | 返回 x 的向上取整,即大于或等于 x 的最小的整数 |\n",
    "| 2 | comb(n, k) | 返回不重复且无顺序地从 n 项中选择 k 项的方式总数 |\n",
    "| 3 | copysign(x, y) | 返回一个基于 x 的绝对值和 y 的符号的浮点数。在支持带符号零的平台上,copysign(1.0, -0.0) 返回 -1.0. |\n",
    "| 4 | fabs(x) | 返回 x 的绝对值 |\n",
    "| 5 | factorial(x) | 以一个整数返回 x 的阶乘。 |\n",
    "| 6 | floor(x) | 返回 x 的向下取整,小于或等于 x 的最大整数。 |\n",
    "| 7 | fmod(x, y) | 返回 fmod(x, y)  |\n",
    "| 8 | frexp(x) | 以 (m, e) 对的形式返回 x 的尾数和指数 |\n",
    "| 9 | fsum(iterable) | 返回迭代中的精确浮点值 |\n",
    "| 10 | gcd(\\*integers) | 返回给定的整数参数的最大公约数 |\n",
    "| 10 | isclose(a, b, \\*, rel_tol=1e-09, abs_tol=0.0) |  a  b 的值比较接近则返回 True,否则返回 False |\n",
    "| 12 | isfinite(x) | 如果 x 既不是无穷大也不是NaN,则返回 True ,否则返回 False |\n",
    "| 13 | isinf(x) | 如果 x 是正或负无穷大,则返回 True ,否则返回 False  |\n",
    "| 14 | isnan(x) | 如果 x  NaN(不是数字),则返回 True ,否则返回 False |\n",
    "| 15 | isqrt(n) | 返回非负整数 n 的整数平方根。 这就是对 n 的实际平方根向下取整,或者相当于使得   n 的最大整数 a。 |\n",
    "| 16 | lcm(\\*integers) | 返回给定的整数参数的最小公倍数 |\n",
    "| 17 | ldexp(x, i) | 返回 x \\* (2\\*\\*i)  这基本上是函数 frexp() 的反函数。 |\n",
    "| 18 | modf(x) | 返回 x 的小数和整数部分。两个结果都带有 x 的符号并且是浮点数 |\n",
    "| 19 | nextafter(x, y) | 返回 x 趋向于 y 的最接近的浮点数值 |\n",
    "| 20 | perm(n, k=None) | 返回不重复且有顺序地从 n 项中选择 k 项的方式总数 |\n",
    "| 21 | prod(iterable, \\*, start=1) | 计算输入的 iterable 中所有元素的积。 积的默认 start 值为 1 |\n",
    "| 22 | remainder(x, y) | 返回 IEEE 754 风格的 x 相对于 y 的余数。对于有限 x 和有限非零 y ,这是差异 x - n\\*y ,其中 n 是与商 x / y 的精确值最接近的整数。如果 x / y 恰好位于两个连续整数之间,则将最接近的 偶数 用作 n  |\n",
    "| 23 | trunc(x) | 返回去除小数部分的 x ,只留下整数部分。 |\n",
    "| 24 | ulp(x) | 返回浮点数 x 的最小有效比特位的值 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10个幂和对数函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | cbrt(x) | 返回 x的立方根(3.11新增) |\n",
    "| 2 | exp(x) | 返回 e  x 幂,其中 e = 2.718281... 是自然对数的基数。这通常比 math.e ** x  pow(math.e, x) 更精确。 |\n",
    "| 3 | exp2(x) | 返回 2 的x次幂(3.11新增) |\n",
    "| 4 | expm1(x) | 返回 e  x 次幂,减1。这里 e 是自然对数的基数。 |\n",
    "| 5 | log(x[, base]) | 使用一个参数,返回 x 的自然对数(底为 e  |\n",
    "| 6 | log1p(x) | 返回 1+x 的自然对数(以 e 为底)。 以对于接近零的 x 精确的方式计算结果。 |\n",
    "| 7 | log2(x) | 返回 x 以2为底的对数。这通常比 log(x, 2) 更准确。 |\n",
    "| 8 | log10(x) | 返回 x 底为10的对数。这通常比 log(x, 10) 更准确 |\n",
    "| 9 | pow(x, y) | 将返回 x  y 次幂, pow(1.0, x)  pow(x, 0.0) 总是返回 1.0  |\n",
    "| 10 | sqrt(x) | 返回 x 的平方根 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9个三角函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | acos(x) | 返回以弧度为单位的 x 的反余弦值。 结果范围在 0  pi 之间 |\n",
    "| 2 | asin(x) | 返回以弧度为单位的 x 的反正弦值。 结果范围在 -pi/2  pi/2 之间 |\n",
    "| 3 | atan(x) | 返回以弧度为单位的 x 的反正切值。 结果范围在 -pi/2  pi/2 之间 |\n",
    "| 4 | atan2(y, x) | 以弧度为单位返回 atan(y / x) 。结果是在 -pi  pi 之间 |\n",
    "| 5 | cos(x) | 返回 x 弧度的余弦值 |\n",
    "| 6 | dist(p, q) | 返回 p  q 两点之间的欧几里得距离,以一个坐标序列(或可迭代对象)的形式给出。 两个点必须具有相同的维度。 |\n",
    "| 7 | hypot(\\*coordinates) | 返回欧几里得范数,sqrt(sum(x\\*\\*2 for x in coordinates)) 这是从原点到坐标给定点的向量长度 |\n",
    "| 8 | sin(x) | 返回 x 弧度的正弦值 |\n",
    "| 9 | tan(x) | 返回 x 弧度的正切值 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6个双曲函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | acosh(x) | 返回 x 的反双曲余弦值 |\n",
    "| 2 | asinh(x) | 返回 x 的反双曲正弦值 |\n",
    "| 3 | atanh(x) | 返回 x 的反双曲正切值 |\n",
    "| 4 | cosh(x) | 返回 x 的双曲余弦值 |\n",
    "| 5 | sinh(x) | 返回 x 的双曲正弦值 |\n",
    "| 6 | tanh(x) | 返回 x 的双曲正切值 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2个角度转换函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | degrees(x) | 将角度 x 从弧度转换为度数 |\n",
    "| 2 | radians(x) | 将角度 x 从度数转换为弧度 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4个特殊函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | erf(x) | 返回 x 处的 error function |\n",
    "| 2 | erfc(x) | 返回 x 处的互补误差函数 |\n",
    "| 3 | gamma(x) | 返回 x 处的 伽马函数  |\n",
    "| 4 | lgamma(x) | 返回Gamma函数在 x 绝对值的自然对数 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5个数学常量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 序号 | 函数或常量 | 描述与示例 |\n",
    "|  :---:  | :--- | :---- |\n",
    "| 1 | pi | 数学常数 π = 3.141592653589793,精确到可用精度 |\n",
    "| 2 | e | 数学常数 e = 2.718281828459045,精确到可用精度 |\n",
    "| 3 | tau | 数学常数 τ = 6.283185307179586,精确到可用精度。Tau 是一个圆周常数,等于 2π,圆的周长与半径之比。 |\n",
    "| 4 | inf | 浮点正无穷大 |\n",
    "| 5 | nan | 一个浮点的 \"非数字\"(NaN)值。使用 isnan() 函数来测试 NaN,而不是 is 或 == |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "print(math.pi)    # 3.141592653589793\n",
    "print(math.e)     # 2.718281828459045\n",
    "print(math.tau)   # 6.283185307179586"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这些函数一般都是对C语言库中同名函数进行简单封装,仅支持整数和浮点数,<font color=Red>__不支持复数运算__</font>。  \n",
    "如果需要复数支持,可以使用cmath模块。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面对其中较常用的函数做简单说明,其他情况可参考官方文档https://docs.python.org/zh-cn/3/library/math.html 。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 数学常量\n",
    "###### math.pi:返回圆周率常数π值\n",
    "###### math.e:返回自然常数e值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "print(math.pi)\n",
    "print(math.e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 数论与表示函数\n",
    "\n",
    "###### math.fabs(x):以浮点数形式返回x的绝对值。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "print(math.fabs(-6))  # 返回浮点型的绝对值,输出6.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.factorial(x):返回x的阶乘,要求x为非负整数,x为负数或浮点数时返回错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.factorial(6))  # 返回6!,输出720"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.factorial(-6))  # 参数为负数,抛异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.factorial(6.5))  # 参数为浮点,抛异常"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.fsum(iterable)):返回浮点数迭代求和的精确值,避免多次求和导致精度损失。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(sum([.1, .1,.1, .1, .1, .1, .1, .1, .1, .1]))     \n",
    "print(math.fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.gcd(\\*integers):返回给定的整数参数的最大公约数。\n",
    "如果参数之一非零,则返回值将是能同时整除所有参数的最大正整数。<br>如果所有参数为零或无参数,则返回值为 0。<br>在 3.9 版后添加了对任意数量的参数的支持,3.8版本或之前只支持两个参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.gcd(88, 44))      # 返回2个值的最大公约数,输出44\n",
    "print(math.gcd(88, 44, 22))  # 返回3个值的最大公约数(3.9新增),输出22\n",
    "print(math.gcd(88, 44, 0))   # 返回值是能同时整除所有参数的最大正整数,输出44\n",
    "print(math.gcd(0, 0))        # 如果所有参数为零或无参数,则返回值为0\n",
    "print(math.gcd())  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "######  math.lcm(\\*integers):返回给定的整数参数的最小公倍数。(3.9版新增函数)\n",
    "如果所有参数均非零,则返回值将是为所有参数的整数倍的最小正整数。<br>如果参数之一为零,则返回值为 0。<br>不带参数的 lcm() 返回 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.lcm(5, 44, 22))  # 返回3个值的最小公倍数,输出220\n",
    "print(math.lcm(22, 44, 0))  # 如果参数之一为0,返回值为0\n",
    "print(math.lcm(0, 0))\n",
    "print(math.lcm())           # 无参数时,返回值为1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.prod(iterable, \\*, start=1):计算输入的可迭代对象 iterable 中所有元素的积。\n",
    "积的默认起始值start为 1。<br>当可迭代对象为空时,返回起始值。<br>此函数特别针对数字值使用,并会拒绝非数字类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.prod([1,2,3,4,5]))            # 1*2*3*4*5,输出120\n",
    "print(math.prod([1,2,3,4,5], start=2))   # 2*1*2*3*4*5,输出240\n",
    "print(math.prod([]))\n",
    "print(math.prod([], start=2))            #当可迭代对象为空时,返回起始值start(默认为1)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.floor(x):返回不大于x的最大整数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.floor(9.99))    # 输出9\n",
    "print(math.floor(-9.01))   # 输出-10\n",
    "print(math.floor(9))       # 输出9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.ceil(x):返回不小于x的最小整数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.ceil(9.01))    # 输出10\n",
    "print(math.ceil(-9.99))   # 输出-9\n",
    "print(math.ceil(9))       # 输出9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.isqrt(n):返回非负整数 n 的整数平方根。\n",
    "即对 n 的实际平方根向下取整,或者相当于使得   n 的最大整数 a。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.isqrt(99))            # 输出 99的整数平方根 9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "某些情况下,对于正数 n,需求使得 n   的最小整数 a ,或者换句话说就是 n 的实际平方根向上取整,可使用下面的方法实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(1 + math.isqrt(99 - 1))    # 输出不小于99的平方根的最小整数10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 幂和对数函数\n",
    "\n",
    "###### math.log(x[, base]):返回x以base为底的对数。\n",
    "\n",
    "使用一个参数,返回 x 的自然对数(底base为 e )。\n",
    "\n",
    "使用两个参数,返回给定的 base 为底的对数 x ,计算为 log(x)/log(base) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.log(10))      # 返回10的自然对数\n",
    "print(math.log(10, 2))  # 返回10以2为底对数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.log2(x):返回以2为底的x的对数。\n",
    "其值通常比log(x, 2)值更精确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.log2(50))    # 返回50以2为底对数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.log10(x):返回以10为底的x的对数。\n",
    "其值通常比log(x, 10)值更精确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.log10(50))    # 返回50以10为底对数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.exp(x):返回 $e^x$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.exp(2))               # 输出 e 的平方,7.38905609893065"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.pow(x, y):返回x的y次幂\n",
    "结果为浮点数,pow(1.0, x)  pow(x, 0.0) 总返回1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "1.0\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "print(pow(2, 3))             # 输出 2  3 次方 8.0\n",
    "print(math.pow(1.0, 5))           # 输出 1.0\n",
    "print(math.pow(5, 0))             # 输出 1.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.sqrt(x):返回x的平方根\n",
    "结果为浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.sqrt(100))   # 输出 100的正数平方根 10.0\n",
    "print(math.sqrt(2))     # 输出 2的正数平方根 1.4142135623730951"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 三角函数\n",
    "\n",
    "###### math.sin(x):返回弧度x的正弦值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.sin(math.pi / 6))   \n",
    "print(math.sin(math.pi / 3))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.cos(x):返回弧度x的余弦值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.cos(math.pi / 6))   \n",
    "print(math.cos(math.pi / 3))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.tan(x):返回弧度x的正切值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.tan(math.pi / 4))   \n",
    "print(math.tan(0))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.asin(x):返回以弧度为单位的 x 的反正弦值。 \n",
    "结果范围在 $-\\frac{\\pi}{2}$  $\\frac{\\pi}{2}$ 之间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.asin(0.5))   \n",
    "print(math.asin(1)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.acos(x):返回以弧度为单位的 x 的反余弦值。 \n",
    "结果范围在0到$\\pi$之间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.acos(0.5))   \n",
    "print(math.acos(0)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.atan(x):返回以弧度为单位的 x 的反正切值。 \n",
    "结果范围在 $-\\frac{\\pi}{2}$  $\\frac{\\pi}{2}$ 之间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.atan(1))   \n",
    "print(math.atan(0)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.hypot(*coordinates) 返回欧几里得范数。\n",
    "即从原点到坐标给定点的向量长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.hypot(3, 4))          # 计算点(3, 4)到原点(0.0)的距离,输出 5.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ ### 角度转换函数\n",
    "\n",
    "###### math.degrees(x):弧度值转角度值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.degrees(math.pi / 4))  # 输出 45.0\n",
    "print(math.degrees(math.pi))      # 输出 180.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### math.radians(x):角度值转弧度值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(math.radians(45))    # 输出约为pi/4\n",
    "print(math.radians(180))   # 输出约为pi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实例:梅钦法计算圆周率\n",
    "\n",
    "梅钦公式是格里高利/莱布尼茨计算的公式的变体,它的收敛速度很快,至今仍然是计算圆周率的主要公式,请根据此公式计算圆周率值。\n",
    "\n",
    "$$\\frac{\\pi}{4}=4\\arctan \\frac{1}{5}-\\arctan \\frac{1}{239}  $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 提示:math库中反正切函数为<font color=Red>__atan(x)__</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "quarter_of_pi = 4 * math.atan(1 / 5) - math.atan(1 / 239)\n",
    "pi = 4 * quarter_of_pi\n",
    "print(pi)  # 3.1415926535897936"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}