新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
4 t% H1 ~, {3 s5 X# t4 Z8 l# @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。3 T* x' q8 N0 q7 L# }- y+ N$ S

  l- L4 A6 B5 }方法16 M) Q; o" b. c; Q: \
  1. >>> import math
    0 i( @$ Z* `% B+ j% P$ Z
  2. >>> math.sqrt(9)
    - [# P' \: l! Q5 A; E  r, ^
  3. 3.0
复制代码
方法2# |$ ^2 a- o) U* f7 c6 q
  1. >>> from math import sqrt
    9 n/ ?! D$ X' N" n, ~
  2. >>> sqrt(9)
    ) [3 r# O1 s& b1 k1 H& b, x  v
  3. 3.0
复制代码
$ O& L# Y. s; |5 B! g' y" ]

! X. E# m$ @" G7 n3 j6 h3 ]
math.e  表示一个常量
4 d5 f! T' S, f/ }9 I
  1. #表示一个常量- s2 f$ R, \0 J7 S% P3 n& O
  2. >>> math.e
    . A& M' j: ?- b* t
  3. 2.718281828459045
复制代码
$ K3 Z) W1 ~( x' q$ y
math.pi  
数字常量,圆周率

3 M# P% p% Q, O2 N/ V
  1. #数字常量,圆周率
    ! a9 n1 x. ?5 v: i$ U( q- d
  2. >>> print(math.pi)9 R- Q; K9 Z( ?0 C
  3. 3.141592653589793
复制代码

/ E. q: E& }% o5 A7 Vmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
; t, }/ p& f5 ?
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ' n  n! r1 |" B2 {$ Q/ c( S- |
  2. ceil(x)$ \- M/ g  z' P5 p
  3. Return the ceiling of x as an int.
    - T- \2 T2 S5 r4 f% o- f
  4. This is the smallest integral value >= x.
    ; _1 d, l3 \5 f( h* y4 M3 X

  5. 7 H: k5 m3 x) d2 b: n& ]4 |
  6. >>> math.ceil(4.01)
    . Q' ^3 V* U( A2 y$ a
  7. 5% t* m6 K( e7 i3 ?% M' R4 l8 x% U2 E
  8. >>> math.ceil(4.99)
    # k3 }  K8 `# \  X8 |& M; G! y
  9. 57 q) V1 q  `' r" u! ]) ?
  10. >>> math.ceil(-3.99)0 \6 \& i3 d* B* X" {+ G
  11. -3  F3 F' U) A2 }3 e  M# N
  12. >>> math.ceil(-3.01)- G: ]3 P: k+ y6 E/ R$ B, N
  13. -3
复制代码

) h& }8 `1 j& K+ K% emath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
/ n, t" `. T8 G3 \6 |: a4 ?
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    + d7 g" R2 }9 l$ R8 v/ @& y2 m
  2. floor(x)6 F; Z' o' q3 q! |* q
  3. Return the floor of x as an int." o* f0 q( ?& w3 o
  4. This is the largest integral value <= x.% {7 j( r2 O" z* s: G
  5. >>> math.floor(4.1)
    . `% }; ]5 C7 [6 ]. J! o- K. u
  6. 4( q+ Q# h, }, Y/ o6 V. i3 |: o+ O
  7. >>> math.floor(4.999)
    7 G6 {7 e! k; V+ F
  8. 49 y8 c9 R! P; d# ]! T' `
  9. >>> math.floor(-4.999), V* f1 g8 H0 B( B9 f) h! Q
  10. -5
    1 O5 @+ }) E9 L0 H" s
  11. >>> math.floor(-4.01)1 O3 F: d$ C' A
  12. -5
复制代码
- d4 R! k; D& i$ ?4 U$ n
math.pow(x,y)  返回x的y次方,即x**y
7 w2 r' X1 ]1 S: f4 l: J; d* z+ L
  1. #返回x的y次方,即x**y
    9 c0 h) L. a7 |" @" [& E# A
  2. pow(x, y)& @6 R7 j" `) z5 b  R# X& {2 @1 B
  3. Return x**y (x to the power of y)., z  }# b% u9 v+ t: P2 H9 L: a
  4. >>> math.pow(3,4)9 `0 a7 `# Q2 P' K( a! Z
  5. 81.0
    $ W1 z" b/ B' g0 B
  6. >>> & Y8 z5 o4 d" R2 c
  7. >>> math.pow(2,7)
    % G' _" t1 `& A7 g, r$ j! L$ A
  8. 128.0
