新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

8 _5 J3 g" n. S! m% w2 u5 y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
) f3 ~- i+ p3 y, d" k

3 W( W$ q  D4 J% e方法1
& b, d! ?2 s- i3 z3 A( Z
  1. >>> import math0 ?$ j  }" p) Z" z
  2. >>> math.sqrt(9)
    3 z" X4 s8 b0 M" x8 [3 l
  3. 3.0
复制代码
方法2
* \6 X4 M% L, A% Y, ~
  1. >>> from math import sqrt
    0 N7 @# W  t8 P) x# F7 V
  2. >>> sqrt(9)4 p& A/ Z6 Q' ^8 g- N
  3. 3.0
复制代码

- x2 u" J' o' c- u& |0 k
! h/ z: T2 K% P7 |
math.e  表示一个常量
! w* G5 |( g  s5 E3 e
  1. #表示一个常量
    / X! j' p5 A6 n1 W5 W' i% T
  2. >>> math.e5 X6 J- n1 q" A( \2 e$ t' {
  3. 2.718281828459045
复制代码

) @8 I. O) Q3 X3 l9 G" X' d, B- r, l/ jmath.pi  
数字常量,圆周率
# \! O" Y8 U- [) c+ H3 k
  1. #数字常量,圆周率
    0 @7 r# Z! Q) N8 N$ M3 Q- M' `. i
  2. >>> print(math.pi)! K6 f: D8 ~' q- \4 o
  3. 3.141592653589793
复制代码

0 ^5 Y0 Z  t' Imath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
- ^& Z( E# p2 b0 C" k+ ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x! D" w4 U8 b  C
  2. ceil(x)
    8 P1 O. x8 G/ l3 o, z: c
  3. Return the ceiling of x as an int.( K, I% i3 a& G
  4. This is the smallest integral value >= x.
    0 y; I0 r5 E% G

  5. + ~- P0 Q1 t& u
  6. >>> math.ceil(4.01)
    . W% c5 C, D( J4 Q& a. ~- H
  7. 5$ f. z' U% B% u" V
  8. >>> math.ceil(4.99)( `7 |# C2 d' _2 f7 A
  9. 5
    ! y, l2 t9 m/ y5 D5 h
  10. >>> math.ceil(-3.99)
    $ {) k, m' v1 a4 N/ P# p
  11. -3
    $ D& ~" S) k/ ]
  12. >>> math.ceil(-3.01)
    & o4 |1 `0 @* A
  13. -3
复制代码
. M9 Z6 r+ l  H- l, Y- d: w8 i; u
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 u% \  \7 _* a3 H: S9 a/ @' Q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    6 R8 d" f; ~' ?( i+ N3 x
  2. floor(x)& }" e( {% q6 p. v1 Z' L5 v
  3. Return the floor of x as an int.( L! h, v$ o; {' ~" s
  4. This is the largest integral value <= x.
    - r* N+ i8 `2 G) t9 t$ S$ K
  5. >>> math.floor(4.1)
    7 e$ O% }5 u$ {# Y; ]
  6. 47 `7 M  @/ D! Q
  7. >>> math.floor(4.999)
    / `2 v/ u' j$ {
  8. 4$ y- x7 \+ z: m8 v) i  w
  9. >>> math.floor(-4.999)
    4 u; M- U# E# }: ^9 W4 M4 j
  10. -5# {1 @! y0 |0 B+ w8 }2 G
  11. >>> math.floor(-4.01)
    ) G9 L; F3 d; @0 V# K
  12. -5
复制代码

0 F  `2 z% l; q, emath.pow(x,y)  返回x的y次方,即x**y+ ?# W7 D/ B. ~
  1. #返回x的y次方,即x**y
    / g/ N5 S# q/ c! M% K) R2 H
  2. pow(x, y)
    ; z. T$ b+ K. X9 U
  3. Return x**y (x to the power of y).
    $ G! m+ n/ D0 ?% T7 l) e; j
  4. >>> math.pow(3,4)% m0 Q+ y" G( E' {) E' S' v7 O$ Q
  5. 81.0
    % A$ _& D; Q$ R) I# |  j
  6. >>>
    - T' ^- ~- i3 j5 j: @0 }. Q
  7. >>> math.pow(2,7)
    - p% I8 P9 X2 r$ G, `
  8. 128.0
复制代码

* l  p4 J. s* r7 amath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)- J* N! g6 f( @
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ) n* Y- E7 o6 m0 c* o
  2. log(x[, base])( p4 R2 ?; `3 P
  3. Return the logarithm of x to the given base.4 h& Y% ?+ o7 c5 r* D# X! c4 l- d
  4. If the base not specified, returns the natural logarithm (base e) of x.
      a9 p/ T. ^/ _* v$ |+ J
  5. >>> math.log(10)4 U7 l) I" M1 F% _
  6. 2.302585092994046# g7 n/ D$ [0 ~; B, C) b; h# ^
  7. >>> math.log(11)1 p5 ]; J9 T# p* J' q. H# g" _
  8. 2.3978952727983707
    * i/ {% b$ e  z( M5 a  i( K0 e) v
  9. >>> math.log(20)
    1 z' \7 C8 a* i
  10. 2.995732273553991
复制代码
+ v$ {; x7 A  H( E
math.sin(x)  求x(x为弧度)的正弦值* A( k7 |8 [/ f( W8 i5 Z4 s
  1. #求x(x为弧度)的正弦值
    ( R9 m$ h& ^! t. k) C7 Q
  2. sin(x)
    ; a% T. X4 L) n% J, G% J
  3. Return the sine of x (measured in radians).3 o7 M* Z+ F6 Y- ^
  4. >>> math.sin(math.pi/4)
    # o9 x! P( u- Z/ x- R- i; F
  5. 0.7071067811865475
    0 ]' _9 ]9 \: V- \5 A% k$ e
  6. >>> math.sin(math.pi/2)
    ' f" A0 `" p% N' a  K
  7. 1.0$ K" Y& S- l: W) |+ [; A# M
  8. >>> math.sin(math.pi/3)
    7 |( t, F+ m! I
  9. 0.8660254037844386
复制代码

# Q% Z0 P. o9 Z4 Q, }  ~$ Emath.cos(x)  求x的余弦,x必须是弧度- j: i! H3 S) d9 [7 V
  1. #求x的余弦,x必须是弧度, {, E( x3 H6 N) L
  2. cos(x)
    7 w6 f' P' U; f0 q  s* X9 s
  3. Return the cosine of x (measured in radians).
    1 U5 J1 ^. l9 w  S. ~
  4. #math.pi/4表示弧度,转换成角度为45度
    " g/ F6 A. a- {- I$ r6 h
  5. >>> math.cos(math.pi/4)  X8 U+ v5 b- U9 j) R# Z
  6. 0.7071067811865476: N; ]1 ^8 J* l" X8 ~' q; g0 t
  7. math.pi/3表示弧度,转换成角度为60度$ x* C6 c- k: H, u3 y) }, v) c
  8. >>> math.cos(math.pi/3), W2 P* u2 {; v6 m# }
  9. 0.5000000000000001$ e1 v( c* s, C2 L* {' F7 j0 z
  10. math.pi/6表示弧度,转换成角度为30度
    5 N) e. \# N1 N
  11. >>> math.cos(math.pi/6)
      F3 X2 v& M$ f* V! N' b+ K
  12. 0.8660254037844387
复制代码
/ u, N& ]) U9 g9 `
math.tan(x)  返回x(x为弧度)的正切值  g. a, Y" ?1 i3 H# N
  1. #返回x(x为弧度)的正切值
    ( C7 U3 \' }6 W$ N! m8 f( M! }/ u
  2. tan(x)" g6 A% T# A+ ^, G9 \+ ?
  3. Return the tangent of x (measured in radians).+ u. ~, G' x/ @& b
  4. >>> math.tan(math.pi/4)
    9 S: v; }, S3 y; w. ?) U
  5. 0.99999999999999991 Y( R' x* c2 m! h, _; p  \
  6. >>> math.tan(math.pi/6)0 j5 b: N1 ^3 m# @1 H% b, X; V
  7. 0.5773502691896257
    * s" Z  y8 O! v8 `# T1 q
  8. >>> math.tan(math.pi/3)
    ( ]6 @& {* @$ b
  9. 1.7320508075688767
复制代码
2 @! z. A2 E, r0 ~2 ]' X3 U: E
math.degrees(x)  把x从弧度转换成角度6 A- w; \5 {3 o2 N1 q5 f
  1. #把x从弧度转换成角度6 n: V3 Z6 ]  Z6 m  p9 y2 D/ y
  2. degrees(x)) f3 _% \4 R/ o  U: g
  3. Convert angle x from radians to degrees.
    3 A4 O. Q/ D  i9 u. J& E9 ~' n1 T

  4. 7 t. E0 ]6 I+ v9 ^8 J: k  [" {; U
  5. >>> math.degrees(math.pi/4)
    * z9 i9 f8 e1 t8 |; E
  6. 45.0
    0 B) O) Y# \5 i. P5 C
  7. >>> math.degrees(math.pi)
    1 u/ q( b# v+ Y
  8. 180.0. K# y- K% C" h  Y
  9. >>> math.degrees(math.pi/6)
    " z" |+ o( F2 O. d
  10. 29.999999999999996
    * O; Y! x6 y8 o9 M
  11. >>> math.degrees(math.pi/3)5 ]7 @7 c" c$ j& a( F0 y/ y6 g
  12. 59.99999999999999
复制代码

1 a+ s5 M5 w' M2 Wmath.radians(x)  把角度x转换成弧度
* B) f7 i) @+ o6 B
  1. #把角度x转换成弧度. @& W, Y5 n( \+ @% b0 d
  2. radians(x)& T% v& {; }4 u/ t$ p) P
  3. Convert angle x from degrees to radians.
    $ Y. \* ?- w: l6 r. c; k- c
  4. >>> math.radians(45)$ T/ P$ M( l- c! [. \/ E9 I
  5. 0.7853981633974483
    7 g- D) h& i! N2 f6 Z4 C4 s
  6. >>> math.radians(60)
      o1 }% G% N  d! Z/ N  ~; V
  7. 1.0471975511965976
