新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
8 {- h7 b0 H4 `0 l5 V) v: p, n
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
* k% X- K( @9 u" p8 `) N

' d4 R# w0 k9 F) J+ d1 g( e% ^- }方法1
$ `8 [, T$ F4 q2 I$ E9 C5 }
  1. >>> import math
    9 F- J+ e2 H" x7 X# b3 _; n# x
  2. >>> math.sqrt(9)
    3 d3 ]" W4 ^5 C" f
  3. 3.0
复制代码
方法2
9 w9 U  f2 N! d, |% U2 K
  1. >>> from math import sqrt* m9 V( H& V5 |; j* X
  2. >>> sqrt(9)
    4 J- J4 K" f( J4 Q  Z4 i1 J
  3. 3.0
复制代码
6 N8 \0 A+ _6 b* {% ?  R) J  C' X


+ @* E  Y* a. M6 F0 d' {
math.e  表示一个常量5 M/ W$ I! A- ?" Y8 A5 q
  1. #表示一个常量
    / m# j6 S0 x( @$ A3 c
  2. >>> math.e
    ) t8 i) U5 S/ U2 Z4 ~
  3. 2.718281828459045
复制代码

  l  h+ P$ @8 d6 V* b7 _0 Omath.pi  
数字常量,圆周率
7 r) p, O4 _3 a
  1. #数字常量,圆周率3 |6 L0 f& ~: f3 w& h6 D+ h6 e& `
  2. >>> print(math.pi)0 O* E. g- w% C) }: }9 c
  3. 3.141592653589793
复制代码

