新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
: l" n% `+ Z! y# z
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。  t6 X  E' H' V% [) u; x$ b& r
6 }" @- t0 C# P) x
方法1
  C' |) ?' V8 |3 `5 I; H
  1. >>> import math
    8 e. P7 D5 X2 C; Z
  2. >>> math.sqrt(9)  A* S' g$ K4 K/ M( o* B& F$ f
  3. 3.0
复制代码
方法2" b2 m" _3 R# i0 ]: W
  1. >>> from math import sqrt1 V9 B0 N0 I: L2 c  C/ R, p( p& m" h
  2. >>> sqrt(9)0 ]7 K" f0 _9 x# z: n
  3. 3.0
复制代码
0 ~) R7 s' K% o4 a1 ~( T- b* A


1 u9 Z/ z" T* R0 o: P
math.e  表示一个常量
: ]3 L6 P1 c3 b+ r; o  ]- W
  1. #表示一个常量* s) p; v8 o0 U2 }  n- I3 l
  2. >>> math.e
    . A$ z) K7 w' b2 K2 v
  3. 2.718281828459045
复制代码

% ]+ t9 I. F# H2 W2 m& Z" l7 tmath.pi  
数字常量,圆周率

' B0 d7 t2 V; V% p  i8 t
  1. #数字常量,圆周率
    ( j- K; {. H7 l8 D4 @1 Q7 F/ T
  2. >>> print(math.pi)' D( h# R* ?9 O; }- M" w) U
  3. 3.141592653589793
复制代码

; Q! ]# _9 i6 r  V' a# g# }5 @math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
' ~0 J- r! ^2 Q% S. {# o1 q  M
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    / b* g& Q/ S' J8 K6 I
  2. ceil(x)1 g3 x# Z+ f; c+ @. M& u/ J
  3. Return the ceiling of x as an int.0 Z, m2 n. o# d- q- p( I5 C& A
  4. This is the smallest integral value >= x.
    + R6 F# m' _7 i
  5. ' @- ]8 \+ I" h- }% v$ Y
  6. >>> math.ceil(4.01)2 ?/ l5 j+ M3 `" `7 p
  7. 5
    1 N" C6 F" G& E" j: k" u
  8. >>> math.ceil(4.99)
    ( ]4 A% V' y7 q1 q$ G! {
  9. 5
    ! @" d5 O$ w  Q5 M: K% R
  10. >>> math.ceil(-3.99)7 v7 l$ U6 n3 L- x  t7 u6 V
  11. -3: V. T) w4 K8 s" e
  12. >>> math.ceil(-3.01)
    7 m' c$ r0 `8 m8 S
  13. -3
复制代码

