新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

" V. o5 E! I1 U9 h( b; k【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
0 t7 g- v4 I# D8 S, v

- H- B/ h! b9 w8 X" [. y% o. q方法1
; X: w6 j& ]! T; R( J, J- b
  1. >>> import math
    / X+ k- o4 _+ `  `6 T
  2. >>> math.sqrt(9)
    4 u  g7 m7 ~( Q# r# l! f0 U
  3. 3.0
复制代码
方法2
7 d: r# [) n9 u$ l' \4 M
  1. >>> from math import sqrt# D$ I- n2 Q, R$ \. o/ L4 ~7 H
  2. >>> sqrt(9)
    . r: q4 o) O/ D) _) a2 ^- C
  3. 3.0
复制代码

! N+ \5 ?! U6 B/ o, @. [( J) l

0 j- N) d( `! h. h5 L& W
math.e  表示一个常量
* H0 s$ G; `8 X/ |% M# E/ N
  1. #表示一个常量
    * e* n' i5 I  I' z6 v
  2. >>> math.e6 C8 G: F1 x# M# ?
  3. 2.718281828459045
复制代码

2 ]  q0 w& y$ n! u# ^: e/ b3 R& v! Qmath.pi  
数字常量,圆周率
/ F( n8 R8 U9 o5 E* ^+ b
  1. #数字常量,圆周率' P4 h3 f8 T- C7 h. |
  2. >>> print(math.pi)
    . S. X2 |& u0 p/ a+ F, H
  3. 3.141592653589793
复制代码
- {2 U! Y( |9 D9 }6 \2 ?: k3 e$ o
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 ]4 F0 O- C+ Q) q
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x1 j3 }. o- H' k, A5 }2 k& Q0 h, L
  2. ceil(x)% q+ P5 \. g  z- u% t
  3. Return the ceiling of x as an int.% l. E9 Y. p9 T, W
  4. This is the smallest integral value >= x.
    8 C# M9 m5 t/ _5 U

  5. / [0 |6 Z3 d& _# ]! U5 a% E
  6. >>> math.ceil(4.01)' [- _8 p6 d( B* G  |8 b
  7. 5* N9 J5 b6 a  l  ^7 m: o: e' K& V
  8. >>> math.ceil(4.99)/ ?: E& t6 ~* s" G) f- S; p
  9. 56 }' `" g4 F8 I$ F) j
  10. >>> math.ceil(-3.99)) }; j4 \: a/ o
  11. -3- g& y3 T7 Z5 P8 s6 p
  12. >>> math.ceil(-3.01)& y! i" Y4 H% t- T
  13. -3
复制代码
. A1 l: z+ I! W+ Y( H. H
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 m; }2 i+ ~; @% X3 R
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ) c% L) K3 R1 y5 @& F' O
  2. floor(x)
    , u1 ^8 x$ f" H+ R
  3. Return the floor of x as an int.; F/ t1 }4 e' P' }
  4. This is the largest integral value <= x.- Q& z6 i8 K; E4 G4 o- w% k
  5. >>> math.floor(4.1)
    3 y0 D1 U5 I8 }
  6. 4: m8 E2 \& H# Y& h% @
  7. >>> math.floor(4.999)8 r9 T" M; b! d4 s
  8. 45 R; f, @- r2 C0 m
  9. >>> math.floor(-4.999)2 X1 K" V9 D, |$ L! |: f: H
  10. -5
    * |8 f& w3 ^4 e: [
  11. >>> math.floor(-4.01)8 L  S" p' z# N( i$ s+ r. u+ f
  12. -5
复制代码
. `; a5 k2 D4 i! I9 o: d$ Z5 g
math.pow(x,y)  返回x的y次方,即x**y2 a! ]: q4 O) C4 k. `/ O
  1. #返回x的y次方,即x**y
    : A8 S& f5 u/ |# j  z$ P
  2. pow(x, y)9 i) f' q: B$ B/ @
  3. Return x**y (x to the power of y).9 T, ]! G  Y1 E
  4. >>> math.pow(3,4)
    2 v0 N. ?  _* [: S
  5. 81.0
    " b: W2 v5 y7 I1 L0 z
  6. >>>
    # Z& a/ ?9 s( _5 l
  7. >>> math.pow(2,7); I& l  x1 x  X1 P) c
  8. 128.0
