新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
) G- X! Y; O  |8 |1 @1 y0 I
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。9 v; e2 E0 B" m# n3 v  ~, f

! y% h* z% G2 H8 Z; \方法1; C& m7 E( H/ l* w
  1. >>> import math8 v) d) d# A0 t  f, o; s( w2 `
  2. >>> math.sqrt(9)# S& f$ x' o- i; Y
  3. 3.0
复制代码
方法2* t6 k- b' ]: Q7 l: G
  1. >>> from math import sqrt, B# T5 f2 U# I- L# w9 X, `7 q
  2. >>> sqrt(9)7 I3 y) I& t% J3 r, d; ?8 ]. H
  3. 3.0
复制代码
5 E* X' |8 ^! {) L

; h& ~$ {$ d, @% y% ]. Q# |
math.e  表示一个常量
8 q0 Q  F) F& o
  1. #表示一个常量
    ) p' `/ Z9 H; V% t- T' Q! N
  2. >>> math.e
    # @$ T; j5 n: A5 r+ z" E' z5 J" I- O" A
  3. 2.718281828459045
复制代码
) i) B% ~7 o' q- k# g5 v9 }% O
math.pi  
数字常量,圆周率
* D& ]5 Q- u9 g; @: l/ m! \
  1. #数字常量,圆周率
    ( ^) Y$ q, T' U6 Z
  2. >>> print(math.pi)
    4 J1 x/ P9 g# U
  3. 3.141592653589793
复制代码

3 K. a* M- k% Q/ dmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
& S+ ?- K) {3 Y6 Z. n7 S; u
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    1 S) @6 s7 b1 M  E3 c6 s4 w/ E
  2. ceil(x). t# T  c/ R+ E1 Q# E4 ~
  3. Return the ceiling of x as an int.
    4 A2 }" O) N6 u6 X8 q% u
  4. This is the smallest integral value >= x.
    0 Q" E% r& P+ b" ~: {1 u% h

  5. 1 U0 u; B) Q  Y: j  e8 D* F" u
  6. >>> math.ceil(4.01): {( m) P+ n% X- ~# A% y! L6 o
  7. 5) e9 P+ ~% D' |+ {& ?6 s6 w
  8. >>> math.ceil(4.99)
    5 F. y; A) ]; Y9 l( _& k
  9. 5, }% j" c0 l" ?, o' G
  10. >>> math.ceil(-3.99); g) ~# N: I$ F1 _# e
  11. -3/ m5 c4 ~  n' y0 ?# }
  12. >>> math.ceil(-3.01)9 K* K, A  ^, S5 k- D) b
  13. -3
复制代码

4 v$ U8 s6 g0 h: fmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身) l+ X2 ^* i( Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 S3 u0 s# Y, w8 T  Q
  2. floor(x)+ K5 Q1 X2 O, g* z) ?
  3. Return the floor of x as an int.
    , d: M( y" ]7 v2 F: B
  4. This is the largest integral value <= x.  }5 |/ _; C3 }, A) _; R6 g4 o
  5. >>> math.floor(4.1)( i) \$ b& q& H% x2 `0 z! R
  6. 4
    2 w* p/ d- ], c) V5 c% f6 R
  7. >>> math.floor(4.999)
    9 ?' Q$ F2 P% q+ Z' d8 m
  8. 4
    5 j) l2 T! n$ G, Q- f, _. s+ K$ b
  9. >>> math.floor(-4.999). s1 }8 Q2 c$ |8 F: {! C
  10. -5
    ; `9 F1 O7 \  z5 f
  11. >>> math.floor(-4.01)
    9 w' r. T& |- n
  12. -5
复制代码

