新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 2030|回复: 0

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

 关闭 [复制链接]
发表于 2021-7-24 10:21:24 | 显示全部楼层 |阅读模式

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

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

x

# c+ L2 ]4 _# N1 V0 I, w) d& V( _& E. ]【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。  z* R; R2 N8 \9 n+ {# ~
* S+ N! x! ^) q1 p5 H1 D$ H0 C
方法11 w, j% M7 w: w9 p
  1. >>> import math
    ! p- t$ d5 ^. _! ]/ w( p4 `
  2. >>> math.sqrt(9)
    ( \" m" v  a. S/ Z
  3. 3.0
复制代码
方法2
' q' v/ g) [* S% p4 ?3 a
  1. >>> from math import sqrt% W/ H8 H* Y( |1 l) ]
  2. >>> sqrt(9)0 v3 \( f  z* S( ?/ ^" U2 n
  3. 3.0
复制代码

; r2 ]( X, _. T2 H6 v
# [2 F! J6 X+ q5 J; E
math.e  表示一个常量
: l) S; _6 m  K; D0 `
  1. #表示一个常量5 A- _8 F! _" |/ ]
  2. >>> math.e
    2 Z; x% q$ U( l; ]' P; C
  3. 2.718281828459045
复制代码

2 Z( ?( J) k0 xmath.pi  
数字常量,圆周率

# S- L: A/ `  g3 z& \
  1. #数字常量,圆周率0 r' [/ _# J/ W8 _
  2. >>> print(math.pi)
    . O6 a5 X7 V" |2 D# ^% v
  3. 3.141592653589793
复制代码
$ f8 }6 b& ~2 I: k) r& V+ ?/ T* ?
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

6 F, J( N. k: r& G; u
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 x' d& [" H! o! i
  2. ceil(x)
    0 p" l) M6 e6 y' H* M$ ^; @
  3. Return the ceiling of x as an int.) H& a3 T- |8 \2 M
  4. This is the smallest integral value >= x.
    2 |6 K, ]/ I% |# N& _* A- M

  5. & d0 b/ o& k2 ]) j' \. {# u# b
  6. >>> math.ceil(4.01)
    4 z+ v5 u: N# B8 h( R1 d
  7. 5
    ( S% b' U0 ~' L" E
  8. >>> math.ceil(4.99)3 d  R7 v8 ]. i
  9. 5
    % p+ P% R/ u4 p$ \9 T+ t
  10. >>> math.ceil(-3.99)
    1 R$ C! _; ^' g' a2 {) A0 z
  11. -3- w$ n& t- O4 `/ e2 b9 T
  12. >>> math.ceil(-3.01)2 v4 W, K, i  a+ U4 @0 ]* {& F
  13. -3
复制代码

: G3 S$ P6 l2 G- y+ Y. F# ^0 H' k+ A# `math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# }6 f" D" Z/ \1 p) q) }! E8 I
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ! l$ Z! j6 w0 V
  2. floor(x); l, r/ ^- W  v$ k1 b& B4 y
  3. Return the floor of x as an int.
    ; n! O1 d! E; ~% a6 s' O
  4. This is the largest integral value <= x.
    5 Y2 S3 q6 J2 F) d+ d8 _& N
  5. >>> math.floor(4.1)
    3 \, ~4 G& @. R& P& F
  6. 4
    0 v: @" ?9 X1 L3 C/ W2 i! S' t
  7. >>> math.floor(4.999)0 _! i* z! `8 K$ e" Z
  8. 4
    + r& R8 E0 b: ?" j7 F4 c
  9. >>> math.floor(-4.999)
    3 k* ]1 \- N# H1 b" p
  10. -5( j0 Z4 F% t/ W
  11. >>> math.floor(-4.01); g0 G9 m2 @, H& b1 ?
  12. -5
复制代码
. \  J& R8 J+ T/ h, p  K6 g- A* H
math.pow(x,y)  返回x的y次方,即x**y
3 H& \' B  Y' T+ v5 k9 r
  1. #返回x的y次方,即x**y. n$ f8 G( H! _! L
  2. pow(x, y)$ {- p2 Y# q! e( X5 |% i
  3. Return x**y (x to the power of y).8 U1 ]0 P! {& J4 l/ k
  4. >>> math.pow(3,4)
    3 H# A  g1 J$ W! V
  5. 81.0, K9 @: n, r7 g, f- t
  6. >>>
    0 Q( b' q, |8 @
  7. >>> math.pow(2,7)
    ) p) l6 ^& h( @* E& v5 ^
  8. 128.0
复制代码
: P0 E$ l& a/ H4 N% ~
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 ]& _/ q% K7 }1 U& W: L
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ J( v2 G/ q, @' v( {
  2. log(x[, base])( u  ]; P4 k% M% |
  3. Return the logarithm of x to the given base.
    ( u+ x+ \& \( d- l; v2 d9 M
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ' i4 f: f0 Y! Q; l" m# f# g
  5. >>> math.log(10)
    & X3 v6 J3 l, h- J* p/ d1 B
  6. 2.302585092994046
    ) _$ Q5 c( f4 d+ A* ]6 O
  7. >>> math.log(11)
    2 A* c: t& v5 w/ M7 U5 {7 u
  8. 2.3978952727983707
    " T& z( ]( x0 \
  9. >>> math.log(20)
    3 K/ I! z7 [6 a, D8 @
  10. 2.995732273553991
复制代码

! w' L) M7 k! L+ y$ `  x8 k+ \math.sin(x)  求x(x为弧度)的正弦值* [9 u1 d& y) f' U% l3 r$ B
  1. #求x(x为弧度)的正弦值" ^! G- D7 `; g
  2. sin(x)
    ) V/ e$ `  K1 t4 w! F* J3 i3 g
  3. Return the sine of x (measured in radians).
    $ M4 y$ A0 O! ~" g/ y; H5 p
  4. >>> math.sin(math.pi/4)' r# u/ z. S* Y) U  a+ K
  5. 0.7071067811865475
    / B4 `2 J+ E. U$ t8 k/ ?
  6. >>> math.sin(math.pi/2); X) V3 m/ R5 w' a( K" R
  7. 1.0  y& E! a! J- q" l
  8. >>> math.sin(math.pi/3)
    4 M- Z+ d& a5 n9 @3 }  s
  9. 0.8660254037844386
