新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

6 k& t2 |7 ~( w3 F【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! ]1 `2 T% o; I% U

) X: H: a+ q9 E- C: ~/ L9 X& n" q. j' U方法1
- z0 B7 C% M8 f2 _( w6 n
  1. >>> import math4 Q3 Z! E) ?& ~/ r7 W
  2. >>> math.sqrt(9)
    , Y( B2 w1 A( s; b: E" o( h
  3. 3.0
复制代码
方法2
' p: R) C$ ]( Z  M6 I# p$ P
  1. >>> from math import sqrt
    7 g8 l- P1 b3 I) l2 n( z8 S8 B: Z& H
  2. >>> sqrt(9)8 D9 f& F7 s  w/ g5 s6 {9 X; y5 f
  3. 3.0
复制代码
, X5 l- G( c# N+ a, m+ X. j, @3 ]

, D- s- g5 z# ]% C! I
math.e  表示一个常量$ Y, `5 {& N1 n) `
  1. #表示一个常量$ z$ D" s9 h8 a6 H8 g
  2. >>> math.e- q; Y0 D5 Z! `6 T
  3. 2.718281828459045
复制代码

) @  t9 F0 u' H; wmath.pi  
数字常量,圆周率
) B& x& `, |; ]: ~
  1. #数字常量,圆周率- Y1 L! g7 `. ~, o3 c9 B; o+ F) x  i
  2. >>> print(math.pi)0 D3 y2 Z) k; v
  3. 3.141592653589793
复制代码
; o. o1 W$ a! T3 T. t
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