( F6 I3 |& P# U, u& v. z! b* Pmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
2 M# E( ?# S* G! h" U( a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x; J4 {0 V6 B! A' M6 z0 S
  2. ceil(x)
    & h) l3 [/ `& }) U, q
  3. Return the ceiling of x as an int.
    4 i) r' ^2 V. M3 R& W
  4. This is the smallest integral value >= x.
    5 s! g3 Y7 x- r5 g) J- K, Y
  5. 5 Y2 ~: r1 I. q' g
  6. >>> math.ceil(4.01)
    ) n3 A) z! k7 V! ?9 A
  7. 50 n2 \4 q0 T; }
  8. >>> math.ceil(4.99)
    " Y, A/ R, S2 r0 p
  9. 5
    . B( K8 Y1 b" M
  10. >>> math.ceil(-3.99)
    * X, I. X9 q# E6 y1 J2 X, q
  11. -3- s! n& T$ T7 a: l
  12. >>> math.ceil(-3.01)* q/ s7 I. ?" M- N3 F! T
  13. -3
复制代码

2 X- L. A! l& R  I  }: C4 |math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身) J$ F( Z* K" a& Q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    $ ~7 n; C( O8 u5 h8 Q; G' d1 \/ Z
  2. floor(x)
    ( ]& b# G5 Y( Y6 C& d) m
  3. Return the floor of x as an int.0 d- q- p8 I7 n) _' [3 L
  4. This is the largest integral value <= x.' _9 y+ {8 ?" e
  5. >>> math.floor(4.1)* x: e, ~; z4 _" u0 p
  6. 47 O* P4 K. ^9 z$ q3 }! h
  7. >>> math.floor(4.999)  M2 _+ u' Y0 g" Z( R& N
  8. 4
    ; ]5 Q& x3 w! ]1 c2 @8 z# ?. r
  9. >>> math.floor(-4.999)
    - M+ d6 V, q8 Z! N0 W! p+ T) o
  10. -5: Z6 B8 a3 q4 }# U! F
  11. >>> math.floor(-4.01)" D: `7 a) s0 \; X6 [$ Y' V1 |7 u
  12. -5
复制代码

. h7 A' ]% I2 J. ~( w* r. a$ qmath.pow(x,y)  返回x的y次方,即x**y' a$ `2 D$ Y7 t7 B  H9 ]
  1. #返回x的y次方,即x**y% T1 W$ h' z4 N
  2. pow(x, y)
    3 o- a9 F( Y2 E/ z; y. Q7 P9 i
  3. Return x**y (x to the power of y).8 `# x8 T6 c: d7 w! N9 W
  4. >>> math.pow(3,4)
    0 X. x! Y5 [. f  n9 M
  5. 81.01 b, o1 |* @4 ?3 M
  6. >>> 2 u: x; y/ i1 ^1 P; U6 V
  7. >>> math.pow(2,7)
    . F7 m% B4 l; j& \2 }+ `; Z
  8. 128.0
复制代码

& C3 P4 y% |9 D1 K# _7 Zmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& q, Y% w" r/ ~2 o
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! Z5 w1 O4 u& T1 j
  2. log(x[, base])8 P( F5 n8 S7 H
  3. Return the logarithm of x to the given base.6 q( C! d/ X# m
  4. If the base not specified, returns the natural logarithm (base e) of x.
    , b' q+ ]4 i# \2 K0 g' A4 m* W* c
  5. >>> math.log(10)
    ; M" i0 @& p2 c6 X) [2 i
  6. 2.302585092994046
    + C* f5 a& y% a0 Q! N9 q
  7. >>> math.log(11); `0 I( O0 s8 k6 H3 d. v
  8. 2.3978952727983707, c0 d6 w- c  M5 |7 {
  9. >>> math.log(20), T! \! d- n$ \
  10. 2.995732273553991
复制代码
8 }8 t! Q9 I7 B
math.sin(x)  求x(x为弧度)的正弦值9 H" s# y, a0 g# u% q& G8 N1 X5 K
  1. #求x(x为弧度)的正弦值/ {( d1 `! H! n* G
  2. sin(x)
    9 u7 f! A7 O, `0 R
  3. Return the sine of x (measured in radians).9 c% ?, T! b5 y! H( X0 n
  4. >>> math.sin(math.pi/4)+ W4 P1 G: j  q  \- \' L# u2 Q
  5. 0.7071067811865475% I6 B  {/ w% F" h
  6. >>> math.sin(math.pi/2)+ R1 \' r9 D5 _9 u' j: V
  7. 1.0
    3 @& d, @& o  J5 w- A" D
  8. >>> math.sin(math.pi/3)9 g; x6 y5 h3 W6 E! D
  9. 0.8660254037844386
复制代码
& |$ x; z5 L8 T. O  i2 h* f" o# w
math.cos(x)  求x的余弦,x必须是弧度
* T+ p' A" K4 q8 B4 }( k7 N( Q2 Q
  1. #求x的余弦,x必须是弧度
    + {6 n/ R* _1 T
  2. cos(x)+ K5 m4 x9 m* r, W$ [9 j8 D
  3. Return the cosine of x (measured in radians).
    # L5 l: g- [. w+ M$ I8 B7 @& Q
  4. #math.pi/4表示弧度,转换成角度为45度" G) [2 n/ P' m, e! i
  5. >>> math.cos(math.pi/4)1 A* g/ c) k9 p7 M, h
  6. 0.70710678118654764 z1 j% v0 [* `
  7. math.pi/3表示弧度,转换成角度为60度7 L0 m  o! j1 P5 a
  8. >>> math.cos(math.pi/3)$ y/ g% ?/ b7 w/ M
  9. 0.5000000000000001
    ' h  o8 c# f  R: R/ e7 R
  10. math.pi/6表示弧度,转换成角度为30度
    % U8 ]5 x) K1 Z" o8 A1 v
  11. >>> math.cos(math.pi/6)7 f: V* D# V- T
  12. 0.8660254037844387
