新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

3 m% |- T: h1 X2 W2 u2 _' _【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
& R6 P0 r4 i2 r$ S4 v

+ D# ~# S0 L( \* F方法1- k( T6 f2 e9 l3 ?' B) a3 i
  1. >>> import math
    : s4 W, W9 ]+ p9 \: S2 `+ }; L
  2. >>> math.sqrt(9)$ Y4 C/ E/ Y5 z) [4 e
  3. 3.0
复制代码
方法2" u/ l2 J; y" W5 L1 B- G; d8 x. C
  1. >>> from math import sqrt
    : c; }/ E5 N; I6 m) _- f6 U
  2. >>> sqrt(9)! w6 P8 D7 o/ `2 E1 F
  3. 3.0
复制代码
, b! q$ A, n$ F( K  n2 L

0 C2 K: A) K6 }" Q( H3 y
math.e  表示一个常量  R6 A2 A7 S  x
  1. #表示一个常量6 z5 `1 I( o; [( H; B# X$ M8 e& Y
  2. >>> math.e
    6 Y+ g. r( k1 S0 `
  3. 2.718281828459045
复制代码

! ]* j9 W* L6 l; A- J4 ~& B: a! K$ }math.pi  
数字常量,圆周率
4 `1 _0 K5 ?. m
  1. #数字常量,圆周率# S. ?/ E  i) U3 n
  2. >>> print(math.pi)
    3 R$ m' D( X3 J  K2 O( j8 ~
  3. 3.141592653589793
复制代码
" ]( y8 o, Y4 i$ ^9 k0 `' H
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
( z* d+ H5 |- M1 Z
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 f1 c, V& r) s4 u
  2. ceil(x); c$ A0 `; B  A4 I
  3. Return the ceiling of x as an int.4 H" R, Z4 k& @- b! D- }) D6 i
  4. This is the smallest integral value >= x.
    1 ^5 P  \/ ~$ ?- r9 v, e

  5. . C  _! @( W  k. K+ ]
  6. >>> math.ceil(4.01)7 v* n5 L# O! k6 v
  7. 5
    * j0 Y" y$ `! B8 J
  8. >>> math.ceil(4.99)9 N# `4 R. m* v
  9. 5
    6 p- \) R2 Q) L( s5 N0 `
  10. >>> math.ceil(-3.99): ~2 D+ J& e, x! c9 ~
  11. -3
    - x6 Y& A; i+ B% K5 a
  12. >>> math.ceil(-3.01)) _( ]; Q+ T0 V
  13. -3
复制代码

, o) d" T$ h& i% j* B% H; h- xmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 p" k4 \; I( o3 J4 T
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* z; W2 M% ?$ R! g
  2. floor(x)/ Z0 A% x9 P. [4 g, S5 l8 t0 h
  3. Return the floor of x as an int.( N! f7 |* t1 n2 {) Z' J, m
  4. This is the largest integral value <= x.
    5 F  N3 D: H$ }1 ?
  5. >>> math.floor(4.1), E5 E6 l, m- y9 W% v7 p* ?0 F. a
  6. 4
    ( W, ]0 F) O* Q% T2 M8 Z
  7. >>> math.floor(4.999)" w& M8 G1 f8 G2 _9 t
  8. 4
    , O# M. X, _9 i
  9. >>> math.floor(-4.999)
    # x8 ?0 d$ q! _5 ^2 H5 i
  10. -50 V$ E6 u6 P3 r
  11. >>> math.floor(-4.01)
    7 L0 L3 S2 m8 I+ t7 l
  12. -5
复制代码
* X, s1 J. F! q) N- v
math.pow(x,y)  返回x的y次方,即x**y
) x% C5 h7 N: P% i& E  a3 I
  1. #返回x的y次方,即x**y5 c# U6 D& M' }+ m# L* M
  2. pow(x, y)' O2 M; p7 X( K9 D. V3 {
  3. Return x**y (x to the power of y).
    9 t' f" E- j% k" `) B
  4. >>> math.pow(3,4)
    2 F  U" E. G; T7 Q" A4 i. K
  5. 81.0
    2 o* d- _8 U' v0 K5 u( T. `- u
  6. >>> 3 Z( w1 h# W- {2 c) |- q6 E
  7. >>> math.pow(2,7)/ j! S" ?( b5 f$ a- A
  8. 128.0
复制代码

1 R6 N: L# ]! g* E1 s1 Hmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ X. t7 a8 h- c) D
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! H& b) e5 x0 A. c9 _
  2. log(x[, base])
    / Z1 ^6 U/ m( ?- c/ Y: Z
  3. Return the logarithm of x to the given base.) [/ g0 K2 H; ]$ M  |: ]* l) b
  4. If the base not specified, returns the natural logarithm (base e) of x.0 V% z4 M8 e# m- H, W  @
  5. >>> math.log(10)
      e+ O- A" c. a9 b* e
  6. 2.3025850929940466 u' b. |& x9 a+ z" k0 b
  7. >>> math.log(11), n" b8 L/ B1 I7 p/ {9 ^( C* v
  8. 2.39789527279837073 J1 N* w4 n# p: Q: M& Y
  9. >>> math.log(20)
    + k, I0 j3 R% A7 ]( l7 \" s" c  B
  10. 2.995732273553991
复制代码
+ i+ y) A+ _) s, R: {, k
math.sin(x)  求x(x为弧度)的正弦值
  W6 z+ l" W  [1 P5 f
  1. #求x(x为弧度)的正弦值
    ) G+ f+ e, q- ~6 F- Q, c. L
  2. sin(x)
    ) @& d5 v1 j. F! V
  3. Return the sine of x (measured in radians).6 b6 ^# e+ n2 r$ N- A
  4. >>> math.sin(math.pi/4)( M4 B7 y: h6 Y7 ]
  5. 0.7071067811865475
    ) ]# F! ^- N1 `, Z( k
  6. >>> math.sin(math.pi/2)
    / r' K& d3 @0 ?" u7 X  \7 h
  7. 1.0" z. c7 _$ k$ `# L
  8. >>> math.sin(math.pi/3)& s1 }* N) H) x0 b, @, H
  9. 0.8660254037844386
