新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

* W" n- T. S# b6 t5 R: V9 x【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。% A6 Q* T/ k: o) z0 d$ ~6 [7 z! y

* G6 @+ d! |/ m/ ?6 K方法1" ]; V1 v+ T3 L  O/ H* T! n
  1. >>> import math
    ) ?1 y, W  |8 }7 ^. A* q$ H
  2. >>> math.sqrt(9)6 T1 H+ b" y* C2 W; \" a0 w
  3. 3.0
复制代码
方法2
; X9 x# ^0 N9 N. J) l  G7 Q  U
  1. >>> from math import sqrt
    8 ~, ?- l: k% }9 V8 I& F
  2. >>> sqrt(9)! i! a% s8 {* \9 Q$ ?: Q9 W3 \$ J
  3. 3.0
复制代码

9 z. D" ^" g8 j' S- d: D0 U1 X# c# N
. f4 [2 Z  w* n
math.e  表示一个常量- v7 I) C' S0 S* n' a0 w; J" T
  1. #表示一个常量2 h4 _7 ^7 G/ \4 I" b& w* K# ?' z
  2. >>> math.e7 I  |2 P+ O% A9 X, \0 X
  3. 2.718281828459045
复制代码

. Z8 [0 \/ V" Y; \! k8 Gmath.pi  
数字常量,圆周率

# J" p, h, `1 }( x6 }; @$ a
  1. #数字常量,圆周率
    $ X1 D* y$ L  ^2 _. s
  2. >>> print(math.pi)
    * l3 }1 B: W' t8 {8 L0 G; q
  3. 3.141592653589793
复制代码
: P9 i& H8 P+ Y% S1 ]
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

