新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

! ^4 X3 J6 n! w8 B; A& T【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, q# Z5 U# d! L6 Z, ]3 T& n
& @0 R& q6 z# @  ?: h
方法1. P8 n0 @8 i  L% [0 s
  1. >>> import math
    5 G7 ]' M$ e) X! m/ _
  2. >>> math.sqrt(9)% s! Z" e2 ^0 C: b2 T
  3. 3.0
复制代码
方法2! ?  X; M% T# v
  1. >>> from math import sqrt$ u3 L2 L; F# M. N4 N( _
  2. >>> sqrt(9)
    4 s1 W( f' N' W' [% Q
  3. 3.0
复制代码
4 Q: r+ Q# B) [% m, H$ |9 c6 I# }3 ^

- f& N* U# d' H1 L. Q1 I
math.e  表示一个常量6 {* p: }; v1 f# M! J# y5 a
  1. #表示一个常量+ }# G% v0 i8 U  F
  2. >>> math.e& k5 [2 u2 z# t- T. R' I1 Q
  3. 2.718281828459045
复制代码
+ j+ t/ G! _. H# D1 L& @  i
math.pi  
数字常量,圆周率

( j  ?2 D! P4 M+ g6 t2 V8 y+ j
  1. #数字常量,圆周率. X' T; N# z7 C: }( Q* |
  2. >>> print(math.pi), @; a! t! l3 |3 N
  3. 3.141592653589793
复制代码
( W% K' O$ v6 j6 W; j- Y3 ]# \% Z
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
. A8 k% \; y, ^
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    2 H+ B7 ?) R) U- }) m$ |2 \1 w4 E+ B) ?
  2. ceil(x)9 N- O3 ^+ D$ `- w
  3. Return the ceiling of x as an int./ H1 H( z. \3 @9 x  x. u
  4. This is the smallest integral value >= x.2 j, M8 }/ T$ U
  5. + ]: }5 P$ N' q
  6. >>> math.ceil(4.01)
    7 y# k$ I3 ]2 p, T/ r& E
  7. 5
    / `; c; s; q3 I1 N
  8. >>> math.ceil(4.99)) m, m* a6 ]9 U0 ~+ Y2 Z  f: ~' h( h
  9. 5+ s7 [. ^* \" G6 g
  10. >>> math.ceil(-3.99)
    8 B5 o, V, E% |3 ~1 C
  11. -3
    6 ]8 p5 U5 K3 v! e1 E) x5 t
  12. >>> math.ceil(-3.01)$ L4 y4 W- h; a. U" d
  13. -3