复制代码
) U2 ~5 \/ r( ~+ X8 D) D
math.cos(x)  求x的余弦,x必须是弧度) m7 D  |) y: b: q/ I; v
  1. #求x的余弦,x必须是弧度
    4 o' t- p# ^( S, M6 R* _
  2. cos(x)
    3 B2 B& i1 \1 w: g; u# @
  3. Return the cosine of x (measured in radians).2 {' J) M& g& _( a* M! [
  4. #math.pi/4表示弧度,转换成角度为45度9 I+ r8 a  H3 v/ C4 L+ Y. f/ F
  5. >>> math.cos(math.pi/4)
    1 ?+ b# E0 Z7 ?" I$ F
  6. 0.70710678118654768 n, N3 w' z: f4 k+ p
  7. math.pi/3表示弧度,转换成角度为60度
    5 E9 P% O5 E8 h1 T# Z; z( J5 Y
  8. >>> math.cos(math.pi/3)* ^0 A3 b" J% k# J- M3 f0 x- g
  9. 0.50000000000000015 N. H1 |1 \  f+ D" a9 R
  10. math.pi/6表示弧度,转换成角度为30度# Q0 a; D$ D3 m6 H" t
  11. >>> math.cos(math.pi/6)  w$ l  A4 |) L9 C3 v$ h3 }( y
  12. 0.8660254037844387
复制代码
! K5 ]2 h/ u% J5 i" L5 G
math.tan(x)  返回x(x为弧度)的正切值% O; k% t4 R# K8 z% h5 u' x! o
  1. #返回x(x为弧度)的正切值
    3 A) w) b2 Q) N
  2. tan(x)
    7 n" o; v! g) V
  3. Return the tangent of x (measured in radians).
    + S$ t) C) _5 O
  4. >>> math.tan(math.pi/4)4 c( N: a" |0 N8 V8 x
  5. 0.9999999999999999
    ! F# W2 D- L8 C0 I& T$ `
  6. >>> math.tan(math.pi/6)& o' d9 v, e# ]* Z2 C7 b
  7. 0.5773502691896257" N3 Z7 m7 u6 w  F& Q; J9 j% p
  8. >>> math.tan(math.pi/3)
    , ~2 S; G$ g8 M* ]" N
  9. 1.7320508075688767
