新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
3 G) Z0 R! @( w4 w- u+ X$ S) d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 W5 s% r0 a, Q8 _5 x$ w2 C
& i" R3 k: o  C: n. i0 _- G
方法1' r) X8 z" \* u! b/ c
  1. >>> import math$ h& |- e) }  B
  2. >>> math.sqrt(9)$ o) [4 s4 M7 g3 T' S) h+ {
  3. 3.0
复制代码
方法2& f% p1 g. j. Z- A; \
  1. >>> from math import sqrt9 R0 h1 F9 |1 e8 L- f6 t: y. c8 D1 R
  2. >>> sqrt(9)
    & @% F+ I# r7 @" K5 d3 F
  3. 3.0
复制代码

+ b1 e' _& M9 c5 ^! C
# |( E, H5 F- z  R- `3 D# H$ Y
math.e  表示一个常量5 o, [' K3 p# b
  1. #表示一个常量
    3 n3 ~; g5 `6 [2 d
  2. >>> math.e
    ; F5 J7 c3 |) s& h; g5 H$ l6 g2 D
  3. 2.718281828459045
复制代码
$ ?) E0 f# N2 A1 E
math.pi  
数字常量,圆周率
0 c* l1 t4 |3 C6 ?$ z
  1. #数字常量,圆周率
    , k* |( d  ^: b$ H7 |! p* {
  2. >>> print(math.pi)8 \7 U% O' r- z" ]- |: L' M
  3. 3.141592653589793
复制代码
5 C0 O4 J) h, \6 |! A! J/ J( v
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
) I, S( |8 l% s# w* s& ]
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ) W& w/ h' E# K' {: ]9 i6 n# U9 o
  2. ceil(x)
    " w) j' P9 r* ~/ V$ C
  3. Return the ceiling of x as an int.
    , g- ^. W7 u, b2 z4 r+ m$ w
  4. This is the smallest integral value >= x.
    ( c0 i2 y& y) z! |! x. Q4 P( Z
  5. 8 D# `3 E. N% S4 I/ S3 z. z
  6. >>> math.ceil(4.01), G6 l4 J& P8 d' X6 I2 x9 `
  7. 5
    # C8 T8 N/ ]  H
  8. >>> math.ceil(4.99)6 n# R: q" \+ \6 f, U9 P
  9. 5
    % T& r6 D1 D6 I
  10. >>> math.ceil(-3.99)
    7 c: {0 }  Q# Y
  11. -3# ~7 e2 w6 ]% N  ]
  12. >>> math.ceil(-3.01)' W: c- ~% L/ O+ i! ?. r
  13. -3
复制代码
# _4 t+ c8 Q, b' [$ z- H
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ U# x: c+ n, Q( {$ ?: b. v8 b
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% u$ }$ y# \- `# R5 Y# h
  2. floor(x)
    , Z% i' H1 b/ z# B* F+ H
  3. Return the floor of x as an int.0 E& N2 K& V. ]! \5 _
  4. This is the largest integral value <= x.: I$ z5 ~9 P1 n; p2 |! z
  5. >>> math.floor(4.1)
    2 c6 V. X) L7 }7 m; l
  6. 4
    6 ~+ x! K& u' E4 l0 t' |
  7. >>> math.floor(4.999)
    ; d( h2 ]% v8 q; Y6 W" `' e
  8. 4: \/ j- H* u) ^( y# K; V; x
  9. >>> math.floor(-4.999)5 b: R! o" h2 Y0 R
  10. -5  [6 h( X4 e/ M* }6 Z6 H' x
  11. >>> math.floor(-4.01)5 b# d. ~8 B3 ]" q
  12. -5
复制代码
% P' V( X! e3 G: z) N8 r
math.pow(x,y)  返回x的y次方,即x**y) F3 I% f' A2 E" r8 Q
  1. #返回x的y次方,即x**y+ w0 U! H! @/ O; R+ i4 K/ L
  2. pow(x, y)
    2 c5 C" {- n) q
  3. Return x**y (x to the power of y).
    7 u, m  b8 K: i% m5 D1 H. ]
  4. >>> math.pow(3,4)
    / L) J, c+ _& g: V$ J$ m
  5. 81.06 V+ P+ e2 j, q
  6. >>> ( N& r5 U5 K, [5 x6 ^
  7. >>> math.pow(2,7): b4 ^$ J7 |& L
  8. 128.0
复制代码
6 A+ g, k, a9 l
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ \* T  L' c1 ]& k& E- P! `
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); Q! c4 K! F8 }+ F( G
  2. log(x[, base])8 S. P) H7 G1 h1 ?6 e
  3. Return the logarithm of x to the given base.
    4 `1 k4 O$ W4 ^( i" H. J! n  M$ ?
  4. If the base not specified, returns the natural logarithm (base e) of x.
    1 a+ J& D. D4 ?: |! D; i
  5. >>> math.log(10); A" ~: E4 E: \) l( ?/ g
  6. 2.302585092994046" _; `& b. ]5 d) i* s: P" k' z
  7. >>> math.log(11)' I) Y/ o! o* O0 l
  8. 2.3978952727983707. ]% ]/ C0 J* C8 s4 ?5 b
  9. >>> math.log(20)
    % U, j/ i, i8 j/ y/ D# t  J, l2 B6 j
  10. 2.995732273553991