! n1 Z, @3 |- s& f8 hmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
7 O' O0 U/ H) f9 u* I. g( z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    % i+ M* M0 p# x; s% N+ @/ l3 E
  2. floor(x)
    $ o# X  f% ]  e+ c
  3. Return the floor of x as an int.) k$ A$ e; w1 H- h) \) ^% n
  4. This is the largest integral value <= x.
    * |' T4 ]8 M, \* \
  5. >>> math.floor(4.1)
    % E" c7 a1 {) N" p
  6. 4
    5 f# ?) v  v/ k2 E
  7. >>> math.floor(4.999), m% ]: l; q3 T8 A5 m. ~
  8. 4
    . M( h' M. v2 T9 {3 M  W! C
  9. >>> math.floor(-4.999)
    + h/ I4 y2 K, P: i3 y5 ]/ j
  10. -59 I6 S1 P$ F! \: z- D4 _  {+ h
  11. >>> math.floor(-4.01)
    $ ~+ O/ H* [9 d( [9 f
  12. -5
复制代码

8 m) H3 o& d2 q, G& h6 E) hmath.pow(x,y)  返回x的y次方,即x**y. }! M" H0 E) j. _
  1. #返回x的y次方,即x**y
    - k0 \/ ~& n' Q8 ?) |
  2. pow(x, y)1 i* w% Y7 R$ O0 C
  3. Return x**y (x to the power of y).. U- ]) P+ m6 y; u' K
  4. >>> math.pow(3,4); Z! f8 m8 J8 w( L* P7 w0 Y, v
  5. 81.04 S; s8 }6 |* q2 M4 {% K3 [
  6. >>>
    7 C" z. `4 ~4 S. K0 \
  7. >>> math.pow(2,7)
    5 e; [- Q8 S5 t' B8 a$ y# c
  8. 128.0
复制代码
$ l. s7 W+ @7 f2 k; t3 M; D. ]! X
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" A' x$ g6 z" J
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    , M& X& a9 H- m) p2 z
  2. log(x[, base])
    3 Q: Z; o, v, w$ F3 {
  3. Return the logarithm of x to the given base.* B3 V+ Y  c/ ^$ Q
  4. If the base not specified, returns the natural logarithm (base e) of x.. A6 k& t* F0 F. o8 t9 b% U
  5. >>> math.log(10)9 J) `. @! {# f! D0 L7 v
  6. 2.302585092994046" q. x0 ^$ h  L% p
  7. >>> math.log(11)5 P7 u+ T! ]5 N* s: c7 }0 Q" t
  8. 2.3978952727983707% G& n% D" t3 b, R, P# s
  9. >>> math.log(20)! T! A! ]2 Q7 P5 t$ D* o
  10. 2.995732273553991
复制代码

" K0 S0 M) E$ i0 W# ~7 I4 wmath.sin(x)  求x(x为弧度)的正弦值8 i7 v2 v4 M3 ^* y
  1. #求x(x为弧度)的正弦值7 r% G, S  L) j0 B  e/ e, ~
  2. sin(x)" d; a+ X! J. x) n1 A: D* t% @8 k
  3. Return the sine of x (measured in radians).
    - M* v0 y+ f, k$ E$ H% v6 P0 n
  4. >>> math.sin(math.pi/4)
    ' V4 ?9 H8 F1 ^3 ~( {
  5. 0.70710678118654755 G% {% r4 r6 l0 I/ R  L* Q2 l' o0 s
  6. >>> math.sin(math.pi/2)
    ! h: u2 i( P& I3 \( Q! h% C5 i
  7. 1.0, c' p+ `$ |2 P; M" p, m" d# ?
  8. >>> math.sin(math.pi/3). @0 Y, a) @3 N7 Z5 @3 N; p4 L  B
  9. 0.8660254037844386
复制代码

2 G: M) E- _0 ~7 V& Y+ nmath.cos(x)  求x的余弦,x必须是弧度& }/ ^4 y( `& `
  1. #求x的余弦,x必须是弧度* ?5 n7 ^- s' _# h! u! L# Z- q
  2. cos(x)% X3 Z. H! g$ p: g. Z- e! T
  3. Return the cosine of x (measured in radians).6 @1 q8 b; g& R8 }4 a+ r- ~
  4. #math.pi/4表示弧度,转换成角度为45度
      G" J+ @% H: O% q- C! l0 }- d( R
  5. >>> math.cos(math.pi/4)
    # G, A9 z5 |+ B' y  G- u
  6. 0.7071067811865476
    ; B3 ?1 E- @1 F
  7. math.pi/3表示弧度,转换成角度为60度
    * x( _/ M  |. K, t
  8. >>> math.cos(math.pi/3)
    ! L' m. [( P- K7 [$ I3 P
  9. 0.5000000000000001% ?# T! y7 w( b, V+ V
  10. math.pi/6表示弧度,转换成角度为30度7 ^$ X7 ]5 H" l( z4 x7 W' z0 D# z# y
  11. >>> math.cos(math.pi/6)
      f8 C6 h3 p- v4 f* j) L$ g
  12. 0.8660254037844387
复制代码

