新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

) Y2 _8 |6 v5 r【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 D+ P& ], @! B
9 d1 V6 T0 g' T" J/ b$ j
方法14 l2 n6 S2 t$ l6 s% q
  1. >>> import math& h3 M1 a& P& @8 W# P
  2. >>> math.sqrt(9)
    + @$ u. @- L4 b6 x& _
  3. 3.0
复制代码
方法2, c- n: a8 s) p4 ]4 X# g
  1. >>> from math import sqrt5 r) R! {& g9 P  |
  2. >>> sqrt(9)0 V& s& c) }) G7 H$ a& I& `; w
  3. 3.0
复制代码
* X" S) {8 y+ K5 C3 a- ?

: w5 ^0 t0 V) k2 h" K9 ]
math.e  表示一个常量. f* J; D7 ^- P2 a& Z/ U. Z
  1. #表示一个常量
    , {6 u1 U& r$ H* i/ B8 h; a0 O
  2. >>> math.e2 V' X1 z6 F& ?7 \0 |: l
  3. 2.718281828459045
复制代码
5 v+ X$ O" H" b7 X9 P/ ~
math.pi  
数字常量,圆周率
* q1 P  K2 S5 b# h6 h
  1. #数字常量,圆周率
    , U9 [% w$ [- S: I
  2. >>> print(math.pi)7 h- [" z8 o, T7 Z3 e
  3. 3.141592653589793
复制代码

# N2 P6 g8 A) x4 m0 Z) `+ [math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

/ w% l+ `: |: ^7 g2 J. `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ! |3 b4 W! V2 \6 E  Y  U) O) W
  2. ceil(x)9 v' t9 a& ^4 s1 m1 x! D; a
  3. Return the ceiling of x as an int.
    ! J; N1 J  @/ b4 N
  4. This is the smallest integral value >= x.
    ) E" H2 t' ^$ I5 u; ?

  5. 3 ]8 L0 h7 w, a+ o
  6. >>> math.ceil(4.01)
      p- X0 i8 K, d" Z4 n  O7 w& @
  7. 5% B( y1 s' ^) O  d: _7 s% j; r0 C
  8. >>> math.ceil(4.99)
    - B- t4 ]" ^/ I: O# ?) m% ^
  9. 5. X! [* r9 O0 x$ \
  10. >>> math.ceil(-3.99)
    # \' a2 i- H4 A2 N
  11. -3- o$ R- K  U9 u& q
  12. >>> math.ceil(-3.01)6 ?- k0 _1 R' E, S/ _) z
  13. -3
复制代码

% y: N5 k; q+ U. r2 Y' k* {math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ I+ |0 S: W- b6 R3 Y2 ], g
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 g1 v  N  `( q2 X7 h  u) S8 Q
  2. floor(x)
    5 m3 m$ U' ~% K4 b. N' V
  3. Return the floor of x as an int.3 h# |% z& T/ d, y* m- \+ G+ ]
  4. This is the largest integral value <= x.% E+ U+ _3 ~4 W8 ~1 V
  5. >>> math.floor(4.1)
    " W+ o: \( Q" M- i! @
  6. 46 f6 b; P% N- D& f2 R
  7. >>> math.floor(4.999)
    * ]) p5 P6 u& i3 t
  8. 4
    / X7 S. x' Q3 e/ x0 H: c3 w
  9. >>> math.floor(-4.999)
    4 Z+ K' s, s" T4 m+ d  j
  10. -5% v; N4 w. O" f6 `3 u* @9 v
  11. >>> math.floor(-4.01)# O! n. H# w! o* I
  12. -5
复制代码

5 \% s! K8 h2 z" mmath.pow(x,y)  返回x的y次方,即x**y
! C& Q3 M, r) i
  1. #返回x的y次方,即x**y5 |* T( a* x: B2 z0 e* T
  2. pow(x, y). R6 L1 l& A9 |2 d8 t
  3. Return x**y (x to the power of y).
    0 N$ o; e$ _* [& v; W9 f0 }
  4. >>> math.pow(3,4)- J" c) w( i: K8 l9 m+ r- v
  5. 81.0- q1 W) |+ u  G6 c" f+ }$ ]
  6. >>>
    + Q2 J& p! x0 M' b/ A* x% y
  7. >>> math.pow(2,7)
    : `' w  U" N4 x6 t& G2 Q; ?- ]  }
  8. 128.0
复制代码
7 p- C* R5 P; n* p$ g
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' o- j% q# H5 l1 a3 v
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% H  |$ P2 M- C5 a
  2. log(x[, base])
    4 c9 ?' d5 E, n; m" p: x8 B8 y
  3. Return the logarithm of x to the given base.
    ) M0 n! S  v, V0 j1 ~8 m5 w
  4. If the base not specified, returns the natural logarithm (base e) of x.
      u$ N1 B; z! |
  5. >>> math.log(10)% |5 c% N" ?# g) s
  6. 2.302585092994046) e5 U7 D3 f, `) L
  7. >>> math.log(11)7 n4 v9 U  L* j# U; o  k
  8. 2.3978952727983707
    ! V: ]# x% f1 `9 y
  9. >>> math.log(20)
    - k1 c" l0 G# s6 z8 V4 |- p
  10. 2.995732273553991
