新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
: {, G# G6 j: |. M, a
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。. j( O) A. T2 K# R- C- e1 z0 C1 `2 ~3 M
5 P0 U! _% R6 I$ L
方法1- n) i, y5 ^" g
  1. >>> import math3 y& M* ]8 m/ t) b, {3 j$ w
  2. >>> math.sqrt(9)0 Q* W. H( j; @6 ^, o; S; t
  3. 3.0
复制代码
方法25 x. O4 T0 \2 b+ _- Y3 b/ C  L
  1. >>> from math import sqrt
    $ r$ C4 f" b- |
  2. >>> sqrt(9)
    2 E3 c+ j: ]0 g% f& c. `/ v7 ~2 _7 E
  3. 3.0
复制代码
+ @9 e7 k7 V* [& V' c0 P4 o) ~


) ~2 C" I6 L7 l$ \/ x' U
math.e  表示一个常量: d/ F3 Y  W" C$ i; Q8 `! k
  1. #表示一个常量# o! {. _4 i- u" u
  2. >>> math.e
    6 `; c9 ~7 ^5 T  d
  3. 2.718281828459045
复制代码
6 s6 `* q' i7 T) e  T
math.pi  
数字常量,圆周率

- ?" e/ \& E7 H! [' Z
  1. #数字常量,圆周率
    & H* `& q' F( w- B
  2. >>> print(math.pi)
    * x) y/ ^5 @4 ?
  3. 3.141592653589793
复制代码

) Z, j7 G. n/ K% L, K& }math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 ]3 u3 K7 z$ u$ h5 M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    + z- G# Z" f: |' e  i
  2. ceil(x)8 F* Q3 i& h- U, B
  3. Return the ceiling of x as an int.$ v, S! v, L, J0 V9 ?0 J
  4. This is the smallest integral value >= x.5 u$ O4 E  D0 ?1 H( g$ q; G
  5. 3 e; k! f9 u! L# O
  6. >>> math.ceil(4.01)
    ! i5 b% X9 a  ]1 I1 r
  7. 5
    ) f* K. z$ J9 t/ X2 N
  8. >>> math.ceil(4.99)5 L2 B7 K) _1 j" K
  9. 5
    ! L/ x0 Y0 y; C' `2 F: o& p( w# r
  10. >>> math.ceil(-3.99)) H& K7 Q) k2 t# k& z' I
  11. -39 A8 \1 v3 p( e" ]1 ]6 s  _+ ]
  12. >>> math.ceil(-3.01)
    8 l- [: h/ i/ s3 W) g1 ^
  13. -3
复制代码

; B8 L# ?* L  ]: S: Fmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身' r9 ]3 j' ^0 {8 `# z8 W
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身( x1 t9 L$ h2 X. K4 Z: k$ k& l' s3 f
  2. floor(x)9 ?4 x5 E2 B& X9 B  C
  3. Return the floor of x as an int.$ B; @5 i0 U$ p3 c( }- G
  4. This is the largest integral value <= x.
    $ Z) N: E+ u" z+ D0 u$ w; v
  5. >>> math.floor(4.1)
    * `- b1 N1 J; R. `3 z( A% |
  6. 4
    7 N; A9 \' G! n9 n0 n5 v
  7. >>> math.floor(4.999)
    8 m2 J3 v4 m: e( v* a4 g6 i
  8. 4
    3 A- `  \4 s1 A$ i9 H+ q$ Q
  9. >>> math.floor(-4.999); M- w- F2 N4 T. n' u
  10. -5! t$ Z' b& X  V7 Z9 q) E0 {3 X0 X
  11. >>> math.floor(-4.01)
    , r7 i3 ?9 w& f+ v/ }5 f
  12. -5
复制代码

  o; e& ]( ?7 q8 h& Wmath.pow(x,y)  返回x的y次方,即x**y
1 ^$ }$ G8 W3 ^0 K
  1. #返回x的y次方,即x**y
    $ V: ]  B, m% @/ r# I
  2. pow(x, y)2 b7 G/ l! N) u. v; h
  3. Return x**y (x to the power of y).' J  |  I. q0 v/ H. O
  4. >>> math.pow(3,4)& \4 V1 B6 x' H# B7 r7 L
  5. 81.0
    7 S$ o/ }2 t9 u) R' `, Q# _8 F
  6. >>>
    $ G* @* G( j, A+ S/ U
  7. >>> math.pow(2,7). G3 S# m, F5 ]- Y( h6 w
  8. 128.0
复制代码

! i3 O' S+ Y2 mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): f2 _5 S3 V, Y
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)( M$ H% V+ I' o2 ]0 e' g8 f4 z/ P
  2. log(x[, base])
    1 `) ^- B" G+ C6 b& W+ @5 K! R
  3. Return the logarithm of x to the given base.
    ( l2 t0 d7 q& v$ p
  4. If the base not specified, returns the natural logarithm (base e) of x.
    + B! C! |  X! z& o/ Q: ~/ {
  5. >>> math.log(10)
    5 P: r4 O( b6 K, H
  6. 2.3025850929940464 J& Z5 [1 ~! G5 T" `  a
  7. >>> math.log(11), U1 G/ z4 r& R5 D
  8. 2.3978952727983707: T) T! M  }' ^& v* f7 [; c9 |# k
  9. >>> math.log(20)
    ) b* a: U9 n( t& T* }, x
  10. 2.995732273553991
复制代码

+ W4 v" Z. w. F! V7 [. dmath.sin(x)  求x(x为弧度)的正弦值
; N, s- }; [" S
  1. #求x(x为弧度)的正弦值
    + K7 i; J* U# n5 m4 q
  2. sin(x)' B* |4 n* k0 A7 a
  3. Return the sine of x (measured in radians).4 M, g9 A, [3 Q- ~% p
  4. >>> math.sin(math.pi/4): w5 N3 s+ f; O& W- ?7 R
  5. 0.7071067811865475  G' x+ u6 g7 j6 ]$ o6 m/ t
  6. >>> math.sin(math.pi/2)- z: _+ {2 U# j  L: n, h1 {
  7. 1.0
    4 G; c$ h( q2 ~2 z. e
  8. >>> math.sin(math.pi/3)5 |: |+ O) x9 s# ^7 n) n$ Q# c
  9. 0.8660254037844386
复制代码

1 b- j) h7 Y8 R8 x+ j6 rmath.cos(x)  求x的余弦,x必须是弧度# s8 B$ c3 j+ i. s7 a$ C7 f
  1. #求x的余弦,x必须是弧度. E+ c* P/ I; O( {- ?
  2. cos(x)7 Y# S+ ~. k" K* _( F7 d2 j' f' ]
  3. Return the cosine of x (measured in radians).
    ! p, T  ~: J' p" i# }4 r
  4. #math.pi/4表示弧度,转换成角度为45度1 _+ _. T* w8 U; `+ G5 d- a0 }: Z
  5. >>> math.cos(math.pi/4)3 T7 K, d9 E! G9 t9 E' \- F' `' I
  6. 0.70710678118654763 m: u+ e. F% U
  7. math.pi/3表示弧度,转换成角度为60度  g- T6 O  v! B. d/ E, q' B
  8. >>> math.cos(math.pi/3)  y: i9 y7 M/ W+ ?6 I! j4 ?! ~
  9. 0.5000000000000001/ e7 w5 Y, k; |2 _  W
  10. math.pi/6表示弧度,转换成角度为30度
    6 m" U& U- R9 L
  11. >>> math.cos(math.pi/6)
    8 o# m( N, Q/ N1 J: X
  12. 0.8660254037844387
复制代码

/ [% B' M1 d& ]5 i& amath.tan(x)  返回x(x为弧度)的正切值
$ X; \: o! r+ _8 P& i6 x& d! j
  1. #返回x(x为弧度)的正切值
    2 @5 M! k1 c# B. c3 q6 m: @0 k8 x# U( k
  2. tan(x)7 K) E5 [& C0 L, E: q
  3. Return the tangent of x (measured in radians).
    7 `* ?% E8 m1 T: |/ {5 ?
  4. >>> math.tan(math.pi/4)
    7 a) G4 U2 _, |+ u$ k
  5. 0.9999999999999999# j* t, y  y% h6 _2 e* c$ P
  6. >>> math.tan(math.pi/6); T3 j. \8 w) W6 h& O3 y
  7. 0.5773502691896257
    9 H" E  C3 i- K' a( J
  8. >>> math.tan(math.pi/3)6 O6 r, J! @; V" z- v) Z. K
  9. 1.7320508075688767
复制代码
8 L* S5 B' Y9 E# w8 f! u6 f
math.degrees(x)  把x从弧度转换成角度% S' [0 ~2 b, X1 n( x2 i
  1. #把x从弧度转换成角度
    $ Y3 k/ O# ^! C
  2. degrees(x)* M, ^4 L" J9 l% v% @1 I2 ~6 Y
  3. Convert angle x from radians to degrees.* g8 w% |& v* e, M- A& Q/ z3 N% D

  4. * `2 j4 |% B9 f0 W
  5. >>> math.degrees(math.pi/4): N  t  Z- s5 a' ^8 p# c0 A
  6. 45.0( [! @0 q+ \, A0 e! L
  7. >>> math.degrees(math.pi)
    % g) V( F& A. e/ m: ^
  8. 180.08 W3 V2 f0 z' h( Y
  9. >>> math.degrees(math.pi/6)
    ; A+ N: H6 x0 s; i+ ?0 _5 t
  10. 29.999999999999996& ?+ U/ o! A& J1 D+ b
  11. >>> math.degrees(math.pi/3)
    ; A7 z% e9 v' k4 N  \2 A/ |# ?
  12. 59.99999999999999
复制代码

4 A3 ?1 p9 C/ j1 x* imath.radians(x)  把角度x转换成弧度" S, q/ B# Y! n( L! F9 V9 ^9 d  ^
  1. #把角度x转换成弧度
    % n' o# f+ x" h5 K' i4 ~3 @: p0 E
  2. radians(x)' U# Q$ O7 |6 G# }% |: K
  3. Convert angle x from degrees to radians.
    ' i* ^% j: L# j  D; `. S$ _+ C
  4. >>> math.radians(45)
    & f. e- o# f$ [0 Q' s
  5. 0.7853981633974483
    8 |3 \2 o4 |( K
  6. >>> math.radians(60)
    ' S# |4 J0 J( Q9 {- |- v
  7. 1.0471975511965976
复制代码

8 W0 X0 D8 D# Z: S  y0 X* _math.copysign(x,y)  把y的正负号加到x前面,可以使用0
' o+ n2 j0 o- `0 g0 |
  1. #把y的正负号加到x前面,可以使用0
    / q3 P8 P& z2 g& L: y
  2. copysign(x, y)& Q" P1 o  d6 K4 B7 u9 E
  3. Return a float with the magnitude (absolute value) of x but the sign . c. H# [& w7 l
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    0 L% ~2 f3 O7 m7 G
  5. returns -1.0." e. R3 G8 B# A. ~. {

  6. * |  q$ F4 e/ P
  7. >>> math.copysign(2,3)
    + I7 E( |2 z, t% W9 w: d- L
  8. 2.0
    4 e, K" V: d+ {  b+ G% {3 V0 f8 n
  9. >>> math.copysign(2,-3)
    ' d9 s1 H( D, y( ?7 u
  10. -2.0: _3 }7 u3 D6 s9 d. g
  11. >>> math.copysign(3,8)
    , @  r$ T3 }6 w4 S# d
  12. 3.0
    ( U: {8 e) N  D- Z' b
  13. >>> math.copysign(3,-8)$ s$ D* T6 S, y) @: |
  14. -3.0
复制代码

0 K$ U  M2 a6 V5 r. ?  hmath.exp(x)  返回math.e,也就是2.71828的x次方! ?( T* h% ~* S, ^7 C* R
  1. #返回math.e,也就是2.71828的x次方
    / n2 P" X6 J- U# i' s, y! k. ^
  2. exp(x)
    9 z' v; y; _; X5 l6 V  l
  3. Return e raised to the power of x." A6 Y' j. |! T# e2 k( O+ K
  4. : j: O. Q! I- A$ x. O
  5. >>> math.exp(1)
    6 k) ~. O# [3 n6 X
  6. 2.718281828459045
    * z4 v1 r5 b( L( F4 O
  7. >>> math.exp(2)
    6 _& e( b/ N' X4 H" U) O8 h" Y
  8. 7.38905609893065
    & A* k/ Z# @& l! s, q! `9 W- L7 v
  9. >>> math.exp(3)
    ' s4 T) K4 ^5 ~  N! Y+ Z3 I
  10. 20.085536923187668
复制代码

8 f# m8 }2 _8 p0 m( rmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1! [+ G! `% ]0 b
  1. #返回math.e的x(其值为2.71828)次方的值减1
    3 h3 `7 k1 t+ P' \# f% `: ^7 B
  2. expm1(x)0 E4 r- x- ]3 f  D) `
  3. Return exp(x)-1.
    / t* B! d+ r0 c$ X# q
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    , N* F* R, K$ j4 F- n- O

  5. - _* g! s+ F9 Q5 M
  6. >>> math.expm1(1)! s5 G+ S2 d. m. f6 s% a
  7. 1.718281828459045
    + `* B6 L+ }/ a  w
  8. >>> math.expm1(2)
    + E& T% t$ F3 X. n9 [/ x+ F" e
  9. 6.389056098930659 ]8 Y# b& O+ g, ]6 a2 b
  10. >>> math.expm1(3)
    - M6 n% Q% N- {! q! A! f2 i: B
  11. 19.085536923187668
复制代码

' a' c. O# r: v! d3 [math.fabs(x)  返回x的绝对值
5 v# M3 s9 ^& f4 q- z
  1. #返回x的绝对值
    9 g3 F$ s6 O% E+ z. k7 c
  2. fabs(x)
    ) S2 Y9 i2 A# r1 ?3 G) o' X
  3. Return the absolute value of the float x.
    2 E3 b4 g; @& m9 W2 K; p0 z2 {) Y% `
  4. # l. |3 O, u; K
  5. >>> math.fabs(-0.003)
    % Y" q4 ]5 h# a8 T
  6. 0.003
    , [$ b; f- S0 S& C& |
  7. >>> math.fabs(-110)7 P6 N6 \- g; K: _/ d1 I/ P9 w6 u
  8. 110.0. M) m+ S0 Y# p/ k( D- j
  9. >>> math.fabs(100)- g5 d& x( t% k% I; i/ h: d
  10. 100.0
复制代码
3 R7 g. D9 Q2 ~3 o, E3 N) q
math.factorial(x)  取x的阶乘的值
) b/ @. m0 b/ ?* I5 J% }  X
  1. #取x的阶乘的值# D8 m5 D0 D, T) z" D) j9 D
  2. factorial(x) -> Integral
    2 a! q+ ^+ T; e% h$ Y+ m3 f! R$ q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    " u& G* y( o' d# d  ?$ I
  4. >>> math.factorial(1)2 F/ a$ ?  _. O
  5. 1
    3 }5 t! }) k  S* w9 K% y
  6. >>> math.factorial(2)
    2 K; w4 a+ F+ O, [9 N9 |' H# h
  7. 2+ ?: S8 P7 O- T+ z7 M. l
  8. >>> math.factorial(3)$ i" j" H0 Q4 ]: H! k4 k' e( Q. `
  9. 6' [& o) Z$ A2 |; b5 D+ i( t9 y
  10. >>> math.factorial(5)
      S. f+ T. L, h5 |& p/ r
  11. 120
    7 T6 K3 p9 Q) T) ], a
  12. >>> math.factorial(10); ]- T& k% W# t. {* ]9 u
  13. 3628800