$ ~3 F( o* c0 L2 Y2 l
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    : ^- m7 R) Q" \) H( [+ t
  2. ceil(x)
    5 f( p8 s) i6 A
  3. Return the ceiling of x as an int.  p! Z8 D* L/ m* E# k) [! R+ w* u
  4. This is the smallest integral value >= x.
    - M% ]% h! y/ [

  5. 6 _  P# @# N# O/ d
  6. >>> math.ceil(4.01)$ l& ]. G; Z" q- ^! ]
  7. 5
    9 ]# V  K0 |/ o' _- ]) ~
  8. >>> math.ceil(4.99)
    3 R$ Q) q  F2 p9 O
  9. 5/ D+ E7 b0 z) `7 x7 u' C+ Z
  10. >>> math.ceil(-3.99), i- C* [6 j& T
  11. -3
    , I% v7 |9 d% H7 S1 L9 W) Z
  12. >>> math.ceil(-3.01)
    4 V( @( r/ Q: \1 V6 Z  V
  13. -3
复制代码

3 G% U" \! f1 C) S  E+ C# a# Omath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身% l8 c# g8 D9 k4 g
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    & Z9 `; v' s% R) _8 h  g
  2. floor(x)2 G8 B. o8 M, b( x
  3. Return the floor of x as an int.# n" j( e+ m7 n3 M' B
  4. This is the largest integral value <= x.
    + S/ h  h1 l) |+ @! _
  5. >>> math.floor(4.1)7 \9 y! q9 l6 L1 s/ b6 G6 @6 b
  6. 4* d( u" u: p' h$ o
  7. >>> math.floor(4.999)
    . h( ~3 q5 E4 A0 P4 K1 X
  8. 4
    1 }$ w$ l7 r: e' p7 B
  9. >>> math.floor(-4.999)& V+ x! T) A/ b7 {
  10. -5/ a) M2 t3 A1 A5 K1 u: _, U
  11. >>> math.floor(-4.01)9 m( ~: `- i  |* |
  12. -5
复制代码

, C/ w2 O6 r- k" x: f# O4 _) wmath.pow(x,y)  返回x的y次方,即x**y
% Q& X! ^$ Q( R* U
  1. #返回x的y次方,即x**y
    6 ^1 Z! i! B+ m# V
  2. pow(x, y)$ K$ X3 f& l  t* n" n
  3. Return x**y (x to the power of y).
    / U3 g( L, d4 }) M
  4. >>> math.pow(3,4)6 P8 [1 o3 y& [! ^  N! N
  5. 81.0( G' S6 r1 W. o; z# F' M2 ?0 L
  6. >>> ; k; v& H  B: o: G- Z, ?
  7. >>> math.pow(2,7)! ^: [1 E7 b; A! R7 ~) D
  8. 128.0
复制代码
& Q; T, _0 I. T# u
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 E! I! r; f8 i0 v
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ d2 a7 r3 F* k' R, |
  2. log(x[, base])
    6 G7 g; a' n! u' K2 E1 q
  3. Return the logarithm of x to the given base.
    1 [. f  g7 W! t" E" P+ I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    , l1 t  E6 g* k; x
  5. >>> math.log(10)
    * h+ G- T4 ^; k% C* e- ~: M
  6. 2.302585092994046
      c% A9 \: I) k! G3 \
  7. >>> math.log(11)
    . g' [0 o& n+ [% A$ [& m
  8. 2.3978952727983707
    % E( q3 y1 j9 c" Q7 H9 m3 J
  9. >>> math.log(20)
    - Y$ S* }0 G( k& N% ]7 M$ W8 o
  10. 2.995732273553991
复制代码

3 R2 H$ p4 Q7 Nmath.sin(x)  求x(x为弧度)的正弦值, ~5 T# g3 G$ I; p
  1. #求x(x为弧度)的正弦值! P, k' {# _4 x2 |$ L
  2. sin(x)
    % [" t! k1 o; j2 n  B$ ~1 C2 z) N
  3. Return the sine of x (measured in radians).
      K9 b! A' Q$ p, ]. Q3 v
  4. >>> math.sin(math.pi/4)4 k# C# z0 M5 w! x  F
  5. 0.70710678118654754 \) `4 R% ]5 _+ F$ Y- M! g1 {, S
  6. >>> math.sin(math.pi/2), H- w1 ~- D  c6 D$ Z" g  R8 m" r
  7. 1.0$ \" Q+ h0 o: Z& ?9 N2 G2 n7 b
  8. >>> math.sin(math.pi/3)
    & v0 P% O4 Q. D' U- _* g9 |
  9. 0.8660254037844386
复制代码
3 W2 k/ {% |9 t6 G0 J. V
math.cos(x)  求x的余弦,x必须是弧度
4 V7 }  N2 I  u! i- f& w( D
  1. #求x的余弦,x必须是弧度
    # C& b# b7 q: n) y6 {% ?  V
  2. cos(x)
    5 R6 M2 _1 U6 ?
  3. Return the cosine of x (measured in radians).0 ^' R' Q% A0 U# |9 N
  4. #math.pi/4表示弧度,转换成角度为45度
    5 P5 Z" {' K* u) `: L
  5. >>> math.cos(math.pi/4)& }2 ^% V6 T. v5 g
  6. 0.7071067811865476
    ( r- C5 o& L; m( D
  7. math.pi/3表示弧度,转换成角度为60度2 Q( ^6 ?& B) E- s8 W
  8. >>> math.cos(math.pi/3)1 {9 {* e( C  r9 `7 c6 S* h. E
  9. 0.5000000000000001) v" v$ X; a( {2 a% Y
  10. math.pi/6表示弧度,转换成角度为30度3 _% R6 U5 C' X5 ^# Y
  11. >>> math.cos(math.pi/6)% ^0 d0 i! t6 A/ Y+ r% z# }. c
  12. 0.8660254037844387
复制代码
7 p; T, J1 V: g' n
math.tan(x)  返回x(x为弧度)的正切值
6 L$ U% O1 Z- x# c& R
  1. #返回x(x为弧度)的正切值
    , i6 B: a1 F% ?* e  H& Q2 s" q; @
  2. tan(x)
    6 W* i& D( f7 D* n' M
  3. Return the tangent of x (measured in radians).4 c7 f6 v3 c+ F% O. b, Y
  4. >>> math.tan(math.pi/4)% N/ q- D% H% ]- t
  5. 0.9999999999999999
    9 i# [2 l. w5 p3 T5 X& I
  6. >>> math.tan(math.pi/6)) ~* M! B5 S* V; X8 u; i
  7. 0.5773502691896257
    # R$ t3 _; \, k
  8. >>> math.tan(math.pi/3)+ ?6 J, o( G. G* e8 B1 c' D
  9. 1.7320508075688767
复制代码
/ }' a; V/ Q" P. N4 Q1 D
math.degrees(x)  把x从弧度转换成角度$ F7 E4 S4 C4 }+ Q7 a
  1. #把x从弧度转换成角度
    6 [+ t( K: h: i& a7 [. \
  2. degrees(x)
    * H) ~$ h2 g- }; r
  3. Convert angle x from radians to degrees.
    8 d% I/ E: s9 o* F3 `% U
  4. 7 k0 p0 ]9 [  i2 [
  5. >>> math.degrees(math.pi/4)$ T& W( ~# e& h; h3 v  }7 H: l
  6. 45.0" d1 j  a1 L& ~, q' T8 [
  7. >>> math.degrees(math.pi)7 L; M* r4 I4 e- D6 }- K
  8. 180.0
    2 q5 L+ A9 q5 |  c/ r: |" H( Y  U
  9. >>> math.degrees(math.pi/6)& G" f8 p7 s- d/ {: w5 v6 j- k6 ~# P
  10. 29.9999999999999961 T) X( L" e  T: q
  11. >>> math.degrees(math.pi/3)
    0 m' I! z: q$ [) k0 h# }" J9 H% f
  12. 59.99999999999999
复制代码
3 T& {3 s$ P* t/ M* e: z4 }. D
math.radians(x)  把角度x转换成弧度. e3 |! R  u1 W8 [  ]# u5 G7 n
  1. #把角度x转换成弧度& A' b" n+ W; {3 t6 E' U% K
  2. radians(x)/ r/ i5 ]. Y! n) ^6 ?% G  W  m6 }* T
  3. Convert angle x from degrees to radians.
    ) [+ y; F( n: D* T; Q1 O
  4. >>> math.radians(45)9 q& ^3 l/ O+ G0 @, A3 q0 }
  5. 0.7853981633974483
    ' h6 D! E/ P, E( Z# |
  6. >>> math.radians(60)
    8 Q8 w( T0 A5 @$ c, v* J. k
  7. 1.0471975511965976
复制代码
6 w- M4 x3 f, x* F1 G" F9 m6 U
math.copysign(x,y)  把y的正负号加到x前面,可以使用0/ N8 k  L6 r/ B* P# M2 F$ W: a/ t* E
  1. #把y的正负号加到x前面,可以使用0+ ~2 b2 {  A8 v% y: a  K& ]
  2. copysign(x, y)
    : m0 X; y/ _, ~# E- H
  3. Return a float with the magnitude (absolute value) of x but the sign 4 c7 J- _' A' E/ z
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & p+ n0 x$ Y2 R
  5. returns -1.0.- ]! e/ W( l% I- b
  6. 1 X9 S- s' L8 X9 X8 G0 w
  7. >>> math.copysign(2,3)
    ) ?$ l, w6 J- o! T" W7 e
  8. 2.02 @1 V7 W' u3 E: {1 Q! U0 a
  9. >>> math.copysign(2,-3), K) R* Z. E% u' s
  10. -2.0
    , P" v: G& y- ^' q
  11. >>> math.copysign(3,8)5 a9 \% x5 G' {# Z. ^, q7 B
  12. 3.02 }: Q: t; j2 D5 }# p! j' b0 N; I
  13. >>> math.copysign(3,-8)
    + ]& ]6 L5 y1 M; H# a! R& `" b& D
  14. -3.0