复制代码

, X) J1 |5 u0 T( T5 p1 Vmath.sin(x)  求x(x为弧度)的正弦值
/ o# ^- b9 a) V# N
  1. #求x(x为弧度)的正弦值3 ]$ ~5 l/ D  |& U6 Y' @6 B
  2. sin(x)
    : n6 ^4 T" N4 W; r& s  T9 c# X
  3. Return the sine of x (measured in radians).
    , d( G0 A- W: t6 G- Z* ]" l
  4. >>> math.sin(math.pi/4)
    0 u5 R7 h1 P# ~/ p8 ^
  5. 0.7071067811865475
    / M; T2 K  l" n3 c! ?4 D7 N  K, ~5 w
  6. >>> math.sin(math.pi/2)7 |& m7 y) m1 J+ n
  7. 1.0
    $ t3 a& g' S) L7 q
  8. >>> math.sin(math.pi/3)
    : L: E% s+ Q8 i# u6 d
  9. 0.8660254037844386
复制代码

7 Y7 O3 l: }8 {/ w! X0 @. j4 jmath.cos(x)  求x的余弦,x必须是弧度
, b! T- x: q/ b; e) {- w6 G
  1. #求x的余弦,x必须是弧度
    - v0 _3 j$ ~6 h/ o
  2. cos(x)
    ! F! j- m0 f$ Q" }" v, T- }
  3. Return the cosine of x (measured in radians).
    : F  q4 B2 ~% ~( U9 u
  4. #math.pi/4表示弧度,转换成角度为45度
    ; ?( J3 u! K/ \% ]# B  ^
  5. >>> math.cos(math.pi/4)
    + B( Y' e3 ^; n% w- f0 H+ ?& E/ v4 X
  6. 0.70710678118654760 \0 `; Q) p. W" D
  7. math.pi/3表示弧度,转换成角度为60度
    " @: q" @$ C7 O& R: S. r, D
  8. >>> math.cos(math.pi/3)
    ; d3 Q5 z# g0 |/ H
  9. 0.5000000000000001
    ; P  l" ~) ]8 k: Z: a1 f8 c0 w; r
  10. math.pi/6表示弧度,转换成角度为30度
    2 N7 p5 ]0 F$ S8 z" W7 x- z
  11. >>> math.cos(math.pi/6)
    4 h3 [0 q9 R7 K0 Z/ A4 h( m4 Z+ T
  12. 0.8660254037844387
复制代码
& S7 O7 V) Q3 g+ n7 B# [
math.tan(x)  返回x(x为弧度)的正切值* o& S( e& M; M) F' F. L% S
  1. #返回x(x为弧度)的正切值
    2 t+ p0 R6 U) |: ^/ Y( _) X0 c
  2. tan(x)8 F. _$ Q, P% Y
  3. Return the tangent of x (measured in radians).% |- x6 y) \! u+ g* z& @3 _
  4. >>> math.tan(math.pi/4)
    8 L& S+ l( c. R- \3 f6 \
  5. 0.9999999999999999
    & _9 m' y3 d& q# M6 U$ ^9 j
  6. >>> math.tan(math.pi/6)% m3 [3 ^7 P% b0 @" K9 x1 m
  7. 0.57735026918962572 L: l+ ?% r0 V9 h3 [  M3 L0 I
  8. >>> math.tan(math.pi/3)
    " |9 v$ \  D& B$ \1 M2 F+ b
  9. 1.7320508075688767
复制代码

1 N* N5 G0 ~2 |math.degrees(x)  把x从弧度转换成角度
* X: P4 M  N. t" i6 X, R
  1. #把x从弧度转换成角度0 I. l2 U2 {) ?! _" K0 F
  2. degrees(x)
    $ I% Q; ^* e1 y
  3. Convert angle x from radians to degrees.
    # u; N9 {# U! C
  4. & E0 P; a" O/ |7 D8 q+ \- g1 N( a- U
  5. >>> math.degrees(math.pi/4)
    3 U' g; [/ e0 T; ]& W
  6. 45.0! F  a) [: I3 o) n, I% e: x' h& J" S
  7. >>> math.degrees(math.pi)
    " H, G7 b$ x, [
  8. 180.0% a( K3 H( P# @; }0 V
  9. >>> math.degrees(math.pi/6)" B- E0 C* J; o
  10. 29.999999999999996
      Z7 G; Q- B) u/ }; Q
  11. >>> math.degrees(math.pi/3)# V- m; x5 t1 V6 K. |: X7 f4 `
  12. 59.99999999999999
复制代码
$ C  {1 {7 h3 _9 |" j0 g
math.radians(x)  把角度x转换成弧度
; U. ]- n- r: z2 `
  1. #把角度x转换成弧度
    ! W/ w1 n1 `% n" ?. O
  2. radians(x)
    4 ~3 R4 Z/ P3 _
  3. Convert angle x from degrees to radians.
    ' [* x% ?% @4 ^. ]. y
  4. >>> math.radians(45)  \  B% L# J, l1 F
  5. 0.7853981633974483
    / n9 {4 B+ }) F* B- ~& j6 h
  6. >>> math.radians(60)
    4 Y( ~) ]) ?) e9 H
  7. 1.0471975511965976