" F' ^3 c' r1 b9 bmath.pow(x,y)  返回x的y次方,即x**y' _) J# \) W) R; y+ M
  1. #返回x的y次方,即x**y
    $ j: S  `' f. K
  2. pow(x, y)/ y0 g" {# U/ e9 ?( h/ i) G  p+ [$ U
  3. Return x**y (x to the power of y).( Z6 U( a: Q" ^0 q! t
  4. >>> math.pow(3,4)0 o% i! z# `+ V; Z: N: G6 o
  5. 81.0+ h9 N# Z; E$ w8 j7 z$ q+ f
  6. >>> - h$ d' G/ Z& Y' A$ F5 k* J' \) ~
  7. >>> math.pow(2,7)( u2 y" i+ D5 A& q
  8. 128.0
复制代码

2 e; }+ {7 S  I) d/ n7 `2 Wmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" X8 C& l$ y  J
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 V/ b' p; U6 {9 C  F5 w& S
  2. log(x[, base])
    % u( |' F2 @+ ^
  3. Return the logarithm of x to the given base.
    ( v$ D$ D- m# m) R
  4. If the base not specified, returns the natural logarithm (base e) of x.
    1 o0 _2 E( U) v4 ?) m5 x( }! D
  5. >>> math.log(10)! a( q/ S& }  ]: R% C9 P! v) L
  6. 2.302585092994046
    5 p3 f2 R9 K* |
  7. >>> math.log(11)
    + l3 ]4 ~8 w$ R+ `6 }/ z
  8. 2.3978952727983707
    5 g0 q* J, r" |0 E8 K. j8 [$ U
  9. >>> math.log(20)
    2 _8 H7 t" M* C+ R8 a
  10. 2.995732273553991
复制代码

