新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

  W4 j# E6 Y; @0 }' |$ m【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
# e* i* c$ w" K6 t; {1 f

8 i) z8 q/ @8 d0 u. h8 G方法1
+ E: S" I2 ^# ~( c' H* L6 [
  1. >>> import math; x; u/ q9 |5 A! T
  2. >>> math.sqrt(9)
    ) P; d- s  r* [* W" B8 p! ~
  3. 3.0
复制代码
方法2
4 j  ?+ Y% c* i
  1. >>> from math import sqrt$ O: U% P! P" \4 ~6 g& E5 B
  2. >>> sqrt(9)2 u1 a( v: v; F3 u3 p0 T" z
  3. 3.0
复制代码

( i8 ?5 E5 {& P

. _; w8 ]  g9 P- Q& z# M) Y) H9 A; k
math.e  表示一个常量6 K( p0 C: ]  F7 s+ K( o: r
  1. #表示一个常量
    3 f+ J. `# O' F
  2. >>> math.e. G. c! j  @/ f, T1 |
  3. 2.718281828459045
复制代码
) m+ V4 d2 h/ W! j( J( J
math.pi  
数字常量,圆周率
5 e1 h4 M9 x2 I" L' s- T
  1. #数字常量,圆周率! e8 T  R. N4 P/ @, b4 P, F" r
  2. >>> print(math.pi)
    ' ^5 ~! G- W  ]! {* p4 H" Q
  3. 3.141592653589793
复制代码

- c+ w. N" g  ?0 _4 S& umath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 u1 n* v  a2 i- l$ B1 y* g# s
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 x) F  T8 I1 n) L) U
  2. ceil(x)4 k9 g' x3 c2 O9 z6 [7 x; n$ v
  3. Return the ceiling of x as an int., y3 ^% Q; W8 v+ x' r- j
  4. This is the smallest integral value >= x.4 U: u, x/ T  @: X/ ?! z

  5. : M/ Y( Z! u/ h" R
  6. >>> math.ceil(4.01)% T" ]: T: a/ d- r" s5 o
  7. 5
    ; v0 H$ S. D6 C5 F7 @* K
  8. >>> math.ceil(4.99)
    & \5 b" l& j$ B
  9. 5
    . t, C) N9 r* ?) v' b6 D
  10. >>> math.ceil(-3.99)$ u$ r+ r1 |2 g
  11. -39 I) h! k0 z* h4 J
  12. >>> math.ceil(-3.01)( @" I+ I: e  x, R" g9 h
  13. -3
复制代码
! F7 u/ J  b9 k" J1 M: A, F
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
! W) `8 w5 g7 f* K
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ; D  q' a9 z; e) w; b9 U
  2. floor(x)+ b0 _3 G' D: F, c9 |6 R
  3. Return the floor of x as an int.# [7 L% ~+ v$ F( S/ S
  4. This is the largest integral value <= x.% E4 D. o4 o  ^
  5. >>> math.floor(4.1)6 V# b5 H% s/ {7 {) B$ I: t
  6. 47 i/ T6 r/ }# V5 i& W/ }2 p8 L7 @$ }
  7. >>> math.floor(4.999)
    3 H# d/ @. j9 I
  8. 4
    4 g) A9 r# M$ O& Q. b
  9. >>> math.floor(-4.999)
    3 N4 d7 N# s' ?
  10. -5, n2 _9 b% E* k, `5 h6 R
  11. >>> math.floor(-4.01)+ _+ F$ O" v. p) J+ M5 n+ k
  12. -5
复制代码

3 U& ^  }- k) x9 Q  d% i1 _4 R8 ]: nmath.pow(x,y)  返回x的y次方,即x**y
5 P) H9 \& e- C6 z( G% o/ G6 a
  1. #返回x的y次方,即x**y3 a/ F$ h# j  N3 I- {8 X
  2. pow(x, y)# K5 ~" i$ n+ L1 K# M
  3. Return x**y (x to the power of y).
    6 A" @+ l( i4 o7 h
  4. >>> math.pow(3,4)
    . d% j+ `! J/ V3 m, J/ E- k  J
  5. 81.01 K6 V6 a* _* n, B9 C. [2 G% _
  6. >>> - C3 o( x4 [3 T6 }
  7. >>> math.pow(2,7)
    & ]; f/ s3 ]. ~; x. y2 P
  8. 128.0
复制代码
. g; k4 k' {$ o5 G  `# I
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' _4 s5 }5 [, ^
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 h; L# D8 p9 r6 \
  2. log(x[, base])6 B, Y6 A5 w( U
  3. Return the logarithm of x to the given base.$ r3 C& |+ S% b  F( U) v
  4. If the base not specified, returns the natural logarithm (base e) of x." g; x4 |4 B0 Q( Y* ^
  5. >>> math.log(10)
    ! V* _1 m" J5 B/ s7 v
  6. 2.3025850929940461 |2 r2 x4 y; b, q* W$ k  g
  7. >>> math.log(11)+ D/ F, @7 K: S0 S9 Q* J6 @, x! }
  8. 2.3978952727983707% ]8 H2 b' h. i2 \/ O
  9. >>> math.log(20)! ]+ f& ]0 v4 L  e
  10. 2.995732273553991