复制代码
  w% F4 l! t/ a7 y& [6 g6 c
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). T! G5 I0 I/ e, C' y, ?; r# ?
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ G/ U5 U; x- o9 \2 R0 s
  2. log(x[, base])
    7 [4 B( D1 C+ ]0 g; o: D
  3. Return the logarithm of x to the given base.
    & w7 ~# C; @6 q2 e1 R
  4. If the base not specified, returns the natural logarithm (base e) of x.
    6 S; m& J/ [2 ~. \0 o) O; V
  5. >>> math.log(10)  f( T$ ~5 t7 w2 }% A
  6. 2.3025850929940465 S+ v$ s( B; c  q! g
  7. >>> math.log(11)
    5 I5 }+ k% z- r4 B9 y- h% h
  8. 2.3978952727983707' Z- z; {  U9 I" ^1 |" L
  9. >>> math.log(20)
    ) P( D, {" ?7 i9 w; T7 b7 r. W
  10. 2.995732273553991
复制代码
! J9 i% E7 K$ _
math.sin(x)  求x(x为弧度)的正弦值
* P; r, L1 i: ?
  1. #求x(x为弧度)的正弦值
      _0 l3 k* J! A$ @3 U" g& e& t
  2. sin(x)5 Y. k% L' ^* ?
  3. Return the sine of x (measured in radians).# s+ f( k$ r8 }3 ~$ J8 ?7 g) V3 K
  4. >>> math.sin(math.pi/4)
    ( h4 d; P/ n* j0 M) U. l0 z& B
  5. 0.7071067811865475
    3 S" C- K9 t( s
  6. >>> math.sin(math.pi/2)& h$ l4 i; \2 S9 r2 N% ]  e1 I
  7. 1.0
    ( y% O% x1 Z# S' H. M6 @
  8. >>> math.sin(math.pi/3)
    " m' E4 r1 }8 D1 t) ^0 `, N
  9. 0.8660254037844386
复制代码
& b. o- C  [; I: m) C: @
math.cos(x)  求x的余弦,x必须是弧度: w2 ^) F* @+ m  y) F6 N6 @
  1. #求x的余弦,x必须是弧度
    + Z% E- N+ G$ N- U, h8 A
  2. cos(x)- w7 |+ L% J) P) D
  3. Return the cosine of x (measured in radians).
    * K( q7 F# M4 p
  4. #math.pi/4表示弧度,转换成角度为45度
    " |+ s3 H) J( u+ `' g* v! F* C
  5. >>> math.cos(math.pi/4)
    ; g( i" |2 {' {- P% w% \
  6. 0.7071067811865476
    0 F( s; M- d  W
  7. math.pi/3表示弧度,转换成角度为60度2 U2 e' V4 d+ Y' M8 T) a: C
  8. >>> math.cos(math.pi/3)$ l# J! b$ R* L& m
  9. 0.5000000000000001, Y3 E$ f2 Y( f' s
  10. math.pi/6表示弧度,转换成角度为30度
    - ^- I! E# o/ a( o3 [2 H9 V0 t
  11. >>> math.cos(math.pi/6)+ T$ `2 }  `0 O# r# t
  12. 0.8660254037844387
复制代码

" i( x- Z% |  q9 n& d5 [1 Xmath.tan(x)  返回x(x为弧度)的正切值
2 {% N' K# J7 X9 G6 V) l
  1. #返回x(x为弧度)的正切值* l' c& Q" j* n- U
  2. tan(x)
      T* ?% y6 a7 e+ K# S
  3. Return the tangent of x (measured in radians).
    ; b; ~3 o+ \4 A* a5 V( q
  4. >>> math.tan(math.pi/4)
    ' Q. c/ _) C, b% S  Q. h6 J
  5. 0.9999999999999999
    " a6 M. r6 P2 o5 ?2 f2 i+ {8 }
  6. >>> math.tan(math.pi/6)& F$ g( e/ P3 K+ Q. ^8 x2 j: r
  7. 0.5773502691896257
    5 j, }* V) U0 {* L
  8. >>> math.tan(math.pi/3)6 b" @7 C; p; ~
  9. 1.7320508075688767
复制代码
; L1 M; V* q' o
math.degrees(x)  把x从弧度转换成角度: }! X: o8 z; s, O: H5 h6 z6 q
  1. #把x从弧度转换成角度
    . ?7 O0 M  `0 X; |' p
  2. degrees(x)5 K. g  j' P7 w* l
  3. Convert angle x from radians to degrees.
    $ d4 q2 E& {# f/ j$ R0 w8 W" {

  4. & C; W8 Y, d+ O7 K0 H9 N( g
  5. >>> math.degrees(math.pi/4)
    - H( r6 o7 i% L
  6. 45.07 e8 e; k( R9 S; v
  7. >>> math.degrees(math.pi)
    8 O4 V% d3 N8 u2 L: N' G
  8. 180.0, W0 V) `. {/ I
  9. >>> math.degrees(math.pi/6)- Y% a$ e3 |3 C
  10. 29.999999999999996" ]: ^6 c( ]% U3 A' ~8 M
  11. >>> math.degrees(math.pi/3)
    $ h8 g* Q  ^8 a8 Q9 q
  12. 59.99999999999999
复制代码

" O: F7 X7 [: E1 A+ L9 {math.radians(x)  把角度x转换成弧度
0 r+ H$ |) w6 J$ T5 |/ @4 _, B0 {
  1. #把角度x转换成弧度
    ' o" h1 X+ [1 H& N. |: }
  2. radians(x)
    - H7 K, q, n! w
  3. Convert angle x from degrees to radians.
    ) G8 T* l' W- A
  4. >>> math.radians(45)
    0 L5 B# E3 z* v: r& Y
  5. 0.7853981633974483$ n! ~! _* N9 ?4 _4 j! w
  6. >>> math.radians(60)
    6 x, |8 A; Z9 d3 W0 ^' J# R
  7. 1.0471975511965976
复制代码

1 J- X! S2 S0 S  X: \/ imath.copysign(x,y)  把y的正负号加到x前面,可以使用0
2 M' P7 ^, w) t: F
  1. #把y的正负号加到x前面,可以使用0  S5 X: U: g$ G* `: h* e, t
  2. copysign(x, y)
    7 }  J, M. R) y; S2 Y; K, H! H
  3. Return a float with the magnitude (absolute value) of x but the sign . z- H5 V# g9 c9 ?" A: r8 _* `# p5 `
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 Y9 G0 p' q2 U$ S0 w
  5. returns -1.0.
    ! @( G4 G. ^- z7 h
  6. 4 q3 R2 |, ]8 B4 y. M
  7. >>> math.copysign(2,3)$ G! ]: u' j, Z0 L/ d* T3 x& M
  8. 2.0
    , Y3 c: h* u9 v! `: L
  9. >>> math.copysign(2,-3)3 t/ ^( n" I  k& Y" X
  10. -2.0& p% R! Y1 B0 L
  11. >>> math.copysign(3,8)
    ( I. j8 A! t% E& M. z/ O
  12. 3.03 V( j% t) Y. L5 Y9 I
  13. >>> math.copysign(3,-8)
    4 m' N$ p* I1 b$ ^9 K' E1 v
  14. -3.0
复制代码
6 Q. U% A9 b# M" M( i- l$ a
math.exp(x)  返回math.e,也就是2.71828的x次方
6 `/ ]; U4 S& }
  1. #返回math.e,也就是2.71828的x次方
    5 L" X' ?& V. B& ?
  2. exp(x)
    * o' ?, n/ d1 _4 z' m! `
  3. Return e raised to the power of x.
    6 F" ^: d) a) S& {; z

  4. ) u3 n4 h/ W, |$ P/ }. e
  5. >>> math.exp(1)5 _% w# D2 x' l! `) `6 }9 x
  6. 2.718281828459045
    3 c; }# m+ s* F& t; Q: V3 \. T: F
  7. >>> math.exp(2)/ k2 K* U6 `" L
  8. 7.38905609893065
    + b3 O* n) w5 K1 c
  9. >>> math.exp(3)4 x  V/ Q6 x+ v0 a7 ]
  10. 20.085536923187668