复制代码
$ `! T( Q* D, B; i3 }' |' D" t
math.copysign(x,y)  把y的正负号加到x前面,可以使用00 ]) \; K5 O) }
  1. #把y的正负号加到x前面,可以使用01 Q& ^! }/ \0 E' \8 ^( J
  2. copysign(x, y)
    3 n! _. v# U# K, m9 T3 v5 Y
  3. Return a float with the magnitude (absolute value) of x but the sign
    2 L9 h: I6 b" j, r2 `
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 h) C3 @6 n; W5 l, A( l3 ?/ a
  5. returns -1.0.. i* d  {3 |& p
  6. # i$ {) ?, u/ p4 v- k/ E3 T( |5 c3 \
  7. >>> math.copysign(2,3)
    # ?) u- B' t* V3 P* b
  8. 2.0
    : q# [( y5 d5 {' |
  9. >>> math.copysign(2,-3)
    & Q; x* V0 Z" l5 y8 p! `
  10. -2.05 F+ r8 p) x; {5 l
  11. >>> math.copysign(3,8)
    6 ]9 \1 W0 h% k3 m) B: B/ ~$ |: X
  12. 3.0/ L# J4 B( Y+ P1 m& d& c
  13. >>> math.copysign(3,-8)0 P! z5 v" z' _/ L% X: ]/ _8 m: Z# k
  14. -3.0
