新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

) S7 X! G& L2 f- g# j) `6 z" I# \【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! A, o* }% ]' o( P5 \
0 b8 ?  v: g2 D  D7 {9 w
方法11 _$ w) j  ^% ^/ J( w
  1. >>> import math8 Z! m9 A" X' Y" Z: W( ]! |1 B4 m% V
  2. >>> math.sqrt(9)
    6 S5 p' z" c1 s
  3. 3.0
复制代码
方法2$ Y- m/ H7 q! A5 N3 E. q+ X$ p
  1. >>> from math import sqrt
    4 s1 u' |) l' [8 K
  2. >>> sqrt(9)3 f7 @( h' o$ B
  3. 3.0
复制代码
( F, Y; g  p. w9 I* e/ I* s

! |1 A$ {- o- k, z5 X3 x
math.e  表示一个常量
8 u' W" Q5 f( i# z. d5 `- |
  1. #表示一个常量
    ; T4 d) y. m: w6 j- Y1 v# G, B
  2. >>> math.e
    ' C. m5 R* k& j% N( A9 ~
  3. 2.718281828459045
复制代码
2 h/ Y; g3 ?' G
math.pi  
数字常量,圆周率
1 j6 w$ A9 C/ {' m+ O' Y6 j) a: ]! v
  1. #数字常量,圆周率
    . j. i' L2 q# C5 Y' L0 y
  2. >>> print(math.pi)) G$ ?9 D% ]' R. e( K0 v; I4 n
  3. 3.141592653589793
复制代码
$ U. y1 b$ t. B$ O# Z- H4 w
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

# H* a5 A5 B7 |9 c. h
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ( }8 N9 g7 H0 }; e, ]; y* L% s
  2. ceil(x)
    . N. h0 h1 l) J0 \
  3. Return the ceiling of x as an int.
    9 V4 R( O, }0 q3 ~2 y8 `: E" z* }, i, ~
  4. This is the smallest integral value >= x., S% F; n( F$ g( U1 `# m( |

  5. 0 b$ C* B& F0 R" F  t2 `' q" ^
  6. >>> math.ceil(4.01)2 N. R3 m$ \# K: x! c/ {5 c
  7. 54 N& L6 y5 C  s1 ?' d, K* B
  8. >>> math.ceil(4.99), E" A4 k: N# @4 s' k
  9. 5
    , p. ^. ?2 L; U1 R1 V; D6 b3 l
  10. >>> math.ceil(-3.99)
    4 F+ `5 [6 P1 H+ U0 X
  11. -3
    + e. e1 ?4 `# A) I' j
  12. >>> math.ceil(-3.01)
    5 {" t  v, R( R7 }) l
  13. -3
复制代码
5 s" ^3 Y# _2 S7 @! ]# ^5 A1 W
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身# y. e% X2 T) N
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      `8 F+ O* H9 G$ E( r1 Z
  2. floor(x)
    7 w. \5 {. Y& q9 j) E6 W- A* a
  3. Return the floor of x as an int.3 E  ~8 k* _* l
  4. This is the largest integral value <= x.& x' @% C' Z5 {9 ]/ i
  5. >>> math.floor(4.1)- |* H6 W$ \, h6 }3 n
  6. 4( X" k' h7 H0 o
  7. >>> math.floor(4.999)
    # x) C8 q/ {8 R9 |# ?8 z' t* B
  8. 4% F  j$ S# |* V2 m; M* m3 L1 S0 u+ h. x
  9. >>> math.floor(-4.999)4 d: X8 r! U) I3 v3 E+ M6 g4 e
  10. -5+ M8 W( A$ l0 F( C( A$ v7 B& }
  11. >>> math.floor(-4.01)
    $ S; s& |1 k" C/ Y
  12. -5
复制代码
( }/ o' v! i: R$ o+ {. p4 Z
math.pow(x,y)  返回x的y次方,即x**y5 S1 M  ^  ~& x6 s8 j6 S# y
  1. #返回x的y次方,即x**y7 t- B( z3 t3 M/ B) O& a7 x
  2. pow(x, y)
    # u# P) k* {) y/ v
  3. Return x**y (x to the power of y).
    # c- Y0 l4 K- y4 l
  4. >>> math.pow(3,4)
    : A5 b  \) d0 L' z
  5. 81.0& b' w, g4 `" ?/ T* X) r* Z$ j; B
  6. >>> $ ?# ?; I. G6 I0 _$ H" i
  7. >>> math.pow(2,7)& R6 N! U% G8 j$ g1 R
  8. 128.0
复制代码

/ J+ ^1 {* M$ gmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 p  ?5 h. U- c* C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). ~  O4 D8 |* H. |4 t
  2. log(x[, base])
    + M7 p4 f( w$ |' a1 f  o( w
  3. Return the logarithm of x to the given base.  W: X! k% O$ f% I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    / R( y; R" u8 U* Y% y# J* F
  5. >>> math.log(10)3 i8 a- z- |3 b) o: f
  6. 2.302585092994046
    . k0 \- h: @; Z" s
  7. >>> math.log(11), v* b* T/ |  H3 M
  8. 2.3978952727983707
    , X/ m7 [1 J6 J5 L$ V2 i
  9. >>> math.log(20)
    % F. v* s3 O; w& K" p) G
  10. 2.995732273553991
复制代码
/ H% R* a: `+ [" C6 N, z
math.sin(x)  求x(x为弧度)的正弦值
8 P$ P  |3 g' h. X9 O' h2 r4 e
  1. #求x(x为弧度)的正弦值0 k; [! ~. M: s9 m' F6 P$ M
  2. sin(x)$ K2 k% P, x  `' p; z2 O/ W
  3. Return the sine of x (measured in radians).
    4 n9 N2 \/ d* N) t$ r
  4. >>> math.sin(math.pi/4)
    1 ^: s; Z  V1 {$ s0 {7 S; ]- E
  5. 0.7071067811865475
    8 D% o2 s) K) t6 O8 L& n
  6. >>> math.sin(math.pi/2)! C0 s/ u0 E; c7 l: j3 c  r
  7. 1.0
    & K4 m) D" D4 b
  8. >>> math.sin(math.pi/3)
    * i0 D4 m1 R# O! x! U
  9. 0.8660254037844386