复制代码
4 C! I( l; E9 w, F
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减11 H' H  _: f# G+ }+ B9 g
  1. #返回math.e的x(其值为2.71828)次方的值减1/ D: _4 ?) t( G
  2. expm1(x)
    - H4 u9 v# ?$ S8 N1 @7 ]2 q  m; M
  3. Return exp(x)-1.
    " j% z" V% E* f  }3 |4 e# z% U1 m7 ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    . w$ K3 e+ |: c9 @

  5. : F  h: B4 R8 X5 K; N. p
  6. >>> math.expm1(1)
    + E  L7 O. g. r2 k8 F& J- Y& x
  7. 1.718281828459045
    9 z- V( M: I+ O- }9 G! ]
  8. >>> math.expm1(2)5 B3 Z1 H3 R4 q2 d3 z
  9. 6.38905609893065, _1 F( }3 B; g$ P" v0 y& F0 i
  10. >>> math.expm1(3)* i* L, w9 C) B" u" R
  11. 19.085536923187668
复制代码

  O3 \1 F  ^; O' p( imath.fabs(x)  返回x的绝对值
6 x( f$ d/ E7 w# ^+ H
  1. #返回x的绝对值
    5 t8 I' ], x" p9 o: D8 _
  2. fabs(x)
    * z, K+ }5 R3 G7 m/ B
  3. Return the absolute value of the float x.
    5 O! K) _8 M& _# ?: F
  4. . v+ w- W3 W3 H' C
  5. >>> math.fabs(-0.003)! G2 q9 P% D/ Y5 A/ h6 L) M6 v
  6. 0.003
    + `* _8 l" O* F$ u% r' b/ I
  7. >>> math.fabs(-110): p: i% |. |1 T
  8. 110.0
    % B$ H/ s4 n4 T; g" ~4 h+ a0 o* f
  9. >>> math.fabs(100)4 S% X& r/ G1 @3 c  ^
  10. 100.0
复制代码

) N3 ~4 N' S. pmath.factorial(x)  取x的阶乘的值
; b- y" _& Q6 S& B6 M. j# f1 i7 O
  1. #取x的阶乘的值
    5 C8 N6 L/ p7 r& F8 n
  2. factorial(x) -> Integral
    & [, d0 Y2 f$ M  k( ]
  3. Find x!. Raise a ValueError if x is negative or non-integral.# L8 R4 r" k' g: H8 G5 Q" v
  4. >>> math.factorial(1)6 Q6 S0 X" h6 q4 Y
  5. 17 C$ I" U3 z* {. l. k. q1 m
  6. >>> math.factorial(2)+ o6 V% v/ x, s7 W# B( Y
  7. 28 P) u4 B1 X: Z, h. Q( B# e
  8. >>> math.factorial(3)
    3 M. U" B5 y! W4 H; f  i
  9. 6
    / s: f( |6 ^" `6 C
  10. >>> math.factorial(5)& B6 F3 ~: X' |$ h
  11. 120
    1 p. U. U) ^" ?) `
  12. >>> math.factorial(10)
    , T2 M* i  z. [; X6 [
  13. 3628800
复制代码
1 l, @1 i* S" x; P- U! {" K
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
3 u- q$ n* E. B3 ^7 j$ I
  1. #得到x/y的余数,其值是一个浮点数4 \. K* f: V5 Z
  2. fmod(x, y)# t  `- J3 i, _5 g7 b
  3. Return fmod(x, y), according to platform C.  x % y may differ.3 B. D  V4 l5 j1 V# X5 I+ m6 ?. I: l- M( m
  4. >>> math.fmod(20,3)
    2 m5 {: K+ l' c9 v- G; z5 c5 q: J
  5. 2.05 m: S! g! T) ~9 H
  6. >>> math.fmod(20,7)
    3 V# r( T( Q2 S! V
  7. 6.0
复制代码

2 V5 V" m1 N% _. jmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围2 J: V4 f) d* R
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* E; X- Q* z7 u+ s0 ]9 s
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    " e# ~+ F) P4 n
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    & U# |: u6 \7 s0 ^  s
  4. frexp(x), \* s1 `$ I: @# i! v
  5. Return the mantissa and exponent of x, as pair (m, e).
    8 p$ E# i2 C; E6 |2 d  [( W- |
  6. m is a float and e is an int, such that x = m * 2.**e.  D/ `0 T; K  n& ]. Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.0 n, k; r" U2 d
  8. >>> math.frexp(10)& w/ @0 m8 X1 q0 Q# n" p/ [1 Q: S
  9. (0.625, 4)
    " `8 @& y7 b- Y
  10. >>> math.frexp(75)+ T, z1 b# F( A: Y1 E; d+ w
  11. (0.5859375, 7)
    / Y) R+ _$ V4 Q4 ^
  12. >>> math.frexp(-40)3 g5 e- p. q7 u/ d4 N+ M9 r
  13. (-0.625, 6)
    8 Y  ~# ~8 d' z) a
  14. >>> math.frexp(-100)
    * {: Z" b! D, Y1 E6 R% J! l9 I4 U
  15. (-0.78125, 7)7 z! N; P2 A+ \" n3 S- O! O: q& `
  16. >>> math.frexp(100)+ P) W4 z' G0 n1 D8 d9 a
  17. (0.78125, 7)
