新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

$ {# X3 {2 L6 g& T# G9 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
3 B! F2 {" g" F. g, l2 d
4 ]$ Y" g* Y, E! |) W7 @5 k5 q7 Y
方法1
% Q. r4 U3 W) @; E: V
  1. >>> import math  A6 L6 s% Z! ]1 r3 Q
  2. >>> math.sqrt(9)
    ( w/ @! R6 G* h
  3. 3.0
复制代码
方法23 ]! q8 L* [  q1 H
  1. >>> from math import sqrt
    * T4 [% Y/ ^" _( ?/ M
  2. >>> sqrt(9)
    " c3 L/ V! v. l6 Z
  3. 3.0
复制代码

2 o) k& C, j3 ]4 }9 s8 b# D
, S  _+ G! |" ?4 G, ?7 n3 c( q
math.e  表示一个常量: k6 }, R; `6 I: v4 H
  1. #表示一个常量4 g. J* F2 u4 \1 Z) \
  2. >>> math.e1 C( e, I& x( K7 D: S0 p
  3. 2.718281828459045
复制代码

. ?% t( n: J8 a  k+ W3 imath.pi  
数字常量,圆周率

& s! z0 _4 _2 V$ @
  1. #数字常量,圆周率
    2 D! O% T- ]3 W, m2 b) K
  2. >>> print(math.pi)
    5 Z' J0 f- n' _3 [8 b5 I
  3. 3.141592653589793
复制代码
& ?- b3 u- D7 _! ]; u( _0 |
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

  L% [- {! N" }1 ^* d: e: }
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    7 {0 M  _. D; a3 V! K; \
  2. ceil(x)
    * N- `- Y8 b8 U; j+ |8 Y- i
  3. Return the ceiling of x as an int.( W. ^6 I, E1 \9 I2 P3 S
  4. This is the smallest integral value >= x.( U8 H2 ^& h' s% v
  5. ( l$ `/ p* C' G! c
  6. >>> math.ceil(4.01), G9 ?1 J# t+ E
  7. 5
    7 `6 D4 o3 N1 _7 e* t( M5 L+ p; a4 X
  8. >>> math.ceil(4.99)/ |/ Z0 I+ Z0 x7 R; v3 W, c3 w
  9. 5; S5 J; z5 @  S# X/ X2 o+ E6 `" e) }
  10. >>> math.ceil(-3.99)
    & w! s. U$ h2 u# [/ `  b
  11. -3
    ! y  `% n9 T! V( j  j
  12. >>> math.ceil(-3.01)4 V; c. b1 Q' h6 I+ K, G
  13. -3
复制代码
6 B1 f9 Q. E3 F0 b* L' I# T
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身; i" F; Y0 M0 w( D8 n0 {" J& s$ r
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    : a1 L. ^- ~' t) M
  2. floor(x)- D( g5 O7 @6 N, a
  3. Return the floor of x as an int.
    3 C( w5 A& x& H2 [" V$ A* @, z+ N
  4. This is the largest integral value <= x.
      M+ ~. S+ |/ I2 Z% Y% P6 s
  5. >>> math.floor(4.1)1 H: X; ?4 h: B8 `2 `' x
  6. 41 l& {# s& n& s* |
  7. >>> math.floor(4.999)3 f2 }# N+ H6 \4 m& Y
  8. 4, y2 F3 k8 x) f
  9. >>> math.floor(-4.999)) v" D7 R* U  i1 t' s
  10. -5
    ' l' t2 ^4 E( r  S. h* Z+ P) \
  11. >>> math.floor(-4.01)
    ) f: U! j# S2 E9 z' X
  12. -5
复制代码

- T! A/ ?: L+ {  \4 l$ gmath.pow(x,y)  返回x的y次方,即x**y* ^1 n  c6 y- \: _+ Z# p; c
  1. #返回x的y次方,即x**y# r1 j4 F9 y5 s" b2 l
  2. pow(x, y)
    2 D( W# I/ a6 I& E
  3. Return x**y (x to the power of y).! N; l+ `; B$ F
  4. >>> math.pow(3,4)6 A1 `; D, m8 l0 }1 S" F
  5. 81.0( T& D; b5 v' ^% i/ b, |: F& \
  6. >>> ; L6 F6 k7 _" m6 a: v. W
  7. >>> math.pow(2,7)/ b. X" L4 G9 j& {* h# H
  8. 128.0
复制代码
5 _( A4 L2 t9 F4 [
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& p0 Q# [8 D2 N( M
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    - e% \( \5 P& d- R  v
  2. log(x[, base])
    , Z9 p6 [+ |3 {
  3. Return the logarithm of x to the given base.. d, D% e' E* }# E& _/ O
  4. If the base not specified, returns the natural logarithm (base e) of x.
    4 J, N; i9 U5 t. J5 X
  5. >>> math.log(10)
    6 i  g1 ~- p2 G% X" P, i6 C# G
  6. 2.302585092994046
    : V0 W2 C, b) O$ S
  7. >>> math.log(11)
    ! Y3 p, y9 O$ s
  8. 2.39789527279837075 E9 E* d/ M, A' s/ k2 v( g* _
  9. >>> math.log(20)
    . q, g. y5 {) E
  10. 2.995732273553991
复制代码
9 Y, ]: W4 Z/ f$ ~  i4 A0 ], d
math.sin(x)  求x(x为弧度)的正弦值
2 v/ p: W1 z+ Z4 g. F+ t# [# C  \6 t) L
  1. #求x(x为弧度)的正弦值' R0 k; P' W5 f: t4 t  c/ j7 l/ z
  2. sin(x)2 R7 z( L( u/ D; ]6 C4 t
  3. Return the sine of x (measured in radians).6 l* k3 \) v7 E' g1 u) f2 @
  4. >>> math.sin(math.pi/4)
    % ?; m" J8 E8 ~
  5. 0.7071067811865475; C9 h3 F: ]: g: u/ F9 l+ D
  6. >>> math.sin(math.pi/2)
    * c- f7 `% y% n2 t2 D0 U& S7 O
  7. 1.0- r& |- C* N! b: r8 C0 N2 P" x
  8. >>> math.sin(math.pi/3)1 N5 K% J$ `0 R7 s( }+ {
  9. 0.8660254037844386
复制代码
6 h# w# w- y, ]) C6 ?5 ~
math.cos(x)  求x的余弦,x必须是弧度( x5 S% {" P) o; C$ ^  s5 c
  1. #求x的余弦,x必须是弧度
    ! |1 g9 \: w1 `) c4 }% G
  2. cos(x)
    : x$ \, G! R: v7 y7 t8 ~8 ?
  3. Return the cosine of x (measured in radians).
    1 j2 i: w- K( l) e. M5 P) ?
  4. #math.pi/4表示弧度,转换成角度为45度
    6 I% z3 S! [$ t
  5. >>> math.cos(math.pi/4)
    2 w7 ?, ~! V/ W( F5 Y& K
  6. 0.70710678118654769 |/ v( v! q+ l1 o: @" m0 b2 k
  7. math.pi/3表示弧度,转换成角度为60度- f2 R/ N5 T5 p% T$ X& }$ z
  8. >>> math.cos(math.pi/3)' l0 r& k1 l6 J
  9. 0.5000000000000001
    8 i0 L) E5 t% ?/ \: G6 `  {
  10. math.pi/6表示弧度,转换成角度为30度
    - M/ J& |7 Z- `% ]/ j
  11. >>> math.cos(math.pi/6)
    # j1 a  E6 U. O
  12. 0.8660254037844387
复制代码

# Y1 O  f; K# Vmath.tan(x)  返回x(x为弧度)的正切值
  d2 K% u- j! v3 u
  1. #返回x(x为弧度)的正切值
    9 V! C, \% j' T% f! M
  2. tan(x)! g& W! z" w0 T
  3. Return the tangent of x (measured in radians).) T* W! E" v0 r6 r" ]8 w
  4. >>> math.tan(math.pi/4)) P; [3 D5 H5 y4 f( I7 |
  5. 0.9999999999999999
    3 J9 @( q; V/ o
  6. >>> math.tan(math.pi/6)% F& s6 |" A) `
  7. 0.5773502691896257
    ) Z; V( H/ C' }7 Y) _: J
  8. >>> math.tan(math.pi/3); U7 q& W" v; F7 L, ]  L- p
  9. 1.7320508075688767
复制代码
! K; q( z& v4 p" c: W' O0 q0 C: h7 t
math.degrees(x)  把x从弧度转换成角度) s- Q9 S5 g5 m$ M+ g4 i
  1. #把x从弧度转换成角度, j; X! F2 s8 o
  2. degrees(x); Q) e1 i. U# A. Z
  3. Convert angle x from radians to degrees.
    * o1 ?! o  D( Q. l6 K! M- K
  4. ) Q3 E0 o3 g0 f- H" Q2 M  H
  5. >>> math.degrees(math.pi/4)
    + B4 y0 d5 Y* J$ D+ w0 k! c9 N% o' h
  6. 45.0
    ) }! E% \: y' D) [
  7. >>> math.degrees(math.pi)
    . n# y5 L) P7 r, \
  8. 180.04 ^1 U5 g& }# p5 d
  9. >>> math.degrees(math.pi/6)4 E2 ?5 `1 k, T! g/ P% U- `
  10. 29.999999999999996
    3 N% b) b* o6 k8 P& u7 ?
  11. >>> math.degrees(math.pi/3)0 s8 {, t$ p" w2 q$ Y3 b3 J* b- w
  12. 59.99999999999999
复制代码

* o0 f' Z" |9 j1 Q, K1 \' T+ Kmath.radians(x)  把角度x转换成弧度/ Q$ r8 D$ f8 v! O8 i: z
  1. #把角度x转换成弧度
    4 Z" s- S& K4 N. R4 G* N/ w& g
  2. radians(x)
    * q' g5 x. ]( S
  3. Convert angle x from degrees to radians.# |5 ]2 ]' P# ?& T
  4. >>> math.radians(45)
    " y$ B8 Y8 Q3 O2 F- L
  5. 0.7853981633974483( u! F/ i6 ~4 z0 B% S# E7 E
  6. >>> math.radians(60)
    6 P, P" t; @; I! i/ u3 U
  7. 1.0471975511965976
复制代码
. _5 j. \, i8 J3 b
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
8 m/ P, j9 f1 U) u) D- \
  1. #把y的正负号加到x前面,可以使用0
    8 e! V! k( P% p3 R( |
  2. copysign(x, y)1 T9 f! }" u0 Y+ q% t. [
  3. Return a float with the magnitude (absolute value) of x but the sign   Q/ e6 p8 X& g# V( K& {- I/ ^! p
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 5 G3 X; a$ e4 r9 @% ?. H
  5. returns -1.0.
    ! f5 k; n9 Q, r' D5 q3 ~
  6. * T4 R4 m$ K( n% M" K7 ~0 P, o% \
  7. >>> math.copysign(2,3)
    + w8 m, e2 ^' W+ s( k$ M' q, ^
  8. 2.0
    % _% Q1 l) I2 C" `
  9. >>> math.copysign(2,-3)
    1 l0 n; Q8 |) P0 l( u% R# E- F
  10. -2.0$ N) s2 g) Y8 X6 X
  11. >>> math.copysign(3,8)8 n2 H* p! G+ L5 h
  12. 3.0  t" O: b3 X2 v; W, v+ X
  13. >>> math.copysign(3,-8)+ @" d  c7 g  B/ U
  14. -3.0
