新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
; y; Q) ~8 Q  E& d/ V! r) j# O  p
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 h8 P; d  ]. y, c
( N6 Q1 |: H+ `% w3 L- ~" Q! l. y
方法1
9 {, ]+ Q6 r; o1 P7 p3 x" O
  1. >>> import math
    6 C1 O1 E- ?% p0 ^0 K, w" O
  2. >>> math.sqrt(9)' B+ l' b5 d7 l6 _6 L/ m) D6 ?
  3. 3.0
复制代码
方法2
" e% G( d3 F3 M
  1. >>> from math import sqrt
    + g3 n  x5 R) l+ ^, Q
  2. >>> sqrt(9)
    ) B) _. \# [) _
  3. 3.0
复制代码

# u# S  D1 b; @& d. R+ e2 T5 |

) z# z( z" `# }: n8 p* _
math.e  表示一个常量
  D' a- [$ T/ o9 P1 C" A4 H
  1. #表示一个常量3 R' _4 X4 G8 N' L
  2. >>> math.e0 B/ W3 {) q. S
  3. 2.718281828459045
复制代码
7 `5 J% x' Z2 s! v1 _' ~" p! Z
math.pi  
数字常量,圆周率

; n; r) o; ^, a( f
  1. #数字常量,圆周率  C9 s7 E, Q! P
  2. >>> print(math.pi)
    # j! O& {8 {; k+ j/ T1 t
  3. 3.141592653589793
复制代码

. s3 ^1 Q9 D$ F$ D  Smath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

( r% ?# b% c$ s" `) H
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x  A1 w! z1 T. E+ R
  2. ceil(x)2 n7 Y& I8 k, S2 Y# E9 J) j$ a
  3. Return the ceiling of x as an int.
    3 y8 c; i6 W7 V" m1 a9 K6 L
  4. This is the smallest integral value >= x.
    ( {7 N- i% ]0 }: o' X" d! A

  5. % Q9 D. F, B' l9 o; I
  6. >>> math.ceil(4.01)* v% T+ i( O- F* S; |
  7. 5
    9 m' N* {0 S$ `5 q& v% r
  8. >>> math.ceil(4.99)4 ?) z5 P* c; w# \, o1 D8 N$ ~2 D
  9. 5; ?2 w4 E4 j8 W) s
  10. >>> math.ceil(-3.99)/ M" P9 t! E* `* a8 |& Q4 s/ h
  11. -3
    $ Z( m) T2 _( D* D4 S( ?( m
  12. >>> math.ceil(-3.01)4 x% t7 M& C. u6 i/ w: a
  13. -3
复制代码
9 s, p+ z" F: C! u2 b
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; O5 \$ {) B% s
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    8 L8 [  R) X& G! X$ m. V; R
  2. floor(x)  E: F5 a+ r$ e) b0 m
  3. Return the floor of x as an int.( W- C+ ]8 [" x4 m7 c8 `1 N: {/ k
  4. This is the largest integral value <= x.- I5 u+ H9 A6 |* a- u' `
  5. >>> math.floor(4.1)+ x3 i/ M! v6 g
  6. 47 k6 r2 v, f! L$ A* ^' V
  7. >>> math.floor(4.999)* X! }' x& q" s2 c: R" ?
  8. 4
    & n' w. y- M8 O& P* O0 t
  9. >>> math.floor(-4.999)
    - W3 C7 \7 K# B6 L) f' I
  10. -5
    * O; ]' M3 d- u
  11. >>> math.floor(-4.01)) e% [" ]( ], `: j+ i2 `
  12. -5
复制代码

0 R3 U) `% |% ?math.pow(x,y)  返回x的y次方,即x**y. V" E/ `" d. w6 \6 l/ v  u) |0 l
  1. #返回x的y次方,即x**y6 c/ G7 o8 D; Z! \. `
  2. pow(x, y)
    - G7 X. I( T* Z2 X
  3. Return x**y (x to the power of y).0 F. t6 x( d; E3 `
  4. >>> math.pow(3,4)
    - `) R- X- u0 d5 q: _
  5. 81.0$ d. {6 k5 v- a# \
  6. >>> 9 C5 @: x' v( _' H7 T" d
  7. >>> math.pow(2,7); G! f4 z; ], E8 X& ]
  8. 128.0