复制代码
" t* M, |- A- T0 w) e. A
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列4 p: u) ]( x9 Q& a6 i( ?% g  `" |
  1. #对迭代器里的每个元素进行求和操作
    * K" Z0 [' C: u
  2. fsum(iterable): D9 y1 q) z, }2 p
  3. Return an accurate floating point sum of values in the iterable.& {* F! W: ]  S1 D. r& k
  4. Assumes IEEE-754 floating point arithmetic.' J" L% O8 o# |' x, }1 }8 A9 V5 F
  5. >>> math.fsum([1,2,3,4]), G/ n1 q  u3 r8 A1 K
  6. 10.0
    * D5 S4 s$ a( b# r0 ~3 o) x
  7. >>> math.fsum((1,2,3,4))
    ' ]/ x! `2 Q/ v9 g
  8. 10.09 ^; G, o5 G  R- r4 L4 p7 R
  9. >>> math.fsum((-1,-2,-3,-4))+ Q% d9 F( u* T  L. q, ]
  10. -10.0% d& m. w5 w) j; w
  11. >>> math.fsum([-1,-2,-3,-4])
    1 X# f- O5 V- F+ O
  12. -10.0
复制代码

1 a* J3 e- K# d* Z$ jmath.gcd(x,y)  返回x和y的最大公约数) F. N  x4 ^* f1 y% g$ e- g; }6 n8 s! X
  1. #返回x和y的最大公约数. \! m% P4 G: K4 ?8 E9 X' {
  2. gcd(x, y) -> int3 \4 }8 X- q& m0 c" n/ r
  3. greatest common divisor of x and y7 @$ j' J% g: n7 R1 i. d# I& _
  4. >>> math.gcd(8,6)
    # [" c8 Z9 o( h! k
  5. 2
    ' @$ W, [/ N; Y! B# C; }# m+ [
  6. >>> math.gcd(40,20)$ y( t7 |" I, o8 N% ~: j9 `
  7. 20
    $ K$ j) l! V- H' D7 D( Z- i- E7 M* m" p
  8. >>> math.gcd(8,12), i/ L6 l7 f; {* j
  9. 4
复制代码
8 {7 n  _8 b3 A1 l; e
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
' `3 w) Y% u, u6 U  C' u! F1 e
  1. #得到(x**2+y**2),平方的值5 G+ [. I6 u! ]
  2. hypot(x, y)3 ~" H) X# `$ O
  3. Return the Euclidean distance, sqrt(x*x + y*y).
      |1 O0 ^3 ]- D
  4. >>> math.hypot(3,4)+ z4 D6 D. I' a
  5. 5.0/ M+ y. U0 |( o, W& k$ U$ w
  6. >>> math.hypot(6,8)
    4 Z: N( M1 C8 k0 x/ G
  7. 10.0
复制代码
7 n: a3 u# Q& a  f
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False6 Z8 K. F, X; l! G* E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % S) _1 g; A* Y# ~! J
  2. isfinite(x) -> bool" i: T7 ]. F. l$ Z) z
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.9 }0 r$ A' m6 k7 u
  4. >>> math.isfinite(100)
    . x+ c& K" w% _: z, I( m# v( }
  5. True8 I# |8 F! j, b# O
  6. >>> math.isfinite(0)$ _: x! b6 H( W$ p4 Z
  7. True
      O, S; w- V7 p7 M% M
  8. >>> math.isfinite(0.1)6 b' _" {; k2 a/ W" }
  9. True8 l6 O6 c7 @) g
  10. >>> math.isfinite("a")* c; i1 d5 s% X: Z
  11. >>> math.isfinite(0.0001)
      f  q2 S% }2 x/ D2 K
  12. True