复制代码
& i7 t4 ^( T1 L0 M2 w' H
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  n8 N: H' Z& g" ?+ S
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) A1 F  ~5 j6 p/ e5 d, O
  2. log(x[, base])
    0 ~8 G$ Q5 C/ j. G
  3. Return the logarithm of x to the given base.
    8 K% p( c9 n# p: N8 z; \9 N; _
  4. If the base not specified, returns the natural logarithm (base e) of x.
    + }9 g+ B6 o+ X7 i" j) t% A
  5. >>> math.log(10)
    2 Q# R; e# G6 L) {; I: L
  6. 2.302585092994046
    1 h$ \2 C1 y; f' f+ O: D8 u" U4 A  |
  7. >>> math.log(11)
    , @7 K: u: O' ~( u/ d. m. p
  8. 2.3978952727983707+ A0 W9 k& A  |- P2 x. s& o: A
  9. >>> math.log(20)
    3 t5 g9 S6 b& k8 }
  10. 2.995732273553991
复制代码
& T3 l3 O9 ^5 v3 i7 C0 {
math.sin(x)  求x(x为弧度)的正弦值
! K+ o( z. R! J# E. ~
  1. #求x(x为弧度)的正弦值! @' A' z6 a4 P6 l' _
  2. sin(x)- F" q! T( V* j; h% N  \" g) n
  3. Return the sine of x (measured in radians).8 F6 X# v+ Y2 H  l! M
  4. >>> math.sin(math.pi/4)
    5 b1 M9 J: K; r6 w4 ?3 m
  5. 0.7071067811865475
    6 L* i! Y0 L& d# m
  6. >>> math.sin(math.pi/2)
    : H, V( n# C4 x% w! T2 {' Z) w6 Y) k
  7. 1.0( b9 h" S* ?: m
  8. >>> math.sin(math.pi/3)5 ]! m9 o, c0 r6 i7 d8 }
  9. 0.8660254037844386
复制代码
& D1 b: m0 f2 |* W8 x% m
math.cos(x)  求x的余弦,x必须是弧度
" H+ V: d% B4 E; d4 O
  1. #求x的余弦,x必须是弧度
    7 \, V6 a/ c% F! y7 j+ @5 }
  2. cos(x)7 C$ k# r( y, a5 ?% K
  3. Return the cosine of x (measured in radians).
    ; r. R( |8 G+ e% n' f1 v
  4. #math.pi/4表示弧度,转换成角度为45度3 w" J( r* N- R& m% P: l" o' x9 a
  5. >>> math.cos(math.pi/4)# f& {* i. \1 j! T1 x& S
  6. 0.7071067811865476
    $ @/ R3 W. v' N5 D8 t1 \
  7. math.pi/3表示弧度,转换成角度为60度% c) x" |! C8 V- G; A0 E9 [
  8. >>> math.cos(math.pi/3)
    * D' c7 |# k- U1 Z
  9. 0.5000000000000001
    ' B( }, ^5 A0 g7 |1 T0 ~: o
  10. math.pi/6表示弧度,转换成角度为30度# B4 r" b( g2 B& G, w
  11. >>> math.cos(math.pi/6)
    8 b# j" [* \( Q" I! e; D# A+ I- q; a* T
  12. 0.8660254037844387
复制代码

$ z2 P; G* e1 y8 b7 h" \$ Nmath.tan(x)  返回x(x为弧度)的正切值; ^* [1 d# z7 Y: }  |& F0 a+ @
  1. #返回x(x为弧度)的正切值
    & A3 Q3 D7 s/ \8 m) o
  2. tan(x)
    6 b; f; O* b; ]* L0 L2 D; J# u
  3. Return the tangent of x (measured in radians)." @9 L3 n- e5 \
  4. >>> math.tan(math.pi/4)
      T4 U8 R& s7 p# c9 e' V: A( Y
  5. 0.9999999999999999
    7 y9 g9 N6 s0 @
  6. >>> math.tan(math.pi/6)
    9 C, c/ a6 H' @( J' _* X' O- v! v
  7. 0.5773502691896257
    ( c: l0 K2 @+ N. q7 p4 _
  8. >>> math.tan(math.pi/3)# ]- S, I! l( X( j0 a4 ~3 A
  9. 1.7320508075688767
复制代码

: X! I/ t8 X* U) Gmath.degrees(x)  把x从弧度转换成角度
1 U+ N( y2 ^) H4 e; E1 x1 u, l
  1. #把x从弧度转换成角度1 Q, O# R) O2 Y, @; V- X+ z
  2. degrees(x)
    # o& a: F/ e" l" U0 `8 C
  3. Convert angle x from radians to degrees.3 s: B0 |& k4 B% V3 l5 p" X

  4. ; C3 d! e9 S* k0 M- ]4 c! l
  5. >>> math.degrees(math.pi/4)3 c/ l3 ^  e+ v* P5 s7 e1 @
  6. 45.0% c1 l' [  c! x; Q0 w6 H. y" \
  7. >>> math.degrees(math.pi)! S' v8 O: L+ I3 B0 c- G
  8. 180.0
    4 h2 H# ^$ a5 O
  9. >>> math.degrees(math.pi/6)7 \# J& z; s" c, o- v
  10. 29.999999999999996
    2 G+ E4 `1 I$ b/ z4 X
  11. >>> math.degrees(math.pi/3)3 R3 l# l7 G6 Z. |, V6 X9 j/ B
  12. 59.99999999999999
复制代码
$ i- e/ V2 |, ~, U8 m: A! V0 I
math.radians(x)  把角度x转换成弧度
/ p+ l9 s, E" {
  1. #把角度x转换成弧度
      ]" X6 u5 t/ K6 B3 o
  2. radians(x)
    ' {% L! r4 L6 o0 V/ e) \; A
  3. Convert angle x from degrees to radians.- ~7 K* h# |6 y
  4. >>> math.radians(45)6 K) M; l5 `! J# B' j" z$ Z9 Z
  5. 0.7853981633974483. e- `& x, i2 O
  6. >>> math.radians(60)* D" O. ?8 z' B  _
  7. 1.0471975511965976
复制代码

/ p' |3 I# @" g! r2 [2 w+ i! X0 fmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
) f3 Q7 t6 I+ a6 u+ [
  1. #把y的正负号加到x前面,可以使用0
      Z% f% A# |, x% }( q( A
  2. copysign(x, y)
    - a% W! C9 m) t7 _6 m9 z
  3. Return a float with the magnitude (absolute value) of x but the sign
    % A! e5 O& m7 x' i% x! _1 L4 T
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 }* H% L3 C) }! ?9 W2 ]1 s+ P
  5. returns -1.0.
    ! ^# J% y4 b2 `* c! \4 Q6 k. i
  6. 3 {6 L( N7 M  O: S7 o# Z3 U, D
  7. >>> math.copysign(2,3)3 ^* Q# p' F8 K
  8. 2.0
    " c1 Z+ D3 y: p* ?: i' n) k
  9. >>> math.copysign(2,-3)
    8 @- |* m$ l1 d) n
  10. -2.0- i1 u0 {1 s" F; o/ I2 @
  11. >>> math.copysign(3,8)- J5 t! s5 a) w7 G0 ?; r
  12. 3.0
    ) p9 q  r% M  C! n3 U$ M2 h+ q
  13. >>> math.copysign(3,-8)9 v- {$ l4 J: s$ S- g
  14. -3.0
复制代码
& U1 J5 Y3 V7 g9 Q
math.exp(x)  返回math.e,也就是2.71828的x次方
! |  h" ^' E& b/ Z+ t
  1. #返回math.e,也就是2.71828的x次方* z' L2 ^/ i: f
  2. exp(x), Y& |/ u4 ?, U) \. D) X& l$ ]
  3. Return e raised to the power of x.0 h. t" k; @! S3 C

  4. ) `* O/ s' m0 b  A2 c
  5. >>> math.exp(1)
    # @0 k2 A! d9 ^& s$ u$ `) `
  6. 2.718281828459045! Z4 E4 v) s7 s& k/ _, F
  7. >>> math.exp(2)
    * s0 z3 X, g; i: E/ P
  8. 7.38905609893065, L& i9 g' A4 `" F
  9. >>> math.exp(3)
    / b* e% q* \9 \
  10. 20.085536923187668