复制代码

! c- d" d. \6 G8 Z. vmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' ~$ D/ @# J! Y& ?
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    / W* t- B' a* s
  2. log(x[, base])6 n6 C* Z7 O# C. V1 }8 l) ~
  3. Return the logarithm of x to the given base.5 D% C3 Y  b2 J: _! z  u
  4. If the base not specified, returns the natural logarithm (base e) of x.1 Y: ^: Q* O& e7 b, Y7 x2 O. M
  5. >>> math.log(10), Z8 s2 [, R: d* R! o
  6. 2.3025850929940460 e1 j) \/ U! E  ^
  7. >>> math.log(11)! d/ ]# q; k# a- Y, O' i9 \
  8. 2.39789527279837076 ~2 r2 o& ]1 [( |# a
  9. >>> math.log(20)5 C2 B, ?) ]: H* H$ u
  10. 2.995732273553991
复制代码

' ~* L4 v, X3 R+ Fmath.sin(x)  求x(x为弧度)的正弦值, ~$ }8 M- E& g8 G5 m3 M  S3 Z/ y- T
  1. #求x(x为弧度)的正弦值" V+ ]: A. g+ s
  2. sin(x)
    4 H; d6 o9 y0 c, ^
  3. Return the sine of x (measured in radians).
    ' W- Y! m  @& A" e& O+ Y8 M4 F* [
  4. >>> math.sin(math.pi/4), a7 `5 b6 ]! ?
  5. 0.7071067811865475
    6 x5 b% }8 o& n
  6. >>> math.sin(math.pi/2)
    - q7 z! o* |, T) ^# y) N9 _9 o
  7. 1.09 j# t# w. [$ a! `" U
  8. >>> math.sin(math.pi/3)% C- X8 O: X, r6 x; [' ~! d5 ~
  9. 0.8660254037844386
复制代码
  z# ~# G* ^! G' D1 U
math.cos(x)  求x的余弦,x必须是弧度
3 G, ^* K( V+ `
  1. #求x的余弦,x必须是弧度
    / }1 ~  l% k: R' y9 e7 F. R
  2. cos(x)7 [8 K5 d. O: r8 n5 x0 W# f8 J- _
  3. Return the cosine of x (measured in radians).
    $ ?$ s+ o) E3 y1 _/ y/ @
  4. #math.pi/4表示弧度,转换成角度为45度* Z6 V3 Z8 q( [  W' I/ X# s
  5. >>> math.cos(math.pi/4)# Z( T  w) B9 ?- M) b9 @  P1 ^
  6. 0.7071067811865476& V1 F# g3 k0 V+ f
  7. math.pi/3表示弧度,转换成角度为60度! o( _! @7 \& v! Y% n0 ^
  8. >>> math.cos(math.pi/3)
    9 T7 h- v! c' I8 X* l
  9. 0.50000000000000014 T/ G) a4 A6 A
  10. math.pi/6表示弧度,转换成角度为30度8 c3 W# A; o4 t* G
  11. >>> math.cos(math.pi/6)$ q0 q9 F. R7 S; v& T8 M
  12. 0.8660254037844387
复制代码
2 I, J, f  T0 H5 G3 _7 J
math.tan(x)  返回x(x为弧度)的正切值
$ X; V( h: N! H1 g3 G4 f7 e) I) R
  1. #返回x(x为弧度)的正切值0 ]% H, r) H' F
  2. tan(x)% x) I  g' j5 g6 q9 _, p+ x7 Y
  3. Return the tangent of x (measured in radians)." m' Y" `% S8 n
  4. >>> math.tan(math.pi/4)$ a+ ]' I3 _0 f9 o0 G/ b/ _+ ~
  5. 0.9999999999999999
    " S. y& e, Z7 v* Y$ Z
  6. >>> math.tan(math.pi/6)
    ' R3 b8 Z( D8 J3 @
  7. 0.5773502691896257
    % ^9 D& e3 F" K9 N; e8 k: M$ Z
  8. >>> math.tan(math.pi/3)
    ! G* T& Y1 b4 {) k% w) ~3 r8 g
  9. 1.7320508075688767
复制代码