复制代码
% i- R# Q6 I. f0 C3 n/ _
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 Z6 |, J* b: |/ x) Y) d
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身! A. E. |* e2 \
  2. floor(x)0 }1 }6 k( K- n. e# Y
  3. Return the floor of x as an int.
    . Y6 A( _$ i: F! t' ~, J
  4. This is the largest integral value <= x.& k+ W) U2 a. ^& }8 {' d/ o
  5. >>> math.floor(4.1)5 Z- c- B3 l4 C# z: \8 T
  6. 4% T  s3 e9 x" B; J
  7. >>> math.floor(4.999)1 y$ j6 U: T# M$ @
  8. 4
    ) Q+ @% n) {5 o$ h4 Q+ D% H' L
  9. >>> math.floor(-4.999)2 H) P: s6 {6 G+ k5 {& a0 c
  10. -5
    + M+ ]" Z: p. h
  11. >>> math.floor(-4.01)/ m& S, I# Q* V1 q) t+ x6 I9 T# G
  12. -5
复制代码

- e# F7 A- o7 o, X% M4 |4 |( dmath.pow(x,y)  返回x的y次方,即x**y
" o: s9 i) [- i% C( V4 D
  1. #返回x的y次方,即x**y0 J2 I( V: t6 B( T# e! c
  2. pow(x, y)! H; q# q5 Q' y( A. t0 m  C2 E! z
  3. Return x**y (x to the power of y).
    9 [+ j( _" }9 B$ t: r! P6 r- q( r
  4. >>> math.pow(3,4)
    ! N/ R6 r- U; U7 {
  5. 81.0' J# ~; R4 U2 T6 w1 p9 c+ r
  6. >>>
    1 v  w. S% I. @; N: a
  7. >>> math.pow(2,7)
    # t: }1 G0 `: W( i6 ?1 n
  8. 128.0
复制代码

1 b5 z# y0 S1 t! ?  T, V! G* Hmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 h$ d3 d5 s. q1 @
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- {7 p* e) L/ e- q$ ~8 |7 f( n& a5 C
  2. log(x[, base]), @& H9 J, b2 C$ q# j$ Y
  3. Return the logarithm of x to the given base.
    2 O) \* \8 m6 e; ], [$ P
  4. If the base not specified, returns the natural logarithm (base e) of x.
    6 x5 m* w+ `7 |' L9 j1 j
  5. >>> math.log(10)4 ~4 D) O, _3 Y8 f1 d: A$ J
  6. 2.302585092994046
    ! p0 q4 r: R! _( ]' b& X& a
  7. >>> math.log(11)
    0 U# p; F. R7 I: E- ~! i
  8. 2.3978952727983707: m! A9 U" N" Y. ]4 P& p. [9 B& C, D) d
  9. >>> math.log(20)6 L) a1 E2 k! I  O2 m
  10. 2.995732273553991
复制代码
' U3 W( T* A. B% Z0 X0 ?1 d
math.sin(x)  求x(x为弧度)的正弦值0 b+ ~* M4 {* Q
  1. #求x(x为弧度)的正弦值
    ) N( F# h1 Q# X7 Y: ]- G
  2. sin(x)
    # ]1 ?! w, G/ d  I2 W! |% z
  3. Return the sine of x (measured in radians).
    + b1 Z" o: _8 m9 S$ i+ R; {' s
  4. >>> math.sin(math.pi/4)0 [+ I& c/ i2 z* n3 G  ?9 H) B& x
  5. 0.7071067811865475$ V, J* i5 y1 a. E3 T
  6. >>> math.sin(math.pi/2)
    . H/ t1 G# B* ?0 E
  7. 1.0
    ( P" E6 W+ [* L) c+ ~" U
  8. >>> math.sin(math.pi/3)' G0 ?, T5 q  j7 A2 O8 P  c% |
  9. 0.8660254037844386
复制代码
) l. M+ E2 {& P
math.cos(x)  求x的余弦,x必须是弧度% p6 _& g3 w7 l7 R8 ?
  1. #求x的余弦,x必须是弧度  ?8 M  E1 R9 [
  2. cos(x)
    7 |! W  b9 \+ U5 }  v  n8 u
  3. Return the cosine of x (measured in radians).3 n: I0 \+ f6 d4 u
  4. #math.pi/4表示弧度,转换成角度为45度
    + k. B$ @( L9 }( p! j
  5. >>> math.cos(math.pi/4)- p$ }" v4 N) G0 g, [
  6. 0.7071067811865476' e* r4 e6 N8 k- [. J* V; B
  7. math.pi/3表示弧度,转换成角度为60度
    ; C, }8 M$ }0 T3 I
  8. >>> math.cos(math.pi/3)
    ( h2 w8 F0 ^6 m4 i1 ~( f5 K; `* w2 W
  9. 0.5000000000000001
    . f! K) e8 ]2 v6 V/ O/ Z: J
  10. math.pi/6表示弧度,转换成角度为30度
    1 P+ a+ ~& ?, F
  11. >>> math.cos(math.pi/6)! `5 `" T* r" q( }) T1 w9 `4 c1 n
  12. 0.8660254037844387
复制代码
# C6 g6 \5 K  q
math.tan(x)  返回x(x为弧度)的正切值' R1 X  ^# R6 Z, h: `/ U* T
  1. #返回x(x为弧度)的正切值/ ~( Y1 H7 }/ a. M8 w2 b7 K5 f3 ~) h
  2. tan(x)
    ) P& [  m9 s# Z6 y+ J* S; ~
  3. Return the tangent of x (measured in radians).
    % H7 @" [2 [+ Q" {3 J
  4. >>> math.tan(math.pi/4)9 i0 l1 F/ \8 Y- w3 B
  5. 0.9999999999999999
    / ?3 K* P& e3 x/ L9 ~: u/ O
  6. >>> math.tan(math.pi/6)
    ; I. t& ?! J( M3 p: O4 N
  7. 0.5773502691896257- w2 y: a. b7 e
  8. >>> math.tan(math.pi/3)
    ( i& X. E8 o2 k4 P' z, e: H  O7 x2 w
  9. 1.7320508075688767
复制代码

, y- }5 @8 o/ m  j' O. ]math.degrees(x)  把x从弧度转换成角度
. a" r5 E. x; r7 \8 \2 T! T- H. s
  1. #把x从弧度转换成角度; ?' M5 k6 Z  u1 j0 e/ V5 _) j
  2. degrees(x)
    # J% q; J$ `9 K+ _8 X* ]8 _
  3. Convert angle x from radians to degrees.4 r; n+ H% w6 H  G$ V$ J

  4. - }0 {% H0 L9 k; I: r4 i2 h% e  o, s
  5. >>> math.degrees(math.pi/4)$ a* ~/ D- b' O6 U4 M1 d
  6. 45.0
    ' O' X  D, z# s, a2 g6 e
  7. >>> math.degrees(math.pi)5 U4 W5 T) b8 p6 ?0 u; Q. r5 A' d
  8. 180.0
    0 s4 c; q! C. s6 ]/ L# ~
  9. >>> math.degrees(math.pi/6)
    " o# h: A6 l6 r+ y
  10. 29.999999999999996
    - D# ~4 w0 S" K# t+ I8 C: Z
  11. >>> math.degrees(math.pi/3), y1 X; ]* X3 p
  12. 59.99999999999999