复制代码
4 T/ [  s* Q. d1 o: Z5 t9 l* o
math.exp(x)  返回math.e,也就是2.71828的x次方
* A6 o# d4 N1 i; F$ ^( S  N
  1. #返回math.e,也就是2.71828的x次方
    2 U; `2 ]9 f0 f, u$ |) p
  2. exp(x)' X6 G0 F! A' h. Q( ?9 g, R
  3. Return e raised to the power of x.; K) u0 k5 O5 R* x9 N0 o

  4. / M9 o8 d: T. t0 J, p
  5. >>> math.exp(1)
    + a( D; E" ]4 }1 u$ C$ \5 ?
  6. 2.718281828459045: F1 h+ J. C5 ]; y; ^3 N
  7. >>> math.exp(2)
    & E" V" z) f9 \7 N
  8. 7.389056098930657 b# X- o1 o) a$ G, g) l, m
  9. >>> math.exp(3)  H( ?- q. ]- G) w+ k
  10. 20.085536923187668
复制代码
. f% B. o' x8 Q
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 Q2 t7 \) N* V6 S. T
  1. #返回math.e的x(其值为2.71828)次方的值减1& p& V% W- Q& H1 K7 A" O
  2. expm1(x)
    3 e0 S4 V* E2 A" V1 Q" r
  3. Return exp(x)-1.
    ; \3 M' I3 E3 w, s4 U& J* K
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    6 [0 _# U% W4 E3 _5 d

  5.   P  g  z3 ^$ d- q6 P
  6. >>> math.expm1(1)5 E2 V! s- u7 ?8 Q/ Z2 o
  7. 1.718281828459045* u/ P/ Y( \5 V( L
  8. >>> math.expm1(2)
    * H' H- x* S1 p4 R7 I9 s# X( q! R
  9. 6.38905609893065
    0 }# `% z' K0 M6 L) v" l
  10. >>> math.expm1(3)
      y8 ]1 p9 f; Z1 l( T0 ]
  11. 19.085536923187668
