新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

《新大榭》- 创大榭地方网络社区先锋品牌 新大榭始终专注于地方网络社区平台的建设 关于我们- [大记事]- 留言建议- [新手报道]

发布 .新大榭软件管家(Excel版) V6.0版 财务/仓库/生产/销售/采购/行政/人事/校园 .公告 - 客户 - 打赏 - 职场 - Excel - Python.

新大榭镜像-音乐-法律-图书-高中课堂-实验 广告是为了能更好的发展 [欢迎商家支持本站互利共赢] 广告位招租.首页黄金广告位等您来!联系 13566035181

新大榭论坛 门户 查看主题

7442 - Python库 AP085【math】数学模块常用方法

发布者: admin | 发布时间: 2021-7-24 10:21| 查看数: 1906| 评论数: 0|帖子模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!

您需要 登录 才可以下载或查看,没有账号?注册

x
5 ]0 Q7 w: J  n9 ?1 k
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. R# L3 S9 Q" R& j% u

) w. }6 G7 |5 S7 g! @" s  K1 B, F6 Z方法17 ?! A. E( h; h; M* a5 Z
  1. >>> import math) B" @  U5 U+ X8 Q8 n# T& W% O# A
  2. >>> math.sqrt(9)  {% z1 i0 W) |, \( [
  3. 3.0
复制代码
方法2; K! l3 _- T8 |' ^: i2 r" o: ?
  1. >>> from math import sqrt; s2 n9 r0 g# j+ M5 v
  2. >>> sqrt(9)$ p( D3 c4 T4 J# s# G1 L8 X. U
  3. 3.0
复制代码

$ H! n( X8 H4 E5 [* ?) E! J

" a. y. n; H) A0 W) f& F
math.e  表示一个常量' R- Y9 S$ ?* u' i0 H
  1. #表示一个常量) G' y: ~7 A8 a. e" G/ ]3 d6 a
  2. >>> math.e0 ?0 `& Z3 F8 A% J
  3. 2.718281828459045
复制代码
% X' r6 T4 T! W3 t4 D8 R8 ]
math.pi  
数字常量,圆周率
- C$ d' ]+ q5 ^
  1. #数字常量,圆周率! O4 @0 Z2 a' K% F7 O2 t9 d5 ?
  2. >>> print(math.pi)- e4 V$ S3 P& ^2 Y  ?3 R0 \" X  ^
  3. 3.141592653589793
复制代码

; j( D; O: ~; `/ xmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

- R( f$ B9 i$ k# x" N
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ) ]. Y3 i: `% p5 x: R
  2. ceil(x)
    " f5 W$ }/ i) ^$ x: e* p" y8 m
  3. Return the ceiling of x as an int.: Y1 p( ]4 z* ?% B
  4. This is the smallest integral value >= x.$ q/ J3 N1 L! x) q* A$ b+ j

  5. 9 Q/ D6 P( m! v6 c$ V
  6. >>> math.ceil(4.01)9 A' E7 v$ j0 m' }  m) u) ^* w
  7. 5
    7 l9 k4 V) Y  k. z
  8. >>> math.ceil(4.99)
    & v4 `' o$ p9 |/ I! [
  9. 5
    9 m  d8 A4 z; |3 }2 F" s: K& m" R
  10. >>> math.ceil(-3.99)
      }' t) n& F, T  H; Z. i# g
  11. -3
    4 m! e7 M( C% i: ^! O
  12. >>> math.ceil(-3.01)
    # G& ~, X& o% R) v  W
  13. -3
复制代码
- T) {- m$ q4 d: @3 s
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 p7 u0 Z  |3 A1 b9 f7 w$ t
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* c; [5 O# Y  Q! G; ]9 l4 D
  2. floor(x)
    ( j0 ?- T- q/ d  I* p
  3. Return the floor of x as an int.
    4 M6 N! O0 T6 S
  4. This is the largest integral value <= x.
    - {0 a" o& v# j5 J2 v% a3 K# F' m
  5. >>> math.floor(4.1)+ c0 c0 Z. ^1 V% G
  6. 4) }  {3 ~* B+ S/ [% {: m$ h- C
  7. >>> math.floor(4.999)
    $ _, T* C' F) t
  8. 4
    ( {5 Z& N+ f' E+ L
  9. >>> math.floor(-4.999); V7 A0 v: F! Z
  10. -5- R) z4 \1 Y; y" t: k
  11. >>> math.floor(-4.01)
    0 ], q  t% T6 S( e* e4 F
  12. -5
复制代码
$ x* s$ l& }8 V( X* k
math.pow(x,y)  返回x的y次方,即x**y0 T! X6 ?2 `" R. r  U( B* i. h5 p- @$ R+ i
  1. #返回x的y次方,即x**y! L* Z) D* X9 n" ?' J5 _, b! @6 y
  2. pow(x, y)
    0 V5 l$ I, ]) f! h( D: ^6 L0 f
  3. Return x**y (x to the power of y).
    ; O/ g. i1 h8 \( k; ?
  4. >>> math.pow(3,4)+ m. r4 d; C" Y0 ~% s
  5. 81.0
    : D0 Q( a! L( y+ H& b
  6. >>>
    9 o: y8 F: ^- q; w
  7. >>> math.pow(2,7)' T$ U% t! H* B, f7 W% K1 \. P+ N: R
  8. 128.0
复制代码
' K( q+ R! h3 ~! n. X
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  w% i- g, _# ^% C7 h* D
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    , n+ I) H3 P( `( W+ t! l3 X
  2. log(x[, base])' a+ n- i9 \1 s9 ?9 @$ Y- T# K
  3. Return the logarithm of x to the given base.3 C) g: ]2 |+ b
  4. If the base not specified, returns the natural logarithm (base e) of x.
    - c+ \5 |2 U- i
  5. >>> math.log(10)
    / e0 r, X: b1 m- T" x& J* J
  6. 2.302585092994046
    6 U$ z) d9 b2 E
  7. >>> math.log(11)3 V/ D" M. y+ Z/ r; ?! m0 h& }
  8. 2.3978952727983707! |. c4 Z; U5 L: d1 X, n( H6 q- E
  9. >>> math.log(20)* s8 q: `0 A9 s. J# T8 @
  10. 2.995732273553991
复制代码

3 Z6 p4 w" r) a9 N9 N& p' X8 t. Wmath.sin(x)  求x(x为弧度)的正弦值
  h' x- T4 s3 ]8 F! I8 W! }
  1. #求x(x为弧度)的正弦值% O4 t. A! a( |- a- A. B
  2. sin(x); F! d) J, I- v1 T3 N, S" j
  3. Return the sine of x (measured in radians).
    * P5 r& W1 P# m. T+ ]
  4. >>> math.sin(math.pi/4)' \( W5 o8 p* I  l2 C' q$ ^
  5. 0.7071067811865475
    ; ^) k+ p4 p% Q
  6. >>> math.sin(math.pi/2)
    * @% |# v( b; G) Z5 j8 ?6 S) r
  7. 1.0
    / S& }- ]' {0 p4 j( a
  8. >>> math.sin(math.pi/3)' L/ q0 X% I; n- y. p; ?& @! w1 a
  9. 0.8660254037844386
复制代码
2 H% Y" Y. Q" @
math.cos(x)  求x的余弦,x必须是弧度
; Q/ y% B. v5 b, t- b
  1. #求x的余弦,x必须是弧度
    , y3 N9 s+ `/ D
  2. cos(x)  s! P2 d: I) x" |5 T# V) V6 s& T
  3. Return the cosine of x (measured in radians).! @( Q6 V9 k$ f1 G5 ~) P8 C
  4. #math.pi/4表示弧度,转换成角度为45度
    6 @) ?# d/ L4 m; N3 ^0 d
  5. >>> math.cos(math.pi/4)
    % F& Y" }' Z; o+ q- ?2 `
  6. 0.7071067811865476
    % Q# p  O: U, t/ S
  7. math.pi/3表示弧度,转换成角度为60度: M0 O9 k2 E0 q+ _6 F6 s2 m
  8. >>> math.cos(math.pi/3)
    3 R" E& Q+ G+ t5 S
  9. 0.50000000000000019 F6 p, e  W" Q  |1 W' k
  10. math.pi/6表示弧度,转换成角度为30度
    ; N, I8 J3 m+ ]7 O4 ^
  11. >>> math.cos(math.pi/6)4 b% X( M; w8 P& q, d
  12. 0.8660254037844387
复制代码

( |- W* Z6 @7 N0 U. ~" L2 m( Jmath.tan(x)  返回x(x为弧度)的正切值
% z" B" b6 V- a+ V
  1. #返回x(x为弧度)的正切值
    , v% l4 A$ l9 }3 @
  2. tan(x)! f% \' L0 d: A9 S+ h& M8 A
  3. Return the tangent of x (measured in radians).4 M0 y1 ]7 F* \% q, x: V
  4. >>> math.tan(math.pi/4)7 n9 y: D5 L$ Z# Y
  5. 0.9999999999999999
    + {$ m) _. w9 Z. b% [& ]5 u
  6. >>> math.tan(math.pi/6)
    . D2 ]; x. U- s4 S$ w2 a3 ^
  7. 0.5773502691896257! N1 m6 h% ]# I( Z6 H
  8. >>> math.tan(math.pi/3)
    ' H9 p2 \# X% a: `
  9. 1.7320508075688767
复制代码

( \' D& g( p+ P$ x' e6 Xmath.degrees(x)  把x从弧度转换成角度# G3 b% M! ?; J" m1 c% O) T! k
  1. #把x从弧度转换成角度4 H. h! q2 w( m0 l8 |
  2. degrees(x)
    / s$ ~  n! @+ ?" n" u4 h
  3. Convert angle x from radians to degrees.5 v" D! D! j+ o" }8 V& r% {2 Q
  4. # _  t1 q5 i  F2 \1 J+ N) C
  5. >>> math.degrees(math.pi/4)6 J1 ^2 X3 a' X2 L" @. K# k
  6. 45.0
    + y" q5 V% C9 V, u: ?) a
  7. >>> math.degrees(math.pi)
      w( }' u/ O, \. U7 M' u4 B. x
  8. 180.0
    # I! h# y. r$ i0 n5 D
  9. >>> math.degrees(math.pi/6)
    . ?  k( k7 P4 i4 Y
  10. 29.9999999999999968 P3 w# g6 S. F/ g4 ?6 G$ r
  11. >>> math.degrees(math.pi/3)
    1 n: b  V5 [7 X3 \# I% q& F. v1 g* y+ ^
  12. 59.99999999999999
复制代码

* n. i5 u) N/ a" @$ d& Gmath.radians(x)  把角度x转换成弧度: e4 f' C# P/ G/ u" k; T' a$ I5 I/ _
  1. #把角度x转换成弧度$ B2 p6 ]1 H* @* ?* S4 v, |; y; T. P
  2. radians(x)
    8 u- a$ [+ o# u6 m
  3. Convert angle x from degrees to radians.
    ; |8 R/ o9 s0 b0 D0 t
  4. >>> math.radians(45)$ n9 n# b9 X8 m' W* ]% z
  5. 0.7853981633974483
    " F. ^/ {& N9 w* W
  6. >>> math.radians(60)7 b+ k$ Y2 U; d2 b2 c6 \' m. j+ k# y% |
  7. 1.0471975511965976
复制代码

: Z8 b8 ^4 _; p% kmath.copysign(x,y)  把y的正负号加到x前面,可以使用0: F9 v0 k) C0 j
  1. #把y的正负号加到x前面,可以使用0# q- b" J3 I8 X8 @
  2. copysign(x, y)
    ! q7 P3 t5 l& ~% e
  3. Return a float with the magnitude (absolute value) of x but the sign 8 B- ?, @- h& E6 Z& X
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) - O, d9 o6 J( h5 _) a. }
  5. returns -1.0.3 Z" R. o! ]) h: D* ?  j7 g+ d
  6. 5 R, [9 K# U2 R7 _$ P2 u8 ]% f
  7. >>> math.copysign(2,3)
    6 ?1 V; R' h  D
  8. 2.0
    2 r& C- \5 E# O- Y2 d5 k* \" l
  9. >>> math.copysign(2,-3)
    . ]8 }3 A' A: D5 M
  10. -2.06 n& i7 u" U8 j- V# G0 }
  11. >>> math.copysign(3,8)) ~: a  i% r: a+ M; K2 F
  12. 3.09 o$ M+ O6 G# k' u8 b
  13. >>> math.copysign(3,-8)- t+ E- {+ t5 T# q( _  E  P
  14. -3.0
复制代码

2 R  g, W/ A- a' ~math.exp(x)  返回math.e,也就是2.71828的x次方
0 o' \# D2 ?6 x1 _/ O9 M
  1. #返回math.e,也就是2.71828的x次方; `8 Z9 B# o; ]/ s6 |
  2. exp(x)
    : K1 P% L1 }& @, e2 q
  3. Return e raised to the power of x.
    0 n/ F! C9 G; U4 V* v1 W( C+ a$ o
  4. 0 F# B4 x4 t. g% z
  5. >>> math.exp(1)2 K: l& f! u7 a2 a# `# a: t: q3 b
  6. 2.7182818284590450 |; [' w4 m" C6 s( e9 {; T, D2 A, d
  7. >>> math.exp(2)
    $ [( c" _8 ?/ `. N
  8. 7.38905609893065$ ]# c2 ^% ~& X6 ]& a
  9. >>> math.exp(3)+ r( O% k* B& w2 S+ E
  10. 20.085536923187668
复制代码
& A2 Y: a  `1 Q2 E2 u* \4 W0 q$ s
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
4 S1 n( ?( o  x; X! [
  1. #返回math.e的x(其值为2.71828)次方的值减1: `; Y+ V7 L0 P, l$ _, d
  2. expm1(x)1 S* ]$ j9 L5 ]- P
  3. Return exp(x)-1.( f9 U$ k# ^8 U; O2 u) F2 H
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.3 d, ]% I' b) t; V6 f8 ?

  5. , d# N0 Q8 s7 f- N, Q
  6. >>> math.expm1(1)
    0 ]6 I+ o6 ~5 ^+ r7 c" k2 r
  7. 1.718281828459045# v. p+ ^0 f6 K9 T0 R4 D
  8. >>> math.expm1(2)
    ( Z) x  m  L5 _8 a1 g# I/ ?7 q
  9. 6.38905609893065
      @9 ^6 ~" p* f
  10. >>> math.expm1(3)
    ! G5 [) l5 ]: p
  11. 19.085536923187668
