新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

7 m9 v; Q$ }4 f) Z( m' R' h【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。$ ]6 T( d% Y2 p% d. j- e

6 C  q! T- A. y/ A$ E方法1
, d% [8 C! a1 Q: ?9 `" {5 g$ y
  1. >>> import math% J# _6 r! G0 i  ]% z
  2. >>> math.sqrt(9)
    1 Q8 V2 y, L/ g- B9 X* l
  3. 3.0
复制代码
方法2
) D% h4 e2 p4 v/ O6 ]9 u2 W
  1. >>> from math import sqrt' D9 s/ C2 C: ]0 n& D. f* f
  2. >>> sqrt(9)
    , n: R/ J" Z# R% Z# n( J& C
  3. 3.0
复制代码

5 l4 T, N# x7 L! ^6 e; ^
$ Y  L* W; t8 G- t( d, F" a6 |
math.e  表示一个常量5 T  m+ y9 m1 {9 c6 P" h. w/ h
  1. #表示一个常量& x, \7 V, z# N* J1 P; n% |
  2. >>> math.e0 ?9 ~. ]1 L7 k
  3. 2.718281828459045
复制代码
9 {$ d8 O! z0 V; G' u
math.pi  
数字常量,圆周率

' h- t. ]; n, `. \: g- Q" o
  1. #数字常量,圆周率
    , B7 s0 p1 U- v
  2. >>> print(math.pi)% O# m$ O  T: E' _6 P
  3. 3.141592653589793
复制代码
0 g. S. T- j8 }% q$ @' _5 l
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
+ d9 H7 Y2 z% F
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    - u9 l: r+ a7 x7 B
  2. ceil(x)4 m2 p& d! t! _
  3. Return the ceiling of x as an int.
    & W$ g. u8 O1 z& _& v  ?, w
  4. This is the smallest integral value >= x.
    3 z3 o" ^& B* D
  5. . N$ l$ n  Y% t$ e: d4 Z
  6. >>> math.ceil(4.01)
    ! Y7 f' a; M% F# u
  7. 57 x' Y8 k6 z6 y$ l- n' [2 ]
  8. >>> math.ceil(4.99)0 k4 F5 [" K, d: j/ w
  9. 56 b: W. `( M. l
  10. >>> math.ceil(-3.99)
    % g6 u. s  C; v" A, }! o/ v
  11. -3+ g6 Z! M0 X% S
  12. >>> math.ceil(-3.01)
    ; X0 ]& N0 {; x- A+ ?& {
  13. -3
复制代码

& y- K' S9 m: V0 Jmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身. o& Q  b# m8 a7 q" U
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    & `5 f( F/ r8 S/ t& j5 V
  2. floor(x)9 s( [0 o  X3 j. I+ `
  3. Return the floor of x as an int.
    6 L! s: D* d3 x; H# O/ D
  4. This is the largest integral value <= x.- r( ^9 [) v1 t0 z# `
  5. >>> math.floor(4.1)$ T" e! j# ^) ]* K. L
  6. 4
    5 V; D$ s, ]3 ~
  7. >>> math.floor(4.999)
    - p$ P9 k3 N) C/ X, X, G
  8. 41 N* l" _' z0 y* V& ]
  9. >>> math.floor(-4.999)
      q9 o& j/ v+ v
  10. -5! O6 K0 c" N# m' q( H
  11. >>> math.floor(-4.01)
    9 L6 A; s5 l  {4 Z
  12. -5
复制代码
/ S3 o& P, a  U& ]4 M3 C1 X0 |
math.pow(x,y)  返回x的y次方,即x**y
& N: s9 G* k$ L" |( o1 l9 u
  1. #返回x的y次方,即x**y, W' w) O* }8 X5 E2 L9 z7 k% d
  2. pow(x, y)
    # u; W" N$ \( d* l# ?2 n
  3. Return x**y (x to the power of y).
    % e; s( r7 C( z% [* `+ F* b' j
  4. >>> math.pow(3,4)
    0 f/ q+ z$ q/ _4 U1 r# H6 w
  5. 81.09 c5 j# J- c4 h# H, t
  6. >>>
    9 o+ c& h2 K* K0 u
  7. >>> math.pow(2,7)
    ) g' j- I# v+ j! c  w: R! ~: x
  8. 128.0
复制代码

4 C5 F& k" z! D9 hmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# Q+ w5 n# U& o, n! _2 R
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" v2 t3 b6 L9 p2 m0 \0 D' m2 G
  2. log(x[, base])
    ) v" o! K4 I4 I/ ]! r% W
  3. Return the logarithm of x to the given base./ |% r# ]# l( R7 T" @
  4. If the base not specified, returns the natural logarithm (base e) of x.
    : a# k2 {. G# w: P
  5. >>> math.log(10)
    ) r9 _' S% ]2 h) e: A( W5 K6 z
  6. 2.3025850929940460 ~+ Y4 x  s' j7 k
  7. >>> math.log(11)
    , H( S; J2 B/ k5 Q2 m6 [# o) Y' e
  8. 2.3978952727983707
    * i! J: ^0 T$ [  l
  9. >>> math.log(20)
    # W9 N. j2 ?- i; G" R. ?& K  L
  10. 2.995732273553991
复制代码
, f2 _. k. X% U
math.sin(x)  求x(x为弧度)的正弦值
, E* B+ A" u6 e7 S
  1. #求x(x为弧度)的正弦值$ ^7 U+ T& x* a0 V3 n
  2. sin(x)( ?( B' _. T0 d6 @& n5 s' w$ k( o* ?
  3. Return the sine of x (measured in radians).$ w- k9 Q* t) \3 H# ~5 E- H6 v% ]& U  y
  4. >>> math.sin(math.pi/4)
    % n) \# I* ]. U' O) }- f1 X" _2 w
  5. 0.70710678118654752 s- r1 V) ^' V8 N: p+ v7 g7 [
  6. >>> math.sin(math.pi/2)3 P. O# \( e  b
  7. 1.0! {, b+ D% i$ D0 ~* r
  8. >>> math.sin(math.pi/3). m& g7 x: j3 b3 ]* `) C) l6 @
  9. 0.8660254037844386
复制代码
+ v1 L* I$ u# S: \; C" `. y0 B. e1 d
math.cos(x)  求x的余弦,x必须是弧度9 h- R4 ^+ D) g' p2 W
  1. #求x的余弦,x必须是弧度' j4 Z: N- a: k2 Y9 |! O
  2. cos(x)
    5 b4 [6 y3 K/ f9 R4 W9 |7 o0 X
  3. Return the cosine of x (measured in radians).
    8 K+ Y+ a1 {) U) ^8 D
  4. #math.pi/4表示弧度,转换成角度为45度0 ^; h6 O. Q; B; |  z/ ?
  5. >>> math.cos(math.pi/4)! G, L8 |$ i6 X2 a5 @5 d+ b& z
  6. 0.7071067811865476
    / y( E1 s0 ]2 |* ]: d' W
  7. math.pi/3表示弧度,转换成角度为60度
    3 y. G. o  C% z& Z; r
  8. >>> math.cos(math.pi/3)
    * g7 _: A- J" \$ _
  9. 0.5000000000000001& r9 |% [. T  V, T3 s# ]" O
  10. math.pi/6表示弧度,转换成角度为30度; M2 |! ], K4 f$ u$ N
  11. >>> math.cos(math.pi/6)
    ) \6 n: I  q* u7 q+ j/ I9 ~
  12. 0.8660254037844387
复制代码

& \/ _( F6 K8 [3 d; @; Q4 Nmath.tan(x)  返回x(x为弧度)的正切值6 T, G) w2 I4 Q7 N  Z" _
  1. #返回x(x为弧度)的正切值
    8 ?# A% N9 U+ a+ `
  2. tan(x)* T3 W: B( \' @; x6 w
  3. Return the tangent of x (measured in radians).7 W9 o! D: d6 G
  4. >>> math.tan(math.pi/4)
    1 }/ R) K& J. W+ C  e; M0 b
  5. 0.9999999999999999
    " @3 B. Q5 b- D
  6. >>> math.tan(math.pi/6)/ t# ]( S: ?5 s$ Z/ v
  7. 0.57735026918962573 s& M2 A- @* X; ~. i9 N# k5 J
  8. >>> math.tan(math.pi/3)
    + Z5 W3 M5 f/ x: a& G
  9. 1.7320508075688767
复制代码

$ g; W. K/ T% Y' vmath.degrees(x)  把x从弧度转换成角度
, S; f4 m2 Z- q, M/ f! \+ H
  1. #把x从弧度转换成角度7 O4 _$ X- u! H2 d
  2. degrees(x)- u; `: F/ M: L0 [# U
  3. Convert angle x from radians to degrees.
    0 b7 n( h3 n. A! B: q1 T

  4. ) U. D: U+ y' X8 S
  5. >>> math.degrees(math.pi/4)1 X5 l9 z# n% l% C" \! z
  6. 45.0
    8 P1 w* h& K  U6 @
  7. >>> math.degrees(math.pi)
    " N9 a& c2 k6 \6 }8 a1 k
  8. 180.0
    , I2 a, P8 n' |4 Q0 ~+ n
  9. >>> math.degrees(math.pi/6)
      A5 D1 X/ g/ Y& K- ~& G1 }
  10. 29.999999999999996& l* r! O# D+ i6 O) w
  11. >>> math.degrees(math.pi/3), U. j3 o* F- E& M& _
  12. 59.99999999999999
复制代码
' S% N' B% a- d/ s4 z6 Y
math.radians(x)  把角度x转换成弧度
6 n( c/ E% |" B  J+ n+ `# X
  1. #把角度x转换成弧度2 u3 p# T7 [3 l! Y: M& v7 s
  2. radians(x)
      G8 A, x/ L& c9 S8 v0 l
  3. Convert angle x from degrees to radians.8 @; G) @1 f5 |& j- x- n7 b7 J
  4. >>> math.radians(45)
    / l, j8 n) j& j; w
  5. 0.7853981633974483) t: \; p2 W( Y( J3 A5 `! c
  6. >>> math.radians(60)9 f- H! m$ y4 F8 ?$ y% G9 k) z
  7. 1.0471975511965976
复制代码

1 c) b8 E8 _& W6 P  A# Z8 xmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
' [/ o5 S" g0 p; @
  1. #把y的正负号加到x前面,可以使用0
    % P6 _6 M# _* E
  2. copysign(x, y)
    , ^- |' |4 f/ e0 v
  3. Return a float with the magnitude (absolute value) of x but the sign
      S6 F2 Q: k, f' ^  C2 _" Z; V
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' K8 H0 z) x6 a7 x0 p
  5. returns -1.0.
    * f  l/ t. _' r; k( O

  6. 7 P4 v( ^' g" ?% r9 u2 x
  7. >>> math.copysign(2,3); b% e* l( R* {: m0 ]
  8. 2.0
    4 i. h& j$ i7 Z; A- x) i( _' }) n) g* n
  9. >>> math.copysign(2,-3)+ N" r' F7 A- b  ?$ h  O
  10. -2.07 q4 ?# q, ]% `4 m! v, s
  11. >>> math.copysign(3,8)
    3 }8 N! H, @  U( F
  12. 3.0
    2 W- M( U$ K: }
  13. >>> math.copysign(3,-8)+ ~7 T1 m! {3 H. _! o
  14. -3.0
复制代码

) Q. x+ F) B& I# Y1 ~0 R& [$ rmath.exp(x)  返回math.e,也就是2.71828的x次方) i; X6 ~# w9 v2 Y- R, A0 c3 \) D
  1. #返回math.e,也就是2.71828的x次方
    5 s: }* u% z4 j# f+ n2 ?
  2. exp(x)- y7 e, u* N2 n$ u+ Q
  3. Return e raised to the power of x.
    3 ?: _: i& I0 _8 [

  4. ) ?, O9 i2 Y' j$ s
  5. >>> math.exp(1)7 m. W: X  `7 w3 a% R0 Q9 g. t
  6. 2.718281828459045
    % B! J4 Y' u3 D9 T
  7. >>> math.exp(2)
    9 y$ x3 |4 X% x$ ~* @" v
  8. 7.38905609893065- B/ @7 Q. Z; q6 Q& }2 H8 w4 R
  9. >>> math.exp(3)' u( X& @3 J8 r, \
  10. 20.085536923187668
复制代码
' {% O6 [9 I4 C% p- y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
9 P" W: w% `( s" S- `
  1. #返回math.e的x(其值为2.71828)次方的值减1
    / B9 t0 a3 F( F) M0 @0 y. i% |" _
  2. expm1(x)
    * s0 J4 W* |- q' i5 ]" z) X. }8 ~
  3. Return exp(x)-1.
    $ i+ M4 w/ [7 X! G1 H4 Z
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." f1 A+ t& W- O

  5. ( K! k3 H7 p: y$ s+ R6 a, W
  6. >>> math.expm1(1)- D. M4 C* F+ r0 Q! _
  7. 1.7182818284590455 X/ y9 q7 A7 V3 F5 S
  8. >>> math.expm1(2)" d: P: L& J$ c9 H
  9. 6.38905609893065+ u9 c2 S8 }: ~! X" N
  10. >>> math.expm1(3)
    ; e' p+ @' n' G: W% Q7 Z
  11. 19.085536923187668
复制代码

- z7 r2 M& V8 z3 W! m! bmath.fabs(x)  返回x的绝对值
7 K4 Z, l1 A* z4 w5 {) y
  1. #返回x的绝对值' @: U. H" z9 ~- @: v& L3 @/ m
  2. fabs(x)) h8 A$ O! `& p" S/ V
  3. Return the absolute value of the float x.! e0 H* w  i/ R- _* j7 f

  4. " B  g. J& B! X$ u0 E: P
  5. >>> math.fabs(-0.003)
    6 P: X+ L9 C- ?2 S2 v
  6. 0.003. b2 [- }+ h% y1 S& ?$ L, n* j
  7. >>> math.fabs(-110)' P" Z2 X) ~5 S$ V( l4 v
  8. 110.0
    & O$ R1 m+ z: b, W6 w! E
  9. >>> math.fabs(100)
    9 }/ o+ b+ q4 U& ?" x) _' R- u
  10. 100.0
复制代码

. S' }% Q$ Z- n3 P: n& v$ E. Z. zmath.factorial(x)  取x的阶乘的值% k+ Z, X* H1 |8 t0 G
  1. #取x的阶乘的值# n" J# L" C+ b. L9 T9 u- D
  2. factorial(x) -> Integral! P" O% [6 \, Q8 G- @& X5 r
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; o) p# Q3 E( g6 z* E% V
  4. >>> math.factorial(1)
    0 |2 Z' @' h5 A% H7 `
  5. 1: H8 c% V  i& R# J, M
  6. >>> math.factorial(2)
    * g3 v% ]' q" m3 N% v* V
  7. 2( ~2 W! \) |' D" ~$ |) j; B
  8. >>> math.factorial(3)
    * p, H5 Q- j* Y; x! C
  9. 6
    0 S, m% ~6 r4 ]/ y# p' X1 h8 T
  10. >>> math.factorial(5)8 C0 G# B% L. S+ w! D; Q  A; r
  11. 120
      D# |8 o5 K9 v0 \! O! u
  12. >>> math.factorial(10)
    ; o0 v( V: W- _# n5 M7 {
  13. 3628800
复制代码

5 A' ~, d( }* V+ r7 ?0 Jmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数# g% Z& K- L. |" O/ i1 K
  1. #得到x/y的余数,其值是一个浮点数
    & w7 G6 Z8 Q3 P% @4 V9 n3 x
  2. fmod(x, y)
    4 p1 C" f/ P( ]
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ' x* z) p; o2 M' S) ?3 Y
  4. >>> math.fmod(20,3)+ |6 b9 f/ r% x2 a4 v
  5. 2.0& A, V; _8 ]0 V- x1 p/ Y" C
  6. >>> math.fmod(20,7)
    ! P* g# ^' h9 `
  7. 6.0