复制代码
9 Q7 x# ~! z5 P' ^. t4 ~* v' U4 b
math.tan(x)  返回x(x为弧度)的正切值
6 H1 _" s7 q: ?
  1. #返回x(x为弧度)的正切值4 c) L# R2 o* ~: M' x" `- k
  2. tan(x)
    3 b0 c% Y7 R0 q! l1 q2 u; P+ P
  3. Return the tangent of x (measured in radians).
    # _+ U& e* m  h% Q
  4. >>> math.tan(math.pi/4)
    $ }9 f8 O5 z7 N. Q" k3 B
  5. 0.9999999999999999! m* q0 J# o3 r: r( T4 \; S! d
  6. >>> math.tan(math.pi/6)$ y2 O! F3 O" h! k1 |4 I
  7. 0.5773502691896257
    ' \* m9 h: v$ ~3 s$ s/ r4 t+ P
  8. >>> math.tan(math.pi/3), r& `+ ~4 D. O2 S; T
  9. 1.7320508075688767
复制代码

- F/ h7 e) D6 m1 D9 ~. e6 V, rmath.degrees(x)  把x从弧度转换成角度
" b. m& V  P- K1 ], `- d: k* ]+ ?
  1. #把x从弧度转换成角度) u4 Y! M7 G- Y; D( d
  2. degrees(x)
    ; J- n9 \1 e" `1 t! o* x
  3. Convert angle x from radians to degrees.
    9 i6 Z. z0 i5 ^3 v' S6 n9 g& o
  4. % @. D9 o5 }. F& |0 z: g4 S
  5. >>> math.degrees(math.pi/4)8 A7 g  h4 S2 m, G' u: {
  6. 45.0
    6 K8 J1 @! X+ m2 y' T4 \
  7. >>> math.degrees(math.pi)
    $ U7 @& w4 ]: h, u0 e6 k
  8. 180.03 @/ g( B# e. w; L' I
  9. >>> math.degrees(math.pi/6)5 Y2 Q4 {! Y  L6 e/ \; ?. Z8 Z' x
  10. 29.999999999999996) F8 F. A1 M0 ?5 q/ F7 W
  11. >>> math.degrees(math.pi/3)
    " [' H$ Z2 [! a! i  Q
  12. 59.99999999999999
复制代码
; E# v) s1 N; k7 D+ a" W; x
math.radians(x)  把角度x转换成弧度% w1 T. Q; t4 W; C- ]- @' I
  1. #把角度x转换成弧度7 @3 y% r2 n3 q$ L& r4 m3 h
  2. radians(x)
    $ g, r2 u0 i( d( J. ^
  3. Convert angle x from degrees to radians.
    7 @, _% A  P8 x
  4. >>> math.radians(45)
    8 P: z# k1 H0 D* `5 w& @
  5. 0.7853981633974483
    6 Z9 d5 z6 x% W# r8 e
  6. >>> math.radians(60)# Q; g" v5 ^9 v( C# b
  7. 1.0471975511965976
复制代码
& m: k) u$ z, l( E$ _/ T$ ~% \
math.copysign(x,y)  把y的正负号加到x前面,可以使用0- ^9 o- c& ]# x" M0 {( k: R) _
  1. #把y的正负号加到x前面,可以使用0$ K) i  |. W" f# y0 ?, L! X
  2. copysign(x, y)+ ?4 E$ c: b# R0 }* {
  3. Return a float with the magnitude (absolute value) of x but the sign 8 J7 D+ G5 G" J. X8 |9 Y
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , ]& l' I9 s6 v; T
  5. returns -1.0.
    ; X+ i+ V4 ^- D3 Z6 b5 w1 U

  6. $ V3 P& O( E2 S, J5 J; E2 y
  7. >>> math.copysign(2,3)
    , q8 Y4 V: i" T4 v
  8. 2.0' D, M3 H5 A$ _2 k/ W
  9. >>> math.copysign(2,-3)" q1 `) M5 X# ~  r  p3 G- R3 e$ {2 d
  10. -2.0" V( D  t  n# K8 ?8 D
  11. >>> math.copysign(3,8)5 E6 k7 N. l1 ^; ^9 _' H
  12. 3.0
    $ a) s0 [" c: g0 E5 B
  13. >>> math.copysign(3,-8)5 d9 ]# o8 e3 Y
  14. -3.0