复制代码
7 O) b1 \! d& A' d9 o  u  T
math.copysign(x,y)  把y的正负号加到x前面,可以使用0& s) w! U' W7 O" ?9 N# C
  1. #把y的正负号加到x前面,可以使用09 \4 b# V; X& E$ s- K( H) c0 o
  2. copysign(x, y)4 }  x" y/ X% m& ?1 m9 W
  3. Return a float with the magnitude (absolute value) of x but the sign & `  M4 X. H6 a+ b5 m+ _
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) : t( o+ R& m9 Z7 h+ ?  E; M
  5. returns -1.0.+ q3 Z4 |: Y: w/ `0 T, O/ t6 U9 x
  6. 8 O' }7 @8 E9 J% P' ^  v
  7. >>> math.copysign(2,3)
    * J8 Z; ~0 ~: d, Q$ a6 h0 X
  8. 2.0
    6 @& R! \8 k( ~% b( P( q
  9. >>> math.copysign(2,-3)
    ! p7 O/ d# b8 p6 I7 s
  10. -2.0
    2 o* Q. ~3 W7 d' V8 e! g1 `5 z; K
  11. >>> math.copysign(3,8)
    6 o) A, j$ [0 m5 a$ N- Z! \
  12. 3.06 h, ?* P: q, X, o0 d
  13. >>> math.copysign(3,-8)  h9 T& Q+ a# n& _1 y; ]4 z: @
  14. -3.0
复制代码

+ A$ O9 B. ^! F5 J' H, f/ Xmath.exp(x)  返回math.e,也就是2.71828的x次方0 @0 p$ k! w3 ]" s
  1. #返回math.e,也就是2.71828的x次方
    % q$ y0 a3 L  e, y: b# P
  2. exp(x)
      T# a. ?$ f1 W5 z* g# m5 m
  3. Return e raised to the power of x.3 d8 O  P  Y. o4 S3 k5 V
  4. 7 Q& t0 L  M2 r' C! `1 n5 q
  5. >>> math.exp(1)' x' \7 u. {1 ^4 G6 Y
  6. 2.718281828459045! m( a$ y% o7 {: S, V7 j
  7. >>> math.exp(2)8 p; i3 z9 j, H9 _/ X  w: S
  8. 7.38905609893065
    ' S; {' l: s6 W9 m8 p6 g8 Q
  9. >>> math.exp(3)) Y& k# P% P, g: g( f% K* u2 F
  10. 20.085536923187668
复制代码

( `" T1 e3 z% q% Hmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1& \+ I/ P4 Q# H$ d) [  x1 q* S
  1. #返回math.e的x(其值为2.71828)次方的值减1% O3 K7 P: Y. o: T' M" J% ?
  2. expm1(x)+ K! ]2 ~1 M7 L& _) u' j. Z
  3. Return exp(x)-1.: D! ]  r* x5 c
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.* ]4 {8 K5 d- G2 \" d2 X

  5. ! s$ G+ o3 A/ n! A; A: N
  6. >>> math.expm1(1)
      o9 p) f! O. x, `- T0 d
  7. 1.718281828459045
    8 ?+ H! N0 Z6 t9 ^+ I: ^! ~/ a
  8. >>> math.expm1(2)8 h+ h8 e/ }. W8 x
  9. 6.389056098930656 n1 Y$ j+ H& ~# |, Y. c# m% |
  10. >>> math.expm1(3)$ e  U* A  }' F) ?6 u# V
  11. 19.085536923187668
复制代码
! t1 M: q- @  E  o1 t  E
math.fabs(x)  返回x的绝对值
/ ~  b& g# R; ^1 x
  1. #返回x的绝对值
      a1 E6 P  z& g' h! P
  2. fabs(x)
    7 A. h, }, W+ y7 M7 g: y
  3. Return the absolute value of the float x.
    % v. `4 v# W' y+ K

  4. 6 y/ V- L6 _. ]* V- p) q3 |7 f
  5. >>> math.fabs(-0.003); I$ _- i7 i; h7 p6 @  G6 b, o
  6. 0.003- a" ^2 l. C9 w7 P
  7. >>> math.fabs(-110)
    5 E7 m- s* y* T$ R* X% F
  8. 110.05 Y1 S/ f; A4 D7 q- r
  9. >>> math.fabs(100)* C& n+ f* x" z* W, h' Q
  10. 100.0
复制代码
, e) D; D: M' m0 \
math.factorial(x)  取x的阶乘的值9 U1 ~' V9 j% [0 `$ n4 d( A4 e% m
  1. #取x的阶乘的值- G) H7 F* U; ?$ W/ y6 h
  2. factorial(x) -> Integral
    8 Y4 m6 o, s! v( i3 i4 i8 N1 `$ e
  3. Find x!. Raise a ValueError if x is negative or non-integral./ r! {* L( b- ]! U- A/ r: X0 s
  4. >>> math.factorial(1)- p" ^5 ]9 t# v; v
  5. 12 Q' O, a- A3 N3 \. R4 |
  6. >>> math.factorial(2)6 E( a) |+ a" m0 J& d0 t0 C
  7. 20 ~5 f6 c+ I$ ?& z: `: ~
  8. >>> math.factorial(3)& U; D1 {5 h. t4 O2 Z$ m
  9. 6
    5 F6 O* f% v% H: X# ~) \, }. f  ?* B
  10. >>> math.factorial(5)/ H. W" Y5 t  k. X: a* q. Y& q9 M/ s
  11. 120" H' K! G# ]# E
  12. >>> math.factorial(10)
    & E" S" L( l% x
  13. 3628800
复制代码

% w+ W. z8 N+ s, A0 R4 Y0 Fmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数" K5 \5 Y1 }) t" e, Y7 P
  1. #得到x/y的余数,其值是一个浮点数# K/ V+ v/ U. X3 ~7 \) H1 }0 [
  2. fmod(x, y)+ E. |, e: |- X( @5 k  h
  3. Return fmod(x, y), according to platform C.  x % y may differ.. \1 N9 u2 |8 A6 H: A2 z# O
  4. >>> math.fmod(20,3)
    $ Q7 `. a4 M' R6 J' n# L- A
  5. 2.07 n9 D2 s, ]( ]5 b6 a4 i0 o0 i
  6. >>> math.fmod(20,7)* Y$ e& h7 h% W6 S4 h
  7. 6.0
复制代码
* l% {+ f; d- |0 Z
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围' `& u9 i  X' ?$ C. P8 n* y
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    . N/ X/ W; [! V) {& ~0 \
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' m' _4 Y' r3 ?" B+ A$ }
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和18 T: Z. `! A/ x& \$ ~& R8 {
  4. frexp(x)
    % m( l& T( U) s3 |' K5 u5 X
  5. Return the mantissa and exponent of x, as pair (m, e).
    3 a/ V8 A/ b' K1 u+ U
  6. m is a float and e is an int, such that x = m * 2.**e.- N6 ~6 N& h; q' i
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.' n# S& a4 q  j
  8. >>> math.frexp(10)0 m4 I1 v& ^7 r
  9. (0.625, 4)
    0 @/ `- e) Q5 E9 F9 ]2 L
  10. >>> math.frexp(75)
    # i2 S! S2 c$ D( E$ K  l- ^
  11. (0.5859375, 7)2 o* |6 _; w! S9 Y3 M4 M5 F. ]
  12. >>> math.frexp(-40)& s8 b2 D' W1 Z; T
  13. (-0.625, 6)
    # o2 j# y+ Q9 W
  14. >>> math.frexp(-100)9 Q4 A9 T3 I: z0 E
  15. (-0.78125, 7)$ X6 q2 n7 t$ N6 T( A
  16. >>> math.frexp(100)
    & d3 C% Q  ~# X/ T8 g# M: G9 c$ J
  17. (0.78125, 7)
复制代码
, Q' `9 k2 K9 W4 o- U1 O
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
1 z& Y! U' }% u$ ?, c6 e7 P% J1 C. a
  1. #对迭代器里的每个元素进行求和操作. I' ?  {0 v$ d) Q& S
  2. fsum(iterable): n& h! L2 y3 G. F7 A" i3 U; n
  3. Return an accurate floating point sum of values in the iterable.
    # X, W  U, a9 O2 Z2 c, \+ e& E
  4. Assumes IEEE-754 floating point arithmetic.
    3 W0 Y; ^  V  Z3 y
  5. >>> math.fsum([1,2,3,4])+ S, S3 S* K( I* u
  6. 10.0
    0 n. ]  U  f$ V, K$ }1 b$ H0 J
  7. >>> math.fsum((1,2,3,4))
    ' R; L* y9 ?2 G1 M+ T5 [
  8. 10.0* l# H5 V" _, f* }5 w' ~% p8 s9 R# i6 Z
  9. >>> math.fsum((-1,-2,-3,-4))
    ( q/ V$ e% d; z
  10. -10.0
    $ b* f: s1 n5 ]. {' x* r# R: N, |" w
  11. >>> math.fsum([-1,-2,-3,-4])
    2 s0 X! @( }  b) o- b" D% G
  12. -10.0
复制代码
3 ^8 T- k' d9 `) s! A
math.gcd(x,y)  返回x和y的最大公约数
& |" T% d( ^" D4 M4 A) C
  1. #返回x和y的最大公约数4 z  U& t* U1 a7 x
  2. gcd(x, y) -> int5 d- N: i+ j5 o& ]; G
  3. greatest common divisor of x and y
    / h3 P2 l! Q- W$ J
  4. >>> math.gcd(8,6)- F3 U: m4 t' r0 c7 j
  5. 2
    ) q+ q5 a. n9 F& v
  6. >>> math.gcd(40,20)5 E6 f7 S( g) b( Z" q* T/ e
  7. 205 o7 G" ?# E2 m
  8. >>> math.gcd(8,12)
    & K8 p; B' Q& |1 N& _1 u
  9. 4
复制代码

7 B+ N5 Q9 ~, K1 Q( d. f8 Q$ \math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
9 N: R* J. E4 ]4 T  P' V8 U
  1. #得到(x**2+y**2),平方的值% j/ i  Q, j" [1 x
  2. hypot(x, y)' F5 w  T) }0 y1 ^8 w" M; i
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ! e% t) u& f; q, M( S# d1 }
  4. >>> math.hypot(3,4)8 J: ?# |2 a$ _/ j" W/ |$ ^
  5. 5.0/ i* l/ `2 X3 E# o: O) K" `7 J
  6. >>> math.hypot(6,8)" O% F* A+ `0 S: k- Z" e
  7. 10.0
复制代码

) M2 C+ H  Z7 d& c$ cmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False. w( c3 o/ n; B# w8 D
  1. #如果x是不是无穷大的数字,则返回True,否则返回False+ i1 z- T% ?/ }" G- H5 _7 g% P3 r
  2. isfinite(x) -> bool
    ' O  Z( q9 [4 C* _5 }
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    " a  |! y* C1 c' l0 Q( R/ w
  4. >>> math.isfinite(100)* Y+ V4 s6 H7 Q! L7 Z5 Y
  5. True
    ) e* ?5 F6 L& d8 {9 @2 q2 P
  6. >>> math.isfinite(0)* e$ d! r5 D, O4 y6 f
  7. True
    7 Z: j! f( N3 E5 h% k* b
  8. >>> math.isfinite(0.1)" W. {1 u9 P# }1 ~/ i
  9. True
    + d% K! ]# h( V- f& U
  10. >>> math.isfinite("a")2 [# g, V# w. C: B8 n
  11. >>> math.isfinite(0.0001)) Q" _6 x0 o5 g1 |8 M2 d0 O
  12. True
复制代码
" H  f5 ^8 ^# C3 G% k" j. p' X" H
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False/ j- D! _$ Z5 v1 Q5 y
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False; K4 W. o% Q$ Y& m7 l
  2. isinf(x) -> bool
    / M0 H8 B8 j% l4 m5 t7 z
  3. Return True if x is a positive or negative infinity, and False otherwise.# ?7 u& Y4 _9 J; f8 H* c
  4. >>> math.isinf(234)# q& t& {6 i* i4 j# C
  5. False
    % ?7 ^- V- _- G/ a0 L  Q$ m
  6. >>> math.isinf(0.1), t* L  O  h5 V; y; r( Y& c
  7. False
复制代码
% d; q* B1 a% {$ e$ z$ r0 q0 M
math.isnan(x)  如果x不是数字True,否则返回False
3 a4 ~# L$ n0 l. O; T% o4 U
  1. #如果x不是数字True,否则返回False1 w! G% H$ L8 h. w4 z  Y# b, Q7 M" n
  2. isnan(x) -> bool# ]7 f# K; p2 h  q- w1 `+ h" k
  3. Return True if x is a NaN (not a number), and False otherwise." k4 H- t3 g5 O: W7 s4 p" m" J
  4. >>> math.isnan(23)' S* @0 b: x) a& K8 {% w
  5. False2 z  t7 z  k: W
  6. >>> math.isnan(0.01)* q- v& F2 H; ]
  7. False
复制代码

- G0 _  v& E6 B2 @) \math.ldexp(x,i)  返回x*(2**i)的值
2 N, H+ Q) V- w9 y
  1. #返回x*(2**i)的值
    ! G! t' ?. L/ W% u6 f
  2. ldexp(x, i)) b+ Y1 i! y9 T( }- `
  3. Return x * (2**i).
    ( c& q7 L# f8 z0 B: J( O! K
  4. >>> math.ldexp(5,5)
    8 `- b, j. e5 g  I9 ?
  5. 160.05 U5 _4 N/ `8 L9 r* |
  6. >>> math.ldexp(3,5)
    7 z  v) x+ Y5 M  f# V* \
  7. 96.0
复制代码
1 r% K. s. X" U% r4 ]7 E& [
math.log10(x)  返回x的以10为底的对数+ e; J" T% G$ ~7 c
  1. #返回x的以10为底的对数0 g0 `+ {! n% M# [/ {8 Z( n
  2. log10(x)
    5 z' s  Q( S' G( J& p# r9 B
  3. Return the base 10 logarithm of x.
    , ?; p9 i9 G8 Y
  4. >>> math.log10(10); H/ f  ?; f6 `3 x
  5. 1.0' l1 j4 I% t+ L8 A/ D3 H
  6. >>> math.log10(100)
    / c, h4 ^  K6 V$ [
  7. 2.0
    + E8 J! l) `. B9 |: i+ M
  8. #即10的1.3次方的结果为20
    5 B, h2 e, e# z! ^% J3 Z& U1 w
  9. >>> math.log10(20)
    & d* ~' L0 N7 A% y8 k" C0 @
  10. 1.3010299956639813
复制代码

$ z: r6 l, t/ \math.log1p(x)  返回x+1的自然对数(基数为e)的值0 s. ?" E& e% g; R. m; G' `
  1. #返回x+1的自然对数(基数为e)的值" Q2 D$ R$ y: V  k& b
  2. log1p(x)
    $ R8 E. K8 h& f% T4 x* y
  3. Return the natural logarithm of 1+x (base e)., A) W* x2 x$ y2 V
  4. The result is computed in a way which is accurate for x near zero.: a, Y9 E- R9 Q, m3 G. h' m: y
  5. >>> math.log(10)
    3 H4 B! K$ Y8 E7 o+ q3 n* v1 J$ s9 {# I
  6. 2.302585092994046, c& R$ O  q  B+ @) K  Q# ^7 {6 Q
  7. >>> math.log1p(10)
    5 j0 z3 Y  v4 [# m& N
  8. 2.3978952727983707
    ) E$ b  b" ?# Y  X& e
  9. >>> math.log(11)7 K3 q% A2 u( U- r' `* g
  10. 2.3978952727983707
复制代码

4 o0 D: g! |( r) k9 xmath.log2(x)  返回x的基2对数: ]1 d' Z/ j0 e) T1 ]3 w# v# g# ~
  1. #返回x的基2对数
    - O; ^1 E' j1 M5 W1 E- i
  2. log2(x)
    9 @( A* f2 z1 C. V1 g
  3. Return the base 2 logarithm of x.
    . y5 Q0 s' p' F- {% w0 G
  4. >>> math.log2(32)
    / S& [2 y4 @& ]8 s( s& v0 M" b
  5. 5.0
    ! w" @; j6 ~5 ^
  6. >>> math.log2(20)4 n4 M/ X8 l. d0 h! j" \
  7. 4.321928094887363
    . |! h4 u) L5 `! z1 c. \7 }
  8. >>> math.log2(16)
      K% N% c! ~: ^' n
  9. 4.0
复制代码

9 B2 V+ e- H( q, |2 Umath.modf(x)  返回由x的小数部分和整数部分组成的元组
$ w: Q7 C- T9 w! N. @5 h: Q; f3 g) U( C5 q
  1. #返回由x的小数部分和整数部分组成的元组
    9 G4 a& K1 L6 V& R' e
  2. modf(x)3 R! z! u: T6 h7 n0 d1 m
  3. Return the fractional and integer parts of x.  Both results carry the sign7 d  L: S; V2 a
  4. of x and are floats.
    : A& b3 W" P# o2 }, S. U
  5. >>> math.modf(math.pi)$ c' a* E% \5 b7 ^
  6. (0.14159265358979312, 3.0)! j# z% @+ j0 A4 O; t( C- ]3 h2 k
  7. >>> math.modf(12.34)* r+ G5 R# ]' s& A+ ~& V
  8. (0.33999999999999986, 12.0)
复制代码
4 J# F% ~. C1 K9 v  \* u0 K
math.sqrt(x)  求x的平方根
8 t% P2 H3 g4 s  R2 g+ Z
  1. #求x的平方根/ X: k9 R3 F& J8 t1 p0 {& A
  2. sqrt(x)" ^2 `1 E. G# z& s) g
  3. Return the square root of x.
    $ g6 u! `: M  J, _. ^
  4. >>> math.sqrt(100). v2 [7 Z- p" D, o- a
  5. 10.0
    * a$ h" ^  W$ t! D! Q  M7 i
  6. >>> math.sqrt(16)
    ( l2 L. x$ H6 \" P; l3 ^) n; P
  7. 4.0
    # K1 r; o1 ^! }; i
  8. >>> math.sqrt(20); W4 K& D5 L7 u! t
  9. 4.47213595499958
复制代码

6 ~% L3 E0 |0 B8 W3 Smath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分4 t$ Q  @& k( G6 D" X
  2. trunc(x:Real) -> Integral
    ) z+ C( C2 d4 z! r/ }
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.5 g3 ?: I) i: ^/ R, N4 f' D5 G
  4. >>> math.trunc(6.789)4 f7 ?) {8 g* `. R7 l( p/ t2 u
  5. 6. Y. A' Z. T0 u" s
  6. >>> math.trunc(math.pi)5 q& @8 P9 Y5 R/ h$ W+ s; D
  7. 3
    7 ^& k, ?; p* i# L/ i
  8. >>> math.trunc(2.567)0 }, ]  w: C2 i& j5 q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-14 06:32 , Processed in 0.077779 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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