复制代码
0 h2 k2 M* n! ~. ]4 s, o0 \
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减11 L# h! z2 R. v  s/ k
  1. #返回math.e的x(其值为2.71828)次方的值减1
    , b3 s- l2 u4 N% ^2 C# W4 ^
  2. expm1(x)
    ) S0 w% f; _; B0 L' J6 U
  3. Return exp(x)-1.
    + x. b; u  D8 U
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.6 s: L" e7 x; i, D1 }

  5. ' T3 n0 R+ x) p6 f0 D" ^1 }. I
  6. >>> math.expm1(1)* H$ S" J- N3 L6 z! s
  7. 1.718281828459045
    ; M9 D* n7 [  {1 p/ x
  8. >>> math.expm1(2)
    5 e" x9 V3 T5 w, W: Q& i
  9. 6.38905609893065
    0 \  A4 f9 y8 ^9 V, a/ Q* N; V
  10. >>> math.expm1(3)
    # K3 R" \5 W. U  z$ g3 O0 y( F
  11. 19.085536923187668
复制代码
, }/ Q7 J, X- K( ~
math.fabs(x)  返回x的绝对值
  L1 {) K7 J9 z" W
  1. #返回x的绝对值4 o* J" N* }- K- n- y* ]
  2. fabs(x)3 @0 |# G, I- P' t; V0 Q$ U
  3. Return the absolute value of the float x.# ~9 ]9 y. N3 y/ B+ y& K$ T0 s

  4. % [, K. Q- z2 a2 v5 h3 b
  5. >>> math.fabs(-0.003)
    & x0 V$ D) X  P% D0 x5 W
  6. 0.003
    $ V, S+ d, Z1 u0 v5 Z4 J) K
  7. >>> math.fabs(-110)
    / n% c+ W5 K: U, t1 H' E
  8. 110.0! C: _3 |" X* l( G3 D8 z* x
  9. >>> math.fabs(100)
    8 _4 @* x4 M9 i2 b0 v: l; l  {
  10. 100.0
复制代码

8 K8 w2 m( X/ @! l3 O9 E' ]( Amath.factorial(x)  取x的阶乘的值
$ k5 }! \( U. N
  1. #取x的阶乘的值1 c/ c" S  ]7 T, x. \" X
  2. factorial(x) -> Integral- Y4 l. p! o0 _' Q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
      l. @, X5 t8 Z
  4. >>> math.factorial(1)9 I1 }& F) x# H, O$ J
  5. 1
    - a; a& C5 H$ J- y* N
  6. >>> math.factorial(2)6 ?$ D7 J/ r4 m+ i+ z/ Z0 A8 b( h
  7. 2
    : k8 x3 w0 u  d) N3 l$ @0 b" b. J
  8. >>> math.factorial(3)2 T/ c7 i+ ~) G! U8 D6 T! C& j$ h
  9. 6
    7 O* B+ E% K6 M2 h, v, i- b* N1 l
  10. >>> math.factorial(5)4 ~0 E, \% i# H
  11. 120
    7 p. G  q# y6 @8 \+ j5 T: t$ n
  12. >>> math.factorial(10)
    $ {- u% V* O! |3 z
  13. 3628800
复制代码
) e, `% e& o6 O
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数/ S- x7 {1 `- G
  1. #得到x/y的余数,其值是一个浮点数7 r# I: D  x" F
  2. fmod(x, y)9 N( ?* F. c# x# n  b; ?
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    5 }& j0 r* B4 K8 C& ^
  4. >>> math.fmod(20,3)- }6 ^: W; \4 j1 l% b2 N; C7 ?
  5. 2.0
    1 o% }3 a: N3 M7 E; B: m* F
  6. >>> math.fmod(20,7)
    ' D% ?5 t  B' o9 r4 S
  7. 6.0