复制代码
! A9 Q: ~, R+ F2 w
math.radians(x)  把角度x转换成弧度& f6 j7 G, s) h* |* T
  1. #把角度x转换成弧度: w# \. }' Y& Y/ Z% {  m( k2 v
  2. radians(x)" }  ^. A2 }1 \8 L, q$ w
  3. Convert angle x from degrees to radians.* I& @' ?2 {! x7 n
  4. >>> math.radians(45)
    6 Q) D6 Y; c7 e; S# L* q. d
  5. 0.7853981633974483* y' n  z7 ~9 Q6 @! M
  6. >>> math.radians(60)( s0 f+ [0 }3 a! ^
  7. 1.0471975511965976
复制代码
! V4 h, W' P- T" k' T  e
math.copysign(x,y)  把y的正负号加到x前面,可以使用0' M! r) a4 F( O3 Q
  1. #把y的正负号加到x前面,可以使用0% X! q' L, c6 |0 `1 Y% U
  2. copysign(x, y)/ v8 t6 ~) ?" A! [
  3. Return a float with the magnitude (absolute value) of x but the sign ; s& x" p& Q" B
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    9 U0 u- s8 b3 |( d+ A, L) L
  5. returns -1.0.5 _2 ?! i$ Q' y* a# p( ~0 @$ ^9 U6 c

  6. - k0 g7 y& [/ v$ X/ j* V9 t5 z
  7. >>> math.copysign(2,3)! G( s, T' e$ G
  8. 2.00 o' p* A% X# e( q
  9. >>> math.copysign(2,-3)
    * _& D: n( k  l6 |- L. ~3 {
  10. -2.0
    - \2 l% V# W7 `: P
  11. >>> math.copysign(3,8)
    ) u8 O/ }5 K6 ~! }
  12. 3.0
      ^- r! Q) T2 P% c' l4 J
  13. >>> math.copysign(3,-8)
    . _$ T* f: _# K& u* a/ P' F5 y% g# r
  14. -3.0
复制代码