9 Z. B. n5 b" C0 h6 Wmath.tan(x)  返回x(x为弧度)的正切值
$ `. B) B# W0 Z# G
  1. #返回x(x为弧度)的正切值
    ( _- d% f6 R  }' V4 }3 e' Q' F1 [
  2. tan(x)  `- m; H# D. e6 q3 ^4 v; T+ k
  3. Return the tangent of x (measured in radians)./ S  Q1 |% q1 d2 B, t( K9 `
  4. >>> math.tan(math.pi/4)
    & v( Y$ U3 C; g6 S7 ~
  5. 0.99999999999999998 R7 h% `5 i1 ]2 A( f* K4 I
  6. >>> math.tan(math.pi/6)
    9 A0 D. d+ e5 g- m3 g
  7. 0.5773502691896257" Z  N  F* d, N; w1 D
  8. >>> math.tan(math.pi/3)
    * C3 V0 t2 \3 B
  9. 1.7320508075688767
复制代码
& o% U3 R& o+ i' y" B* e9 y% o
math.degrees(x)  把x从弧度转换成角度
6 X" O2 m4 |6 A9 t
  1. #把x从弧度转换成角度# g' P% H+ l% r; m# J' Z
  2. degrees(x)
    $ g; k& G% _( h9 w3 @. e2 {& G
  3. Convert angle x from radians to degrees." m; r' J3 D9 C/ u

  4. : W, D6 Y5 a. P+ Q3 B
  5. >>> math.degrees(math.pi/4)
    - L) a5 z. U* V/ G% n, A! {$ X
  6. 45.0
    , m* C+ A8 x, Y
  7. >>> math.degrees(math.pi)  W+ Z3 a0 s' n5 r- Q: |
  8. 180.0( O" A. `- ]: h" O8 S# k2 ]2 V
  9. >>> math.degrees(math.pi/6)1 ?+ {% D2 z& k0 S! @% r  r# c) G
  10. 29.999999999999996
    ; x$ N$ Z4 l, {. f- @0 `1 o
  11. >>> math.degrees(math.pi/3)' H: s3 N& P& L; U1 ]
  12. 59.99999999999999
复制代码
* T1 g8 v6 r' e* I9 F
math.radians(x)  把角度x转换成弧度
' f8 Z) E. Z1 t: v& k4 n
  1. #把角度x转换成弧度
    5 ?( O2 ^# z1 X" W/ y
  2. radians(x)& _+ ~& }0 g: h1 t, V/ z
  3. Convert angle x from degrees to radians.
    % w# }1 U: e- p) R
  4. >>> math.radians(45)
    % s( Q* i" S; ^
  5. 0.78539816339744831 @* K. b) X7 o# a/ P6 @. _: S  j
  6. >>> math.radians(60)
    # [8 @' `5 L! u
  7. 1.0471975511965976
复制代码

( m. d1 @/ n# Xmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
3 }( ^* V7 J9 P- _$ p
  1. #把y的正负号加到x前面,可以使用0& A' F' b. ~0 g0 H8 u
  2. copysign(x, y)3 \, f, P. W$ J0 r, D8 G
  3. Return a float with the magnitude (absolute value) of x but the sign
    ! C9 z% t$ J! a. U0 Q' z
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 t! `; v1 C. C  I4 J$ s
  5. returns -1.0.+ ^% A8 e) y( n7 T! J5 ^2 b+ q
  6. 2 i& M* ^  d% ~) _+ D# |& T; d$ P/ ~
  7. >>> math.copysign(2,3)8 T* L% q. y  t9 c% `$ h# h
  8. 2.0% V  s* k& z% Z" I# V/ m, U
  9. >>> math.copysign(2,-3)
    - y: q% o9 h7 @3 w2 w) m
  10. -2.0
    : @1 M) b( r- Y8 u) W5 t. J5 s: m4 e
  11. >>> math.copysign(3,8)
    ' b0 _: Q! w0 n$ l" _# K/ T9 H
  12. 3.0/ k) s; a- P; w! }1 |6 a* R
  13. >>> math.copysign(3,-8)& o/ I  C3 A- A& W. }+ m. J# g, Q) D
  14. -3.0
复制代码

5 j+ v* N5 f4 y6 l$ W3 Rmath.exp(x)  返回math.e,也就是2.71828的x次方
+ A0 B$ o  N2 P' ~4 T  h, p
  1. #返回math.e,也就是2.71828的x次方' S, U. V% C" w. Q  ~8 d% V2 ]
  2. exp(x)
    ( f9 n! t+ \0 a1 \0 a
  3. Return e raised to the power of x.
    * \8 P+ I% K1 l/ {3 L3 [3 @$ K

  4.   u/ b$ V" R4 \) E" v1 E
  5. >>> math.exp(1)
    7 ^) @6 x- D/ q) ~* g, o* d
  6. 2.718281828459045
    + u# Y. b' H: \& Q7 ?( {
  7. >>> math.exp(2)0 J, O& \* I6 A; o. }& I
  8. 7.389056098930650 i6 k7 B+ K  ?% ^& U+ d) q2 D
  9. >>> math.exp(3)1 X8 r# D" h  s8 B- }3 A
  10. 20.085536923187668
复制代码

9 {: e3 l: p0 n) R6 t5 J; ^8 Z/ cmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 b/ m: h8 B0 g! n! o( Z9 V( E; [
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ( x. k/ q' R' [  B0 T+ h% |
  2. expm1(x)
    ! A, h% d. S; E0 \/ K
  3. Return exp(x)-1.; U& q: n$ ~( l0 U( h: O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    6 u3 A& v4 C' N4 w5 U* L

  5.   T0 T2 Z6 ]* K0 Y
  6. >>> math.expm1(1)$ n, i' W/ Y  L
  7. 1.7182818284590450 U! }' _/ T( Q9 e1 k" y
  8. >>> math.expm1(2). j9 O3 U0 O% `1 [% {) g2 w( r( y+ S8 k
  9. 6.38905609893065; A  ]7 n1 |3 Q6 e
  10. >>> math.expm1(3)
    # l. b* p7 g# |/ w0 r, p+ `
  11. 19.085536923187668
复制代码

, |, d1 M' y. D: g2 a% dmath.fabs(x)  返回x的绝对值
3 K0 A7 P1 w. g- v$ A% w
  1. #返回x的绝对值3 n4 j1 C. d4 |5 ~4 N0 r2 ?* A
  2. fabs(x)$ N! ]( g( \3 l+ U; X8 e: ]
  3. Return the absolute value of the float x.) c( G$ z1 K( o+ p5 n6 {) t
  4.   N% q0 `+ m8 ^- ?8 _& m  U# m& }
  5. >>> math.fabs(-0.003)
    " a0 ^. G3 X# a' |3 |6 O0 M
  6. 0.003
    : n, }, L, n) P" ?" n
  7. >>> math.fabs(-110)
    7 E3 }1 J4 d  u% [+ {( n
  8. 110.0! R8 b8 Y) o. t8 h8 d1 c
  9. >>> math.fabs(100)
    7 \: Q( }9 p4 l
  10. 100.0
复制代码
4 [! t/ S5 p0 W! j; p1 D
math.factorial(x)  取x的阶乘的值( t& S  `7 D* K1 @& K
  1. #取x的阶乘的值
    4 N& ^$ M. @) N: ~8 @
  2. factorial(x) -> Integral7 r3 S# |' j9 _+ z1 t
  3. Find x!. Raise a ValueError if x is negative or non-integral.* C, U$ ^7 \' f) q0 t
  4. >>> math.factorial(1)7 C5 Y; ?# v5 @* F9 `& X
  5. 11 M7 n' I( Z7 W- {2 t* B
  6. >>> math.factorial(2)8 a. k# W/ K: W9 v
  7. 2: A& R. @' O4 u) z4 H( Z' n, b1 M
  8. >>> math.factorial(3)
    - {9 R" E6 S3 x6 Q/ T, M
  9. 6) x. C# q8 O/ ~) G+ ?+ }
  10. >>> math.factorial(5)! z1 Z* s2 I8 K6 O" Q6 f
  11. 120
    6 e0 e& L+ E# ^
  12. >>> math.factorial(10)6 }# \9 z. Y- J, h4 @6 w
  13. 3628800
复制代码
  m4 J% ?7 L; e. Z* J
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数7 B; U% X# P7 J0 F* e$ b% Y
  1. #得到x/y的余数,其值是一个浮点数6 C7 {0 o, @: A
  2. fmod(x, y)
    . H, n4 C3 x; L9 s
  3. Return fmod(x, y), according to platform C.  x % y may differ.1 R9 H, C$ j  ]; U$ ~
  4. >>> math.fmod(20,3)
    $ e; j  ^% V" w6 S1 q
  5. 2.0
    0 L% H% ~  Q4 w2 `9 w/ p: f
  6. >>> math.fmod(20,7)$ e  |, D, F" E/ ?+ e# C
  7. 6.0
复制代码

+ f6 h  {3 E- p. d6 V1 gmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围+ s: }* p5 l) m! X+ @6 P
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    * O4 e: D2 Y5 ?7 d
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    - k2 v8 D& Y( I4 A+ v' P
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    6 ?- R% h2 J2 q8 q: C1 z1 _
  4. frexp(x)) ?6 ~- T# w0 D- ~. [$ R
  5. Return the mantissa and exponent of x, as pair (m, e)., u/ ]0 C. k. Y8 S8 ]
  6. m is a float and e is an int, such that x = m * 2.**e.$ x6 y7 |# S7 i; v7 J# P
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
      s, H) V# M6 @1 H5 N0 [6 x8 U+ n+ P
  8. >>> math.frexp(10)
    ( S+ O7 u: U/ G4 A( A
  9. (0.625, 4)
    - b3 I* I' ~+ e  f
  10. >>> math.frexp(75)
    : r! B+ Y8 n% T4 [3 t* M
  11. (0.5859375, 7)7 j  C& T; }4 v+ s
  12. >>> math.frexp(-40)
    $ G4 M3 ^3 }7 d  F" T
  13. (-0.625, 6); A" Q& z+ X+ j/ b$ h
  14. >>> math.frexp(-100)7 S0 H* o0 B; W9 P4 U' g
  15. (-0.78125, 7)
    2 H$ l& z; E1 g$ }
  16. >>> math.frexp(100)
    ' \1 y: w4 Q/ o
  17. (0.78125, 7)
复制代码
' B7 ~* ]+ W$ j* b0 q
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
( \$ R$ a8 H! J) L) m; b
  1. #对迭代器里的每个元素进行求和操作) K; s" q( Q+ g/ d* j
  2. fsum(iterable)3 x- g& R' J) F# c* ^  i0 X; P/ b
  3. Return an accurate floating point sum of values in the iterable.5 p+ V" y  m) }: [6 |* c; E' O
  4. Assumes IEEE-754 floating point arithmetic.( U! _& Z  w) L
  5. >>> math.fsum([1,2,3,4])
    8 y* R: g" @5 G4 p' o- c9 G; ?
  6. 10.0
    ( i, B5 _5 j9 I9 S
  7. >>> math.fsum((1,2,3,4))
    6 \5 \) F% o0 r  v
  8. 10.04 V! X! K- G; v8 D3 [* j
  9. >>> math.fsum((-1,-2,-3,-4))- H. D+ s6 X; }4 P! d7 z
  10. -10.0
    4 c9 A9 M7 [  R4 R% G2 l
  11. >>> math.fsum([-1,-2,-3,-4])3 a" j: V+ x* g7 u2 v) W0 N
  12. -10.0
复制代码

8 o2 R- ]+ n4 V/ \" ?' @( T8 Smath.gcd(x,y)  返回x和y的最大公约数! E2 w% f7 C4 q& T& r( G" `
  1. #返回x和y的最大公约数
      Q- o9 A& J# c
  2. gcd(x, y) -> int2 |3 j2 c3 D5 w+ ~4 M
  3. greatest common divisor of x and y
    3 K4 l% e3 b" z2 c- @
  4. >>> math.gcd(8,6)
    5 a+ y; m8 x# \/ b8 T5 U5 d
  5. 2- s/ D" E5 [5 ?  z. ^( p
  6. >>> math.gcd(40,20): ]( ]6 c$ }% D" n: }; d$ l
  7. 20* y7 J; u5 @) m# \( }
  8. >>> math.gcd(8,12)4 \( d. ]; H  Q7 V0 R- H
  9. 4
复制代码

) s; k# W# b8 ]math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False" c3 F: S9 {  E7 ~5 |
  1. #得到(x**2+y**2),平方的值2 P! |5 A- {: \1 x: {9 }
  2. hypot(x, y)/ O$ I% @7 C5 U3 m! Z- T
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 h5 o+ P- Q' E0 G/ {7 @( S
  4. >>> math.hypot(3,4)" d8 Q- Z; I$ R1 _
  5. 5.0
    & t, Q$ B9 }9 d5 B
  6. >>> math.hypot(6,8)$ _0 O' j" ]  S+ d; R6 f5 \5 g
  7. 10.0
