新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
& r( r$ k, e1 E: l1 [
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。3 o% }) R* ~: ?
' |6 C: _; b  ~
方法13 o, N- n4 X( g  `
  1. >>> import math; Q4 `& V8 l% J0 ^3 s! r" d+ z
  2. >>> math.sqrt(9)
    6 b2 @3 V  H4 P/ Q( e7 n
  3. 3.0
复制代码
方法2
( i4 O% E7 o+ h3 E( D" c9 Q
  1. >>> from math import sqrt
    $ D8 ]0 u1 Z* ]9 x3 Q% O4 @/ r
  2. >>> sqrt(9)
    / A. K0 r) a' Y4 o; ]' j0 {
  3. 3.0
复制代码

' O2 [! ]+ s- ~* e' Z7 W

! X, X. I: S2 I7 J4 f
math.e  表示一个常量+ i9 j& T6 Q; h2 x. J$ X
  1. #表示一个常量) e/ H. C, ?, W4 ~
  2. >>> math.e( Y4 ~' _  P; y0 O, S/ I6 t
  3. 2.718281828459045
复制代码
9 p2 g+ S5 i, }" d
math.pi  
数字常量,圆周率

3 Y2 h0 G. p. c: C) e
  1. #数字常量,圆周率
    8 i! q, g2 F& v3 w
  2. >>> print(math.pi)
    : k( O& ~/ i: h) u, C
  3. 3.141592653589793
复制代码

' U7 Z. `6 D, i1 f% _) N& ]math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

2 c) y9 w! S  s, J; W' V" B* ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 ], y1 b2 \6 T
  2. ceil(x)7 L) q/ P  }7 S) B1 N
  3. Return the ceiling of x as an int.
    3 F1 b8 ]5 \4 Q7 {9 s2 G1 _' `9 c
  4. This is the smallest integral value >= x.
    & Z7 w% X! I3 ~# X- r1 k

  5. ! B2 M4 W4 t/ M2 g; Z( b$ g" g/ x: g
  6. >>> math.ceil(4.01)
    * e6 P$ [" ^, l; a! U9 ^4 U, A
  7. 5
    8 \1 K  c$ @5 f" c
  8. >>> math.ceil(4.99)6 f. k6 t" n- z3 z2 L7 B- F& m
  9. 5% k* g) s1 v1 {
  10. >>> math.ceil(-3.99)5 m) K: p2 \4 p# w9 f, u
  11. -3: Z- |5 `0 d! e# [' {! o
  12. >>> math.ceil(-3.01)
    ( M1 @, R" F* R1 S5 j. W8 N
  13. -3
复制代码
0 k6 m7 `& y; e/ F3 O
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ g" @9 ]5 N5 [
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      t) h. m+ ^: D& a. _
  2. floor(x)4 j; o. h+ S* ^) P  Q. f. p# p7 k
  3. Return the floor of x as an int.7 E; d! K1 t( i$ m& ]
  4. This is the largest integral value <= x.# M& }. ^1 m6 n( z( h. [! c5 H& A+ v
  5. >>> math.floor(4.1)
    : d$ B1 v: Q  c/ j
  6. 4" W" H1 ?# |2 G( a  ]9 Q
  7. >>> math.floor(4.999)8 g+ d) [! Z( k4 t7 J0 C" g
  8. 4; @  I6 z$ L' ], F
  9. >>> math.floor(-4.999)- z, B; h" h0 W
  10. -5# @7 n  S- t# C% l. W4 q
  11. >>> math.floor(-4.01): Q% w2 C- S  q: b- H1 |
  12. -5
复制代码

: n: L' \6 K+ N- s6 |math.pow(x,y)  返回x的y次方,即x**y
5 C4 U& }7 f2 R; j' @9 ]
  1. #返回x的y次方,即x**y
    , ^2 ?% c3 a6 W: P7 R, b3 m8 A+ E
  2. pow(x, y)
    * i/ r3 A7 Y$ w2 J0 ?
  3. Return x**y (x to the power of y).
    . W; b8 b. w! z+ S8 d, E! e4 b5 ^
  4. >>> math.pow(3,4)
    / H+ Y2 `7 x4 L9 I/ H, P& O. ^4 t% A
  5. 81.0
    4 U7 b- m! L/ R4 R
  6. >>>
    7 I( @& G( t; d! n7 p! ~
  7. >>> math.pow(2,7)! y3 Q' F- Y% B1 \9 k' m
  8. 128.0
复制代码

1 i5 E* l0 q& m. X% qmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ i# F" F) R9 C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 ?# N1 O2 ?% h
  2. log(x[, base])
    + i& T5 h  ~/ e+ [) |
  3. Return the logarithm of x to the given base.. I5 a3 n& S$ C
  4. If the base not specified, returns the natural logarithm (base e) of x., U2 Q+ d9 t* B+ O6 u
  5. >>> math.log(10)4 {. [" Z+ V! l/ s, B
  6. 2.302585092994046) m/ Y# w+ \1 o* @  f$ u; `
  7. >>> math.log(11); a9 c, d5 K6 a8 v$ A* d4 a4 e
  8. 2.3978952727983707( s# o9 y' F+ Y  P& b; M
  9. >>> math.log(20)  [0 i  {" f0 K( z5 p8 [
  10. 2.995732273553991
复制代码
7 y) \' M/ [+ P, i3 l
math.sin(x)  求x(x为弧度)的正弦值5 R+ \) A. O" ~
  1. #求x(x为弧度)的正弦值
    % g6 D$ o% e7 |/ s
  2. sin(x)
    - }0 y+ O- `0 _3 E1 G
  3. Return the sine of x (measured in radians).
    ! X0 j# C$ J/ r4 Y3 P9 _, [; I+ }
  4. >>> math.sin(math.pi/4)0 b& s! x9 F9 y  ?3 G) `
  5. 0.7071067811865475" n* v: ]; ~4 D+ P6 ?
  6. >>> math.sin(math.pi/2)0 \+ h9 T; H7 c* p3 L/ b6 k
  7. 1.0
    ' {4 {% P8 n; W+ p; e
  8. >>> math.sin(math.pi/3)
    ' o6 F! [6 O; Q* W: Y$ u
  9. 0.8660254037844386
复制代码
2 p. u' ?# Y7 ?
math.cos(x)  求x的余弦,x必须是弧度
) r) Y& I5 j; l( R; P
  1. #求x的余弦,x必须是弧度
    5 v0 X2 @" `% K
  2. cos(x)
    ) P4 N* u6 n7 [5 K
  3. Return the cosine of x (measured in radians).
    * |( Z; T( E) r& D
  4. #math.pi/4表示弧度,转换成角度为45度% M+ S8 b+ I5 B  L
  5. >>> math.cos(math.pi/4)
    / \2 u8 n/ P2 [5 g1 q2 a/ ?
  6. 0.7071067811865476$ [5 Y8 O! J8 ]  n
  7. math.pi/3表示弧度,转换成角度为60度! Q' o, p+ m5 k) @' ]# r
  8. >>> math.cos(math.pi/3)
    8 w8 f/ N. b* t8 _- y' l# o
  9. 0.5000000000000001
    1 E4 H) x& Y; \. O# p2 u& f, n4 A
  10. math.pi/6表示弧度,转换成角度为30度1 @% g; p( E; n
  11. >>> math.cos(math.pi/6). }) d  r. b% C- l& x" M5 |  p% `
  12. 0.8660254037844387