复制代码
+ u2 O5 d% S8 k
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
; \; U' p9 {% k7 J- A
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    / D4 h4 A& j* c* q. L. q0 M
  2. isinf(x) -> bool
    - y) a% o$ |# |  ?
  3. Return True if x is a positive or negative infinity, and False otherwise.5 p/ d7 p! E$ B
  4. >>> math.isinf(234)
    6 @* g  U' ~* H' [, e) Q
  5. False
    , w; R3 S4 Y0 i0 M/ t2 J$ ^9 {4 a
  6. >>> math.isinf(0.1)
    - U2 K5 q8 X3 s. e/ [
  7. False
复制代码
7 ]6 f7 q" e; |* v
math.isnan(x)  如果x不是数字True,否则返回False7 ^/ r+ A; K) Z3 h% z0 e: L- e3 u, z, a
  1. #如果x不是数字True,否则返回False
    : l' B' {! m1 H% x
  2. isnan(x) -> bool  M- I# d# Z  k) ]$ A
  3. Return True if x is a NaN (not a number), and False otherwise.; ?5 d& E/ h. f+ s9 @( U- X
  4. >>> math.isnan(23)
    9 O6 n3 B- T- n3 h* ?
  5. False! u. q5 A5 g. y" ^2 W0 {5 O+ R
  6. >>> math.isnan(0.01)
    6 e/ e  y7 l) i( c
  7. False
复制代码
0 N, D/ R2 b4 b7 t9 X. P
math.ldexp(x,i)  返回x*(2**i)的值) e6 X& C: q5 z& f) ~; O; B2 F, U
  1. #返回x*(2**i)的值
    % J0 |" Q* w* y4 S7 n6 b
  2. ldexp(x, i)
    ; d- v: Q3 V1 E0 S6 C
  3. Return x * (2**i).5 Z: h8 W5 q7 Q; \3 X, K: S
  4. >>> math.ldexp(5,5)
    , W1 J" p$ M/ r" @% t
  5. 160.0- G3 ~4 S9 o' r: x4 Z! _0 t+ R
  6. >>> math.ldexp(3,5)- m. Y* C8 D5 d2 k" v& B
  7. 96.0