复制代码
8 S1 _2 n/ E8 h. r* K3 G# _
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False; ]+ f6 K& ]+ T' }. e7 W
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 Q+ i) E9 Q4 g' L7 `
  2. isfinite(x) -> bool6 D. l" D3 l" M: |
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! I2 D7 e- r1 k( i
  4. >>> math.isfinite(100)
    2 p; z% z) E  W6 r* ^" @4 z; B2 ^
  5. True
    ' u/ [3 V4 ^) b0 |3 d- }
  6. >>> math.isfinite(0)$ U( f8 Z- H7 W) ]9 f
  7. True
    2 g5 F6 U# N  c; v  U
  8. >>> math.isfinite(0.1)
    ; I& b: L# x/ F/ G
  9. True
      J3 V) B5 c: ?& a
  10. >>> math.isfinite("a")6 `! I4 \# |" |6 F
  11. >>> math.isfinite(0.0001)
    ' B: u& l  c; z/ f# T" o
  12. True
复制代码

+ }& {8 U! Y) ]: a2 ~5 A- m% [+ omath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False/ a! Q3 S) T- F4 F# T! y9 `7 O. w: M
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    + R/ s; t/ O8 C- H0 L* R
  2. isinf(x) -> bool
    ; D# ~: J2 V. w  ^  S3 {$ e. r; G
  3. Return True if x is a positive or negative infinity, and False otherwise.4 t# I8 n% M6 U- J+ B$ k
  4. >>> math.isinf(234)
    " q+ t8 ]( t- p9 q$ X
  5. False! a: d+ H$ n8 t% y
  6. >>> math.isinf(0.1)
    6 \+ U, a# G0 J  Z/ U- B
  7. False