复制代码
3 O! A4 P2 ?6 E5 H* E
math.degrees(x)  把x从弧度转换成角度/ e; j( D' L$ Z$ r" m7 q/ Q& [
  1. #把x从弧度转换成角度) m" K% x. i3 ^; N& A3 W
  2. degrees(x)
    3 k1 U0 j6 b" @: y' t  v( I
  3. Convert angle x from radians to degrees.8 T8 P. ~' s( b

  4. ) J5 e3 K" p" ~2 E
  5. >>> math.degrees(math.pi/4)
    , i5 ?- E6 X: Y3 @. ~! u
  6. 45.0; H( {6 E' i: E, Q* N; Z' G. g
  7. >>> math.degrees(math.pi)
    0 c: y$ ?& g$ N
  8. 180.0
    * \9 A* K* W( x  f/ ~
  9. >>> math.degrees(math.pi/6)
    0 d6 a, c  e3 P/ H+ t
  10. 29.999999999999996; o$ {) |, Y, Y+ U
  11. >>> math.degrees(math.pi/3)4 G3 J: S. E. H3 K0 L
  12. 59.99999999999999
复制代码
- u6 S/ m* K. [7 R
math.radians(x)  把角度x转换成弧度
% [' w  D9 s9 [7 @& Z2 m5 \4 _
  1. #把角度x转换成弧度
    & b" L6 T# M6 Y3 m, s; c& O) e6 n5 X
  2. radians(x)
    & h, {, x$ J& D
  3. Convert angle x from degrees to radians.6 X5 N# p7 c/ V  ]& C1 J
  4. >>> math.radians(45)" Z# A& n9 Y; _0 E4 \  S. R, c
  5. 0.7853981633974483
    % J* T' }2 x" k5 X' |
  6. >>> math.radians(60)) r+ x5 `) ?3 R9 U: n7 M! i
  7. 1.0471975511965976
复制代码
- H7 ^* H6 ]# Q$ f7 L; \
math.copysign(x,y)  把y的正负号加到x前面,可以使用0+ ^2 W1 Z% w2 U* z* J; m! g3 ]
  1. #把y的正负号加到x前面,可以使用0
    " G% Y+ M0 _* j
  2. copysign(x, y)) Z1 N- o# J* e- D5 r7 }5 w" h
  3. Return a float with the magnitude (absolute value) of x but the sign
    1 b- J: o3 ]' r. |2 S' U, w1 p( i
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) # c: w' a9 n% J5 ~0 ?! u4 C
  5. returns -1.0.5 m9 W- X/ @! S

  6. " I8 r) u4 a. O6 d& P4 C: B; v
  7. >>> math.copysign(2,3)
    6 i) w# l' e& a2 z) s; p4 Z
  8. 2.0  _9 a  R! F) N
  9. >>> math.copysign(2,-3)0 G+ r: |2 I7 H+ R
  10. -2.0
    3 l: {: [; K6 a5 X5 {9 }! p
  11. >>> math.copysign(3,8)
    $ U$ n  p* m/ M7 z3 ?6 x
  12. 3.0
    1 ]6 e2 [8 k9 S/ G7 n5 X
  13. >>> math.copysign(3,-8)& @' X8 p8 w0 V0 Z7 }2 k
  14. -3.0
复制代码

" }7 B& \- e9 dmath.exp(x)  返回math.e,也就是2.71828的x次方8 u( ~' {- r. _+ T  y3 w7 x( W5 y
  1. #返回math.e,也就是2.71828的x次方
    % T2 S! ?( [, f9 o& Q9 _) X
  2. exp(x)5 O3 J/ I( v5 L$ I  z% |; B
  3. Return e raised to the power of x.9 c% W  r& \- N9 i0 X
  4. : z0 o! k+ r+ P" C& L! C
  5. >>> math.exp(1)
    ' Q, a: @! ]* T7 @8 M8 c
  6. 2.718281828459045# n! _" r% t% y/ [7 [& ]
  7. >>> math.exp(2)
    ! M- V9 z& [6 _* h
  8. 7.38905609893065
    + E$ S8 X, P; N; O$ Q& d6 M4 u
  9. >>> math.exp(3): n2 i$ N5 Z( R
  10. 20.085536923187668
复制代码
/ D* B0 {" |- G6 x: w
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
; ]5 _& E/ f! m; ]$ x5 `4 k8 e
  1. #返回math.e的x(其值为2.71828)次方的值减1
    + E2 U6 y8 @) J+ B# d9 \
  2. expm1(x)4 t# ]" m7 r$ i# |9 C3 g
  3. Return exp(x)-1.4 p/ G, L) |1 R9 v0 ~
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    % t* D5 p" V; O1 O

  5. # `8 [1 P' I. m7 O
  6. >>> math.expm1(1)
    ! Z* h# B" \( ]' F
  7. 1.7182818284590455 Y! u( C: t4 e# |
  8. >>> math.expm1(2)
    + o# E6 D' x9 F7 o7 W: c
  9. 6.38905609893065, \- p! V' i2 Z- E3 a
  10. >>> math.expm1(3)
    3 S3 o2 ^! b  ]
  11. 19.085536923187668
复制代码
0 Z1 V7 _! ]5 U' j4 Y
math.fabs(x)  返回x的绝对值
; u6 e1 i9 }' |" F; Y$ e
  1. #返回x的绝对值
    2 X3 T8 [5 A5 H  J" v/ X
  2. fabs(x)2 v1 c0 H8 S5 A
  3. Return the absolute value of the float x.: j7 a) r& A6 }' F
  4. & i/ M: B8 C( h1 I" M" c
  5. >>> math.fabs(-0.003)
    4 l$ A3 Q5 x3 N% y# E* t
  6. 0.003; d0 [+ `- \1 H% J, e
  7. >>> math.fabs(-110)' C7 o+ L6 r, B$ [4 l- V1 ~
  8. 110.0
    ' j9 ]' Z/ L; ~3 z, `- ~
  9. >>> math.fabs(100)0 q$ K- r7 {7 k( I1 {/ z
  10. 100.0
复制代码
$ u7 h, |8 T5 O6 p, I' A
math.factorial(x)  取x的阶乘的值
+ R1 u4 A) `# X: u% D
  1. #取x的阶乘的值! ~3 F& S. |& L0 b( E  }
  2. factorial(x) -> Integral
    + f$ t3 l$ d* d3 N( i7 _+ K
  3. Find x!. Raise a ValueError if x is negative or non-integral.2 o$ A6 f; [) K
  4. >>> math.factorial(1)
    * n! e6 U0 o: i: a& k
  5. 19 Y) W1 w' B3 g1 K% R, {3 T  J
  6. >>> math.factorial(2)" y! E# R1 ^; a( k  E' ]' u
  7. 2/ p3 E+ G$ |* B9 T  w( K$ d6 j
  8. >>> math.factorial(3)
    , _9 g4 Z& M* f3 w$ q. Z6 c
  9. 6
    , p' |9 y$ ]: @* p0 c4 J2 W) M( i6 z
  10. >>> math.factorial(5)5 o& Z7 [- B& P& F( ?
  11. 120
    . p" J/ Q# H" G1 E
  12. >>> math.factorial(10)# w4 p  a; ^9 M8 J- |# Q/ H' ?4 K/ T5 f
  13. 3628800
复制代码
% @0 h  w- A/ @. A  S. A: O
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
7 E3 D4 Y  ?2 i& O, j
  1. #得到x/y的余数,其值是一个浮点数
    ) X( A4 `1 O% d6 @/ F" Q
  2. fmod(x, y)3 n6 J$ s0 X1 d" d, @$ ?8 `9 `/ X5 t
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    . k: o$ v! j' u$ M
  4. >>> math.fmod(20,3)
    ) N. Q. ~! G4 y# i& c% u0 \
  5. 2.0- V7 [3 o- Q+ D3 p/ H
  6. >>> math.fmod(20,7). _" A3 e, i. t5 |
  7. 6.0
复制代码

7 m/ f4 A; Z4 N' b3 x- K0 Lmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 }8 _1 s' h5 X1 O
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,; a! a0 n7 z, s( y1 @
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    * l9 D: z/ N) k$ p1 c
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1# C% B; I1 @2 [8 v
  4. frexp(x), x0 A: m3 S9 f/ d/ i# ^
  5. Return the mantissa and exponent of x, as pair (m, e).
    0 J' @- j" X: }0 A
  6. m is a float and e is an int, such that x = m * 2.**e.8 E1 o, n1 U$ m& h' O4 D
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- l$ n, y; M/ d
  8. >>> math.frexp(10)
    ' \2 }5 z. j& ]4 j. F
  9. (0.625, 4)$ C1 M, b: M( f# C4 i& i9 ~& @3 f
  10. >>> math.frexp(75)& t6 m  B2 s$ d. T8 S
  11. (0.5859375, 7)
    / Z1 n+ W. ]2 l' B
  12. >>> math.frexp(-40)
    & y, S# a; ~( \; @
  13. (-0.625, 6)1 W7 P, d: ^* j: q9 u* R, {4 c
  14. >>> math.frexp(-100): U! W' s7 d' s5 R1 J' j$ \, t
  15. (-0.78125, 7)
    - Q; _2 c( w& n5 \5 O4 ]. z
  16. >>> math.frexp(100)
    0 I8 `0 N* M& Y- Y
  17. (0.78125, 7)
复制代码

8 X1 F- |8 {% A' g9 @math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列0 _5 I  ]8 W9 P5 T5 q
  1. #对迭代器里的每个元素进行求和操作; }7 H2 j0 [* m, E+ O
  2. fsum(iterable)- x7 c. H; W1 d) N
  3. Return an accurate floating point sum of values in the iterable.9 j8 p; b: o! H9 R8 B. M
  4. Assumes IEEE-754 floating point arithmetic.
    ' }& z! U8 o4 i
  5. >>> math.fsum([1,2,3,4])* M3 ^5 H2 A9 M0 z6 j) q8 w0 A( V
  6. 10.0, W# n, R! _9 r# k4 K
  7. >>> math.fsum((1,2,3,4))/ R* Y  _1 M6 z9 I
  8. 10.06 T/ {6 x+ Y: h( [  Q) v" Q5 r
  9. >>> math.fsum((-1,-2,-3,-4))# m0 A' u/ Y7 S7 d; C7 n
  10. -10.0: `( V' P0 T8 T8 @9 @% l+ Z/ |' V+ ]
  11. >>> math.fsum([-1,-2,-3,-4])
    - ^% c1 D  S# s" q! N3 E% ^7 u* }
  12. -10.0
复制代码

/ r* P5 B$ I. m& I- F' }3 kmath.gcd(x,y)  返回x和y的最大公约数
5 U4 L  h% d3 T- D7 b0 x! J
  1. #返回x和y的最大公约数
    $ l4 L4 }, I, B2 \( o* @" [3 w& J
  2. gcd(x, y) -> int
    . C9 V1 P  d8 A; e2 W
  3. greatest common divisor of x and y
    2 B+ V( s* k! w% ]
  4. >>> math.gcd(8,6)' x% T$ ~: \1 T
  5. 2% B5 ?, J. Y+ b& _
  6. >>> math.gcd(40,20)0 u) i0 q0 |0 a# C1 t
  7. 20
    5 T! L/ @- ?1 h5 a
  8. >>> math.gcd(8,12)
    # w1 W9 p, O, J# M4 A" L
  9. 4
复制代码

$ {$ H- W$ ~5 ]% t! Lmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False" ^4 L* t' B: J& a- S% q3 H6 Y
  1. #得到(x**2+y**2),平方的值
    ; y+ z6 J: I' |4 U. n
  2. hypot(x, y)
    " o$ {. }1 n4 t/ L+ P/ z
  3. Return the Euclidean distance, sqrt(x*x + y*y).8 ]: G7 H" j6 s" K6 ?* y! p  Z
  4. >>> math.hypot(3,4). e+ i6 }* I9 ?, }0 G6 }
  5. 5.0
    # b/ j1 x* U4 Y9 C5 l; X
  6. >>> math.hypot(6,8)
    4 d4 S3 L! v4 E' i# P
  7. 10.0