复制代码
$ _: {6 t' ^0 H8 L6 l9 S0 s
math.fabs(x)  返回x的绝对值6 B. G; @! U5 d  y- C& P; b
  1. #返回x的绝对值& Q* y( _+ Y  M9 U7 ]; _, g) t' `  }
  2. fabs(x)
    * ]: E, A& _! G
  3. Return the absolute value of the float x.7 N- w' F+ O' B
  4. 4 e4 ~* G$ K' R' h" e! ~. I5 Q
  5. >>> math.fabs(-0.003)+ U; H6 |. l* I: f
  6. 0.003
    . m* _: r/ p9 b- b# }# _
  7. >>> math.fabs(-110)# A3 I& J+ Z8 S% a
  8. 110.0& }2 L5 r; {+ X, c1 \
  9. >>> math.fabs(100)
    + x# S* E1 K, h0 }0 ?
  10. 100.0
复制代码
: p! w* S3 N! J6 T( ?) _0 z
math.factorial(x)  取x的阶乘的值& |- S; M" e7 p  C
  1. #取x的阶乘的值
    7 n7 ~$ x. S" _# K7 L
  2. factorial(x) -> Integral" n4 O2 W( K4 v' R. _  F
  3. Find x!. Raise a ValueError if x is negative or non-integral.$ g: K/ m$ q' e# k3 y5 h
  4. >>> math.factorial(1)
    7 J' ?5 k2 E1 J5 ?$ |' f& f0 D
  5. 10 `7 `6 D. ?1 L
  6. >>> math.factorial(2)
    & V0 ]. `$ F. B& x
  7. 2
    ( Y$ O/ e( q8 @
  8. >>> math.factorial(3)+ Z3 S& l- J) }% t0 y% l0 O! |. F! f
  9. 6$ D1 e$ B  z3 r: p! C$ h8 w( |. m
  10. >>> math.factorial(5)4 H2 N& M+ V7 f* q" ]1 O" y: h- l
  11. 1203 |' j& |5 B6 F, w
  12. >>> math.factorial(10)
    / a+ `/ D- V) w: V# X
  13. 3628800
