新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

查看: 1979|回复: 0

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

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

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

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

x
; B; ?8 q6 E8 n/ J
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' n, c9 G% S2 U7 r  ?
* l/ W' {( c. x; p
方法12 L1 r$ P& I6 r& m- C
  1. >>> import math
    7 v5 }) f& ^2 }
  2. >>> math.sqrt(9)
    5 Q3 ?. w# l/ p$ P+ ?
  3. 3.0
复制代码
方法2
% k6 S4 u% b' r3 Q1 |9 V# c
  1. >>> from math import sqrt
    ! i: q  Q& L  v. V9 d- L
  2. >>> sqrt(9)4 Z8 f, I: C0 `5 M8 A
  3. 3.0
复制代码

% c3 ~5 ^" @. L9 L6 j# E, S

" r9 Z8 P9 K# S7 f
math.e  表示一个常量
5 r% @9 L2 e$ e- K" U. g2 o/ l  Z
  1. #表示一个常量8 P% w% ^; |& J! b
  2. >>> math.e
    / J5 ]: ^* Y  ^6 V1 P- \- b" ~
  3. 2.718281828459045
复制代码
4 T, }0 R- _& J. z1 Q/ c$ P
math.pi  
数字常量,圆周率

/ I9 ?2 [0 s; ~% k5 z
  1. #数字常量,圆周率' h; V+ G: }2 F2 E8 ^) S
  2. >>> print(math.pi)
    0 h0 h, S2 a. }: A% w& b
  3. 3.141592653589793
复制代码

7 D; h! t7 m1 V/ @9 j8 W, B) Amath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 W( z- [9 |7 W, x2 b
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x( J7 T0 `7 [6 W2 n/ ]: X1 j
  2. ceil(x)& R3 t9 q; j& ]* D- J2 i
  3. Return the ceiling of x as an int.
    & J1 C3 s9 [% u1 p5 G! `
  4. This is the smallest integral value >= x., g0 }4 k# a5 F

  5. 4 L" D4 c& r/ b6 x( [9 {
  6. >>> math.ceil(4.01)! k) g0 x$ W6 q% X; V, ^
  7. 5
    2 O+ Z7 e$ u; G
  8. >>> math.ceil(4.99)4 Q/ m7 E8 o) [* J( C2 X- {# ]2 E
  9. 5
    $ R" Q1 W% k. v7 @7 H7 h, Y: N
  10. >>> math.ceil(-3.99)
    1 ?; Z4 W# w( t1 D
  11. -3
    / d* ]$ `9 R9 P6 u' s, |
  12. >>> math.ceil(-3.01); V! A. q/ W$ [5 x  S2 ~
  13. -3
复制代码

' |& I5 V' \  ~9 p4 a+ mmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身& c$ r* m7 N9 K; D' I, u7 e
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- d& ^( ^) z, C5 b/ C4 j
  2. floor(x)
    " p$ i* F" P+ F9 O
  3. Return the floor of x as an int.
    ! N# @. L. m3 V- |
  4. This is the largest integral value <= x.1 M; G" }  c: H/ \
  5. >>> math.floor(4.1)6 I+ f4 l( [: i" J3 |
  6. 4
    " p' @1 R: e/ [' @- D
  7. >>> math.floor(4.999)+ _+ b4 _4 Z1 s9 N2 m) M, N
  8. 44 Q% Y% k9 I- ^5 x; x) _
  9. >>> math.floor(-4.999)
    ; G% i* J6 @5 m. P: F/ t5 w
  10. -5/ n- W' Q. S: p* \
  11. >>> math.floor(-4.01)
    ! T2 Y. J7 g# d2 r" O5 V6 t7 e$ u
  12. -5
复制代码

' T8 B6 V. [" bmath.pow(x,y)  返回x的y次方,即x**y
; c& a- w$ {9 Q* q
  1. #返回x的y次方,即x**y
    . @9 B) r* W% s" A( i2 P' r5 P# z3 w
  2. pow(x, y)
    / {9 |* E/ x$ k0 J' D' w
  3. Return x**y (x to the power of y).
    % K1 z' ^) a% J: ~! L0 k
  4. >>> math.pow(3,4)3 E1 O: }7 w4 G8 V, m
  5. 81.0
    % u+ m. Z' V5 a8 h. F
  6. >>> 3 p7 b8 C. ~, m: i' q' M, b9 m
  7. >>> math.pow(2,7)
    % o1 c! u4 K1 Y9 f, D
  8. 128.0