复制代码

; z5 E0 V, U9 ?! ~math.sin(x)  求x(x为弧度)的正弦值$ s+ F: @9 V3 U+ o6 t  i( o* X
  1. #求x(x为弧度)的正弦值$ Q: r2 E8 Y+ B* W# O5 \7 `+ Q
  2. sin(x)- E, G6 ~$ x: K( L  C
  3. Return the sine of x (measured in radians).1 o) w" P; n. P6 T
  4. >>> math.sin(math.pi/4)
    $ ]. H* C5 I" g; P6 J  [
  5. 0.7071067811865475  B8 d; r$ Z8 a; ?5 d% j
  6. >>> math.sin(math.pi/2)# r6 `$ x, l9 _: V9 n* S
  7. 1.0& i8 L' `6 K& `5 K: s
  8. >>> math.sin(math.pi/3)
    + U+ @; ^6 s+ U) c; J) r
  9. 0.8660254037844386
复制代码

6 s) S1 ^1 M3 O2 m) T" U3 [math.cos(x)  求x的余弦,x必须是弧度
3 ~2 ^# x: B# w6 X
  1. #求x的余弦,x必须是弧度( Y* G& E/ ^- q3 B
  2. cos(x)* l, W) D' u' f0 P2 J$ T8 f* b
  3. Return the cosine of x (measured in radians).
    * ?4 {/ z6 `2 a
  4. #math.pi/4表示弧度,转换成角度为45度4 }7 R, N$ N* x# d" N3 D
  5. >>> math.cos(math.pi/4)
    5 ]" g  C8 T/ |2 _" K- H; m
  6. 0.7071067811865476
    + B+ i3 A# Y; Q' Z
  7. math.pi/3表示弧度,转换成角度为60度" U6 W: b, @: v
  8. >>> math.cos(math.pi/3): i7 j; ~. P6 T9 g
  9. 0.50000000000000012 l: l: i8 T3 V$ {# o0 W' Z# j3 a
  10. math.pi/6表示弧度,转换成角度为30度
    # f5 z& ?# E$ K
  11. >>> math.cos(math.pi/6)
    ' S$ |5 h! L" X* E7 Z
  12. 0.8660254037844387
复制代码
6 n  z5 u9 y- _7 u6 b
math.tan(x)  返回x(x为弧度)的正切值
( t" E- J" g% o0 n% M! y4 L( M$ D" ?
  1. #返回x(x为弧度)的正切值
    . y, M% E9 o; o: Q) w) g$ r+ X
  2. tan(x)( {3 C' }- Q8 K6 [1 t
  3. Return the tangent of x (measured in radians).) M9 K2 q3 W! n1 o
  4. >>> math.tan(math.pi/4). k0 C& y6 N  Z! ^3 ?9 Q3 p
  5. 0.9999999999999999
    3 ?# w# B. v: N* M1 m( H
  6. >>> math.tan(math.pi/6)
    2 \0 {8 ]0 A. d3 {4 q
  7. 0.5773502691896257
    . k( o$ v. H0 D2 H- l1 j
  8. >>> math.tan(math.pi/3)
    - f" q! T0 ]2 T
  9. 1.7320508075688767
复制代码

& Y2 h3 H% ^9 P3 Imath.degrees(x)  把x从弧度转换成角度
; d  q, w, Y0 ~3 P) ^
  1. #把x从弧度转换成角度4 g. q5 t6 _1 F( `" d
  2. degrees(x)" C) Q7 |3 f3 G: G' V1 A
  3. Convert angle x from radians to degrees.
    $ l2 I& \1 l! W5 M* j

  4. 6 Y# T2 L. G0 K' s
  5. >>> math.degrees(math.pi/4)
    1 y4 ?9 ^1 A. s5 z2 V! T- ]
  6. 45.0* ~7 D9 f4 U" @; Q
  7. >>> math.degrees(math.pi)
    6 h, J9 ^# \! O) Z5 W
  8. 180.0
    ) i% e# g1 c3 Q# p
  9. >>> math.degrees(math.pi/6)
    5 x) y+ R0 A9 n3 x9 X
  10. 29.999999999999996( g$ R" |6 w# r  S0 T  a
  11. >>> math.degrees(math.pi/3): @& v( k# b8 C# c$ y7 v
  12. 59.99999999999999
复制代码

( m: ]* M  Y' z/ l0 omath.radians(x)  把角度x转换成弧度
$ z& m7 Q3 k* p# B6 W) p# c- d
  1. #把角度x转换成弧度
    * m+ R% _* s3 B5 G+ m& K  A
  2. radians(x)" h7 a1 v' V( _
  3. Convert angle x from degrees to radians.
    . L7 L- U8 }8 l2 D
  4. >>> math.radians(45)
    0 B. k9 J4 t+ f
  5. 0.7853981633974483  N9 M( C0 b4 ~
  6. >>> math.radians(60)
    ! A: H) P, \$ P: h4 I* ?
  7. 1.0471975511965976
复制代码

3 F/ i8 @( ~0 Qmath.copysign(x,y)  把y的正负号加到x前面,可以使用0" Z. P4 z1 _  G+ F0 {* A8 k
  1. #把y的正负号加到x前面,可以使用04 }$ ], I  `* o1 e
  2. copysign(x, y). c) u4 ?, M9 y0 [( b; }( ^
  3. Return a float with the magnitude (absolute value) of x but the sign
    ) s* I8 a4 g7 c& w. a9 D
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    $ u% z: J- b% }$ a* D
  5. returns -1.0.
    " Y( C- }, c+ e' e% U9 `1 G7 t* X! D/ z' [

  6. $ y2 l* e5 v% w% A; f/ M6 @, d9 G' @
  7. >>> math.copysign(2,3), H9 g! Z: F3 a/ e9 O0 k
  8. 2.0
    8 I, o0 T- u, L7 ^- [! w; G0 \
  9. >>> math.copysign(2,-3)
      r( `- H2 e' ]& h. L
  10. -2.0
    $ X/ y2 O+ U' U" J, h
  11. >>> math.copysign(3,8)
    + K1 _8 W$ f2 k8 m& W
  12. 3.0
    / ^4 c% [  e2 E9 ], B( I
  13. >>> math.copysign(3,-8)/ d1 f3 P2 ?3 m. e" k
  14. -3.0
复制代码

0 s" j/ Z7 [, H7 S7 c3 y7 Nmath.exp(x)  返回math.e,也就是2.71828的x次方8 Y+ n$ V9 y5 G3 N) I9 q
  1. #返回math.e,也就是2.71828的x次方
    7 ^8 W8 k1 k8 \' H% L' h* Z9 S
  2. exp(x)& X7 Y: w, j+ T
  3. Return e raised to the power of x.# U( D: ^- t' `9 x" w/ |6 D( n

  4. . k) Y2 A0 b: s, T6 W' d3 s
  5. >>> math.exp(1)
    . C/ Q5 ]1 E7 \' M$ f: I
  6. 2.718281828459045
    : E; h3 i, j6 ]/ k
  7. >>> math.exp(2)- D) h) H$ c  E9 u. E
  8. 7.38905609893065
    ! K. K% q. D- `, T0 `8 s1 P1 {
  9. >>> math.exp(3)
    ) X5 |* w+ X. p% j* E
  10. 20.085536923187668