复制代码

8 i4 _; [4 s- O9 I( \7 |math.fabs(x)  返回x的绝对值) B6 ]  }2 @' l* ~: p7 i8 ?
  1. #返回x的绝对值
    & m& `3 ^1 ]" L3 w, p
  2. fabs(x)" {: D& N, _+ }! M% K# |
  3. Return the absolute value of the float x.8 v/ |4 ~4 O$ ?* s' s. t
  4. 0 U* p+ g% |# K# h- h9 V
  5. >>> math.fabs(-0.003): L0 p% K" N' y8 s* Y
  6. 0.003, j$ k# U1 \: F$ o4 u! Z, q
  7. >>> math.fabs(-110)
    ' b" `, X4 H: O0 W% k# o8 B+ f! F
  8. 110.0
      s$ c5 s, }; N% M9 b5 ~: k
  9. >>> math.fabs(100)- R5 I" |/ \) q; d7 w
  10. 100.0
复制代码

7 N0 l/ {7 D+ m6 z) `math.factorial(x)  取x的阶乘的值, x# n/ s7 f- ^8 H" j- y' }
  1. #取x的阶乘的值4 b$ |4 y! V& W" ^3 l1 p
  2. factorial(x) -> Integral
    ) h7 N) k. U" i( m; d; _, t
  3. Find x!. Raise a ValueError if x is negative or non-integral.3 u7 I- U  G1 Z/ a% v4 p* T
  4. >>> math.factorial(1)$ C; U  @( L8 [& c. _! }
  5. 17 p$ A% C: i/ Y$ B( c
  6. >>> math.factorial(2)
    ; U3 k4 e& ~1 l% l# o4 }0 X
  7. 2
    8 L0 A4 V0 L: y( x4 `- j: l8 }
  8. >>> math.factorial(3)# g) J; I8 C: ?# O, `. s  c" T5 |; B
  9. 6, T3 R" |- u2 a; T
  10. >>> math.factorial(5)2 `. j8 |, C3 r' L) D: f" `1 y
  11. 120$ M# w4 a4 d. i& D) U/ c2 f
  12. >>> math.factorial(10)
    : ^+ B* ?% }0 O/ A1 z1 n0 E- i
  13. 3628800