复制代码
5 P. y$ E2 A1 ]8 g
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% ^: M" R& [  t$ k8 f
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" Y; T6 N7 Y$ I% ]
  2. log(x[, base])
    1 C! i4 _4 H- N- Z
  3. Return the logarithm of x to the given base.
    / [1 |5 P7 x  w2 a( T: b4 x
  4. If the base not specified, returns the natural logarithm (base e) of x.4 y+ w1 j0 m' G2 x
  5. >>> math.log(10)
    ( q8 _, m* n5 l5 C& v
  6. 2.302585092994046; K! k0 h4 i! V
  7. >>> math.log(11)
    + L8 r7 p9 v9 k# {2 H6 o6 I
  8. 2.3978952727983707$ f. g" R% e" Q0 \0 j5 Q: @
  9. >>> math.log(20)) @3 x1 ~( S$ J! M; Y) a# N
  10. 2.995732273553991
复制代码
" D, L) f9 L& f
math.sin(x)  求x(x为弧度)的正弦值# @1 z4 P" m9 K: m; ^; T  O. E4 k
  1. #求x(x为弧度)的正弦值6 w+ j' {+ H( t$ b3 W( `6 ]+ h
  2. sin(x)
    7 a' @( F7 H. b
  3. Return the sine of x (measured in radians).
    : F6 |" N' j# O0 D1 ]
  4. >>> math.sin(math.pi/4)
    % r$ W2 R4 F0 a4 A: E
  5. 0.70710678118654756 ^+ B+ u* ?' l
  6. >>> math.sin(math.pi/2)8 m. d& O  C8 ]' ~+ P) W
  7. 1.0
    , i' m3 F" }' w" y/ ], i
  8. >>> math.sin(math.pi/3)2 Z5 J* R3 A) V# B  f
  9. 0.8660254037844386
复制代码
; s  m/ R# Y" c, M
math.cos(x)  求x的余弦,x必须是弧度
8 ?5 I5 w  i5 a4 Y* R* ~# a7 y0 O8 B
  1. #求x的余弦,x必须是弧度' s- t2 s+ H. B: {" I
  2. cos(x)! l% E: T  H3 f! w+ O3 Y
  3. Return the cosine of x (measured in radians).% D" d- M. O9 v# @5 ?. C
  4. #math.pi/4表示弧度,转换成角度为45度/ c, ?, l5 g8 [; M. R
  5. >>> math.cos(math.pi/4)  W) [4 v, _) r' {9 e7 w* q
  6. 0.7071067811865476
    5 |3 u# L( _. O6 _
  7. math.pi/3表示弧度,转换成角度为60度
    : m) P5 M8 |) ]6 ]
  8. >>> math.cos(math.pi/3)
    6 m% M; Y" L; K5 e; ^
  9. 0.5000000000000001
    0 z" k( U( w, v2 c: g+ I4 C7 i
  10. math.pi/6表示弧度,转换成角度为30度
    + T8 j% n8 ^+ G2 H" D
  11. >>> math.cos(math.pi/6)
    7 _1 U' a0 q; M
  12. 0.8660254037844387