复制代码

; S  S3 {7 }+ smath.exp(x)  返回math.e,也就是2.71828的x次方% u3 d0 @, X( _) s& }
  1. #返回math.e,也就是2.71828的x次方
    - }0 R  R3 W* C: h# |- e* j2 G3 ?
  2. exp(x)
    / X( g( {# z3 z& g/ I) B- @" G: A/ w
  3. Return e raised to the power of x.
    4 C' U$ n* A" E4 a# R

  4. . z/ h+ S9 V9 |' k
  5. >>> math.exp(1)0 ~, s* Q7 A: Z6 T! N- J7 P
  6. 2.718281828459045
    8 z3 _$ f2 e( Z  M8 |) a/ l
  7. >>> math.exp(2)
    ! j8 N1 I7 J4 W) d
  8. 7.38905609893065& H! a, {0 G+ @/ _! P; h/ D9 W. X7 _9 ^
  9. >>> math.exp(3): y! j" X" C( A! _
  10. 20.085536923187668
复制代码

- U" Y" G& P8 zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
7 h3 ~- k# }- g  B! E
  1. #返回math.e的x(其值为2.71828)次方的值减1
    3 n" Z( p/ v! X% Y! \- l3 f
  2. expm1(x)
    4 m+ U: M  X8 H8 w
  3. Return exp(x)-1.
    : V& Z6 j0 B3 }5 h
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    + x. l# l. \4 I3 n! r) Z

  5. 3 x; w0 z2 H" ?$ y5 H% x# j
  6. >>> math.expm1(1)7 S) q/ }# S& D+ P. k! f$ T, p
  7. 1.718281828459045# G  q8 f8 p8 A! ~3 J0 N
  8. >>> math.expm1(2)( g' a5 x, v/ H0 }. B/ Y: C/ |
  9. 6.38905609893065
    9 ]/ M; Y$ B4 b- L5 r
  10. >>> math.expm1(3)0 a+ Z1 _6 J1 h9 d1 h
  11. 19.085536923187668
复制代码

' g% K% u4 K9 Q5 U$ F- z6 Q/ a5 C. N+ ]math.fabs(x)  返回x的绝对值
' B+ a9 |9 L% w4 ]+ {3 o6 C( f' P
  1. #返回x的绝对值
    / L5 |. |1 E9 T( K2 M4 S. }
  2. fabs(x)& ~9 j4 `# ?  d3 s5 V# }, \
  3. Return the absolute value of the float x.
    # Y  w/ z# ~/ J# o, B  n: r  V2 O
  4. ( G' L3 x7 G# j6 N3 ^3 d
  5. >>> math.fabs(-0.003), }" b5 o% V% L1 ^& P0 ^" y
  6. 0.003
    ) F1 [3 A2 v' _& e: p
  7. >>> math.fabs(-110)
    # c& d  [: B' K" d/ t+ j7 W0 ^
  8. 110.0
    + O! J  t; ~$ w: h4 u8 p
  9. >>> math.fabs(100)
    / f( ?5 h1 x1 `0 Q6 y3 |: Y
  10. 100.0
复制代码

' y+ _0 f# ]! Y' b. |# g' cmath.factorial(x)  取x的阶乘的值
+ h' c' C$ V* Q8 ?' U0 P$ f
  1. #取x的阶乘的值
    ( u+ e9 d3 ]. [* W3 u7 Y
  2. factorial(x) -> Integral% U, i! U: e+ X  Y+ ]
  3. Find x!. Raise a ValueError if x is negative or non-integral.% T( r8 q& m. K/ t6 t/ f
  4. >>> math.factorial(1)
    ' X0 c; O6 v, g. t
  5. 1
    / y5 B& X- }, O' S6 u0 B
  6. >>> math.factorial(2)
    # b, `! l. w4 q6 Q
  7. 2
    $ D6 H* o$ r- {% c$ \9 o7 X
  8. >>> math.factorial(3)1 r' [- c" J5 w% i" J* s+ ~  V
  9. 6
    2 C8 E; O- y$ O+ Y" ?  A# x& c/ ]
  10. >>> math.factorial(5)
    : X- r) l, }% H/ A' k
  11. 120  w( Q3 x: H$ {
  12. >>> math.factorial(10)$ ~4 g( e; H- S/ U  c9 F& n
  13. 3628800
复制代码
$ [: c5 _# {, r+ [  O
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数  I. T# ?8 m/ ~! r
  1. #得到x/y的余数,其值是一个浮点数
    7 D1 d: S2 P, J" p! o+ }
  2. fmod(x, y)
    4 P8 d  s7 p1 q8 s. s2 j; J6 Y
  3. Return fmod(x, y), according to platform C.  x % y may differ.  r. n  \- W! H
  4. >>> math.fmod(20,3)( n! N* V; S" E- X1 A
  5. 2.0
    8 a. ?# t$ t$ \0 h& w
  6. >>> math.fmod(20,7): |% k# H% U; b5 p4 t: u
  7. 6.0
复制代码
& @/ d: ~. L* G: Z$ A' I* ?
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
$ A$ l1 c& w, T; n, d6 Q) t$ M% J3 _
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 W! f/ Z1 ~# {/ W: C$ F/ Q4 T& S
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值& _2 z' q5 B& {8 N) M9 k* D
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1  a2 R( W# I0 h; M( c
  4. frexp(x)1 }( \0 |5 u; `% T2 ]& F
  5. Return the mantissa and exponent of x, as pair (m, e)." Q! \8 x( W1 P# g+ E% G
  6. m is a float and e is an int, such that x = m * 2.**e.! ~$ z3 h3 Q8 ~5 o; F7 w
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.8 K# L; B9 k) l# T. c" P
  8. >>> math.frexp(10)+ Q! z5 z* t2 E. z
  9. (0.625, 4)
    0 z' o9 c9 ^! O7 f9 N% C
  10. >>> math.frexp(75)
    / Z2 f/ x  _$ q/ d( W' i5 Y
  11. (0.5859375, 7)
    + ~) u: z- f% u- D- ]- k( K6 T
  12. >>> math.frexp(-40)
      P* H( L4 [" A& |8 G/ \, s- Y4 ]- z
  13. (-0.625, 6)
    0 p; g! [) ?4 k8 ~$ U" ?: M
  14. >>> math.frexp(-100)
    4 G$ Q1 h; W  g% Z  b0 ~% y
  15. (-0.78125, 7)
    - \. i3 f$ N; M& a8 M' _" o( K
  16. >>> math.frexp(100)
    * S6 y, S7 W# }3 P/ A
  17. (0.78125, 7)
复制代码

) ~/ ^5 n5 q9 H0 `2 t( s& u$ Q0 h* ?math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列- V4 ?7 g- O# T0 R  b3 H  n& N# e
  1. #对迭代器里的每个元素进行求和操作' A0 x1 F( ~# H& ?: W
  2. fsum(iterable)8 ?: A: a- b# q( ~& r! {, o
  3. Return an accurate floating point sum of values in the iterable.
    - B$ n' R! Y' u! y9 S" r
  4. Assumes IEEE-754 floating point arithmetic.; k; x5 q" W  X
  5. >>> math.fsum([1,2,3,4])9 ?& k. d. c; n2 h$ \# A' c
  6. 10.0, k+ w6 E/ a- f5 C% Q1 W# y% H/ Q! C7 h, d
  7. >>> math.fsum((1,2,3,4))) q+ G3 i+ k: v4 @7 f! D
  8. 10.0* O1 m- v6 p6 }' C+ L
  9. >>> math.fsum((-1,-2,-3,-4))% p+ _; n* W: [1 D: D9 d3 n) e; o1 G
  10. -10.0
    6 ?4 J2 z( a6 \4 t+ u: }9 C
  11. >>> math.fsum([-1,-2,-3,-4]), d0 _7 m# s, y, J
  12. -10.0
复制代码

; f9 ]/ Z7 U4 j) H0 M# Zmath.gcd(x,y)  返回x和y的最大公约数
# A' S( m& i; t3 `6 T5 X( d
  1. #返回x和y的最大公约数
    , R: K8 W& J( A* ?. m% W6 g# `8 V
  2. gcd(x, y) -> int( \& O5 U2 m, u6 x' [2 a4 N
  3. greatest common divisor of x and y
    8 P; m1 P9 L% P) |
  4. >>> math.gcd(8,6)
    - W3 U2 _0 m/ A& _/ U, ~
  5. 2
    % J  @/ p; E. R7 E6 _( d8 P8 g0 q
  6. >>> math.gcd(40,20)
    " X0 w0 R$ e! l4 G
  7. 20
    * l; {! w1 _6 ]5 T3 P6 t# E
  8. >>> math.gcd(8,12)
    * M0 T7 L8 m0 t5 `- y+ G. h3 `* |
  9. 4
复制代码
1 Z6 t0 ~5 y$ w0 k
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False& }- \" H6 ?! @/ j) u% M4 S' Z% A
  1. #得到(x**2+y**2),平方的值, F) s; Y& A* m5 Z5 j
  2. hypot(x, y)$ g4 q" \3 @/ b3 ]. S, w
  3. Return the Euclidean distance, sqrt(x*x + y*y).# _! ?1 K. k4 M
  4. >>> math.hypot(3,4)+ F, E( N  e1 o  C7 x
  5. 5.0
    + l# p1 t. K" m: h7 z! N9 g- p
  6. >>> math.hypot(6,8)
      |$ k/ i+ a7 \# M$ ]1 n
  7. 10.0
复制代码

3 ^3 F5 y; [: m3 Amath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False& O, ?' i" d& Y0 B$ ~7 N
  1. #如果x是不是无穷大的数字,则返回True,否则返回False2 V: W, ~! M6 j
  2. isfinite(x) -> bool& e% a0 L/ \& |: A
  3. Return True if x is neither an infinity nor a NaN, and False otherwise./ E! A) l0 `+ V! N1 H: m
  4. >>> math.isfinite(100)3 w4 y; |) g( T1 c* @9 Q
  5. True( k! n" g" |. f( p0 s* O* I0 p
  6. >>> math.isfinite(0)
    # I9 _1 w' Q1 x; K9 {0 P4 m
  7. True$ e& u1 @7 s- \6 k8 \$ x, n% L
  8. >>> math.isfinite(0.1)
    $ u! c, j1 n3 W8 m! }4 a
  9. True
    3 [$ @+ p7 C. T+ c: t2 W
  10. >>> math.isfinite("a")
    # o; u- a8 i1 U) n* O* ~0 m
  11. >>> math.isfinite(0.0001)9 l% A# r$ M  X9 I* h) A
  12. True
复制代码
* d5 |6 E7 b8 Y1 n
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
$ X, `6 E& J. s( `1 d) b
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    / c& i2 H0 I# i4 }: \
  2. isinf(x) -> bool5 \! v# s- j' I
  3. Return True if x is a positive or negative infinity, and False otherwise.9 F( {8 V. X2 m; v9 t7 S
  4. >>> math.isinf(234)1 p* R8 H/ Q1 f: q& [, J/ a
  5. False
      S" U% i, Z# s; N" D# J+ |' {
  6. >>> math.isinf(0.1)
    9 h- T$ Y4 f. r- {( s4 Q
  7. False
复制代码

# t1 G2 T! b5 [, L; m* ^5 smath.isnan(x)  如果x不是数字True,否则返回False3 ?4 O* A& r7 e! p, o5 R* [, m( U6 E
  1. #如果x不是数字True,否则返回False, Y" v" Q0 M! o  M5 q+ G' h1 m( p
  2. isnan(x) -> bool
    " `, X3 r9 d: j* P
  3. Return True if x is a NaN (not a number), and False otherwise.6 A- o& _$ `, E) @
  4. >>> math.isnan(23)
    ( n4 |9 M+ W3 @
  5. False
    ; S: f8 Y+ j/ T+ ]
  6. >>> math.isnan(0.01)9 k  G# J# H6 k% @$ W5 y( V9 F- T0 F
  7. False
复制代码

6 `, K+ E# b3 D8 H) @- }- g- jmath.ldexp(x,i)  返回x*(2**i)的值/ s6 w! W7 \4 o+ W
  1. #返回x*(2**i)的值0 M' @. }4 g; B: _3 k5 z9 U% O
  2. ldexp(x, i)
    / ~/ `' [* J1 E9 U/ W
  3. Return x * (2**i).' [6 @8 K  u+ |/ O4 E7 P% h
  4. >>> math.ldexp(5,5)
    & o# `1 n* Z5 \8 G
  5. 160.0
    2 A3 a+ S: C# ?1 f
  6. >>> math.ldexp(3,5)& \3 o2 V8 E: c8 j& [( M  w
  7. 96.0
复制代码

+ i" D3 D  o; \7 B. D$ p( Z2 E/ `math.log10(x)  返回x的以10为底的对数
: h% B1 l( u& }4 H6 j2 O/ N
  1. #返回x的以10为底的对数: N2 P) v( ^! L& O# x( B
  2. log10(x)" i3 B& i* X3 e  c4 S
  3. Return the base 10 logarithm of x.# f" R$ I. O2 U& X% Y) d
  4. >>> math.log10(10)* i% ]: @! M8 E/ v5 W& B- P
  5. 1.0
    : M1 j% s( Y1 {
  6. >>> math.log10(100)
    ' m8 o2 Y  U/ c
  7. 2.0
    5 Y2 X1 e- }. e! E
  8. #即10的1.3次方的结果为20
    4 i( i/ Q  z) m( a1 j5 i, U
  9. >>> math.log10(20)
    7 c$ Q7 A$ `3 D3 Z- j$ y* O: B
  10. 1.3010299956639813
复制代码
# i; z5 O# s% K5 M
math.log1p(x)  返回x+1的自然对数(基数为e)的值
7 L: L# a. l. j, y" J: L& H  |
  1. #返回x+1的自然对数(基数为e)的值3 Q& {$ R, [+ J0 v" f1 I' _
  2. log1p(x)
    % @# N% X6 l: G! t
  3. Return the natural logarithm of 1+x (base e).- e* B4 E: [: |# ?( ]/ u9 B
  4. The result is computed in a way which is accurate for x near zero.
    * t* Y0 F5 I8 V+ @7 h$ h* u
  5. >>> math.log(10)
    1 e3 M$ V5 [2 ^! I; H  @
  6. 2.302585092994046
    * @1 h0 g6 F. ~4 V
  7. >>> math.log1p(10)
    . v( M. L6 p$ w$ _$ y5 U. b5 h9 P
  8. 2.3978952727983707
    6 i& }8 D% P' @' v$ q2 v# o
  9. >>> math.log(11)
    & D; G- ]1 y, @' X  v; [
  10. 2.3978952727983707
复制代码

: x0 \1 Q+ O+ ]0 R8 x6 |  Fmath.log2(x)  返回x的基2对数  x% X/ M9 [$ ^* V  W
  1. #返回x的基2对数
    2 b. P+ r) @9 s+ X6 v. Q+ z
  2. log2(x)
    ' k) d8 T' y2 W* o
  3. Return the base 2 logarithm of x.
    2 K2 j; J2 O* ~
  4. >>> math.log2(32). v* Z( o! a; u+ y
  5. 5.0& }# S9 l% ]- A
  6. >>> math.log2(20)
    ' z! j- a+ H4 H8 p$ Q: D
  7. 4.321928094887363
    + Z3 \. |9 p- `# k6 o: b. U
  8. >>> math.log2(16)
    ( r4 e7 q: F: ~* h& A2 m
  9. 4.0
复制代码
4 P1 j" u( z" S, i
math.modf(x)  返回由x的小数部分和整数部分组成的元组2 E( ^" m4 g9 y5 U2 _. z
  1. #返回由x的小数部分和整数部分组成的元组; K, M7 e: x! G$ w2 U
  2. modf(x)! E8 X6 m2 b5 z( g, K3 B
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ' `- Q! i! g" V' v* u
  4. of x and are floats.2 X, b0 i# {8 m
  5. >>> math.modf(math.pi)( m7 j& t1 p) x2 Y
  6. (0.14159265358979312, 3.0)
    ) v4 k  L  C9 q# q& b" f1 _% Q
  7. >>> math.modf(12.34)4 ^- x4 Q6 u$ r4 `
  8. (0.33999999999999986, 12.0)
复制代码

2 ~+ B$ O" n: E8 i% gmath.sqrt(x)  求x的平方根
5 \4 C# o" `7 q. t0 Y$ t6 [
  1. #求x的平方根* i! d5 l  V+ y- V( Y: D
  2. sqrt(x)3 K; H" {+ ~8 @$ C
  3. Return the square root of x.
    2 a/ l7 k& s6 r; T
  4. >>> math.sqrt(100)5 w' M1 @) c$ Q- ]. R, D( }
  5. 10.02 e$ f2 `" P) u
  6. >>> math.sqrt(16)5 ^" |# u4 c) X$ P+ ]# s' z
  7. 4.0  H( o; H7 Z# O4 w  T
  8. >>> math.sqrt(20)# \9 L( [3 x* Q" U" f8 n- P
  9. 4.47213595499958
复制代码
7 c+ j: U9 W$ G) a6 \; `+ W
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    0 j5 u4 r$ ^8 @: L. c# c
  2. trunc(x:Real) -> Integral/ n3 {' X. D, P9 @: |
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 k% _/ ^4 b5 a. s2 r( Z$ O( X
  4. >>> math.trunc(6.789)
    8 e5 c( z& }# E# n% ^/ `& s
  5. 6
    ) j! M2 o9 Z+ Q. G
  6. >>> math.trunc(math.pi)8 K+ \% [% h' z/ e
  7. 3
    4 n& E# c5 N, [& o+ i& N
  8. >>> math.trunc(2.567)
    - G2 M* F0 M2 i4 e7 g
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-7 18:47 , Processed in 0.083001 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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