( z6 j5 j; L  z6 m8 `math.exp(x)  返回math.e,也就是2.71828的x次方! y/ X1 d- j. l; X8 u
  1. #返回math.e,也就是2.71828的x次方
    " @# W5 I) z* l, {
  2. exp(x)
      H, C& B( l$ a0 G; p, u6 k2 S5 V
  3. Return e raised to the power of x.$ O7 c1 |7 z9 I% r! D' v

  4. * D% e* ^- c' s, ?+ |) X
  5. >>> math.exp(1)" |4 a, U- e0 Z6 ^/ V
  6. 2.718281828459045" ~! A+ x: f3 z$ f( O8 z- o4 L
  7. >>> math.exp(2)/ \( h- _1 h+ b* B; }  p+ b
  8. 7.38905609893065. P2 h7 u0 u# p* U& ^
  9. >>> math.exp(3)- ~. r; k* h2 r: C: D0 k
  10. 20.085536923187668
复制代码
) S3 X8 z" }. Q( p0 X
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
& L* ^: B* ], f" }9 F
  1. #返回math.e的x(其值为2.71828)次方的值减1+ i" E$ H3 ~8 l2 F6 ?
  2. expm1(x)
    ) ?- A& O/ P8 ]9 V" X' Q9 Y
  3. Return exp(x)-1.3 B, `/ o$ n9 |' |9 z
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 E8 r, S( \- @; v0 Y

  5. / h$ ]' U9 |3 q  u* L
  6. >>> math.expm1(1)* L; A# j5 c. f
  7. 1.718281828459045
    8 k1 P4 X3 b* q' t
  8. >>> math.expm1(2)
    & g; d( s1 K# C: B
  9. 6.38905609893065
    + b! \9 M5 D3 S) p
  10. >>> math.expm1(3)
    , _( R* z! ?0 Z5 K$ F
  11. 19.085536923187668
复制代码

8 b' J( s+ i0 e" N: z5 N3 kmath.fabs(x)  返回x的绝对值
. m' T9 E; \8 |* V; \
  1. #返回x的绝对值7 g9 h! Q0 |- i. M+ ~4 I
  2. fabs(x)/ F+ J5 ^  f: b
  3. Return the absolute value of the float x.
    9 v: u8 Q' s  b+ Y& k( ^  G

  4. $ O7 O* @, `2 X4 ]8 H" |
  5. >>> math.fabs(-0.003)
    9 R8 o: m# W1 @% x- l+ P- c! Z( A; j
  6. 0.003
    % B5 {( [$ }0 @
  7. >>> math.fabs(-110)
    - C1 D4 I( J# z1 k' _
  8. 110.0
    5 x* |( o. N5 y0 x* i) l* R
  9. >>> math.fabs(100)
    6 u/ S& V& _, L7 \
  10. 100.0
复制代码
  v" P; b# f$ d' I0 @
math.factorial(x)  取x的阶乘的值3 [" a4 z  d& l) Q
  1. #取x的阶乘的值0 e+ R  I, [; S( C; Z. r8 O
  2. factorial(x) -> Integral3 ~9 v& m0 y: w  a& ?2 h
  3. Find x!. Raise a ValueError if x is negative or non-integral.! h9 P" G! b  ]4 u1 }$ T3 L: b" n
  4. >>> math.factorial(1)
    5 e4 K8 ~5 k4 v2 o2 }
  5. 1
    ! q# }, {5 [2 w& C$ A: C6 T
  6. >>> math.factorial(2)3 {- m, o$ y2 `8 ]# V7 |) \" }  @
  7. 2% u# r# r/ K1 p& ~, P3 [/ d4 m9 T
  8. >>> math.factorial(3); {5 Q1 \& Z, X: k6 L; N9 t9 a! _
  9. 6
    : W2 l8 {2 H( w2 b, M  Z3 v' H* P
  10. >>> math.factorial(5)) q8 @; ]/ }) f+ Z- s" |' s% }3 k
  11. 120
    3 C# R" r  k2 `
  12. >>> math.factorial(10)
    1 S. B) w6 N6 R# z/ e- C* I
  13. 3628800
复制代码