复制代码

3 E% ~+ m/ b8 Rmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 l( ]: Y. h; d# F7 k
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
      ]+ u6 c1 h3 b$ g& l3 S' e
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值1 g  }& ?: I3 A
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( }* a. Z) |( t, L
  4. frexp(x)
    ( T% V6 t2 ^8 {; a& n1 z$ H
  5. Return the mantissa and exponent of x, as pair (m, e).( U: t2 g* `: D, G
  6. m is a float and e is an int, such that x = m * 2.**e.
    % Q0 W; }2 s* o; e' d. L% L  e2 H3 W
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.. K8 G" S/ w4 Z9 _5 H
  8. >>> math.frexp(10)
    6 b) p6 v8 A% G0 L( ^3 }  V
  9. (0.625, 4)
    + X5 C1 |" ]) c$ Q
  10. >>> math.frexp(75)
    ( K, I" [# ^. U
  11. (0.5859375, 7)2 [1 r0 p' L. Z. f0 @* I" r& h2 a7 M+ @
  12. >>> math.frexp(-40)5 Q) _3 J1 z/ J9 |  ]
  13. (-0.625, 6)
    , X% Z9 s7 }9 H# R/ a) @2 Y
  14. >>> math.frexp(-100)
    $ K: P9 N, H4 O6 L( B, o
  15. (-0.78125, 7)' m5 ]% M' a# y& `
  16. >>> math.frexp(100)# @' S( l- s, D. T7 S' U
  17. (0.78125, 7)
复制代码
/ e; `8 X/ m3 t; h, l' _
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
; E! l7 C7 z+ k: p+ X0 A
  1. #对迭代器里的每个元素进行求和操作
    ' g5 W3 w0 g/ b2 X0 T
  2. fsum(iterable)1 ~- b1 B! q5 p% H$ |; `
  3. Return an accurate floating point sum of values in the iterable.
    - F9 n1 j5 l: d& ?6 K8 d
  4. Assumes IEEE-754 floating point arithmetic.
    9 I6 r3 ]4 A9 G+ b- O% `9 B% o
  5. >>> math.fsum([1,2,3,4])
    6 X5 I) i/ T( y  H" T( q, b. X
  6. 10.0$ t2 g  K: O1 {& H$ J9 w) }
  7. >>> math.fsum((1,2,3,4))
    5 C  [0 s5 U" {8 G/ g
  8. 10.0
    & M5 w) G- W+ y% e1 E7 Y- A' N( o
  9. >>> math.fsum((-1,-2,-3,-4))- n6 }& E8 Y# {' v9 w4 r4 H; R
  10. -10.09 P- o0 E0 z) M7 ~3 R9 Q
  11. >>> math.fsum([-1,-2,-3,-4])" d: j; ~5 r- M4 m' G
  12. -10.0
复制代码

- b% j$ W3 F! d7 S+ bmath.gcd(x,y)  返回x和y的最大公约数
- G3 T) ?' {6 n$ g
  1. #返回x和y的最大公约数# m/ `: E' N8 _. u7 m
  2. gcd(x, y) -> int0 Y3 y" H- D4 [4 |$ V( F
  3. greatest common divisor of x and y+ x, `. B2 K' Z! ]. ]  W8 O9 J
  4. >>> math.gcd(8,6)
    ; _4 I* g) B$ }. W1 y: U
  5. 2' C5 n8 ^: s& s1 ^  I
  6. >>> math.gcd(40,20)
    6 I) I& E+ K# U8 H- o: b# x
  7. 20
    ) T* P& W: q9 [* O9 a
  8. >>> math.gcd(8,12)$ g5 H) C  W( f
  9. 4
复制代码

, D5 l* h2 H  Emath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False4 U8 y# u' f. ]  L7 r. r% v$ D
  1. #得到(x**2+y**2),平方的值" }, M4 ~  N; I3 N, }* Q5 d6 F
  2. hypot(x, y)
    & U9 F6 c* m& m9 J5 R& m: d
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    1 W) W' o& ^: e
  4. >>> math.hypot(3,4)
    3 b2 g5 d1 I' `
  5. 5.0, t) I; ^' _/ c
  6. >>> math.hypot(6,8); A/ D6 j6 c' \$ {$ k
  7. 10.0
复制代码
/ M( }0 C& f3 n# E/ ]" B5 L8 L! ?6 P
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False  M# {# m; w9 M6 S  F
  1. #如果x是不是无穷大的数字,则返回True,否则返回False5 d. F4 v9 X! k
  2. isfinite(x) -> bool
    : e& ^4 `# T. }8 v% b6 k, M& K4 x
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ) }' U5 d* V% q) F2 j* `/ r
  4. >>> math.isfinite(100)
    2 X& q8 F0 ^, \3 [  J) h& i
  5. True3 \/ n! E0 a$ _4 k9 v6 C0 r0 w6 A
  6. >>> math.isfinite(0)
    0 R. z; C' L. z) B; V
  7. True) ]7 v  k. h( P6 s5 H3 v+ Z
  8. >>> math.isfinite(0.1)$ e( t* {8 O. L8 x0 J
  9. True
    . C+ w# R6 M0 D) T# p5 O- d7 J
  10. >>> math.isfinite("a"): c1 N; `  m* w& Y; p
  11. >>> math.isfinite(0.0001)
    8 i+ X' k" ?0 O, T' `( I- p
  12. True
复制代码

/ s. Y5 F+ u# s0 D/ Xmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False" y7 n# X. f) A- c2 y6 Y
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    1 K( D/ \1 j% R9 F7 t3 P+ k( q
  2. isinf(x) -> bool. a4 q" M/ l4 |
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ) {- h+ u( y7 f$ n8 h" _
  4. >>> math.isinf(234)2 o5 _. ~! v$ U# B( [
  5. False
    ) f- @1 {2 D9 E! u+ }
  6. >>> math.isinf(0.1)8 M, o5 W" {9 R* Z9 M
  7. False
复制代码
2 }$ J# M6 Q& b: r1 R
math.isnan(x)  如果x不是数字True,否则返回False2 c( h( Z7 f1 M. ~2 O0 ^
  1. #如果x不是数字True,否则返回False1 H0 v# M0 m4 r
  2. isnan(x) -> bool$ j7 i0 S4 G5 |" V% ~
  3. Return True if x is a NaN (not a number), and False otherwise.
    ; [0 n7 |% h( M$ l
  4. >>> math.isnan(23)0 R! Y% B3 J% m; R7 A
  5. False
    * l9 [/ y8 H( Q5 w% N7 F6 A
  6. >>> math.isnan(0.01)
    / W! o; D* w( t6 p* C8 D
  7. False
复制代码

1 X& ~. h. y7 C  y7 V7 ~8 Rmath.ldexp(x,i)  返回x*(2**i)的值
3 o+ a6 D. c" @, N" X; S
  1. #返回x*(2**i)的值, r4 ~6 I" W# a
  2. ldexp(x, i)6 a& M2 B* [6 M
  3. Return x * (2**i).$ S$ v' T9 W  R0 x% ^" h- ]
  4. >>> math.ldexp(5,5)9 n1 F# z8 s; W/ D
  5. 160.0; i" e4 R+ G" p( _8 q' B/ e
  6. >>> math.ldexp(3,5)
    5 S, h! T$ v" K
  7. 96.0
复制代码

7 r3 f! R( W+ A  C9 c6 B2 n- A- wmath.log10(x)  返回x的以10为底的对数
5 o7 Z" W$ g# z* V+ H0 t# ?0 N
  1. #返回x的以10为底的对数
    & n# ^: F  i. o% y" q" Q
  2. log10(x)
    4 r$ n' r7 g/ C
  3. Return the base 10 logarithm of x.2 e8 T: q1 z( N: n3 ]# Y2 i/ W
  4. >>> math.log10(10)7 \* ~5 D5 N/ b- _  @0 L. m
  5. 1.0; V8 ]  R3 I3 e
  6. >>> math.log10(100)
    3 G  o& K  C$ N6 Q) h
  7. 2.0( y/ q$ i6 j2 h3 `: |
  8. #即10的1.3次方的结果为20/ \" n# ?/ Z  h* r  q+ b
  9. >>> math.log10(20)
    + k8 R( j; |6 m
  10. 1.3010299956639813
复制代码
4 n' W5 Y: y, v4 G6 B* C; _1 G  X
math.log1p(x)  返回x+1的自然对数(基数为e)的值
# C. f2 S9 i# _* S
  1. #返回x+1的自然对数(基数为e)的值
    + `9 l$ V8 b) Q# B9 {  b/ ~
  2. log1p(x)6 T) ~- P' W% |. K# t- t: K' M$ i
  3. Return the natural logarithm of 1+x (base e).5 n& A9 K1 Z$ {( [, ?& c/ D
  4. The result is computed in a way which is accurate for x near zero.0 h/ J0 k/ D- s. S' M+ v, r
  5. >>> math.log(10)
    $ X3 k( B. j' L+ J
  6. 2.3025850929940460 a5 N; g  L, `3 w' ~9 W0 c
  7. >>> math.log1p(10)
    / v7 p. {. B1 _% u
  8. 2.3978952727983707' R! t/ B1 \" u) V8 D, C
  9. >>> math.log(11). K' v; D/ ?) F# `  [
  10. 2.3978952727983707
复制代码
" ]  K2 p( x; O8 l$ N
math.log2(x)  返回x的基2对数' s/ q* [" ^* W0 z, W
  1. #返回x的基2对数9 M) I0 C7 w2 X' \/ G
  2. log2(x)# Y4 p) r8 ?2 \5 x" Y/ x- Y5 `. H
  3. Return the base 2 logarithm of x.# Z! J- y5 m: ?, Z- c
  4. >>> math.log2(32)
    " t5 |- C6 i; y# H0 f( Q" }" z% V
  5. 5.05 t  s7 L8 `+ X) T  T5 y; p( y7 l
  6. >>> math.log2(20)
    0 g9 `) ?! j( Z+ H; A: D' |* ]
  7. 4.3219280948873632 l0 w: ?* b3 n
  8. >>> math.log2(16)
    3 [: |# Y$ o, P
  9. 4.0
复制代码
( N# K9 R) |/ h6 Q0 e  L5 O
math.modf(x)  返回由x的小数部分和整数部分组成的元组3 J/ B: w$ i. E. b* f# ]
  1. #返回由x的小数部分和整数部分组成的元组
    6 A4 ?3 [+ S& e; Y: G. W" \
  2. modf(x)  x3 f7 g* M0 ^5 c6 k
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ( P6 V* U  H( S. H) L. W
  4. of x and are floats.1 z+ V) R8 Y4 e3 G" c
  5. >>> math.modf(math.pi)
    * l% j( q* I# u4 n  W7 D/ a
  6. (0.14159265358979312, 3.0)* j+ O2 K) U" a5 [$ {: M' T1 V
  7. >>> math.modf(12.34)
    - Z3 {; L+ q* h5 O) V6 Y# k- z
  8. (0.33999999999999986, 12.0)
复制代码

9 }" N# F. q' v' n/ V2 O7 @math.sqrt(x)  求x的平方根$ e0 e) |, R: }+ s: V* o% m) q) V/ B6 u: H
  1. #求x的平方根" r: `' h1 t' W! \7 A' F/ K1 I* ?
  2. sqrt(x)! I! S7 ]1 ?% |6 N0 }
  3. Return the square root of x., F& G: ~! r2 m  Q
  4. >>> math.sqrt(100)4 n/ c4 S9 |* r" d
  5. 10.0; d" w# r. I" @: |9 j) d* Z
  6. >>> math.sqrt(16)
    8 P, {5 [3 h8 M7 F3 `
  7. 4.0' A3 y8 L4 F; W, H+ y
  8. >>> math.sqrt(20)
    1 I$ D* Q$ Z4 N+ V9 R" S- F4 [8 A& g
  9. 4.47213595499958
复制代码

4 I; E8 W% i$ o& q3 S0 b! Zmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    3 \) y! x! F+ R* x. B
  2. trunc(x:Real) -> Integral
    : M. x. D5 V5 p* r0 X/ i. w
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# I9 B' j( s$ Y. S# ?  h4 O( j( ]: |
  4. >>> math.trunc(6.789)
    0 \. I, @+ b& Q. V( |
  5. 67 \. g. t& Y) k5 I; @
  6. >>> math.trunc(math.pi)
    4 S( T0 L( F% G  f4 R
  7. 3
    . j' ~  L( Q) i
  8. >>> math.trunc(2.567)9 P2 t3 v, f, F& V# l  r9 C7 `* p
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-5 10:26 , Processed in 0.096135 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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