复制代码

- G+ S& o# t# a3 U0 ^$ gmath.cos(x)  求x的余弦,x必须是弧度
) x7 v+ i6 S* T9 B! ?
  1. #求x的余弦,x必须是弧度
    $ }2 @! l6 F9 B2 t* v8 B
  2. cos(x), T# m: g, P, L5 r. j$ j
  3. Return the cosine of x (measured in radians).. s, Z+ p3 `# ]9 V2 G
  4. #math.pi/4表示弧度,转换成角度为45度5 o0 Q# a4 J/ g
  5. >>> math.cos(math.pi/4)( M. X4 T7 B8 _0 j- v/ L3 i# ?
  6. 0.7071067811865476
    2 x! a8 B- o* V& H/ w
  7. math.pi/3表示弧度,转换成角度为60度, H) w" u0 U4 u9 D7 X( W
  8. >>> math.cos(math.pi/3)
    4 M" C7 Q. G# E
  9. 0.5000000000000001! v7 h$ Y- n- _/ e4 O
  10. math.pi/6表示弧度,转换成角度为30度
    ! h1 p6 {) q5 Y/ G7 z
  11. >>> math.cos(math.pi/6)
    1 t% h: Z* m" `: K$ B
  12. 0.8660254037844387
复制代码
! [  o! k( h" b/ E/ ^% O& O5 L8 Z
math.tan(x)  返回x(x为弧度)的正切值
8 G$ B4 r! J: S
  1. #返回x(x为弧度)的正切值1 \; r5 c( f* }9 @+ J, t& F. G
  2. tan(x)
    1 j2 }( W2 [/ \# t1 |
  3. Return the tangent of x (measured in radians)./ I8 t3 E" j, B' \" |/ l' K+ D
  4. >>> math.tan(math.pi/4)) [/ d$ ?, E' |+ d' q* X
  5. 0.99999999999999995 E: b0 S* H) h3 M$ Z5 W% D
  6. >>> math.tan(math.pi/6)$ x( M  f! }4 c* p+ j' O6 h
  7. 0.5773502691896257
    ) Z' \) u; i+ d
  8. >>> math.tan(math.pi/3)) y& P2 [$ ^& O# q0 b
  9. 1.7320508075688767
复制代码
1 l: @* e# S0 r) b# J
math.degrees(x)  把x从弧度转换成角度6 i# k$ t) f" i( F7 W. _7 T' l
  1. #把x从弧度转换成角度
      A" R  A* r$ {: M# {" J
  2. degrees(x)! }0 x+ D; Q8 M% j! A6 T8 ]
  3. Convert angle x from radians to degrees.7 Z6 p  _5 l$ Y9 t
  4. , ]# k; R+ G' ?' A! j9 f. M( J
  5. >>> math.degrees(math.pi/4)
    3 O) E6 L- R  D' D+ M1 G0 i+ ~. q* t- k
  6. 45.0% d+ Q- y  U- c0 ~
  7. >>> math.degrees(math.pi)) c! h( K6 I+ M
  8. 180.0
    6 q, j- R1 E$ p. \0 {2 X. P  P* s
  9. >>> math.degrees(math.pi/6)
    ; U4 d& H5 {5 H. p. T
  10. 29.999999999999996
    ; k* L; @, A& u/ k
  11. >>> math.degrees(math.pi/3)1 T+ ?* q1 n( R6 ^3 y) |4 o
  12. 59.99999999999999
复制代码

; P. M  C$ g& I' |/ Umath.radians(x)  把角度x转换成弧度
( E) V& e+ F  c4 B
  1. #把角度x转换成弧度# w' G* S; H2 @
  2. radians(x), e7 [2 K' R$ H" C) F# ~# ~
  3. Convert angle x from degrees to radians.
    . r9 ~3 g' P! @& B+ ~) t$ C
  4. >>> math.radians(45)
      |, ]# v7 c5 V! _0 D. T
  5. 0.7853981633974483
    7 z% R4 p+ x# S* m7 A  T5 F% D
  6. >>> math.radians(60)
    + Y( @" k) k0 @# w
  7. 1.0471975511965976
复制代码

: H- F' s% e# b! gmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
! F  _' _( I8 T' f- Z/ ?
  1. #把y的正负号加到x前面,可以使用0! F) f  @+ G6 v0 ~
  2. copysign(x, y)
    * s* I3 g( }7 D3 p, _) W
  3. Return a float with the magnitude (absolute value) of x but the sign " E2 ~& l  t  _2 n5 I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ! g3 H, W+ x' `) U9 ?& ]$ z
  5. returns -1.0.
    6 Z: b% o$ a/ t( p
  6. 7 V5 _& s+ o7 q) q& z
  7. >>> math.copysign(2,3)
    # b5 f/ i2 a) x$ D5 p/ c
  8. 2.0
    $ V. U! n9 Z5 r, T8 A+ |" X7 q1 ?9 D3 M
  9. >>> math.copysign(2,-3). f# U: c0 G% F, P
  10. -2.0
    $ `1 |. H2 F7 Z8 J
  11. >>> math.copysign(3,8)$ l8 J: d( d. ~/ k4 I0 `
  12. 3.0& X. b, R: Y& ]3 l5 q
  13. >>> math.copysign(3,-8)6 P- K8 U: L7 ~) z. \/ z
  14. -3.0
复制代码
" J) Q9 I! x/ Q6 c6 o( x
math.exp(x)  返回math.e,也就是2.71828的x次方+ {8 p, s9 d2 P8 h( q9 w) t' u
  1. #返回math.e,也就是2.71828的x次方
    ' U0 P3 D' w7 @. l' v; S$ V9 W
  2. exp(x)/ D+ n* @+ j9 F( P& g- E% y* u
  3. Return e raised to the power of x.
    6 x" q1 D1 y: M$ D( l" `- C

  4. : E  ]5 L9 H5 _
  5. >>> math.exp(1)8 u$ {* \  ~6 l0 f" \* D
  6. 2.718281828459045# n9 @' i- |* n( B: r
  7. >>> math.exp(2)
    / s1 U# ^* l7 e3 z. i
  8. 7.38905609893065, ?# }* |( m9 @3 x- @
  9. >>> math.exp(3)
    4 y4 E! z! K4 }. P$ i
  10. 20.085536923187668
复制代码
* d' D, c% j+ c) Y9 p
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1& p- Z0 h# k+ X2 @1 s, t9 l% d" g
  1. #返回math.e的x(其值为2.71828)次方的值减1
    $ z6 K& ]( A5 S* `! {! `
  2. expm1(x)* h  ]+ X2 p6 U7 a2 m* N' e
  3. Return exp(x)-1.
    . K: L) M% C: H1 s6 w3 h/ S- y& i; z: t
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 Q) y4 g  Z* X4 U/ j9 Q. U

  5. , o9 T( C/ k+ \. _4 y( }6 D/ ^3 Z
  6. >>> math.expm1(1)
    $ }& ~. Y% i7 G8 W$ l: Y7 @/ y
  7. 1.718281828459045
      @) a( O5 I+ t* p
  8. >>> math.expm1(2)! j) s% Z& v6 c6 b, }6 {
  9. 6.38905609893065
    9 }( C+ R* W  P% ?0 a  L
  10. >>> math.expm1(3)" T" H9 X: d7 k4 Z: h
  11. 19.085536923187668
复制代码

( X$ s. J* `3 J. T7 \9 V5 d$ Zmath.fabs(x)  返回x的绝对值2 ]& w: }# E9 t( L" u* e
  1. #返回x的绝对值
    ; u5 z2 a( R1 X; y/ }& L( a
  2. fabs(x)7 x: n; t6 |8 W0 Z
  3. Return the absolute value of the float x.5 e6 g3 d7 P* Y$ w

  4. + K- _/ A. J/ l$ p  t
  5. >>> math.fabs(-0.003)* w9 h% D1 G: L6 O1 p1 q* |
  6. 0.003
    $ e2 N! p" c! Q  _2 s
  7. >>> math.fabs(-110)
    * x8 S3 b' s3 W/ z8 z2 P
  8. 110.0% h8 u. d2 C: S% c
  9. >>> math.fabs(100); t6 S! v0 Q) K
  10. 100.0
复制代码

9 j" \8 W& k. |( X; nmath.factorial(x)  取x的阶乘的值
' b' Y: O" R" I
  1. #取x的阶乘的值
    4 k9 S7 C, M! v5 {' J
  2. factorial(x) -> Integral
    : n' S0 P2 u; B2 V9 i
  3. Find x!. Raise a ValueError if x is negative or non-integral.) _) r- h( }& E4 ]1 F0 c* }1 ]
  4. >>> math.factorial(1)
    $ h: E2 q) p- w( h/ X% s& h
  5. 14 \. m  k9 [- G
  6. >>> math.factorial(2)- f8 U) {! c, h
  7. 2
    / Q8 W" p) e* N8 d
  8. >>> math.factorial(3)
    ; f0 ?7 F4 @+ I9 u1 X7 q. p& @
  9. 6, J5 q2 ^/ q" |+ n% r
  10. >>> math.factorial(5)
    4 c/ `: a3 w/ o6 T/ _; {1 B! ^' [' P
  11. 120
    " t! ?7 [/ a9 A7 J  n# W/ |; D* Y+ A
  12. >>> math.factorial(10)5 F( A' X/ H' o* [6 r9 y
  13. 3628800
复制代码
9 ?% `& \  a( [( c7 h2 R5 D
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数" t+ v0 w# ~0 `5 d1 a1 E
  1. #得到x/y的余数,其值是一个浮点数" m/ n# @# [- r2 a; Q& f
  2. fmod(x, y)
    6 L; j, L# K- @4 ~: g* E) n
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    2 {+ m+ k+ q- Y( w% O2 `
  4. >>> math.fmod(20,3)
    ' ~8 K" k8 C! C8 W  M
  5. 2.0
    ; n& A9 D6 K$ b% m6 t  f- a
  6. >>> math.fmod(20,7)- J4 f) ?1 q+ k/ G+ ^4 s/ I
  7. 6.0
复制代码
2 ?3 x- U, |7 f6 J
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
' Z0 ?! V$ K. C/ M+ e% ~6 E, h
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    7 Q+ X! w) u" ?6 z/ h
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 h5 f4 }+ Q  B# `
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) C$ a, ?; B0 @; k7 a# H
  4. frexp(x)
    - Q0 _: I9 J$ Y7 n; O: ~
  5. Return the mantissa and exponent of x, as pair (m, e).6 h2 [2 A2 \. o) D( B
  6. m is a float and e is an int, such that x = m * 2.**e.8 j) V) d2 m: W  H9 y3 H
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0./ p6 k# d* m/ B$ U
  8. >>> math.frexp(10)2 ?3 M" J/ v( O8 V9 [
  9. (0.625, 4)
    " B+ c, ]! H- T( f; Z
  10. >>> math.frexp(75)
    - ?# d+ z4 A( z* H( @7 Z) _2 n# c! v7 \
  11. (0.5859375, 7). F& j* G1 H: |/ \7 C( L
  12. >>> math.frexp(-40)  k" {1 m# q0 E. g, o
  13. (-0.625, 6)
    # k9 H* g& ]1 l2 R  _6 `& r5 j; m5 r8 X
  14. >>> math.frexp(-100)
    * Q0 W6 Y0 `; l8 n  \
  15. (-0.78125, 7)+ }- ?. @2 }' z0 q0 R$ b: P4 I! X
  16. >>> math.frexp(100). z& l+ u- `" j- c. P
  17. (0.78125, 7)
复制代码

; m2 P6 q: t( g% Q, A$ rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列: U# J' \0 W# y1 m
  1. #对迭代器里的每个元素进行求和操作
    . g! Z3 H' d3 }1 [- D
  2. fsum(iterable). H  B8 T1 P" d* S
  3. Return an accurate floating point sum of values in the iterable.  x& ?/ s& g- s. Q9 k; h0 s! T2 E2 N5 z
  4. Assumes IEEE-754 floating point arithmetic.
    9 q$ y+ c: C& n5 X
  5. >>> math.fsum([1,2,3,4])
    ' J* s. \) }5 E! ~+ U5 H  p
  6. 10.0+ f- o* p7 o6 R; O0 ^* T  w9 x- Z
  7. >>> math.fsum((1,2,3,4))- G8 U) L" |, o5 a; [
  8. 10.0+ C! {; B$ b! V1 m8 h/ E% T& l. X
  9. >>> math.fsum((-1,-2,-3,-4)). x2 v* g% l( I/ v% S" r
  10. -10.0. Z3 Q1 e! N5 C% r4 b* _
  11. >>> math.fsum([-1,-2,-3,-4])
    ! g3 q; j( r" t" Z1 J+ i
  12. -10.0
复制代码
! h7 ^7 G% N$ m/ x5 ~' l! @" `
math.gcd(x,y)  返回x和y的最大公约数# }4 B+ h3 Y) }& P5 ^* K
  1. #返回x和y的最大公约数" t9 U% m. G- J8 P; z9 d7 L
  2. gcd(x, y) -> int
    + y" `% g8 L6 k; y' c
  3. greatest common divisor of x and y
    ! [4 \2 a7 \3 i7 [) I6 r# Q
  4. >>> math.gcd(8,6)
    ) m& y6 }, L% N4 k
  5. 22 W2 o8 b4 l. W0 v# b: ^- h
  6. >>> math.gcd(40,20)1 [  z& T. d/ N+ n( g1 P; M6 s: C6 ~$ P
  7. 20# l7 J6 H4 `- ~5 I8 L6 I
  8. >>> math.gcd(8,12)  ]/ o4 i/ ^! p# `' E
  9. 4
复制代码
+ _0 G" ?5 A8 k. ]
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False- V9 C& f5 B% g* \6 G, i
  1. #得到(x**2+y**2),平方的值
    ! \* C  J) h- Q7 O! p
  2. hypot(x, y)
    , u% q# m* {: u$ j  ?9 c
  3. Return the Euclidean distance, sqrt(x*x + y*y).; y0 p, c! E& }0 {# n$ H
  4. >>> math.hypot(3,4)
    5 i1 N& i2 N3 G+ q" W- m: N2 I
  5. 5.06 V) N. F# k/ n2 W
  6. >>> math.hypot(6,8)$ r! t5 J' y* Q. n" Z' p
  7. 10.0
复制代码
, R6 M0 I) W0 y# U. X
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
  ~/ k4 u6 }# ?8 I1 o. j5 Y( P9 I
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ( e2 F! G6 @. r# R, ^! N
  2. isfinite(x) -> bool
    8 H$ S0 T$ i- ]/ o$ F' |; ]2 t
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' B& l. g2 M5 o* v0 S4 u* f
  4. >>> math.isfinite(100)
    4 j7 R+ ?7 V! k6 g1 T3 l+ o
  5. True
    3 T" V9 e/ L( x$ V$ f/ m7 ]3 ~
  6. >>> math.isfinite(0)
    2 {$ k: O! l1 h- M
  7. True; j5 E/ D' S( s
  8. >>> math.isfinite(0.1)
    # G0 B8 @' f) a: R$ p6 f( C, i4 }7 j7 G
  9. True; e/ T+ [( V5 i2 @$ A7 t
  10. >>> math.isfinite("a")
    ( I4 M6 i6 _0 u4 x
  11. >>> math.isfinite(0.0001)
    7 c: M( q8 L6 Q2 _9 L
  12. True
复制代码

# ]5 e& N1 @/ D5 |math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, V2 l$ E; M' E7 Q9 L  T. r, p
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False7 o0 x: ^. }2 a* {' d6 q8 I% a
  2. isinf(x) -> bool3 Q) i1 ~' N3 O6 E" F
  3. Return True if x is a positive or negative infinity, and False otherwise.
    . s2 H8 {( U" _+ b3 R8 h& ]  I
  4. >>> math.isinf(234)3 o# `! N" i  ?3 g. G' c8 x8 C
  5. False3 R" C- G1 V# B+ X
  6. >>> math.isinf(0.1)
    # z! f; M& j! U4 r7 k8 Y, z/ l2 [
  7. False
复制代码
- {2 p3 K$ v" D# k
math.isnan(x)  如果x不是数字True,否则返回False2 ]/ A% Y8 r! ?+ Q; p/ x) t
  1. #如果x不是数字True,否则返回False
    / O; c3 m3 u& |, |7 u2 A/ x3 H
  2. isnan(x) -> bool
    " ~$ @8 [( X" {! W5 o+ C8 B
  3. Return True if x is a NaN (not a number), and False otherwise.
    2 J% ?; B2 |+ G
  4. >>> math.isnan(23)* \0 P& f" O1 r
  5. False
    1 K: h5 m9 O, ]" N0 a# w8 g
  6. >>> math.isnan(0.01)
    9 f( ]5 O+ t( B  [" m, L% C
  7. False
复制代码

# s/ X( C; D" Imath.ldexp(x,i)  返回x*(2**i)的值
3 q% }; L, V7 f6 y
  1. #返回x*(2**i)的值& b) b6 v4 X0 X3 e- ^6 H- N
  2. ldexp(x, i)1 v9 r1 \1 ]  L5 ~9 Z- x7 o5 ?
  3. Return x * (2**i).3 A! x6 e/ ]& H5 \" h& t
  4. >>> math.ldexp(5,5)7 o% c4 m8 p2 _- M2 }4 e5 I
  5. 160.0- Q2 B3 x: b- f0 b9 S  |% ~/ f
  6. >>> math.ldexp(3,5)5 |% ~$ _: g0 a0 t
  7. 96.0
复制代码

6 k* t2 O& g6 G! X$ Tmath.log10(x)  返回x的以10为底的对数! }) _9 Q6 c  ?8 }' g+ @
  1. #返回x的以10为底的对数0 y" B. _$ c9 a! _% N  g
  2. log10(x)
    . T  O* H5 e! y" Q! i6 L
  3. Return the base 10 logarithm of x.
    % r' U6 a9 I/ K. J1 a! S, [1 i0 f
  4. >>> math.log10(10)
    / k& O$ R4 u. V" p8 K
  5. 1.0
    5 ]$ G  X& S; d- e
  6. >>> math.log10(100)
    6 ?! D( h% B3 j) j/ C0 }, w4 L6 Y
  7. 2.0
    - _8 ?8 ], a* |, H) ^1 A2 i7 `+ Y
  8. #即10的1.3次方的结果为208 y6 q+ w8 y7 R/ L$ }- i& J
  9. >>> math.log10(20)
    7 ~$ o# A0 ]) }
  10. 1.3010299956639813
复制代码
/ N+ Q& N: D, L3 i) d
math.log1p(x)  返回x+1的自然对数(基数为e)的值1 @) _& q5 J, g( N
  1. #返回x+1的自然对数(基数为e)的值
    ; q$ F% G8 r4 p% U' p  P
  2. log1p(x)
    * m7 [  I8 V) k, z/ h
  3. Return the natural logarithm of 1+x (base e).3 r5 {& _, L" J. q6 W1 |
  4. The result is computed in a way which is accurate for x near zero.
    7 q2 j) \* {1 E
  5. >>> math.log(10); H/ ?% g1 z  Z8 v6 k3 _$ {
  6. 2.302585092994046
    1 g: l( ~3 g5 r5 A& p" B" k
  7. >>> math.log1p(10)
    8 ]& n* x, @6 N4 T7 b# g% M
  8. 2.3978952727983707/ X' r- E! Z4 U1 S# r4 z' F
  9. >>> math.log(11)/ d- }" s* g' f2 e4 a& \8 N4 N
  10. 2.3978952727983707
复制代码

) y4 u! A$ c5 D1 W$ k+ f9 cmath.log2(x)  返回x的基2对数
( M; D: |2 o( z: n
  1. #返回x的基2对数
    - [0 b4 d' i+ K) C7 o
  2. log2(x)# R* h5 V; K, ]
  3. Return the base 2 logarithm of x.
    7 @9 r5 H  `$ \, h
  4. >>> math.log2(32)
    ( m4 _* A1 C% N- h, h; u' y
  5. 5.06 P% {0 D- n$ I+ Y) a* p2 n
  6. >>> math.log2(20)
    ; o+ @% p' m$ j4 H* M( Y
  7. 4.3219280948873631 W4 y) ?# ?! T) _
  8. >>> math.log2(16)
    . o+ y3 A9 l" V2 J  O
  9. 4.0
复制代码
" _5 L4 v3 K' X# g* j
math.modf(x)  返回由x的小数部分和整数部分组成的元组
, D; B# x1 U1 z  I- {# [! y
  1. #返回由x的小数部分和整数部分组成的元组
    $ [" t! L! K% w; D# c  `8 O/ o
  2. modf(x)1 c2 c& }! A# m9 \
  3. Return the fractional and integer parts of x.  Both results carry the sign- a/ B! w5 R  q3 A! P
  4. of x and are floats.
    / q/ p; H, }# V7 m7 X: J
  5. >>> math.modf(math.pi)4 Z' ?4 h2 g* Z
  6. (0.14159265358979312, 3.0)/ g. q9 d; A5 W0 N2 p
  7. >>> math.modf(12.34)
    " v: z0 _' Q) f/ J2 Z
  8. (0.33999999999999986, 12.0)