复制代码

8 }0 _- L$ u: k% C8 _2 mmath.sin(x)  求x(x为弧度)的正弦值
4 K6 `! T% o+ Q4 j
  1. #求x(x为弧度)的正弦值$ [2 e' C6 P  N( @1 V$ }# c- `
  2. sin(x)
    , O6 w& u4 J/ M% t/ ^$ `
  3. Return the sine of x (measured in radians).
    + ^( |, j6 n- Y  q! ^$ z
  4. >>> math.sin(math.pi/4), x& F1 A# ^9 P4 s% C/ |
  5. 0.7071067811865475
    9 |( z0 P0 D  L: m  H( I2 @
  6. >>> math.sin(math.pi/2). z$ o! h" U6 E7 O" H
  7. 1.0  @( j* w# W4 h! I2 S3 X+ p3 `
  8. >>> math.sin(math.pi/3)
    $ G8 H# [9 x( z( m" H
  9. 0.8660254037844386
复制代码

0 s( c* y; f# r$ Q- N, j8 a- Amath.cos(x)  求x的余弦,x必须是弧度$ o) r1 J  p4 D. i2 j* }8 |# |5 F
  1. #求x的余弦,x必须是弧度
    0 b% O8 h% Q# M: x
  2. cos(x)) l0 o' s8 Z# I5 s4 s+ {
  3. Return the cosine of x (measured in radians).
    , @, U( j# `9 C# {* M
  4. #math.pi/4表示弧度,转换成角度为45度9 B  U" J1 K% y$ [5 B" ]
  5. >>> math.cos(math.pi/4)
    2 E% {2 O4 c. q5 e+ d' j6 K
  6. 0.7071067811865476
    1 \9 T; w! k+ I6 G
  7. math.pi/3表示弧度,转换成角度为60度
    % v' y/ F& x! k9 O' @
  8. >>> math.cos(math.pi/3)9 X1 L, n# Y  ]0 s0 ~* o
  9. 0.5000000000000001' ^5 Y7 @* D2 c0 H; S' G! L" ^
  10. math.pi/6表示弧度,转换成角度为30度3 P0 C& W. t, |, D6 N
  11. >>> math.cos(math.pi/6)3 h. y6 Y. |4 h& \4 p
  12. 0.8660254037844387
复制代码

" W& ~1 V/ J% m4 Z% X6 H: [/ Wmath.tan(x)  返回x(x为弧度)的正切值
! c. d+ P$ w6 Z1 C5 R
  1. #返回x(x为弧度)的正切值
      f, m1 l' |0 T0 G! o
  2. tan(x). V7 h* }" B3 z0 `1 _+ U! l% k$ d
  3. Return the tangent of x (measured in radians).
      d) u7 S9 c7 x1 j# S$ \$ U
  4. >>> math.tan(math.pi/4)1 ]# w1 z. i$ D" I" A2 s! {8 ~! i
  5. 0.9999999999999999; p  e! L6 P# v, ^: `2 ?
  6. >>> math.tan(math.pi/6)& `/ S. P2 F* s& T3 o9 F
  7. 0.5773502691896257
    7 W5 }2 }9 A; h0 H
  8. >>> math.tan(math.pi/3)8 j  h( `! @) A$ L% ~
  9. 1.7320508075688767