复制代码
6 U& o' b0 L4 c9 V' w4 Z/ A
math.tan(x)  返回x(x为弧度)的正切值
8 n6 `4 {3 y2 c$ s& [! F$ n1 c
  1. #返回x(x为弧度)的正切值: F$ G2 I8 O& c/ x# Y
  2. tan(x)
    ; G! p9 U  Z5 G
  3. Return the tangent of x (measured in radians)." k' `0 \, ^( P$ o, I* T' h2 I
  4. >>> math.tan(math.pi/4)
    * F8 _- U) v1 J% B6 v1 R
  5. 0.9999999999999999
    1 ?; D2 ]( t% Y
  6. >>> math.tan(math.pi/6)
    6 K6 R+ g& o/ q# g0 M% |
  7. 0.5773502691896257
    1 N( w- p) R# s8 i# l
  8. >>> math.tan(math.pi/3)
    $ x  B* r/ m' x5 f, Q6 _
  9. 1.7320508075688767
复制代码

& c& o- h1 P$ J, V- l! Cmath.degrees(x)  把x从弧度转换成角度
: u# P& H* l" ^" C* C1 a
  1. #把x从弧度转换成角度
    ( e* H9 ?: c7 N! w. k4 [
  2. degrees(x)
    # P% X/ m, E% B7 G1 _$ y. s
  3. Convert angle x from radians to degrees.
    % s7 l9 p/ m  L% q3 L9 ?  j4 p* I

  4. ' Y- Q+ M2 B5 O; l: w, n( I4 k, O
  5. >>> math.degrees(math.pi/4)1 t6 C  J+ F0 K
  6. 45.0; A# ~$ J0 \6 V4 {" M
  7. >>> math.degrees(math.pi)( q# K) r  J$ }, n
  8. 180.0
    . G8 {! X& Q2 N) R4 ?
  9. >>> math.degrees(math.pi/6)
    : V4 }' `/ I8 I$ C: Y5 j; U
  10. 29.999999999999996
    , p* `' A* K6 J3 a( n' ^1 R
  11. >>> math.degrees(math.pi/3)
    ' j5 }" H' p) q6 T
  12. 59.99999999999999
复制代码
# [0 N, J1 u+ S! i* ?; p; q! ]
math.radians(x)  把角度x转换成弧度
$ I$ |; s- s$ X- W/ _3 K! e
  1. #把角度x转换成弧度
    3 S  b" F) Z/ z; E$ ?1 x7 r6 H
  2. radians(x)
    . Y1 j( c  ~$ y3 B8 h& j1 b/ k
  3. Convert angle x from degrees to radians.
    0 v! A: W6 P# x* [
  4. >>> math.radians(45)
    & u( s9 U# |+ ?* o* n, W
  5. 0.7853981633974483
    ! H3 |" g7 w+ N( A* F
  6. >>> math.radians(60)' L! @, R5 i( V+ j8 W3 M9 [; \
  7. 1.0471975511965976
复制代码

: F/ A+ _' j" q* emath.copysign(x,y)  把y的正负号加到x前面,可以使用0: I8 C' g5 t" c/ a$ K9 a: O
  1. #把y的正负号加到x前面,可以使用0. h2 L6 m3 G* P) ]
  2. copysign(x, y)
    : S8 p1 j, i, ]
  3. Return a float with the magnitude (absolute value) of x but the sign * x1 i0 z- H' _6 p8 P4 M) ]
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    : O0 m$ D$ u' P' b
  5. returns -1.0.$ w8 V' k2 ^! Q. z4 d7 ^8 F

  6. 9 \5 Z0 a& w* P3 c) r2 H0 W) K- R
  7. >>> math.copysign(2,3)
    : ~5 K9 m( w. A8 k, H: {. b
  8. 2.0
    " n; h7 b: A! A* n
  9. >>> math.copysign(2,-3)
    9 f8 y$ ], H+ ^7 R; H/ T
  10. -2.0
    + I! u* n! m0 j, V: U, |
  11. >>> math.copysign(3,8)) C9 [: n! x0 }2 {3 g4 J
  12. 3.0
    8 |/ @2 m* u# j9 O
  13. >>> math.copysign(3,-8)
    / w) h5 Z4 t# {/ l
  14. -3.0
复制代码

  T0 Q7 q) Y  A" Z; t/ t9 }math.exp(x)  返回math.e,也就是2.71828的x次方0 O( i* d3 R$ V2 I: Z
  1. #返回math.e,也就是2.71828的x次方& W1 F3 f7 o+ h( P/ o3 U0 w
  2. exp(x)2 j  k1 s9 J; F, ~7 G" Y! B! s
  3. Return e raised to the power of x.& |' `: D  ~, z& C$ [# V
  4. " O0 [3 K- |0 f+ P7 i  d& L
  5. >>> math.exp(1)
    ! y- j" V6 W5 S  t5 q6 j, l/ ^, h
  6. 2.718281828459045
    : W. f  U' @( o4 s; a3 R0 g# @
  7. >>> math.exp(2)
    + k( v3 G' F( G
  8. 7.38905609893065+ N6 e/ `5 o$ P8 x; \6 W2 Y# g
  9. >>> math.exp(3)4 K) D1 Y( G, W# x" Y
  10. 20.085536923187668
复制代码

, D, d+ f/ A. u8 Z9 N0 Lmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
" _/ s6 ?% {' l& f; l8 M$ B
  1. #返回math.e的x(其值为2.71828)次方的值减1  d" m2 F8 ]' S# X
  2. expm1(x)
    - J" ?1 x# T7 D# z1 g
  3. Return exp(x)-1.
    ) D' c, N) E# P+ A
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    0 \/ E& C, P9 b5 b9 V5 e+ k% H

  5. ; L& ^+ r- y- U) i8 w- b. q$ v/ B
  6. >>> math.expm1(1)
    4 W  Y# [+ G% O3 v( N
  7. 1.718281828459045
    & l" u: e8 S, n6 }5 |
  8. >>> math.expm1(2)
    ) l6 A8 _2 u  J9 ^
  9. 6.38905609893065
    / s9 H  {( R) |- e0 X* ^6 o
  10. >>> math.expm1(3); V& ?4 R8 A6 F2 R* D
  11. 19.085536923187668
复制代码
7 C0 w' S4 r! ^; r) e! m1 U
math.fabs(x)  返回x的绝对值
0 d, i6 o1 h5 I0 B2 h( B
  1. #返回x的绝对值
    & a& n. v& l  |9 M: e# O: p; p
  2. fabs(x)
    2 N9 ]' h$ L& G+ S7 E
  3. Return the absolute value of the float x.
    * z1 O. s. W% A8 J8 N0 B
  4. & b& _5 ?/ D/ t9 v" i7 O9 z! h
  5. >>> math.fabs(-0.003)
    + p7 w% T! C. z' V0 r9 M
  6. 0.003: _8 |& `" p  x: P7 D7 X
  7. >>> math.fabs(-110)  V: @4 H5 w/ Z( G0 ]
  8. 110.0( m# u. Z" f$ C0 F! u1 i$ z
  9. >>> math.fabs(100)# H. [! z+ c# a3 M
  10. 100.0
复制代码
( D$ i5 ^% l# W8 X* H* E6 \% C3 B
math.factorial(x)  取x的阶乘的值* G) t+ I# l  F0 ^
  1. #取x的阶乘的值
    5 t0 X! `- m- r
  2. factorial(x) -> Integral
    ( y" {' ^; u: B$ J" d+ \# Z/ r7 T' K
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    5 M& O  I7 `- }2 z& X5 |( E1 `
  4. >>> math.factorial(1)
    # ^9 D: E  _7 H7 D- w  B, t- u
  5. 1
    6 H, |( F: ?5 [1 _( f( b  }5 _2 n2 e
  6. >>> math.factorial(2)
    , G3 _) {% T# t& e$ m
  7. 2
    % I4 b' h2 M2 e7 B1 P
  8. >>> math.factorial(3)
    3 T1 a$ _! q% ]: o" t' t
  9. 60 h5 u1 M% L% B' S5 Q5 X& V
  10. >>> math.factorial(5)/ K( P( l' d5 `& c
  11. 120
    7 N7 i" J( t6 c* b+ r) `
  12. >>> math.factorial(10)3 d7 y* e* w, V+ ]) R
  13. 3628800
复制代码
8 z& X. s! {" X- t
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
; k7 t) v1 [" R. n# D
  1. #得到x/y的余数,其值是一个浮点数! b4 a8 Y+ a6 }( d/ q
  2. fmod(x, y)
    ; M) k& p! P# @. _8 e+ ?' i8 A
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    / [; s- ^/ Y, J- j
  4. >>> math.fmod(20,3)
    ( _  g* o" Q9 @/ b
  5. 2.0  i+ q+ X, z) j" N+ \- W2 P
  6. >>> math.fmod(20,7), R! T( c1 a5 r! o% w5 V6 c
  7. 6.0
复制代码

1 D' y# P. E4 u# B: C9 L  Pmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ ~$ P- I, G$ u" V* k& Y2 {
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' E( R& j" o; v. g: ~; y( c" f3 _9 w
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    2 G8 F: p5 Z  x' s% F8 R7 {
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 u5 P" k% A1 t' @' {  N2 d
  4. frexp(x)
    # }- f0 {  L8 X& s. {
  5. Return the mantissa and exponent of x, as pair (m, e).+ K) H2 ~* T$ G6 P' M: ]0 Z
  6. m is a float and e is an int, such that x = m * 2.**e.
    & d& ]0 S2 m5 [1 ?- J: Q4 i
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- D6 \( D* N0 a( o' f6 Y
  8. >>> math.frexp(10)- j4 p; U& ?! k" ~, S! p' E- g9 k1 C
  9. (0.625, 4)
    ' {4 g4 g9 m+ j& f9 K3 n. X4 g
  10. >>> math.frexp(75)
    * Z' \+ _$ k' S7 ]! }% ?
  11. (0.5859375, 7)
    - u  y& }+ X: R( i( N, s
  12. >>> math.frexp(-40)
    7 e  F3 e4 n: y! o; t' w
  13. (-0.625, 6)
    $ N* g2 W5 _  w
  14. >>> math.frexp(-100)/ g  E/ B5 g7 A  H, v3 s
  15. (-0.78125, 7)$ K3 f" R+ O" D0 l. Y4 Y6 p
  16. >>> math.frexp(100)
    8 r3 p; A) h, O) O1 W7 g* G
  17. (0.78125, 7)
复制代码
% }5 |& h. u' X+ \$ S: {
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
% t) K- Y7 K* Y: I5 G4 x
  1. #对迭代器里的每个元素进行求和操作
    / X4 H6 ~- a( K) N4 Y8 {8 W
  2. fsum(iterable)
    # g# x! B- W) L0 h
  3. Return an accurate floating point sum of values in the iterable.
    7 h. h4 |7 j, K( `) P7 y. p# y
  4. Assumes IEEE-754 floating point arithmetic.
    9 J$ E( K! Q* I# R! z3 Z" c
  5. >>> math.fsum([1,2,3,4])
    ; C+ u6 |; W8 y: D2 A* K
  6. 10.0
    ! o& C! }9 H/ ~( c+ _
  7. >>> math.fsum((1,2,3,4))
    5 u6 D) K4 k$ w: T$ X, A1 `' Q0 v& g
  8. 10.0
    / i( [: Q0 e7 P
  9. >>> math.fsum((-1,-2,-3,-4))2 l8 B7 ]$ `) o+ r4 C+ P; \$ o
  10. -10.0
    / ?$ Z7 F3 h: \, U7 \+ r
  11. >>> math.fsum([-1,-2,-3,-4])
    ; d8 j4 }, P2 u" `* H
  12. -10.0
复制代码

# h$ w- T7 G$ b) r& k/ V% O) _math.gcd(x,y)  返回x和y的最大公约数0 ^, a" g* S! p' X
  1. #返回x和y的最大公约数3 i* b. Y% K* l: b) m
  2. gcd(x, y) -> int) `9 }. I7 Q) U, Y/ G) n
  3. greatest common divisor of x and y
    0 E+ S- X- d* g3 Q4 w+ Q
  4. >>> math.gcd(8,6)
    # w' H0 }4 S5 Y/ a. b
  5. 2, Z7 c; A! D# @4 c
  6. >>> math.gcd(40,20)
    ' a7 H3 d0 g8 N% b7 R2 ~1 X, g* z
  7. 20% z4 f9 i& m% g" l5 |& Q! _
  8. >>> math.gcd(8,12)
    9 p' K( {" J3 @2 [; G
  9. 4
复制代码

/ p! O* j7 l) ^math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
) r3 \4 a5 l+ c
  1. #得到(x**2+y**2),平方的值) |1 J; e/ f8 `9 E2 G
  2. hypot(x, y)( o8 _* A+ h% y2 S  ]! u% Q
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 r+ B* m) B- n/ w1 y* I
  4. >>> math.hypot(3,4)3 I& l  F- N" ?* p, U" i# o% R
  5. 5.0( s  P% N  q" }2 }. O- L. Z
  6. >>> math.hypot(6,8)
    8 ]( H: t5 C" {5 B9 w1 D0 P; T
  7. 10.0
复制代码

; l8 ~3 e  |' V+ [; [/ @math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
3 X1 a- Z% K$ n$ N4 f. `- Q8 `
  1. #如果x是不是无穷大的数字,则返回True,否则返回False! O; M% B" S) X7 X
  2. isfinite(x) -> bool* @. {! u3 G1 u- W
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    . P( b4 g% E5 V7 O5 o3 g
  4. >>> math.isfinite(100)+ ~3 U& Y% q; g. h- u! ~0 H
  5. True
    . d6 [" R& q0 ?2 L3 {
  6. >>> math.isfinite(0)/ B! G" V: f) R0 ~1 ^! S* ^3 q( n
  7. True
    . `; j* F' Q  V
  8. >>> math.isfinite(0.1)) d) T7 M3 e; o7 Q  H( @- o0 s8 P
  9. True
    7 x  f" X* w+ i; W; Y( U+ D
  10. >>> math.isfinite("a")
    1 \. @  Q" C) e" q, k
  11. >>> math.isfinite(0.0001)( p1 _! p4 G  Q: A6 ^
  12. True
复制代码
: ?$ [- z2 b4 [0 ~1 j/ K
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
- M9 d$ o. z! x' T* w+ A# O' e
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False* X( j- u, R' c% b8 v, r8 Y8 g
  2. isinf(x) -> bool
    + Q; f7 i, P( A0 J; t/ c: ?. X* w$ z
  3. Return True if x is a positive or negative infinity, and False otherwise.0 V1 m  A/ y" v- [/ T  p8 S/ Z
  4. >>> math.isinf(234)0 N+ S7 R) m6 R9 b* ?6 m  N
  5. False
    ) s, f2 j4 b1 y0 v' x
  6. >>> math.isinf(0.1)
    , f5 I7 _( ]/ r
  7. False
复制代码
8 S6 y% G4 W: I( S9 o( N. q
math.isnan(x)  如果x不是数字True,否则返回False
9 S( A3 u# w; L: x: R+ A
  1. #如果x不是数字True,否则返回False
    * J( ~* O5 ]. Q1 D0 H) I
  2. isnan(x) -> bool
      R$ u9 p  q/ b8 Y) c
  3. Return True if x is a NaN (not a number), and False otherwise.7 o! m: p8 ]6 T
  4. >>> math.isnan(23)  O5 g# ]0 h/ q* B7 y- H# x
  5. False0 q! t7 Z( F# C8 E
  6. >>> math.isnan(0.01)
    1 B5 e/ W2 P- S. X
  7. False
复制代码
0 u$ p6 t: w9 v8 U6 u7 l: q: H
math.ldexp(x,i)  返回x*(2**i)的值
! O4 v+ n4 [6 z5 K
  1. #返回x*(2**i)的值
    4 w$ Z0 e" Y: N" `
  2. ldexp(x, i)9 z+ F+ p( F3 J& [7 e
  3. Return x * (2**i).) G% g, e: x* F7 v0 B1 E
  4. >>> math.ldexp(5,5)4 G" ?# m: x+ g4 o- ]1 C
  5. 160.09 `( Y1 {& S1 e) b: @: d
  6. >>> math.ldexp(3,5)* y) [) Q5 h# \# U0 m+ a: F* N
  7. 96.0
复制代码
6 z5 U4 N4 S$ w- Y' P
math.log10(x)  返回x的以10为底的对数
! k" h! Z8 ~4 N
  1. #返回x的以10为底的对数
    ! V( G  K6 E  \) @7 a: ?
  2. log10(x)
    1 _! A# `, ?5 e6 n5 \$ @% P
  3. Return the base 10 logarithm of x.; K7 U/ i2 T9 t; s0 P
  4. >>> math.log10(10)
    8 r) Z2 K' z# W: G9 O( A3 Z
  5. 1.0
    , ]5 u% v/ i) x6 I$ a" L
  6. >>> math.log10(100)
      T; _- ~. }& N- i. x5 P8 V# w
  7. 2.0
    4 K# d5 h8 G0 H7 m" r0 H9 d3 ]. F
  8. #即10的1.3次方的结果为200 m, k6 ~' O, `/ X9 P
  9. >>> math.log10(20)2 c$ o' t, M( M9 W; v( r5 b
  10. 1.3010299956639813
复制代码
5 }( f, `  {* N
math.log1p(x)  返回x+1的自然对数(基数为e)的值
9 y' D7 n% K, y) l
  1. #返回x+1的自然对数(基数为e)的值8 i( M) b0 _* R
  2. log1p(x)
    ! p  _& i4 ?" i0 y) I
  3. Return the natural logarithm of 1+x (base e).
    ) u4 ~8 i' Y3 s# c  G, d; j) p
  4. The result is computed in a way which is accurate for x near zero.( y! {6 ^! J  _3 _* x; [8 ]
  5. >>> math.log(10)
    ' ?& k* x5 E" J* B& E
  6. 2.3025850929940467 c5 P0 m  |% X3 I/ Z, t% W. V
  7. >>> math.log1p(10)  B0 a) h9 m4 o
  8. 2.3978952727983707* \) n! E1 |0 ^9 X$ N: }7 l6 c2 T
  9. >>> math.log(11)3 Q; ?* h. ^) N+ h/ t& J
  10. 2.3978952727983707
复制代码

. j& S0 u# V& g9 V7 b4 n& P/ B* [math.log2(x)  返回x的基2对数6 t0 g7 n! e9 k4 m) O- d" Y1 J* n. _
  1. #返回x的基2对数8 R1 Z9 i; O' C
  2. log2(x)9 M# q  u/ B3 q* B
  3. Return the base 2 logarithm of x.
    5 N" L% S0 J$ J6 j; C( ?9 {
  4. >>> math.log2(32)6 Z- C9 I7 y* p( s) f( C# D
  5. 5.0
    - C' N8 b1 P5 p! F; i, F
  6. >>> math.log2(20)! N. O% U4 m; Z3 m
  7. 4.3219280948873639 R6 V5 j6 z& W8 e5 N8 S" W! M
  8. >>> math.log2(16)- G7 |$ S7 C, Z4 n+ \
  9. 4.0
复制代码

- D1 j; z% ]2 a* ^( pmath.modf(x)  返回由x的小数部分和整数部分组成的元组
' \6 p: i% }# k, Q2 w1 g
  1. #返回由x的小数部分和整数部分组成的元组% m4 i. J* g" [+ K
  2. modf(x)
    " G% d" S+ I1 S$ B6 U: X1 j
  3. Return the fractional and integer parts of x.  Both results carry the sign8 V8 y2 Q8 @/ n, n6 ?. Z
  4. of x and are floats.
    5 T4 x5 p8 l" j& B
  5. >>> math.modf(math.pi)1 a- J; ]( e! p7 K6 ?* F5 l
  6. (0.14159265358979312, 3.0)) Z7 q, G+ l! o" P3 D" Y, h
  7. >>> math.modf(12.34)
    ! e" u( q' o; V( q* h8 `
  8. (0.33999999999999986, 12.0)
复制代码

0 r% N- w) W- Ymath.sqrt(x)  求x的平方根
+ j6 `: w, x- y9 l6 E
  1. #求x的平方根3 W' T2 Z# a8 q8 _
  2. sqrt(x)7 n4 U4 O, ?/ h/ O9 P
  3. Return the square root of x., {, Q8 K) j& f; V3 J
  4. >>> math.sqrt(100)  D; g1 |# ?) T. ~% ?
  5. 10.0
    1 e7 R' W+ c- t; B
  6. >>> math.sqrt(16)
    4 s$ F5 {9 {9 U; R6 k1 O) L
  7. 4.0
    # g& c/ Q$ O2 v' v' J
  8. >>> math.sqrt(20)
    6 u1 b# N8 [8 K- \, X
  9. 4.47213595499958
复制代码

$ [) x# a9 X# ?7 ^  o  ?math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分% a; F1 Q. S( ~+ n$ m/ k
  2. trunc(x:Real) -> Integral6 x9 |0 a6 @7 @) u  C
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    3 D( K3 x. B: K" m7 |6 l4 x0 ]
  4. >>> math.trunc(6.789)
    , A! {6 ]$ b" k3 N3 ~$ I
  5. 60 i6 J: l. Y" F1 [2 h7 j' f2 t
  6. >>> math.trunc(math.pi)7 O' P/ u! i9 G. E8 B# j
  7. 3  S% c; h/ v9 {2 y% V/ H9 z
  8. >>> math.trunc(2.567)
    , b, }/ }3 B5 T! R7 B5 T; C, Y% {
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-7 15:56 , Processed in 0.076753 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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