复制代码
; M: C/ k/ k% D3 S) p6 T
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 B; q( |1 g( m0 \
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' }! t: ]. c; A/ |* J2 T
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    . D8 t4 ?4 T$ e2 j# T
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    " p3 ~% b; O7 s8 b1 a' E
  4. frexp(x)+ a8 F4 J% m# ?0 Q0 M, {/ ^5 E6 i
  5. Return the mantissa and exponent of x, as pair (m, e).
    - G* Y3 k1 y, Y6 Y0 z7 u; h8 f
  6. m is a float and e is an int, such that x = m * 2.**e.
    ' ~7 k) v# c8 a; @3 Y% W; Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    2 h" j0 X1 h1 Y- U" W
  8. >>> math.frexp(10)
    ; {" K7 Q$ K  P2 r
  9. (0.625, 4)
    . t4 }. H- P; S0 \
  10. >>> math.frexp(75)
    6 m0 U1 ~8 A8 E; l7 Q
  11. (0.5859375, 7), p* z! a/ y. C) M9 |
  12. >>> math.frexp(-40)/ ~. u2 Y! f2 Y( i- z
  13. (-0.625, 6)4 x  P1 V& L3 m5 A# g$ Q
  14. >>> math.frexp(-100)
    0 \: V5 W8 ~# T/ K. A0 r
  15. (-0.78125, 7)
    : l* y1 F& P! z
  16. >>> math.frexp(100)$ V  \6 x% _9 S5 \- \. E! O7 @' B
  17. (0.78125, 7)
复制代码

' V* P( L' r2 T4 j2 a, ]math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, R" ^" t) \0 n2 l
  1. #对迭代器里的每个元素进行求和操作
    / `! ~0 H2 a1 e$ T  U$ ?/ X
  2. fsum(iterable)
    % \$ t) S8 d& c
  3. Return an accurate floating point sum of values in the iterable.+ y7 U; w: z6 w$ r+ T
  4. Assumes IEEE-754 floating point arithmetic." A" P% }3 ]/ ?
  5. >>> math.fsum([1,2,3,4])% s+ S" x% `) R7 K
  6. 10.0
    3 r; \( Z3 I; @
  7. >>> math.fsum((1,2,3,4)), B* w0 C! Q# n, {: a
  8. 10.05 g; |3 w& d2 x' d! M1 B0 `- i( N0 J
  9. >>> math.fsum((-1,-2,-3,-4))  v1 l; i5 {/ g/ w3 w
  10. -10.06 F1 I5 A/ k& a* K4 w! U
  11. >>> math.fsum([-1,-2,-3,-4])
    8 v' ?# T8 c0 J
  12. -10.0
复制代码
; z# d) O" S: S$ V- J. _, {
math.gcd(x,y)  返回x和y的最大公约数
# s  C3 {  J! t& L# A* k) ^9 l
  1. #返回x和y的最大公约数
    # r  Q$ F/ j6 S0 x
  2. gcd(x, y) -> int5 K. W& x7 P5 B# S# z; }% A
  3. greatest common divisor of x and y9 z- z# ~8 `" r4 m' F
  4. >>> math.gcd(8,6)
    : n$ P' Y% C( W: ^) T1 b+ N, U6 z
  5. 26 [0 _5 }3 ?& B* t8 A
  6. >>> math.gcd(40,20)2 a' S" g& m' B0 M  q
  7. 20
    ) }, }# f/ m% R" a* T
  8. >>> math.gcd(8,12); S% l9 G  E' u/ v
  9. 4