7 u+ D: t3 Y: P8 amath.sin(x)  求x(x为弧度)的正弦值/ r/ z/ P, ]. D' J* L
  1. #求x(x为弧度)的正弦值( B3 [9 }3 ~5 y, W5 w8 u' V
  2. sin(x)+ i) c5 v3 Y" G# n& D/ p+ |
  3. Return the sine of x (measured in radians).
    2 }# S' z$ N' A6 V% G
  4. >>> math.sin(math.pi/4); a: s5 q7 [% j# S) l7 a: s( Z% d
  5. 0.7071067811865475! y4 W0 z& C) s: S( I" I$ L" d2 w
  6. >>> math.sin(math.pi/2)
    6 D5 o" i! x7 m/ |8 \, }0 k& J
  7. 1.0
    ; ~3 q" U, B4 ^) m1 Y' r
  8. >>> math.sin(math.pi/3)8 e0 I- c9 D  o5 ~# H) U3 _3 ]# ?2 o+ Y
  9. 0.8660254037844386
复制代码
" s$ {1 t3 m# V& Z9 q& U- j
math.cos(x)  求x的余弦,x必须是弧度
) z* Z  Z/ R2 O7 K+ c! {; ]+ `
  1. #求x的余弦,x必须是弧度
    9 K* i* F6 f5 }  [4 |" l" N
  2. cos(x)
    3 p. ~) S2 w2 u% O) n9 [1 I. V$ S
  3. Return the cosine of x (measured in radians).
    ; a5 e2 ^3 Q. w9 T+ `
  4. #math.pi/4表示弧度,转换成角度为45度
    : j- x" A4 u4 |  L) F
  5. >>> math.cos(math.pi/4)0 e. i8 D0 p. l* O/ g
  6. 0.7071067811865476- n# h  n5 {; {% D( m, N! }
  7. math.pi/3表示弧度,转换成角度为60度: l% c  O6 a1 m# T- C7 Z- ^: v
  8. >>> math.cos(math.pi/3)$ s0 t4 Y& u% }1 g' y
  9. 0.5000000000000001
    ( s1 s. n1 X: n% I3 s/ F
  10. math.pi/6表示弧度,转换成角度为30度$ P* R- E8 ?6 C; [
  11. >>> math.cos(math.pi/6)( q, ^2 A3 N5 n8 J" ]
  12. 0.8660254037844387
复制代码

4 q. K  r; o1 m1 u, [) ~math.tan(x)  返回x(x为弧度)的正切值4 p' J  ~" e& H6 o! [) \3 O
  1. #返回x(x为弧度)的正切值
    # O6 H/ p; q6 s! Y9 l3 w
  2. tan(x)
    ! P1 v4 K4 i/ W
  3. Return the tangent of x (measured in radians).% j+ Y0 S5 B& l% l( O
  4. >>> math.tan(math.pi/4)
    $ G; r7 U; b0 Y* Q- p
  5. 0.9999999999999999
    6 u9 a, L4 _/ y7 P; r! U
  6. >>> math.tan(math.pi/6)
    # n1 g: l# \  d! q2 j% p" t
  7. 0.5773502691896257
    . C8 l9 e& ?! K. s% b' c
  8. >>> math.tan(math.pi/3)
    - r+ b" v9 _: S2 O5 Y% G+ }  E
  9. 1.7320508075688767
复制代码

) }2 A# s; q9 W8 X+ Rmath.degrees(x)  把x从弧度转换成角度) ~& A" |# O7 ?* t, K0 A
  1. #把x从弧度转换成角度
    & j% j$ Y4 q0 a* u
  2. degrees(x)
    ( z. p, H$ A( o/ I7 \' F
  3. Convert angle x from radians to degrees." ]6 l5 w* R* t5 S

  4. $ ~5 r9 o! W$ A" Q  U7 B) p
  5. >>> math.degrees(math.pi/4)
    ! P5 h% O% W. Y, Q- {- Q2 X
  6. 45.06 P5 L0 O8 \/ |9 a2 g9 E" s
  7. >>> math.degrees(math.pi)
      r" _4 k! O5 }, A/ m0 m" L
  8. 180.0
    8 j! Z4 N" Y- z. i7 h1 v- }& M
  9. >>> math.degrees(math.pi/6)
    8 Y. _; f! N) q% n, i
  10. 29.9999999999999969 X; J* a9 y8 w% W
  11. >>> math.degrees(math.pi/3)
    ! Q; D6 _4 X6 u
  12. 59.99999999999999
复制代码
0 r7 O2 v' Q8 v+ r/ m6 j1 C
math.radians(x)  把角度x转换成弧度
& s$ Z3 X( p0 |) h9 K4 w+ J
  1. #把角度x转换成弧度
    1 y# ^4 e6 E, Q! o
  2. radians(x)9 `& ^! a/ Y9 @) A) T
  3. Convert angle x from degrees to radians.
    ) Q; H3 Z2 q- X: v
  4. >>> math.radians(45)
    ; v1 Y* {! s8 R+ {  G
  5. 0.7853981633974483& @7 e' Z0 [3 I# H
  6. >>> math.radians(60); H9 t# U+ e5 v2 r6 ?! r
  7. 1.0471975511965976
复制代码

$ {$ ^8 S4 v3 P3 W  pmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
! C5 C0 _5 w' n% ^% a. H
  1. #把y的正负号加到x前面,可以使用0  B" N7 f% m- L* o: R% ^& J
  2. copysign(x, y)9 i/ p2 _' b  ]+ F% i3 F7 m( l5 \
  3. Return a float with the magnitude (absolute value) of x but the sign ! y" `( L( t% F; U3 k
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    & C% O/ a2 T+ ]& Z
  5. returns -1.0.! u* {2 N/ @# |' Q: z1 h4 Z

  6. 1 ~) U3 ^% d5 W3 z! |6 B
  7. >>> math.copysign(2,3)/ v% U" L1 C8 R$ |  g5 ~, g4 x
  8. 2.0
    * Q; B- f7 b5 T& ~& v3 P% y! N
  9. >>> math.copysign(2,-3)
    9 X! I5 t1 _7 X, s- W) l8 [* `  m3 b
  10. -2.0
    . ?$ _) p' O2 }' L
  11. >>> math.copysign(3,8)1 L5 g9 V7 y- G# m" l4 ~, W1 H
  12. 3.06 r4 r. l4 M* x( m/ r
  13. >>> math.copysign(3,-8)
    % G: o0 ]  c: G
  14. -3.0
复制代码

) y$ c, _% w/ U" J) ~math.exp(x)  返回math.e,也就是2.71828的x次方+ [( {; F* F! C
  1. #返回math.e,也就是2.71828的x次方! P! ], u+ u) f
  2. exp(x)& Q" ^$ X6 E: e; @9 C' t9 h2 ]* E
  3. Return e raised to the power of x.! ?/ ]8 V+ |* n8 N
  4. 9 V2 Q# D5 L# i2 i. f6 I1 w, E8 D$ v6 d
  5. >>> math.exp(1)
    5 K+ r' @2 Y& \2 e* Q. m. ?
  6. 2.718281828459045; S( P) k6 G; Z; b# Z
  7. >>> math.exp(2)/ U* i# L  ~5 p) }
  8. 7.38905609893065
    7 o- y+ i2 m0 r* |" g- X
  9. >>> math.exp(3)2 ^& L( S" n# o
  10. 20.085536923187668
复制代码
! b4 O8 I( W. A) S# p
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减17 Y: \, C, l/ i9 x( l: i& s7 \" X
  1. #返回math.e的x(其值为2.71828)次方的值减1- m4 R2 n( f# ?/ ?
  2. expm1(x)
    # C- ]1 b' |, ]2 u( o8 N( w
  3. Return exp(x)-1.8 r3 v# c; P+ Y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    & _" }' i6 @* T+ L# B: {
  5. 1 Y9 ^5 @4 o& o1 x/ L+ l0 }
  6. >>> math.expm1(1)- [3 f  F. `6 H3 r# p
  7. 1.7182818284590453 E' q* }: w8 f" |- x3 f/ H  i
  8. >>> math.expm1(2)
    - _2 b$ I) l4 [) H& m
  9. 6.38905609893065. f# _% S* K8 M6 \- }% u
  10. >>> math.expm1(3)
    9 }0 H, c- n' O. d$ ?. T  X
  11. 19.085536923187668
复制代码

/ h; w# a- M/ ^4 b6 h+ H1 D) jmath.fabs(x)  返回x的绝对值6 X' H: H" ^: k; Z1 P
  1. #返回x的绝对值" Y+ _2 N7 N" f: a5 m
  2. fabs(x)# [3 t: j2 U5 O  ^# v) Z
  3. Return the absolute value of the float x.
    6 b( c. D" @! Y0 G" n

  4. 9 p' Q" ?+ \% |$ ~; K1 e
  5. >>> math.fabs(-0.003)9 N7 i( F9 t' ~
  6. 0.003
    9 t$ \8 i4 i' ?+ y- k$ J+ s
  7. >>> math.fabs(-110)
    2 @& u& e2 t6 A7 k: O0 W; v' p) P
  8. 110.0
    / _% h- X1 R0 Z1 N  B4 n6 |
  9. >>> math.fabs(100)
    ! G( P3 F9 G/ k) ~7 y; M* S* y! k
  10. 100.0
复制代码

! d: p$ C# K( `  m9 l) h7 Imath.factorial(x)  取x的阶乘的值5 g! q3 D. k0 M( ]
  1. #取x的阶乘的值6 L& {, A8 y* S
  2. factorial(x) -> Integral
    * {" V  H# ]. w. A7 E
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    & C) e& D- A3 G+ s( T
  4. >>> math.factorial(1), ]* s5 ?6 j! a; ~2 Y; E
  5. 17 [6 ]6 O! S+ e: ?& w8 H( r
  6. >>> math.factorial(2)
    : f9 U1 K4 N! d) U) n7 X* n
  7. 2
    3 ]$ F  j* D6 r4 C. [4 |. X& G
  8. >>> math.factorial(3)5 D2 E( u7 T8 X' S8 V( q3 l
  9. 6
    * r, O& d* b5 u8 f0 d
  10. >>> math.factorial(5)8 z( J5 g4 D7 A" N( V1 w3 [
  11. 120' U' P1 f9 S: m! t% K
  12. >>> math.factorial(10)
    ! C9 H5 C2 A$ J# F1 k! K9 b9 |  A
  13. 3628800
复制代码
7 Z+ _8 @$ _, }: i
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数6 H- s) p8 J8 [. e( M4 `9 c4 _1 p- k
  1. #得到x/y的余数,其值是一个浮点数
    2 x. c& s6 b- H# j8 j) R, s7 [- `
  2. fmod(x, y)
    ! N, T2 [" E6 W9 m  Q
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    3 q& B. p$ p' J4 R' H/ U- z9 H
  4. >>> math.fmod(20,3): o  j% ^+ [1 U  T( |
  5. 2.0
    / @$ h2 {$ _% S, b
  6. >>> math.fmod(20,7)& t' v$ t4 n6 F
  7. 6.0
复制代码
1 o3 e/ D: G9 H" O' {( D! x
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
# M. n! F* N* \0 L( _# p' r9 y
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,) @/ ?  `! ]8 h8 M  @+ g2 A' v
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    4 y# @$ g7 b' F" U
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1' X! q, ~# Q3 d  _  o6 F2 \9 y
  4. frexp(x)9 f6 ?/ B. D) O, i- m6 H
  5. Return the mantissa and exponent of x, as pair (m, e)., x4 r! N  `8 D* Z4 r4 W& c8 G
  6. m is a float and e is an int, such that x = m * 2.**e.& b4 I! B+ N  [( X: Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    / X' `  Y" C5 S/ F; J( D4 ~
  8. >>> math.frexp(10)
    # ^- R! R8 U9 {  J9 {
  9. (0.625, 4)1 L! k7 L+ V: g% [6 N3 K
  10. >>> math.frexp(75)
    $ b" h, [% x3 ?$ _' o
  11. (0.5859375, 7)# F. x* O6 i6 h& Q! k
  12. >>> math.frexp(-40)5 d) }4 a/ d# r& y$ r( c/ D4 g: P
  13. (-0.625, 6)  T: s! e# [& ^
  14. >>> math.frexp(-100)
    5 C  ~* ]$ q: O  O
  15. (-0.78125, 7)/ F& G4 `$ r5 _7 c' b7 @
  16. >>> math.frexp(100)
    5 Z+ U% W' e# r' K3 @6 ^$ {4 ^+ m) E  N
  17. (0.78125, 7)
复制代码
0 G2 |% x' x8 F& W
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
3 l' P) ]9 ^  J6 f4 `
  1. #对迭代器里的每个元素进行求和操作
    " S+ h$ z7 C) ~( \- t
  2. fsum(iterable)
    & {- @8 m( f6 l( x; M' |
  3. Return an accurate floating point sum of values in the iterable.
    # k6 D; F+ x, I! ~5 N' e$ B; o' x
  4. Assumes IEEE-754 floating point arithmetic.
    , |% Q# l! O) z6 q. I
  5. >>> math.fsum([1,2,3,4])
    3 ^0 a" f% M1 f2 |
  6. 10.03 T% \# ^1 A+ H/ [* C5 ?
  7. >>> math.fsum((1,2,3,4))) w, C$ [4 f& V2 I2 O7 q
  8. 10.0$ R' a4 W1 d3 E4 _5 }- T
  9. >>> math.fsum((-1,-2,-3,-4))5 C% C  g- b3 W7 p; f- ^
  10. -10.0
    7 w. d+ q1 |) d4 H! X
  11. >>> math.fsum([-1,-2,-3,-4])
    , C8 @% m, ^& K) M8 z0 I
  12. -10.0
复制代码

) A" J( D; N2 nmath.gcd(x,y)  返回x和y的最大公约数
7 u  D9 _) A1 \- K: Z  {5 ^
  1. #返回x和y的最大公约数
    9 n4 f4 {1 o( F9 Q6 ^
  2. gcd(x, y) -> int
    ! h) U! n2 T5 Y5 }3 f
  3. greatest common divisor of x and y) c3 a% W  G6 [. E& ]
  4. >>> math.gcd(8,6)
    $ \+ {! y7 E( m, h
  5. 2- H- h  g' \* `/ L$ u6 {
  6. >>> math.gcd(40,20)
    ) r* O9 j% f7 w: ~
  7. 20
    9 z7 a* y4 t- C5 r2 S; }
  8. >>> math.gcd(8,12): x, H2 E. K7 }, s: U$ N
  9. 4
复制代码
, X. p) x9 j( }7 @/ M' f0 q. p: L
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False. y1 g, L% [( q9 [* u
  1. #得到(x**2+y**2),平方的值0 n. J! r% k! O9 W  U
  2. hypot(x, y)
    2 t. H) K2 f) a! \# o' R- k3 D
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 P5 j. N1 w2 r
  4. >>> math.hypot(3,4)
    9 Y, L, ^" R9 i4 J/ L1 G5 \
  5. 5.0" b3 a4 f8 h7 M+ Q: S
  6. >>> math.hypot(6,8)
    5 Z: k1 ^0 @8 T  f- _
  7. 10.0
复制代码
$ n& _6 }8 p4 Q9 d8 o5 P
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False3 B% I" h+ D8 U& }- C$ A2 j
  1. #如果x是不是无穷大的数字,则返回True,否则返回False& v! {) p* u7 J/ j/ Z/ s
  2. isfinite(x) -> bool
    / Z% A+ A: U) \; j0 V/ q
  3. Return True if x is neither an infinity nor a NaN, and False otherwise./ f( F0 D) I" A3 E3 A
  4. >>> math.isfinite(100)
    6 _! a- H/ L8 }% b
  5. True* S$ e9 z3 l, Q$ ^
  6. >>> math.isfinite(0)) I: D2 R1 Y7 f/ q1 o
  7. True$ \& T4 h4 i+ x
  8. >>> math.isfinite(0.1)! ]* Q* ^& s; s8 G
  9. True9 G* O1 _$ U* q+ L3 }8 L; k& F. T
  10. >>> math.isfinite("a")( E; R5 S% _3 X; w6 A1 v+ A5 d: n" E- J
  11. >>> math.isfinite(0.0001)
    0 d# X$ Q9 s( {, M1 g: S
  12. True
复制代码
& ^/ p, h, B4 ^& R* j* c! D
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False0 e$ x1 M' L$ j: ^5 v" V
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ) V! U; r8 g: Q6 S7 ]
  2. isinf(x) -> bool* H' T; m( b1 O) r
  3. Return True if x is a positive or negative infinity, and False otherwise.
      {. [; O; c+ S
  4. >>> math.isinf(234)- n2 d! ?& o9 {$ i& [  Q) l
  5. False6 c! S+ y5 W, A7 e, L& s; E
  6. >>> math.isinf(0.1)
    ; V' A- b- v8 ]: D
  7. False
复制代码

7 y" s/ M; l8 ~# xmath.isnan(x)  如果x不是数字True,否则返回False. a0 u3 _7 ^. Z- G. f' |
  1. #如果x不是数字True,否则返回False0 H- h8 k5 }8 \& F: u3 f' t
  2. isnan(x) -> bool2 Y* r* Q# y6 H
  3. Return True if x is a NaN (not a number), and False otherwise.
    * k! T- \* N' B" }3 R+ A9 [( U
  4. >>> math.isnan(23)( e/ ^( y& d7 l5 g& Z- j$ u
  5. False
    8 Q) D1 v: \+ U$ ?1 \) j0 i& A- P
  6. >>> math.isnan(0.01)" E8 u* J* ?  j6 e0 d
  7. False
复制代码

! V# ^+ K+ H4 B* Mmath.ldexp(x,i)  返回x*(2**i)的值$ c# u+ G4 b+ a8 x
  1. #返回x*(2**i)的值
    ) l; o. p1 F# p7 l
  2. ldexp(x, i)
    . h& W% j4 e) |+ t3 p3 Y
  3. Return x * (2**i).
    1 ~& b0 Z& o! U
  4. >>> math.ldexp(5,5)) Q. Y. i2 H3 I8 }3 f0 @( [8 v
  5. 160.0( z4 l" d2 O7 {6 [' {/ r
  6. >>> math.ldexp(3,5)( e0 E0 J- T% W$ p* v
  7. 96.0
复制代码

9 @5 k; x; a5 s- }7 Gmath.log10(x)  返回x的以10为底的对数
! N; s4 \! x' j: S) U+ o
  1. #返回x的以10为底的对数% h. z: M6 n0 L2 B2 z. `' D
  2. log10(x)
    & @0 K2 ^, K: F( j2 ^! {9 n5 ^/ e
  3. Return the base 10 logarithm of x.9 Z7 u2 I* Z) V6 ]: N3 m, t5 U
  4. >>> math.log10(10)
    ; e- Z8 T" N: d. w1 d; i' E4 v
  5. 1.0
    6 m2 Z5 L8 A& g) N' j" t
  6. >>> math.log10(100)# x) ^& v! l* @
  7. 2.0
    6 o) @- j1 y; K" z# u! I
  8. #即10的1.3次方的结果为20% F& V: ~5 j# b, \, r9 p: X
  9. >>> math.log10(20)
      P5 k; k0 F- p  I
  10. 1.3010299956639813
复制代码

+ _! ?; T) l! A! Dmath.log1p(x)  返回x+1的自然对数(基数为e)的值
/ E8 G8 D2 y/ o$ k
  1. #返回x+1的自然对数(基数为e)的值
    3 ^6 I' u! l0 D0 F( b& V
  2. log1p(x). A. Q) T9 a$ k0 A6 S# z0 w" I
  3. Return the natural logarithm of 1+x (base e).7 o3 \$ x8 ~' h+ W5 `( j' `+ G
  4. The result is computed in a way which is accurate for x near zero.
    ; q3 J$ {+ _1 m9 b7 x
  5. >>> math.log(10)* h4 z  e! z9 S
  6. 2.302585092994046
    $ @' T8 k/ i% G! ?( `
  7. >>> math.log1p(10)7 F( d5 i3 n2 u8 @' v
  8. 2.3978952727983707* u5 C4 E; ]  d+ h  \
  9. >>> math.log(11)
    ( S  S. n4 G& ]( |+ U. w3 S
  10. 2.3978952727983707
复制代码

3 F  t3 Y- M/ E2 W: B3 i8 M  q6 |; P. ?math.log2(x)  返回x的基2对数, u: T; {6 ?( F/ O9 X  L1 {) B
  1. #返回x的基2对数
    2 N# W" Z, z( x  z, A. L
  2. log2(x)
    - A6 J, F6 b4 ?7 K7 W( x; n( T
  3. Return the base 2 logarithm of x.
    # v% m, ~  u9 Q
  4. >>> math.log2(32)
    9 A& C  w0 O3 S
  5. 5.0  t& M: ]! j. x4 n9 p- {
  6. >>> math.log2(20)
    7 w, G, v  W2 j4 z" R+ W6 s3 b
  7. 4.321928094887363. [0 g. ~( p( n
  8. >>> math.log2(16)
    8 L4 k% Q0 ?/ p
  9. 4.0
复制代码
& C, w1 k8 t  S9 V1 H
math.modf(x)  返回由x的小数部分和整数部分组成的元组( r2 ~: U- `  {9 _) p: q
  1. #返回由x的小数部分和整数部分组成的元组
    ( T) e1 O3 r0 T
  2. modf(x)4 v8 S$ Z1 W) _- K! c
  3. Return the fractional and integer parts of x.  Both results carry the sign( r4 b4 `: ?1 S1 U
  4. of x and are floats.  J. F0 A  [& _/ [- F$ D9 r
  5. >>> math.modf(math.pi)' I2 w4 `9 K4 d  I0 I/ s
  6. (0.14159265358979312, 3.0)4 P+ Q# C9 ]2 M& C
  7. >>> math.modf(12.34)* x, U6 [1 H: e
  8. (0.33999999999999986, 12.0)
复制代码

, L5 c6 |' \+ p: h4 @math.sqrt(x)  求x的平方根9 J( f! M1 v5 T, [. q  O
  1. #求x的平方根( v: Q" E& @; Y
  2. sqrt(x)  w6 o3 L  k& D4 Y" T( W0 M5 a
  3. Return the square root of x.; J) r- t; X1 R* h7 k  k
  4. >>> math.sqrt(100)" {1 K& R0 ^4 f0 N2 [0 ^9 ?
  5. 10.0
    ( d- g" l6 Q+ P+ N/ X; i5 O
  6. >>> math.sqrt(16)* z7 W4 v4 z: L4 L* }
  7. 4.0$ ]8 _9 j- e9 e& |, I
  8. >>> math.sqrt(20)
    9 W  t" E) o9 i% q# |7 O* w
  9. 4.47213595499958
复制代码

6 M2 h% R. l2 Z& `, K/ d1 \math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    1 ?5 U3 [! v) X& E, ^& }& z
  2. trunc(x:Real) -> Integral8 f  ?+ U0 p, Q6 U6 J
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.  N/ V0 I6 T6 d$ \: k' }1 E
  4. >>> math.trunc(6.789)! S7 W* A; X! \: V, S3 u, K+ D
  5. 6& N3 k$ L4 I+ P- D5 M  X
  6. >>> math.trunc(math.pi)
    . G8 D; P% T% q, k/ f5 M0 h6 y
  7. 3. c0 D5 V9 O; |+ I- E/ Z
  8. >>> math.trunc(2.567)
    ( e+ i1 w% x/ a- [, B
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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