复制代码

  y; y9 [$ s. ]- V. b0 |3 }" M. L) Kmath.exp(x)  返回math.e,也就是2.71828的x次方  {- S  n- N& P+ K' y0 b
  1. #返回math.e,也就是2.71828的x次方* \- q' F) U9 C: Q
  2. exp(x)4 e; s7 B7 b- C2 d& g
  3. Return e raised to the power of x.
    9 ?6 N+ B: F7 C
  4. 8 k4 @% {" o4 u! ?9 D. J/ i
  5. >>> math.exp(1)- q( |% l4 {% v/ a/ e
  6. 2.718281828459045' p4 b$ X8 D+ [+ u. x
  7. >>> math.exp(2)" ?% _  ], H! ~1 C$ h2 E5 k! b
  8. 7.38905609893065
    " e1 H. b+ R3 a
  9. >>> math.exp(3)$ X8 E3 C  k' n! x- V
  10. 20.085536923187668
复制代码
% v( y/ R% f: k% Y% t& q
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
0 t/ ?/ X% J- q
  1. #返回math.e的x(其值为2.71828)次方的值减1
    2 Q$ l! `: }7 E0 G& z3 m+ o
  2. expm1(x)2 r/ {- s9 D; `6 F8 I
  3. Return exp(x)-1.) c6 i! j( q/ x$ y0 Y' ^6 q1 e9 e
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    : ^& g6 a8 ?2 G0 {

  5. 9 N: v# ~2 O( y& W7 H
  6. >>> math.expm1(1)* o5 h3 [# w' Z  D" R
  7. 1.718281828459045
    8 T, J. \5 ?; H7 n% L1 ^( O* p
  8. >>> math.expm1(2)
      m+ ^2 S& ~, [: k5 N
  9. 6.389056098930656 t6 L4 l5 l3 f* V0 L' {
  10. >>> math.expm1(3)& n  [3 X/ L: `) \+ a
  11. 19.085536923187668
复制代码

% }* \/ `, R- `4 N. j1 j) @' W4 imath.fabs(x)  返回x的绝对值
# {) I7 e( {- B$ m/ [: ?3 X
  1. #返回x的绝对值
    $ e- J4 Q( {" \/ a7 c# s
  2. fabs(x)
    5 e( |# L' g' P5 O1 H
  3. Return the absolute value of the float x.
    ! Y- s- l* f* l& d: x* k/ S2 O

  4. 8 x, S3 b5 x- F. p
  5. >>> math.fabs(-0.003)5 {3 p2 A0 J* {, d, X
  6. 0.0038 ]+ A8 `1 ]* p+ Q( Z7 k! X
  7. >>> math.fabs(-110)! i7 K/ s2 f! u2 T5 Q* R. |, l; D
  8. 110.0% _4 p( c, ?+ u  o% X3 Q6 ?8 R
  9. >>> math.fabs(100)1 n- M; @6 `4 J. k
  10. 100.0
复制代码
0 M/ `4 X: ]0 G4 K* S/ O
math.factorial(x)  取x的阶乘的值% K1 ]2 }, q. V, K0 j
  1. #取x的阶乘的值+ f* J. q! n) q; Q! q4 D3 H* d! T
  2. factorial(x) -> Integral
    % j2 L- G- d. D  T3 V: M/ j' t4 D3 s9 p
  3. Find x!. Raise a ValueError if x is negative or non-integral." y6 j  V( V5 P4 g$ W* s
  4. >>> math.factorial(1); S0 T" b# V4 G! L! q% f
  5. 1$ y+ d" y* L8 R9 l( ^5 w
  6. >>> math.factorial(2)
    ! x" b3 L# _7 {  t+ h; |! K( h, x; a! B
  7. 2
    2 }" J. B6 W- {% T
  8. >>> math.factorial(3)
    # p9 C/ L# ^/ B7 u; b1 U0 t
  9. 6
    * g. i% b+ o/ u
  10. >>> math.factorial(5)
    + V$ s, G) o/ [8 d7 U, {
  11. 120
    2 F! y$ @8 Y5 S( i* ^& @
  12. >>> math.factorial(10)# d9 T$ k) F9 o/ x& X* u
  13. 3628800
复制代码
9 K: j2 t3 Z7 _3 L& |
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数  g! B' f9 a/ C4 h
  1. #得到x/y的余数,其值是一个浮点数
    . e, K2 R: n% s6 p1 g
  2. fmod(x, y)
    2 f/ A" \* B8 k, E9 V1 u
  3. Return fmod(x, y), according to platform C.  x % y may differ., `( |+ J. L6 }( Y; M7 c7 y
  4. >>> math.fmod(20,3)
    . r. H5 ]: R1 R  |3 l5 D# p( g
  5. 2.0% Y! ?) `  ^/ |# W" b4 P* c
  6. >>> math.fmod(20,7)
    ; H9 W& b3 W% t- }; x; |3 K
  7. 6.0
复制代码

8 A: f2 H8 a: _+ A% ?math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 ~9 g- M0 _" }0 e& G
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 x9 w% T+ H6 U( a3 R
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 t2 Y6 z* D9 S5 t+ Z
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和13 ~$ ~! V( u) X5 }; q8 ~; ^8 o* I+ l
  4. frexp(x)
    - \! V4 M( j. y+ x, ]$ k  Z
  5. Return the mantissa and exponent of x, as pair (m, e).
    ! q8 j, A6 T( t8 k( o
  6. m is a float and e is an int, such that x = m * 2.**e.$ Q: E, x- _" [
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.. |# c8 A' Y9 C4 G0 [' K
  8. >>> math.frexp(10)1 U+ \2 r0 @# X# Z/ q* K& `6 s
  9. (0.625, 4)
    / G5 A  S; o7 Y9 P- M
  10. >>> math.frexp(75)
    / Q$ t9 B4 X; S3 N1 X
  11. (0.5859375, 7)- `% l, F) d# F7 i& M+ S
  12. >>> math.frexp(-40)" W' h, |0 v* I5 ~, u1 M
  13. (-0.625, 6)5 z& p2 t# W" B- h# Q( f6 v
  14. >>> math.frexp(-100)
    # i* Q- c* P" R. y3 K2 k& {* ^5 V
  15. (-0.78125, 7)0 S1 g/ _6 p+ Z. L" h( B
  16. >>> math.frexp(100)
    + Y" u9 s9 q4 G/ `7 @0 z5 j8 z7 p0 ^
  17. (0.78125, 7)
复制代码

# T" u' ~# C8 ~math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 O  [) \+ n0 z3 d
  1. #对迭代器里的每个元素进行求和操作
    2 n9 V3 X8 S# z: D/ i( D
  2. fsum(iterable), e) z2 m9 }. y" z
  3. Return an accurate floating point sum of values in the iterable.  z4 \) y- g  x2 ]0 `
  4. Assumes IEEE-754 floating point arithmetic.
    ; H' m. y) C: z
  5. >>> math.fsum([1,2,3,4])) l9 f; j# X7 ?: J
  6. 10.0
    5 `! X# ~8 z% T( V( ?1 d
  7. >>> math.fsum((1,2,3,4))
    0 A- s6 r: j/ f" X% ^
  8. 10.0
    - x& E; ~6 V  m1 ]- c1 }  Y
  9. >>> math.fsum((-1,-2,-3,-4))5 y( G! s9 x2 V6 p2 b, Y
  10. -10.0
    - Q/ N5 t; U) T
  11. >>> math.fsum([-1,-2,-3,-4])& H0 v( a9 S2 b' n. G
  12. -10.0
复制代码
! |$ C9 m+ A) N! ?# b% Z
math.gcd(x,y)  返回x和y的最大公约数: h, T4 o& E. G9 K8 B3 L
  1. #返回x和y的最大公约数( L. b6 }+ A8 c# n' U
  2. gcd(x, y) -> int
    - A& A9 {  P) q. y7 B
  3. greatest common divisor of x and y
    + Z" n- ~/ S! G) h
  4. >>> math.gcd(8,6)
    + ^6 Z. r$ F! H2 g
  5. 2; v4 a; L, V/ o8 _
  6. >>> math.gcd(40,20)" N: i) l+ y' b. J1 C  M, U4 b
  7. 20
    8 F* Z% x) s; M8 n- g
  8. >>> math.gcd(8,12)
    + E) V: B. O5 p. X7 l  a+ I
  9. 4
复制代码

% C- Q' n: O& K  P! s8 zmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
, ?7 P; \5 S3 r' S6 G
  1. #得到(x**2+y**2),平方的值
      G, L& J$ P) [% |$ v
  2. hypot(x, y)1 S) u% {$ E( @. S6 }3 L6 P4 E* v
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    / q% m5 A% J; V
  4. >>> math.hypot(3,4)
    6 m0 l# S. ~/ N' U) E
  5. 5.0. e7 }* |  ~: s7 g  A
  6. >>> math.hypot(6,8)& m! k9 H& W- p2 e  _- b' N' G* t
  7. 10.0
