新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1835|回复: 0

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

 关闭 [复制链接]
发表于 2021-7-24 10:21:24 | 显示全部楼层 |阅读模式

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

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

x
( t" J+ Z0 z7 H% v: Z. G2 @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
2 A- y2 C; b1 E

$ W/ }. ?" p# x8 L: x& z, W+ L( ]方法1
! m# Q4 t# E2 l; |( l
  1. >>> import math# Z- G) `# @8 I! C$ U* ^
  2. >>> math.sqrt(9)
    2 Y6 z2 q4 P9 e6 J
  3. 3.0
复制代码
方法2" L: m( d' v$ w6 W
  1. >>> from math import sqrt
    / k0 F5 |& |) c  \# X& o
  2. >>> sqrt(9)
    1 p3 v$ u2 H& C
  3. 3.0
复制代码
! D% F% N0 a! W+ N; h) V9 V! Y

' L8 K5 T, L% T# ]4 }2 L. K
math.e  表示一个常量
+ i" b  M, a( `6 G' n+ j
  1. #表示一个常量7 A6 K, E+ Y* p% ~5 @6 P  ?( p
  2. >>> math.e. g* n7 k# Z4 U: o, J6 U
  3. 2.718281828459045
复制代码
1 M& X7 ]1 k/ m
math.pi  
数字常量,圆周率
, [7 ~; @& e8 H  d/ E0 L3 ^
  1. #数字常量,圆周率1 X$ X& P' V9 \  O9 ?
  2. >>> print(math.pi)1 z5 U6 y$ p2 ]0 O. T' S
  3. 3.141592653589793
复制代码
5 J/ u7 r1 A$ V9 F8 V
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

