新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
7 H) C1 t' G/ L* ^) i+ W. y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& Q4 R7 I3 x+ i, K8 Q$ K

' y: p6 `$ Z  i+ h方法1) R# g7 h/ l' Z% T5 P- S0 S' v! U
  1. >>> import math; M+ G: o0 y* A0 ]4 Y, A
  2. >>> math.sqrt(9)
    ) D3 y& i7 r- X* b
  3. 3.0
复制代码
方法2& G! Z! y3 d( @% `
  1. >>> from math import sqrt' q3 G4 Q% c( P5 A* g) P/ b8 G9 E
  2. >>> sqrt(9); ^0 ?" p) u( d! r* k0 t3 z/ {* _  D; G
  3. 3.0
复制代码

5 h, y3 i- O) t/ ~; G0 H; M. r  S

4 Y# x4 w* H1 v' ?! q
math.e  表示一个常量
7 O/ U5 H3 J/ ^, j
  1. #表示一个常量5 a' Y4 C8 X3 y7 @$ F4 c7 o5 K
  2. >>> math.e
    7 m; Q0 A  m5 R! r* P. p1 ^% \
  3. 2.718281828459045
复制代码

* n# z: ?' N$ p$ Qmath.pi  
数字常量,圆周率
+ W! f) r/ B' O7 A/ B/ M1 U& ~
  1. #数字常量,圆周率
    ) h2 Y' |! r+ M. @5 u
  2. >>> print(math.pi)
    7 S$ C9 b2 ^1 F8 w8 {1 s
  3. 3.141592653589793
复制代码
; f; m* ^4 K% O" R! Z
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
- q- f( z. ]$ h
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ H# Y& v: W# A: \/ U
  2. ceil(x)
    : p- A; k# ^0 |
  3. Return the ceiling of x as an int.
    " a. s0 P7 G, Y; U
  4. This is the smallest integral value >= x.' u' d# [6 O4 Y4 E3 N2 H. k8 ?
  5. ' Y2 }  j9 R( Y
  6. >>> math.ceil(4.01)1 R0 |  i! m3 o
  7. 5
    . N, E$ f6 x. d; `
  8. >>> math.ceil(4.99)5 Z% b, q1 A  A' ~
  9. 5. @, |& t+ M- ~4 s  h
  10. >>> math.ceil(-3.99)
    6 e, A) z( t; b
  11. -3
    ( ?, r) z  L0 u, M, N
  12. >>> math.ceil(-3.01)/ l4 ?8 i/ a6 Z7 e4 R
  13. -3
复制代码
$ c* m7 a2 N' r7 B* i" e* D
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身- e, C, `4 x/ g  }% j& ^
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ( ]! a% w3 z& ]$ K8 B: q
  2. floor(x)' C: L6 X3 O; n4 m1 [) L* |
  3. Return the floor of x as an int.
    , O+ d0 j0 o, Z  N! l
  4. This is the largest integral value <= x.
    . z& E5 t) U: y& m. H. W8 U
  5. >>> math.floor(4.1)
    ( P5 I& k# P' {3 v: _/ H/ Q+ n$ p
  6. 45 }. ~9 Q# B& w  }1 Y- ]
  7. >>> math.floor(4.999)
    - H1 q3 ?( F1 C- _* D: K
  8. 44 e: p7 \5 @5 i! i3 C) q
  9. >>> math.floor(-4.999)
    ( j% o0 r- J4 |
  10. -5
    4 u7 A1 f) Y" M: p) A$ e  G
  11. >>> math.floor(-4.01)
    ) C0 }7 K8 O7 ?4 ?1 V& f
  12. -5
复制代码

' ]/ I: n, g4 [/ x7 `& [math.pow(x,y)  返回x的y次方,即x**y( ?) ^8 ~, P" A1 y( X' e  R
  1. #返回x的y次方,即x**y
    ' h0 |$ ]2 A3 |9 e/ m- b' R
  2. pow(x, y)
    8 N5 I6 x0 O3 e) @( O! Z. I- C) S
  3. Return x**y (x to the power of y).( l" Q6 ?1 y/ E& `& L7 A7 B: q; N
  4. >>> math.pow(3,4)* x) x! [" M# d0 `' w3 o4 Z2 H
  5. 81.0
    ( l, i. c! q. U+ f# F
  6. >>>
    : F4 C, p! ?+ @% R! R
  7. >>> math.pow(2,7)1 N3 g2 H( B' C* l" [
  8. 128.0
复制代码

( O7 D$ H1 _8 a1 C: _6 l5 mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
. s; c0 M8 T" D3 R
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    5 m4 z. [0 S+ f2 w
  2. log(x[, base])/ _9 d8 O- X9 y6 U# G2 P" V
  3. Return the logarithm of x to the given base.
    ' J7 I( g0 u& h  Z2 T4 E' m' p
  4. If the base not specified, returns the natural logarithm (base e) of x./ M9 C' G4 E: ~/ U) E
  5. >>> math.log(10)0 a" w( H0 Z3 _! [3 f- O4 D  j/ R2 Q
  6. 2.302585092994046
    * ~0 o  ?% w3 `: S- `: r" ?
  7. >>> math.log(11)
    $ F" H! Q9 ?0 T# T! e) V8 s
  8. 2.3978952727983707
    ! F/ ?# D: k6 N5 I
  9. >>> math.log(20)
    ; @0 T0 d& {3 O3 p
  10. 2.995732273553991
复制代码

$ i( Q. h) q0 {math.sin(x)  求x(x为弧度)的正弦值
% E' }1 V' |' d2 ^* T' K
  1. #求x(x为弧度)的正弦值+ s- }, J* ^7 ?' [" C
  2. sin(x)
    1 m" a& l+ E2 M- [
  3. Return the sine of x (measured in radians).
    9 S0 w) k% N7 k
  4. >>> math.sin(math.pi/4)' V0 R- M7 F: v/ u; X0 p% o, g( y
  5. 0.7071067811865475
    4 k' c2 P& G( Z0 B
  6. >>> math.sin(math.pi/2); o7 ^7 R- R( T* V$ v
  7. 1.0
    ' v7 w- r6 \4 N/ n& J
  8. >>> math.sin(math.pi/3)
    & t8 o$ n7 b- \3 E% Q+ ~
  9. 0.8660254037844386
复制代码
& W5 |! X4 d4 K! L' H% x% X; q! z0 e5 Y4 w
math.cos(x)  求x的余弦,x必须是弧度
1 z3 N7 \2 b# R& a! {- R6 o/ w
  1. #求x的余弦,x必须是弧度
      g0 P, v' V% ~, Q8 |3 i7 {
  2. cos(x)$ Y9 B; `7 I1 @* K4 M6 A. l* ^  i
  3. Return the cosine of x (measured in radians).
    ' w5 L9 }% o  w2 x' p
  4. #math.pi/4表示弧度,转换成角度为45度7 |9 V5 N  q! x! M) y
  5. >>> math.cos(math.pi/4)
    7 K% m/ v# Q1 Q) u8 v3 D2 A
  6. 0.7071067811865476
    7 e+ @/ F2 B# z2 L: }; N
  7. math.pi/3表示弧度,转换成角度为60度
    $ M+ I" W2 Q" H0 r0 k
  8. >>> math.cos(math.pi/3)
    ( f# z* Z7 c, }% u: U% A# }
  9. 0.5000000000000001
    6 d9 w- u6 j+ r' b
  10. math.pi/6表示弧度,转换成角度为30度
    * m2 r$ x, F! Y& @+ H
  11. >>> math.cos(math.pi/6)
    - ^- O# g' Q+ V0 N2 Y
  12. 0.8660254037844387
复制代码
/ ^& l! k- {; x, G$ n2 k/ g7 [7 V
math.tan(x)  返回x(x为弧度)的正切值
1 _6 [! d& @5 U' ^& L% F2 |
  1. #返回x(x为弧度)的正切值
    : z" P+ y( \# `# K9 g+ {
  2. tan(x)
    " A% Y  Q; o8 V  ~
  3. Return the tangent of x (measured in radians).
    6 g, L$ }  \4 f+ \$ I+ L
  4. >>> math.tan(math.pi/4)* l% ^9 l$ h$ E, g$ R" H, d7 v
  5. 0.99999999999999991 G: M$ `" U! Z3 ^
  6. >>> math.tan(math.pi/6), L3 ]/ P/ A, F$ t1 h' |3 N. Z
  7. 0.5773502691896257
    ) H- \% z; |0 _9 C- q
  8. >>> math.tan(math.pi/3)/ _: k5 Z) T8 I5 J8 O
  9. 1.7320508075688767
复制代码
' a! {' X" G) h+ U7 G) J
math.degrees(x)  把x从弧度转换成角度) \2 ?; v6 C7 }+ c5 d
  1. #把x从弧度转换成角度
    , V7 ^- U8 H0 z- N
  2. degrees(x)7 r8 P1 |& ^  V) z, F8 _( I
  3. Convert angle x from radians to degrees.
    - m4 ?! |3 J# O$ l3 Z' R
  4. 2 U3 O2 |, }( K4 S4 |% R
  5. >>> math.degrees(math.pi/4)
    3 {/ [/ K# I7 l4 m3 G# J1 S
  6. 45.0
    : d3 H6 N" U. d) c6 W& u( B6 W$ t
  7. >>> math.degrees(math.pi)
    ' [+ v4 O* p( q# V' p
  8. 180.0
    4 }4 L4 B) F: G: e2 h
  9. >>> math.degrees(math.pi/6)
    - v  G, W" t* B7 o1 d
  10. 29.999999999999996+ c4 P' P6 Q0 }* l, D4 z0 y9 ~& M
  11. >>> math.degrees(math.pi/3), w, e1 @9 w' B* l/ v4 K
  12. 59.99999999999999
复制代码

; m2 q& S% m: t5 s* M: x* r. rmath.radians(x)  把角度x转换成弧度
+ C# @/ P3 b+ M
  1. #把角度x转换成弧度
    $ G( y5 |. O& x3 O0 l4 f, r4 t
  2. radians(x)
    ! G3 b% ?! R6 I) V9 t
  3. Convert angle x from degrees to radians.
    ( t$ l1 `$ w" R
  4. >>> math.radians(45)% }( _8 f! q- p9 v; l4 r
  5. 0.7853981633974483
    ) F& s' `- T) d, p# l
  6. >>> math.radians(60)
    + w7 O% Y( p) A: A1 t+ c* h+ i$ b6 P" V
  7. 1.0471975511965976
复制代码

; K1 j/ r. ]( Z+ o( A- M) Rmath.copysign(x,y)  把y的正负号加到x前面,可以使用08 ]# h; x. i# z" M
  1. #把y的正负号加到x前面,可以使用0
    ) l' c9 I6 G( D2 b( a  E
  2. copysign(x, y)0 Y/ E. Y4 x$ \) s% q" b' Y) ]# l
  3. Return a float with the magnitude (absolute value) of x but the sign
    . ^) [- s: m! ?; e
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    9 q, n5 a& Z6 I+ @
  5. returns -1.0.
    4 ~4 k9 L0 U/ c2 e7 u) F7 w

  6. ( b* Q9 y  U- o5 b$ H
  7. >>> math.copysign(2,3)1 |4 V1 j$ ]' G  l/ L
  8. 2.0
    * r* T- P! N9 ?0 }
  9. >>> math.copysign(2,-3)$ b: s7 E' j3 d2 V, W
  10. -2.0
    & M- r. @  S  Y# D( g
  11. >>> math.copysign(3,8)4 [8 Y' X+ ?8 S: K; {8 e
  12. 3.0* p) N8 h- F  N' Y$ W  m/ S
  13. >>> math.copysign(3,-8)
    6 {) K( j* ^  l( W
  14. -3.0
复制代码
0 \/ R' }+ q& O3 K" @8 Y0 L% t
math.exp(x)  返回math.e,也就是2.71828的x次方& {1 W+ ]; k  j/ E
  1. #返回math.e,也就是2.71828的x次方
    ! u+ r# c$ U) T7 p4 J4 S8 k
  2. exp(x)1 }" m7 {, X" I) e
  3. Return e raised to the power of x.
    ! Z, l, ~6 l( @# ?0 e. N; g: G+ F

  4. * B: p7 [/ q/ Z
  5. >>> math.exp(1)$ p) G6 ^; w& d2 p! c
  6. 2.7182818284590455 _& b4 g, C, N& C) a
  7. >>> math.exp(2)
    2 E5 T, c  ^5 F# Z$ m
  8. 7.38905609893065, E/ y/ b5 B$ {8 R4 D
  9. >>> math.exp(3)4 S, a, ^1 Q( Y* N
  10. 20.085536923187668
复制代码

, Q( S# m' A7 u! K1 u1 B0 Jmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
7 M8 }! A% c, Q
  1. #返回math.e的x(其值为2.71828)次方的值减1
    : x; ?" }/ v9 P) z$ e
  2. expm1(x)( P6 W. Y8 \+ Z( s* m3 @: m
  3. Return exp(x)-1.: t8 N: P' U7 k8 S7 T' b
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.  x' _7 O7 A1 ~3 x. `

  5. 9 v- ]1 Y' o. K
  6. >>> math.expm1(1)+ g( E% m8 F8 C/ D: K
  7. 1.718281828459045
    . R. I. S# w' N1 k. N4 J
  8. >>> math.expm1(2)
    . c( G. [' \) N% Q9 d3 q
  9. 6.38905609893065
    9 Z* E4 O0 E9 o1 d& c$ ^
  10. >>> math.expm1(3)
    $ i# q3 _, ^- S, ]
  11. 19.085536923187668
复制代码

; Q2 K) Q8 E5 O# C/ u- ]% e1 Bmath.fabs(x)  返回x的绝对值
% S. a. n. ?3 @+ ^. h& _0 n
  1. #返回x的绝对值
    * b4 i3 h$ m6 z* D
  2. fabs(x)1 T. B( J; `6 V- p( \- i( _
  3. Return the absolute value of the float x.
    7 j" c. o& u) u& e7 M5 c

  4. - @* l' y% Y& X+ x+ B* s
  5. >>> math.fabs(-0.003)" P; ?- R, d- u8 W( R: z
  6. 0.0036 q& q/ [6 d9 ]  H
  7. >>> math.fabs(-110)
    1 V% r+ _$ A( p/ L) {& R/ f( E# X
  8. 110.02 w  O4 l: G+ K5 n; }
  9. >>> math.fabs(100)
    4 U6 r5 n" X; W* j
  10. 100.0
复制代码
+ u2 I4 f( E: {) B
math.factorial(x)  取x的阶乘的值1 X8 W6 c. Z* Q
  1. #取x的阶乘的值4 p: U  E: `6 ~) i
  2. factorial(x) -> Integral
    " \+ J" y6 T5 {
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    0 N6 ^- u$ p" g" Y2 s3 T: h
  4. >>> math.factorial(1)
    ( X* J# j1 r, g* F+ y& b( i
  5. 1. c; i) L9 E0 b/ O6 d
  6. >>> math.factorial(2)
    ; i/ v$ H5 Q5 B0 a0 [- F  u! g
  7. 29 ~6 _. L4 P' o( Q, ?
  8. >>> math.factorial(3); w" P' {$ K8 }
  9. 6$ S' n" l4 a. `- z; {2 s( f
  10. >>> math.factorial(5)
    * x! V' @$ D2 i" ]. `/ N* E6 `
  11. 120/ j& r+ g. E7 h6 y. o% U0 M. Q
  12. >>> math.factorial(10)5 D; N5 k. f1 L# J9 N
  13. 3628800
复制代码

; j, K6 P; d- smath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
& b: t: |# ~9 G5 R  r5 ~  w* \
  1. #得到x/y的余数,其值是一个浮点数! s: R8 o" u. C; @2 L
  2. fmod(x, y)
    ) ^& @6 e( @) M9 J
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    # {6 \. `7 [6 A! F+ d
  4. >>> math.fmod(20,3)# M. B% m* n' t
  5. 2.0& E7 @& |% B, w
  6. >>> math.fmod(20,7)
    / q3 g/ v8 B3 ]" B% H* r
  7. 6.0
复制代码

; J6 a# x" p0 }; R7 K4 kmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
8 L. @! N  s6 X; w; {3 j
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- y' k8 n# D  C
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    - h2 _, ~! K' N+ }) Z7 k
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    4 x  C9 ]  u7 t7 C! N6 h! G: Y
  4. frexp(x)+ I3 C( h% u- K2 G9 N4 }* r) U
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 X- e; g, }$ \+ F% @5 V
  6. m is a float and e is an int, such that x = m * 2.**e.& {& n6 X1 C! J4 H
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0./ \6 U8 b1 ?# q9 \1 C7 ]
  8. >>> math.frexp(10)% a' p: o( \% x' L
  9. (0.625, 4)8 _/ O9 @  ]7 H
  10. >>> math.frexp(75)9 d) v6 R; w, @) e/ a
  11. (0.5859375, 7)7 f9 T7 G8 _" Y- D$ S6 w9 M1 g
  12. >>> math.frexp(-40)2 [- z0 ]% g" p4 Q
  13. (-0.625, 6)- q, N/ O8 Q  J; V
  14. >>> math.frexp(-100)) e2 l' b$ M  G7 ]6 N( p
  15. (-0.78125, 7)9 E0 j3 D# K; @0 o9 ^, v+ f
  16. >>> math.frexp(100)
    7 v+ W; u) Y" `
  17. (0.78125, 7)