复制代码
) b8 y7 W8 D- j. F2 g% b2 q" `
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数1 h2 T) F$ o) _
  1. #得到x/y的余数,其值是一个浮点数0 |. i5 O/ B, H
  2. fmod(x, y)
    % g% M, z9 ^  s2 D
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ; W: C; }& \& ^! A4 o8 T" R7 @9 x
  4. >>> math.fmod(20,3)
    # i& W( u& p: V7 f" [+ B( m7 h
  5. 2.0
    5 d+ k- F+ k  d( ]
  6. >>> math.fmod(20,7)! l( |' W' m- O% D! F
  7. 6.0
复制代码
# J, Z1 x% F' r! t& M4 E- X
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: \, B( M$ ?  {0 V
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 R! u  ]# ^' k
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 E0 @  r  B" C+ N$ x3 z; G' L
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    - e- P) v; c" ?% X2 b3 r1 e; j
  4. frexp(x)! e& D' b, y+ B* R- X2 `
  5. Return the mantissa and exponent of x, as pair (m, e).
    7 _, O; X1 h% a1 r' T* }% c, X
  6. m is a float and e is an int, such that x = m * 2.**e.
    4 U; G, D0 [4 ]& t
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    + Y6 z: P( J7 x- p5 t1 N4 y9 I
  8. >>> math.frexp(10)
    8 W& l# o2 b7 L, |1 j! C
  9. (0.625, 4)* x8 u+ g9 X% P8 ^- P7 [1 Z
  10. >>> math.frexp(75)$ @* F4 v5 b) w7 I
  11. (0.5859375, 7); I/ D, [: {3 @
  12. >>> math.frexp(-40)
    1 L! ~: {1 I! }% E; I3 q, @! D& W7 G
  13. (-0.625, 6)
    , n' x/ T- p( \! O2 }
  14. >>> math.frexp(-100)
    ; O9 o. e7 _8 I" q$ H$ w
  15. (-0.78125, 7)
    7 E) I0 N; |, I  H4 q1 ^+ B9 `0 q
  16. >>> math.frexp(100)
    6 B$ t! q, Y9 u- [4 _
  17. (0.78125, 7)
复制代码
7 {( Z/ [1 \  o4 D, E* g: Z
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列3 d1 s& p% X+ j; u
  1. #对迭代器里的每个元素进行求和操作
    ' C7 p1 Q) w) S; c1 i; @! T
  2. fsum(iterable)6 ^3 \; Y  i! b1 K5 [# \; q
  3. Return an accurate floating point sum of values in the iterable.
    5 t+ h: S: }# g3 f& }) ~0 N
  4. Assumes IEEE-754 floating point arithmetic.
    2 F7 o* g5 r& l) U# e
  5. >>> math.fsum([1,2,3,4])' S0 ]2 |5 A- ]7 E4 t
  6. 10.0
    % O* G( s+ ?* Q  z
  7. >>> math.fsum((1,2,3,4))* d0 t5 p7 i3 }% n, y1 t1 f- Y
  8. 10.0& H& M) W* E5 p
  9. >>> math.fsum((-1,-2,-3,-4))' d2 z* B; g. ^- K/ K3 _
  10. -10.01 ]1 I' J6 o& y5 p, D' {1 ^1 ]2 A
  11. >>> math.fsum([-1,-2,-3,-4])* S' ?& }( p7 ?% J% B
  12. -10.0
复制代码

' y' w, Y( _+ d; `4 T4 A; B! b  dmath.gcd(x,y)  返回x和y的最大公约数
3 a9 I7 `% Q/ A, T) f
  1. #返回x和y的最大公约数; ]3 @: g" S) `, K9 [9 Q) z
  2. gcd(x, y) -> int
    7 S# A1 s; i& R6 p6 }) p9 F
  3. greatest common divisor of x and y5 p; q; v9 b. m; I) _' \' C: e
  4. >>> math.gcd(8,6)
    # m7 \; Q8 ^* \4 G+ _; E- H) b
  5. 25 s) K4 F) l) U. J
  6. >>> math.gcd(40,20)- R# q& w2 }- e! G7 `8 R
  7. 20
    2 j$ l, ?5 M7 X  X# M" P
  8. >>> math.gcd(8,12)
    " c; i/ Q, F, `; a
  9. 4