复制代码

) C; N$ J7 g+ X( Omath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
: A" u2 r! p' j
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    $ N3 I* g) d* N5 r% M& ^
  2. isfinite(x) -> bool% _5 x8 V6 o9 l0 y, A+ I
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    / Y8 V& e/ `- U/ C6 n
  4. >>> math.isfinite(100)
    8 r! ?- T7 m/ w& d2 s! D
  5. True9 ~1 k) R1 A" k3 @- d4 u
  6. >>> math.isfinite(0)5 u- O7 @7 c5 A" p$ z8 T
  7. True
    ) {' H6 r4 g7 |: ~& h2 L7 ~
  8. >>> math.isfinite(0.1)
    " \: y: R: t! G3 m
  9. True
    ! U% G% U- O( {% p6 d
  10. >>> math.isfinite("a")5 ^; m% V6 G% g
  11. >>> math.isfinite(0.0001)$ U4 b0 x3 |# Z
  12. True
复制代码

, p+ {5 y, p/ w5 ]+ Z& N2 `* umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
. M- V! s1 o  H5 Z7 O7 R3 h$ v
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ; c- P0 J2 `* M7 @4 c* ]
  2. isinf(x) -> bool# T9 Q9 E. v1 W$ F. c2 o
  3. Return True if x is a positive or negative infinity, and False otherwise.) |8 `. {, R9 k
  4. >>> math.isinf(234): L) r2 Y" U' i2 \
  5. False
    . M  R; g' o$ @" |0 q% u2 b
  6. >>> math.isinf(0.1)7 n1 |4 j5 i: v, c) V/ s- C6 A
  7. False