复制代码
7 s7 O* H. r! W8 K9 F
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False# X# B: X  t% }  t8 Y+ i% r5 p
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 p% g3 h  R3 i
  2. isfinite(x) -> bool& P" z+ U5 Y) H( u9 }; [
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    + N; j8 O# i& A% t0 {; @
  4. >>> math.isfinite(100)" _7 V0 @# j: s* X
  5. True
    4 W* R. k! O9 e5 m
  6. >>> math.isfinite(0)- }, c% E- k& t  S1 ?+ K& @
  7. True4 _+ x4 s, I+ p; d
  8. >>> math.isfinite(0.1)
    6 z9 z9 L9 Y: }7 t& n" H" ~% Q+ h
  9. True4 ^% U% |, Z$ f; h9 P, Z
  10. >>> math.isfinite("a")
    8 d( A& `, ^1 B7 h. Z4 `0 K) V3 t
  11. >>> math.isfinite(0.0001)! u/ o" k5 u2 C  D- o
  12. True
复制代码

. }1 G6 A& \0 W0 w9 N9 K: R$ i) Omath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
! Q) O5 E4 N5 e' E1 u
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 E# l9 U/ Z9 C  h: F/ H- Y, g
  2. isinf(x) -> bool
    ! f  Y# y4 V! B, K% h
  3. Return True if x is a positive or negative infinity, and False otherwise.
    % y3 {9 k2 j$ W8 y$ p$ L
  4. >>> math.isinf(234); t! `/ K+ S4 v: B
  5. False
    " P6 N" n; o; H+ P% ?6 e2 c* l
  6. >>> math.isinf(0.1)
    ( i* G7 x& B1 ~4 ~: e
  7. False
复制代码

  j8 Z! T1 b  Z$ c% c; Omath.isnan(x)  如果x不是数字True,否则返回False! t# A& y8 C/ F- I! @
  1. #如果x不是数字True,否则返回False. C4 j8 o; s- o& r
  2. isnan(x) -> bool8 V+ R' Y$ k0 @3 H* T
  3. Return True if x is a NaN (not a number), and False otherwise.* d3 I& V/ |+ x1 b8 x
  4. >>> math.isnan(23)
    & t/ P% @, j1 T) b9 G5 Q6 a- ]
  5. False& b  ~+ p! U' B8 J) k
  6. >>> math.isnan(0.01)
    9 J, V3 v" p% U( o7 z' _
  7. False
复制代码

# |+ V" {- {! t5 e) u" d" Vmath.ldexp(x,i)  返回x*(2**i)的值; ?; F' q) k1 y( }; _9 C7 p
  1. #返回x*(2**i)的值
    6 V4 P! f* y1 u
  2. ldexp(x, i)/ G3 ^+ V, ~5 r" q; s; \: C
  3. Return x * (2**i).
    8 f8 Y) u# p/ C% v; j
  4. >>> math.ldexp(5,5)
    5 p2 ]- f0 n. R+ d6 v  r
  5. 160.00 ?( ~+ M( x6 g2 g* q
  6. >>> math.ldexp(3,5)
    1 l4 g/ D; u5 f$ b" l; o, {2 |) m" M
  7. 96.0
复制代码
* v5 d1 c7 m" p7 E( T+ L* F
math.log10(x)  返回x的以10为底的对数5 ^( h, q6 D, T. v  {. E
  1. #返回x的以10为底的对数6 `3 a7 @3 x8 Y6 X9 _
  2. log10(x)9 [( g3 I5 `7 C2 ?8 W
  3. Return the base 10 logarithm of x.: T. f3 P/ V9 U3 T
  4. >>> math.log10(10). P: i) x0 X7 Z0 i; f2 y# Q
  5. 1.07 @$ \$ w1 t1 n5 x& x% v- d
  6. >>> math.log10(100)) `# {9 L: [; ~; S3 b
  7. 2.0% O8 o; t% k9 e
  8. #即10的1.3次方的结果为20& m" m+ n  Y3 b4 E- ?
  9. >>> math.log10(20)- c% u# v4 p( q! e( Q
  10. 1.3010299956639813
复制代码

8 H  a, S: M+ c6 x$ Z9 Nmath.log1p(x)  返回x+1的自然对数(基数为e)的值
- r/ u: p7 S# t7 h; K- o
  1. #返回x+1的自然对数(基数为e)的值
    0 o; g% ~6 T' _: I8 @8 P9 L
  2. log1p(x)/ S5 Z) S. F% S  c
  3. Return the natural logarithm of 1+x (base e).
    * g6 w; [0 ]7 X3 n: d9 g& u
  4. The result is computed in a way which is accurate for x near zero.+ }* I+ e& [5 _" g
  5. >>> math.log(10)* S# U8 G4 w: h+ R. l
  6. 2.302585092994046
    / \) ?, O1 K2 s% _) Q
  7. >>> math.log1p(10)! n5 s. G5 W/ O% ~9 a
  8. 2.3978952727983707
    * C1 d" J( o" b3 `, L0 j6 @- b* E
  9. >>> math.log(11)
    ( Q" e1 p' d8 p% |4 g) D
  10. 2.3978952727983707
复制代码

: n: d, F( q6 ^1 emath.log2(x)  返回x的基2对数) L* W: H& Z* K9 W$ v) k# S  x! }
  1. #返回x的基2对数0 m1 v, ?' j3 X0 o. W
  2. log2(x)
    4 O) {- c* R3 d" g
  3. Return the base 2 logarithm of x.
    ; E4 @  q# n& k2 T3 ^
  4. >>> math.log2(32)
    . M* H8 @# u' P' }' p
  5. 5.0
    4 U& e; a. P# n. u
  6. >>> math.log2(20)9 b, D: J5 C3 i" d0 [' r( g# G, u
  7. 4.3219280948873637 n. Y) C0 K# R$ S4 A" f
  8. >>> math.log2(16)- i- O3 N  @+ _$ E! S( p8 T$ a
  9. 4.0
复制代码
# C" \7 z  q; H1 r  [5 C. A
math.modf(x)  返回由x的小数部分和整数部分组成的元组7 V; y/ j! ]5 V2 X6 }9 C8 I7 E8 j
  1. #返回由x的小数部分和整数部分组成的元组
    3 T5 m! ?* X$ N* n8 h4 b
  2. modf(x)! o, a: a, X1 }; o' p2 Q
  3. Return the fractional and integer parts of x.  Both results carry the sign6 q2 M$ \  W$ X0 |8 D: U" e
  4. of x and are floats.
    3 C5 ^5 h5 g. n' [0 x
  5. >>> math.modf(math.pi)7 P6 K( F& |( ~! x- I
  6. (0.14159265358979312, 3.0)- S0 q1 T! K  {3 U0 C  s
  7. >>> math.modf(12.34). }+ V; Y+ R5 L9 a5 h/ w* b& J1 R
  8. (0.33999999999999986, 12.0)
复制代码

& f' A- K# ^' ?1 H0 j# u$ Pmath.sqrt(x)  求x的平方根- W1 ]! m) s- P* g1 w. Z( c* C
  1. #求x的平方根+ n6 I* ]; P8 c$ _4 R: F3 x: I. r
  2. sqrt(x)
    7 t* W- i- w; {4 d
  3. Return the square root of x.
    ( a: ~% Y6 s6 X7 g( }
  4. >>> math.sqrt(100)
    6 V4 ^# v; M' ^6 G4 {$ D
  5. 10.0. M% ~; \$ A$ e6 l( c# M; o
  6. >>> math.sqrt(16)
    * s4 [, U1 h, G. I. w
  7. 4.0
    ! l6 ?0 R$ G" B/ [& y) n( S' G
  8. >>> math.sqrt(20)5 P$ j; C  r8 |
  9. 4.47213595499958
复制代码
* E3 _! A7 R3 h( T
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    . x" J7 C( k% W. u3 [) J& s
  2. trunc(x:Real) -> Integral
    ( B4 }# c, P9 P
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    " o: z, u4 ~$ r. E. ]( G6 \
  4. >>> math.trunc(6.789)/ Q8 E' P! U2 {
  5. 6
    # m. P( Z% F& g+ k+ H7 ~6 v6 }, S2 V7 p
  6. >>> math.trunc(math.pi)( y' o2 l5 Y3 _. O4 _
  7. 3
    , g1 o. p+ |% C2 s" B
  8. >>> math.trunc(2.567)0 V1 `/ F! |) y2 k5 O) l
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-23 12:37 , Processed in 0.093304 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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