复制代码
: f  k; j2 c! W: `3 o8 S8 Z6 k, b
math.exp(x)  返回math.e,也就是2.71828的x次方- B6 c) m3 O. g% H+ V0 A
  1. #返回math.e,也就是2.71828的x次方7 C3 U5 C2 i/ N1 w9 }( `% e
  2. exp(x)0 i: N9 y' J( D* W4 g
  3. Return e raised to the power of x.6 ]! ?, d' j7 ~: h+ M
  4. 5 Y3 i; h1 {- a! K
  5. >>> math.exp(1)8 r7 A) d( n# X9 `
  6. 2.718281828459045
    0 A. j9 B+ `9 W* K$ C  s) e. H
  7. >>> math.exp(2)
    9 Z0 b* t, r( r% J( E! X
  8. 7.38905609893065* a4 B' L( v1 V: @
  9. >>> math.exp(3); k* a* N3 ~5 V& e8 {( Q4 Z1 W5 A* }
  10. 20.085536923187668
复制代码

% S1 R7 T) g( Y- Kmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
( m* c4 P+ i3 k- S
  1. #返回math.e的x(其值为2.71828)次方的值减1  s4 Q' i/ w1 r8 {7 v
  2. expm1(x)# [% K2 ^: t" T4 l
  3. Return exp(x)-1.
    1 ]6 @5 L8 g, t
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 a9 r+ O1 `. |! ]) f+ G

  5. 9 s/ }: \# T# h3 z6 h
  6. >>> math.expm1(1)
    3 v6 w; Z' g) M1 G; g" E* H
  7. 1.718281828459045- h$ b9 h* K( j# k) B  @
  8. >>> math.expm1(2)
    ; G/ E: ^$ b  ~2 Y' ?
  9. 6.38905609893065
    1 n3 `+ y7 o; x: t% I* A; v5 c
  10. >>> math.expm1(3)2 z- }" A% b- H7 N0 {8 e" V$ ^
  11. 19.085536923187668
复制代码
( }" S7 `% Y7 B0 j) Q
math.fabs(x)  返回x的绝对值
& i1 @7 r" R3 e7 w
  1. #返回x的绝对值! Q* @5 n+ k. X- ?8 o7 n
  2. fabs(x)
    6 g& W1 n! c7 Q% I; P- |* t
  3. Return the absolute value of the float x.
    3 S, O8 \3 ]) j* c6 f

  4. , \/ K+ t, z& E+ K
  5. >>> math.fabs(-0.003)& G" R  R4 q; _0 k3 ]6 Y
  6. 0.003
    / ]+ N  P* k1 v8 \3 t
  7. >>> math.fabs(-110)
    : }) m3 w- N4 v; [) v9 |8 o7 H
  8. 110.0  o: Y' m. d( x
  9. >>> math.fabs(100)
    , S1 @( T3 |$ ^" t( S& w1 _' Q
  10. 100.0
复制代码
9 N& V) @0 h8 W/ y+ ~
math.factorial(x)  取x的阶乘的值
4 C) ?. R- n; J2 F5 m8 M. Y
  1. #取x的阶乘的值
    / D  j1 ]6 a$ \& e& g# ^! u+ }
  2. factorial(x) -> Integral+ d! z; t: P9 J* o/ |8 H) i( W
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    , w+ A# g: M0 W, x. y
  4. >>> math.factorial(1)
    5 k. \. P- X( E0 p+ J, q
  5. 1
    9 [* }+ O8 |+ @
  6. >>> math.factorial(2)$ _+ ]: {: _: e0 B' @! e
  7. 2
    * m3 Z0 b3 d/ i4 m! D
  8. >>> math.factorial(3)4 z5 r9 ?6 A+ r) @
  9. 6
    ' ]4 X, \+ e5 b% O" E1 `* @
  10. >>> math.factorial(5)
    " b: ^, G6 L( U' M# N( A  D
  11. 120
    & `8 @' G5 ~: R3 G/ Z; F2 C6 e
  12. >>> math.factorial(10)- G+ k( U/ w9 ?1 O5 l
  13. 3628800
复制代码
) d8 q7 M4 E9 J6 [) i8 G
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
" ~' z- o  b8 o8 U4 c) z
  1. #得到x/y的余数,其值是一个浮点数, i( N: o) G7 S, @9 Z9 W9 w! j
  2. fmod(x, y)8 D" T: {" {6 V  Z
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    2 Y: ?  x- f( j. \0 M
  4. >>> math.fmod(20,3)
    0 x$ }# i+ T: a6 C
  5. 2.0( G8 ^# }( N! f9 l
  6. >>> math.fmod(20,7)
    ( |$ q9 q$ W" o; `% t1 j9 i4 y$ L0 t1 E
  7. 6.0
复制代码

0 d8 F: N3 a/ H( Umath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 A: T4 \, ]7 v$ R5 W# V! w! N
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    * `8 h: N) {7 X: Z' Q
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
      A! Y+ u8 _# v6 H1 m
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 d  ?; h* O1 ^5 G$ M% R" a7 O0 n
  4. frexp(x)
    1 z( ?) r8 X0 k; }2 V) g8 t
  5. Return the mantissa and exponent of x, as pair (m, e).
    , ]. c* N, l9 S) h) y" g; g
  6. m is a float and e is an int, such that x = m * 2.**e.$ ]5 D) _- M9 z: v) g- C
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    " E$ i% B6 z8 z
  8. >>> math.frexp(10)% d  \" ]8 L; m) o3 _% k4 R
  9. (0.625, 4)
    2 r% c+ w; ^* k/ S" N$ X; f
  10. >>> math.frexp(75)
    4 V: a+ _+ {- w1 `
  11. (0.5859375, 7)0 ?  _' p) h+ q( Y( S* ~* X! A9 ]
  12. >>> math.frexp(-40)
    ' M' ~; W) m0 x8 w! d3 t
  13. (-0.625, 6); Y9 _' S  f' M2 E
  14. >>> math.frexp(-100)$ e; L; o% T) a& u
  15. (-0.78125, 7)
    ; k# {2 B/ d; w0 {
  16. >>> math.frexp(100)
    ; D4 F# _; h" d- @
  17. (0.78125, 7)