1 L5 K, }' G9 V* s
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x% c& F( h$ p3 Z: ^- Z+ C8 N
  2. ceil(x)% t' @5 }' K: x2 ^- w
  3. Return the ceiling of x as an int.
    $ n" E% Z" U4 @
  4. This is the smallest integral value >= x.
    6 w: I5 T; N( K: L& Q( H

  5. # f5 @9 A, `0 Y! F% h, a4 _8 P
  6. >>> math.ceil(4.01)3 ^' q! [+ ?. F; ?3 A. r, D: {. }$ h
  7. 5
    5 b, o) f+ M6 K
  8. >>> math.ceil(4.99), [8 _' x8 j( Y" v( r- [8 {, f0 h4 ~- }
  9. 5! @& Y, J2 V: V8 v- ^. q7 ?/ `! r
  10. >>> math.ceil(-3.99): u8 g  k8 ]" I7 z/ i
  11. -3
    5 N( r% y! t+ t8 f/ ]  L
  12. >>> math.ceil(-3.01)
    - X5 T4 n7 \# ^
  13. -3
复制代码
+ u) B+ @- |; y6 K5 N: x
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 D+ |- G! d! a% q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) f. u: }' p$ P- o
  2. floor(x): ]' S2 T% k: X: o1 h
  3. Return the floor of x as an int.
    6 J$ N% `6 a2 A
  4. This is the largest integral value <= x.2 Q  g- |/ X) D' ]* C% B8 s
  5. >>> math.floor(4.1)2 g2 B& c" M- x. K: P+ @5 F# }5 {
  6. 44 Y2 s6 r  K) g9 J& P- r
  7. >>> math.floor(4.999)  M( @  v" f( A" M" Q
  8. 4
    8 o1 E/ \5 I7 H
  9. >>> math.floor(-4.999)
    " p! c+ u* }8 p, r; r/ U, e
  10. -5! v! \, x+ \& v
  11. >>> math.floor(-4.01)
    4 \5 `/ `1 g5 \1 Z( V/ V+ o) c2 i
  12. -5
复制代码
. a5 s& q' L. `$ H( n# x
math.pow(x,y)  返回x的y次方,即x**y
6 D8 A" }1 R$ j7 o0 r6 V2 \
  1. #返回x的y次方,即x**y% l) {6 m! p: o- p  }' \
  2. pow(x, y)
    $ L" B7 Z: B- [
  3. Return x**y (x to the power of y).
    * b6 b, F5 u" ^( l/ o! m/ `
  4. >>> math.pow(3,4)
    3 S' `( Z& \/ r& t+ i( w  w
  5. 81.01 M! p# ~( _/ T% }- q) }$ b) `
  6. >>> 3 [# V2 k7 e1 ^$ }+ A
  7. >>> math.pow(2,7)
    : C$ [" m/ X, B- b( T0 ~, h
  8. 128.0
复制代码
1 _  N% o4 ?3 D5 f4 f
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 Z. u  Y1 D; o1 Q4 A# o
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), G1 I2 C% n/ t) \$ M! R: z
  2. log(x[, base])
    + W0 f' d8 s6 B' v* F: L! _
  3. Return the logarithm of x to the given base.
    2 t! T  _+ N8 B$ [" z
  4. If the base not specified, returns the natural logarithm (base e) of x.$ h! y* c* u$ j; X8 |7 T
  5. >>> math.log(10)9 Z  B$ l' H& d2 n6 y
  6. 2.3025850929940464 N# b- U! q' s/ H* u& H) Y
  7. >>> math.log(11)
    - ^& x5 _0 l" ~( V. D6 B
  8. 2.39789527279837070 I4 ^, b6 O' n$ s' u
  9. >>> math.log(20)
    ( }, Z8 `# j9 k. q. ]! C
  10. 2.995732273553991
复制代码
- s2 z( H3 b- F) s" [
math.sin(x)  求x(x为弧度)的正弦值
8 a: p2 q9 I/ h% |' R
  1. #求x(x为弧度)的正弦值
    9 c; y' D) `0 G. `: g3 r
  2. sin(x)7 o. u0 Y: ]2 F  j1 k# k0 m, H: m
  3. Return the sine of x (measured in radians).$ j4 _' m: s: `2 Z& V6 A* k
  4. >>> math.sin(math.pi/4)3 s+ {- ~$ l4 o6 _2 E( ^+ M
  5. 0.7071067811865475! a" L4 R# W7 N1 A* b  Q8 g  g
  6. >>> math.sin(math.pi/2)3 c- d& n3 R( Y( s: I7 M6 ~. G  P
  7. 1.0
    # J" B: J5 b7 W0 _# R/ g- Z( n
  8. >>> math.sin(math.pi/3)
    1 g' A" T3 Y- ?0 I. j
  9. 0.8660254037844386
复制代码

" X/ A1 B" w/ ymath.cos(x)  求x的余弦,x必须是弧度
& ]% I7 W6 _6 }% n
  1. #求x的余弦,x必须是弧度) o: w( N/ T% S; g( F4 v: u
  2. cos(x), E% Q. E) C: j$ W1 v* g5 G
  3. Return the cosine of x (measured in radians).
    ( c- j* a$ W9 {. L
  4. #math.pi/4表示弧度,转换成角度为45度
    . g* O3 z  ~% k4 M$ K
  5. >>> math.cos(math.pi/4)
    2 k6 Y: m4 |+ s$ w) @, e/ P
  6. 0.70710678118654767 ?* U5 ], D. m# R4 X
  7. math.pi/3表示弧度,转换成角度为60度# F) x; f6 \! J" j; n6 q9 O' X& L
  8. >>> math.cos(math.pi/3)! K$ E' ~, p, }& z1 R0 g4 H2 e& ~
  9. 0.5000000000000001
    3 M# {6 k) ]6 h& H  w6 y3 T. h* D
  10. math.pi/6表示弧度,转换成角度为30度# C7 M! @4 u3 A
  11. >>> math.cos(math.pi/6)
    + [* W( l  P) k
  12. 0.8660254037844387
复制代码
# y. r& n- e1 v8 D; N2 {
math.tan(x)  返回x(x为弧度)的正切值; `, i7 O% p6 i' K% h' E/ y
  1. #返回x(x为弧度)的正切值
    2 P( S: e2 k) L4 Y' Y
  2. tan(x)/ a7 Y/ l% `6 M4 P- n
  3. Return the tangent of x (measured in radians).+ Z/ @* q  I/ P! d
  4. >>> math.tan(math.pi/4)" |. |9 N$ P# X( c4 o" |9 W& C$ i
  5. 0.9999999999999999$ n; k- b, B3 L# m4 j! U) S- q6 }
  6. >>> math.tan(math.pi/6)
    2 E- R; ~3 k' t% G
  7. 0.5773502691896257
      W% i4 Q; H7 I2 Y; b. q( j5 [
  8. >>> math.tan(math.pi/3)
    8 p1 |4 B7 c% O1 c# V' c6 O
  9. 1.7320508075688767
复制代码

/ K+ d9 n& G$ o. w0 d# I- d% xmath.degrees(x)  把x从弧度转换成角度8 E0 r' b" B) F5 I6 u
  1. #把x从弧度转换成角度5 S$ Y: T& I' ?* K
  2. degrees(x)1 f% P5 a- s. O- l+ A, S" _
  3. Convert angle x from radians to degrees.0 s( x8 i0 t% J' M# v5 u8 }5 X
  4. ; B( |; H0 l3 f. i( G8 E! `
  5. >>> math.degrees(math.pi/4)
    ; ~* W! a8 s0 ?7 c' {! m
  6. 45.08 N7 `/ z+ s1 _$ m) E" t) t
  7. >>> math.degrees(math.pi)4 Y  _8 D, I" ~
  8. 180.0
    2 z$ F# r% ~6 Q/ I3 Z
  9. >>> math.degrees(math.pi/6)
    * E1 m; C1 E$ h, [7 n- |
  10. 29.999999999999996
    $ L' R- w6 y1 O$ w' Y5 H8 N: U
  11. >>> math.degrees(math.pi/3)  S. N* }9 v  U- z: Y8 n- f
  12. 59.99999999999999
复制代码

5 f" p) c, n1 H9 Q7 E8 bmath.radians(x)  把角度x转换成弧度
0 N- o4 H& D7 f( G
  1. #把角度x转换成弧度
    - S; b8 J1 u' U
  2. radians(x)6 d2 z+ x) @1 {% p
  3. Convert angle x from degrees to radians.
    & g: E( f7 I. l4 U& N
  4. >>> math.radians(45)
    # M  g5 S( I9 O, Z4 ^3 W/ b
  5. 0.7853981633974483) A# t" x% R1 ?4 d9 l7 c, ]: x
  6. >>> math.radians(60)
    - T' k5 d) v0 L. L: s
  7. 1.0471975511965976
复制代码

8 w! o4 [3 M% e4 \* `- |, T5 {8 ^math.copysign(x,y)  把y的正负号加到x前面,可以使用0. y! T8 \3 x7 |& t, H4 C
  1. #把y的正负号加到x前面,可以使用0
    0 c& K% ~! R& Z; S( r
  2. copysign(x, y)
    % D+ b% Y$ E6 O2 l  I6 g
  3. Return a float with the magnitude (absolute value) of x but the sign + l. ]- ]: h7 r* h
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    3 f6 i" Z! y/ m* p8 E; {% H
  5. returns -1.0.  K# ]- k9 b( I6 c. S
  6. ; w4 Z! K5 x. m. n' s! ~* k
  7. >>> math.copysign(2,3)1 L# q2 S1 A  D* t; K; `+ \
  8. 2.0
    7 }; M8 x/ q) H4 F4 V9 z9 ~- q- P
  9. >>> math.copysign(2,-3)
    / `  M$ g, C' }% _- G! E* y" `
  10. -2.0
    3 ]: T9 {. a# E; h3 [8 `, A
  11. >>> math.copysign(3,8)
    ( j7 {: c+ L" v8 f
  12. 3.0
    # i: Y! _1 _7 ?, c5 u& }3 u
  13. >>> math.copysign(3,-8)
    " C1 e$ M# o" h( p4 `0 `8 c+ i
  14. -3.0
复制代码
4 P& b" S$ g. f1 o. l
math.exp(x)  返回math.e,也就是2.71828的x次方& f0 |9 \6 ^( w, A
  1. #返回math.e,也就是2.71828的x次方
    6 U% w( y4 I! V6 n+ u3 o, [
  2. exp(x)$ V1 a' x# E$ Q
  3. Return e raised to the power of x.4 `  v) g9 V) j

  4. 2 d. b, B% [% d/ R1 _
  5. >>> math.exp(1)9 R7 p7 A/ j# x% E. j
  6. 2.718281828459045& D7 x9 l- h) P* D2 k/ D& A
  7. >>> math.exp(2)! m) x' R5 O; B- E2 G, w, {! n1 i/ h
  8. 7.38905609893065
    6 m- B+ |2 [0 |7 ~
  9. >>> math.exp(3)5 V; Z: T0 q, e- {5 \
  10. 20.085536923187668
复制代码
, ]( k% a3 Y5 g$ Q0 n) O5 v
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1# b! Q' p/ h" d' O& L( ^) R: E
  1. #返回math.e的x(其值为2.71828)次方的值减1( F4 O$ Z+ z7 q4 m* @# F8 p
  2. expm1(x)
    9 U1 H& L# v! B" u. k4 ~0 F* o
  3. Return exp(x)-1.
    " Q) I: _- Y! f. C
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.2 O; D9 w% U, ?5 W  O

  5. - i) q1 W4 q+ |
  6. >>> math.expm1(1)
    * x% Z+ k5 m- B. j8 [4 o3 ?
  7. 1.718281828459045" T$ V# Z9 l) @  B
  8. >>> math.expm1(2)
    ; I2 n1 A  _1 R3 z
  9. 6.38905609893065
    / t) L2 j0 g% l- |; a! W: ]
  10. >>> math.expm1(3)
    ' ?) y9 G' `' o1 p& Z
  11. 19.085536923187668
复制代码

! p- j0 w1 n" T' {( _6 Rmath.fabs(x)  返回x的绝对值
1 n/ E9 r9 E7 X: A: P+ I
  1. #返回x的绝对值
    - j+ M5 V5 h& z! q" L, }
  2. fabs(x)$ S( d( G7 X  ]* ~
  3. Return the absolute value of the float x.* b! \, |7 ~9 {% z! U* ~
  4. 7 Q. Z2 ]/ ~* r7 g
  5. >>> math.fabs(-0.003)( H6 T# O! o7 o! i! M9 J7 R
  6. 0.0036 `% n/ |4 o6 ^4 U! X
  7. >>> math.fabs(-110); r2 R  P8 ~$ P: @; r+ L( W
  8. 110.0
    & p4 [3 u  t. C
  9. >>> math.fabs(100); g, U( b* x  N/ a1 d3 M6 P
  10. 100.0
复制代码

- V& K/ G! ^. o+ t7 H4 Zmath.factorial(x)  取x的阶乘的值
: E7 O- z: W) h4 B( U! V
  1. #取x的阶乘的值
    # q! P% m: m$ ^; L7 }
  2. factorial(x) -> Integral. A9 o& `9 o( {" d2 d, g
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    , d" z% Q" r9 J6 k
  4. >>> math.factorial(1)8 [8 u+ `/ I# k
  5. 1
    0 W9 E; v, ]4 l9 \/ I  P8 _
  6. >>> math.factorial(2)5 q; c: F$ v* G" B
  7. 2
    7 @% S& s9 _% B. I: \* ?
  8. >>> math.factorial(3)7 s8 h, G: g  U+ h4 f
  9. 6! F/ Y+ w3 m2 q* V2 C6 b: \- y& \; {
  10. >>> math.factorial(5)
    - k- A6 Z2 K. m$ T5 B
  11. 120) a  `+ `) O5 i6 I8 h
  12. >>> math.factorial(10)
    6 v5 S" x* N' ?0 q& k+ z3 Q1 i
  13. 3628800
复制代码
1 C3 z3 Z. v) c4 q+ L
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数- ~- q! f; H! |$ B& R
  1. #得到x/y的余数,其值是一个浮点数& O& s/ P; e1 |/ R! g/ \0 o
  2. fmod(x, y)
    ( o5 {- S2 a3 T: E7 b& _% \7 k& j
  3. Return fmod(x, y), according to platform C.  x % y may differ.& @/ p  E$ {2 o
  4. >>> math.fmod(20,3); s0 P# `0 ?, H
  5. 2.0
    & E$ I1 v, j9 A
  6. >>> math.fmod(20,7)
    9 P0 L  }+ n. N
  7. 6.0
复制代码

/ j; y, p( O5 `: U) Emath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 d6 r9 I! v( ]5 S7 T% Y5 W! ~
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 a" T( f$ s4 R
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值% ?, a5 x1 ]/ s5 V; E  N
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1+ _# Z' s0 d) A7 ~' {/ U
  4. frexp(x), @2 t, Z6 `) u; b4 T
  5. Return the mantissa and exponent of x, as pair (m, e)., b6 }1 q* R- A" U1 ^3 @2 T
  6. m is a float and e is an int, such that x = m * 2.**e.
    ( s/ r7 f9 }! z" d" b9 y: y
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    - r; b" m  X( m' @
  8. >>> math.frexp(10)
    1 o% k( w, f8 D0 @
  9. (0.625, 4)# q# l! [! M( u, F& x
  10. >>> math.frexp(75)
    7 }5 W: `( B$ s; a# k
  11. (0.5859375, 7)
    * f& ]( D9 p( O
  12. >>> math.frexp(-40)
    . V) H# d2 |- r2 }  A' F/ Z
  13. (-0.625, 6); i* z3 J) v7 j' \* L
  14. >>> math.frexp(-100)7 G. z4 f1 W' n1 ?9 k, Q
  15. (-0.78125, 7)
    - E: k6 L* V/ ~8 ?! a2 q8 a
  16. >>> math.frexp(100)8 L3 z9 j8 j7 J5 ~
  17. (0.78125, 7)
复制代码
1 _; I8 H, _: ~0 u7 X; s5 j
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
4 _) f* ?1 e& M9 |& z! d8 _+ d! m
  1. #对迭代器里的每个元素进行求和操作
    . Z+ ?  }) r! A( {. m* c6 {* X
  2. fsum(iterable). n3 f; L. m  w! A# f0 }( m0 C
  3. Return an accurate floating point sum of values in the iterable.
    / g5 C) h! [! w& W
  4. Assumes IEEE-754 floating point arithmetic.
    ' i& X- {: V: e' s( ?  [7 y$ j
  5. >>> math.fsum([1,2,3,4])
    % q5 g' A7 t5 Q( B% F5 S
  6. 10.0. K* W. }+ s3 N( m1 ?7 [
  7. >>> math.fsum((1,2,3,4))( N. ^8 V: {8 t+ U9 Q- s
  8. 10.0, o* s% b* j- N
  9. >>> math.fsum((-1,-2,-3,-4))
    $ @6 b& T# `8 ^7 u$ _( }
  10. -10.0# O6 j+ n; }6 I/ f
  11. >>> math.fsum([-1,-2,-3,-4])* E" M0 L* q! h3 \" f' Q
  12. -10.0
复制代码

4 T) T$ e  k! V# z& B$ Rmath.gcd(x,y)  返回x和y的最大公约数
+ G! B$ U: E0 L5 D
  1. #返回x和y的最大公约数
    5 N' C$ k; I, D4 f7 O) s
  2. gcd(x, y) -> int
    % V  l; w/ @% a
  3. greatest common divisor of x and y
    0 d6 O! i  i1 M& F# c- Z& l. Z; ~
  4. >>> math.gcd(8,6)
    ( N: p: |; w% M# s
  5. 2
    3 `/ a! s3 j) j* o
  6. >>> math.gcd(40,20)9 L6 I9 m, F( ~( P2 i3 h
  7. 206 w3 U. f5 t2 [4 ~2 u
  8. >>> math.gcd(8,12)
    0 H1 ~9 S* W. x, _, F. l
  9. 4
复制代码

' A( t, A7 _9 I" kmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
: Z+ G0 w! I" c1 M# S. ~
  1. #得到(x**2+y**2),平方的值2 {5 P9 E' h8 T7 Q+ L
  2. hypot(x, y)
    3 [: B% L. c6 I, h: g" J7 U
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 L; H! I9 U0 i" r" A
  4. >>> math.hypot(3,4): S. q5 ^$ V: A) p1 C
  5. 5.0
    9 U/ `" f, h% Y; d
  6. >>> math.hypot(6,8)+ P; M7 w/ t, I* [! S4 Z
  7. 10.0
复制代码

( F2 B0 j, a( P1 H: m% y' @/ o& ymath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False; _: x6 o; g# B) d* {" l! @
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    : v6 C0 p6 f1 K" e1 z2 G: S
  2. isfinite(x) -> bool" {2 [, G& B* x
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    3 A- l( p% r/ p, I' M9 J
  4. >>> math.isfinite(100)& \0 e" G8 \2 n( u) ~# z
  5. True1 W0 d* N( D0 m$ H/ J
  6. >>> math.isfinite(0)* t$ Z/ z  z; }9 t
  7. True
    # v. e' [3 e# o% s/ O4 w
  8. >>> math.isfinite(0.1)9 \+ e0 x0 E5 b3 }# x- s
  9. True/ H! l7 Y8 V- J* M7 _* K4 G4 ^
  10. >>> math.isfinite("a")
    ) g* ]! T5 }( ]; O
  11. >>> math.isfinite(0.0001)+ m+ l' l- L% H5 J
  12. True
复制代码

* s. H8 h. W/ @, Umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ F; `+ j' A1 B
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False7 A: H: N4 }( r# ?* o) u8 Q
  2. isinf(x) -> bool+ V, L. v4 R9 j) F$ U! b3 T
  3. Return True if x is a positive or negative infinity, and False otherwise.
    3 n2 A; _* S* C9 U' e1 v8 i* H
  4. >>> math.isinf(234)
    6 @; ?$ Z1 |5 ]
  5. False
    # n) V: }" l$ z
  6. >>> math.isinf(0.1)* L' y$ u8 u( Z2 L% A
  7. False
复制代码

  T% y3 s/ t; d% A/ C/ smath.isnan(x)  如果x不是数字True,否则返回False
5 K' f/ t, }: W2 {' \
  1. #如果x不是数字True,否则返回False6 {# ?, E" C7 g6 T* p% _
  2. isnan(x) -> bool
    3 M9 J# L8 z8 q
  3. Return True if x is a NaN (not a number), and False otherwise.1 Y8 C0 @  N8 _3 |$ p! }
  4. >>> math.isnan(23); u6 F; m0 w* J0 Y
  5. False0 V8 m6 W% B8 d- D
  6. >>> math.isnan(0.01): Z' F; A" ~1 C% H1 A4 J
  7. False
复制代码
7 U, U* G; X( L1 b; ]$ B3 s/ J5 z. w
math.ldexp(x,i)  返回x*(2**i)的值6 |* {& e; ]8 y- ^2 k
  1. #返回x*(2**i)的值2 V; Z+ J$ `5 s) d# h
  2. ldexp(x, i)2 Y6 P; ~6 @( R! L/ ?& D) z  N: c) ]/ o
  3. Return x * (2**i).
      U- |1 O$ Q1 [/ o
  4. >>> math.ldexp(5,5)
    / `3 L$ r* P% y; Z
  5. 160.0* b2 O) C4 g# ]
  6. >>> math.ldexp(3,5)
    ' b  q0 Z* F2 [9 F( E/ C( c. y
  7. 96.0
复制代码

  j  ?3 V" B! n2 l% C0 M* omath.log10(x)  返回x的以10为底的对数
9 Z; g; w/ L4 o6 r# w! Y
  1. #返回x的以10为底的对数- W7 ], Y. F8 J' o- `
  2. log10(x)
    5 e: b" h& M! C' Z: I, p
  3. Return the base 10 logarithm of x.! x8 f9 a: Q- D
  4. >>> math.log10(10)
    8 ~* y9 d9 m% x% W( ^
  5. 1.0  M& l5 ?( s: R% a4 m
  6. >>> math.log10(100)
    6 ^3 M" s/ d2 l0 ^* a5 Y$ S7 Y
  7. 2.07 k0 ^6 y0 I+ m/ w0 s5 E0 a+ ]+ }
  8. #即10的1.3次方的结果为20
    $ s2 l  E" {+ A; {1 o: ~; F) Q
  9. >>> math.log10(20)
    7 K. b; F- f! ?3 w: H4 H1 o' _
  10. 1.3010299956639813
复制代码
/ {5 a( u  q. P: q6 T
math.log1p(x)  返回x+1的自然对数(基数为e)的值3 i8 _& P, P  m+ X# g4 s
  1. #返回x+1的自然对数(基数为e)的值
    5 `; Q5 t2 z# J+ s& `; g# L: r) \
  2. log1p(x)/ d- X3 R* l8 q+ @$ n- `4 A
  3. Return the natural logarithm of 1+x (base e).
    ' B, X' _! R" ?8 r2 @0 A
  4. The result is computed in a way which is accurate for x near zero.4 v- G2 Y/ ]- {( T7 s
  5. >>> math.log(10)4 U- ~  y% J/ l+ t
  6. 2.302585092994046' Z. d# P3 o) p2 a' F
  7. >>> math.log1p(10)! ~" }* d$ B3 C3 e7 U3 V& j
  8. 2.3978952727983707
    $ i! A8 Q3 B: w- e
  9. >>> math.log(11)0 \, e7 q( T: Y# ]
  10. 2.3978952727983707
复制代码
0 c0 z8 f  u! A1 W+ |, U. z% a3 u+ U
math.log2(x)  返回x的基2对数0 d* d9 _! }- Q" t
  1. #返回x的基2对数
    6 C' m% T# Y! S2 C- f2 _5 |
  2. log2(x)7 o$ o; a, d' R- L5 U& Q
  3. Return the base 2 logarithm of x.
    1 S& z5 O- B! L0 w* W5 O
  4. >>> math.log2(32), `% q% d$ }5 H# P  f( l' P- Q, Q
  5. 5.0
    % e- o: ]; c# c! ?
  6. >>> math.log2(20); k- o1 M  F: t1 {7 O! u
  7. 4.321928094887363/ H/ I8 g. q) w
  8. >>> math.log2(16); j0 S  M! x+ ]; F7 s+ w
  9. 4.0
复制代码

0 m) e" o2 M3 ?9 O, V, Fmath.modf(x)  返回由x的小数部分和整数部分组成的元组
4 f* s; a7 h: x5 ?3 l
  1. #返回由x的小数部分和整数部分组成的元组
    $ a6 V* H% v5 y0 Z5 S
  2. modf(x)
    ! G% H$ z. B' d/ v4 P
  3. Return the fractional and integer parts of x.  Both results carry the sign: r2 P- ?* {8 m# C2 |4 @
  4. of x and are floats.
    ; z7 C" s7 K' Z( h! U; n5 ]
  5. >>> math.modf(math.pi)8 V- N  O1 x) i$ W7 L  }( D& W! ^) X
  6. (0.14159265358979312, 3.0)% I1 Q2 i( s% J% B0 g
  7. >>> math.modf(12.34)
    4 ?( O! g6 C- I( y2 }) \
  8. (0.33999999999999986, 12.0)
复制代码
6 B4 Y0 q$ B' ?- y2 D; ^9 G( J
math.sqrt(x)  求x的平方根+ q3 Y+ C5 c0 r5 G
  1. #求x的平方根& y' O9 P3 K9 h6 ]. m7 G4 U
  2. sqrt(x)
    $ {1 G3 ]: g" r. c
  3. Return the square root of x." \8 p7 X1 [+ a3 {+ Q, Y$ Y
  4. >>> math.sqrt(100), O* z- p5 S$ l1 i) L
  5. 10.0
    ; N" g4 U: a- s
  6. >>> math.sqrt(16)
    4 \5 W- s9 {! A9 b- T' E
  7. 4.0
    . c6 J* s, U( x+ d. x9 ]
  8. >>> math.sqrt(20)' c1 }4 i. k$ I3 j% N
  9. 4.47213595499958
复制代码

/ x& V9 U' |6 D, x# emath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分# n8 i3 d2 f9 |
  2. trunc(x:Real) -> Integral5 `. d0 N1 E* M, U( c3 T
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' T! a  ?8 o& a7 V
  4. >>> math.trunc(6.789)
    * Y+ A* _3 ~5 K7 e! z" g
  5. 6
    5 d) R! s- h3 W  A0 k- ]
  6. >>> math.trunc(math.pi)
    3 t% w# C5 m% a( L
  7. 3+ j' I0 R0 N: k0 N
  8. >>> math.trunc(2.567)
    % i$ D6 L, ]- z, r; C  c# r
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

新大榭七周年,感谢由您!

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

GMT+8, 2025-7-2 15:18 , Processed in 0.084344 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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