复制代码
# Z8 N1 m  H: G8 g
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
& M4 a1 B1 a  L8 X5 D, |" \  q
  1. #对迭代器里的每个元素进行求和操作
    ( M9 a, N6 o8 l9 b7 L% K
  2. fsum(iterable)
    0 j2 |1 O7 g) l# Q5 v
  3. Return an accurate floating point sum of values in the iterable.
    9 e6 e5 X/ N. M) ]" z: ?! M, {
  4. Assumes IEEE-754 floating point arithmetic., y# O2 A- i9 K+ }% s
  5. >>> math.fsum([1,2,3,4]); a% _) G% _6 Z3 ^/ H- ?2 O6 `
  6. 10.0
    & O* [' E5 V% Q
  7. >>> math.fsum((1,2,3,4))" E* I9 f6 W( j4 t
  8. 10.0" }6 n1 b9 o  |0 i# T
  9. >>> math.fsum((-1,-2,-3,-4))  @0 v3 B& ~9 H! Q
  10. -10.0$ M: K" f' U$ ~
  11. >>> math.fsum([-1,-2,-3,-4])
    % c' e5 z' P2 f# y: l" K; P0 G  j7 c
  12. -10.0
复制代码

% o, m) j1 `% D' w3 d6 ]math.gcd(x,y)  返回x和y的最大公约数9 C& W8 p0 I& h5 V" Q) g9 Q4 f
  1. #返回x和y的最大公约数" Q* S) q$ ~3 ]2 U: p
  2. gcd(x, y) -> int1 d, N* ]3 o+ z( `. X& k4 c- g( a6 l
  3. greatest common divisor of x and y
    " b9 F8 L" ]' X7 R
  4. >>> math.gcd(8,6)7 }0 W4 M4 c, I
  5. 26 u& V  Q" Z2 N" @! L+ F7 g
  6. >>> math.gcd(40,20)4 i. t% X* Q% u- M8 B! G- _
  7. 20
    + I5 M* L  w3 D
  8. >>> math.gcd(8,12)4 x. G, g- w: p; l8 ]
  9. 4
复制代码

' M  ]' m7 [* X' g' O2 omath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
" n/ z* Y5 q8 V; D
  1. #得到(x**2+y**2),平方的值
    + S# Z" w, Y$ V$ B6 W7 F
  2. hypot(x, y): O5 M' o) X% m' @& n
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 G( c. H' d2 N, a7 m, [( R* b5 u
  4. >>> math.hypot(3,4)
    . N8 i% q4 O( M
  5. 5.07 P7 s) z2 u+ O5 E
  6. >>> math.hypot(6,8)6 r' F& q) h+ R1 ?4 i& t3 t: g
  7. 10.0
复制代码

9 p0 p+ \  U1 y; z; B) K/ F* Jmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False/ A0 W- @9 J5 g+ t! N
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    3 ^% m9 Y4 C- Y( v1 t& w- }
  2. isfinite(x) -> bool! B9 O# @6 }# d9 J/ C
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ! q5 D" @' Y& Z  S$ J
  4. >>> math.isfinite(100)
    ) s2 O# \. l: T. N$ [* L
  5. True
    9 ?2 v$ [7 ]9 e% R2 {8 s
  6. >>> math.isfinite(0). _$ b" n/ d, S4 `3 j  l
  7. True
    / S) x+ C; o/ {5 J% o' ~5 g, ^" q1 }
  8. >>> math.isfinite(0.1)
    3 M. P4 p5 P4 U
  9. True
    4 c- j+ D; ~: E& q
  10. >>> math.isfinite("a")- m0 L$ {# i- Z6 O2 W' A- p% u2 K1 Z+ B! _
  11. >>> math.isfinite(0.0001)
    ; ?! P+ Z; Q9 f- E, M! l# j
  12. True
复制代码
6 l3 M9 u# p" o0 _9 h8 H1 F
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False" v% }8 p: ~$ j0 k+ [! m& p
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    2 o; t4 e% U1 }+ p
  2. isinf(x) -> bool/ n  l/ d1 i' K6 T* w; x
  3. Return True if x is a positive or negative infinity, and False otherwise.
    3 Z& Y+ R8 w- l8 S  J
  4. >>> math.isinf(234)
    6 }) w# I% A6 f6 M
  5. False; a$ k& _. y4 K! w2 p- f
  6. >>> math.isinf(0.1): r4 t' g0 v/ F' }- ]
  7. False