复制代码

3 e# n' b5 o) _8 \math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
; r/ }: r: F) _- }! l6 ]
  1. #对迭代器里的每个元素进行求和操作: @2 ^; L7 r; t2 \/ R
  2. fsum(iterable)
    + ?) p6 N% ~2 ?( `2 m+ P
  3. Return an accurate floating point sum of values in the iterable.
    ( P. k, u- l; J: ?2 |$ U( S6 r8 L
  4. Assumes IEEE-754 floating point arithmetic." A2 |; Q+ c; @: r" N7 H: _. j
  5. >>> math.fsum([1,2,3,4])1 |. j+ b& G+ j! |5 `. Z
  6. 10.0
    5 B: Z; p/ A: L, T
  7. >>> math.fsum((1,2,3,4))6 X  I/ w/ G- M! L$ B6 {9 w7 {) z; y) m) d
  8. 10.0
    9 X& q0 c8 Y* P4 g: G3 N4 g+ d) _1 g
  9. >>> math.fsum((-1,-2,-3,-4))" o, [+ \1 f/ W* S
  10. -10.03 f/ n8 B( n# e( |
  11. >>> math.fsum([-1,-2,-3,-4])
    1 S' D- ^9 p; |6 W8 t: h
  12. -10.0
复制代码

* `, x0 }0 E* s& smath.gcd(x,y)  返回x和y的最大公约数
9 B, O0 k; f# F" G- }( i
  1. #返回x和y的最大公约数
    ! N$ B4 b' Z3 b1 _* \
  2. gcd(x, y) -> int# ~  u; O* U8 h; \& a. c" O
  3. greatest common divisor of x and y7 }, [, h$ b- O  N) [8 R8 m; [
  4. >>> math.gcd(8,6)$ ~/ N. n! ?7 _4 A. u
  5. 2) I# b9 P- [- |9 |6 H
  6. >>> math.gcd(40,20)! q1 q! F3 ~0 |( n
  7. 20
    6 M1 l6 G/ N7 k6 I& p. a- v
  8. >>> math.gcd(8,12)
    , N+ R: Y% x- Z# Y1 K3 B8 t3 u
  9. 4
复制代码

2 p/ c) L: X, k; [& r* Kmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False. U3 B5 U# h$ R0 t( e. s/ y8 ^
  1. #得到(x**2+y**2),平方的值6 F7 ^. f4 k8 W7 C
  2. hypot(x, y)
      \4 J" c  w% u/ ^* m
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    & V8 W9 m: T" a- t! |1 V
  4. >>> math.hypot(3,4)
    8 w$ L( r* Q6 d- p! ]
  5. 5.06 J2 Z. U8 G0 C" A6 v; ]& _
  6. >>> math.hypot(6,8)
    7 A+ ^. b/ E( J6 \# X+ }  Q  R3 Q
  7. 10.0
复制代码
* W. D9 `- x/ Y" Y# f8 {7 o' S% R! h
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False: z& v& K1 Z' }
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    . T" u  p4 ^1 g/ e& E; Z
  2. isfinite(x) -> bool6 l& f) Q  n9 \- B; }
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.% N# ?% T0 ]1 K/ Y' A# P
  4. >>> math.isfinite(100)
    # m% D& @9 e3 h/ K3 C9 X
  5. True
    ' a/ j: \2 a  e" [3 W2 N0 R+ u
  6. >>> math.isfinite(0)% b, _7 u# R5 ]+ I7 B
  7. True
    * f/ t/ Q! @# S- e# d3 ^! Y6 N
  8. >>> math.isfinite(0.1)5 R( d' y# [1 l# m3 F! K6 X5 o
  9. True
    . }7 z0 q# O+ i1 x1 E
  10. >>> math.isfinite("a")
    , e  g3 Y: r/ ?1 M) q& C3 ^  Z% y
  11. >>> math.isfinite(0.0001)
    3 j8 T2 W# \+ B
  12. True