复制代码

" k" G+ e" e# J, l8 Tmath.isnan(x)  如果x不是数字True,否则返回False
' Z6 R5 F) L& I$ K$ _; `4 F
  1. #如果x不是数字True,否则返回False
      k0 W% @) B) E- i
  2. isnan(x) -> bool/ e' W! c4 F" p# Y, j
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 I% E7 w$ o) @. J, F
  4. >>> math.isnan(23)( A/ _1 z$ T9 \5 `- h
  5. False$ l+ H6 k0 q! I5 _
  6. >>> math.isnan(0.01)0 g: ]6 `9 I7 C! a% K& L& M3 Q
  7. False
复制代码
" ^+ w) A6 N9 X5 I
math.ldexp(x,i)  返回x*(2**i)的值
( d6 I8 Z6 h. @' h& o  I- x
  1. #返回x*(2**i)的值
    & m/ d  |5 H0 f& c. `1 ]4 p
  2. ldexp(x, i). u/ F* `" U- }0 K" Z
  3. Return x * (2**i).
    / v" T  p# \) q' X% W1 j
  4. >>> math.ldexp(5,5)- b0 U# q  F& U+ k; q
  5. 160.0
    3 H3 U, c* Q/ |4 V- I# `
  6. >>> math.ldexp(3,5)
    / T- `% n- [6 q/ v* u
  7. 96.0
复制代码
3 ^/ u8 W) X: i' J: j
math.log10(x)  返回x的以10为底的对数
4 j' i  C2 d9 y- j4 A* N
  1. #返回x的以10为底的对数: _: g  F/ v9 D, {- ?+ X
  2. log10(x)
    2 D4 l9 S. p6 i8 d
  3. Return the base 10 logarithm of x.
    ! \, Y: I& \! {& Z' R+ E5 h1 J: S
  4. >>> math.log10(10)# M* R! K: y( b' F# R
  5. 1.04 {  q. ?+ Y0 y$ |
  6. >>> math.log10(100)/ P! D* n7 a' x# }% e
  7. 2.0/ {5 M. ?0 j0 s
  8. #即10的1.3次方的结果为20
    2 `% }, ^7 O0 ~
  9. >>> math.log10(20)
    ' t$ Z* |1 G) |- i9 Z5 T6 o' X
  10. 1.3010299956639813
复制代码

1 J( Z8 S1 c, W0 S# T% A; fmath.log1p(x)  返回x+1的自然对数(基数为e)的值. H. K1 D7 T& O  z- R
  1. #返回x+1的自然对数(基数为e)的值
    6 x5 D% s: K8 f, ]1 {$ `
  2. log1p(x)
    ' r, r/ S1 m6 f2 I8 m; v
  3. Return the natural logarithm of 1+x (base e).
    % j6 |2 _1 `$ X/ q* C6 ?. g) L: V7 b' N
  4. The result is computed in a way which is accurate for x near zero.
    ! T  l2 `0 G; ^- R5 f
  5. >>> math.log(10)7 s- U8 W! D6 a5 v9 K6 d
  6. 2.302585092994046
    & D% x2 c) Y+ G1 R* H
  7. >>> math.log1p(10)9 Z- U2 t. g! K" s$ o5 g5 `+ C2 J
  8. 2.39789527279837074 l* E$ @5 d4 Q; B1 O! n) [
  9. >>> math.log(11)% s3 a, v/ U1 g% @  {
  10. 2.3978952727983707
复制代码

: i$ C0 x! b7 \+ v& Q, R' Qmath.log2(x)  返回x的基2对数
+ \' N: w1 i* `/ H" J3 v  h5 J5 c; T/ c
  1. #返回x的基2对数0 W' |) E4 c/ t
  2. log2(x)  y* ~$ Z& @) t( o8 Z# H; x
  3. Return the base 2 logarithm of x.
    # N5 U9 j3 I6 Z$ L: h3 T
  4. >>> math.log2(32): X7 |+ X: N1 D
  5. 5.0
    " C/ o5 A( k* b  ^% x( v
  6. >>> math.log2(20)
    , ]! G  |7 ?7 A
  7. 4.321928094887363
    1 G! r% v, s1 Y# M1 G- _
  8. >>> math.log2(16)7 F& I- k3 t- p
  9. 4.0
复制代码
; n' i+ _7 m% G' s
math.modf(x)  返回由x的小数部分和整数部分组成的元组. g+ C3 s2 V1 s8 J
  1. #返回由x的小数部分和整数部分组成的元组8 \3 T7 s" S! p  e8 c) J
  2. modf(x)  G# l4 T% |+ h; u% p% y$ l
  3. Return the fractional and integer parts of x.  Both results carry the sign& |, w5 E0 Z- D# h$ A0 ]2 }) x
  4. of x and are floats.
    # d; r' |2 n% L
  5. >>> math.modf(math.pi). ]! a) k9 e! u4 W5 @" R9 Z
  6. (0.14159265358979312, 3.0)8 V0 o4 ^) y! x# k9 |( e4 V- c
  7. >>> math.modf(12.34)
    ; x# f- I- w1 c6 ]3 y
  8. (0.33999999999999986, 12.0)