复制代码
8 X& Z' H" `( D$ g, m7 h" Y; m2 m; d
math.degrees(x)  把x从弧度转换成角度* M" U! u! D6 Z9 I% {) T6 J1 y8 S
  1. #把x从弧度转换成角度' ]6 \* }& T( l6 j3 w. _' \1 ~
  2. degrees(x)9 ?  D+ \1 k* `. E) p3 J/ d
  3. Convert angle x from radians to degrees.. I( ^5 G1 d- t+ f) ]
  4. $ M  c# v; ?' B" E) l: B
  5. >>> math.degrees(math.pi/4)
    - Q$ d2 b1 i* U1 {
  6. 45.0
    : v2 p( C4 ]. H/ ]5 D
  7. >>> math.degrees(math.pi)
    5 \( F# M- F& Q* a
  8. 180.0% q- U% t. \0 }/ D% d
  9. >>> math.degrees(math.pi/6)& b; {2 }& n' C! z' T( A; M0 T1 E( ^
  10. 29.999999999999996
    * P! ^2 T& J' U
  11. >>> math.degrees(math.pi/3)# F- B8 R' d' Q
  12. 59.99999999999999
复制代码
+ V* i6 B0 F% q! g9 X
math.radians(x)  把角度x转换成弧度7 g; c. p1 }/ O/ B. P
  1. #把角度x转换成弧度' I7 R: _3 Y( v2 K
  2. radians(x)3 w& Y( _+ `% h( b7 R, @
  3. Convert angle x from degrees to radians.) u$ y1 X5 ^# L+ }
  4. >>> math.radians(45). y0 m# l, |8 J9 I; Z! p" j  G7 N
  5. 0.78539816339744836 r+ x8 q1 T. g7 v, e1 B6 }
  6. >>> math.radians(60)
    $ H+ _  R8 D: b) x9 I; i' ]
  7. 1.0471975511965976
复制代码