复制代码
2 B8 `' V" Y- }! g( E- U
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
! U: d& I6 y; Y- j! t! f
  1. #得到x/y的余数,其值是一个浮点数
    * h  f* o, }1 u2 x4 j
  2. fmod(x, y)+ U5 N. c( U; p7 I8 G
  3. Return fmod(x, y), according to platform C.  x % y may differ.& k4 J- W- o0 }
  4. >>> math.fmod(20,3)/ d( t7 b1 }  q" \- O: w# E9 U! o( t" u
  5. 2.0
    " g* B" o& e- e: n& }) I# c+ q9 k
  6. >>> math.fmod(20,7)3 s4 y( r/ ?$ E5 b* K* D& q
  7. 6.0
复制代码

! u. x' l( z4 u5 d( z6 gmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
& O- V! O' ^, Z
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    9 e7 f  x3 }+ ]8 u9 x6 M% w
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. H" L- ^4 T4 [
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    $ V" K2 \9 H! @: f5 g" s
  4. frexp(x)
    4 s1 F8 O1 h! n2 |* P9 |4 X
  5. Return the mantissa and exponent of x, as pair (m, e).- j# S3 F# P' T2 m
  6. m is a float and e is an int, such that x = m * 2.**e.
    ' U; V, e/ s+ }' R  G4 ^' _
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.; s2 u6 v$ G: ~9 X6 ?  u  M) A9 O
  8. >>> math.frexp(10)& g& p; Q* v7 E( R6 T; X2 G
  9. (0.625, 4)1 ~- K' k! e2 b" @8 e6 S
  10. >>> math.frexp(75)
    # \8 e: p' j  U# k" r: ~, r
  11. (0.5859375, 7)' l( U4 h4 J* c$ e7 X6 ]" k4 _, M  Y
  12. >>> math.frexp(-40)/ h: `3 U! L6 V
  13. (-0.625, 6)9 R" O. o: l  k; m" T2 W8 r
  14. >>> math.frexp(-100)4 f! T$ }  @0 A* @
  15. (-0.78125, 7)
    2 m* p6 V$ v& {4 B
  16. >>> math.frexp(100)$ ?# a$ ~4 U7 p1 N; W) a0 a1 M
  17. (0.78125, 7)
复制代码
& r  n, r9 D- F
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
) t: k! F2 J8 {6 C9 n
  1. #对迭代器里的每个元素进行求和操作/ c1 x& x0 l: G
  2. fsum(iterable)
      S' b* I0 W6 |4 k  h0 c
  3. Return an accurate floating point sum of values in the iterable.
    ( p! u2 y' Y0 o6 P! m. h: N9 Y4 y' F
  4. Assumes IEEE-754 floating point arithmetic.
    : j& e1 T0 e) G  G
  5. >>> math.fsum([1,2,3,4])
    ( ]& z* J- F3 B1 E3 `
  6. 10.0
    * T# b/ N" e: S8 r  J
  7. >>> math.fsum((1,2,3,4))/ A! y/ O6 `- }; Y
  8. 10.0# d- }3 u7 x5 v3 c7 m
  9. >>> math.fsum((-1,-2,-3,-4))
    6 b; V: ^3 b7 R; X% B
  10. -10.0
    ) d9 U: ]. Q, N# ?  e
  11. >>> math.fsum([-1,-2,-3,-4])
    $ W& i' J0 _' o# Y
  12. -10.0
复制代码
5 z7 y: s6 Z+ R, N( y* f
math.gcd(x,y)  返回x和y的最大公约数9 _% f8 B9 G2 b# |2 _, _
  1. #返回x和y的最大公约数' s+ B7 i) I2 w. {* N
  2. gcd(x, y) -> int* h" `- j* q$ R$ l6 ~4 i$ L3 B
  3. greatest common divisor of x and y
    " F- z4 _2 Q5 B1 g. |+ h  v; P8 i
  4. >>> math.gcd(8,6)
    ; t  }+ _, q7 q# _  U
  5. 2
    ( E3 v  d& X9 w( E
  6. >>> math.gcd(40,20)
    & ?6 k6 k0 O9 k4 T
  7. 20' m8 V6 ~0 g/ z$ c
  8. >>> math.gcd(8,12)1 }, _6 d- ^8 r% ?6 e  c7 k
  9. 4
复制代码
0 i/ V4 }: e  c" V' T7 e
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
) t& s- Z$ J" e, n9 r
  1. #得到(x**2+y**2),平方的值
    7 N4 h8 _: D8 P3 K2 O- Q
  2. hypot(x, y)6 N( P# U7 c- o
  3. Return the Euclidean distance, sqrt(x*x + y*y).
      k; [$ H, v7 u9 ^' M4 c* `
  4. >>> math.hypot(3,4)2 \0 a+ R9 }2 I& c. i3 l- p9 |
  5. 5.0% W) a* u. L* X# _  R- B
  6. >>> math.hypot(6,8)
    * w) \# ]0 j* Q5 K0 q% w1 R( h
  7. 10.0
复制代码