复制代码
, Q) F# D. L/ h9 }/ c
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
* U/ N* H; [$ c) [% L' u" K/ J
  1. #返回math.e的x(其值为2.71828)次方的值减1! I5 P7 W2 V2 @) }7 M4 g3 F
  2. expm1(x)
    / o: i' q& d0 r0 V- n( f, x! o
  3. Return exp(x)-1.
    9 L* L5 P. o( Z) R* T7 _  A
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    5 n$ J" J" t  P0 v. x, u
  5. ( H& b1 F" y8 }  G- x3 X
  6. >>> math.expm1(1)1 y2 H/ C% t/ T% u6 p; P; K
  7. 1.718281828459045  J3 L. ]3 k; ]
  8. >>> math.expm1(2)
    1 p. d4 N. V4 B) d, x
  9. 6.389056098930659 N! E2 T. F6 w0 U0 H! `
  10. >>> math.expm1(3). x) d$ D  A& c4 W
  11. 19.085536923187668
复制代码

7 o( V9 J7 b2 e8 n, ^+ B) fmath.fabs(x)  返回x的绝对值
+ ~/ ^' p; s0 [0 l3 K1 t5 ]6 b3 P
  1. #返回x的绝对值+ [# [6 U% P$ B% u% I0 ~
  2. fabs(x)
      E) {  z; h* X3 Y+ t
  3. Return the absolute value of the float x.
    / s! Y4 U0 C- l

  4. 2 f! O0 P2 k+ b- ?6 e- k( `
  5. >>> math.fabs(-0.003)
    9 K& t/ j% ]* ?' L
  6. 0.003" @9 U- _6 Z' |  t) Y! J
  7. >>> math.fabs(-110), ^# G3 M5 @5 }6 H3 @& W
  8. 110.00 G/ F- I, d& N# B! F2 x  f& ^4 k
  9. >>> math.fabs(100)
      A" f2 P" B" `# D& G: c4 j* }1 X
  10. 100.0