复制代码
- R& D! H; Z  J0 d" w+ u
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数+ y6 w+ k8 @4 B0 V1 C
  1. #得到x/y的余数,其值是一个浮点数, B# h" ~+ |( b3 n. v! U
  2. fmod(x, y)5 ]& u! x. N/ r' f( n2 r: ]
  3. Return fmod(x, y), according to platform C.  x % y may differ.+ I% [* j9 v5 ^/ T8 j9 `
  4. >>> math.fmod(20,3)9 j5 j9 Z0 I: K5 {7 M
  5. 2.0" n! T2 k& i0 T/ [3 e
  6. >>> math.fmod(20,7)
    ( _0 e- O  }) ]+ w$ y4 M
  7. 6.0
复制代码

, c0 u) O5 `. b6 T: Q( o% mmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ [* M1 H0 |: Z, U5 z3 h
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 e% b' L. @% a9 z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ) w9 ?. I4 r" O( G4 {
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
      m) J) K& J! z) |, w$ l
  4. frexp(x)
    8 m: x. d; z  _0 e1 I$ I7 V
  5. Return the mantissa and exponent of x, as pair (m, e).
    # ]1 g& _' |* f/ m& l. ]
  6. m is a float and e is an int, such that x = m * 2.**e.
    & _* ?  K& p0 D/ S! R
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! x! B# R, q3 ^' k
  8. >>> math.frexp(10)
    $ H, q! K+ n/ ], S) D: w& n
  9. (0.625, 4)5 s0 I" a" Q+ a, G7 |0 m
  10. >>> math.frexp(75)
    7 l( @! q8 A6 M/ w' p
  11. (0.5859375, 7)3 a/ g& _5 U8 b  [1 V+ k* r
  12. >>> math.frexp(-40)
    4 Z- [5 G, P  j% W+ e
  13. (-0.625, 6)  [. N/ @* i% Y. s2 I9 [. s
  14. >>> math.frexp(-100)7 r' S6 ]% O2 t+ i! G/ n1 R
  15. (-0.78125, 7)
    2 n$ D7 k* Q: B) y
  16. >>> math.frexp(100)5 K& `1 v4 C: u$ g: h& U9 B2 [
  17. (0.78125, 7)
复制代码

) d0 J4 Z/ Y/ N3 \) |math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
* g, C& B* E6 @5 T
  1. #对迭代器里的每个元素进行求和操作
    * _4 s% m. G+ z& w/ y# i
  2. fsum(iterable)
      i; T8 e! \0 P. r: A) w2 }
  3. Return an accurate floating point sum of values in the iterable." }7 _& ?! g# i5 I! M
  4. Assumes IEEE-754 floating point arithmetic.
    : f, H9 ?& |, n7 H
  5. >>> math.fsum([1,2,3,4])/ e& ^  g! i5 K5 p" {: {6 {% @2 X
  6. 10.0
    # Y! d! P) j; h1 I% m
  7. >>> math.fsum((1,2,3,4)); B- g3 _* Q( Q1 k
  8. 10.0
    # _, |0 K  x, s( A3 f1 F6 U
  9. >>> math.fsum((-1,-2,-3,-4))
    7 w2 ^1 }& O7 M9 g& o
  10. -10.0; T2 r. z8 a. Z, y, J
  11. >>> math.fsum([-1,-2,-3,-4])
    : q2 X+ P$ g6 G1 t
  12. -10.0
复制代码
( E1 s- \5 ^  W! g3 |6 q
math.gcd(x,y)  返回x和y的最大公约数
7 i! S# y( Y( T. _" o7 y0 }- d
  1. #返回x和y的最大公约数% X/ O: ^) O  i
  2. gcd(x, y) -> int, l1 H0 L3 Z# U1 V3 q) g
  3. greatest common divisor of x and y
    7 P$ G' R3 Q! N: K
  4. >>> math.gcd(8,6)
    ' S+ {6 F6 @/ K2 V0 I9 K3 m
  5. 2
    " n/ T" E0 B; E9 K' v0 w
  6. >>> math.gcd(40,20)
    & o4 y/ s# |7 z4 @- W# @
  7. 202 i0 N9 G% ^% D) h, p9 f4 Q6 x) d% ]
  8. >>> math.gcd(8,12). |% v% K3 A; t$ j- [, a
  9. 4
复制代码
& R: K' d  Q; |$ |0 B
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
' m* O! n7 V1 Y/ }5 N
  1. #得到(x**2+y**2),平方的值* u3 I2 t& T4 ]
  2. hypot(x, y)
    : R$ a- e: h- j6 n
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    % L: ]" d. G$ y& D+ k! \1 i
  4. >>> math.hypot(3,4)
    & r3 |3 Z8 {: T3 V* ~* H7 d- [" y
  5. 5.0* m$ a) E  S; e# ~$ }6 {
  6. >>> math.hypot(6,8)% \& `% C% T* W0 z( P" x$ e/ }
  7. 10.0
复制代码
8 ~' |$ Q, t& v3 @4 N
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False& E8 S, d9 t' f
  1. #如果x是不是无穷大的数字,则返回True,否则返回False$ ^$ G/ P  s" f( H0 S) V
  2. isfinite(x) -> bool
    1 _4 f, h( R  n8 W" D
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    4 Z4 b4 V* `9 O1 d" i4 Q
  4. >>> math.isfinite(100)
    4 G" ~6 r) v! }8 y6 `0 V5 J
  5. True% }7 D6 q( {& h# P& W
  6. >>> math.isfinite(0)
    9 \( z0 k2 {& |- v& [
  7. True& Z# p: }3 @6 P7 {
  8. >>> math.isfinite(0.1)
    7 Q/ Q* p0 f6 A" I
  9. True
    1 Y3 b; n/ J3 J4 }1 b; l8 [
  10. >>> math.isfinite("a")
    . E: H8 l$ U: v/ W. q
  11. >>> math.isfinite(0.0001)
    * M, t. y$ y  ~5 q  y4 o1 ^8 v
  12. True
复制代码

: C. Y, F" |1 G$ d2 b7 Tmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 k# a4 A( ^: A: `& s
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False% D4 a! m  n- L. x9 }
  2. isinf(x) -> bool* d% n3 `, e& H/ o' g; }
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ( a; F4 O% v- l9 N) q1 c
  4. >>> math.isinf(234)% r  j8 @3 I; k. f. Y7 o2 V. h
  5. False" f- S+ Z8 g; }8 n) {6 k4 e6 `
  6. >>> math.isinf(0.1)
    , U5 |' P% a& G# e
  7. False
复制代码

: r3 o, u5 F4 F; emath.isnan(x)  如果x不是数字True,否则返回False8 R' E; v0 O4 J  {% R0 V, r
  1. #如果x不是数字True,否则返回False
    ! c+ b6 S1 K. Y/ _5 ^
  2. isnan(x) -> bool* o& _% _  [% i$ U
  3. Return True if x is a NaN (not a number), and False otherwise.
      ~) _; M/ c+ K( S& k
  4. >>> math.isnan(23)
    + ^$ X/ `* U+ O+ H. S
  5. False
    ' m' ~" i  F; w4 g( p
  6. >>> math.isnan(0.01)
    % n/ |. ?. I' `' n$ b
  7. False
复制代码
1 f' b2 w* L! Y! X0 s( x6 n9 ~' e
math.ldexp(x,i)  返回x*(2**i)的值% q* o( ]  c0 j& W
  1. #返回x*(2**i)的值
    " y  [1 r: I' i
  2. ldexp(x, i)
    5 L  w  C- N& f# w( U9 r& ?
  3. Return x * (2**i).5 F0 ]5 J. x& U3 d& j* U) \
  4. >>> math.ldexp(5,5); s% F* W  ^9 w
  5. 160.0
    6 x$ g/ n! R  n- x, I6 Q; h: [$ m4 c1 o
  6. >>> math.ldexp(3,5)5 q% h* ^, N" \! V; m
  7. 96.0
复制代码

2 d+ U% `. O/ Z1 g4 Jmath.log10(x)  返回x的以10为底的对数
0 _. ]6 p" Y, C& E! M4 @6 U/ ?
  1. #返回x的以10为底的对数5 F7 T, Z* B" L
  2. log10(x)
    ; [+ p7 o& r" |. ~8 W6 p
  3. Return the base 10 logarithm of x.& F1 h6 K- t3 ?( S' k. |+ ?7 Y
  4. >>> math.log10(10)* v/ F1 r6 ]7 m* l
  5. 1.0
    1 f& q- E9 s# K+ m& T
  6. >>> math.log10(100)
    . m! o, l) x, f6 ?) q4 y. v
  7. 2.0
    5 F/ M1 |& j- r
  8. #即10的1.3次方的结果为20- o& s' d( c' g0 Y0 r# u. j
  9. >>> math.log10(20); A- B2 k8 q2 t! @1 {. Z2 L9 b
  10. 1.3010299956639813
复制代码

! }2 C! I4 W; r7 G' v5 gmath.log1p(x)  返回x+1的自然对数(基数为e)的值- R$ C8 ^, P! u' v  \& k! _
  1. #返回x+1的自然对数(基数为e)的值
    9 k& J( o, O2 M. x
  2. log1p(x)3 y0 l$ @1 D6 b5 a, Y9 ]" p$ u
  3. Return the natural logarithm of 1+x (base e).  p, M' j* x6 w0 f& p: u' f
  4. The result is computed in a way which is accurate for x near zero.
    ) v) W/ O6 C8 u$ F5 y
  5. >>> math.log(10)6 d0 y0 E; i+ i- x5 A
  6. 2.302585092994046
    ; e. o. X3 A5 ]
  7. >>> math.log1p(10)
    ( I( K$ C# A* v! B3 l: @
  8. 2.3978952727983707
    5 S. y! O; F5 y- x% w
  9. >>> math.log(11)
    , }3 u/ ^% J: _- J  H
  10. 2.3978952727983707
复制代码

( g+ M2 c& M- \+ ]math.log2(x)  返回x的基2对数
5 E) l* \1 Z4 B# l# @" V  g$ V3 ~
  1. #返回x的基2对数# y2 U# `9 e0 `# r! s
  2. log2(x)
    4 M9 H# ?7 }9 t* Y" R
  3. Return the base 2 logarithm of x., h2 j" C- j1 M3 f+ m
  4. >>> math.log2(32)3 i( p0 _  D& Z5 H
  5. 5.04 G; P, |  Z& g0 [- b* p
  6. >>> math.log2(20)
    0 i9 h% J: ~* t! Q4 N
  7. 4.321928094887363
    & u  A4 S' l/ Z; x7 @7 S
  8. >>> math.log2(16)  [* w+ M5 H+ z4 w  W& r
  9. 4.0
复制代码
2 i) f* B0 u- o9 b! v+ M$ o$ |' r
math.modf(x)  返回由x的小数部分和整数部分组成的元组, I8 O3 \& f7 a. D, g( j) w/ e
  1. #返回由x的小数部分和整数部分组成的元组
    ! [% F; C: y" y8 n- Z! J" e% L" Y5 p
  2. modf(x)
    . u$ F$ s. g  q; ?2 y) g
  3. Return the fractional and integer parts of x.  Both results carry the sign. n2 c: R3 w  w4 B/ x$ [# m3 j
  4. of x and are floats.
    ' B5 \; R5 q6 M# E0 h+ M
  5. >>> math.modf(math.pi)
    9 B1 i0 h5 @* N" ^# {' |
  6. (0.14159265358979312, 3.0)
    # z( H! }6 {6 w, m: d% d' o
  7. >>> math.modf(12.34)
    ! C5 i  _7 D& y3 [/ q! S
  8. (0.33999999999999986, 12.0)
复制代码

# g% G6 ~$ i) ]$ d; c3 H% S- o! }math.sqrt(x)  求x的平方根& N" o+ g4 W* z$ s7 K! X
  1. #求x的平方根
    ( L- p# y- \' j4 m# O) J! o. P
  2. sqrt(x)9 W  B. V7 D3 o5 _( E
  3. Return the square root of x.6 E- S5 [2 ?% Z' M# p# Q
  4. >>> math.sqrt(100)
    4 A& V8 {. W, G2 w' Q; `; C
  5. 10.0$ _5 z& A# g9 _
  6. >>> math.sqrt(16)
    % \/ R6 v+ f, I# }( X
  7. 4.0
    & c# Z6 |' v% Y: ^+ e  z( o
  8. >>> math.sqrt(20)8 M+ h# N2 F  l2 i2 p0 J: Y9 M
  9. 4.47213595499958
复制代码
' a% ^' c1 Z+ X. q
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分# m  N7 I7 u# M/ d/ [
  2. trunc(x:Real) -> Integral
    " i$ a4 A; P4 o+ ~1 H2 n0 K
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    0 \! o, ~+ z  V# M  s
  4. >>> math.trunc(6.789)$ u7 h6 k# Y6 _8 M$ c
  5. 6
    ) F- @% T  S- r" ?' C' p) m
  6. >>> math.trunc(math.pi)' P/ i7 |% [$ ~0 n/ t+ A
  7. 3
    2 p" c# [# f! A6 ]* }2 l: N
  8. >>> math.trunc(2.567)8 k2 `6 f# Y8 P1 I! |9 C
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-4-15 10:23 , Processed in 0.094705 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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