" P3 _5 N% U7 `7 G: q4 }6 Q$ smath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False1 J1 Q: Q5 o% c) X
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    5 U  k! j. m6 T- v1 B4 b/ l) B/ c
  2. isfinite(x) -> bool, s4 ^0 D) x7 I8 K
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.9 p$ P$ }: L! [3 O
  4. >>> math.isfinite(100)
    " u7 M- ~5 C# {
  5. True
    ! m+ S% S/ j" u* E5 q$ [2 g
  6. >>> math.isfinite(0); t8 ?( M9 C- {* i
  7. True6 y* ~- K$ \8 _8 ]6 t7 m# O* p( C6 ~
  8. >>> math.isfinite(0.1)
    8 s1 I3 Q8 K. |, o: f4 e. y
  9. True* O) m! D4 j# C" B* c2 L
  10. >>> math.isfinite("a")
    " P; O/ Y) K5 ~* C' ^. L
  11. >>> math.isfinite(0.0001)! l/ S0 R2 g  o$ e
  12. True
复制代码

$ I- ]" \' I! Y5 I" Umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False6 b' s( m8 f7 O3 q  f2 m
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    " @) }/ w. ?& g: P% ^9 z# J
  2. isinf(x) -> bool
    * m# \, M& I4 t# B- b3 i' G. R2 H
  3. Return True if x is a positive or negative infinity, and False otherwise.0 [+ U3 S5 E, E) z, V1 M7 j7 S
  4. >>> math.isinf(234)) @+ i; ^) E8 i  G5 S
  5. False- x0 g- L2 h6 q( X7 F) n0 Y- ^
  6. >>> math.isinf(0.1)
    / N" j; t8 @  G' w
  7. False