复制代码
, q, N# S/ m8 J' g. R2 V+ Y8 Q
math.factorial(x)  取x的阶乘的值# a: R$ j2 a1 M4 S, g& o0 k+ \# z
  1. #取x的阶乘的值
      c; d  t9 M- d  c
  2. factorial(x) -> Integral
    6 U& m9 M6 h' T. B1 O) k. S) x
  3. Find x!. Raise a ValueError if x is negative or non-integral.- I1 Y4 y, w$ h+ n5 q. W; r
  4. >>> math.factorial(1)- ?: K) d  V+ o
  5. 1; ]3 D$ C* b- c' s/ h
  6. >>> math.factorial(2)0 S# M" [! y( @6 s( t
  7. 2% C4 E/ {! w2 |8 c4 S
  8. >>> math.factorial(3)
    + V% C# g" e8 X# h' v! V
  9. 67 D' |4 |% B. Z1 _8 Y: n5 j
  10. >>> math.factorial(5)
    ; p& p( t, m' }7 F1 ?2 `# z
  11. 1205 Q2 r( T! ]/ R$ M# [7 V- c
  12. >>> math.factorial(10)/ L: D6 I* F" K4 Y9 U' O
  13. 3628800
复制代码
- l8 u5 K. J( w2 s5 E
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
, C1 R0 h7 e! D. l
  1. #得到x/y的余数,其值是一个浮点数
    - r) S1 E: M, u7 s5 J) e& Z7 ?  J
  2. fmod(x, y)
    4 [8 M% |+ V9 Z. m/ s
  3. Return fmod(x, y), according to platform C.  x % y may differ.  k8 m/ D5 u6 s0 R6 b; Y; M( R( M
  4. >>> math.fmod(20,3)9 u2 y: B. R: }% n* S
  5. 2.0
    5 _: f: g& Y% U4 F* Q( l; ]( ?" s
  6. >>> math.fmod(20,7)% v1 p9 a7 f" V# Y! @
  7. 6.0
复制代码
. X8 t+ }9 [! J+ H4 m- J# j
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 \- M  b* I. [) b
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    0 e$ F# G4 ^" ?
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    , E# u! [. A# `% t" E
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! y* T7 Z* X; R( p
  4. frexp(x)
    0 H$ S. L/ |: O+ c& I9 l. H
  5. Return the mantissa and exponent of x, as pair (m, e).8 t: {" ]  x; u6 G5 Z: n* H. M
  6. m is a float and e is an int, such that x = m * 2.**e.
    . j" P) V6 d( p$ p# R9 z
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.5 k: f. e! @- X! s& X3 ?% l
  8. >>> math.frexp(10)
    1 H8 R, y- d$ y6 E. N9 B7 v! v/ `0 C
  9. (0.625, 4); K' O( Y5 A- X- `+ D* t. e
  10. >>> math.frexp(75)1 I7 A* w0 W& U. g
  11. (0.5859375, 7)
    / n! i) d0 `3 Y) Q' r) ~
  12. >>> math.frexp(-40)3 M+ X' V% _' V% V& \4 e
  13. (-0.625, 6)9 B2 G0 b* y! F; U/ T, d" h1 v
  14. >>> math.frexp(-100)9 n! u- K9 A& M) J2 D) W; e
  15. (-0.78125, 7)
    2 c: w9 v7 v+ @& o( F5 T
  16. >>> math.frexp(100)+ m8 b% r) t4 @1 A
  17. (0.78125, 7)
复制代码

; a( Z; q( m- X5 Y6 d4 r1 ]math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列4 x+ N7 _" Y% D* I( Q4 H
  1. #对迭代器里的每个元素进行求和操作
      j- \9 J9 x7 a! V
  2. fsum(iterable)9 @& A, e$ o3 {
  3. Return an accurate floating point sum of values in the iterable.
    $ A$ K- X) F" ~
  4. Assumes IEEE-754 floating point arithmetic.
    * N. O8 F, m5 d+ V- f2 }* P
  5. >>> math.fsum([1,2,3,4])
    : z$ \8 |1 g! e% A  b* L
  6. 10.0
    . g) H/ M4 h+ g+ Y6 ]' |
  7. >>> math.fsum((1,2,3,4))# V& ~; k+ M- M( b% B/ D2 T
  8. 10.0
    4 W5 @+ Q( u$ ?! v2 @
  9. >>> math.fsum((-1,-2,-3,-4))
    ( p8 r  O' P1 ~
  10. -10.0
    4 }6 u0 t1 J" Y- F' e3 _8 {. |
  11. >>> math.fsum([-1,-2,-3,-4])$ W6 _" ~$ J3 `* J% ]9 p. {  o) S* u
  12. -10.0
复制代码

. \" o# a: V. j. q& ?math.gcd(x,y)  返回x和y的最大公约数
- W3 W' O- I) S0 |' m, _, E
  1. #返回x和y的最大公约数
    8 u2 k- c+ S- \1 ~5 V5 L0 r6 ?
  2. gcd(x, y) -> int: ]  n8 E! V. K- Q! N3 x& R' [' {
  3. greatest common divisor of x and y
    8 `' X+ `2 c8 g0 t9 t8 \8 B9 Z
  4. >>> math.gcd(8,6)! O0 W1 g4 ~) u: g* c
  5. 2
      i; o- s1 p6 l9 k. \6 ]& D- ~
  6. >>> math.gcd(40,20)
    * f# C6 r6 n) N7 l$ h9 O
  7. 20! a9 m0 I  k* }" K" F
  8. >>> math.gcd(8,12)4 ^' B4 E) I6 B0 G7 p3 L
  9. 4
复制代码

* A" }( ]( K7 Umath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False0 [7 h' ]8 y$ c! Y6 P
  1. #得到(x**2+y**2),平方的值5 b9 |% j! x1 n9 {
  2. hypot(x, y)
    ( k4 O- z: S" g* x/ ~* C5 N
  3. Return the Euclidean distance, sqrt(x*x + y*y).* Q4 C2 {% p/ o/ P: P# m
  4. >>> math.hypot(3,4)  h+ L' }- k8 n: Z1 E1 L
  5. 5.0: O% R* Q0 \" F0 |
  6. >>> math.hypot(6,8)
    ! @# O8 U$ O9 {1 q
  7. 10.0
复制代码

3 V8 o( f* q, q/ q3 Wmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False" h, p3 T8 u/ h# v
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    " m+ @$ S. {9 G
  2. isfinite(x) -> bool) {- |: c, p9 k: C2 j
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    1 ~/ E' ^+ h3 r
  4. >>> math.isfinite(100)
    ( ~, v' H. K1 g9 j3 W- Q, E
  5. True+ Q  t1 Q- t' G6 B1 n
  6. >>> math.isfinite(0)9 j: s5 A+ k/ P
  7. True1 u6 }! Y% D- k5 @9 p+ ]) s& E
  8. >>> math.isfinite(0.1)4 X: I" h" {9 l4 i, g& |( E
  9. True' G) O5 H( [* R7 O
  10. >>> math.isfinite("a")
    & `6 J* K! W( k) V4 H
  11. >>> math.isfinite(0.0001)4 ~4 v# d  X3 y) {
  12. True
复制代码
; ]' G5 e! l  c4 `
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, q) A; Y# s2 _6 m* H
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False# z) d6 g5 X# d0 N& z0 r, k
  2. isinf(x) -> bool! g6 Q2 D: h, f; N$ K
  3. Return True if x is a positive or negative infinity, and False otherwise.2 o; d* ?6 H4 m& Z
  4. >>> math.isinf(234)
    ( w# j& L+ d1 H& X8 M1 X
  5. False
    + G5 O/ ]/ K. t  C& H4 R# h
  6. >>> math.isinf(0.1)
    9 P" x  m* V3 A, z
  7. False
复制代码
( ?* C) z+ B8 w0 Q
math.isnan(x)  如果x不是数字True,否则返回False
( O2 K% @) V0 Y
  1. #如果x不是数字True,否则返回False
    * m( Y- n. h+ y) t+ k+ W6 D
  2. isnan(x) -> bool* {/ q6 N. |1 \; l9 i3 g
  3. Return True if x is a NaN (not a number), and False otherwise.0 I% R5 z# m5 f+ V/ q+ V# h. X$ E
  4. >>> math.isnan(23)# `8 d- e* W  d0 W8 G" r
  5. False; k/ U) G. [0 E3 N
  6. >>> math.isnan(0.01)
    $ [, b& I/ M& v; z1 P  o
  7. False
复制代码
) e, J' E& j6 m. F1 E
math.ldexp(x,i)  返回x*(2**i)的值0 n& G8 h% q' k$ B1 U3 y& N- G& a
  1. #返回x*(2**i)的值$ \) k' x; V$ B
  2. ldexp(x, i)
    ( U4 t3 y% w3 _; G; c$ F" \. e7 \
  3. Return x * (2**i).  d. _3 D" S8 z8 \
  4. >>> math.ldexp(5,5): W) i7 z9 ?1 R
  5. 160.0
    , H, b# i$ c6 ]' N7 U
  6. >>> math.ldexp(3,5)
      u4 ]# p/ ^. x' r
  7. 96.0
复制代码
+ R) N) ]  ~+ m: l) D: \  w0 a
math.log10(x)  返回x的以10为底的对数- W2 R- a' m( ~
  1. #返回x的以10为底的对数8 @, f, T) p/ U. f+ L/ {
  2. log10(x)
    . s0 q6 C  [2 Z9 D8 W- C4 a; o
  3. Return the base 10 logarithm of x.
    . J5 S- v, x, W
  4. >>> math.log10(10)! m& A& c) v9 b+ V' e: K" \, ^
  5. 1.0/ C+ b5 D' C" N" ?9 J) U8 ~
  6. >>> math.log10(100)3 s) ^* `7 k( u, W
  7. 2.0
    9 s. Q) ^& m; O4 j& V
  8. #即10的1.3次方的结果为20
    ) _! t$ ]' F. K  A. K
  9. >>> math.log10(20)
    * j- |1 G+ t  `: |  b4 v
  10. 1.3010299956639813