& {! d: W; O5 d/ p2 I, H, Tmath.degrees(x)  把x从弧度转换成角度. Z4 Z: m! i- V8 v# b* a
  1. #把x从弧度转换成角度1 A5 D" C) r1 j
  2. degrees(x)+ I9 T$ {/ q+ V
  3. Convert angle x from radians to degrees.
    4 F; a0 j$ k% P) K

  4. / B9 Z$ p% K* ^( K9 j6 p% }& N
  5. >>> math.degrees(math.pi/4)
    * j& h# A3 [, Y  w
  6. 45.0
    2 Q" F0 U3 I! w+ s5 y* @
  7. >>> math.degrees(math.pi)+ V; [/ C1 h; b" M' T$ {- `
  8. 180.07 s3 v- T  z& L+ c
  9. >>> math.degrees(math.pi/6)
    7 |1 ^3 `  h0 f; i. L  k4 _/ w
  10. 29.999999999999996" h( A% Q; \! Q8 r( C! _8 u0 V
  11. >>> math.degrees(math.pi/3)4 _2 P' D. n: P9 {+ C
  12. 59.99999999999999
复制代码
0 j% `: }0 Y* X1 m
math.radians(x)  把角度x转换成弧度5 B5 i$ l2 L( V* H% ?
  1. #把角度x转换成弧度
    3 J& p  U' C1 e; F
  2. radians(x)
    $ t% B9 G1 [& e7 t
  3. Convert angle x from degrees to radians.0 E; j$ T8 {& G
  4. >>> math.radians(45)6 g& F1 s2 y! D+ r' E) f
  5. 0.7853981633974483
    - a) s% j; d0 [7 a- v& C
  6. >>> math.radians(60)
    5 ?3 k* O: z7 _; |; \. K2 b
  7. 1.0471975511965976
复制代码
* E: K/ t9 o9 W) B" E% j8 A0 }
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
* L. ?6 H$ |) `  T
  1. #把y的正负号加到x前面,可以使用0
    ! X+ m& {3 J  S$ s. f
  2. copysign(x, y)
    ! ^: C4 {; a/ T8 h) P6 a: Y
  3. Return a float with the magnitude (absolute value) of x but the sign
    9 B$ g' V% q  e0 s7 e8 h
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 C1 l+ r" B" E* }$ z$ d6 @: Y
  5. returns -1.0.+ g- y( {: z  ^% R& ]
  6. * `1 N. n9 r1 r7 q' Y: S
  7. >>> math.copysign(2,3)
    5 K5 ^/ z+ ~/ A( e  n
  8. 2.04 g7 ]# _6 O% ~- F
  9. >>> math.copysign(2,-3)
    # ?  ]  |/ `/ Z6 Y
  10. -2.0
    ) @' E1 L0 Q! x, y/ ~- k
  11. >>> math.copysign(3,8)
    6 P/ }" ^2 x* J8 L6 b
  12. 3.0
    6 B( z9 P, l5 ?$ G
  13. >>> math.copysign(3,-8)8 r: y5 `+ o4 F1 F# X$ |
  14. -3.0
复制代码

8 t% D* R) B! r7 f$ O7 d. o" b1 zmath.exp(x)  返回math.e,也就是2.71828的x次方
6 Q) y# a" o' W
  1. #返回math.e,也就是2.71828的x次方; b0 ]/ i# s# ^$ o- x6 ^
  2. exp(x)  ^& P9 |5 L+ i% `- S
  3. Return e raised to the power of x.
    - h% O+ @: W0 P- ]- b8 M
  4. ! \! l7 X  g1 ^" }0 E
  5. >>> math.exp(1)8 U5 |( `/ t6 v$ C3 ^$ `+ F' T
  6. 2.718281828459045
    3 s4 M" ]7 `) U( h' J
  7. >>> math.exp(2)
    ) F& p0 @3 q  ]
  8. 7.38905609893065: x1 r) P. _8 J1 m; f4 I
  9. >>> math.exp(3)% c, q2 x5 D: B; {4 T8 m
  10. 20.085536923187668
复制代码
# i& M# q* @' e2 c, Q
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1$ h( s1 e: d! k' ?9 @, |
  1. #返回math.e的x(其值为2.71828)次方的值减19 @3 E* @! y$ k0 V4 k
  2. expm1(x)
    ( I2 u* ?! B: \0 ?' D' Z
  3. Return exp(x)-1.7 W7 D1 U' j7 r) L/ w
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. n; n/ T1 [, i0 L" x0 Z% L7 ~; Z: Z

  5. * M( N# Z! x$ R& T' Y8 V. X& W0 J
  6. >>> math.expm1(1)
      k5 |# J! Z& E/ t5 I1 \- z7 C% w
  7. 1.718281828459045; u# L) _6 u6 `7 d, s* X& _
  8. >>> math.expm1(2)* ]( Y9 `7 _( r2 x' I' k9 _# v
  9. 6.389056098930650 k% J8 r3 j0 d& |) J& k; r9 P% W% c
  10. >>> math.expm1(3)
    ! a8 M& W! f* ^3 t4 b; w% D
  11. 19.085536923187668
复制代码

0 L* S& u/ ~: s" L/ jmath.fabs(x)  返回x的绝对值
: [! `. t% z. K7 O- @6 I6 C
  1. #返回x的绝对值  q  J/ m6 p& }7 Y3 \+ J
  2. fabs(x), O% q% Q/ e( |& a
  3. Return the absolute value of the float x.
    : D2 a) _) \( ^/ C- q$ P1 x

  4. + M% P/ K4 I6 N+ S$ [7 w
  5. >>> math.fabs(-0.003)
    9 H. p* u: W; @! J; E4 v  w' K: N
  6. 0.003, T7 H" f% j6 e* t9 V, Q
  7. >>> math.fabs(-110)
    7 H6 A/ ]+ a- D
  8. 110.0, x0 z- Z# t$ L6 l/ P8 b# C  ?0 B6 Y1 k
  9. >>> math.fabs(100)$ h" q3 Y( n9 R+ x7 s
  10. 100.0
复制代码

) l! [" [  a1 ~; i$ d* ~5 U9 b& \8 ?math.factorial(x)  取x的阶乘的值5 ?8 ]& T% z/ F" l$ G% M; N* m, W
  1. #取x的阶乘的值
    5 \; {1 e$ [% s5 x+ @
  2. factorial(x) -> Integral- w7 v# ?  }" O
  3. Find x!. Raise a ValueError if x is negative or non-integral.# ^: g8 [% t* m3 Y2 l) V) _
  4. >>> math.factorial(1)) P  _) X$ I" S! x8 j
  5. 13 c  s, z& o4 {1 |. l; r7 ^4 `; v
  6. >>> math.factorial(2)9 Z* v7 Z( }& B
  7. 2& \, s9 @5 }% r* o7 b7 a6 I
  8. >>> math.factorial(3)* f! L1 Z" c0 q, E. e. N% x
  9. 6
    ) X  m7 V+ L# M& n) k
  10. >>> math.factorial(5)  }! u% d+ O+ w( @5 F4 x
  11. 120! q$ C+ K, z5 f+ c
  12. >>> math.factorial(10)3 ~& E, J& H; E' H: O6 W- I
  13. 3628800
复制代码

" O. g' t3 j4 Z8 wmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
! O. }7 j- ^0 j, X* z
  1. #得到x/y的余数,其值是一个浮点数, y, q% w9 s; g* Q, d$ `% H0 @1 W
  2. fmod(x, y)
    8 i! k2 Y% s. O1 {" P
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    2 W$ ]# l' Z0 V, @* J  h, ?
  4. >>> math.fmod(20,3)  ^' \0 a6 f& _! w* B
  5. 2.0
    1 K' y/ N. I) d6 s, Z: X. }! t, t
  6. >>> math.fmod(20,7)
    " K" \+ Y2 ?0 G* N
  7. 6.0
复制代码

1 J. I4 t7 |, G. t& t+ Rmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 i) c3 R+ m# ?& c1 p7 |
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ( D' `/ \. b3 t
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值+ Z: n  h3 V4 T/ Y
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 y9 W$ e3 B; M. T4 F8 c2 j
  4. frexp(x): j* s# q% T! ]/ ^  q2 }' q4 ?4 Q
  5. Return the mantissa and exponent of x, as pair (m, e).
    " G$ V# ]$ y4 h$ @2 k. `
  6. m is a float and e is an int, such that x = m * 2.**e.
    " W5 X$ D  ?# [# r4 L  N
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.% l. a; v  U: c% n+ m! P0 x
  8. >>> math.frexp(10)* b/ }- U! M9 ?1 G9 ?: w8 p' E
  9. (0.625, 4)
    8 j( S9 E0 N: w4 x/ I/ ?! B$ _+ F
  10. >>> math.frexp(75)
    - }5 X- d" U0 P2 W7 Y. K5 j/ _; J
  11. (0.5859375, 7)( v# h$ f( v; K2 J1 X0 X) j
  12. >>> math.frexp(-40)
    5 _; i6 w$ O2 B7 N- o. W
  13. (-0.625, 6)+ q; v. U$ B- w+ I% {: N/ e9 ?
  14. >>> math.frexp(-100)
    & j, J* Y0 N2 y4 |1 U: L
  15. (-0.78125, 7)
    : A/ ]- x" V) Z- V4 {
  16. >>> math.frexp(100)7 t1 V4 v* b+ B8 m
  17. (0.78125, 7)
复制代码
, \/ @' T. j: |2 [# P) l. H( V
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
0 Q, n+ q7 ~+ E6 Z& o
  1. #对迭代器里的每个元素进行求和操作
    : P  z) B3 r& e; n- E
  2. fsum(iterable)" v% _0 d9 r% B7 Z  P' j* u
  3. Return an accurate floating point sum of values in the iterable./ q/ V* g$ i+ t, n( G  Q7 |( J: Z' c
  4. Assumes IEEE-754 floating point arithmetic.! e# I& F* h  e' H3 _. z& a
  5. >>> math.fsum([1,2,3,4])% s% F4 D! H6 T* t. g1 F
  6. 10.0
    1 b) z  p4 i) Q; Z" T) j, a6 d
  7. >>> math.fsum((1,2,3,4))3 }( [$ h: W0 `% M/ r- w2 l
  8. 10.0
    1 u. D( g7 U( h4 I. B1 ]9 R
  9. >>> math.fsum((-1,-2,-3,-4))
    1 B% f' h" k- I) g
  10. -10.0
    2 h# ^2 \" I3 e) a5 [% f/ p
  11. >>> math.fsum([-1,-2,-3,-4])! L! Q9 Z' C; T7 y! U* c
  12. -10.0
复制代码
$ {( G! q7 K& E9 W" ^" [  A
math.gcd(x,y)  返回x和y的最大公约数
9 L8 u( P9 [8 r- v
  1. #返回x和y的最大公约数
    ; n1 f' s; K5 z; Y; O
  2. gcd(x, y) -> int- Q% m3 o( p' ?* l% D6 W; m0 X
  3. greatest common divisor of x and y  t% R- W9 s  l5 j. ]7 E  u
  4. >>> math.gcd(8,6)
    ; W- X  E3 p: x# t+ F6 ~
  5. 2
    / N+ Y" j5 q0 g3 t0 q$ p
  6. >>> math.gcd(40,20)
    4 m& _) v: C7 K; n8 q# `2 j+ V9 w
  7. 20
    2 p) g( o9 g, S
  8. >>> math.gcd(8,12)( V6 x$ o  K$ D. ]7 N; m1 e
  9. 4
复制代码
1 O( U& p( ^) c
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False  z- J" E- T1 o8 C
  1. #得到(x**2+y**2),平方的值
    4 k9 q9 _3 Y: s# e4 X3 l+ r' F
  2. hypot(x, y)
    3 b7 l/ v' m- H, N' \) }& i  u* v
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    % w4 G, \9 f- z( K. c4 N
  4. >>> math.hypot(3,4)! w# u  z& \' y; s# s9 t
  5. 5.0% {" I# ^0 M6 p* |: v) _
  6. >>> math.hypot(6,8)9 f4 ^$ T% l  p4 _8 X" b; M+ n
  7. 10.0
复制代码
1 x/ V9 O* S9 |1 f6 s+ V- P3 C
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False+ y+ |5 d) K$ W/ |( c$ d) g
  1. #如果x是不是无穷大的数字,则返回True,否则返回False7 G0 a( f; g: F8 ?- w5 F
  2. isfinite(x) -> bool, r3 t8 C: ]  L7 Y, ?2 B/ d
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    # s" j+ m2 F2 ?; m
  4. >>> math.isfinite(100)
    8 f% h9 _7 W+ b' s# m
  5. True4 c9 I5 w/ M0 V$ j/ p* p, L- A
  6. >>> math.isfinite(0)8 L' O6 {  u+ ?+ A; V$ J- F: U
  7. True- i8 [7 w' }) j2 H
  8. >>> math.isfinite(0.1)
    + z1 U- Q# e8 C; l" C
  9. True, N  w, D1 {3 \3 m; O6 A
  10. >>> math.isfinite("a")
    " \+ ~, O0 k& u
  11. >>> math.isfinite(0.0001)
    5 i0 }, j& i8 ~- _/ s& ~
  12. True
复制代码
! i. |" t) T. q5 J6 p- r
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
% R2 ^7 R( w* _% t; G; c/ M) P) y
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    # |1 T' {3 N5 \) P
  2. isinf(x) -> bool# f% d) g- a) m; i7 M6 E1 R1 Z% ~
  3. Return True if x is a positive or negative infinity, and False otherwise.
    - X8 r+ ^% R1 @# H4 R
  4. >>> math.isinf(234)
    ! T- ^! j% r" h6 k" A! l+ {
  5. False7 p6 Y) Z1 G$ o/ s' h. n
  6. >>> math.isinf(0.1)
    5 c  j6 w8 d+ b# n
  7. False
复制代码

2 R$ p8 N& ^$ G2 @3 k& Lmath.isnan(x)  如果x不是数字True,否则返回False
  K/ M3 r3 E& K. b- X4 l
  1. #如果x不是数字True,否则返回False
    # o) Y& g- e  x. N1 K3 q/ M7 G8 \( O
  2. isnan(x) -> bool7 O8 {7 m3 d4 b% ]! p4 E0 Q* {
  3. Return True if x is a NaN (not a number), and False otherwise.4 |' D: D  f& j, ~' _  D: p. A
  4. >>> math.isnan(23)) ^! L( J9 b/ W! @% i
  5. False& K% @: t; }; ]6 ^- E6 E1 V7 @" O
  6. >>> math.isnan(0.01)
    1 I% `  e2 i, m( H$ v' |
  7. False
复制代码
) S9 |' H. D1 D8 x: P: l2 I5 H0 e0 x
math.ldexp(x,i)  返回x*(2**i)的值
. g( o# w4 B1 J+ F0 @
  1. #返回x*(2**i)的值
    , D. w' I: M! h4 g% n) T
  2. ldexp(x, i)
    6 _$ b4 s% n6 I8 z' S9 U, V
  3. Return x * (2**i).
    : }+ P7 ?/ r2 m/ t, n
  4. >>> math.ldexp(5,5)
    4 P) |. |9 H4 z; y; q
  5. 160.0
    ' y2 P, z) C( `- v, h0 j9 Q
  6. >>> math.ldexp(3,5)
    3 d1 d9 r& _  B2 ~8 V7 j% ^# y
  7. 96.0
复制代码
: g( }' T" r- d; @/ Y# G
math.log10(x)  返回x的以10为底的对数
0 \; }, p* `" ]& ~( z" v
  1. #返回x的以10为底的对数% \/ w! Z' w; S4 `/ p
  2. log10(x)9 z- u) ~; r8 o" J
  3. Return the base 10 logarithm of x.
    , B- u8 E# J+ u/ f4 y
  4. >>> math.log10(10)& `+ B/ R  H) s
  5. 1.0/ Y6 i! |. N( G! B4 A
  6. >>> math.log10(100)
    2 ?( c/ i& Y3 }2 E1 P" x" G# j
  7. 2.0
    9 M5 i0 Z  `7 W; N% R9 }. I
  8. #即10的1.3次方的结果为20
    * }% l) M" X. y
  9. >>> math.log10(20)# L# ^: l1 O5 U7 B
  10. 1.3010299956639813