复制代码
  I8 @( J2 C1 H8 ?8 w
math.tan(x)  返回x(x为弧度)的正切值
  t9 t% q+ Q# f
  1. #返回x(x为弧度)的正切值; f( R7 ]- e" u/ h$ j. U
  2. tan(x)
    # X+ ^1 Q+ Q" S0 ~+ V/ p0 z8 [
  3. Return the tangent of x (measured in radians).0 M5 ?$ l' i2 V" M
  4. >>> math.tan(math.pi/4)
    ! k* J  c$ Q/ ?3 ^* }
  5. 0.9999999999999999
    6 O4 Z- F3 a: @: H
  6. >>> math.tan(math.pi/6)2 |* Q2 [' _* ?% H& i- \
  7. 0.5773502691896257
    6 y' E0 f8 L) @/ P3 y+ c  {9 X: u
  8. >>> math.tan(math.pi/3)1 Y3 [' T$ U2 p6 B
  9. 1.7320508075688767
复制代码

4 @. Z3 ^+ i7 V* ]. o1 wmath.degrees(x)  把x从弧度转换成角度& N* }( x5 ?- K9 f# B
  1. #把x从弧度转换成角度
    - ?# A! u2 @4 @" h
  2. degrees(x)
    ; x& G; l) U& l: C( V
  3. Convert angle x from radians to degrees.8 X: w% c+ Y5 s* {% O* r
  4. 7 Q2 B* y& ]! ]) q$ w( j
  5. >>> math.degrees(math.pi/4), E2 H0 z; P& R8 y* b' ]$ e
  6. 45.0/ l) U, G: o/ g0 ]/ n3 W
  7. >>> math.degrees(math.pi): a; X& ~: I2 K/ R( ^) I
  8. 180.06 X  G; g) {0 q) I& D5 z
  9. >>> math.degrees(math.pi/6)
    # v) S" j: c, H/ d6 n+ I6 M3 {7 R3 I
  10. 29.999999999999996) ~4 {3 f: @4 y6 f: I' R
  11. >>> math.degrees(math.pi/3). a$ X: r- n4 Y" Z0 F0 p, K$ g
  12. 59.99999999999999
复制代码
# H8 |8 j$ |; y! A* ?
math.radians(x)  把角度x转换成弧度
- H1 J: L+ B" A6 E) }
  1. #把角度x转换成弧度
    6 X1 H, H+ w* G/ n! P9 p& a
  2. radians(x)
    / n" e% A1 O; r" H
  3. Convert angle x from degrees to radians.) n& {3 z/ z; _3 X. y  M
  4. >>> math.radians(45)
    ! ^' I& s9 R; ^+ c& [
  5. 0.7853981633974483
    $ m1 t' Q& c9 E* C
  6. >>> math.radians(60)
    7 s; `1 y: A5 D7 X2 E
  7. 1.0471975511965976
复制代码

$ z* z3 T# v, t% v8 F2 omath.copysign(x,y)  把y的正负号加到x前面,可以使用0) b0 V0 I9 C" P1 j  i1 l
  1. #把y的正负号加到x前面,可以使用0
    ) p& A4 }: v% r1 |) x# ^( T
  2. copysign(x, y)
    $ T/ D  p: D/ ?6 p2 [
  3. Return a float with the magnitude (absolute value) of x but the sign : Q% z2 K) i; {
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) , y8 i0 Z9 Y2 X  N$ ^8 L
  5. returns -1.0.
    3 B, V0 T* V" }3 C

  6. 5 C$ t# }. r: |  H  S: r; ^& `- D( x
  7. >>> math.copysign(2,3)5 S  t7 o9 M( y% U; @
  8. 2.0
    2 F) ]& f2 |8 Y" y
  9. >>> math.copysign(2,-3)
    8 l0 K/ V5 ]' E. J; @4 u0 S
  10. -2.08 S  U  E% W$ D9 ^$ b1 W% Q# l
  11. >>> math.copysign(3,8)6 a# X4 }) h5 M3 T6 _
  12. 3.01 v5 x9 m+ P( {
  13. >>> math.copysign(3,-8)
    " u% |( [$ y' _* }/ ^, F
  14. -3.0
复制代码
4 X  }. a! g0 [  ?5 n
math.exp(x)  返回math.e,也就是2.71828的x次方
* z. d/ t" F! _) U  ?$ h3 n
  1. #返回math.e,也就是2.71828的x次方
    1 p7 c% d8 p6 R( W% }
  2. exp(x)4 V# j+ r" z( ^/ r7 W
  3. Return e raised to the power of x.
    5 [$ v' A' J3 Y* L  ~: U5 k' z8 |
  4.   D' ?/ Z  @$ r+ O$ q  o( S
  5. >>> math.exp(1)
    " |- l& f% Z. C! A5 Y9 S8 l) S
  6. 2.718281828459045; {/ c: E0 E7 u! e
  7. >>> math.exp(2)6 M/ r3 H2 M& o8 H! [1 r# k
  8. 7.389056098930653 ~( |7 F  {$ [' v9 s& Z
  9. >>> math.exp(3)
    2 s5 b" }+ L- X7 T
  10. 20.085536923187668
复制代码
. F% r& w3 ]& S2 F
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
. w8 }# }5 a$ b: p9 o2 o3 |
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ' {# u4 z  T( u2 W/ V/ X
  2. expm1(x)' n6 h  J; m3 [' ^. J% {% D  s
  3. Return exp(x)-1.  C5 B' j+ Q4 g4 v3 y# d
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    % \3 ?9 ?( B% Q/ l
  5. 6 I# B, `4 I1 c4 l. N$ Y
  6. >>> math.expm1(1)
    3 a2 t2 W( s/ _6 z
  7. 1.718281828459045
    7 j$ n, q) T7 s& D* [9 j
  8. >>> math.expm1(2)% G5 r- K6 H" z5 E: q1 h0 [. K8 I; l
  9. 6.389056098930654 S, A: x! `1 W, D$ u/ t" q
  10. >>> math.expm1(3)
      \( ~* E9 S4 B/ `
  11. 19.085536923187668
复制代码
1 o  |% S. I9 j* A3 @( Z  a
math.fabs(x)  返回x的绝对值
/ _' |5 `. E) u4 z/ N
  1. #返回x的绝对值
    ! Q6 K  j; I# x' N! M6 |+ o
  2. fabs(x)
    , n! H; e' F0 z1 O/ X; x
  3. Return the absolute value of the float x.
    & w) z5 ~+ m2 p" T4 F6 Z

  4. 3 M7 w3 s) x' X2 ?
  5. >>> math.fabs(-0.003)
    $ ?  n, Q4 g8 c) F( ~* h
  6. 0.003  B9 O. g4 f: J
  7. >>> math.fabs(-110)
    . D/ {' s: M0 p9 w4 g% W9 b
  8. 110.07 Q9 x0 y0 I, O6 ^% G7 |  U1 H' Q
  9. >>> math.fabs(100)
    ( o2 f- @* N& h& |( L0 R! B# v
  10. 100.0
复制代码

" ~- C6 R: C9 w$ Pmath.factorial(x)  取x的阶乘的值
( d: U$ C0 m; D  L& g2 g" {7 a
  1. #取x的阶乘的值- x" {* ], P9 Y) H# v/ x4 u+ S) h
  2. factorial(x) -> Integral
    ' H! Q& @7 O+ C# @( H+ P" K1 P: ^; f
  3. Find x!. Raise a ValueError if x is negative or non-integral.0 L6 |+ l$ N# H9 f
  4. >>> math.factorial(1)
    3 @9 L) F/ n4 |" D/ d7 b0 S
  5. 1" K+ g/ G6 }# E& s
  6. >>> math.factorial(2)
    $ A+ t4 y( |& p* v. p4 m1 L: F5 R
  7. 2
    3 a6 u9 T) k7 g0 {* }0 X
  8. >>> math.factorial(3)$ ^& A0 S3 o8 X. L. k. }
  9. 6
    % }; U5 S! w. p& [( _' Q) k
  10. >>> math.factorial(5); j/ b( q* z( e4 d
  11. 120
    & @1 o4 t, {2 q7 s+ \
  12. >>> math.factorial(10). x. @5 }0 w* L. [
  13. 3628800
复制代码

: a# Q$ {  Y- @0 `math.fmod(x,y)  得到x/y的余数,其值是一个浮点数/ q* s( O) N( C& l, Q- N$ {$ F
  1. #得到x/y的余数,其值是一个浮点数% C$ ]5 n# F5 H* ^; x$ j
  2. fmod(x, y)
    9 J# @# Z2 X! N( ?1 ~  r
  3. Return fmod(x, y), according to platform C.  x % y may differ./ [- t# L9 T' H% \9 a1 a
  4. >>> math.fmod(20,3)( w! j9 H) I# K
  5. 2.0" |. ^* s# d( |
  6. >>> math.fmod(20,7)
    ' u! }' Z& _7 x! p, Z) }, X
  7. 6.0
复制代码
; `$ j8 b8 b- K2 y3 y) y& P
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围  W5 @9 Z. ~" k# t
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ' o( @, ]8 c; q7 O' L9 C' z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 f$ Q5 q- D  V! G, k% F% O
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    & m0 ]9 b& U" S# g0 S! l! u
  4. frexp(x)  {% k1 M! A* Z: {# c: X
  5. Return the mantissa and exponent of x, as pair (m, e).
    6 B+ |6 z6 |* U9 ^
  6. m is a float and e is an int, such that x = m * 2.**e.
    - r0 O1 [* _# }$ w( [& H3 l
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    - @& j- m9 ?, r. ~2 s
  8. >>> math.frexp(10)/ n" O6 A/ e7 f/ c- f
  9. (0.625, 4)1 b- u0 r7 x5 f# x
  10. >>> math.frexp(75)) n/ w, d6 d- O8 |2 |) l. W9 _
  11. (0.5859375, 7)2 B4 B* U+ |& a1 m5 h! x. p, Y2 c9 X
  12. >>> math.frexp(-40)
    $ \# R. C5 a" L$ p$ p4 S
  13. (-0.625, 6)' l9 V. [3 f5 g- P6 L0 G3 P& ^
  14. >>> math.frexp(-100)* D' [+ V' O9 g% x5 X
  15. (-0.78125, 7)
    0 U! b8 X' S% W$ [$ D
  16. >>> math.frexp(100)
    ( b" z0 p/ F: p+ u& d0 v
  17. (0.78125, 7)
复制代码

/ h+ Y( A" e7 qmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
/ j) t7 U' x( w" j% o
  1. #对迭代器里的每个元素进行求和操作6 [/ ?$ z1 j4 r6 V& f% k9 W, N! U  b
  2. fsum(iterable)
    7 h) X  X! X. z* [
  3. Return an accurate floating point sum of values in the iterable.' O8 f  O7 z8 h3 U* h5 D6 Q6 M- @
  4. Assumes IEEE-754 floating point arithmetic.
    ! k% I( C0 Y; ]
  5. >>> math.fsum([1,2,3,4])3 c; _" A1 a% X" o  r! |; T
  6. 10.00 @$ G* a9 I6 ^5 O' e( g1 V+ a8 d
  7. >>> math.fsum((1,2,3,4))& a. \" p) Z3 P' ~
  8. 10.07 b8 r7 l0 N6 g9 R0 N0 S4 b
  9. >>> math.fsum((-1,-2,-3,-4))
    8 |* W- ]9 ~1 @0 W3 F
  10. -10.09 v" _# a0 h" Q( p( v0 z' t2 x9 o
  11. >>> math.fsum([-1,-2,-3,-4])
    ! a- J8 V; W) t  V, _
  12. -10.0
复制代码
+ x" F$ D5 Y# {9 o& P
math.gcd(x,y)  返回x和y的最大公约数
) a, p2 o' {$ F0 f
  1. #返回x和y的最大公约数; l8 I; w9 P- Z  p3 d
  2. gcd(x, y) -> int1 l4 x3 @+ k" P% m) \0 l. f
  3. greatest common divisor of x and y1 k. S4 z7 h. R6 N1 s
  4. >>> math.gcd(8,6)
    + n6 o1 U; c! G) b
  5. 28 ], @7 L- t" p9 x8 L# ?6 @
  6. >>> math.gcd(40,20)
    ' x% I( X$ t" y1 N  Y  O5 I& [
  7. 20, v: |2 K; q2 L6 g3 l
  8. >>> math.gcd(8,12)
    ' O/ Q) l* E3 K7 y( R
  9. 4
复制代码

1 o1 Q6 M$ ^) T: c! ]1 R8 J( Qmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False4 P  C8 o2 ~& K; w/ {
  1. #得到(x**2+y**2),平方的值
    6 ^3 E# y+ `+ O; E# t* N+ K
  2. hypot(x, y)
    ! G- p8 d; r- W- A" U& [6 p9 Y3 h! Q
  3. Return the Euclidean distance, sqrt(x*x + y*y).! H: e/ d5 F8 [" p5 [2 {$ b) {
  4. >>> math.hypot(3,4)
    2 U% I. G* y$ B% q- _% b
  5. 5.0/ o& Z3 H  x% M4 D5 A: r8 @2 r
  6. >>> math.hypot(6,8)
    ' f- X0 e1 N- I* p% T% I: E
  7. 10.0
复制代码
' ]# o9 X3 K; v! X5 m* C
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 G# |7 \: Z9 Y* W$ x( A
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    . P" v: u' `+ l' W* _' p7 G' t# {" W  y
  2. isfinite(x) -> bool0 m& O' r& H# l1 W" X2 v, A
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    4 ]" z2 s  N+ u1 z- G
  4. >>> math.isfinite(100)* ~: o- R6 W0 w* ^2 e6 \
  5. True
    " m0 [7 h/ w2 l* H. K
  6. >>> math.isfinite(0)
    2 H$ m: E% s0 y
  7. True
    0 N* i* ^) J+ M0 s; j! j% W5 ~
  8. >>> math.isfinite(0.1)
    % }5 U& ]# W, ?; f3 |- u
  9. True$ b- M2 v; T; h1 _5 \8 c
  10. >>> math.isfinite("a")) T/ s0 ?' O) f, M/ ]  e+ o
  11. >>> math.isfinite(0.0001)
    7 N% t) q/ f+ o. E4 h) ]
  12. True
复制代码
( O; _" Q6 y' `0 s3 T
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False, ]7 W' j& g% Z/ X3 N0 m
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False7 X! A- U2 s: I5 N, z# L, E
  2. isinf(x) -> bool
    3 [+ r4 t" z+ {& i, f2 s( J
  3. Return True if x is a positive or negative infinity, and False otherwise.( t7 ^. S# B  l4 v5 J2 {$ N
  4. >>> math.isinf(234)
    # h$ F" L+ c5 G3 n
  5. False
    0 N! d2 p0 s, ?( Q
  6. >>> math.isinf(0.1)% W3 s/ y) `+ J
  7. False
复制代码
7 \' V, V6 A' d
math.isnan(x)  如果x不是数字True,否则返回False  B4 g, L4 I7 b  l
  1. #如果x不是数字True,否则返回False
    # G, ^9 a% }6 I! N3 L5 P
  2. isnan(x) -> bool
    1 R& N& t4 f$ v% o9 \. l8 l2 P' x
  3. Return True if x is a NaN (not a number), and False otherwise.
    : _9 Z) n4 z- o) X: u
  4. >>> math.isnan(23)
    $ v5 z. N- t$ ]- U1 e
  5. False
    5 R3 ?  z( q/ Z) ~7 P
  6. >>> math.isnan(0.01)8 }; v0 f/ {* R
  7. False
复制代码

" h1 r' \- l; v4 h6 c9 P0 |0 ^math.ldexp(x,i)  返回x*(2**i)的值+ x; F# m# I* p
  1. #返回x*(2**i)的值
    7 k" f" w" K8 ?( m3 q* ?) s
  2. ldexp(x, i)2 a! J" j- d' h/ D% L6 B
  3. Return x * (2**i).
    * \% ^. W4 `. C
  4. >>> math.ldexp(5,5)
    & t( L* A# b9 H" y
  5. 160.06 `$ d& r: C1 b+ d% X! c9 ^4 p
  6. >>> math.ldexp(3,5)1 T% i! m6 R5 s& Q9 X( V
  7. 96.0
复制代码

7 g) D7 v/ _, ~7 r2 l' d$ X% rmath.log10(x)  返回x的以10为底的对数
/ u" j5 J6 v2 u8 a
  1. #返回x的以10为底的对数5 L8 T1 Z0 ]2 Q" R4 X* }
  2. log10(x)7 K( o* v) s) G6 t; L8 C
  3. Return the base 10 logarithm of x." ]/ A/ n2 }, L9 }
  4. >>> math.log10(10)
    ( T. }5 ~" ^- k% n& x) v/ h
  5. 1.0- i0 L" N* l+ i0 q
  6. >>> math.log10(100)
    : l2 u3 H3 n2 D/ t' |( S# T2 L
  7. 2.0
    * r) r$ c  X2 L1 H* y* d6 ^2 Z
  8. #即10的1.3次方的结果为20: b3 T$ Z0 e' }% E) @5 z
  9. >>> math.log10(20)
    5 ~) O/ ^5 d- T* _( t/ O8 h4 d
  10. 1.3010299956639813
复制代码

# I6 F! p% c/ ?) J- q2 P; kmath.log1p(x)  返回x+1的自然对数(基数为e)的值9 S; G( p, U2 n; p  i! `
  1. #返回x+1的自然对数(基数为e)的值
    : D; j% g# F, t, J$ S$ j
  2. log1p(x)5 r$ l% I, P$ _5 ?, a$ v
  3. Return the natural logarithm of 1+x (base e).
    $ I$ _1 Q' ?3 Y7 `
  4. The result is computed in a way which is accurate for x near zero.
    . f# \9 Q4 g5 g
  5. >>> math.log(10)
    / H9 Q; u2 _$ X, o: B6 ^
  6. 2.302585092994046# l( s; C* r  K
  7. >>> math.log1p(10)
    , y: R4 g. _; q7 k9 H- G8 q
  8. 2.39789527279837078 a0 L& V7 [+ y4 e6 r
  9. >>> math.log(11): s. m) W" c4 H- @  h7 s
  10. 2.3978952727983707
复制代码
* \3 K& S. `0 ~4 u3 q
math.log2(x)  返回x的基2对数; f, k* {, I0 K$ b% E2 \
  1. #返回x的基2对数  V6 m2 ]- U( {8 f2 Y9 I; c8 S/ ]
  2. log2(x); J( u) C  p7 h8 z
  3. Return the base 2 logarithm of x.
    7 C! J% u) g; A. |0 E' I
  4. >>> math.log2(32)6 Z& h- z7 K, n
  5. 5.0
    1 H5 P$ B$ c* E! \; r. p+ N: O
  6. >>> math.log2(20). m: q% d" @. ~3 i1 o  `6 x' m
  7. 4.321928094887363
    5 z/ p2 q0 Q9 V
  8. >>> math.log2(16)
    # W& {- y0 K) P6 t$ n  I  Q4 Z" P
  9. 4.0
复制代码

5 R. w3 k, n1 w( N8 @4 O9 Umath.modf(x)  返回由x的小数部分和整数部分组成的元组( t$ E+ Q& E, J4 j6 R
  1. #返回由x的小数部分和整数部分组成的元组
    # Z8 f  C! S; s4 d: A
  2. modf(x)
    - R' `' s5 v2 [$ }
  3. Return the fractional and integer parts of x.  Both results carry the sign0 j: [' ]* M) z, W8 |5 T
  4. of x and are floats.
    1 G/ Z3 j8 t5 V) m3 Z$ [' ~9 k
  5. >>> math.modf(math.pi)
    0 [8 X$ G" M+ I! X5 G; ], O' @
  6. (0.14159265358979312, 3.0)
      s+ K% t1 R# n7 t( ?: Z2 K0 n) E
  7. >>> math.modf(12.34), P2 E: j6 k% k) l. k1 q2 K& Y* ~
  8. (0.33999999999999986, 12.0)
复制代码
* A6 C5 `* F: n% x; r( o' ]' j& r9 b
math.sqrt(x)  求x的平方根
' a5 q% ?6 m! {
  1. #求x的平方根
    8 n2 u; r: p( \
  2. sqrt(x)2 D4 w6 e* W/ R' R. ?
  3. Return the square root of x.
    . F, z$ j6 X! z. _* h
  4. >>> math.sqrt(100)
      @" e" P* J/ i; v2 W9 E, A% B
  5. 10.0( w9 F' C4 |9 o5 j: V
  6. >>> math.sqrt(16)4 Z# a; L- q5 d3 E2 P2 }. a
  7. 4.0; e* A6 u2 U  j) o' K5 J0 }) ^( v
  8. >>> math.sqrt(20); T) H* n/ P9 J8 P; S6 I( T
  9. 4.47213595499958
复制代码

+ p( B* [1 s" K' {math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分) u1 m* C7 ]4 Y; ]2 X9 \' u
  2. trunc(x:Real) -> Integral
    ) y, }6 `5 G: \; }' k/ U; U
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.& Y6 b2 k! E/ }0 n. f" o
  4. >>> math.trunc(6.789)0 A2 g; W* s: T" j" ^! T+ Q
  5. 60 m% F9 S" ]2 r: J7 H( Z
  6. >>> math.trunc(math.pi)
    3 Y5 E2 d/ X) E( b# Y/ C
  7. 3/ Z8 l7 x; k" v1 z* l
  8. >>> math.trunc(2.567)
    / S6 f4 y6 ^  q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法
新大榭Python学习社区培训、Excel业务指导、办公软件定制、网站建设;新大榭探索实验室欢迎您!http://lab.daxie.net.cn/
Q群推荐 大榭本地求职招聘QQ群,欢迎转发分享本地招聘信息资讯! 官方招聘1群(已满);官方招聘2群:315816937 *
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-4-15 13:22 , Processed in 0.091107 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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