复制代码
# u3 `* ^1 |& b. W% s
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
4 ]6 r' J& _, r3 ?
  1. #得到(x**2+y**2),平方的值
    + E- ~) T# |, ]) a" C5 D
  2. hypot(x, y)
    % N/ {8 Q( I+ C1 ]% Y0 k
  3. Return the Euclidean distance, sqrt(x*x + y*y).5 R: C+ A! ^8 w% ~
  4. >>> math.hypot(3,4)
    " a: |5 Y; d' |/ A& L- s! d
  5. 5.0% o; R/ S9 Y- M2 _6 F; V
  6. >>> math.hypot(6,8)
    4 s1 `/ F  Q( \+ s3 R& ^; u
  7. 10.0
复制代码
$ F& w8 x& o# z. |3 F3 i5 Y1 Y/ J
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 l$ n. q6 ?$ D: g: Q( u# S
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % U4 A# t, e6 Q- o% t
  2. isfinite(x) -> bool
    8 t; _* d. ]& _) F; w
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.1 X8 {0 n% s  X8 ?' h: L+ E
  4. >>> math.isfinite(100)9 H( ]/ D! j* ~' k# v! V, _; }
  5. True& ~7 k% m' Z+ A
  6. >>> math.isfinite(0)
    % r5 ~' K3 j) y! n' m+ K  l
  7. True: ?5 l$ n3 C3 Y8 R- E
  8. >>> math.isfinite(0.1)
    ' u) Q" g% @8 c3 _/ a
  9. True8 r5 q+ d8 W  ?
  10. >>> math.isfinite("a"); |. h2 F% p% P
  11. >>> math.isfinite(0.0001)7 f" @( f3 R! P# J6 z- I2 S
  12. True
复制代码
) a5 I4 g) F' D# u3 y# j
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False) T9 }) ?! u( Z$ Z0 U* d) h
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    9 X4 {# g+ i( e1 v
  2. isinf(x) -> bool
    + T1 v; i" y+ g* [* c
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ; o2 Z" u* A  J* _, o
  4. >>> math.isinf(234)
    ; x4 S! V1 e2 q8 W  C' `
  5. False( m* H4 `3 A" z" Y
  6. >>> math.isinf(0.1)7 Q' n* c! `# T
  7. False
复制代码

6 L3 W( Y& W+ gmath.isnan(x)  如果x不是数字True,否则返回False+ {7 C# J* _# I; k
  1. #如果x不是数字True,否则返回False- ], r, j% o; v0 Y) Q8 Z6 _
  2. isnan(x) -> bool
    2 z/ H- O! q  Q: I$ |
  3. Return True if x is a NaN (not a number), and False otherwise.6 Y* ~* ~3 b* P6 W2 P- Q' r  b
  4. >>> math.isnan(23)' p" B/ }+ ]8 B/ ~, e3 U; [
  5. False
    2 k! E# m! Y5 v- L$ |8 y) J
  6. >>> math.isnan(0.01)' U( D( u; Y4 o. a+ H1 ~" L
  7. False
复制代码

4 u$ K3 K+ e4 hmath.ldexp(x,i)  返回x*(2**i)的值
# \& x; z: ~; g$ v  T
  1. #返回x*(2**i)的值
    5 a3 }# w: ~6 g! W9 w- t# L
  2. ldexp(x, i)
    7 W8 N2 H6 {- h% E9 S! @. ^! o
  3. Return x * (2**i).) P) Z- e' E6 N
  4. >>> math.ldexp(5,5)
    # q% Q7 t/ j$ @$ K% _
  5. 160.0% t% F5 c# g6 @7 k) W
  6. >>> math.ldexp(3,5)
    3 ^% h1 ^* Y8 z
  7. 96.0
复制代码
. K0 |- }- Y9 C  q
math.log10(x)  返回x的以10为底的对数
+ F3 C$ V" Q8 I7 B0 L7 k
  1. #返回x的以10为底的对数
    , c1 _$ s0 o4 [, O8 l# y4 p# R' V  \/ F
  2. log10(x)
    6 Z& K( b% c, o7 p
  3. Return the base 10 logarithm of x.
    # z, P+ Q+ ]. f3 Y" r  S' p
  4. >>> math.log10(10)
    : H( T9 y/ E, o6 r
  5. 1.0% ?  b$ G% L$ w8 k
  6. >>> math.log10(100)2 U; k3 s4 c$ a+ ^  p! X/ t
  7. 2.0" h" n& F1 X2 A
  8. #即10的1.3次方的结果为20
    . ^0 R/ D- W  R3 e6 j: Q' X9 X
  9. >>> math.log10(20)
    6 C5 k4 [: e1 R7 i3 g: B/ Z; l  B- n
  10. 1.3010299956639813
复制代码

7 w  W/ S5 ~4 Q' X& @5 I( ^: M  Ymath.log1p(x)  返回x+1的自然对数(基数为e)的值; Y' x) S- S' r- B. G( W
  1. #返回x+1的自然对数(基数为e)的值7 S( N. D+ l- R, S7 o
  2. log1p(x)' q  C' E3 o8 Z
  3. Return the natural logarithm of 1+x (base e).- ^* x: b/ B2 O- ~9 L# E4 g4 X0 v
  4. The result is computed in a way which is accurate for x near zero.9 Q- R' J2 D# `5 @( U* k
  5. >>> math.log(10)
    % \, R8 i# ^7 T% B4 g, ^" ?
  6. 2.302585092994046
    , W9 P$ i: R" m& f
  7. >>> math.log1p(10)3 X: {: w8 m( K/ A9 i' t( n5 u
  8. 2.3978952727983707. ~+ {; f4 F. N. {0 T. p3 ?0 o
  9. >>> math.log(11)1 @" R& b1 c; p$ i! f7 |- d3 [
  10. 2.3978952727983707
复制代码

* L# U" m7 v- Q: B' jmath.log2(x)  返回x的基2对数
# F; q8 u0 s5 s. c2 r$ [/ m
  1. #返回x的基2对数4 S3 V5 R: t; S0 A) G
  2. log2(x)
    # T2 O* |$ e+ v) e
  3. Return the base 2 logarithm of x.1 ]' Z2 h; p* G
  4. >>> math.log2(32)
    " C" c5 J3 {8 w
  5. 5.0$ v% @5 l- P# h1 L4 s( Z
  6. >>> math.log2(20)" g. h$ ^9 L8 ]* D1 |3 W, u( b
  7. 4.321928094887363
    8 g' X- T+ F' S$ f7 i! Z
  8. >>> math.log2(16)
    . p! N/ ]3 P7 C! y8 S
  9. 4.0
复制代码
/ q3 J+ r; ?/ i1 H5 T% G
math.modf(x)  返回由x的小数部分和整数部分组成的元组9 d6 x2 T  ?8 n( M: G! x
  1. #返回由x的小数部分和整数部分组成的元组' D! h# p( j5 A* \! q. N9 @
  2. modf(x): |" O% \0 A! y2 \  z5 [9 L3 `% Z
  3. Return the fractional and integer parts of x.  Both results carry the sign8 n" `1 a  \4 u' w* \
  4. of x and are floats.
    & A% w" b) D- e/ D
  5. >>> math.modf(math.pi)& [8 z* z' [# q- m
  6. (0.14159265358979312, 3.0)/ P3 H2 p  l$ A; Q* r- }
  7. >>> math.modf(12.34)
    " M1 n! W) {" i; v" m
  8. (0.33999999999999986, 12.0)
复制代码
- \) D* W5 g! O3 K  A
math.sqrt(x)  求x的平方根
. q5 ]' O3 j- p) n: D- m
  1. #求x的平方根& c; _* _4 b! V& i
  2. sqrt(x)4 @$ p) a( \% D) c
  3. Return the square root of x.: z7 q+ d7 o' p0 q9 B) L/ L
  4. >>> math.sqrt(100)4 [! ]$ h) h4 ^6 a
  5. 10.0) g8 K8 f# A) F- g- K+ v0 T, S, \" y8 F/ S
  6. >>> math.sqrt(16)
    1 `, C  m) h6 l( a
  7. 4.0
    - V9 W& }7 E  W
  8. >>> math.sqrt(20)# k5 S7 j. M# i% D
  9. 4.47213595499958
复制代码
6 ?+ V2 u  I' a7 n$ m
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分) \8 b2 i$ l" q! Z
  2. trunc(x:Real) -> Integral
    % ~4 v& C. I0 ^* q$ m: c, q' M
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    8 w7 _1 s% s) v6 g& W
  4. >>> math.trunc(6.789)) y2 U& g/ j- S4 G8 @4 C
  5. 6
    - m; ^( |' N  y  i% n
  6. >>> math.trunc(math.pi)- I1 X+ I0 U8 ~- q3 c5 {/ i- t7 _
  7. 3
    : N, V6 m( f1 |1 O, t% X6 _) [4 |9 L, X
  8. >>> math.trunc(2.567)
    1 s& U1 V* d. p: M3 I; g% T: _6 q! y
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-20 13:40 , Processed in 0.107144 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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