复制代码
: [7 p# B- K5 k5 v% [% ?
math.sqrt(x)  求x的平方根( P6 O0 p& Z. e3 G- {/ ^( w
  1. #求x的平方根
    , E0 V9 m5 n3 A0 m& ^, h
  2. sqrt(x)+ |0 h( G& F% \0 e
  3. Return the square root of x.
    / W' {+ k+ W# E. h. O) M9 n2 \9 B
  4. >>> math.sqrt(100)
    2 X( l  Q- K# B7 }0 {7 K
  5. 10.0
    & D9 {3 T6 ~/ ^; E7 A; |
  6. >>> math.sqrt(16)  P& y  ?! V2 K# }8 G$ @! Z" x
  7. 4.0
    5 N7 V) u0 Q, s9 l% o5 N
  8. >>> math.sqrt(20)+ g( h. l' v0 K) @
  9. 4.47213595499958
复制代码
% b2 `7 [; z# x- d8 t
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分4 R8 {' X  b: v, b
  2. trunc(x:Real) -> Integral! z' K( O- @/ J8 u
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.. Y& j; P# J  _5 p; \) s
  4. >>> math.trunc(6.789)( r1 ]8 b0 p$ T+ U3 {2 h: J
  5. 6  N5 s" }7 w+ a. g
  6. >>> math.trunc(math.pi)) U( H! P; @, v( {
  7. 3
    ' c# B" O. \9 Y- S! l: d
  8. >>> math.trunc(2.567)
    # k; D+ [3 C, ?/ O4 C4 _9 q0 Y2 x
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-5-30 14:42 , Processed in 0.082470 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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