# |3 p/ ~& K6 l3 Z9 Fmath.copysign(x,y)  把y的正负号加到x前面,可以使用0' e  N- k, u) U) N4 C
  1. #把y的正负号加到x前面,可以使用0% W2 y8 D( p/ V: c
  2. copysign(x, y)
    ( ?# v: k' |2 d. A: ]( r0 F5 W
  3. Return a float with the magnitude (absolute value) of x but the sign
    ) A8 ^5 F( v$ W! E8 e% S, A
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , T/ \2 V3 J5 w* x! ?
  5. returns -1.0.3 n5 W$ C8 W: f8 E

  6. 8 G% b  b3 d( K- p! G
  7. >>> math.copysign(2,3)% w! _( v( k+ K( R
  8. 2.0. X; V- J& X7 y2 A
  9. >>> math.copysign(2,-3)
    + o' A( s6 X+ u. e6 Q5 T6 R1 e; t' q
  10. -2.0% b  B8 h6 ^! e( X# x7 g, _! B' n3 t
  11. >>> math.copysign(3,8)2 h  `& E$ x" Z( x5 t0 W. L
  12. 3.0! T* J5 I% D$ z# t- \. s
  13. >>> math.copysign(3,-8)
    ; C5 f- Y- m: {" ^6 G. G7 E$ a- m
  14. -3.0
复制代码
1 F2 ^4 ^* m0 h0 S2 B
math.exp(x)  返回math.e,也就是2.71828的x次方
. {6 u) F: x, o7 R
  1. #返回math.e,也就是2.71828的x次方/ ^/ f/ u; \7 g( X& S# k/ j
  2. exp(x)6 A' h, J* T6 w7 s, F$ E" q
  3. Return e raised to the power of x.
    ( H  h( L8 e, X
  4. . N  w  N- g& i6 B6 z! g
  5. >>> math.exp(1); v5 a) e  b' p  x) ]5 W9 d
  6. 2.718281828459045: B* I6 u, g3 O* k3 `3 i$ g8 U
  7. >>> math.exp(2)
    # I* K& A4 \! H/ ~0 T
  8. 7.38905609893065& Q6 M) B- F/ ?! P6 v, }  \% V
  9. >>> math.exp(3)
    ! d  W9 H6 a# G' J6 Y" Q$ ?' M
  10. 20.085536923187668
复制代码

: {9 @) u; q4 D! Mmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1* D8 L7 I8 ~' a+ _  G
  1. #返回math.e的x(其值为2.71828)次方的值减1  N1 u0 b  \5 p3 e$ u6 w
  2. expm1(x)
    2 P9 T3 l5 `! \! d: Z' k
  3. Return exp(x)-1.. }1 T6 H. L) e7 v% I9 @3 p  w
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x., H* {8 [4 a- P. a0 s( V

  5. 8 P! j  O$ q, Q) h2 B, `" J
  6. >>> math.expm1(1)
    5 O$ z3 ~+ u- ^, g4 B4 V& F. t7 S
  7. 1.718281828459045
    0 m8 n4 ?+ s' K+ O' R
  8. >>> math.expm1(2)
    : _: b+ \# c8 g" t. X
  9. 6.38905609893065
    % p# r+ m# ~7 \4 D
  10. >>> math.expm1(3)
    % l* _7 D& H1 Y; @
  11. 19.085536923187668
复制代码
: ~2 z# a3 Q9 ~: P, |6 a3 x+ F
math.fabs(x)  返回x的绝对值
) }+ B0 ^' @; N- u0 c, b
  1. #返回x的绝对值
    + _: D$ \) l1 h, z/ T
  2. fabs(x)
    , [% a0 c9 U+ _
  3. Return the absolute value of the float x.. e2 P* t0 J1 P- D) f3 m+ B

  4. . I" N* p% O& ^  b, h9 A; S5 {
  5. >>> math.fabs(-0.003)" J+ |' H, Q% d# X" O
  6. 0.003
      K$ e8 Y; }4 G; h
  7. >>> math.fabs(-110)8 B3 f% p* R* S" ?9 `1 M1 ?: z' _
  8. 110.08 {1 A2 b! I! L/ A  |5 Y: m
  9. >>> math.fabs(100)
    2 t& f6 t: L2 l1 ]$ l  a. \& h1 r9 c
  10. 100.0
复制代码
) ^- O- K0 j# R% ]# b1 Y9 h; `: c" }
math.factorial(x)  取x的阶乘的值0 ^5 S, B/ T  H- B2 \, G
  1. #取x的阶乘的值2 j1 e4 r& R) x) V" v/ s
  2. factorial(x) -> Integral" ?5 Y' v: i$ j7 W( C. F# L
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    0 s9 m- u  e, K% ^# F, Y* q( `, |
  4. >>> math.factorial(1)* [  h$ G8 F  H& t* M+ Q7 U* D; \+ y
  5. 1
      J/ Z9 _- \8 o/ X  }
  6. >>> math.factorial(2)
    ) t9 ~6 |& e& a- R3 L
  7. 2
      v$ a: R# d$ [# j6 f
  8. >>> math.factorial(3)
    3 ?$ v2 X6 L2 u+ c7 E4 r2 V/ h
  9. 6" V* U: a) e- W/ o( T
  10. >>> math.factorial(5)
    2 \( w/ M1 }" G# {/ n
  11. 120( U. j6 {5 g, x
  12. >>> math.factorial(10)
    2 t5 q8 r/ ^" Q1 S5 S( m  f1 @
  13. 3628800
复制代码

8 w9 _; R) ?1 kmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
+ }! ?2 r2 p. h. g
  1. #得到x/y的余数,其值是一个浮点数
    ! C3 u1 D2 a8 p$ Q2 l
  2. fmod(x, y)9 W# o$ c: U2 i4 H2 j
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    4 z1 U" S/ @9 j$ a9 J& \
  4. >>> math.fmod(20,3)
    5 O/ h& t9 K* |- Y! t1 S
  5. 2.00 P3 \0 \0 U& \8 R5 t
  6. >>> math.fmod(20,7)4 ]3 }% K6 e8 a8 R' O% i
  7. 6.0