复制代码
8 A4 L8 a, Z' Y
math.log10(x)  返回x的以10为底的对数7 |  {7 n7 k& t; L
  1. #返回x的以10为底的对数
    9 \; N2 \& O( Y
  2. log10(x)
    $ [1 o2 T- J$ {1 x0 T/ t) H
  3. Return the base 10 logarithm of x.2 w2 F0 k; v8 \; h" l' L/ ^' |
  4. >>> math.log10(10)
    1 v7 o7 r5 N: u6 u  M6 e7 K
  5. 1.0% w0 V  p. _; L' Z+ X8 r
  6. >>> math.log10(100)  W3 T$ L+ B" t0 V/ o8 y
  7. 2.0
    ( [' v9 @7 \. p( i
  8. #即10的1.3次方的结果为20* C$ s8 D' {+ z+ J
  9. >>> math.log10(20)! T7 g0 v6 R9 P8 b
  10. 1.3010299956639813
复制代码

! r7 A) H9 m+ K3 l* O( _, Bmath.log1p(x)  返回x+1的自然对数(基数为e)的值
& e& p& s7 X$ U2 F% Q2 m2 F# R& p# A" R- }
  1. #返回x+1的自然对数(基数为e)的值
    0 j' }2 F3 h( d) R
  2. log1p(x)
    7 }  M7 R. K1 |) J5 z: K, d2 a3 M
  3. Return the natural logarithm of 1+x (base e).' A8 Y1 }! q9 W7 S; J! \7 z, s
  4. The result is computed in a way which is accurate for x near zero.9 {; i+ V; t" j" K0 |+ O! Z
  5. >>> math.log(10)
    : Q- [$ O" X4 w9 f- |* p
  6. 2.302585092994046
    " Y/ A! n' J" k- n
  7. >>> math.log1p(10)+ R/ n3 H4 N8 w
  8. 2.39789527279837077 f5 h/ m$ L$ f( T. z2 j
  9. >>> math.log(11)% ?0 `2 B, X3 g9 M9 M
  10. 2.3978952727983707
复制代码

$ l. ]0 |" T6 [8 Y: mmath.log2(x)  返回x的基2对数
/ Y0 M0 H* n1 D/ d- T7 o
  1. #返回x的基2对数" N8 D6 f$ e8 a1 Z( d# Z
  2. log2(x)3 n, e1 f, p' U" T  ?" h; ?6 g4 K
  3. Return the base 2 logarithm of x.
    4 L5 ~! w% h4 V! j# I# m
  4. >>> math.log2(32)# }7 V& L6 m) N" h! f6 V9 Q+ k
  5. 5.0; |4 f4 h7 y8 Y* r. ^' s, W
  6. >>> math.log2(20)/ u9 E& c) J9 S+ K6 [; w
  7. 4.321928094887363% y7 S9 R$ U8 O* ?7 ]# s
  8. >>> math.log2(16)
    2 p9 J0 }' f& c# z$ }1 m4 w2 ~  q0 M
  9. 4.0
复制代码

7 ~4 S3 R. b/ W" {& @  pmath.modf(x)  返回由x的小数部分和整数部分组成的元组4 b9 q8 c' A$ p3 o* K* B
  1. #返回由x的小数部分和整数部分组成的元组5 ]- l  t% ^& G6 P  M9 _
  2. modf(x)
    9 p- }6 B0 _; M9 O: a( v8 N
  3. Return the fractional and integer parts of x.  Both results carry the sign& [+ x1 D% T. x; e7 H; E
  4. of x and are floats.
    7 K6 a) _2 G4 D$ Q! g4 L
  5. >>> math.modf(math.pi)
    - {& V6 I% z" l% E" D6 P: M9 a
  6. (0.14159265358979312, 3.0)/ [2 c" B6 G% }* @4 }3 ^
  7. >>> math.modf(12.34)
    ; {9 g5 d* j! W: B' d2 L
  8. (0.33999999999999986, 12.0)
复制代码
" i2 J1 I+ @- b+ v
math.sqrt(x)  求x的平方根
% j, N6 C: ], D5 y8 f
  1. #求x的平方根
    ' ]. Z: v- S# j% G4 a5 J+ r3 M
  2. sqrt(x). P: x' R* [) P/ t0 p
  3. Return the square root of x.7 `0 ?! ]. _, P4 U. N
  4. >>> math.sqrt(100)+ E; {5 o4 S4 U  r5 b* T: R
  5. 10.0: r- N$ T) N) T8 q5 Q2 S
  6. >>> math.sqrt(16)6 S* o& o* _1 `, j# V% g- }3 J
  7. 4.0
    2 D" ?- ?) v* R; P1 _) C
  8. >>> math.sqrt(20)
    8 n; {) K1 e0 D: a: u
  9. 4.47213595499958
复制代码
$ w! W# v/ a5 [9 ?
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分, }; T9 H# J. e2 b( w' Z2 x6 j
  2. trunc(x:Real) -> Integral7 V7 K  o7 ^" ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.- r2 n5 \$ ~% w% G! T2 B6 x& m
  4. >>> math.trunc(6.789)
    # D& a3 y' F' R
  5. 6' Q$ U" z6 ?: q3 A7 I% v
  6. >>> math.trunc(math.pi)
    # |3 G+ T1 ?/ ^
  7. 3- C# C( O9 \5 J" I- p0 X
  8. >>> math.trunc(2.567)
    , ]7 f! v  R1 ^9 l
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-12 19:05 , Processed in 0.086611 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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