新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
3 @. F) g6 {7 Y3 _3 t% F
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
  l* q# R( |! L5 J3 o$ d& H- {

* p& Y) d5 o$ L' ^1 i4 Q% t方法1
9 R% o4 k! i* d, V5 E2 b; P
  1. >>> import math
    4 g7 Z2 e# e. P0 N
  2. >>> math.sqrt(9)
    ( P4 p- k1 M2 Q  u: G; \7 s
  3. 3.0
复制代码
方法23 Q5 T- f( j7 b! n% p) h
  1. >>> from math import sqrt) A* Q* x/ J7 G
  2. >>> sqrt(9)
    3 v) E5 ?& V' s
  3. 3.0
复制代码

: T/ j; x. p( r5 q7 r+ Q7 }
; d; K" p) K0 U) T; n
math.e  表示一个常量
% \* @+ i, K1 e: [- a* W! m
  1. #表示一个常量# H) e. K1 D/ y5 K  G; \
  2. >>> math.e
    9 z. X7 J3 q4 p: e3 O+ g7 U8 _
  3. 2.718281828459045
复制代码

/ ], q& V: H) F" S4 V, J5 dmath.pi  
数字常量,圆周率

; H4 r; o0 Y3 n, U4 X9 _
  1. #数字常量,圆周率- w4 v( E/ k, ]8 i
  2. >>> print(math.pi)
    / w4 A8 C; X, y
  3. 3.141592653589793
复制代码
* M# o" h) U3 O# E4 G% \# |
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
0 U5 N3 Y4 A1 W6 R0 A9 `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    4 n# I: S6 j7 }) ^# l$ t" `8 Q& w
  2. ceil(x)$ G, C& c' j1 u  t
  3. Return the ceiling of x as an int.
    , U* p& |8 h% P* |+ V  X; k9 \
  4. This is the smallest integral value >= x.: N" c0 ~' F4 W  [$ I; A

  5. 2 C) X+ h6 r6 U) ?
  6. >>> math.ceil(4.01)$ @: o- P) U3 W# M
  7. 5& T" E8 M0 m) x
  8. >>> math.ceil(4.99)
    5 }9 u$ ]6 R( S
  9. 54 }$ M# E; d; p) ]; G+ q- c
  10. >>> math.ceil(-3.99), _# b) g6 J$ L% N5 T9 q1 Z% R
  11. -3
    ' h' }5 Q" D9 j. Z
  12. >>> math.ceil(-3.01)0 M4 E% q6 J% }& Z
  13. -3
复制代码

0 s0 Z$ F1 v! P+ `; Z" amath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身! n8 ~& E* }- k/ C2 A
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身; G" \; l! P1 q+ S3 O' `: i
  2. floor(x)
    + I) \6 y: ]( l
  3. Return the floor of x as an int.8 j4 V) D- e# i' x9 r( J
  4. This is the largest integral value <= x.6 x9 }5 ]/ J4 [
  5. >>> math.floor(4.1)' a. c' f6 I, D5 _
  6. 44 |. |* a, U+ G! d
  7. >>> math.floor(4.999)0 i9 |! ?0 N( V- J9 l. i; T& j
  8. 4# M& ~5 q$ C2 A# {' ^
  9. >>> math.floor(-4.999)
    # k1 c) O6 v! q
  10. -5: j7 @' k/ Y) N
  11. >>> math.floor(-4.01)
    8 G4 _7 O2 v# Q6 `
  12. -5
复制代码
/ R" \3 e# w! O5 Q: ~2 j/ W8 c; X* @
math.pow(x,y)  返回x的y次方,即x**y2 M- A  Q& ?! Q. S' L7 a
  1. #返回x的y次方,即x**y
    - f$ j! {6 M' u  Y4 w5 o+ _5 O( F2 \
  2. pow(x, y)
    3 f. w7 R6 w% }% i/ Z; n! }
  3. Return x**y (x to the power of y).
    * U' A5 B+ k" A. {5 Q4 r
  4. >>> math.pow(3,4)- J8 I5 T% f- K8 i
  5. 81.0
    . q- n7 @; K' _) A9 N
  6. >>>
    , f8 F2 Y2 T! g/ n) I
  7. >>> math.pow(2,7)# S1 z& l9 S: h( j8 Y; n  q
  8. 128.0