复制代码
+ |& x5 Z8 U0 f2 j
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" c+ ~' u3 H% d: J2 r& `
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ! e& q6 z  i2 D  k9 E* A
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    9 f' U1 w7 ?0 E# `/ v
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1! K+ @3 F5 `* z: s0 j- c
  4. frexp(x), D$ A8 B$ S2 Q$ m& @1 y. M
  5. Return the mantissa and exponent of x, as pair (m, e).
    : ~  v* [" N2 o/ \, A' N+ p& ~% ~
  6. m is a float and e is an int, such that x = m * 2.**e.9 W, L, M+ n' Z# ~
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.$ H% [# o3 f# E' u8 m* k
  8. >>> math.frexp(10)+ z! ]8 [* X4 A& z: y
  9. (0.625, 4)
    ' v, b( F$ t% w. X8 T
  10. >>> math.frexp(75)
    5 ?" h; q& S: l! n
  11. (0.5859375, 7)- c# r2 J' Y8 k( H7 V
  12. >>> math.frexp(-40)0 M4 @' Q) n+ a4 }" H. F' W
  13. (-0.625, 6)
    ( {. S- f4 y9 p! Q8 S
  14. >>> math.frexp(-100)5 a& h/ f0 w5 z" E8 R
  15. (-0.78125, 7)
    ; M  R0 J/ e4 p
  16. >>> math.frexp(100)
    8 y1 c9 Z9 J" J' y/ L9 L2 P, Y+ K
  17. (0.78125, 7)
复制代码
* s/ m$ z- A- p3 T: f' j' @, q- Z
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列% K" J' k  l" g
  1. #对迭代器里的每个元素进行求和操作
    6 ]3 ^* s2 B6 v$ v: h8 u7 A- e
  2. fsum(iterable)
    7 O/ I* N( L* _, X/ P) [8 Z0 o
  3. Return an accurate floating point sum of values in the iterable.
    % ?- m9 F6 Z, v, A7 ^
  4. Assumes IEEE-754 floating point arithmetic.
    * \# g% L  o3 V- {/ a7 A
  5. >>> math.fsum([1,2,3,4])
    ( L9 m* j' W: J# d) ~
  6. 10.08 V; ~% `3 L- o& E. M
  7. >>> math.fsum((1,2,3,4))
    ; ^/ \" ?  e5 a8 u% O
  8. 10.0
    . o  \. E: f. T4 Z$ M( Q1 c# G
  9. >>> math.fsum((-1,-2,-3,-4))
    $ x2 G1 @; q  @% f
  10. -10.0
    ; n, \" P+ L+ H, @" t1 [
  11. >>> math.fsum([-1,-2,-3,-4])
    3 j: E, N5 }- b
  12. -10.0
复制代码
7 D5 ?- R5 O! \( p* g7 F
math.gcd(x,y)  返回x和y的最大公约数" Y, g; i/ @1 ~% B1 ~3 H
  1. #返回x和y的最大公约数
    8 z, k; Z3 L" F+ h5 o; k0 d9 |
  2. gcd(x, y) -> int+ x* w5 R# f) z
  3. greatest common divisor of x and y7 N8 g; J* {& V3 x4 H3 J& C
  4. >>> math.gcd(8,6)
    ( i! L9 \3 ^6 O
  5. 2
    0 N9 g+ ]4 w! ]  |1 D& @8 m
  6. >>> math.gcd(40,20)- t$ t1 P5 f) h% Q
  7. 20
    % Y7 }+ `% V. e
  8. >>> math.gcd(8,12)
    4 V9 o, U- w, i% q; q" {; a
  9. 4
复制代码

  p" R& ~8 E; t" w% X: e4 h# M& ?math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
, p9 m, B, m. h5 M+ i/ N. @. j
  1. #得到(x**2+y**2),平方的值! v" m! V+ F6 a: A. M0 w
  2. hypot(x, y)! t7 I0 s! j3 k& P
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    , U6 [! I+ N# x. ^
  4. >>> math.hypot(3,4)
    - n8 r3 R% I: ]
  5. 5.0. d; b# J4 W& Z/ s
  6. >>> math.hypot(6,8)+ l5 O& [1 g5 Y9 n
  7. 10.0
复制代码
7 A* ~6 ]! A+ q& u& B$ m
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False( f0 b2 U7 v) F: l* l
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ( h% ~! o# ?5 x* r
  2. isfinite(x) -> bool6 q& N/ v" a3 _/ X; N" l
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.) c2 g% S. ?1 o4 ^7 P' Z
  4. >>> math.isfinite(100)6 V5 y% N2 t  v+ b. r( V
  5. True- _3 m8 t1 ]" M* g7 d
  6. >>> math.isfinite(0)
    6 O" r7 j1 E# R, N
  7. True# `& z0 o& ]- u$ \
  8. >>> math.isfinite(0.1). ~: j1 \+ \$ z9 A1 @/ i
  9. True
    . N: R" Y" g* x% q6 \& K& J! S
  10. >>> math.isfinite("a")
    , k- [7 n& p8 F, u" @% x2 F, q& U, l
  11. >>> math.isfinite(0.0001)
    : e6 _8 i7 G$ w- t1 E0 K4 [( H
  12. True
复制代码
2 r  o" g1 Z9 H; e$ n( r% e% o
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
, }& T) G$ B) p* r' r
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False  U$ A1 r* k* u& l
  2. isinf(x) -> bool7 ~* {1 B) r5 e  ~  E
  3. Return True if x is a positive or negative infinity, and False otherwise.
    , G  \6 }; b% t7 K
  4. >>> math.isinf(234)9 v" n- P( O% V6 {. L
  5. False! ?  \4 m8 Q+ o/ U; p! m6 R6 |
  6. >>> math.isinf(0.1)7 f; X& _% ^; N% M3 a. I: e
  7. False
