新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

& S! c7 o2 Y$ `- v0 X2 [【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
6 t! \8 k1 \0 \' F, A; ~

* a& J4 x6 F4 b* D/ V方法13 H. @& v. O. O4 p
  1. >>> import math
    ; G, E% D- r) L5 P9 B
  2. >>> math.sqrt(9)% O( T3 g. X7 R! B- ~( R0 y
  3. 3.0
复制代码
方法2
8 \$ |  h7 v/ N6 w4 ~  Q2 q
  1. >>> from math import sqrt; I) c1 q# r6 d: a( W7 e
  2. >>> sqrt(9)1 k; q. r' s& D3 [6 S* J
  3. 3.0
复制代码
3 e; k" I  M3 y, ^( |& O

% c6 o7 {. R- [, O+ o
math.e  表示一个常量
# o  K! K& c! y2 g, q6 \4 X  l" a# h
  1. #表示一个常量
    - o* {& S# i2 {; n7 ?
  2. >>> math.e' a* [) T: H6 F+ c
  3. 2.718281828459045
复制代码

7 w1 v- O; M% ?. m/ rmath.pi  
数字常量,圆周率

3 Y1 [! w, v9 M& _" z% D
  1. #数字常量,圆周率
    # e! J" Y2 Z" C6 g/ x+ h* `
  2. >>> print(math.pi)9 q( C, ?: ]) F$ l
  3. 3.141592653589793
复制代码