复制代码

7 t0 ]4 O6 d% P+ s1 m! e; g; Amath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
2 n8 r7 k0 J5 }' A9 U/ d% `
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 r" k! ~1 D- y$ |4 j
  2. log(x[, base])
    . _/ ?  p' r0 o* J. K# T
  3. Return the logarithm of x to the given base.
    . Y' G, ~# y+ ~5 X% B6 ~
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 F* f) b5 _$ p
  5. >>> math.log(10)0 s; R6 X, F1 O) \3 \8 L
  6. 2.302585092994046
    & T% Y" V& L8 c% y! s
  7. >>> math.log(11)8 |! k+ T% s" z& b! n8 n: \
  8. 2.3978952727983707
    $ [7 d  m" M  Q3 ]( R  m$ F
  9. >>> math.log(20)5 s* b. I( v+ ]' U, r3 k
  10. 2.995732273553991
复制代码

: a9 h$ T/ E9 f. c4 k$ l8 I8 Kmath.sin(x)  求x(x为弧度)的正弦值
! K* o3 k# ~; D5 w
  1. #求x(x为弧度)的正弦值
    ! \! T/ U$ u7 A6 [# }' D; V/ W
  2. sin(x)# L0 c. C& w( z1 }& J: F
  3. Return the sine of x (measured in radians).# N( y9 L2 R5 @( U5 U) U5 o
  4. >>> math.sin(math.pi/4)/ F1 q0 `' j( u$ X
  5. 0.7071067811865475$ k9 ?6 y1 L3 }1 G, z6 {6 R7 u
  6. >>> math.sin(math.pi/2)8 ?# f4 @) q' t0 I( B3 c
  7. 1.0' ^3 q4 M6 m) a" I! b3 r
  8. >>> math.sin(math.pi/3)1 k. c6 `( p8 _4 A+ h- T2 |9 @
  9. 0.8660254037844386
复制代码

9 L9 u! K% `+ K7 omath.cos(x)  求x的余弦,x必须是弧度
) B4 G1 j6 W- a: b4 P
  1. #求x的余弦,x必须是弧度+ g9 x, N" V# R7 m, j. s
  2. cos(x)- Z" c7 X0 [0 V4 a; f1 E# H8 t  ~) x- j
  3. Return the cosine of x (measured in radians).
    ( O& s' E2 L6 p5 _! }
  4. #math.pi/4表示弧度,转换成角度为45度1 x) V% @1 W: d( R% }
  5. >>> math.cos(math.pi/4)
    8 [6 Q+ D* _' l* W. ^
  6. 0.7071067811865476
    - I$ Y% C3 x9 v" [
  7. math.pi/3表示弧度,转换成角度为60度) w  h4 d+ \. ?
  8. >>> math.cos(math.pi/3)9 B7 _  l; Q: }' J' Y0 t
  9. 0.5000000000000001
    # e# o, E2 u& O
  10. math.pi/6表示弧度,转换成角度为30度
    ' R2 Z% s( H0 s/ t# p
  11. >>> math.cos(math.pi/6): m5 X! O7 L! l6 P2 h# u6 J0 M6 x
  12. 0.8660254037844387
复制代码

& u/ b6 a9 \. o% a5 _math.tan(x)  返回x(x为弧度)的正切值; e$ S& B5 c5 A) F: y5 H
  1. #返回x(x为弧度)的正切值+ W3 t9 d) P5 y; ~: M
  2. tan(x)- w4 a, c2 O2 _  S2 K  |
  3. Return the tangent of x (measured in radians)./ j) r: O* i# L/ X" c
  4. >>> math.tan(math.pi/4)/ Q3 D" n6 U: G: L9 x. H' A" b
  5. 0.9999999999999999
    3 J# H% J! @. a: {2 |6 ~- o
  6. >>> math.tan(math.pi/6)+ t6 Y( E+ a5 M! p
  7. 0.5773502691896257
    4 J& p) k; Z  K' ]% x7 d3 d
  8. >>> math.tan(math.pi/3)2 Z+ n, `* {/ S& C' V& G
  9. 1.7320508075688767
复制代码

% A: y1 F2 G. A6 [! b3 e# s$ a0 Emath.degrees(x)  把x从弧度转换成角度
8 Q/ i" t" _  p7 M# @0 i
  1. #把x从弧度转换成角度+ t5 @5 T7 y) A& w8 f2 |/ A+ {# Q
  2. degrees(x)
    4 l+ o8 i8 C$ Z7 c
  3. Convert angle x from radians to degrees.
    / t/ c7 h1 h3 A! M- J& P4 [

  4. ; _7 y' z4 Z% y
  5. >>> math.degrees(math.pi/4)
    , e5 C2 b  {$ f) }) ?2 ^' {
  6. 45.0) \- O2 s( z/ H. u1 \7 V
  7. >>> math.degrees(math.pi)0 M7 Q4 u" U/ n% H( d/ u7 _
  8. 180.0
    + X7 H1 {% b: {
  9. >>> math.degrees(math.pi/6)
    ' `2 k1 Q- l. z' B( E) K( G- r
  10. 29.999999999999996- i& ^1 t1 n6 f6 C" Q) _7 ?
  11. >>> math.degrees(math.pi/3); L% n+ n4 s5 L. o9 ?
  12. 59.99999999999999
复制代码
' b9 ^4 ~: m0 T2 U( }9 L6 T9 K
math.radians(x)  把角度x转换成弧度1 b9 v6 t8 `$ X9 ^3 z
  1. #把角度x转换成弧度
    $ P; h! U) [, @9 Z2 g0 Q2 v" p- `) a
  2. radians(x)
    . M2 y" o8 Y( \7 ^" s8 N- f) |6 T
  3. Convert angle x from degrees to radians.
    " G* ^% u+ m. ^. i$ e( s
  4. >>> math.radians(45)
    ' Q' L5 @5 X1 y2 R' r% K
  5. 0.78539816339744833 V. R! g" R# s' ~  D( ^2 c3 l7 L
  6. >>> math.radians(60)+ j2 B' X! V  i: s) W
  7. 1.0471975511965976
复制代码
- b5 ?6 t& G' D: @0 \% G" T1 f4 j9 \( M
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
" I' e' L2 X. P; d. I4 ]
  1. #把y的正负号加到x前面,可以使用0, v- ~- |* |6 q+ ]+ ?) x* U
  2. copysign(x, y)
    + J) w- [- }! K/ [) D" V
  3. Return a float with the magnitude (absolute value) of x but the sign 7 T% Z; U2 ^' x0 j. v( @5 j
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    5 F# d/ D& G  L7 W8 a$ K; T9 r
  5. returns -1.0.% H5 Q: Y8 `! L4 S+ W

  6. 1 m/ K+ _8 b5 Y+ `: ~: V2 u
  7. >>> math.copysign(2,3)
    ; n1 o4 X; n8 K
  8. 2.0
    / ^8 j2 g6 d8 y/ i! W
  9. >>> math.copysign(2,-3)
    ' B, [8 ]/ O9 i
  10. -2.0
    1 z5 w( J! c3 {2 F* ?+ q
  11. >>> math.copysign(3,8)
    1 B8 H, r2 D  Y  [3 ~. `
  12. 3.0
    : ^) _6 N% j* c+ D/ n$ N
  13. >>> math.copysign(3,-8)
    4 C: ]3 \% Y. I3 M& N8 h
  14. -3.0
复制代码
/ b- `" s: }/ P0 N$ o1 R
math.exp(x)  返回math.e,也就是2.71828的x次方, [+ [0 C1 P0 \$ M6 N" A
  1. #返回math.e,也就是2.71828的x次方: m0 i; ~) A6 W9 f
  2. exp(x)$ H3 |" _/ R& t  v' D
  3. Return e raised to the power of x.
    . |6 B+ B( r4 t
  4. , q2 N/ B; H) s! v4 w1 ~
  5. >>> math.exp(1)" ^& H$ z- E/ g2 [: @
  6. 2.718281828459045; u; O' j/ d8 v$ t: W& |6 [) E
  7. >>> math.exp(2)
    1 f# h( s5 `! ~2 N' e
  8. 7.38905609893065( {- K7 Y9 H% ^5 b0 T
  9. >>> math.exp(3)6 h. l' s6 G. f6 E" Q$ ^
  10. 20.085536923187668
复制代码

  {# t+ `; I+ F1 z2 V9 Z5 V  Lmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
- D0 p5 h) {6 A: K2 c
  1. #返回math.e的x(其值为2.71828)次方的值减1- u% ^* ?4 @% E
  2. expm1(x)8 U4 q& P! a7 J0 ]$ C' m1 u
  3. Return exp(x)-1.# K3 D8 i8 b0 k$ z, Y  J& Y% w
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    5 w* Q7 S/ b, P- p- z

  5. & d9 X5 ^: Q+ T! F- ^6 z/ o
  6. >>> math.expm1(1)8 v0 N- t& E/ `( {! e: y! s/ p
  7. 1.718281828459045
    : @  T$ J# C: N7 M" h# y
  8. >>> math.expm1(2)
    5 M$ u/ P6 q7 V' {/ E. s# l, _; A0 R
  9. 6.38905609893065/ Y( E1 g* M- o4 A6 C2 i' \
  10. >>> math.expm1(3)
    ! H4 S' k) X6 [( Y& F4 X' q
  11. 19.085536923187668
复制代码
! B; }% R# \6 [( L& O6 p8 k: o
math.fabs(x)  返回x的绝对值
# T  }5 j" Z) S8 h) A4 Q% d- n, W
  1. #返回x的绝对值
    - w$ I& S7 [9 T
  2. fabs(x)
    " h: O1 ~+ a6 H* u; I3 w
  3. Return the absolute value of the float x.% U6 Z. \# o) j6 n9 \
  4. 0 ^9 ~, u6 `+ v3 u) N7 Z  @7 v
  5. >>> math.fabs(-0.003)
    ; ~9 x4 U8 t! ]
  6. 0.003, L  j9 {) f! `) T- x
  7. >>> math.fabs(-110)/ }2 j: ?  |9 v" U$ ?
  8. 110.0
    / X: ~( _0 |& S
  9. >>> math.fabs(100), n# [# B& x% H5 J% q
  10. 100.0
复制代码

5 p# ?! W+ H1 h3 K) jmath.factorial(x)  取x的阶乘的值$ T: E2 }" ], ~/ o% p/ g' ]
  1. #取x的阶乘的值0 F; a9 V2 c2 H4 p0 U- B
  2. factorial(x) -> Integral
    * ?4 M7 y& A( F
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ) w4 i- o7 B* C: n+ K+ y
  4. >>> math.factorial(1)
    ( y  E! U; z% h& a( w9 D3 i- u3 q
  5. 1' l$ e4 z' o  R3 v/ }
  6. >>> math.factorial(2)
    * j) M* {* a; p
  7. 2
    $ k+ `* m7 A" X4 I% f0 p% G' `
  8. >>> math.factorial(3)6 d4 z# H& r. F8 o5 l
  9. 60 U7 q' }: s6 L" G4 X, b
  10. >>> math.factorial(5)% H, N) W! m4 G1 N* E* j; n) l! o( y
  11. 120& f1 o( Q  l' W
  12. >>> math.factorial(10)9 G* u7 L( E! w
  13. 3628800
复制代码
5 V$ Q9 k' g+ N/ ^
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
5 |# P; L( @3 P( Z( t8 h2 W* M& h
  1. #得到x/y的余数,其值是一个浮点数- ~* ^6 B, h+ ~2 j
  2. fmod(x, y)
    8 z( u3 ]6 P* C
  3. Return fmod(x, y), according to platform C.  x % y may differ.* Y0 K1 K" y9 {2 V/ G7 Q- \/ H. \. g
  4. >>> math.fmod(20,3)- [9 N8 X9 r. C/ w4 s! Z0 D4 ?
  5. 2.0$ \; }8 C% {1 {  ~
  6. >>> math.fmod(20,7)& I! c& t0 e# i3 e
  7. 6.0
复制代码

5 z: N7 Y" E4 w2 c4 i3 @, cmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围( s; F: f6 ~8 Z9 Z7 p# }* L" K( n- H! e
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% w& C/ S  I. b
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值# w, t! u: `  C! b/ \
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ s  L/ ~. Z$ ]3 c
  4. frexp(x)2 {7 u! Q; _4 r* D0 Y; z: x! V
  5. Return the mantissa and exponent of x, as pair (m, e).' P+ ?; W3 R+ G: n) z# g" T
  6. m is a float and e is an int, such that x = m * 2.**e.
    * x5 m8 i) U" C5 g
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.5 [: H1 f9 @" l) L9 w& J
  8. >>> math.frexp(10)/ l( f  E, k# t7 U5 `1 ]3 ^. P
  9. (0.625, 4)7 d% Q' S* l, G6 Z* }4 d- W
  10. >>> math.frexp(75)
    0 m9 y/ n1 L# r5 ]
  11. (0.5859375, 7)
    8 w; U" K; v: v5 a
  12. >>> math.frexp(-40)
    + L; r$ G5 T9 [/ `  }
  13. (-0.625, 6)
    * u: M* E/ s1 n" l4 J/ Z4 X
  14. >>> math.frexp(-100)
    $ a0 N# g/ S2 [6 M
  15. (-0.78125, 7)2 Z6 R& a' B$ e# U( r( A: [0 w8 n
  16. >>> math.frexp(100)! N' n7 z( N: H. D: v
  17. (0.78125, 7)
复制代码

& \' x! v) k$ E. D% {+ F' }math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
$ u# Q9 ~. f+ r2 k9 X
  1. #对迭代器里的每个元素进行求和操作& T0 X5 R1 _+ N, x' n' e
  2. fsum(iterable)0 C! L* p0 [) t
  3. Return an accurate floating point sum of values in the iterable.
    # U4 G+ b7 h/ _. z& \6 p* C
  4. Assumes IEEE-754 floating point arithmetic.
    1 ^1 z  z  p# z0 Z
  5. >>> math.fsum([1,2,3,4])
    # p' K2 N  ^% j. d8 \9 g8 a
  6. 10.0/ f& Y8 a5 B4 P. M2 I  F/ j
  7. >>> math.fsum((1,2,3,4))
    ) u( j) h4 p7 h$ @- h8 L
  8. 10.0
    4 Z3 c( P4 Q, k5 v. {$ B
  9. >>> math.fsum((-1,-2,-3,-4))
    - Z2 R; N8 p+ f9 w" y% D
  10. -10.0% U, V' ^$ O- P
  11. >>> math.fsum([-1,-2,-3,-4])& c. k* h/ Q9 ?* W8 k, M# R
  12. -10.0
复制代码

6 ]& }% J# _. p+ qmath.gcd(x,y)  返回x和y的最大公约数
  N4 [" w$ Z. t; J' x9 w0 ]5 H
  1. #返回x和y的最大公约数
    7 y- n  h3 O8 ~  p
  2. gcd(x, y) -> int
    6 X  |* G# y0 i5 P
  3. greatest common divisor of x and y
    8 _# Y% C4 D' F! T/ W! f
  4. >>> math.gcd(8,6)3 G- E+ V. @- i0 ?3 \; t) L
  5. 2/ i4 W( D+ j& _4 K# R1 N% w3 k2 y  H
  6. >>> math.gcd(40,20)
    8 _' c0 `; |! B9 V8 m5 Q# G
  7. 20
    ) y* f5 T( O0 m
  8. >>> math.gcd(8,12)
    ; s! I( [8 j/ z* Q3 [
  9. 4
复制代码

/ h, z& C0 W' V" ~* w* Xmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
4 O; m3 J' J5 M8 w$ e9 w) p
  1. #得到(x**2+y**2),平方的值) h3 W4 K$ |" B6 C
  2. hypot(x, y)# y/ P- F) V0 D# b1 W3 y
  3. Return the Euclidean distance, sqrt(x*x + y*y).  P, U& A" d; V! U) \
  4. >>> math.hypot(3,4)
    7 ~0 |) G9 v* K) l
  5. 5.0; R+ _9 q" Z+ s) n# R- S
  6. >>> math.hypot(6,8)/ ~9 r% |5 C4 I6 K
  7. 10.0
复制代码

( q2 E* {2 ^* _math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False- v9 p) ^# i1 B+ ]
  1. #如果x是不是无穷大的数字,则返回True,否则返回False5 }$ G. b/ h% [! A( z! Y
  2. isfinite(x) -> bool+ t6 R3 j" d# J& E
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    / R) S- T2 v+ ?7 f
  4. >>> math.isfinite(100). x% J& K5 ~9 Q4 C# i9 ~
  5. True
    , B% w/ j  K- m  l
  6. >>> math.isfinite(0)
    6 w/ R# w! z4 O* s' g5 h% D; K
  7. True
    0 ~" Y) _# L1 \( b* `8 S
  8. >>> math.isfinite(0.1)
    & N/ Q% W! _  z
  9. True
    " l* j, ~& @0 y$ e* m. K9 X
  10. >>> math.isfinite("a")
    " I6 b8 k. ?3 K& n, g; z* F0 e# E
  11. >>> math.isfinite(0.0001)
    $ |; V9 F, t, k* o  S+ i1 B
  12. True
复制代码

, v9 l8 [6 v- g0 n' o2 d. Q8 T% Imath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False% X7 G' ~1 i, {; _% k
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False' {3 @+ u. L+ N0 \$ K  j
  2. isinf(x) -> bool# e6 y) @& J- o9 ?! d
  3. Return True if x is a positive or negative infinity, and False otherwise.
    0 N' z5 S& i. n1 Q" d$ l% j2 e
  4. >>> math.isinf(234)
    # T% f8 \3 T% D6 v+ l: i
  5. False' S. z8 l0 Y* U; U* E* U) p, H
  6. >>> math.isinf(0.1)" D3 s. Z7 C( J! u$ |
  7. False
复制代码

+ t7 ^. @0 z  F; [; smath.isnan(x)  如果x不是数字True,否则返回False/ C6 X  F$ i* \; E" P- W  q
  1. #如果x不是数字True,否则返回False6 n1 F! C: U0 ?8 p. s
  2. isnan(x) -> bool
    ; t' L; w# O9 H# v. Z2 _+ b9 G
  3. Return True if x is a NaN (not a number), and False otherwise.. x; Y, z+ D1 [$ |6 |% @' v# n
  4. >>> math.isnan(23)
    , N0 v+ ~$ H2 n0 G
  5. False* u* W# Q7 U  r$ s
  6. >>> math.isnan(0.01)5 E! G4 G5 O* H$ }
  7. False
复制代码
  F: l2 ^! E- H* G% d7 D8 M0 L
math.ldexp(x,i)  返回x*(2**i)的值1 X9 f% j4 [' A6 _1 G+ J
  1. #返回x*(2**i)的值7 E6 ^* w" o9 x* g1 Q" s
  2. ldexp(x, i)
    6 C. `2 i2 N% u7 L: d: k
  3. Return x * (2**i).
    2 q  ]. A/ X: ^
  4. >>> math.ldexp(5,5)
    ' E- ?% ?# {: o* ~) j
  5. 160.0
    1 i* U) A  s& ~
  6. >>> math.ldexp(3,5)
    ) w* n! J5 m5 T) D9 E8 c# ], D$ r; B
  7. 96.0
复制代码
! y: {! d" {2 J& z, K7 Z0 l
math.log10(x)  返回x的以10为底的对数+ w7 ?! k' X2 V8 R, V/ i& z
  1. #返回x的以10为底的对数
    ; I( U0 ], Y5 `4 N8 N5 h$ V! Z0 s7 R
  2. log10(x)! f1 m9 x  P3 @
  3. Return the base 10 logarithm of x.
    ) D* ^6 O2 ^5 P( g& Q
  4. >>> math.log10(10)6 q, l, {' j7 c, t
  5. 1.09 B! ~% r9 r& s  |
  6. >>> math.log10(100)
    $ N( N. Z" S3 q
  7. 2.0
    * w: L* ~% r  K' B8 O6 g, Q4 |
  8. #即10的1.3次方的结果为20
    ; a5 d# T% X" S% o- A) l7 g9 s
  9. >>> math.log10(20)! e* d1 g8 Z7 k, I( z% s- k
  10. 1.3010299956639813
复制代码
. K/ e2 \" N) E' R& e0 ?7 Y
math.log1p(x)  返回x+1的自然对数(基数为e)的值2 U1 E0 K' q$ P
  1. #返回x+1的自然对数(基数为e)的值
    % p1 j$ F7 @9 v+ N; }
  2. log1p(x)  P7 H  t7 N' _6 l! V% z& y
  3. Return the natural logarithm of 1+x (base e).) K9 h1 D7 R) M+ ]; g: h) m
  4. The result is computed in a way which is accurate for x near zero.
    9 D$ y5 M& l9 h. [0 p
  5. >>> math.log(10)0 b8 k( p, U2 O# [9 L- y% u. b2 R0 q
  6. 2.302585092994046$ J; ~. e9 ]- G2 K
  7. >>> math.log1p(10)
    % ], W) l7 ~' D) x5 O* z% ?. A; p
  8. 2.3978952727983707
    4 f3 ]- D% X# Z+ f* g
  9. >>> math.log(11)
    ' x8 Y3 F/ f+ o" d+ m
  10. 2.3978952727983707
复制代码
$ e( v; i: H* g
math.log2(x)  返回x的基2对数2 ~- J2 I5 p* Q$ \$ t7 h
  1. #返回x的基2对数( I' U4 [! y* F4 p" C0 I) B
  2. log2(x)
    4 i# U; R! a0 u3 o* H" e
  3. Return the base 2 logarithm of x.
    & P; F. M1 M4 c2 Z/ w& n# g
  4. >>> math.log2(32)
    , C  p! ]: i4 [2 d* W9 `6 ?5 i
  5. 5.01 A4 T, w# ?/ W& S  H
  6. >>> math.log2(20)! y& u4 k+ W' n  S1 @" X
  7. 4.321928094887363
    * s; c& v) Z) J
  8. >>> math.log2(16)+ h& J# p2 c4 ]- ~
  9. 4.0
复制代码
' e' g/ s4 g! u+ X+ x
math.modf(x)  返回由x的小数部分和整数部分组成的元组
  A7 z) V; P) H
  1. #返回由x的小数部分和整数部分组成的元组- F& T6 L: |# ?/ V" |: f
  2. modf(x)
    & \7 t% Q# i. [4 \; b9 D1 b2 y" r) S
  3. Return the fractional and integer parts of x.  Both results carry the sign
    4 [9 [, l/ d! ^+ N+ |0 i
  4. of x and are floats.7 |4 W4 o) `- D0 |7 q
  5. >>> math.modf(math.pi)  Q8 T2 w) o6 i4 @  b- n- u
  6. (0.14159265358979312, 3.0)
    ! s& |5 ?( q6 T! x( A4 ]& c
  7. >>> math.modf(12.34)' ]3 ~% J; f+ s+ \& r" ?
  8. (0.33999999999999986, 12.0)
复制代码

* n% J2 @( G" P& Z. ymath.sqrt(x)  求x的平方根# k2 w3 b3 S( }7 B, \5 l6 ~
  1. #求x的平方根- K- K( U7 b! s" z% A
  2. sqrt(x): Y5 @! u1 J: x0 j
  3. Return the square root of x.
    ! D7 _9 V1 c: E3 k- K4 e6 [
  4. >>> math.sqrt(100)
    7 s0 ]/ E% G# E  Z+ ]8 m# [
  5. 10.0" m+ W/ _6 U2 `
  6. >>> math.sqrt(16)
    9 N) G1 _% V7 k
  7. 4.0
    & O7 G5 ^( b1 Y" ?
  8. >>> math.sqrt(20)
    8 V6 D- y1 a  L, r. I& }3 z
  9. 4.47213595499958
复制代码
, z/ |1 t1 l  k
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分. a/ a% k2 b5 M% b6 N% _
  2. trunc(x:Real) -> Integral' f8 ^% u5 R1 b/ M+ O! w
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method." [) ]3 d9 c3 P2 k# I* Z5 g6 L
  4. >>> math.trunc(6.789), l7 J2 L  O4 O1 i. W
  5. 6' |5 E+ ^& f3 k
  6. >>> math.trunc(math.pi)
    9 C. m$ O2 A$ {- o1 E7 L& q! {
  7. 3
    , p7 K9 w( P( P( _* Q
  8. >>> math.trunc(2.567); g7 b9 |/ ~5 W5 r/ f) d
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-6 06:21 , Processed in 0.083711 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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