: ^! z- e' x; S9 `math.fmod(x,y)  得到x/y的余数,其值是一个浮点数4 R2 h/ {4 ^7 P1 Z) T0 b
  1. #得到x/y的余数,其值是一个浮点数
      y& [" c4 [. O( ^
  2. fmod(x, y)0 E) l3 J: S6 s# P# S$ {/ F
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    * ~: m, A* @+ w  p
  4. >>> math.fmod(20,3)0 h0 W$ e( Y; e+ |
  5. 2.0
    8 W1 p, w7 q- a
  6. >>> math.fmod(20,7)& z$ D+ W% O' R
  7. 6.0
复制代码
4 R! k# n1 U* J( {% e: C5 u
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
1 C6 V8 @% _! G! b, l; D
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 E) @8 ?  D: H
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 L/ E) I- J1 K0 _' J/ t
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    $ j; G5 c  R4 C8 v
  4. frexp(x)3 _# k4 W+ {( \7 F1 V. x* O; o
  5. Return the mantissa and exponent of x, as pair (m, e).
    ) B+ U3 O  e7 q
  6. m is a float and e is an int, such that x = m * 2.**e.
    $ X! i- l: V/ M( s, x1 T( i" R6 W0 Y" s
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    / ]4 T  m2 |+ Y$ _7 I
  8. >>> math.frexp(10)
    ( y4 V6 t# Y4 x
  9. (0.625, 4)
    ( _- v& ]+ [# Q& \
  10. >>> math.frexp(75)  C  r1 w1 G) z- {2 C
  11. (0.5859375, 7)
    0 D- s3 \* \0 G4 }; F/ c, Z
  12. >>> math.frexp(-40)
    ) t% j1 h" K, C
  13. (-0.625, 6)  R; T5 P: |2 }5 u
  14. >>> math.frexp(-100)
    1 s( E' H& H  h" w+ {) M
  15. (-0.78125, 7)
    5 `6 p$ q5 K8 n1 G* E' X9 f
  16. >>> math.frexp(100)4 E5 b3 H9 P6 \9 C1 ~! I5 f4 r/ ?
  17. (0.78125, 7)
复制代码
# i: K8 \: p/ s( `! j, p6 P
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
* u$ e, N# J. k& `3 `. E! X. F
  1. #对迭代器里的每个元素进行求和操作" \. S5 @. d3 P. H6 C4 T- \2 v) o
  2. fsum(iterable); `4 K; |8 J  E" e
  3. Return an accurate floating point sum of values in the iterable.
    6 y& a/ u4 x7 D% E0 x% ?, e  k
  4. Assumes IEEE-754 floating point arithmetic.* C; F! t! S# S' O
  5. >>> math.fsum([1,2,3,4])
    " G4 N9 T; \, F4 i0 K4 h% b
  6. 10.0* P: T" r0 K4 p
  7. >>> math.fsum((1,2,3,4))
    0 M/ M1 ?' k! P0 m7 k. k% t
  8. 10.0
    ' m/ A3 S  ]& ~$ B# e9 s: W1 J: i
  9. >>> math.fsum((-1,-2,-3,-4))
    ( D, |" X$ b/ A1 f# j) q) e
  10. -10.0
    : s3 ]( l! w* v: y* F
  11. >>> math.fsum([-1,-2,-3,-4])
    3 t" ?8 f, Y  m" V2 u# b% x3 \0 o: b
  12. -10.0
复制代码

6 N( H1 S1 J) K8 Cmath.gcd(x,y)  返回x和y的最大公约数7 ~) ~5 l  R/ e9 o6 D8 w
  1. #返回x和y的最大公约数. O5 L- @1 H% A" U. V
  2. gcd(x, y) -> int
    1 U) l! a+ e% _' g! {$ Z! X( U+ n
  3. greatest common divisor of x and y
    : L. O8 J5 ~" U$ K
  4. >>> math.gcd(8,6)
    " u( r$ S& U( m" Y
  5. 22 C9 q2 _4 o) }  z3 E
  6. >>> math.gcd(40,20)1 v0 Q! ^, [) V0 D8 t: w
  7. 207 j3 F# R+ J" S+ z8 V; P
  8. >>> math.gcd(8,12)
    5 C+ y5 N" b* s" F
  9. 4
复制代码

" C( e8 p8 t0 A4 Imath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False! {) u. X) b0 ~
  1. #得到(x**2+y**2),平方的值' R& ^3 ]3 Q6 |( [  J+ r7 t! J# z
  2. hypot(x, y)
    & U; W. ?, R# O+ _* _0 u6 y8 K
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    " e% ^% ]  U) [  x. B. E5 A& c
  4. >>> math.hypot(3,4)
    9 \+ U- g+ A$ b$ g  q% S
  5. 5.0" f+ B7 @# Z: X5 o  t
  6. >>> math.hypot(6,8)# B4 d" ?7 Q% F  v
  7. 10.0
复制代码
  [8 d! I5 m& Y. c. c3 Q5 @, X
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False3 O% R, A# c8 j. J
  1. #如果x是不是无穷大的数字,则返回True,否则返回False! @+ @, M5 v- f. N% W4 ~8 t" p: ]
  2. isfinite(x) -> bool
    , y8 ?. y3 ^+ G2 ~2 k  A
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    . B7 K* M6 C/ E& o7 F, G
  4. >>> math.isfinite(100)) v3 }6 l) n3 h" ?- C4 k
  5. True5 U" R2 a  w+ U/ `% S6 H* y) G
  6. >>> math.isfinite(0)
    4 K1 L* a; J: M; x# G9 n5 B9 }
  7. True
    , g% H# ~- l6 n  n* P& S/ \; Z
  8. >>> math.isfinite(0.1)% T4 m4 [9 w1 {" T% a, ?5 N, H+ U
  9. True! F6 I  Q0 i, |: j% N% n
  10. >>> math.isfinite("a")
    . q+ ?/ y$ S' T# _: @9 X, C# E) o
  11. >>> math.isfinite(0.0001), S1 J- `* p- q2 o' r) Y: {% s3 q
  12. True
复制代码
( H" Q5 p7 \1 P5 c, P
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
6 T' b( J6 Y1 }* A  m3 l
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False+ h2 v; Q( Z: b
  2. isinf(x) -> bool3 r; y) d# R* g4 U! R  t
  3. Return True if x is a positive or negative infinity, and False otherwise.
    # L  M0 x& S0 _( V9 W
  4. >>> math.isinf(234)
    , q& V% u. Q1 q8 G; V" h
  5. False/ g. L/ E) l/ {; n' {
  6. >>> math.isinf(0.1)1 m6 k/ Y" l7 Y0 I! m4 C  N0 t
  7. False
复制代码

. X, ]. q% \% V* _- k- T8 Dmath.isnan(x)  如果x不是数字True,否则返回False' I, K& G  j3 }9 f6 I" d8 r& G  |
  1. #如果x不是数字True,否则返回False
    % h6 f6 U  g% [# }
  2. isnan(x) -> bool4 [/ X/ L& C6 _) s4 z0 s
  3. Return True if x is a NaN (not a number), and False otherwise.! y6 q0 A* V# {$ [3 {
  4. >>> math.isnan(23)% E3 G" j7 C$ d0 ~" y% y& s
  5. False4 A) G% u$ X  c9 s- f
  6. >>> math.isnan(0.01)' X* R: ~: i7 G! o$ B$ y
  7. False
复制代码
% ^' E+ ?- E6 O/ G
math.ldexp(x,i)  返回x*(2**i)的值+ K8 O5 j* i& k+ g5 ]: \2 P( B
  1. #返回x*(2**i)的值: p( _) j2 C' Y- ]' }8 g0 i
  2. ldexp(x, i)
    . V. ~$ d- s* j' U" y8 z
  3. Return x * (2**i).3 n7 Y1 u2 \: q+ ]! b+ B; P  w
  4. >>> math.ldexp(5,5)
    # k) C1 w" J& ~4 w( S4 L
  5. 160.00 j: Q0 q) P6 r
  6. >>> math.ldexp(3,5)
    6 l3 F0 j5 {/ G7 N
  7. 96.0
复制代码

' C" y6 b/ F; A+ ?math.log10(x)  返回x的以10为底的对数. Y; U+ H2 p2 f) r2 \8 A
  1. #返回x的以10为底的对数
    # y! j2 j) e. K8 U& A3 ?' [' s
  2. log10(x)$ y) I, t% S) w
  3. Return the base 10 logarithm of x.
    / j; B; M5 L1 X* R/ ~: v0 K' ]! ~
  4. >>> math.log10(10); n9 r$ p! G* C# u' Z6 Q
  5. 1.0
    ' d7 }! F, U0 ~$ |4 H9 k$ ]
  6. >>> math.log10(100)
    & v% E/ l% J7 p+ ]. s, r
  7. 2.0
    " s; B  L# G. j. J1 c
  8. #即10的1.3次方的结果为207 u, Q$ E% P1 v* _
  9. >>> math.log10(20)  z) I1 t7 j" \( S
  10. 1.3010299956639813
复制代码
8 y; x5 o+ L1 P  r
math.log1p(x)  返回x+1的自然对数(基数为e)的值
& ~  F; m! o7 J) H" {: j2 W- j
  1. #返回x+1的自然对数(基数为e)的值
    : A$ ~& P: A% z5 L3 m0 T
  2. log1p(x)! d* u; t' L2 H0 N' `( b( v3 N
  3. Return the natural logarithm of 1+x (base e).
    * C, G% n. N; o3 a+ |! Q5 D% c8 P
  4. The result is computed in a way which is accurate for x near zero.5 E0 c; _, z& t: l
  5. >>> math.log(10)
    ; S8 v# j; B8 }! P+ o
  6. 2.302585092994046
    * I+ r: @% G% |4 ?' d  }
  7. >>> math.log1p(10)5 }1 _6 z# R- q1 n! [; W% D  S
  8. 2.39789527279837077 V7 s3 M  ?2 W0 z/ l
  9. >>> math.log(11)! l2 m" A& ]+ {/ Z3 G' g9 U7 `
  10. 2.3978952727983707
复制代码
. ~$ T* n2 e% s. l
math.log2(x)  返回x的基2对数
# k: }: x4 J( u7 N  B( M
  1. #返回x的基2对数. S. r9 y) W4 n" z; `
  2. log2(x)+ d+ y& A5 B& F& t" v# A
  3. Return the base 2 logarithm of x.6 _) d7 R  d( w) V7 c& q! {
  4. >>> math.log2(32)* D7 p* a6 S6 Y6 f
  5. 5.0
    4 o: O6 f( l8 F7 h, T/ N' K8 ?
  6. >>> math.log2(20)
    0 I  F( ^6 P3 y# D; p* J; q8 }* e
  7. 4.3219280948873631 i! Z: y# _! S9 @
  8. >>> math.log2(16)2 E; n4 w3 r9 o% r
  9. 4.0
复制代码
( H5 t' T# h; p9 \5 U/ U
math.modf(x)  返回由x的小数部分和整数部分组成的元组  D2 F3 k7 F3 D: K- n# k3 q; m
  1. #返回由x的小数部分和整数部分组成的元组8 }8 U7 }# v6 g2 _$ M
  2. modf(x)7 V% E" L; B: ]" x
  3. Return the fractional and integer parts of x.  Both results carry the sign. }: }* ~7 `" T$ V
  4. of x and are floats.7 p9 J" b% M' t
  5. >>> math.modf(math.pi)
    9 d+ W* a+ ~6 p, V. N( s$ d; z3 ?
  6. (0.14159265358979312, 3.0)
    3 V- f9 R3 T/ P% j! b
  7. >>> math.modf(12.34)
    ! V5 p5 b2 e. b# B
  8. (0.33999999999999986, 12.0)
复制代码

' w8 R6 \( \1 i% H) u3 K6 G9 Vmath.sqrt(x)  求x的平方根6 A' j% h8 v: G. Z0 w0 I
  1. #求x的平方根
    $ N' ]- d) Y  v( w3 i7 t7 f7 \
  2. sqrt(x)5 S/ z4 H0 X1 k7 T4 p
  3. Return the square root of x.
    7 Z+ P1 \6 h0 l: n3 a
  4. >>> math.sqrt(100)
    ; A" a4 Y$ y$ h* B( j# s' S
  5. 10.08 O! k* ^! s: U- v  J0 b; K
  6. >>> math.sqrt(16)
      e3 X0 o: o- J& F
  7. 4.0' e! M' P0 S  z8 R
  8. >>> math.sqrt(20)
    4 k5 g5 e' w& R5 ?# V' E
  9. 4.47213595499958
复制代码

  Y7 T# `* j. I7 T+ omath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    0 A4 x0 J3 |8 _' ~/ _
  2. trunc(x:Real) -> Integral
      [6 d8 `! H# A6 ~$ F
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    . M! Y8 g  k! Z
  4. >>> math.trunc(6.789)5 _( |: r: A8 o5 _: q0 ^6 s
  5. 6
    6 H6 v; `% @  ~! n0 A- a# A" J
  6. >>> math.trunc(math.pi)
    8 Q  J8 ?9 Q; x) o
  7. 3/ ?. E- r2 ?& t& I2 Y+ M
  8. >>> math.trunc(2.567)
    ) j+ P+ t4 s& `( @. I- N( e
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-10 06:46 , Processed in 0.080669 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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