" o" S1 m: d3 }0 K; }# Bmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 O7 @0 L5 h- D* j& T4 |
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    2 J) H) J; b: C3 ~  ^
  2. ceil(x)- w0 F( k; p9 d6 ?  T8 d
  3. Return the ceiling of x as an int.& N  {/ k6 k* Y& i1 q
  4. This is the smallest integral value >= x.
    8 Q5 h" q  j+ F* r* _# B
  5. 2 ]) ]* S; _5 o, N' X
  6. >>> math.ceil(4.01); k$ D6 k% g# g" |9 c: J7 e
  7. 5( J% U' b+ k  U, c5 V: Q2 r
  8. >>> math.ceil(4.99)- j( a" w% P" N/ B3 f: A" p
  9. 5
      A* `) u: J; u! @% B4 {( a- }
  10. >>> math.ceil(-3.99)  E2 i7 ~5 A  K6 Q+ `
  11. -3
    . ]* p! Z$ _: p1 g: E
  12. >>> math.ceil(-3.01)
    ) _1 |2 `6 [+ c9 ]4 L( @8 s/ m
  13. -3
复制代码

% ?1 M0 g2 N$ L3 Omath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 Y1 f- q: s3 z9 L5 Y) ?; P+ m
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    2 m) X3 X  g0 J
  2. floor(x)
    " I' R$ p9 t  m! E( P
  3. Return the floor of x as an int.
    ' E( J: L2 c- u/ C' G( ~# L5 ~) ^! V0 B
  4. This is the largest integral value <= x.
    & v  q2 r, R* z0 k8 {2 q
  5. >>> math.floor(4.1)
    - R9 C, D6 H, b
  6. 4+ n/ C8 O5 ?; O: N* j& U
  7. >>> math.floor(4.999)
    % z4 y1 G* |$ D4 {
  8. 4
    ; }+ s% u0 z4 r( d2 U
  9. >>> math.floor(-4.999)
    7 ^% n; D8 u% t1 F- r
  10. -5
    ! {* Q* d4 d0 u' u$ t  i! y! ]
  11. >>> math.floor(-4.01)8 P) X3 L8 M& M2 u5 ]
  12. -5
复制代码
. v1 m5 A4 F+ M
math.pow(x,y)  返回x的y次方,即x**y
. M' B4 G9 g0 C7 I5 F
  1. #返回x的y次方,即x**y
    ! J, I+ S" `, E# t- A. b* Q
  2. pow(x, y). H/ U% p  F+ r! l
  3. Return x**y (x to the power of y).. r3 J  k' j/ W
  4. >>> math.pow(3,4)
    8 s/ U9 z( x( {9 ~; ?8 C
  5. 81.0
    - d  R! S, T3 H! ]! i6 M& n* ?
  6. >>> " i5 k& u' L7 S8 i; a( S- t' |
  7. >>> math.pow(2,7)9 ^% t' H* ^; D" Y
  8. 128.0
复制代码

! U8 Q7 J0 B* C5 m1 p" ^4 ~math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: f8 q+ F6 r9 y# A) C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 h; E" j$ P1 b4 S7 U8 K. S9 j
  2. log(x[, base])
    - u; y/ L) M  ~4 B5 R; }8 E  V7 B( ^
  3. Return the logarithm of x to the given base.
    . b0 ]9 x" O; v: i( J
  4. If the base not specified, returns the natural logarithm (base e) of x.
    4 p' Y( \, u7 P4 }4 v9 _
  5. >>> math.log(10)
    9 E7 H; T+ B/ {5 v* w& }  a
  6. 2.302585092994046
    + a7 T/ r: X; n9 f( e" i- u* z
  7. >>> math.log(11)
    # E; Q6 I( P7 r8 c9 p6 q+ q
  8. 2.3978952727983707, l6 U) p) W$ p* n3 x# E6 V4 b
  9. >>> math.log(20)9 P( x4 s) J% P( R
  10. 2.995732273553991
复制代码

/ U" i+ j# g( `: J+ L, }: S1 U2 mmath.sin(x)  求x(x为弧度)的正弦值
* [4 Z5 @, O' g# R
  1. #求x(x为弧度)的正弦值
    * Z/ l( E1 `0 p% p# S* k
  2. sin(x)
    / m4 O; j. j4 c; b9 G
  3. Return the sine of x (measured in radians).+ k9 D; d1 ~  o: c1 M2 H
  4. >>> math.sin(math.pi/4)
    # {1 w# X" Z$ r
  5. 0.7071067811865475' ~9 P; P9 h4 j3 J7 L: |$ M) A) B
  6. >>> math.sin(math.pi/2)+ d% d  j6 N+ w1 A# u- J3 S
  7. 1.0
    9 }9 U: E9 y( P8 a( ^- s# }/ t
  8. >>> math.sin(math.pi/3)( D4 H9 ]3 N8 f6 I+ ~$ D  p
  9. 0.8660254037844386
复制代码

$ y6 |  V9 N: R  W3 Nmath.cos(x)  求x的余弦,x必须是弧度) }3 |$ w# `. {' X, e
  1. #求x的余弦,x必须是弧度
    ) U8 O4 ^6 [# i* }: b7 l# I  T! L
  2. cos(x)* W7 O9 z! n* ^: [: G
  3. Return the cosine of x (measured in radians).' i9 K. Q: M4 {( j  \8 N1 D' B8 z
  4. #math.pi/4表示弧度,转换成角度为45度
    * p( m9 \- X0 _& ^
  5. >>> math.cos(math.pi/4)
    ; i3 n  ]/ `  S; O8 B0 `
  6. 0.70710678118654761 X4 c( m% b6 I2 c+ i! |
  7. math.pi/3表示弧度,转换成角度为60度6 q6 `5 \  @, e- q" }
  8. >>> math.cos(math.pi/3)6 m3 m( v9 E6 Q3 q/ U4 @, P) c
  9. 0.5000000000000001  [( o  M! J8 e* w1 O
  10. math.pi/6表示弧度,转换成角度为30度
    9 o" [! H5 K4 x9 D+ K/ I
  11. >>> math.cos(math.pi/6)* F$ t( O0 D: t6 J$ E
  12. 0.8660254037844387
复制代码
! r1 k" |4 Y# W# S! E2 f
math.tan(x)  返回x(x为弧度)的正切值
2 L( x1 j! [, `1 M. {
  1. #返回x(x为弧度)的正切值$ b6 @: }( _. e0 }. W! l
  2. tan(x)
    7 g! c- ]! w- w+ D" ?1 ?
  3. Return the tangent of x (measured in radians).4 `- v) s' A& ]2 x8 B
  4. >>> math.tan(math.pi/4)
    ) D- L* U$ B4 [
  5. 0.9999999999999999
    ; _+ Y  o8 i/ b, o
  6. >>> math.tan(math.pi/6)( q# D( x! E* p2 T. K
  7. 0.5773502691896257
    6 X7 u+ w8 y/ s/ T
  8. >>> math.tan(math.pi/3)2 n" ?7 k" ^0 O2 `  ~/ f' h6 ]  `. a+ ?. L
  9. 1.7320508075688767
复制代码
# c3 d  ~5 w" b$ o6 b
math.degrees(x)  把x从弧度转换成角度
& u. R; L$ j; U7 ?2 L* e% q
  1. #把x从弧度转换成角度
    * B- ~1 O( L. C& p6 f3 f6 E1 h; A$ h
  2. degrees(x)
    ( b3 E9 O& j: v4 w7 K
  3. Convert angle x from radians to degrees.
    - @( v7 W, I2 G4 n. A9 q

  4.   {5 T1 f$ d# N6 a* G! ~
  5. >>> math.degrees(math.pi/4)0 [. T+ v3 _0 N9 w9 Q' i& r
  6. 45.0
    $ L6 F* w( a2 q% ]* M
  7. >>> math.degrees(math.pi)4 n$ c$ f$ ~& I5 F7 o3 j4 }
  8. 180.0! m6 z/ u! _  j3 H1 S6 j. Y" e
  9. >>> math.degrees(math.pi/6)6 W& t$ c8 ]$ m; ^3 T) l- X1 Z
  10. 29.999999999999996
    / f; ]4 Z5 m: F. g6 z" G, `# e0 x
  11. >>> math.degrees(math.pi/3)' P' Q, L0 P* Q& D- W* ~+ y9 E
  12. 59.99999999999999
复制代码

: z( s; x" ~# y7 N, g$ n+ [9 O( Q3 z- zmath.radians(x)  把角度x转换成弧度
# C" O& N/ Z/ ?0 X1 V
  1. #把角度x转换成弧度
    * y  q7 z) B. w  e4 Q
  2. radians(x)0 A+ a$ d9 k. j, \( F& [; }; ]
  3. Convert angle x from degrees to radians.
    5 z3 y# j5 @4 z% ^
  4. >>> math.radians(45)
    , I2 L% p1 w- |- v
  5. 0.78539816339744832 F2 d2 a: V% E7 m% k1 b( S9 j
  6. >>> math.radians(60)
    * o7 w: s# L$ X3 x2 K
  7. 1.0471975511965976
复制代码

$ M; u- B* P9 k7 O* Rmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
/ n% g( k) ^* m8 E, w8 @  J) A' L0 ]$ n
  1. #把y的正负号加到x前面,可以使用06 a7 a3 L. O  p$ L9 ?. Y# q' p
  2. copysign(x, y)  n0 }" ~/ k- P/ B1 X: w
  3. Return a float with the magnitude (absolute value) of x but the sign
    $ y+ t: T( r: e9 W2 t( _# ~6 g% x
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    0 F3 w+ V) k8 }/ o; c/ C$ i
  5. returns -1.0.
    - U. M: q) a, o  t+ N4 v4 b# f
  6. 4 |5 h' t; W8 ~& [
  7. >>> math.copysign(2,3)7 v5 x5 x4 s0 m' v. N+ \5 r
  8. 2.05 O- X* O  s( v
  9. >>> math.copysign(2,-3): I0 `) o7 P' S6 {
  10. -2.0
    0 K; G; T8 G4 U6 ]
  11. >>> math.copysign(3,8)
    - ]# P2 L( d# \  t( f% C' y
  12. 3.0
    ) S# t5 c! i& a7 ~
  13. >>> math.copysign(3,-8)
    , n6 H5 I# K7 e& `3 Z
  14. -3.0
复制代码
4 h3 e# ?( L4 A0 ^0 n2 O
math.exp(x)  返回math.e,也就是2.71828的x次方
4 T0 h# V( o1 R" O4 y% a8 k3 D
  1. #返回math.e,也就是2.71828的x次方$ J1 r- U& \  v& H) @, b+ F" n4 e$ C
  2. exp(x); L- d6 ]7 j% [# r" m+ W
  3. Return e raised to the power of x./ \( |* T+ e3 {: f

  4. 3 I% Z2 |; s1 z5 Y) Z' H
  5. >>> math.exp(1)# m+ @. X5 t! t# ?8 Y3 ]! V$ e
  6. 2.7182818284590456 |. o$ r4 r3 L: Z/ G
  7. >>> math.exp(2); M4 f. W) L% C: x+ Q. ]
  8. 7.38905609893065) G* D! V3 }$ }
  9. >>> math.exp(3)
    * v8 R$ L5 M8 B8 j) a) D, K& q
  10. 20.085536923187668