复制代码

- X" r5 C1 c8 X. C0 `4 G" S; d& N. _math.isnan(x)  如果x不是数字True,否则返回False
5 O. E3 A' Q) H' a
  1. #如果x不是数字True,否则返回False) T6 `2 x$ q8 H( U, k0 b
  2. isnan(x) -> bool/ F9 ^6 G2 U% r  }0 ]% K) j8 n
  3. Return True if x is a NaN (not a number), and False otherwise.
    3 p9 c# z$ q) S6 v% Z( C$ Y
  4. >>> math.isnan(23)
    " P# l* c2 {* u! F$ r6 _
  5. False
    % h, L5 o* w5 ]0 }: v* ^
  6. >>> math.isnan(0.01)+ [) x: q8 y  C& y
  7. False
复制代码
( t# M; j& h2 h) n
math.ldexp(x,i)  返回x*(2**i)的值7 m3 Y7 V5 T5 y0 ~; i- F' R: f  X
  1. #返回x*(2**i)的值
    2 \& n" G( }7 B+ p+ z- k: P2 O
  2. ldexp(x, i)
    % @" d/ A% Z- |" l8 ~
  3. Return x * (2**i).
    / I+ F+ i+ f: ^$ v& {' L
  4. >>> math.ldexp(5,5)( C& o$ H* [2 z1 @- L
  5. 160.05 M' X* V: L! t3 M1 c
  6. >>> math.ldexp(3,5)
    + ?1 J* `4 }0 F8 U! g
  7. 96.0
复制代码

. \* z, K% H% G  J1 Cmath.log10(x)  返回x的以10为底的对数1 b% M9 E# I! A& w
  1. #返回x的以10为底的对数# X3 V8 J9 C- ~5 A5 W! H4 |
  2. log10(x)
    8 I/ D  ]" D7 M1 g6 y( Y9 Z" f
  3. Return the base 10 logarithm of x.
    + m( @+ \  M" u& V
  4. >>> math.log10(10)
    / T" _0 s" T3 d
  5. 1.0
    0 y8 S6 u4 ]& @) M, r" a
  6. >>> math.log10(100)# k2 @$ g2 o) Q# k- ~, F' ^$ L4 P) f2 O
  7. 2.0
    % N# f3 a! z  J$ h* I5 }
  8. #即10的1.3次方的结果为20" r# ]  _  e8 k% k, S
  9. >>> math.log10(20)* P; {8 }7 y; G9 S; i
  10. 1.3010299956639813
复制代码

. S: A& Y/ N8 @# [! }) Jmath.log1p(x)  返回x+1的自然对数(基数为e)的值
* ?; V# `: ?( {6 _$ U9 P( y
  1. #返回x+1的自然对数(基数为e)的值
    ( x; i6 _, N+ R
  2. log1p(x)# G; r, J) _! J' f
  3. Return the natural logarithm of 1+x (base e).
    1 o2 Q, U" o2 o  k2 e( z
  4. The result is computed in a way which is accurate for x near zero., M3 S& K% ]) ~
  5. >>> math.log(10)
    % V/ X0 f6 R. s5 B$ w
  6. 2.302585092994046
    3 ]5 P2 x: ^3 Z. K) [& s4 E
  7. >>> math.log1p(10)' y# y5 I+ L' D. `
  8. 2.3978952727983707) \) z) R* G9 W& R
  9. >>> math.log(11)
    1 K. D1 a8 V  E0 s" I# L
  10. 2.3978952727983707
复制代码
9 p* G$ L5 f- t# n' k+ A. p0 w
math.log2(x)  返回x的基2对数
$ f# J2 a  I4 z6 T8 W; p
  1. #返回x的基2对数; s0 q- e, i' x; U& |
  2. log2(x)7 F; Q& ?! U- Q! m4 j
  3. Return the base 2 logarithm of x., M* I/ a& w+ E. p/ ?8 H$ p
  4. >>> math.log2(32)8 S" ~- f) z/ _" n8 m
  5. 5.0
    + A2 n( G8 \7 L% g4 T- x* n2 L
  6. >>> math.log2(20)
    . B, F' d$ i$ f5 [2 F$ Q  |, Y
  7. 4.321928094887363
    ; B5 U. d5 \9 {5 f( k' h  U8 q
  8. >>> math.log2(16)
    & y0 F0 ]+ y) _6 v
  9. 4.0
复制代码
1 ]8 k! M9 u/ E6 Z3 y8 i
math.modf(x)  返回由x的小数部分和整数部分组成的元组
' R7 X$ l2 ?6 I. N! Q
  1. #返回由x的小数部分和整数部分组成的元组- ^2 Y) W+ Y" g5 V) ]% g4 e
  2. modf(x)
    6 K/ C/ @. z7 ], B8 i5 D# g9 ?
  3. Return the fractional and integer parts of x.  Both results carry the sign. u1 _5 x9 j, B2 p3 |
  4. of x and are floats.
    $ z% y, b7 V' _
  5. >>> math.modf(math.pi)
    7 }$ d, ]' W0 s5 u
  6. (0.14159265358979312, 3.0)7 t8 K/ W& l8 \/ L4 O( U
  7. >>> math.modf(12.34), z5 m) c' A) u+ Y8 e4 z( _+ K" |8 j1 Z
  8. (0.33999999999999986, 12.0)
复制代码

- W% X; H; O8 o$ vmath.sqrt(x)  求x的平方根# J& G# w/ m+ o7 b% k: @
  1. #求x的平方根: T  A2 s3 {- ^- j" @& z
  2. sqrt(x)4 M% G) d0 u7 M! ?4 X9 b9 T
  3. Return the square root of x.' D4 |" j8 V2 B& {) _
  4. >>> math.sqrt(100)
    " ^7 Y) _8 A2 y/ r
  5. 10.0! O# a4 u8 B( F6 k
  6. >>> math.sqrt(16); a# V. I( G3 E) V; \2 q+ s$ W
  7. 4.0
    / C1 K- s" a% e- A8 {
  8. >>> math.sqrt(20)  y! B: G, q) J) k$ q- w+ o
  9. 4.47213595499958
复制代码
' k- N2 u7 M" Y! Y! a+ w+ h
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    " k3 l& m# f; X$ h
  2. trunc(x:Real) -> Integral
    ' t3 ]+ x: d, f9 P
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: f, |5 H) p; d7 \7 u) h3 H3 j
  4. >>> math.trunc(6.789)
    9 V9 E- T8 a& i* r7 E: m, K. d& \
  5. 6: X3 I4 \- J; \" T. e! |0 U! {
  6. >>> math.trunc(math.pi)! f, }4 I2 Q+ B% l; Q( Q  F
  7. 3
      d* D, [3 I4 n0 A: N
  8. >>> math.trunc(2.567)+ A% o" a2 |$ v4 N) G& C
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-27 16:00 , Processed in 0.093011 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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