复制代码

9 Y" R  E; }- j2 N/ Kmath.log1p(x)  返回x+1的自然对数(基数为e)的值
. T" Q$ _7 N! k5 J" [" O
  1. #返回x+1的自然对数(基数为e)的值
    5 v8 b2 G0 @1 _& B6 R2 B
  2. log1p(x)
    * }. f# b4 g$ o- q5 w) T
  3. Return the natural logarithm of 1+x (base e).3 H" S  a& m/ j& d8 @
  4. The result is computed in a way which is accurate for x near zero.
    ; {  C! N3 |5 {* f
  5. >>> math.log(10)
    & g8 U2 T. Y5 o' E
  6. 2.302585092994046
    + O1 x# s: {  _' s8 C3 l: |
  7. >>> math.log1p(10)
    . |- n5 x2 R5 j; A' v, o
  8. 2.3978952727983707
    * K! i8 h; c- E
  9. >>> math.log(11)
    ! g1 M5 o- l4 _6 A6 N: y
  10. 2.3978952727983707
复制代码
1 o' X* r4 o8 e% u
math.log2(x)  返回x的基2对数
2 w9 N5 H5 K- _5 L
  1. #返回x的基2对数$ H6 @" `- f- ?* F7 z( ^4 H
  2. log2(x)- [- U" P* b# \) A9 I' Y7 z' R
  3. Return the base 2 logarithm of x.
    5 g' V' `+ Y* D" U: Y
  4. >>> math.log2(32), X  Y. q, u4 s% V0 x. G* S% x
  5. 5.03 |( U1 {  Z0 T, g
  6. >>> math.log2(20)& c" `6 n: J- }7 f. J
  7. 4.3219280948873636 K  J9 E& ]$ V' ?+ m
  8. >>> math.log2(16)% d4 K/ H, S& e- x# l
  9. 4.0
复制代码

: B; e$ Y8 Q( O4 Amath.modf(x)  返回由x的小数部分和整数部分组成的元组2 Q' `" u7 n: W  P/ e
  1. #返回由x的小数部分和整数部分组成的元组( |- m! L1 J* U) I# T2 a$ S# K0 v
  2. modf(x)
    & P. B* K4 ~# n5 L3 M- N
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ' r$ {0 W$ M  C4 j: v( g3 Q4 Z
  4. of x and are floats.
    . Q! S$ j1 G7 f) s" ?
  5. >>> math.modf(math.pi)6 N, v2 C0 V" ~$ [4 `8 R% a3 P( R4 B
  6. (0.14159265358979312, 3.0)5 s" Y! @- F0 A: N
  7. >>> math.modf(12.34)$ b  B7 H, x. N3 P+ l
  8. (0.33999999999999986, 12.0)
复制代码
' s* K+ L: @9 `$ m6 Q) }  K, d
math.sqrt(x)  求x的平方根( D: T% S9 q. t
  1. #求x的平方根5 t) |% T7 \& C$ [% a
  2. sqrt(x)2 V% x6 b: [7 m3 r' Z+ Q
  3. Return the square root of x.- C$ L+ t/ F6 R( h' y2 @5 r& ~
  4. >>> math.sqrt(100)
      t+ q0 x! t" p# n- m8 u7 |
  5. 10.0& T3 }& d: I! J3 {% T2 P& d8 U" I
  6. >>> math.sqrt(16)
    9 f0 ^# \! X' n: q# g6 r( h
  7. 4.06 I" ?% J) z& g, c
  8. >>> math.sqrt(20)
    ) Z0 O6 Z; _8 B$ x
  9. 4.47213595499958
复制代码
( x3 u* K$ q" v  ^
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分' y. J5 Q7 a$ m, s# q
  2. trunc(x:Real) -> Integral( y! I6 `/ `+ P; c4 f( U
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    . P6 o8 R  l% }. B: D
  4. >>> math.trunc(6.789)
    & V8 W/ x4 e1 r/ K
  5. 6
    , r# M7 h/ C. S' d7 L* S7 D# r5 T
  6. >>> math.trunc(math.pi)
    % W+ L. ]8 r. X: L/ c+ H
  7. 3
    2 k0 Y1 `0 T7 k2 J( D% [& v
  8. >>> math.trunc(2.567)
    ( o7 `7 P- P8 ~  r3 i
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-25 10:59 , Processed in 0.114231 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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