复制代码
5 ?& \+ w7 D% ~* n
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
6 u" }" U, O+ n
  1. #返回math.e的x(其值为2.71828)次方的值减1
    , S/ J4 _+ N/ K7 c/ X( _7 o
  2. expm1(x)
    2 P$ v7 f* o- I* a* w( J
  3. Return exp(x)-1.
    # a& E2 [6 c$ J% o, t* v
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.' @4 d& T6 R7 V! @. z2 G
  5. " T0 N7 d( G! F/ @) @, F
  6. >>> math.expm1(1): T4 Q2 x) p# r) Q- x9 p, z
  7. 1.718281828459045
    5 @2 D8 c  Y" @. [# h
  8. >>> math.expm1(2)
    " T2 U# Q% v6 k( |6 x7 v
  9. 6.38905609893065
    4 e/ R: C/ r9 B( O
  10. >>> math.expm1(3)  g* [& k* w: ~/ A4 ^; W
  11. 19.085536923187668
复制代码
3 y( C( [% ?2 E" o7 J& A
math.fabs(x)  返回x的绝对值
( D! I: H) [1 A9 [& O
  1. #返回x的绝对值  S+ ^. D, n' K- \/ _1 B
  2. fabs(x)
    6 O& y# [+ H' V# ]5 d) d  q, w
  3. Return the absolute value of the float x.3 R& ]6 z3 t4 b& ?0 o* e
  4. 5 P3 p) D* X1 _* p1 q! \; Z( ^4 D
  5. >>> math.fabs(-0.003)
    % ~5 M% N0 s5 O7 L' h& W6 {9 N
  6. 0.003
    * T  h' W0 E, ]0 h
  7. >>> math.fabs(-110)
    ( g& i1 X+ r2 g9 g
  8. 110.06 Z, @5 m, b6 S& j) d
  9. >>> math.fabs(100)
    6 C5 _' g3 Y3 q; p
  10. 100.0
复制代码
6 v  x0 C8 K2 }
math.factorial(x)  取x的阶乘的值+ O6 P: o, v) v  G( O' b2 h
  1. #取x的阶乘的值* Z' Y+ E% v3 Q  {6 s) T2 d
  2. factorial(x) -> Integral
    9 _9 d- P. g" `- r) {
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    / B: w3 v) W$ h: Z/ `
  4. >>> math.factorial(1)
    ; Z* K8 Y9 }) n: h/ C+ j- ~
  5. 1  N1 Z6 B8 [; R, L. ]0 j, L
  6. >>> math.factorial(2)
    - g! i" B# t6 B! U
  7. 2
    $ y2 v0 }# e% w2 u
  8. >>> math.factorial(3)
    9 h) H: i5 i3 B
  9. 6/ g/ e6 `1 h+ K7 Y% X5 ^% \4 ~1 p( C& ?
  10. >>> math.factorial(5)) p) E' C1 h# p3 \
  11. 120: f) ?* ~/ I% l2 N0 n; M% d0 ?
  12. >>> math.factorial(10)
    2 _: i% J* `  T5 P2 G) T* P5 z  B
  13. 3628800
复制代码
# Y  ~7 g  d! q7 N
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数4 O. S; w( p9 r! ^% R# J7 ]/ g
  1. #得到x/y的余数,其值是一个浮点数1 F, y$ a+ H- m. S) b
  2. fmod(x, y)& q. M$ J; @5 }
  3. Return fmod(x, y), according to platform C.  x % y may differ.6 _! x* N! e& j
  4. >>> math.fmod(20,3)
      S$ [2 {$ h- E/ K
  5. 2.08 ^: r: j, y* x: T8 n
  6. >>> math.fmod(20,7); s- [/ b0 x  N2 f. M; j/ b
  7. 6.0
复制代码

4 @4 K- X3 F  ?math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
' \/ |2 L: a0 k& x. z2 p" n
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,6 U7 L. f% B) e; c$ o  H2 J
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 n$ N0 N) ?1 r2 s: _  P% U4 ]
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    5 G' H/ |! O2 ~  w% t, b2 u# r
  4. frexp(x), h  G& f3 K; P/ ^* G- t
  5. Return the mantissa and exponent of x, as pair (m, e).
      k' U1 W. d0 M* A8 R7 I" ?5 W  `
  6. m is a float and e is an int, such that x = m * 2.**e.
    ( Z3 [) Q- }; |  O  ^
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    * q! |1 ^/ o8 u) r/ b# B
  8. >>> math.frexp(10)
    6 d. ]+ z- H$ j2 k7 J
  9. (0.625, 4)
    : h  O3 H) b0 ?
  10. >>> math.frexp(75)
    3 {) L$ {$ o, ~, W- o- X$ [
  11. (0.5859375, 7)6 j/ c+ H( p: a
  12. >>> math.frexp(-40)
    ) h0 X: G$ w2 G
  13. (-0.625, 6)8 u, ^7 l2 b9 N5 d% b
  14. >>> math.frexp(-100)2 F' J7 [8 v- c2 }5 o
  15. (-0.78125, 7)
    $ |( M+ J+ X. M# `/ i$ Z& q( W0 {
  16. >>> math.frexp(100); }( q( E9 b! p# d$ e( K6 d
  17. (0.78125, 7)
复制代码
( C# K* C/ q* S" D# \  D# o
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 Y* P, q. Q1 `4 ]  n2 [; B
  1. #对迭代器里的每个元素进行求和操作
    & m0 L1 y8 `. p+ L0 w' q
  2. fsum(iterable)8 U* u7 U3 V1 t4 e, C4 Q) {' V
  3. Return an accurate floating point sum of values in the iterable.8 ?* R3 {# {, l& S" n* P
  4. Assumes IEEE-754 floating point arithmetic.
    # H% X; N, {5 _% k$ k; M8 B) Q
  5. >>> math.fsum([1,2,3,4])% z( R9 B$ V. K8 `- g3 z& U
  6. 10.0
    . p4 d9 f% D9 s. l) x
  7. >>> math.fsum((1,2,3,4))
    4 H: Y! e# Z- K5 Y0 B1 W
  8. 10.03 [0 x) E( H( i. {5 {
  9. >>> math.fsum((-1,-2,-3,-4)); i) {* D4 ]# [3 w1 `
  10. -10.0# b3 B1 \8 Z4 w4 h
  11. >>> math.fsum([-1,-2,-3,-4])% B3 x7 \  J1 k
  12. -10.0
复制代码

$ y4 l7 ?" w$ G! N+ o1 c( b0 C' Fmath.gcd(x,y)  返回x和y的最大公约数0 Y% C# t* n% ^8 n0 A  }- z
  1. #返回x和y的最大公约数8 V4 J2 G3 r9 x5 x! r7 ]. b; j
  2. gcd(x, y) -> int2 A* k, f; G+ [0 x2 K
  3. greatest common divisor of x and y5 @5 ]6 t5 L! ]/ o% Q3 ~
  4. >>> math.gcd(8,6)3 B  h4 B# w# Z& m6 c, g
  5. 2
    + {" f: v' E: B
  6. >>> math.gcd(40,20)( {- E, h' z/ M1 j1 N) u8 q
  7. 20( G# ?6 B" y4 e+ b0 a# b2 }
  8. >>> math.gcd(8,12)& v) ?2 I% x8 ~
  9. 4
复制代码
/ M! T, b8 }0 Y" C5 _, N7 p
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* P' h8 ?( z, W2 z, g1 g; i
  1. #得到(x**2+y**2),平方的值
    0 _8 E! ]) v- M: C: [1 E
  2. hypot(x, y)" M0 [. x9 W5 n
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    / D! h' P2 W. j
  4. >>> math.hypot(3,4)
    ( X8 {( q8 S, R( E! Y; q
  5. 5.0
    $ w3 f. B5 D( A) V# i
  6. >>> math.hypot(6,8): d! Z* w0 A+ W. W6 k% g9 e% O
  7. 10.0
复制代码

2 Q- M' m9 N9 b& Qmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False2 ~+ V  Q, |. d1 c( n5 s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False# _5 ]& P  Q2 U% B7 S
  2. isfinite(x) -> bool- L3 g4 T8 M& d1 x! p
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ) Z, f6 U6 }" [* Y; I; o
  4. >>> math.isfinite(100)
    2 j3 M# U3 ]5 e( L
  5. True) ^  m/ ?( l' u% j/ l% g% ]3 z
  6. >>> math.isfinite(0)- P+ v1 n/ S! ?, B0 M  K, t
  7. True' W* i* x, |5 O  K) [. T5 C
  8. >>> math.isfinite(0.1)6 T6 v( W+ g# m- {, ?4 m" f
  9. True, ?& A# g0 _+ {0 X( k
  10. >>> math.isfinite("a")
    " T4 }" H* l5 ]7 Q. |4 ]
  11. >>> math.isfinite(0.0001)
    ) j6 J) F2 L1 ~) t, c! S
  12. True
复制代码

6 H; w. F5 J% Pmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False+ y8 n9 t0 L; j  d
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    . _3 J- x7 q# J( x$ s/ \4 \9 w) F
  2. isinf(x) -> bool' n% _* y  o* Q* Q8 Q
  3. Return True if x is a positive or negative infinity, and False otherwise.
    " r2 l% }$ Q8 j: z) d
  4. >>> math.isinf(234)
    : n; q/ r/ N6 D9 Y
  5. False
    * t: I8 i% f0 \; G: R+ ]
  6. >>> math.isinf(0.1): V8 M9 c# B2 x! p; g9 q
  7. False
复制代码

3 [& Y* w) d. o- j2 ~5 m6 F1 \( Kmath.isnan(x)  如果x不是数字True,否则返回False
1 c2 |# b' E2 l( x2 D
  1. #如果x不是数字True,否则返回False
    / Z# i, w9 ?4 I0 R* t3 y; b! D2 R
  2. isnan(x) -> bool5 t% _7 W# q$ c3 ~
  3. Return True if x is a NaN (not a number), and False otherwise.
    " U; @% {3 d, `+ r6 |
  4. >>> math.isnan(23)4 U1 q$ J% W& h. R( j
  5. False
    / G# ~9 U; _  J7 d% q( U* A
  6. >>> math.isnan(0.01)
    9 c8 Q: I) y0 \
  7. False
复制代码

1 ]6 v. [7 _4 |& j% A+ Wmath.ldexp(x,i)  返回x*(2**i)的值8 t$ I7 [6 A" F8 y" U  E9 J
  1. #返回x*(2**i)的值7 E, D# a9 C2 T& ?4 g
  2. ldexp(x, i)9 I2 B2 {9 w4 ~" p+ V  y$ Q
  3. Return x * (2**i).! F! b( D% m. M1 r
  4. >>> math.ldexp(5,5)
    ! s( W% @# ~2 d- B- W: M/ ^* g
  5. 160.0  @$ g# W9 z  [) q& \2 I
  6. >>> math.ldexp(3,5)0 `) L' ~" W7 f7 D" ~: ~$ L5 R
  7. 96.0
复制代码

7 k& [: I8 p( y7 W  p; K$ \' Bmath.log10(x)  返回x的以10为底的对数
( w& D  V1 ?1 Y- v# @; r6 I6 j
  1. #返回x的以10为底的对数5 j8 m. `+ A- D& a$ \* ^
  2. log10(x)* p$ h1 h2 {1 L! P5 @- F, J
  3. Return the base 10 logarithm of x.) K1 W+ J, i8 f# `7 R, y
  4. >>> math.log10(10)" X, y9 q  j8 e  C9 }3 M! X! s
  5. 1.0* m- x& [$ |: J  @
  6. >>> math.log10(100)
    + d# @3 d# W3 v& ~& g* ]+ I
  7. 2.0
    " n- `+ q9 _4 ~- D# Z) J! S
  8. #即10的1.3次方的结果为20
    0 F9 ~8 \; K& E0 E" J- [+ c+ k1 Q. @
  9. >>> math.log10(20)
    ) T9 z5 N0 ]: F$ D' Y
  10. 1.3010299956639813
复制代码

: ]2 N( e4 i3 b7 Y. zmath.log1p(x)  返回x+1的自然对数(基数为e)的值6 ?% |- Q% P2 W4 |" u6 @2 k, E
  1. #返回x+1的自然对数(基数为e)的值
    , P8 b/ P. C: k$ G- c
  2. log1p(x)
    4 D: [/ ?  C- f4 b
  3. Return the natural logarithm of 1+x (base e).7 h' Q0 p: S4 ?: A% o! Y3 W
  4. The result is computed in a way which is accurate for x near zero.
    5 B2 }( w: N5 S  j; v3 C" p
  5. >>> math.log(10)
    # |4 f- a4 V* ?0 [6 W
  6. 2.3025850929940462 u6 k& V* X) k8 R
  7. >>> math.log1p(10)
    / G5 ^, K' A/ l) _/ R; c
  8. 2.39789527279837071 R2 l, k2 h+ _- M+ ~0 _6 C0 \
  9. >>> math.log(11)  q* [; U3 W4 D' d
  10. 2.3978952727983707
复制代码
; S/ q6 z. G% S* O3 z" Z0 V7 k: |
math.log2(x)  返回x的基2对数
  G! A7 n4 o6 ]6 ^1 Y
  1. #返回x的基2对数+ w  x: c6 B! A& X9 X/ I
  2. log2(x)
    , a% F- q/ Y3 w/ s; s, \/ }8 C1 x* Q
  3. Return the base 2 logarithm of x.
    4 a2 \$ F4 E8 M5 y8 }. b- Z
  4. >>> math.log2(32)
    9 \1 b* Y; j- y0 w- i! E: P, f
  5. 5.0
    : c. ^& J+ o2 X" v9 A7 ]
  6. >>> math.log2(20)% k) e2 H5 P. Y7 N% k* e
  7. 4.3219280948873637 X4 j! X$ g& M9 `8 L% F4 e
  8. >>> math.log2(16)
    . [: V# q' E' E, N
  9. 4.0
复制代码
, A: M1 ^4 z6 ?, B6 R( x( f
math.modf(x)  返回由x的小数部分和整数部分组成的元组, z  X, t$ I# f8 T3 ~
  1. #返回由x的小数部分和整数部分组成的元组
    & I1 H6 o# h0 _8 K; u9 h
  2. modf(x)
    9 \' |: G( [$ x* _7 ?. I
  3. Return the fractional and integer parts of x.  Both results carry the sign
    4 p# F% v+ M4 j* S& C  A5 i
  4. of x and are floats.
    6 F( A1 D$ o7 {* z% h3 }3 {: B% `$ |
  5. >>> math.modf(math.pi)
    ; Z8 z7 B6 G8 O0 u" I$ o1 P
  6. (0.14159265358979312, 3.0). _& q* W9 m, D/ b5 W! T
  7. >>> math.modf(12.34)6 D* F3 h- [. K% A1 Z
  8. (0.33999999999999986, 12.0)
复制代码
7 c* i" i2 d2 l9 B1 L- |. d
math.sqrt(x)  求x的平方根3 _3 v8 V. j. v. ^2 t0 K  j: Z) ^0 i
  1. #求x的平方根0 K/ f; o7 J' Q# ^$ E
  2. sqrt(x)
    7 f6 _. W3 _. e% s
  3. Return the square root of x.2 V$ Z% ~& F' Q$ R  B5 M* M8 o: f* `
  4. >>> math.sqrt(100)
      i' W( r7 a3 A0 K: Y# J
  5. 10.0
    : K0 F( j  ~. |  l/ T
  6. >>> math.sqrt(16)( ^! ^0 [1 P% C2 b+ `. e
  7. 4.0: P$ E3 M( D1 H1 M/ q) f
  8. >>> math.sqrt(20)0 s$ e. x) ?( i) p
  9. 4.47213595499958
复制代码
4 y( j* F$ P7 E/ [* B( H# S0 ]
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    : C3 l# x$ L, A, K' |
  2. trunc(x:Real) -> Integral. W) ~( ~! N" c2 a. |$ p2 Q: i
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.8 J& T' ?8 S) _' j$ B
  4. >>> math.trunc(6.789)
    * e: s: P; G" k. U" n) y2 |' n' X
  5. 63 m/ ]" Z( b- ~# L
  6. >>> math.trunc(math.pi)
    1 o5 p6 N  J5 s' s. P) \
  7. 3
    8 G& N) P3 _: [9 d
  8. >>> math.trunc(2.567)
    , U1 {( J- o" G6 b  }5 N5 u
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-21 17:25 , Processed in 0.094441 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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