复制代码
9 g% `  l2 S6 x2 U
math.isnan(x)  如果x不是数字True,否则返回False
# l' c! E) D& D' v. d
  1. #如果x不是数字True,否则返回False7 y) j. b+ r1 g4 q5 F
  2. isnan(x) -> bool
    : g; x( O6 j) w/ ^2 @* ?: |- Z' K
  3. Return True if x is a NaN (not a number), and False otherwise.
    $ K: F4 Y& g! R# Z  H( w3 G
  4. >>> math.isnan(23), i( a  y2 }+ h2 W# t( S
  5. False
    : n2 i+ P3 R$ I6 q" y% {
  6. >>> math.isnan(0.01)' C1 L$ d  N- ^( o' T( B. ?
  7. False
复制代码

) P, M/ E8 L5 ?% a0 Mmath.ldexp(x,i)  返回x*(2**i)的值9 l' s5 Q& M8 i6 {. Z1 V3 Z
  1. #返回x*(2**i)的值
    + K3 H; a, X' G. \4 D- ~
  2. ldexp(x, i)$ i9 t; s' |6 l. x* j; \! c# ^
  3. Return x * (2**i).
    ' k) K5 d# W6 f& H6 y9 i
  4. >>> math.ldexp(5,5)% x8 H# ^2 r7 [" O1 a; F9 r: K0 D
  5. 160.06 g/ i: W( a5 a% A
  6. >>> math.ldexp(3,5)! ^& l. {6 Q6 P8 u) B# f) `/ L" `
  7. 96.0