复制代码
2 u7 ]) T" P$ n* ^% ?
math.sqrt(x)  求x的平方根6 z; z9 a# a1 J
  1. #求x的平方根* _; ^! ]6 ]' Y, `
  2. sqrt(x)3 ^1 p4 O6 H' x: T0 z
  3. Return the square root of x.( F. k' b* y2 [5 {/ G. [( b( a+ I
  4. >>> math.sqrt(100)
    $ Z" L$ U0 k8 F- g) C) b
  5. 10.01 }9 [0 ^4 e: @3 m8 ^
  6. >>> math.sqrt(16)/ p. Q% S: e' Z  q9 h
  7. 4.01 r9 j, H/ }( ?/ N9 ~- K( `, c
  8. >>> math.sqrt(20)
    9 c' W- @# `- B. ^' }: I- Q; Y! ~
  9. 4.47213595499958
复制代码

. p  H# x  M3 a& Jmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分( G- B" w0 k0 D$ Y* w0 P: W
  2. trunc(x:Real) -> Integral0 ^. S, v1 J" g0 c
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: o4 e- _: U4 V# b5 [9 O
  4. >>> math.trunc(6.789)
    9 _8 f% t% t% O% b6 z+ R
  5. 6
    + r& v$ @! E- T8 Q. d; N# z
  6. >>> math.trunc(math.pi)
    3 i6 q! _. N9 A2 u  i  U& s
  7. 3) Q4 E# Q0 Q' X8 @
  8. >>> math.trunc(2.567)2 H+ H, U- N/ J; r1 V
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-23 10:31 , Processed in 0.077077 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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