复制代码

* l' o  ]* V% w- Pmath.isnan(x)  如果x不是数字True,否则返回False" D6 y" \5 _9 G
  1. #如果x不是数字True,否则返回False
    & J/ m! ?, ?7 U. w  D  N
  2. isnan(x) -> bool' Z0 F1 ~7 F6 `: t
  3. Return True if x is a NaN (not a number), and False otherwise.
    , t- {. m6 r* A; G
  4. >>> math.isnan(23)
    0 `5 o) C9 d% r9 P- p$ Z
  5. False% O/ C& n& `; B% j" Y3 L& l
  6. >>> math.isnan(0.01)' L/ W' `8 _+ B  w) [
  7. False
复制代码

' }# \% P% o# K0 _& Q3 _math.ldexp(x,i)  返回x*(2**i)的值
0 n7 d# Y$ d/ y& X0 [8 Z
  1. #返回x*(2**i)的值5 D" ], e+ G. ?$ H. j8 y
  2. ldexp(x, i)
    2 B4 I5 o* J) P3 S
  3. Return x * (2**i).7 b2 ?: ?6 p) U8 d& @
  4. >>> math.ldexp(5,5)
      e4 S2 U0 D; t$ G6 b4 b
  5. 160.0
    2 a0 b# ^' l( Y9 j1 r
  6. >>> math.ldexp(3,5)
    . m& y4 x# J% f# B# B' S5 ]
  7. 96.0
复制代码
: x% y5 V+ m9 k. E1 w: S
math.log10(x)  返回x的以10为底的对数- ?9 q- P9 b0 }
  1. #返回x的以10为底的对数
    - d  I4 A8 D  P' W6 B& `: V
  2. log10(x)
    9 @  k5 c" ?. r" Q
  3. Return the base 10 logarithm of x.
    ' H9 @1 c% k1 Q* P" A
  4. >>> math.log10(10)
    2 n6 M3 |" L9 x; o  ?
  5. 1.02 ?  b: }" h$ f- T# K
  6. >>> math.log10(100)
    - `5 N3 k% Z: e' {
  7. 2.0- a: G# G8 ]4 s# t' N' L7 F
  8. #即10的1.3次方的结果为20# W& Y. V9 l& ^2 t' Y9 f! P
  9. >>> math.log10(20)- o! @9 U  S0 t2 E. f  x9 h
  10. 1.3010299956639813
复制代码

& ?) D, T/ H7 h! m1 ]math.log1p(x)  返回x+1的自然对数(基数为e)的值) a# R; W8 }5 ^
  1. #返回x+1的自然对数(基数为e)的值! h& V1 `8 D4 U) a
  2. log1p(x)$ e% i1 k! d5 m
  3. Return the natural logarithm of 1+x (base e).
    6 s# K$ E6 R  m
  4. The result is computed in a way which is accurate for x near zero.- P, `, s5 Q: {1 s9 L* K
  5. >>> math.log(10)
    / h: S3 l. `7 u2 Q; @8 G% U
  6. 2.302585092994046. r% {- d* [% l: S8 _1 c- U& V
  7. >>> math.log1p(10)4 Z3 }2 x8 }, d
  8. 2.3978952727983707! K# Q0 \( q" E8 o5 x8 x8 s2 o
  9. >>> math.log(11)
    . a' Q+ M: j9 s
  10. 2.3978952727983707