复制代码
- J0 B, ^' y2 g# m6 c' ~
math.log10(x)  返回x的以10为底的对数' I  i2 s3 _3 v9 ~; H# j6 Y
  1. #返回x的以10为底的对数  g, W# }  @4 d9 f# S3 n( d
  2. log10(x)3 G7 Z( K" v& e* ?  ^1 x/ \
  3. Return the base 10 logarithm of x.
    ( I2 Z2 q% i  {& M- g  I6 D
  4. >>> math.log10(10)8 l6 U' f. v2 e' h
  5. 1.0
    . v! x7 M9 E; l# w; U/ r% I' B
  6. >>> math.log10(100)
    , s4 K. k. I5 N- s% p
  7. 2.04 _# X! d3 `( u: ?% L7 t
  8. #即10的1.3次方的结果为20
    : L# A% r# V3 C0 \: \8 n# n. l
  9. >>> math.log10(20)
    3 _! H) d5 z1 J8 z! Q$ S! K
  10. 1.3010299956639813
复制代码

# B: ]. q6 Q  {( W/ S3 A( l4 \math.log1p(x)  返回x+1的自然对数(基数为e)的值
! a" _. M6 e% v# K& h% [6 l3 l
  1. #返回x+1的自然对数(基数为e)的值( C' T6 X/ i9 d4 K, A/ i' m) V% Z) K
  2. log1p(x)  S6 n- A! ]" Q2 z  ~& {3 d& |6 }! T
  3. Return the natural logarithm of 1+x (base e).
    + `/ p+ M+ o! d, J* t
  4. The result is computed in a way which is accurate for x near zero.
    * i4 \9 E( e( m) T1 r9 M
  5. >>> math.log(10)) D5 H2 l% d2 u" t& U
  6. 2.302585092994046+ b' t9 E2 q" e, Z" x
  7. >>> math.log1p(10)* ?2 p2 X! A& `  S' {+ Q: t, g
  8. 2.3978952727983707, y& U! a8 s" D: D# A
  9. >>> math.log(11)
    5 _2 y, }" O% B) f* ]
  10. 2.3978952727983707