复制代码

8 m2 w, m* O. ~; w  |  xmath.cos(x)  求x的余弦,x必须是弧度
1 |4 O# U* g: E* z6 T) H) A/ o9 i7 L. q
  1. #求x的余弦,x必须是弧度
    # H3 E6 {6 d' N5 Z4 L
  2. cos(x)
    9 E2 K! n4 @$ l
  3. Return the cosine of x (measured in radians).
    6 v7 ]- ^8 ^  f: A& \+ g
  4. #math.pi/4表示弧度,转换成角度为45度
    ) y; d8 o0 o* k- p5 j  Y
  5. >>> math.cos(math.pi/4)
    ; w9 f$ F" z0 [/ C
  6. 0.7071067811865476
    & D1 R* O1 K$ ]$ ~) w/ K
  7. math.pi/3表示弧度,转换成角度为60度$ X, |  h0 j( p1 E9 y
  8. >>> math.cos(math.pi/3)
    ; N6 B" i" H1 c& N9 C
  9. 0.5000000000000001
    " @! |9 R5 a0 x5 ^* I  Z
  10. math.pi/6表示弧度,转换成角度为30度
    2 l( R6 ?6 [2 t
  11. >>> math.cos(math.pi/6)
    5 T3 n8 |+ K$ R0 ?- Q2 t$ }+ W
  12. 0.8660254037844387
复制代码

- v7 S( F4 k$ a5 Jmath.tan(x)  返回x(x为弧度)的正切值' Q3 ~& O3 U* n5 w: N. B8 m
  1. #返回x(x为弧度)的正切值% ~2 ?% S( R2 {9 h  P- ]* [
  2. tan(x)6 ~+ q0 d/ f" y# \
  3. Return the tangent of x (measured in radians).
    * X& h; \' l: B/ |5 q# B: L
  4. >>> math.tan(math.pi/4)+ E  ]$ k9 e# ?2 l
  5. 0.9999999999999999
    3 s& |( [8 D1 J- E: I! o8 g
  6. >>> math.tan(math.pi/6)- p. I# I7 W6 i6 _5 [0 P
  7. 0.57735026918962574 m8 p! M8 S9 l. S- y5 t* G
  8. >>> math.tan(math.pi/3)2 s0 u6 C# Q& ?5 Q+ @
  9. 1.7320508075688767
复制代码
' R8 O: \$ m9 W& l( N6 O" ^6 G9 w' B4 v
math.degrees(x)  把x从弧度转换成角度
1 S5 J2 a; O( q) J
  1. #把x从弧度转换成角度
    ! o5 c0 a/ _" t2 Q
  2. degrees(x)/ Q. o6 U% M6 ?4 A5 e' f5 C% f9 d
  3. Convert angle x from radians to degrees.
    & ~3 r; A1 }+ X8 n$ D, E

  4. - J' @3 B$ U8 E* H5 T/ u0 n0 c, O
  5. >>> math.degrees(math.pi/4)
    * n+ H+ e& U  M2 p* i! `
  6. 45.0
    : n' ]( D! Z6 h6 Y' q" n
  7. >>> math.degrees(math.pi)
    1 G! \9 G( c% ^) N) ^7 R" x) v
  8. 180.0
    3 R  l  s. l% t7 \3 P5 {' {# d
  9. >>> math.degrees(math.pi/6)
    ) A$ A$ }5 J) e" m  E
  10. 29.999999999999996: Z/ H0 B7 j, h0 [2 b
  11. >>> math.degrees(math.pi/3)
    # `- i9 M9 `: P2 y" E$ l
  12. 59.99999999999999
复制代码

2 ?8 s6 `1 _7 G* Fmath.radians(x)  把角度x转换成弧度8 E9 B2 a4 s9 K; X
  1. #把角度x转换成弧度
    7 C: N$ N# O  u; j! l. X
  2. radians(x)
    4 O( }9 H& K; x- m2 ?2 v' |
  3. Convert angle x from degrees to radians.
    3 O6 C0 e) Q* P5 Q/ `8 @
  4. >>> math.radians(45)* V/ `3 Y. k, R
  5. 0.7853981633974483
    6 U3 G0 C& M& d" `4 c& l5 |
  6. >>> math.radians(60)
    " l1 z2 R4 r. o; y: D, a! y
  7. 1.0471975511965976
复制代码

  a" _! Q- \. r, B  G5 y/ {  nmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
2 R) h: [9 [( s6 t, C
  1. #把y的正负号加到x前面,可以使用0* H, C" U+ m) y  v+ d0 ?( D4 O
  2. copysign(x, y)
    , p- P, c: v$ L6 J
  3. Return a float with the magnitude (absolute value) of x but the sign
    ; ]* U; `6 I( i5 N% ^
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    4 `1 z/ v+ q6 C: ?( I. Q( V2 W
  5. returns -1.0.
    5 v7 h' N# K! ~. A

  6. ; Z" l2 v* ?) j2 M- I
  7. >>> math.copysign(2,3)' P! E# J. e+ T5 a( H5 D
  8. 2.0
    8 s( H1 ]! ^6 C/ I& U8 w. |
  9. >>> math.copysign(2,-3)
    6 c: D4 O. \) w* Z7 `6 c- G
  10. -2.02 ~% u$ `, c! D1 h# [( M- S
  11. >>> math.copysign(3,8)
    ( j4 k/ U$ m" A  d  i3 W! ?
  12. 3.04 a. Y3 ?$ G9 E6 G4 v
  13. >>> math.copysign(3,-8)9 e0 `- Q1 Y- Z/ C
  14. -3.0
复制代码
1 f  ?$ S: N7 _2 r1 I
math.exp(x)  返回math.e,也就是2.71828的x次方
2 t$ H. E0 r) H# [+ Z
  1. #返回math.e,也就是2.71828的x次方
    ( v4 q5 q! Z, E* }( g, Q7 a6 m
  2. exp(x)
    3 }, |4 q+ O8 [4 n  t
  3. Return e raised to the power of x.8 n# W0 p+ d/ y. [5 Q

  4. 1 w) X. o% u' ]3 N6 P2 M$ c
  5. >>> math.exp(1)8 k4 I& i& L( Q' A; d
  6. 2.718281828459045
    5 B. V( Q" P& k7 P/ ?. M3 K' \% [1 ]
  7. >>> math.exp(2)
    " \+ r) x9 D! g! V
  8. 7.38905609893065# ?0 z# n9 T0 f5 ^: \. H
  9. >>> math.exp(3)% _( a* E; m; L7 e% O4 V
  10. 20.085536923187668
复制代码

4 k. w# c3 }$ zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
5 G" x8 i0 m- h, X
  1. #返回math.e的x(其值为2.71828)次方的值减16 |8 n% Y: |' M: x* q7 U
  2. expm1(x)6 c4 `+ u3 b4 N3 r3 D& Y+ {4 @
  3. Return exp(x)-1.
    1 ]( _# G! I5 ^* n1 @
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ! m) S0 k' G- Y

  5. ; y! \9 Q6 C  R* w8 }
  6. >>> math.expm1(1)7 j  R4 r, g# [$ D9 h  q. s
  7. 1.718281828459045
      H1 d* S7 _3 o( o& Q; k
  8. >>> math.expm1(2)2 b  |; H/ o' e9 h( w' E
  9. 6.389056098930655 s! U+ Q; m. ^, J& v& Y+ q
  10. >>> math.expm1(3)7 z9 A& T2 S( |3 m  F. |
  11. 19.085536923187668
复制代码
0 `! m4 {" U/ O, \
math.fabs(x)  返回x的绝对值+ A. u3 s; w) h0 Q: ]
  1. #返回x的绝对值" c0 k+ x; \/ S3 E
  2. fabs(x)
    / j+ D4 e9 U0 s
  3. Return the absolute value of the float x.
    ; M5 i9 ?+ Z+ D( _

  4. ; D" W% G/ Z) M) I/ e
  5. >>> math.fabs(-0.003)
    ; N, y) W, b; k9 e( F7 f4 N; e5 |
  6. 0.003( N4 z: W. m) ]8 P
  7. >>> math.fabs(-110)
    8 i1 x/ b9 i+ ~9 ~
  8. 110.02 ?4 l  T/ m" g3 p" Q2 b
  9. >>> math.fabs(100)
    " n/ X8 C5 Z, {7 l
  10. 100.0
复制代码
! z0 u) {+ H  t( z
math.factorial(x)  取x的阶乘的值
+ Z0 s4 I7 j9 ~! h. \7 a
  1. #取x的阶乘的值
    4 H4 u) Q' `* N  e& ~
  2. factorial(x) -> Integral
    0 F( g1 m1 L  |7 l1 ^
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    0 P# z2 G7 h( n& j, g7 B2 A3 n
  4. >>> math.factorial(1)
    5 d' Q, i8 N4 j0 M3 l$ G
  5. 1
    ) j  R4 N* D: @: k
  6. >>> math.factorial(2)2 y$ z# x# j% _: {  L( b- X  d( U; }
  7. 24 t. o4 e5 u0 A  ]- J* X
  8. >>> math.factorial(3)7 q  I& T, c/ w1 J
  9. 6  s! N: g2 R% `8 [2 B
  10. >>> math.factorial(5)
      o# t/ Q0 t: Y3 M5 U/ I
  11. 120
    ! t  o2 n+ k$ ]0 q
  12. >>> math.factorial(10)( G6 V% f$ w2 B  V8 Y4 g
  13. 3628800
复制代码

8 W6 k( l1 w) Z; E: j! jmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
. t& l! _2 X; H4 v
  1. #得到x/y的余数,其值是一个浮点数2 O6 X- n# l; I& x- R# N
  2. fmod(x, y)! z. g& Z$ B$ ]
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    $ x. v7 c3 d9 t: I7 u
  4. >>> math.fmod(20,3)# t! ?$ k- ?  v  a9 o! N; @
  5. 2.0
    : R6 e' i. \% J3 J) ~5 B
  6. >>> math.fmod(20,7)' ~1 @; X1 C' k' Z' X7 x* R
  7. 6.0
复制代码

6 S0 }* X0 [# }math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 Y5 ^" U8 i3 w* q7 r! N- C" x; W
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    0 ?% K% r& ?! V/ ?) U4 i
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值1 M1 D1 i' N# m  ~# \7 E1 Y. @0 I- b$ K9 z
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ _! u% N  P2 I' |# B% l
  4. frexp(x)5 u; T' [- k5 V5 X, U
  5. Return the mantissa and exponent of x, as pair (m, e).
    - ?$ Y% o  p' W! F
  6. m is a float and e is an int, such that x = m * 2.**e.+ j  U4 W0 t' B! F( I: V1 E2 B' u
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.: T' Y+ ~# r  r- y0 K
  8. >>> math.frexp(10)* Q- A8 F; J3 U" Q; @7 C0 q: c
  9. (0.625, 4)2 r% Q. O0 _0 B0 a) r5 C
  10. >>> math.frexp(75)) T# J3 ~8 F8 L/ E
  11. (0.5859375, 7)3 E: Z5 W9 `: {1 M
  12. >>> math.frexp(-40)
    7 U8 H0 m3 L" E7 V& p0 `' s
  13. (-0.625, 6)6 b) d0 n9 H( P: _' ?' t3 r
  14. >>> math.frexp(-100)$ A) ~7 K! O  R. V* d, M
  15. (-0.78125, 7)
    1 {1 h4 c4 F+ t4 q- i7 T
  16. >>> math.frexp(100)+ s; m0 O4 R/ _# k8 H, b! V
  17. (0.78125, 7)
复制代码
% ~1 B9 i$ |& O' i
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, e3 T; C% s2 d4 v
  1. #对迭代器里的每个元素进行求和操作; ^1 x" m. D, c
  2. fsum(iterable)
    1 g  Q* u7 L$ b1 I% W% b9 ~5 B
  3. Return an accurate floating point sum of values in the iterable.! B- ?( n( e) B3 D" D4 O, @- N
  4. Assumes IEEE-754 floating point arithmetic.
    * M7 b. R5 W$ {. _; r6 C% J5 w
  5. >>> math.fsum([1,2,3,4])  w* T8 D1 R4 T0 N/ f
  6. 10.03 m; q2 w/ ]4 P7 d" Q3 f! @5 l, n7 O
  7. >>> math.fsum((1,2,3,4))
    ( I! Q1 C3 n  u4 [3 R& V
  8. 10.0
    2 a7 M" ~2 a" V4 c6 e& \% V8 I
  9. >>> math.fsum((-1,-2,-3,-4))
    9 Q( I  A0 E' o1 @1 ?: p- B' d
  10. -10.0
    + o/ w# r8 _& s$ q
  11. >>> math.fsum([-1,-2,-3,-4])6 o# k6 t7 H) n, j  {3 G! g
  12. -10.0
复制代码
8 a$ A5 X" R2 j4 O
math.gcd(x,y)  返回x和y的最大公约数+ E. I# Z: C; i" u. P
  1. #返回x和y的最大公约数( [2 ^3 p& a+ n4 n% s
  2. gcd(x, y) -> int( i. [, B/ q+ u+ K" G
  3. greatest common divisor of x and y) c( v, I# L+ `7 ^; b0 m! S
  4. >>> math.gcd(8,6)- T; }" `9 w2 {. m' U0 r) F  A
  5. 2
    $ V! l. s) F4 V- `4 u  e9 k
  6. >>> math.gcd(40,20)
    1 @- t2 c& `# k0 \; u! t( `4 m, ]
  7. 20
    6 F7 o2 D5 o2 z( s/ U
  8. >>> math.gcd(8,12)- O9 F6 j7 u! I! w8 ~( R
  9. 4
复制代码

' |6 G, z+ w* B! d: @math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False9 J" [8 T: Z) |% x% q# h8 G
  1. #得到(x**2+y**2),平方的值; n- I; i& e& _
  2. hypot(x, y)7 c3 K% t5 W$ A3 B
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    . {; o% \  {9 t) b1 s: ?) P4 g: I, ^
  4. >>> math.hypot(3,4)
    ( N3 _/ y4 X9 h7 U2 ?. y
  5. 5.0
    : c6 j9 k: y( k; z5 j$ t0 ^! K* b
  6. >>> math.hypot(6,8)! e* z+ J0 ?' a4 k% m7 Y! j
  7. 10.0
复制代码

' j4 f( M& j$ l) V: ]+ fmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False; K* U* p% ~$ D6 W: J) M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False% M+ ?. l) D4 A, P5 @1 G6 H+ c3 C
  2. isfinite(x) -> bool4 |0 }+ F& k- \; G: r+ Y. W
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    6 Z  R3 G1 }  c! b6 i4 `1 h0 Y
  4. >>> math.isfinite(100)# Y+ M, J4 w+ k+ \
  5. True
    & Z: {2 F6 f- Z* i
  6. >>> math.isfinite(0)! L" B/ b, e5 W# X- [; h' B% ?
  7. True
    ) M; a5 `9 a/ Z, M( V: U
  8. >>> math.isfinite(0.1)+ T2 Z) u+ L4 K' y' K( C
  9. True; s1 M  l' K* e  q. d
  10. >>> math.isfinite("a")
    1 t  r% o& u, D% v! V+ {: Z
  11. >>> math.isfinite(0.0001)7 L* ^! {3 E0 p8 r
  12. True
复制代码
# i/ _5 y8 e2 }# J5 w1 u8 t
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False/ I! |0 _8 p- v: U- T
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ' ]" B9 N9 i$ ^1 u# n9 }% i
  2. isinf(x) -> bool' ]- s3 X8 T2 C) p
  3. Return True if x is a positive or negative infinity, and False otherwise., U' H7 P* C# A" Y3 |; `' P
  4. >>> math.isinf(234)3 j' F. X$ n  A- P/ A) B' }/ g
  5. False, _1 A4 c4 p( g- ~& S: j/ k
  6. >>> math.isinf(0.1)3 v2 D/ J* L  H( i, ~5 v* m1 L) X
  7. False
复制代码

" a- c( N; e- s# Mmath.isnan(x)  如果x不是数字True,否则返回False3 x, v& h  _- P
  1. #如果x不是数字True,否则返回False6 ]1 G7 L" N: R2 [( [- J5 m
  2. isnan(x) -> bool
    4 N" M) [6 \3 c" m  G$ |+ P
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 Y& _6 \7 n% G* ]) H
  4. >>> math.isnan(23)! m: W; B% a0 U" k" \
  5. False* P9 L7 }9 F9 F) h
  6. >>> math.isnan(0.01)
    . T$ c. j3 x, ~/ w2 H
  7. False
复制代码

0 n& o5 J" s* hmath.ldexp(x,i)  返回x*(2**i)的值
" F1 ~7 T) y$ h
  1. #返回x*(2**i)的值9 N# W1 B% Z7 I. l; Y# {1 K6 A9 p2 b
  2. ldexp(x, i)7 h( f  e7 J0 O2 F, A6 @
  3. Return x * (2**i).
    8 k% k3 i: O. A9 J- B  Z
  4. >>> math.ldexp(5,5)
    3 J( u9 d  X! e0 G
  5. 160.0" H8 z1 ~; o. u3 p
  6. >>> math.ldexp(3,5): o9 j" B- D- A$ t8 H/ ~* y) b/ O
  7. 96.0
复制代码

) B7 }7 R) a( r1 H# }1 ?math.log10(x)  返回x的以10为底的对数
" A/ o7 z3 z) `# J
  1. #返回x的以10为底的对数
    . V+ ?( A! t% b9 Q& ?4 Q, @) x
  2. log10(x)
    , s, x+ s( \  l/ A# W
  3. Return the base 10 logarithm of x.
    * M' U1 x: {: _7 W7 n. D. p7 w) q7 Z& V
  4. >>> math.log10(10)4 d/ t, z! i6 ~* P2 H* F2 @1 q
  5. 1.0
    ( b) E$ b" ?# m; S5 P( j
  6. >>> math.log10(100)( H) ~) K& M* r+ ~) }6 F
  7. 2.0
    - N' J) y5 e5 o; c; w' v' @
  8. #即10的1.3次方的结果为20
    2 ^6 r7 q6 F, d. j9 L
  9. >>> math.log10(20)) ^& U$ B, p) w! }% o' ^3 @  M0 ?
  10. 1.3010299956639813
复制代码
, S' G0 w6 E6 B0 {& Q$ i. F1 I
math.log1p(x)  返回x+1的自然对数(基数为e)的值3 b6 \+ \3 s  n" g# N1 F, U
  1. #返回x+1的自然对数(基数为e)的值
    % S% p& S0 n# t
  2. log1p(x)+ N' w) X. E3 a9 J4 z! h4 f6 P
  3. Return the natural logarithm of 1+x (base e).
    ( ]% x& [, K: @, t- U; j0 Y. A, e. E
  4. The result is computed in a way which is accurate for x near zero.$ b" I( G( |7 u; ]( n
  5. >>> math.log(10)
    9 s: }" ?  [8 `! `+ M1 |
  6. 2.302585092994046
    8 I/ Q9 }1 S, A2 C$ c7 x
  7. >>> math.log1p(10)
    $ w! C' H/ e" Q. v
  8. 2.3978952727983707- r# @# k. ~" Q+ E3 D
  9. >>> math.log(11)
    9 l' P: O+ i; _0 n1 _
  10. 2.3978952727983707
复制代码
. g: [; o9 ]. c+ i
math.log2(x)  返回x的基2对数
9 z5 z# B! g+ V2 M  |2 u
  1. #返回x的基2对数, D. ^, ]- S' G' h1 |# K
  2. log2(x)
    ( m- j; X7 j) W' L8 x, x
  3. Return the base 2 logarithm of x.
    , J4 V3 E" ]8 f  Y8 V' |) \
  4. >>> math.log2(32)! D" j" k% I: h  W5 }( J
  5. 5.0
    , o1 K  O# Z* X) y* c# K! k
  6. >>> math.log2(20)
    : r, I- C% r$ p
  7. 4.321928094887363+ k& d( P$ M! g; S3 d
  8. >>> math.log2(16)
    ( {( O) E( o5 `$ K, d+ m
  9. 4.0
复制代码

6 N9 F/ ?0 m& ?, R) s" Qmath.modf(x)  返回由x的小数部分和整数部分组成的元组2 I- i, T- M1 y% C# Q
  1. #返回由x的小数部分和整数部分组成的元组
    ; Q$ X3 X) F! Z7 M% ~5 S! U
  2. modf(x)
    ; s& |8 e" v" |
  3. Return the fractional and integer parts of x.  Both results carry the sign7 d4 W9 _% _& M" W8 ]9 t
  4. of x and are floats.  P5 D' i" |5 {  Q6 }
  5. >>> math.modf(math.pi)4 U  F( b2 V3 G% b+ f# m
  6. (0.14159265358979312, 3.0); F) |5 f. M* I6 K1 u; N0 j
  7. >>> math.modf(12.34)
    * S3 Y7 ~- G9 Y' h/ ~3 F
  8. (0.33999999999999986, 12.0)
复制代码
; V7 T& v) F' e. J$ L8 R
math.sqrt(x)  求x的平方根
7 g. L3 Y4 R6 P3 D; `7 B8 G2 v. B
  1. #求x的平方根7 @; S( q/ u# ^
  2. sqrt(x)
    " {* N2 P6 j9 s5 l( o
  3. Return the square root of x.
    . `" l9 C3 ?$ t8 X( j% z+ v! z, F( |
  4. >>> math.sqrt(100)
    $ T$ K3 ~9 D" U0 ]; n5 A
  5. 10.0& A: h9 u6 j+ J1 ?( I
  6. >>> math.sqrt(16)1 h6 g) `: U6 ~! N! ]" J
  7. 4.0/ F3 O" \3 \- f% t7 T
  8. >>> math.sqrt(20)6 [$ f% [9 J2 d4 G5 V: h
  9. 4.47213595499958
复制代码

9 T% h. |; _0 [7 Nmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    8 w1 U8 P  U' D7 {% J, H! ?
  2. trunc(x:Real) -> Integral& l  M- P+ E. R8 p  f  @" v) f
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    5 n/ W' _& I; J
  4. >>> math.trunc(6.789)
    * T5 K  e2 p2 D. B1 C
  5. 64 d% i7 V4 ]5 S3 f% T4 k
  6. >>> math.trunc(math.pi)2 C, U6 k$ _; F2 B8 ], N; H, G
  7. 3
    / M. D) |5 m6 o4 F7 ^
  8. >>> math.trunc(2.567)
    0 |8 D  v: Q% `
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-22 20:13 , Processed in 0.091012 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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