复制代码
  z* C4 c6 [" l$ y" _/ a. r3 H
math.log1p(x)  返回x+1的自然对数(基数为e)的值. W8 Y( b: Z' \. a/ S. |
  1. #返回x+1的自然对数(基数为e)的值
    / t- {& ^4 r1 `6 D/ R, s& T
  2. log1p(x)
    / `" V4 k4 c  B: H5 g2 V
  3. Return the natural logarithm of 1+x (base e).0 K3 v2 B: |2 B5 B0 H6 E" r
  4. The result is computed in a way which is accurate for x near zero.% d8 X; B$ \. Q2 W! ^
  5. >>> math.log(10)6 p& U) Q8 k. b
  6. 2.302585092994046
    9 h2 T& d( e& x5 K2 L
  7. >>> math.log1p(10)
    2 n% {; {) c7 b" M: ?- x
  8. 2.3978952727983707
    # b5 b# C, i' T' n, B% H8 c
  9. >>> math.log(11)" l, a5 \0 f9 X% [9 y
  10. 2.3978952727983707
复制代码

3 W& w# k6 \) I# gmath.log2(x)  返回x的基2对数, S/ M6 _1 ]5 O- }* u  f0 w. d, _
  1. #返回x的基2对数  c! P# w* b: Z8 F
  2. log2(x)
    + g! P2 r  [+ n$ }  q  o
  3. Return the base 2 logarithm of x.
      A$ S8 Q& W6 v' M, T
  4. >>> math.log2(32)
    ! o/ o" G! g9 R
  5. 5.0' ~& b/ M* C6 Y6 C* Z
  6. >>> math.log2(20)2 i9 Q+ O4 `/ e: ?
  7. 4.321928094887363- Y* R5 a5 m- E) |
  8. >>> math.log2(16)
    ; S( F+ i5 q6 Y, u2 c' y
  9. 4.0
复制代码

  N; I) X: m  Z4 Q/ pmath.modf(x)  返回由x的小数部分和整数部分组成的元组
% x/ B3 N" B' Z) I3 X; T" s; N! o6 Q
  1. #返回由x的小数部分和整数部分组成的元组
    5 b+ R. f8 W5 f( ^
  2. modf(x)8 J) S4 q, U5 [& i4 Z1 ]: t7 ^- M: F# m
  3. Return the fractional and integer parts of x.  Both results carry the sign+ n/ }6 `& |% u2 r- }
  4. of x and are floats.+ @, x' D: ~/ {  x6 r
  5. >>> math.modf(math.pi)- [7 x. g& h' g* A/ d5 [9 U
  6. (0.14159265358979312, 3.0)6 n% z: ~4 f3 M  m2 p' P
  7. >>> math.modf(12.34)
    # h/ O8 Y& C5 f$ {
  8. (0.33999999999999986, 12.0)
复制代码

; p9 \: |- i- c# B2 mmath.sqrt(x)  求x的平方根  g' d1 f& Q* @/ ]" Z
  1. #求x的平方根
    8 n5 P8 A$ n; K/ w
  2. sqrt(x)3 B% ^2 B5 @  I) K9 g- H* K
  3. Return the square root of x.
    5 a  Q' S" z& a  X+ j3 n# `
  4. >>> math.sqrt(100)4 x3 M% u. N; ^% a
  5. 10.0
    6 L( \6 O# \, W- @
  6. >>> math.sqrt(16)
    ) I7 u1 d" g6 R% s2 q2 o; A
  7. 4.0
    7 \" `) ]$ ~4 R. m6 \* t
  8. >>> math.sqrt(20)
    9 e/ W4 M% |/ Z5 c( t' c# m' D
  9. 4.47213595499958
复制代码

4 E: ]! Q/ U6 G3 T5 [+ d6 Dmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    / m1 X4 i$ q2 Z: P% X/ P
  2. trunc(x:Real) -> Integral# K) ^+ M4 @9 _; K
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    + S7 C/ x3 p  E) Z& r
  4. >>> math.trunc(6.789)
    * T4 ~/ f( B( Y
  5. 6
    + s2 t. r! S' }7 n, @' |
  6. >>> math.trunc(math.pi)& A$ |% I$ a9 N( s! k( t
  7. 3! D, w" T) }+ d- }0 b
  8. >>> math.trunc(2.567)7 W; Q* I: y. }; ^# b3 i- ^! h- G
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-2 08:25 , Processed in 0.078670 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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