复制代码

5 H; V; Q' F. R6 [, Q7 m! `" b7 {math.log2(x)  返回x的基2对数
+ g8 V2 Y/ s% H3 q' q  M2 F
  1. #返回x的基2对数3 f- ^+ j& g4 A6 N1 a* m
  2. log2(x)
    8 h( g1 w- s6 K! T
  3. Return the base 2 logarithm of x.- k, i$ L8 U& }0 T
  4. >>> math.log2(32)
    ( z6 S) p/ n# i. l* J6 |7 X! ~
  5. 5.0: T8 y% `( c' C) b1 \4 g' z$ }$ u
  6. >>> math.log2(20)
    , E- P6 `) w# P7 b1 x  n5 n
  7. 4.321928094887363& `9 n6 m0 W$ ]1 e) n
  8. >>> math.log2(16)
    3 B5 F+ c4 y4 h$ y4 z( F5 P
  9. 4.0
复制代码
0 c- z" _- C3 L3 _- Q
math.modf(x)  返回由x的小数部分和整数部分组成的元组
3 Q) d( n+ ~6 n3 P0 h! {; Q
  1. #返回由x的小数部分和整数部分组成的元组3 S  v' L* m, Z2 [, X
  2. modf(x)
    ) a9 q! O+ M: D3 v) X: P% V3 o+ B
  3. Return the fractional and integer parts of x.  Both results carry the sign: u" s' a( g  g% r
  4. of x and are floats.
    7 l. {4 B2 I3 b  z% D
  5. >>> math.modf(math.pi)
    $ T) p  m% |) O7 E& [
  6. (0.14159265358979312, 3.0)8 s: a; e/ B- r+ K& C8 E% J7 a9 D3 J
  7. >>> math.modf(12.34)$ j& v) e% v0 X7 X, E+ h# T
  8. (0.33999999999999986, 12.0)
复制代码
% D  U% o1 @3 A- A* ^# o: q
math.sqrt(x)  求x的平方根
) i* q9 O: w! E, p3 s
  1. #求x的平方根" N; \  ^: D  t! l
  2. sqrt(x)) H  F, o: @/ e$ |/ K& v8 [( N
  3. Return the square root of x.9 t+ E9 S" G, Q5 Z
  4. >>> math.sqrt(100)% w% N" E0 [' Y4 e" K" J9 a6 z
  5. 10.0
    # f. f/ S' w' B2 w( c$ H! c
  6. >>> math.sqrt(16)( W1 d7 V) Z5 `6 H+ d
  7. 4.09 A5 k: b$ e" ~3 e) Z9 Z# ~7 H
  8. >>> math.sqrt(20)$ u) O+ M+ M" K' w% X' U
  9. 4.47213595499958
复制代码
! o4 K9 V4 d* y' F  m  n$ f
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分/ H2 T, r! U% _( p
  2. trunc(x:Real) -> Integral
    $ p* ~4 ~! O1 Y% l" u! I
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ! w- L0 S2 g: \' t
  4. >>> math.trunc(6.789)8 Q) ?% o" q; p
  5. 6
    * Y5 O3 ]0 F' H% ?( ~7 r9 p
  6. >>> math.trunc(math.pi)# m# P* m  r7 t9 u7 p+ ~
  7. 3  f( N) P/ {2 r, `2 ?
  8. >>> math.trunc(2.567)! M, u5 W7 X5 b9 k
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-30 13:33 , Processed in 0.081543 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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