1 E0 }# H* {* m- }3 Y: C. h
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x4 i: `8 V; I, F6 y  A5 ^
  2. ceil(x)+ @0 ]5 E! e% @* x" A; \1 O/ z
  3. Return the ceiling of x as an int.6 w  [; b) h# @7 b: `( v+ X5 {
  4. This is the smallest integral value >= x.- j- j' a) M* G2 w& S+ f  k

  5. 7 t! k4 f: a$ F
  6. >>> math.ceil(4.01)
    * O6 I& R2 O: U6 Y& J
  7. 5, k# A2 h* c! y& K6 h2 \
  8. >>> math.ceil(4.99)& B- S) N5 w# V& R# H% K* q# w9 t
  9. 53 a) x8 Z, J. U- l- ?
  10. >>> math.ceil(-3.99)+ c! y7 G# A* b0 a7 A
  11. -3
    5 M) O: W( `; [" x2 F" O! [
  12. >>> math.ceil(-3.01)
    . i6 K2 B2 ^! O' }# `
  13. -3
复制代码
& I! L: X! t( V0 l  ?" z: a
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 M5 ^7 D4 O9 H) A3 b" J
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身. F* d1 Z; j* ?' C, \5 h. t
  2. floor(x)
    + m& \4 u- W- d5 |
  3. Return the floor of x as an int.0 U& w( e; e$ O4 E# T# y
  4. This is the largest integral value <= x.
    : v% W- b0 f! f: G: v# O  h
  5. >>> math.floor(4.1)% k( P+ }9 n7 M1 r5 ], x7 z
  6. 4
    3 C' ^1 h9 M4 u( A6 R; N) b8 }* @
  7. >>> math.floor(4.999)
    ! F& u/ V1 Q8 ?+ N; F. Q, o4 R  g; J
  8. 4
    ' v4 c$ y' ?5 Z6 o
  9. >>> math.floor(-4.999)
    ! x' P% l, a9 ]( t) T
  10. -5
    , I4 \( X7 ^* J- @
  11. >>> math.floor(-4.01)
      r" e: C  k1 k6 p" }
  12. -5
复制代码
5 ~, A  r4 X7 {0 T4 w$ M7 P8 D
math.pow(x,y)  返回x的y次方,即x**y4 I7 x5 u" v* K- G8 t1 U
  1. #返回x的y次方,即x**y
    " t( G$ p2 b; E! \9 v$ h1 y
  2. pow(x, y)) H% y3 ~7 _. A" U6 T6 a
  3. Return x**y (x to the power of y).
    - H' A) A- R5 }. F9 Y1 r2 H; C
  4. >>> math.pow(3,4)
    ) C! b  b: A! O, l
  5. 81.0
    ' `- I8 ]+ x& k& h
  6. >>>
    0 c+ n% c, L: k+ [9 |
  7. >>> math.pow(2,7)
    6 c- o  e$ }: k% O( n0 ^
  8. 128.0
复制代码

  z% W; O( T% A( C: q' h* }math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
' M; ^* U/ y- z! i( q; V% w
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    1 r6 S' @; t. |. h% F# q
  2. log(x[, base])& \1 y0 w% A4 E9 U! j
  3. Return the logarithm of x to the given base.! R6 T0 F7 J$ h8 S2 \1 Q* w
  4. If the base not specified, returns the natural logarithm (base e) of x.
    - d! M3 l; P% `  c! k. C5 Y
  5. >>> math.log(10)( t7 ]. ?1 c) w) H. T; z* l
  6. 2.302585092994046
    # ^& v; y% x9 j4 w& ]5 c" l+ l
  7. >>> math.log(11)
    # }" w3 h. |5 m8 ]7 n4 M2 T
  8. 2.3978952727983707
    , F. {' M( v8 i8 y. j& s* T9 ~
  9. >>> math.log(20)& r$ r' I: ]1 P7 L' M* B5 U- C! n
  10. 2.995732273553991
复制代码
/ Q" d2 G  h& V! |5 d
math.sin(x)  求x(x为弧度)的正弦值
! T' V. b0 O# q3 e# P1 Z4 i) G
  1. #求x(x为弧度)的正弦值
    ; f1 P. G# R8 D2 @' O+ J  s$ D
  2. sin(x)
    ) ?6 l* V9 Z# m9 P4 @9 p6 v
  3. Return the sine of x (measured in radians).
    0 N1 }/ g+ M3 y, ], S1 c, }
  4. >>> math.sin(math.pi/4)! K0 k& b  X/ C/ [3 y
  5. 0.7071067811865475& c1 ]% I3 |8 z5 b1 |+ T8 }
  6. >>> math.sin(math.pi/2)+ O$ b( K$ j) B7 h: |
  7. 1.0
    8 j/ t7 Z  t& @: @) e5 Q5 n' p+ l' k
  8. >>> math.sin(math.pi/3)) e$ t; @6 I2 z& @! d0 E
  9. 0.8660254037844386
复制代码

) t1 h! n" K( R2 n# B) i& {5 {+ ?math.cos(x)  求x的余弦,x必须是弧度
$ L; q6 z1 o2 Y6 O  x
  1. #求x的余弦,x必须是弧度
    3 g/ o3 I. u$ ?
  2. cos(x)
    - ?3 J8 \- N5 r8 Z" y2 Q; @
  3. Return the cosine of x (measured in radians).
    3 x: Q% D2 o4 }' n6 ]5 C8 U2 T
  4. #math.pi/4表示弧度,转换成角度为45度
    5 P: X: q2 R6 x/ i7 D' B
  5. >>> math.cos(math.pi/4)
    : @" H& X! p" p7 N
  6. 0.7071067811865476" d$ `# @, f9 `5 `
  7. math.pi/3表示弧度,转换成角度为60度
    - z; K* U6 G& ~% X2 ^* }
  8. >>> math.cos(math.pi/3)2 K; K6 V' E$ e8 `) E9 c+ u3 U
  9. 0.5000000000000001
    6 i) T( _! j* V3 ^& b
  10. math.pi/6表示弧度,转换成角度为30度
    ( [) x# ]; x% e: m) H3 l! A
  11. >>> math.cos(math.pi/6)8 U% u- n+ r7 u' C2 [2 Q
  12. 0.8660254037844387
复制代码

' X; ]9 b2 T( _( l& ^math.tan(x)  返回x(x为弧度)的正切值% K3 q: |) x5 c" x4 t2 f
  1. #返回x(x为弧度)的正切值
    5 D' k& T  \+ y
  2. tan(x)
    5 D% o' d$ E1 S. J
  3. Return the tangent of x (measured in radians).
    0 Q* h5 h6 A, t4 t2 d" y; F% s1 h
  4. >>> math.tan(math.pi/4)8 M, _( d: ?1 f, g
  5. 0.9999999999999999
    2 b3 s; w. {6 D
  6. >>> math.tan(math.pi/6)
    8 A$ v8 T. b3 ?- D) ~0 q
  7. 0.57735026918962571 a' M0 U6 @, F! S1 y9 L
  8. >>> math.tan(math.pi/3)& I' d5 U' H. }/ d: t9 ?& |3 I2 q, ?
  9. 1.7320508075688767
复制代码

# G0 N8 F: X+ K6 y/ Amath.degrees(x)  把x从弧度转换成角度
% y& d- w, m" s: m+ D  Y0 _
  1. #把x从弧度转换成角度6 \3 }; Q5 l. r6 V) m
  2. degrees(x); {9 _4 d* d0 {  z7 X# y) ?# `
  3. Convert angle x from radians to degrees.
    # {' r& D& F6 k# D
  4. / L( O# t' t3 u/ h! j7 r
  5. >>> math.degrees(math.pi/4); {8 g! I& `* ]1 o$ Q, ^3 F. C- g$ b
  6. 45.0
    - h" ~( J! }- @" R. m
  7. >>> math.degrees(math.pi)6 R' l( B& V8 W5 Q; v
  8. 180.0
      I4 L' f  K# Q, r* w% M3 r5 i' ]
  9. >>> math.degrees(math.pi/6)+ v( ^. O1 G: A3 x4 B- F
  10. 29.999999999999996
    $ O2 ~0 t/ a) f8 Z' d
  11. >>> math.degrees(math.pi/3)2 `" |2 t( A4 o) @/ j# b& N: d, }& ]
  12. 59.99999999999999
复制代码
4 P0 G* z8 I0 ^/ z+ \
math.radians(x)  把角度x转换成弧度( W: T  m7 p' ]8 X( ^- h
  1. #把角度x转换成弧度
      a/ h# T) U9 t: V5 d. {3 a* o; ^
  2. radians(x)5 W/ v" Z; [3 R5 ^* m, L
  3. Convert angle x from degrees to radians.8 r5 z. U, V) a* J6 d* |8 }9 D/ s. T
  4. >>> math.radians(45)( b! t  }0 Y1 F% f  i6 b
  5. 0.78539816339744833 T8 [  |& b6 y( j. v$ {
  6. >>> math.radians(60)' S; r4 W% D  Z) q. K9 {5 b: I& L
  7. 1.0471975511965976
复制代码
2 P# P3 M4 J2 g. ]
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
5 X0 g* N2 ^; k$ ~7 w7 z; |
  1. #把y的正负号加到x前面,可以使用0
    $ n, A* P# }7 C; c  J
  2. copysign(x, y)6 Y9 s& f1 O" B& O0 Q5 |
  3. Return a float with the magnitude (absolute value) of x but the sign 9 r3 X% ^/ v4 c1 Y( |
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    / C; H4 X% t  O: E" @
  5. returns -1.0.: C0 v% y  r5 n, f- d9 v
  6. % X9 g+ z1 p# f+ s
  7. >>> math.copysign(2,3)
    1 I: o# S& f3 I. r+ l3 L; Y
  8. 2.0
    ( Q5 a0 F3 f) C/ S" ]/ W7 l* L- i
  9. >>> math.copysign(2,-3)# K! v7 |% P- M/ b9 r
  10. -2.0, ~% ~! @8 s' k( q. k7 \- m
  11. >>> math.copysign(3,8)
    / X" r9 D5 _1 q$ w
  12. 3.0
    " o: p7 w! N% z: L
  13. >>> math.copysign(3,-8)
      }8 `  q; K+ y' Z3 c
  14. -3.0
复制代码
# r; C: z3 f7 h2 ]* t
math.exp(x)  返回math.e,也就是2.71828的x次方/ U; n, |( Q' c; b9 C
  1. #返回math.e,也就是2.71828的x次方
    - X* y" l4 R) C" _% v6 P" t& |
  2. exp(x)5 W& H5 l( h/ O4 e2 Q2 j3 A  X
  3. Return e raised to the power of x.4 O$ M% n# E& g) Q2 G& X( @
  4. ) n, |4 A$ n/ d- R3 C8 d
  5. >>> math.exp(1)1 ^0 x! c- d6 i& ]8 H5 e- W5 q/ X
  6. 2.718281828459045
    * Z& d: H  W9 y+ s& z9 V- I3 ]% f
  7. >>> math.exp(2)8 ~* T: ?; o. s. N: J  R+ n6 C! s6 O
  8. 7.38905609893065* \/ u* O2 W- G* g0 E& Q
  9. >>> math.exp(3)
    " \: k( s) ~6 n7 i7 t) @$ Z# P
  10. 20.085536923187668
复制代码
) L3 {+ k+ P( e* r8 |" L- a
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减14 @; e3 M9 ^0 I; T7 U
  1. #返回math.e的x(其值为2.71828)次方的值减1: v4 C& P* z4 C! `6 j9 o
  2. expm1(x)1 k7 O6 c! \- T1 I" l. Q
  3. Return exp(x)-1.) R; k$ `( K4 y4 L
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; {+ O) E+ H$ Z2 [1 I

  5. 2 [- o9 k' v2 @; w
  6. >>> math.expm1(1)
    . B! j' W2 u0 W" X
  7. 1.718281828459045( t/ L8 c; ]* a: m' t! U1 f
  8. >>> math.expm1(2)
    3 D8 t& ?0 R7 B$ p9 K
  9. 6.38905609893065
    ! @! j+ d* y1 ]
  10. >>> math.expm1(3)
    5 K1 I( f) ^9 r8 M4 f
  11. 19.085536923187668
复制代码
( h5 m  C, U) Z: k* K
math.fabs(x)  返回x的绝对值5 R5 @$ @; I, j3 w7 ?, D6 L8 U
  1. #返回x的绝对值" L+ P$ n3 N" Q( `
  2. fabs(x), D! d7 v; U$ m6 U# i1 h* d
  3. Return the absolute value of the float x.7 Y6 C4 D& J* A6 A/ X% I( `& L
  4. - y0 m. B' p# m% y% v) m8 D
  5. >>> math.fabs(-0.003)
    . o7 Z1 f5 H& }, t* L" A5 {
  6. 0.003
    4 M! E9 T7 W- _) ^1 e+ J! e
  7. >>> math.fabs(-110)
    : q0 E" Q+ j5 y2 ^
  8. 110.0
    1 a4 N7 p/ b; l6 ]- `1 _/ @$ h! s; b
  9. >>> math.fabs(100)
    9 J! S& z; @! e: I  y
  10. 100.0
复制代码
  B, ^6 T, a% W+ X9 y
math.factorial(x)  取x的阶乘的值2 [) i. N: P- S& N, l) ?& L9 h
  1. #取x的阶乘的值
    . L9 G. z: Y9 E7 h5 V
  2. factorial(x) -> Integral0 F& E9 v* ?' O- _0 q$ w! N  \3 o
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ( y/ L+ ~4 @6 z  d/ L! G/ ^3 m" _
  4. >>> math.factorial(1)
    1 j& f! w. G( E( A) R3 @/ K
  5. 1
    * i4 L, U5 T5 [( p- f
  6. >>> math.factorial(2)/ g$ d+ a7 O7 n
  7. 2, h7 c8 Q" L3 q7 H. d0 W! v
  8. >>> math.factorial(3)0 X/ n/ o+ O- m; _
  9. 6
    4 Y7 f& [6 O4 o( Z# |) d
  10. >>> math.factorial(5)  v0 @+ i% \" L6 t5 \
  11. 120
    4 ^( h. _" a3 i. o8 \
  12. >>> math.factorial(10)- k8 \; k4 B1 [% ]* n( d9 E
  13. 3628800
复制代码
0 K8 V) D9 L0 D( S9 K5 j# V: d
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
3 \2 k) l/ [) y' p. @3 r: y
  1. #得到x/y的余数,其值是一个浮点数
    " B. J# K' ~+ T+ l2 L
  2. fmod(x, y), G  @' @/ o3 \! N9 G
  3. Return fmod(x, y), according to platform C.  x % y may differ./ m, S8 a0 T4 q
  4. >>> math.fmod(20,3), w/ z3 W' W7 I$ n9 A2 Y4 o; P5 E( W. r
  5. 2.04 O, I+ V# ]+ r9 v2 e3 }% r
  6. >>> math.fmod(20,7)
    ( z* V- _" t* R) V: ~0 P# N
  7. 6.0
复制代码
. b7 N( q. S* W, F  e
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ ]- X% }" L% n; [
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,5 j6 s: ]; F" w7 [) H  L
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 d) e+ W5 y& ~. w$ d7 d
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ r/ K( A7 T$ [5 d
  4. frexp(x)! m( f8 v( T+ F+ x2 r* ^
  5. Return the mantissa and exponent of x, as pair (m, e).8 R7 M/ `( ]% s3 _2 Y
  6. m is a float and e is an int, such that x = m * 2.**e.3 g" d8 O* A+ u- [3 K! U. U& b
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    4 c( H2 M/ x! N9 k) R) B
  8. >>> math.frexp(10)
      t5 ]/ o. x2 c/ \% ?" s
  9. (0.625, 4); O: M% m7 }6 n& r9 Z
  10. >>> math.frexp(75)- ], Q5 S: M5 N( X8 v
  11. (0.5859375, 7)- W- n) B$ p( O: p3 a' r# R2 _1 X
  12. >>> math.frexp(-40): ^- f% I& k6 E) A; B  ^
  13. (-0.625, 6)2 l1 a$ ]) t9 U, |2 J* E+ {6 o
  14. >>> math.frexp(-100)
    8 c2 a+ X4 Y: b
  15. (-0.78125, 7); e2 s9 q# s6 F; Y4 U1 ]" J
  16. >>> math.frexp(100)' x5 X9 l& N2 `( i
  17. (0.78125, 7)
复制代码
& x6 h% y% Q% ?0 ~0 k3 V0 |
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
( L$ O: v% V% R$ |2 c
  1. #对迭代器里的每个元素进行求和操作+ W; r* L2 h% _) O- e
  2. fsum(iterable)
    # ?& c# y' M+ o$ |9 k! x& B
  3. Return an accurate floating point sum of values in the iterable.
    6 F4 I+ K8 o8 G& G5 K8 G- o/ {
  4. Assumes IEEE-754 floating point arithmetic.0 E( S  V9 A: E1 z4 I+ I
  5. >>> math.fsum([1,2,3,4])
    ) k2 Y. D1 s7 D0 P+ l. s
  6. 10.0: x: `' t+ D* {  d7 h( ?- o
  7. >>> math.fsum((1,2,3,4))- G! H: L1 U" m% Q" G+ J, Y3 @& }
  8. 10.0
    8 q( U+ z! Q3 J
  9. >>> math.fsum((-1,-2,-3,-4))% l# Z( e! [6 m6 ~" l( ~
  10. -10.06 Y- U' k6 N& \, U: Q
  11. >>> math.fsum([-1,-2,-3,-4])0 x9 l* I: ^' J  r  G5 b
  12. -10.0
复制代码
4 h* R6 n6 D% v2 N9 S, s
math.gcd(x,y)  返回x和y的最大公约数
8 m1 J! W4 d0 d- a/ G" H2 h
  1. #返回x和y的最大公约数9 Q3 k3 ]7 A: G  G( h
  2. gcd(x, y) -> int
    , ~7 U$ F6 Q) N2 n+ t: d
  3. greatest common divisor of x and y
    1 i% @) A2 |9 r. \# I! [4 f
  4. >>> math.gcd(8,6)
    $ @9 T1 x9 U) ?
  5. 2
    ' a, K0 C, d7 c" |- E, n/ l. O
  6. >>> math.gcd(40,20)
    0 h6 f) K( B6 L
  7. 20
    7 T" L' o# ~* m" t0 e$ @  u0 b
  8. >>> math.gcd(8,12)5 h, v, i/ b4 o. R1 V8 K1 U
  9. 4
复制代码

9 i8 f. m, G. ~5 K% i$ dmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
: {, ~" m6 I: O1 s* E
  1. #得到(x**2+y**2),平方的值
    0 L8 W" k% W; e; ^) i. _& q) L
  2. hypot(x, y)
    3 K  f* {" j! p- c% g8 e
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ' J& j2 n/ Q" T/ p/ v8 S
  4. >>> math.hypot(3,4): B! E- [( G3 i8 {  m, R
  5. 5.0" a2 z9 T; R. D0 q, y2 k, N
  6. >>> math.hypot(6,8)$ e* ^( f( a* j( d. S
  7. 10.0
复制代码
4 I* N  E* s2 @/ O
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False6 E  X: q% a+ q
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    8 f2 c# y! y6 |  C* u; M& x
  2. isfinite(x) -> bool) h6 C# ~0 Y0 B7 ?
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.. V1 y2 b% ^2 t; w+ \4 R! S9 S
  4. >>> math.isfinite(100)3 }( B( ]# v2 i0 `8 q) p2 t
  5. True& l* s8 N3 r$ |0 \+ a; [: e
  6. >>> math.isfinite(0)3 Z6 _% o3 s/ t
  7. True3 T3 E, C9 T! P( A9 K" X
  8. >>> math.isfinite(0.1)4 m1 I2 A; @! n( j
  9. True. a# z* r. W! d- k  x3 ?0 w
  10. >>> math.isfinite("a"): D+ e! Y( T: w. r; |% x
  11. >>> math.isfinite(0.0001)" j9 j% X' B: l. J+ j% m- n9 Y
  12. True
复制代码

# X. F3 `; J3 \math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ w' F3 s8 c: p$ X. g8 S$ k
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False& x) [% H/ m1 K: ]
  2. isinf(x) -> bool
    7 m, x8 E5 b6 |6 r
  3. Return True if x is a positive or negative infinity, and False otherwise.: v8 B- e1 @) L9 e2 Z$ s
  4. >>> math.isinf(234)
    ( s1 J3 _3 Y! H
  5. False
    $ W6 V; y8 l" H6 y
  6. >>> math.isinf(0.1)) Q* d% U( m' X$ Q: J+ V
  7. False
复制代码
! F: _  b5 C+ X, W5 O, ~
math.isnan(x)  如果x不是数字True,否则返回False
* q/ p+ _+ a# w' d
  1. #如果x不是数字True,否则返回False1 n, A9 a! P9 g
  2. isnan(x) -> bool
      N6 r! h/ h" D$ H- J1 n
  3. Return True if x is a NaN (not a number), and False otherwise., K- u- X- \1 v6 _, L5 i
  4. >>> math.isnan(23)2 B8 J- n! k: b; [1 I/ v
  5. False6 K7 l' W; B  K/ e
  6. >>> math.isnan(0.01)
    2 }3 I3 J0 a6 s) X* p/ }. Z) A
  7. False
复制代码
- T" h. }4 E! e# Y# k  j
math.ldexp(x,i)  返回x*(2**i)的值) G3 H7 e( M! Y% \& B1 f: ^
  1. #返回x*(2**i)的值8 H, s. G7 O8 }2 I1 ~
  2. ldexp(x, i)
    . u  q8 Z, H+ g& s3 @1 R
  3. Return x * (2**i).0 C  v0 l! e" a# ]7 F4 T! p1 E8 y
  4. >>> math.ldexp(5,5)' B" y+ F! z9 |0 O
  5. 160.09 {1 d( }4 {; T& T8 Z' ^* `0 `
  6. >>> math.ldexp(3,5)# f, j8 H1 x8 M" A6 K
  7. 96.0
复制代码
, i* ?$ Z: e" X7 b; Z
math.log10(x)  返回x的以10为底的对数
0 d8 U* L6 \' e+ \) k; |. c- A- v
  1. #返回x的以10为底的对数
    ! ^+ M" Q5 O$ v( Y2 I
  2. log10(x)
    ' g9 G; N& ]$ m3 P2 z& m, s3 |
  3. Return the base 10 logarithm of x.
    / A$ ~  L) k% ^- H7 _
  4. >>> math.log10(10)/ C. R" b7 v+ o) w# n9 H7 M
  5. 1.0( a+ M2 y% n7 m/ j, e4 W$ u1 p
  6. >>> math.log10(100)
    # x$ Q# w$ J. p) i1 T( o
  7. 2.0* x* j/ C3 X+ O- I% _  }4 J2 v
  8. #即10的1.3次方的结果为20
    4 q0 ^" E! P" O) `3 x- T
  9. >>> math.log10(20)
    6 j* s. o* Z' W. P3 j! x1 _9 p
  10. 1.3010299956639813
复制代码

2 |* N) {0 M8 F2 x) Rmath.log1p(x)  返回x+1的自然对数(基数为e)的值
2 k: O( ^0 u6 ^% |- g
  1. #返回x+1的自然对数(基数为e)的值
    ; \& C( F- \1 b' E$ G
  2. log1p(x)
    : ]$ f* p3 ^' ?8 ?' r( x# m
  3. Return the natural logarithm of 1+x (base e).. w7 d* O" b8 I- ^5 F* d5 y4 m
  4. The result is computed in a way which is accurate for x near zero.0 {; Z+ Z7 O$ s, B$ N1 K: V( h
  5. >>> math.log(10)
    & v( S# J( c0 L, o4 g! M% c
  6. 2.302585092994046( c' @, m/ s4 u$ D9 V3 Y5 H) I8 _* n
  7. >>> math.log1p(10)
    9 T: k) v3 O1 I
  8. 2.3978952727983707
    6 c. w% P/ ]  v9 b! n; |7 h' {2 j" x
  9. >>> math.log(11), S5 U5 e/ k# y, U4 b5 E
  10. 2.3978952727983707
复制代码

# h/ g' ~; U- d  U3 Vmath.log2(x)  返回x的基2对数
3 c6 f" ]% S7 D* s5 d+ R+ f
  1. #返回x的基2对数
    4 ^" m; D8 O/ b( {+ h$ C8 y
  2. log2(x)3 a* h8 l  S9 d6 }
  3. Return the base 2 logarithm of x.
    9 s1 ]( z# W* V2 E" C
  4. >>> math.log2(32)
    " B. c" [, |; w+ R  \' i
  5. 5.0
    + Z; n% n: F7 }
  6. >>> math.log2(20)
    6 c  V! c. r" j2 c( o6 p1 u
  7. 4.321928094887363
    $ Z9 L; m- G$ z6 v/ C
  8. >>> math.log2(16)
    3 n' O4 @3 K5 P( U. C, {/ M7 A  X
  9. 4.0
复制代码

% s3 i$ d. d# u6 \) Omath.modf(x)  返回由x的小数部分和整数部分组成的元组
8 J! _) ]) h; q* U) Y
  1. #返回由x的小数部分和整数部分组成的元组3 `1 V. P4 @. O. x+ |
  2. modf(x)
    + q% d2 T7 t& {: d7 l1 g0 s
  3. Return the fractional and integer parts of x.  Both results carry the sign
    : ]$ R- A6 X& A% a5 J( @
  4. of x and are floats.5 O/ l8 ]5 b: W0 u/ j
  5. >>> math.modf(math.pi)
    : P" w& a! W* O& F) w: a
  6. (0.14159265358979312, 3.0)
    % h. G9 H- V% J1 W) G/ _" q# Q* z
  7. >>> math.modf(12.34)
    , y# a  j! u6 G1 N7 W
  8. (0.33999999999999986, 12.0)
复制代码
" L$ Z3 w5 X6 l& h8 W# J' I
math.sqrt(x)  求x的平方根. I7 T) j/ R; `) h' S' z; K# g
  1. #求x的平方根
    . z' W% R! V' |* B
  2. sqrt(x)9 ?" c% V4 ?) i" v) [* @- h3 o8 g
  3. Return the square root of x.
    & ^; l0 v( o" ^6 @) U% z
  4. >>> math.sqrt(100)* M, b2 e" h" P$ ^
  5. 10.0( I9 Q' d+ ]% `" b9 `+ {
  6. >>> math.sqrt(16)
    # K% U0 f- r, G  P
  7. 4.0
    # \; k; p6 a6 A* Z6 J' F6 R
  8. >>> math.sqrt(20)
    + R  ^3 _" Y5 Q% Q
  9. 4.47213595499958
复制代码
" S" ?$ p/ W, X/ }8 o" |% l! h
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    & i/ \" L. r+ C  ]% f" |% o9 v
  2. trunc(x:Real) -> Integral
    % L  N5 F8 A1 p  W0 v
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    & b, U) D$ S, {2 l2 a# Z
  4. >>> math.trunc(6.789)
    % y9 a+ h$ `- d
  5. 6  ?8 k2 C; e* N: P
  6. >>> math.trunc(math.pi)
    # L8 {1 q' H# G% w" S
  7. 3/ O- n: J3 a& v+ J
  8. >>> math.trunc(2.567)7 p& ?* I5 l. u( a+ o1 T
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-6 23:48 , Processed in 0.087174 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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