复制代码

/ _. D1 A9 R, L( X7 \; T8 z: xmath.log2(x)  返回x的基2对数
1 E+ k: Y" H; g$ P- ~
  1. #返回x的基2对数6 H2 C# \8 Z2 h9 F5 k' s: m
  2. log2(x), K) _6 V  n& q; M
  3. Return the base 2 logarithm of x." ^. o* |9 I5 J* _! n: x
  4. >>> math.log2(32)
    ' V1 ^6 f. q' v) m9 S
  5. 5.0! s0 P, q$ R0 g! b) K- N
  6. >>> math.log2(20); x/ V0 _( a+ r7 j/ L1 S# p3 N
  7. 4.321928094887363
    , `3 d" ^' A! X* x7 S  R
  8. >>> math.log2(16)
    % ^+ I0 w: B" q9 ?
  9. 4.0
复制代码

+ }( K) ^, C9 I# l7 ?: ^math.modf(x)  返回由x的小数部分和整数部分组成的元组( B8 ]* K' A: s! J' u# t  Z, s+ ^
  1. #返回由x的小数部分和整数部分组成的元组
    5 ^; c3 t  Y8 h$ u# S; Y
  2. modf(x)
    0 _+ E- U* |0 t. m( }
  3. Return the fractional and integer parts of x.  Both results carry the sign
    7 r, k/ |2 [1 m
  4. of x and are floats.  G+ v* W, C/ u6 c$ y
  5. >>> math.modf(math.pi)- j2 [% I2 P. f4 G, \4 h; W& L
  6. (0.14159265358979312, 3.0)% n6 i5 K* b1 ^
  7. >>> math.modf(12.34)
    0 G5 g5 ]- l1 P: `
  8. (0.33999999999999986, 12.0)
复制代码
# m% `& m$ H. A* }  K
math.sqrt(x)  求x的平方根
" |% B4 X- p5 ]
  1. #求x的平方根& p2 F9 F6 h- D) e* z  S
  2. sqrt(x)
    5 w: |" N7 l" F0 e& W: e
  3. Return the square root of x.9 I- @. D- t% T, M
  4. >>> math.sqrt(100)
    1 W7 {7 R' W( ?1 x+ n; O1 L, j  a
  5. 10.0+ q1 m* o# K; G7 g1 P5 v( R: w
  6. >>> math.sqrt(16)
    % |: z1 u' G8 D* z- S9 ]6 J9 w# V+ D& L
  7. 4.0
    1 y$ n. n7 `! Y3 X  J
  8. >>> math.sqrt(20)
    & q  s/ a1 `& N8 s+ i/ o+ N
  9. 4.47213595499958
复制代码

" V8 [* J" {  q2 l; V# ^math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    0 D( _5 W# M' v/ Q
  2. trunc(x:Real) -> Integral
    ; g4 Z* S- v+ F2 a
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    % L% y- Z: F! D+ q4 q( h$ A
  4. >>> math.trunc(6.789)
    + z- S7 X# |! h( W# O$ K. O5 R+ ~
  5. 6
    " T3 E% _8 N6 n" o* R/ R
  6. >>> math.trunc(math.pi)2 l% Z* r% m2 i; ~7 u( H3 m: a; l* A
  7. 3
    & ?: r- {: ]6 G- J! u9 r6 U% [
  8. >>> math.trunc(2.567)
    # {) z! H( s' {5 o+ H# ]# @
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-28 20:39 , Processed in 0.093153 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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