复制代码
3 Y* g4 b5 v% u* V1 @% X( _
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False: l4 [; P- w5 [. e$ v  O
  1. #得到(x**2+y**2),平方的值
    2 r5 Z7 e& m- `8 [( v
  2. hypot(x, y)
    , a8 i4 B* X3 K8 e5 ?
  3. Return the Euclidean distance, sqrt(x*x + y*y).' R+ B6 A8 U% ~# \0 W% H: K
  4. >>> math.hypot(3,4)
    ; w$ L" v3 W% j
  5. 5.0
    $ n8 J: T3 I7 d  U0 J8 N
  6. >>> math.hypot(6,8)% i- N- J) c# l7 p6 |4 D2 q
  7. 10.0
复制代码
  [2 t9 ?! \8 J9 ^* J7 }7 _
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
* n; X3 p$ a- [+ U& Y3 ?' E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    - ~& V5 b. E# b7 j) t' V
  2. isfinite(x) -> bool
    ' c: x5 L" a4 j  r1 |: D$ P4 {
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    : \* h4 O7 g; Z( U2 [/ j/ A
  4. >>> math.isfinite(100)( F# T0 y  O# h
  5. True- O  Z% W/ _7 x+ H: E3 ?4 V2 L
  6. >>> math.isfinite(0)" _" O1 K" |. p3 _/ d& t( m
  7. True
    1 s  b& E' ?- x  q$ d! o; s1 y
  8. >>> math.isfinite(0.1)
    9 B- E7 V2 J) T1 y) n5 S# E
  9. True. T/ I- p7 u& D% q# s  y. D* M
  10. >>> math.isfinite("a")
    - B5 x9 Q. N+ y+ G( w& ?
  11. >>> math.isfinite(0.0001)
    ( g( y: q/ ?3 r, A
  12. True
复制代码
% n; N, T$ b2 h; k) q- G4 r0 `0 _6 L
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
  F1 L. \9 L" H* G6 A' m
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False, p' b1 U+ B2 P* J
  2. isinf(x) -> bool( I# E: [: C! s& Q
  3. Return True if x is a positive or negative infinity, and False otherwise.. B0 Z" `2 L( n! I% H7 W# ]" o
  4. >>> math.isinf(234)
    4 R+ K  k" R# G/ B
  5. False* z) v, d" O5 E3 B% W$ L
  6. >>> math.isinf(0.1)
    : [: P( u7 F" u# K/ v: v
  7. False
复制代码

5 n; y/ n; k2 d6 S+ G: Q$ G1 h4 Vmath.isnan(x)  如果x不是数字True,否则返回False' z, F+ v" r* Y9 `( f
  1. #如果x不是数字True,否则返回False* u5 t5 c5 {. s/ I0 a3 r
  2. isnan(x) -> bool
    ( L' D) J. q, M8 T4 l) b
  3. Return True if x is a NaN (not a number), and False otherwise.
    8 L; G. H3 B% i5 S! y9 k
  4. >>> math.isnan(23)
    9 K/ h/ y/ v3 O0 U
  5. False' c* S5 N7 O/ R$ Q) m8 _( `
  6. >>> math.isnan(0.01)' q# x1 E  g- p: T; z5 t
  7. False
复制代码
# r; z- {  A+ A: L$ v" u
math.ldexp(x,i)  返回x*(2**i)的值
6 U- }# j! o9 [1 w+ |$ M
  1. #返回x*(2**i)的值* ?- v' J9 E/ X0 i' Q
  2. ldexp(x, i)
    2 d# n6 a& I6 e) K2 b
  3. Return x * (2**i).
    . K/ k  |8 M. ]9 i# N/ j
  4. >>> math.ldexp(5,5)
    - j2 G: f5 D0 U! K" P
  5. 160.0. B$ O+ d8 L, ~
  6. >>> math.ldexp(3,5)6 U6 @0 K8 ?" H1 }  g
  7. 96.0
复制代码

' [: q) k$ l9 t5 L' s- N: rmath.log10(x)  返回x的以10为底的对数% A2 n4 B' ]) s6 D! I  X
  1. #返回x的以10为底的对数
    4 E+ t& _% t8 S; V
  2. log10(x)
    2 @6 T& I8 B  p0 P0 O/ z: U
  3. Return the base 10 logarithm of x.! v9 O$ Y, x4 x5 }) c+ N6 ~
  4. >>> math.log10(10)
    % G6 `8 X2 F1 K$ ~
  5. 1.0
    . T. n& L7 n9 Y1 q
  6. >>> math.log10(100)
    ) \' A& c4 b" a  _9 C* s  X
  7. 2.0
    : r8 X8 T; |; ]2 H( f, [- z8 X
  8. #即10的1.3次方的结果为209 @+ [& p# m7 O0 ^3 E
  9. >>> math.log10(20)- m1 W0 n- g2 L# Q
  10. 1.3010299956639813
复制代码

  E# W& D) N- C" Nmath.log1p(x)  返回x+1的自然对数(基数为e)的值4 V; a' X& p% f
  1. #返回x+1的自然对数(基数为e)的值
    , M* G* }, O, L
  2. log1p(x)+ X5 [# t" _/ _! _2 i8 B4 k1 m
  3. Return the natural logarithm of 1+x (base e).
    ! ~8 j! A$ F7 {$ j! \8 e7 H
  4. The result is computed in a way which is accurate for x near zero.
    ) ~& L8 I! ?7 H$ A- g  M
  5. >>> math.log(10)( Y& w2 ]; b: w0 y6 r& w
  6. 2.302585092994046
    $ z2 A* I: B* e5 F7 Z5 d; @$ u: D
  7. >>> math.log1p(10)
    7 b* X, f. Y+ Y  |
  8. 2.3978952727983707- k5 ^& i5 m; x& t
  9. >>> math.log(11)
    # p! F) p) w% }
  10. 2.3978952727983707
复制代码

9 M8 Q$ }* c# hmath.log2(x)  返回x的基2对数
5 ]/ P4 B& x3 T6 f& Y
  1. #返回x的基2对数
    $ t  [# A! }1 Q& j. o
  2. log2(x)
    0 K1 d9 }4 P# O
  3. Return the base 2 logarithm of x.
    ; a0 f% R7 j; `7 \1 |
  4. >>> math.log2(32)
    1 Y0 I" r6 B2 Q2 E5 p# R. b) D
  5. 5.0
    : L' ^: A/ @: s& |) B4 X
  6. >>> math.log2(20)+ \6 k3 y. a- u1 v3 h0 p5 D5 F
  7. 4.321928094887363/ V: G1 S4 T3 x5 W" a0 S+ n
  8. >>> math.log2(16)$ I: C/ m/ v$ R+ R" W  y
  9. 4.0
复制代码
' f- M2 M* ^2 [' D  r- \, u" b% `
math.modf(x)  返回由x的小数部分和整数部分组成的元组5 Z  i! |6 u. i8 q3 }, {+ \6 i
  1. #返回由x的小数部分和整数部分组成的元组% j5 t5 @0 [& {3 F) _
  2. modf(x)
    6 P4 U6 L8 k5 U1 @7 `
  3. Return the fractional and integer parts of x.  Both results carry the sign
    : G  Q. l% m! a# l9 d3 Y3 z
  4. of x and are floats.; r) g/ m) i1 Y, s$ x3 q+ n
  5. >>> math.modf(math.pi)9 l8 x! Q5 c1 C- p/ J
  6. (0.14159265358979312, 3.0)2 T5 M7 Z( d$ y( k5 v
  7. >>> math.modf(12.34): M3 |. E  P& T$ w  l6 }# S& U4 r
  8. (0.33999999999999986, 12.0)
复制代码
! _9 f. h3 ^, b, X, O6 C) `
math.sqrt(x)  求x的平方根
5 U* n) j/ a, T( b' J
  1. #求x的平方根
    + e% R2 h7 I9 [
  2. sqrt(x)- b/ ^- ^+ p6 \! c! A& S. E* j
  3. Return the square root of x.0 M- p9 g: Y4 x' u
  4. >>> math.sqrt(100)/ |9 q  f: Y7 V3 g1 V; c; U
  5. 10.0
    6 }8 s( Z7 r$ }$ E, X
  6. >>> math.sqrt(16)9 _$ O2 T( a6 s5 F  O& p
  7. 4.0* J1 U3 y, r( p, w" O% c7 r6 G* P
  8. >>> math.sqrt(20)
    . O& p# b. Z' f4 e
  9. 4.47213595499958
复制代码
1 d" g" h) {/ g1 l2 |7 I) ]9 a
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    * u( v& i  Y, F! q% f. H* ?* K! r
  2. trunc(x:Real) -> Integral
    & K. X# C  n( I
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    # i8 H: y( E$ I7 \% a3 i: |
  4. >>> math.trunc(6.789)* K3 U2 z7 a; p3 {' ?- K+ y/ N
  5. 6
    0 z+ T* C: T* E# ?5 c! M% c# _
  6. >>> math.trunc(math.pi)
    / D1 t8 K9 |$ }$ ^0 m4 x
  7. 31 x  R2 n' e5 Z2 ]% r9 {
  8. >>> math.trunc(2.567)# j% Y! |3 E  O1 N& u' E
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

文字版|小黑屋|新大榭 ( 浙ICP备16018253号-1 )|点击这里给站长发消息|

GMT+8, 2026-2-12 13:30 , Processed in 0.084942 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表