复制代码
- I) ~+ L+ V" u5 x. p- C+ I3 O
math.isnan(x)  如果x不是数字True,否则返回False- |# A1 v/ k4 M7 M9 F( a& b
  1. #如果x不是数字True,否则返回False
    * R! G% s' P/ _' l% w4 C9 l1 M& x% Y
  2. isnan(x) -> bool0 @* H4 Y- ?4 s& z
  3. Return True if x is a NaN (not a number), and False otherwise.* s& v3 i) ~1 v" A$ R  b  I4 |: {
  4. >>> math.isnan(23)2 S9 q$ j& x( ^, g& w0 Z/ d
  5. False
    $ P4 N+ M$ V% `# A# _# S( f- T
  6. >>> math.isnan(0.01)
    5 V. g5 v% [; @2 q) {- a
  7. False
复制代码

$ y  x  d, I3 u8 T6 rmath.ldexp(x,i)  返回x*(2**i)的值
0 z# e  b% O7 a' s& D6 C, S6 ^; u
  1. #返回x*(2**i)的值
    ) A- x7 z# u! ^2 O% o
  2. ldexp(x, i)
    * y/ U: I+ d4 |) Z
  3. Return x * (2**i).
    / m5 J! x1 _3 A6 V* ?9 S5 Z
  4. >>> math.ldexp(5,5)2 Z) k) ~" K  b8 L6 k$ P7 m, U
  5. 160.0
    ) R( @) `; q2 N! o/ F" `
  6. >>> math.ldexp(3,5)
    " t9 e, T* z6 [
  7. 96.0
复制代码

3 M( x' M7 C2 t$ `+ jmath.log10(x)  返回x的以10为底的对数5 h( p9 ^1 x$ `
  1. #返回x的以10为底的对数
    1 H: X. ?' n& c% `
  2. log10(x)8 z7 @9 z% u; c3 E# A
  3. Return the base 10 logarithm of x.
    3 a) }* ]6 [* {% |# C+ L8 H
  4. >>> math.log10(10)) {5 b, W3 [/ q8 O9 G
  5. 1.0
    8 ?* x+ z1 m; E( E4 c9 A$ f
  6. >>> math.log10(100)
    1 B2 i) A8 x) A& P2 k: _
  7. 2.0
    : B6 S* F  s. a3 S3 P" U( P
  8. #即10的1.3次方的结果为209 Z* m. U( p# `& t
  9. >>> math.log10(20)
    4 P3 u1 e; y* v) ~. B* @& q
  10. 1.3010299956639813
复制代码
) c+ O1 i- }$ P
math.log1p(x)  返回x+1的自然对数(基数为e)的值+ P! B+ h6 i. z1 j# u; A
  1. #返回x+1的自然对数(基数为e)的值
    ' U+ e$ q/ u1 s( \! l. [% m0 |" {$ a
  2. log1p(x)
    8 x" d  ^5 a+ R- s: Z
  3. Return the natural logarithm of 1+x (base e).
    8 B7 c7 Z' O2 }
  4. The result is computed in a way which is accurate for x near zero.
    ) P9 W+ v+ a, s( @( _& ~
  5. >>> math.log(10)  A+ G$ X8 N! F$ {( d) y& T/ k
  6. 2.302585092994046( a$ z) {, s8 i8 C- H0 D* I
  7. >>> math.log1p(10)
    - P5 \' J% n7 X) ]5 O0 c
  8. 2.3978952727983707
    . W: w$ R" C1 g$ l; Y* @" ]" w
  9. >>> math.log(11)
    ; W3 Y* u9 R% |/ M' M0 ?* O3 y- [# X
  10. 2.3978952727983707
复制代码

7 i8 x( ~. f2 `! \# Jmath.log2(x)  返回x的基2对数! W! x0 I# q: S2 ], w; X; G* y
  1. #返回x的基2对数
    ! g4 ?7 S1 K) i+ C$ O( n
  2. log2(x), {) z1 A8 z" M2 c/ k
  3. Return the base 2 logarithm of x.
    & K3 g' I; m# c% W+ x2 _
  4. >>> math.log2(32)
    0 K4 I4 Z% [2 u0 m' g
  5. 5.0
    # F% z9 h' o5 q
  6. >>> math.log2(20)
    & M0 H$ g/ A  q0 D6 L
  7. 4.321928094887363
    7 u7 |$ Q5 M+ N: r2 H' |
  8. >>> math.log2(16)
    8 t: `* Z( {8 k( ], w
  9. 4.0
复制代码

# x1 t6 t9 U3 V5 m. e# y( g6 z' Mmath.modf(x)  返回由x的小数部分和整数部分组成的元组+ J" J% q' A8 }& N: ]$ }
  1. #返回由x的小数部分和整数部分组成的元组; K. b" n0 @3 G' r  v  |
  2. modf(x)
    3 [1 `5 ?; Y# ~7 E
  3. Return the fractional and integer parts of x.  Both results carry the sign7 e* w. \* ~4 T8 Y: A
  4. of x and are floats." S9 ~* O& g! B2 i1 z" Y' \
  5. >>> math.modf(math.pi)
    # i/ o3 R: r6 X
  6. (0.14159265358979312, 3.0)
    7 n2 E& Q8 S& T1 X- e0 |8 k
  7. >>> math.modf(12.34)
    ' `' z+ \5 Y  e+ z3 W* C$ d6 ?* Y  x
  8. (0.33999999999999986, 12.0)
复制代码
4 u3 B7 l% c7 `$ b8 B; ?
math.sqrt(x)  求x的平方根
$ U* f. `& ~; u! s# m0 I
  1. #求x的平方根
    & I+ ^9 |/ u% X( Q5 r, o
  2. sqrt(x)
    1 Z, D+ ~7 z; O) p) m. ~  [. s5 u
  3. Return the square root of x.. i$ \/ c( U# u/ P) @
  4. >>> math.sqrt(100)  I% {1 k7 u5 ?' W! w
  5. 10.0
    1 I, ]) H6 p* C( D* F6 j* i, ]
  6. >>> math.sqrt(16)
    % S4 `1 ]5 [0 r& Y
  7. 4.0
    ! P" h  W0 o0 ^- a: `2 l
  8. >>> math.sqrt(20)
      d. e! ?" ?0 l9 `7 o$ {
  9. 4.47213595499958
复制代码

4 Q" Q" L' t7 C# T# m  ~math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分1 k8 x+ g$ F: M
  2. trunc(x:Real) -> Integral
    ; Q" o; m  H. P% n
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    9 Q; p8 j; b; b, h% I
  4. >>> math.trunc(6.789)
    $ l% o5 }# J# s
  5. 6; o# w( Q+ S% \' r
  6. >>> math.trunc(math.pi)3 B; H0 b7 r( E
  7. 3
    7 i5 W' w$ c0 q+ H% R, V- r
  8. >>> math.trunc(2.567)& Z4 C. z4 Q( Q% p+ J2 L2 X. I" Q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-15 06:24 , Processed in 0.088490 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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