新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
- s( B0 h/ X* J2 I2 w  {5 _0 X
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 c: m3 t- K6 s% x) {5 _0 o9 N

# b  U/ A( W, _# _2 e方法16 j5 ~# @2 i$ l, g! }/ U
  1. >>> import math
    . R' z2 x/ t0 ~7 w
  2. >>> math.sqrt(9)
    3 T- @% _! U' S1 Q, h) o
  3. 3.0
复制代码
方法2
, M9 }, a  H. Q% N" R
  1. >>> from math import sqrt  H- v; M' [9 e8 i$ O9 v
  2. >>> sqrt(9)* l: f0 H  ~0 E* ^3 C7 ~
  3. 3.0
复制代码

0 L5 x9 ?5 i' ~  j" B# K+ N& N: f

/ ^! g" ^6 S: N3 D) |# I& N* g% \
math.e  表示一个常量
" {* s* b) U- N3 ^
  1. #表示一个常量
    / o# [, e* Y9 O! ?- H$ Y4 A0 ?7 ?
  2. >>> math.e
    ; t7 _2 `$ |4 J3 `/ Q9 K# u) q) Y
  3. 2.718281828459045
复制代码
: X" B7 s& v( D) a9 q4 Z
math.pi  
数字常量,圆周率

  u2 C& ^- i% e
  1. #数字常量,圆周率" l; a+ ^6 I! Q
  2. >>> print(math.pi)
    ' V2 x0 v( f- r. o
  3. 3.141592653589793
复制代码

, V* c- ?8 w$ i, ^( emath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
* u/ _* p7 C4 s; R, [
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    * C& x  }9 V; {2 m! B
  2. ceil(x)$ E+ L+ z1 z# ^+ K, W
  3. Return the ceiling of x as an int.8 |7 V6 }2 K, Q
  4. This is the smallest integral value >= x.
      R4 u) T1 A8 b6 l1 r

  5. % f. I+ m- m' A+ a9 f0 u
  6. >>> math.ceil(4.01)
    5 [9 [$ F0 o- g8 J# ?7 W' P6 V
  7. 5" ?: I8 z% Y- R
  8. >>> math.ceil(4.99)
    ; J1 h3 @/ t" {+ ?
  9. 5" _$ a4 v5 m! Z4 J# e
  10. >>> math.ceil(-3.99)
    ; [# d4 \  m* X0 t) t
  11. -3
      H: N5 L- Y; b2 h. @
  12. >>> math.ceil(-3.01)0 t: S" B4 E7 a" r% U. B6 |
  13. -3
复制代码

5 o) t3 l" {1 N0 j# t# a; Mmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. a8 V; X' H1 {# s$ C1 @* l* w
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    7 K( a; O" e$ S3 Q+ A; ]
  2. floor(x)9 f3 N# h( P" }! q9 f1 s$ k9 ^
  3. Return the floor of x as an int.
    % Y( ~: v$ K! b% K5 r$ b6 `0 N
  4. This is the largest integral value <= x.7 G0 F' ?2 h; U. r. R7 ?6 m' T
  5. >>> math.floor(4.1)1 ~! o& U( V+ s4 k
  6. 4" ^' X" n* B5 L/ m5 g9 c4 v
  7. >>> math.floor(4.999)
    1 A' }' P0 a  M3 {) A
  8. 45 H" k' A/ ^8 y) |. i
  9. >>> math.floor(-4.999). E) o+ u7 v# G$ d) s& A/ R. O% A
  10. -55 i4 v) _' ^7 h$ L" U2 @$ m- X& _
  11. >>> math.floor(-4.01): R. ?. @" P) }; A! e; k& m
  12. -5
复制代码

- M6 ~# r- h& f0 h! @math.pow(x,y)  返回x的y次方,即x**y$ }8 j# o' a1 J) Z# B+ {
  1. #返回x的y次方,即x**y6 Z# V, H5 c9 d( [, f6 ]$ \: f
  2. pow(x, y)/ H; T* R! J+ C9 {0 k
  3. Return x**y (x to the power of y).% Z7 I( i9 A: d6 U, G7 S, ]
  4. >>> math.pow(3,4)
    + d& s9 A+ R! s1 C& Z- @8 d# ^4 }. n
  5. 81.0
    ; U' m) \3 a+ y; P5 I
  6. >>> * Z* h- D! |* k- c
  7. >>> math.pow(2,7)
    5 \5 i9 X3 X6 b7 ^0 W1 A+ `* y
  8. 128.0
复制代码

' R/ A- u& o5 F3 B0 Hmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
  c2 S4 k, c0 e2 H8 D; l; v  H: u! J7 k
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 P. m( y" O1 g+ R4 |+ l" I1 {3 o+ _
  2. log(x[, base])
    - ^8 R2 b' J  d1 O2 b6 I7 ?- k6 n
  3. Return the logarithm of x to the given base.
      s0 l% A' T4 Y+ q7 M5 w
  4. If the base not specified, returns the natural logarithm (base e) of x.
    6 r# x( N1 _8 J6 u
  5. >>> math.log(10)( x- a! N+ s$ c* m
  6. 2.3025850929940468 A+ X) g# l2 W1 z5 S
  7. >>> math.log(11). c" i" O& {8 q$ ^  l) \
  8. 2.3978952727983707( }; ~0 R% ]3 Q+ E  b: z
  9. >>> math.log(20)3 e/ r# h$ {' [0 w
  10. 2.995732273553991
复制代码

: n$ H% e# O9 j  D- Y/ w/ amath.sin(x)  求x(x为弧度)的正弦值2 V5 o% q) }* h* A. d* J8 @
  1. #求x(x为弧度)的正弦值
    ! M3 d9 _5 c9 r
  2. sin(x)) `+ Q" u% ^& D: Q8 |/ i
  3. Return the sine of x (measured in radians).
    9 Y4 y9 [, S! M" V- s" H, H
  4. >>> math.sin(math.pi/4)
    ; l( ?3 s; Q+ s2 N( p% U$ H% B, Q
  5. 0.7071067811865475+ N' S) Q3 K$ h* `. ?! L
  6. >>> math.sin(math.pi/2)
    # A4 ~+ X4 J: ~
  7. 1.03 d% T* C4 J% }& |
  8. >>> math.sin(math.pi/3)- q0 p' C/ X7 n/ ^# _
  9. 0.8660254037844386
复制代码
/ I# ?3 U3 |$ k, M" X
math.cos(x)  求x的余弦,x必须是弧度; B9 I7 }  K( v; e1 T
  1. #求x的余弦,x必须是弧度
    ) w2 b# Q- _* l" i+ R( K
  2. cos(x)% Y( w0 p/ n# V7 C8 z5 I
  3. Return the cosine of x (measured in radians)./ Y% z# ]1 C  I6 w, ?3 p8 Z) \" i
  4. #math.pi/4表示弧度,转换成角度为45度
    ' t( h/ M2 b4 n
  5. >>> math.cos(math.pi/4)
    % }( y1 F! z  E) U2 U
  6. 0.7071067811865476* h' ^: K3 l5 `; H
  7. math.pi/3表示弧度,转换成角度为60度
    . ^1 V, \  o/ \- x( Z  z/ {  X, u
  8. >>> math.cos(math.pi/3)
    ! g* b- M( o" D/ a: ~7 i# R& j7 ~
  9. 0.5000000000000001
    ! I3 h* b3 f/ U( D% L  l. \
  10. math.pi/6表示弧度,转换成角度为30度
    2 |, h9 ?; T, v* t$ s+ q
  11. >>> math.cos(math.pi/6)
    ) e+ Q9 Y7 c& K' C) _- b- j) c
  12. 0.8660254037844387
复制代码

; C4 K2 R# Q* Q4 cmath.tan(x)  返回x(x为弧度)的正切值
' y/ i2 m& a+ x" a& H
  1. #返回x(x为弧度)的正切值- x. q" s: Q0 y. j5 }, M
  2. tan(x)
    1 j' S' _4 S. Q
  3. Return the tangent of x (measured in radians).
    . Q1 H- `/ D% E4 c
  4. >>> math.tan(math.pi/4)
    & U, d$ i' y4 G
  5. 0.9999999999999999
    0 Z8 E5 w2 o0 U+ v. L8 o
  6. >>> math.tan(math.pi/6)
    # B# K, Z6 E% i& @
  7. 0.5773502691896257% I) v, t! Z* F  ]
  8. >>> math.tan(math.pi/3)4 t9 S7 x* V7 I: l  U1 U( f: ]
  9. 1.7320508075688767
复制代码
# j! @" U2 c. b4 \4 `, c
math.degrees(x)  把x从弧度转换成角度4 V, M: d& K7 B/ p- B2 j
  1. #把x从弧度转换成角度  u) E4 O8 p& V, E5 x2 l) x
  2. degrees(x)3 ^( T  k1 o# g7 J2 v; A3 c
  3. Convert angle x from radians to degrees.
    + p& U: [9 J0 ], _' t$ K9 a$ }6 s
  4. : Z2 U* H: m: e: u* _
  5. >>> math.degrees(math.pi/4)8 b( }/ o: R; K+ u$ Z2 h
  6. 45.0
    ; u, E8 O# e7 ?" J. [, t3 B
  7. >>> math.degrees(math.pi)
    # }7 t4 V1 \  R9 _! C7 q1 u
  8. 180.0
    % C4 q3 b0 R4 R: S
  9. >>> math.degrees(math.pi/6)
    * }3 D7 p; X9 _6 T3 T6 Q( h
  10. 29.999999999999996
    , Q. h( ]0 }$ a
  11. >>> math.degrees(math.pi/3)
    . i! z5 p; P( b% T% Q& X5 ]
  12. 59.99999999999999
复制代码

( j: T! [, H. O: K" S. emath.radians(x)  把角度x转换成弧度
" B9 n/ l2 ~6 E7 ]. i
  1. #把角度x转换成弧度
    & C2 Q# t9 ~2 e6 U( O( k+ n# P' p% m
  2. radians(x)
    % c  N. {* D; D2 A" B
  3. Convert angle x from degrees to radians.
    2 n. r* }. u; v& w! h2 w' f0 E; W
  4. >>> math.radians(45)  X# ~# q( ~7 |" H9 C
  5. 0.7853981633974483; ]$ [) v- }4 E) s2 z, ]
  6. >>> math.radians(60)2 a& T6 H1 A3 K  ]5 I; ~$ L7 B
  7. 1.0471975511965976
复制代码

. [+ W  u' @) p# r# Omath.copysign(x,y)  把y的正负号加到x前面,可以使用0" A9 R9 d- ^; v4 {6 {- n% `& ^
  1. #把y的正负号加到x前面,可以使用0
    " W& e' I" B1 h3 i2 [$ |: M3 K5 G
  2. copysign(x, y)
    4 H. k$ Q* M+ F$ Z! B
  3. Return a float with the magnitude (absolute value) of x but the sign
    & W! N7 i' B0 {! w- h
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 O# P: u7 X7 ]
  5. returns -1.0.' H0 k6 V* L# \# O: Q  |5 S

  6. % G$ m9 E- C0 Z6 o
  7. >>> math.copysign(2,3)) V4 g& L6 \, g; }  j& O5 f
  8. 2.0( D: J0 S, b, M8 m0 a. Z
  9. >>> math.copysign(2,-3)1 r& d9 V9 e$ U! _% Q" k
  10. -2.0
    2 g1 k. X- n/ w3 c/ b
  11. >>> math.copysign(3,8)/ x. p6 N0 F$ y- Q4 S- C
  12. 3.0
    6 W, q- a0 M  k
  13. >>> math.copysign(3,-8)
    : b& k- l; B# Y6 e% F+ h( `3 Q! i
  14. -3.0
复制代码
) d- _$ M0 ]# h$ z0 L
math.exp(x)  返回math.e,也就是2.71828的x次方7 b2 A; ^" J7 i9 u# {
  1. #返回math.e,也就是2.71828的x次方
    , N  s8 K3 U; ^) {( T/ e8 @$ i
  2. exp(x)
    # L- h: h: w% N
  3. Return e raised to the power of x.
    $ [( I1 k  q' Y6 F5 C
  4. / W2 B4 I6 c% @- k0 a
  5. >>> math.exp(1)
    $ a* v" Y5 K# q4 E+ z
  6. 2.718281828459045
    , M, Z$ w% k* `4 Y9 e. w
  7. >>> math.exp(2)* J7 m7 m, q& \7 z( }5 |
  8. 7.38905609893065
    , {1 |3 R) H5 D/ K) W
  9. >>> math.exp(3)7 i1 Y/ ]/ g* ^
  10. 20.085536923187668
复制代码

) d( `' ~$ B; \! q. _2 X4 b# N4 a; Umath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1$ b" k! s" Q6 W$ Q& J+ Z
  1. #返回math.e的x(其值为2.71828)次方的值减1
    . @( K3 C, q" i5 V8 f/ H" `, y
  2. expm1(x)* `& ]9 v) {$ c
  3. Return exp(x)-1.& h6 p& \0 X4 I7 E* S9 Q# ~9 n
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ' V, i  s- ?8 P! }. j

  5.   H# n; j* @! U+ O, h/ t/ ?+ L% e
  6. >>> math.expm1(1)# M5 ^3 ?8 i/ {+ L6 I1 G
  7. 1.718281828459045
    ( W3 _3 Z0 d. x# w  y6 E. `
  8. >>> math.expm1(2)  Q- b5 W( ~% A. \
  9. 6.38905609893065
    7 O1 l7 W: b! `6 A( z( V
  10. >>> math.expm1(3)1 ?; @4 e( H8 f' _- m+ M4 A
  11. 19.085536923187668
复制代码
; ^/ d# h( y7 W- ]" t- j/ X# e8 v
math.fabs(x)  返回x的绝对值
* k4 k: g  J. K2 ~+ l1 `
  1. #返回x的绝对值  f! z" X5 \5 {. l5 I' c
  2. fabs(x)2 m1 s8 P8 a! O- s; [0 u8 n
  3. Return the absolute value of the float x.7 F% s  h$ R+ L2 X

  4. . U, w3 Q9 U; c: `* e7 h
  5. >>> math.fabs(-0.003)
    4 v, K: p' _7 K; t% w) N0 I; D
  6. 0.003
    : f  |) M4 h" E! G
  7. >>> math.fabs(-110)
    " N4 e6 m% }0 n. V1 X( }4 D
  8. 110.0$ h# v( r$ M6 @
  9. >>> math.fabs(100)
    9 n9 ?4 V3 ]0 D2 @" {  l
  10. 100.0
复制代码
/ d2 k& ~% X! w
math.factorial(x)  取x的阶乘的值+ q* B' L) J% a
  1. #取x的阶乘的值0 Y8 _( k( |* @' ?# i; k  ~+ m
  2. factorial(x) -> Integral% ]" n% M$ f  y, Z; Z
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; F  T  y+ X6 _: m! V2 v
  4. >>> math.factorial(1)0 x, V) k9 K& T& a, L/ C
  5. 1# D, F. Z$ g# o8 S
  6. >>> math.factorial(2)) @6 m( w, ^4 d  _7 R' Z7 s: h
  7. 2
    / s! l% b4 f& l/ c* m
  8. >>> math.factorial(3)
    * D6 R) B; G! D
  9. 6
    ; J3 s: t) }* h6 I8 M1 h1 }
  10. >>> math.factorial(5)
    ) t8 s7 ~$ ]$ |3 s7 X. Q
  11. 120
    5 Z+ ?/ {: U  ?
  12. >>> math.factorial(10)
    & p- a5 g% Y  Y
  13. 3628800
复制代码
! F7 g+ k9 y) @3 Q5 d
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
: c, |8 v" z8 ]3 w) y2 _) |& z- w
  1. #得到x/y的余数,其值是一个浮点数
    $ J/ p0 }& R5 f+ n4 F
  2. fmod(x, y)
    - P, k8 ~* W# D! l: W
  3. Return fmod(x, y), according to platform C.  x % y may differ.4 x' T' Z! M* Z& R; E9 y& T" V
  4. >>> math.fmod(20,3)3 n4 ]+ U7 m* |) g
  5. 2.08 x1 J/ Y! }" P( D. J
  6. >>> math.fmod(20,7)9 M3 ]1 e8 G7 y. J- H) N
  7. 6.0
复制代码

, j+ ?/ k9 U0 K6 B( V$ Cmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! A" B, J& f/ I
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    0 b1 S" d- O5 _2 c5 N/ i! M
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" _8 k3 V. b! u) x) ?
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 I8 p9 M5 T6 E8 X
  4. frexp(x)
    ' X* ]! o4 \' R( |% D/ v) n+ _
  5. Return the mantissa and exponent of x, as pair (m, e)., t! [0 t2 |& M
  6. m is a float and e is an int, such that x = m * 2.**e.
    ( |1 J5 \5 B; V/ @4 i% j7 m! ^
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.* [4 c* y% z) B/ W% x
  8. >>> math.frexp(10)
    ! D9 h, A1 c3 y- l  T) w9 Z
  9. (0.625, 4)
    4 {# d- h  w3 x
  10. >>> math.frexp(75)
    6 E. U6 O! `* t4 J" R* p+ G, ^
  11. (0.5859375, 7)) O' |  z; ^/ E, q( e9 w1 _
  12. >>> math.frexp(-40)/ ^. z) r( S8 C1 Y" ^" i  d
  13. (-0.625, 6)+ {' {: F% H  }1 x1 @6 Y* R
  14. >>> math.frexp(-100)$ j, [% j! B$ y% T$ e
  15. (-0.78125, 7)4 p: \) |5 I$ P: ^
  16. >>> math.frexp(100)+ J5 \- `# x" u7 ^" \
  17. (0.78125, 7)
复制代码

+ _/ l) I* K) R8 c' a8 e' B( A$ J- @math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
2 W" H6 p8 T% ^; T
  1. #对迭代器里的每个元素进行求和操作
    ! P9 Y; G% Z+ h# N3 h* t+ a8 K
  2. fsum(iterable)
    $ O+ N: X; l6 y$ r! C
  3. Return an accurate floating point sum of values in the iterable.' T  K# d1 m# w' q8 d: j+ N
  4. Assumes IEEE-754 floating point arithmetic.3 M: k% S% Q1 S' T1 h) `. Q
  5. >>> math.fsum([1,2,3,4])  D+ b+ I9 _) `  g6 C
  6. 10.0
    : R, n/ N( a8 [, B! v* |2 D
  7. >>> math.fsum((1,2,3,4))
    + M( h! \9 e+ E9 D0 u" V
  8. 10.0# o0 U; t1 ?, G% E
  9. >>> math.fsum((-1,-2,-3,-4))# T7 t4 @2 `) m
  10. -10.0
    ; S5 G3 f0 k5 ?7 I" F$ e* w' y
  11. >>> math.fsum([-1,-2,-3,-4])
    % a6 j1 v) n; w( N
  12. -10.0
复制代码

/ h( [7 y6 f7 [$ }5 t3 `9 v& ~0 \9 v, Imath.gcd(x,y)  返回x和y的最大公约数
( Q' O) o; B. g% r% {3 {
  1. #返回x和y的最大公约数
    % r! S# P" L1 y
  2. gcd(x, y) -> int
    1 r% l# u4 O, _& K2 I, E) ?
  3. greatest common divisor of x and y
    ' s3 \; t9 U/ F7 ~' T! K" ^9 O* X
  4. >>> math.gcd(8,6)
    9 o* s% \+ ~" q& ~+ L8 t: X' w
  5. 2/ D8 h+ q" _8 ~$ k7 ~
  6. >>> math.gcd(40,20)
    + d4 H; N% I/ \3 Q0 r
  7. 200 n1 |% f% c( ]
  8. >>> math.gcd(8,12)* a. W) i, h( v4 e" t7 s4 i
  9. 4
复制代码
4 W! ?8 x8 l- C  W$ `
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
& Z3 P2 Q& H3 H2 e5 W
  1. #得到(x**2+y**2),平方的值
    7 }! s" ]! H% N9 M3 L
  2. hypot(x, y)6 r0 x$ c' L$ E0 o* }4 i9 `* r1 T4 Z
  3. Return the Euclidean distance, sqrt(x*x + y*y).. K9 L1 f3 e3 m* q0 m
  4. >>> math.hypot(3,4)4 I6 p. B7 l1 U
  5. 5.0' F. u6 y& b* L/ M2 o) y' Q* F
  6. >>> math.hypot(6,8)
    2 _+ |" y' e  J1 ?: k# M) U
  7. 10.0
复制代码
- j/ r8 j, o& N* s% b# a
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False  t5 g. S+ Q. e- q
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    + c9 E* s- K" L/ j- [
  2. isfinite(x) -> bool% e. h8 f% d1 B9 u9 b
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    - k9 p0 `* Y! I$ P3 f+ e
  4. >>> math.isfinite(100)
    , e1 W8 i# v: p
  5. True
    # P- r1 ]5 O5 j) y. `. R6 \
  6. >>> math.isfinite(0); ]- p$ l- A) p& _% H
  7. True& V( I: k; c8 D: A: m2 G6 T
  8. >>> math.isfinite(0.1)
    . s8 B9 L2 o' @& N. a4 ~
  9. True; }  F$ J. K; D6 ^
  10. >>> math.isfinite("a")
    ' z. F: V! C9 F6 G, D
  11. >>> math.isfinite(0.0001)
    , [; v# A, ~3 C! r3 m
  12. True
复制代码

4 S8 ^. ?6 A" K9 t" r$ @math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
) F1 E/ I0 \5 Y! ^( a7 a8 E
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False8 G$ n' H8 _, x) N9 W# O
  2. isinf(x) -> bool7 U" S4 N$ d" P
  3. Return True if x is a positive or negative infinity, and False otherwise.0 K+ ?2 E7 o7 m! s% s
  4. >>> math.isinf(234)6 v8 @) `; d% N) f; r3 q
  5. False
    , L) G4 h* {9 W4 E2 _0 q8 p
  6. >>> math.isinf(0.1)
    2 b0 K9 @3 O; n9 j. N2 ?
  7. False
复制代码

% I4 S- L$ P4 s: p* vmath.isnan(x)  如果x不是数字True,否则返回False7 d5 H, p4 G* E& k  q4 @9 \- |
  1. #如果x不是数字True,否则返回False
    $ w6 m7 Q* X) |* r
  2. isnan(x) -> bool/ n9 y) P# _/ `0 w$ u' {5 I& c
  3. Return True if x is a NaN (not a number), and False otherwise.
    ; ]+ l' K3 ~# W
  4. >>> math.isnan(23)/ y* t) ~( Y$ D2 f9 b* n' ^
  5. False
    4 c5 w. Y) K4 P# h3 l; \
  6. >>> math.isnan(0.01)* |3 F# e4 G- w8 s' u: R. K, b
  7. False
复制代码

/ A# ^' g$ o5 H; I6 V  ]math.ldexp(x,i)  返回x*(2**i)的值
9 y- ?6 O; V5 W7 J8 r5 e
  1. #返回x*(2**i)的值
    ' {3 w$ p# z; Z  f) Q) N, r( K, h; E) ]
  2. ldexp(x, i)# @0 t$ d( o, V# i0 O
  3. Return x * (2**i).8 L. N* i5 a' r, Y
  4. >>> math.ldexp(5,5)) a- j# r0 k! N) N6 B
  5. 160.0
    " O# u3 E8 X4 ?
  6. >>> math.ldexp(3,5)# Q+ @9 K0 @) U# b5 K
  7. 96.0
复制代码

" x& J7 o6 [- qmath.log10(x)  返回x的以10为底的对数
! I6 C2 B* v* G: |
  1. #返回x的以10为底的对数
    3 C6 p# Q: S8 W$ G
  2. log10(x)
    # _8 B. k% w3 n6 z; [0 Y3 z# e: P
  3. Return the base 10 logarithm of x.
    6 y8 {+ b* ?! I% Q6 [6 k& j0 M6 J
  4. >>> math.log10(10)( r8 H: {7 Y' [  P( x
  5. 1.07 W1 k: _. r( `$ l
  6. >>> math.log10(100)
    " i0 l: p4 n8 i" b& U3 Q" R/ i
  7. 2.0
    ! N  L. Q  w2 B1 k) \6 w
  8. #即10的1.3次方的结果为20
    " x) X2 X" B9 Y* |& o* \9 Q& i
  9. >>> math.log10(20)
    . o+ t. A5 }. k4 j0 {
  10. 1.3010299956639813
复制代码
$ o3 @' U3 r# N( u2 @
math.log1p(x)  返回x+1的自然对数(基数为e)的值
6 |" y7 k, y: c6 _! D5 p% k& ^7 w
  1. #返回x+1的自然对数(基数为e)的值6 j7 A* l! R" L
  2. log1p(x)$ D4 `: A0 w1 M. U
  3. Return the natural logarithm of 1+x (base e).
    / f0 M+ M* n! \: j8 M7 u7 O
  4. The result is computed in a way which is accurate for x near zero.
    4 I$ ^7 u  I0 n, {- _0 |4 M, M# F% f3 g
  5. >>> math.log(10): l. v, h+ e6 _. {7 c4 ~
  6. 2.302585092994046$ H' I, t9 m) a- _1 P' C5 {+ D0 I
  7. >>> math.log1p(10)
    % w) R% l/ C; d7 \$ `
  8. 2.3978952727983707& L' K  k* }5 C
  9. >>> math.log(11)
    # ]+ O3 y# P! S- z2 J; d2 p( T
  10. 2.3978952727983707
复制代码
7 S  x) {$ A6 R' {+ K& J9 v
math.log2(x)  返回x的基2对数5 x5 e  b( q2 B
  1. #返回x的基2对数
    9 y+ B6 m, L2 z
  2. log2(x)
    & C: V' C8 ~7 P2 n# I1 C9 Y2 Q5 E: y- f
  3. Return the base 2 logarithm of x.% d8 m. u1 l$ H2 M3 ]6 b
  4. >>> math.log2(32)
    ) d4 g# A+ i4 q9 `8 g
  5. 5.0
    $ u* ]$ X. `( i, @- l
  6. >>> math.log2(20)$ A0 k" D0 O7 X4 M) \+ ]
  7. 4.321928094887363
    2 F# g; }/ h' p. I+ s/ D! q
  8. >>> math.log2(16): a! E* T) L9 P3 e) v9 F2 t
  9. 4.0
复制代码

6 }1 M% W1 z+ U! Q2 l% Mmath.modf(x)  返回由x的小数部分和整数部分组成的元组
$ ?  r$ u' h- Q. Q0 S0 V
  1. #返回由x的小数部分和整数部分组成的元组5 i0 Q8 [1 a( `$ [6 k
  2. modf(x)
    ! Y5 e) z% c7 @# Z8 m, l4 z" u
  3. Return the fractional and integer parts of x.  Both results carry the sign% c/ r9 C/ L: X$ O
  4. of x and are floats.$ {# v) N9 P) J5 X# ^& G( h
  5. >>> math.modf(math.pi)
    " y. Y+ \6 N* i% q+ i/ {
  6. (0.14159265358979312, 3.0)
    3 ~; T, j; L% H- v- d. h; j3 `
  7. >>> math.modf(12.34)
    " J/ q: ?" G0 K% H* Y5 X
  8. (0.33999999999999986, 12.0)
复制代码

( W$ ^. x$ `. imath.sqrt(x)  求x的平方根
& E& U4 k4 l; s
  1. #求x的平方根9 b4 Y0 {, c, L5 h9 X
  2. sqrt(x)
    9 q! a4 T+ [' C7 _4 Q
  3. Return the square root of x.
    * P4 m' a- Z6 e' U
  4. >>> math.sqrt(100)
    * ~4 ^' w% ?6 Z$ Y
  5. 10.07 G& Q% z7 m& l+ j
  6. >>> math.sqrt(16)
    + n. x. N' s/ E: ?9 n( U
  7. 4.0
    0 d- s, j1 y" c9 p# h. p' v# P
  8. >>> math.sqrt(20)/ q. H/ R& c! C* m
  9. 4.47213595499958
复制代码
( Q1 l/ Q+ W1 `# C3 L9 q9 r
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分" F  u7 }) n1 m9 n& G5 d
  2. trunc(x:Real) -> Integral
    / d5 @. N9 o3 z' i8 U
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    / o' M' r* t6 U% K' M3 a  i
  4. >>> math.trunc(6.789)
      p9 \2 B. l+ C" }
  5. 6* `- K* L! x* F( ]0 c
  6. >>> math.trunc(math.pi)
      B! B8 V2 h0 N/ L& Z
  7. 3
    7 U5 y& G/ D$ {0 j2 m
  8. >>> math.trunc(2.567)
    - ?; G0 X! N5 F; U
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-13 11:44 , Processed in 0.087887 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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