复制代码
2 S  s0 w5 {. v) m( Z) w
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False0 S$ d. w3 @! y& a5 a
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    " [& y: Y$ z! F- C' l" o9 D1 n. r
  2. isinf(x) -> bool% @  i; i/ p5 y4 W7 o9 s; s
  3. Return True if x is a positive or negative infinity, and False otherwise.
    : K6 D& j$ i4 Q8 }- p/ p: r
  4. >>> math.isinf(234)3 H5 {# x) h- @6 W7 U2 f% [8 a2 i
  5. False
    ) V2 r' h: l0 w! v: \4 N
  6. >>> math.isinf(0.1)
    " m! \% n7 v9 p2 B$ W2 @5 l
  7. False
复制代码
! L  }' v1 h- s* Q) d9 e$ K
math.isnan(x)  如果x不是数字True,否则返回False
4 {4 g6 D* Y+ q5 U2 ~
  1. #如果x不是数字True,否则返回False; P6 f. ]3 D) U  O6 i
  2. isnan(x) -> bool5 f+ R' W, u2 F6 U+ v2 B
  3. Return True if x is a NaN (not a number), and False otherwise.
    + q# R( q1 ^6 o6 x- h
  4. >>> math.isnan(23); i- b% [5 m* x8 A' t/ f
  5. False4 s! O; q% o% M2 P
  6. >>> math.isnan(0.01)
    $ r( f& e% i" ]0 t  ]9 R5 ]
  7. False
复制代码

4 C# _4 u: u# `# V; y& ~math.ldexp(x,i)  返回x*(2**i)的值/ J, P, K: |8 d, }+ y! |
  1. #返回x*(2**i)的值
    7 q, `& `9 ^  b& `' j% X1 r# S& D  i
  2. ldexp(x, i)
    % c* h6 K1 H$ {- N3 f2 I8 B' ?0 U& x
  3. Return x * (2**i).# E5 g5 `- X% V  p6 B  x' }
  4. >>> math.ldexp(5,5)1 M5 G6 D- U5 |+ {  b0 Q7 N
  5. 160.0: k" f/ i8 ~0 o1 M/ o+ G
  6. >>> math.ldexp(3,5)
    : {' f3 A6 e1 O$ ?# o7 W
  7. 96.0
复制代码

: r/ M, R5 s0 ^* i& ^( g* Pmath.log10(x)  返回x的以10为底的对数
7 {2 P5 A9 T  C
  1. #返回x的以10为底的对数
    : V7 b8 B( o' t- Q( ~; F* X3 q
  2. log10(x)
    - |1 N0 i6 c% H
  3. Return the base 10 logarithm of x., c+ Y! T7 U  _% P  s1 q/ ]8 p
  4. >>> math.log10(10)8 L7 ]& r% B- k% a& {8 d& D0 _' B
  5. 1.0
    8 p0 Y! Q- ?. F5 d+ {, ~
  6. >>> math.log10(100)+ H% ^+ X" {# ]/ T3 u6 j, u; L
  7. 2.0  c2 L* c* h0 h+ S* |: C
  8. #即10的1.3次方的结果为20
    - F  O7 w! X- V+ N, u
  9. >>> math.log10(20)
    8 g6 N0 ^9 ]/ w0 \7 k2 v- h
  10. 1.3010299956639813
复制代码

) T3 h  I- z8 U, k- G9 L) y  Hmath.log1p(x)  返回x+1的自然对数(基数为e)的值9 g% i% X" }8 u2 c
  1. #返回x+1的自然对数(基数为e)的值1 y) ^4 n: R9 X7 [$ w( `
  2. log1p(x)/ r* W2 q. q* ?/ M; a
  3. Return the natural logarithm of 1+x (base e).9 b" L6 G; K% q1 n) i" E/ }
  4. The result is computed in a way which is accurate for x near zero.4 j/ F$ r9 S/ ^& f5 q; ?3 }" s) ?2 p+ z
  5. >>> math.log(10)
    . H; g. K' J/ W/ Y
  6. 2.302585092994046- z2 p- X" k8 s( F
  7. >>> math.log1p(10)* x1 F, H% D0 W( j& y6 n8 u
  8. 2.3978952727983707
    " d  B. X8 g1 z6 M9 Z+ E
  9. >>> math.log(11)
    3 U: u& m# D* a, q9 A
  10. 2.3978952727983707
复制代码
2 m* C/ Y6 U  j. Q. m
math.log2(x)  返回x的基2对数
3 A( j2 I  n8 V7 I- W6 x
  1. #返回x的基2对数
    3 O4 Z0 \9 A  ~! q4 C- p
  2. log2(x)
    . I" t- P8 C+ L* B
  3. Return the base 2 logarithm of x.9 y: a: ^8 M0 o+ j% J) s
  4. >>> math.log2(32)
    % i. t5 e! U# L$ ^  T( s
  5. 5.03 r% d5 ], {3 q1 u( S% E
  6. >>> math.log2(20)% N$ y& [1 O! ~% B. q) w
  7. 4.321928094887363; t( j" F) F, Z; Z0 v
  8. >>> math.log2(16), \# O1 r  M$ `3 f
  9. 4.0
复制代码
3 F. h/ f8 X/ T" D# H8 v. y9 I
math.modf(x)  返回由x的小数部分和整数部分组成的元组2 @: B1 y# B% F) }( ]% v5 `; K7 C! N
  1. #返回由x的小数部分和整数部分组成的元组
    , V/ J/ f- _9 o/ t, v
  2. modf(x)
    & {* B$ m" \/ A: T$ Y
  3. Return the fractional and integer parts of x.  Both results carry the sign) {4 l" i6 p% Q: r
  4. of x and are floats.
    1 T& P" I: E' @8 w* }
  5. >>> math.modf(math.pi)
    # ^# Z6 [# K4 O2 a! E
  6. (0.14159265358979312, 3.0). r  _+ j* n: ~! E& `# ~
  7. >>> math.modf(12.34)
    " ?, z" t2 A0 m0 u. o) M+ S
  8. (0.33999999999999986, 12.0)
复制代码
2 |. b/ j5 v" _
math.sqrt(x)  求x的平方根& c- F, {7 l* c) G% U+ J/ S
  1. #求x的平方根/ S8 W# k) X8 L3 P; F4 {
  2. sqrt(x)
    ' J% n! ?) H+ y, o7 f
  3. Return the square root of x.
    7 o' v  w- k5 H2 s0 o, Z3 z# E5 [0 s
  4. >>> math.sqrt(100)2 z7 n/ v6 A5 I& q  V4 d
  5. 10.0
    8 z8 d" k6 j& ]& F3 z9 w
  6. >>> math.sqrt(16)
    * m# p6 i1 Q. _2 A9 N
  7. 4.0
    : E  Q* \7 C" O1 L
  8. >>> math.sqrt(20)
    8 Y6 ~. c3 G9 d2 T8 b
  9. 4.47213595499958
复制代码
! q( y5 U4 J8 S- O8 E: g
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分4 p6 {  `( K# K' h) f# P
  2. trunc(x:Real) -> Integral! E5 ~0 b) g! G9 C; u7 P# e, z
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
      s3 ]$ Y3 t/ U+ h7 ?& D3 O; H
  4. >>> math.trunc(6.789)+ Y8 L0 C) S+ ?
  5. 6
    : [; q% X8 v; {2 n& l# `: \4 ?
  6. >>> math.trunc(math.pi)" s% u8 C4 g. o3 }
  7. 3# h+ u4 |" l6 u2 R
  8. >>> math.trunc(2.567)1 g' z2 f2 V4 @6 y- B
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-8 14:04 , Processed in 0.096455 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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