新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

1 K+ M- t. n2 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
( j' G* ?) @/ @' s7 I! Z- B  |
+ f* c, ^! A6 M$ f
方法1
# e: k3 j1 m( b6 h- S8 b  M
  1. >>> import math
    " a0 n) z  V* E: ]. z
  2. >>> math.sqrt(9)0 L0 ~7 `" v6 h. {8 d
  3. 3.0
复制代码
方法2+ A; V8 p4 {% m7 v" O; b$ e; I2 I& u
  1. >>> from math import sqrt
    ! J9 L0 N7 Y9 Y  f
  2. >>> sqrt(9)
      ^6 i, o! n% @. L, M$ G
  3. 3.0
复制代码
: u! l- ^2 |0 O* x  p+ {9 V, M4 s

4 v9 h% G! o2 Y/ c( l) ^# ~/ m
math.e  表示一个常量& N( o# b% J4 w' b8 m9 W% C* R
  1. #表示一个常量/ i7 @1 g4 b$ t9 V9 k4 }# F
  2. >>> math.e
    / _5 c3 p8 N8 n5 \- y" f
  3. 2.718281828459045
复制代码

+ C$ p' ?* l  N7 f/ {math.pi  
数字常量,圆周率
+ O2 e; q9 x$ F" N6 N# f8 ~
  1. #数字常量,圆周率
    5 S* J. x& G: h& i
  2. >>> print(math.pi): R0 p3 J+ L3 E" ~( i  w, @! s
  3. 3.141592653589793
复制代码

2 z! m9 h9 O9 e; ^/ v; f# Imath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

6 Y; P. F) ^$ f$ i: ^+ Y6 _' d
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    4 O1 U, E7 z- X5 _; q/ Z2 X5 \* ~4 K
  2. ceil(x)
    - C3 ~' v2 U" u# H
  3. Return the ceiling of x as an int.. v3 }" z6 y" d9 j. d: s% r6 @
  4. This is the smallest integral value >= x.
    9 A: p+ O. ]4 C5 S

  5. / H4 |8 c" V) e  f2 Z
  6. >>> math.ceil(4.01)
    % N5 @" b* d1 p* O7 W4 L
  7. 5- n, p! V7 O/ S! @$ Z
  8. >>> math.ceil(4.99)4 n  G& M  j/ S& p+ ?
  9. 5
    6 M2 S& a* |# u  q3 s" h" K" O
  10. >>> math.ceil(-3.99)
    1 `/ k) w, W0 ^* u$ {' t  V( o! d( ]
  11. -30 i# u9 u0 ]2 r3 S9 N; ^7 K0 z4 Y) `7 O
  12. >>> math.ceil(-3.01)/ E+ r: G, J' A3 {* e  X/ [
  13. -3
复制代码
! h1 L6 Q" W3 ?6 h5 e! b
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ \' _! ]3 M4 p
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    : n8 v" `, p0 S& ]3 R' c4 ~
  2. floor(x)
    , D' ~% Y' o# [7 h1 n% a. _
  3. Return the floor of x as an int.+ Q  W0 d6 R  x! p! p
  4. This is the largest integral value <= x.( G: B6 N- B! R  e; l( ]; s. S
  5. >>> math.floor(4.1)" I; g4 ?# U/ `: N
  6. 46 [+ M2 J" ~( F: u6 r( p
  7. >>> math.floor(4.999)1 [4 ^; j/ ]8 K
  8. 42 @- @0 |& T, @! p- Z
  9. >>> math.floor(-4.999)
    ; h& {, ^6 h. p5 d8 E; I: y( w' X
  10. -5
    ' l3 U" |3 g+ \
  11. >>> math.floor(-4.01)
    # l4 P% h' o' I# v& j# v
  12. -5
复制代码
: X5 ?# y) E0 x0 u3 G% Q3 `3 Q
math.pow(x,y)  返回x的y次方,即x**y
; Z# g' F: b* f: y2 f
  1. #返回x的y次方,即x**y( r  }! k# ^  c
  2. pow(x, y)
    ) B$ n" O. x9 k: N
  3. Return x**y (x to the power of y).$ F/ u8 j9 |2 h! g/ E; O: ]# @
  4. >>> math.pow(3,4)
    5 u$ O, W' _. j; M- G! J& V5 F
  5. 81.0
    " m8 q8 Y! l, U5 |* k) X
  6. >>> % _# w! c) d# n0 k( t2 l
  7. >>> math.pow(2,7)
    % ]6 b& y2 ^+ _) g9 i
  8. 128.0
复制代码
5 N  j9 r4 \4 o! K2 u0 X6 i( W& n
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): s  |- P: [0 ~7 \2 g4 Q3 j; _
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), `2 J- M8 @$ x
  2. log(x[, base])
    1 \( o1 Q$ W, k7 e' p
  3. Return the logarithm of x to the given base.
    ' O1 q3 K3 C: [+ h, U: ~: t/ s& u
  4. If the base not specified, returns the natural logarithm (base e) of x.
    & Q7 q4 s: `  u* q6 X3 x/ B8 U# q
  5. >>> math.log(10)
    * b, _+ C  g' O& H2 V5 p
  6. 2.302585092994046
    ! f+ ]9 ]( u% P% v( C$ L
  7. >>> math.log(11)
    / m# T) T& p1 X" e2 w2 k; k
  8. 2.3978952727983707
    ! J% n! O: i0 H9 S
  9. >>> math.log(20)
    ( v1 H0 q' _7 t! ]2 E
  10. 2.995732273553991
复制代码

( j( b) W2 P5 t4 p; n- q2 B8 jmath.sin(x)  求x(x为弧度)的正弦值
' w0 ?* O" l  c6 f& Q% i
  1. #求x(x为弧度)的正弦值6 T3 q: C" K' K( k/ v9 O8 `
  2. sin(x)
    # O: a( T9 t- n) ~- P, Z# d9 V
  3. Return the sine of x (measured in radians).
    . g- i. L/ a" j: q$ e$ j& B
  4. >>> math.sin(math.pi/4)
      X: t$ ]$ V, p! e6 }" [
  5. 0.70710678118654758 K: C; c4 v5 X
  6. >>> math.sin(math.pi/2)
    & t( T: X# i& T+ B1 {+ k2 B
  7. 1.0
    8 Q' |& Z5 E  T* j
  8. >>> math.sin(math.pi/3)
    / F2 w/ |+ _+ w) B* f
  9. 0.8660254037844386
复制代码

' k4 i+ O7 _; f9 K9 i" r+ _  L* wmath.cos(x)  求x的余弦,x必须是弧度
' @" P$ B$ X* h  q0 T) S1 Y
  1. #求x的余弦,x必须是弧度
      ^# P2 Q! K' {+ w3 T
  2. cos(x)
    # T* Z: O% K' Q. o1 V( a( x% |
  3. Return the cosine of x (measured in radians).7 `( ?5 a* u, V) p4 ?" u; n
  4. #math.pi/4表示弧度,转换成角度为45度
    4 j7 l$ M* }2 K
  5. >>> math.cos(math.pi/4)) R: K- w5 G0 N  `/ O# a
  6. 0.70710678118654767 D  \1 \3 l4 `) ?& g7 b' s
  7. math.pi/3表示弧度,转换成角度为60度3 }$ E- V; E9 P3 [; l% }( y  Q
  8. >>> math.cos(math.pi/3)4 N. P# ~2 n$ P# R
  9. 0.5000000000000001
    0 }& Q, o- F/ W/ D2 B, z
  10. math.pi/6表示弧度,转换成角度为30度
    7 X9 H+ \5 I5 p
  11. >>> math.cos(math.pi/6)
    " s; c5 y/ c1 R& a2 W: v
  12. 0.8660254037844387
复制代码

& g1 G$ U5 V( N7 A2 `/ umath.tan(x)  返回x(x为弧度)的正切值* d5 B. \; U* O6 D& v1 \+ G+ z
  1. #返回x(x为弧度)的正切值" O1 N  t% Z& o8 B+ }
  2. tan(x)3 R, @; C/ V% I# S9 S
  3. Return the tangent of x (measured in radians).  ?4 |3 O7 k, A8 W: B" l/ Q
  4. >>> math.tan(math.pi/4)
    2 W+ i# b& M. Z2 Y" a9 b
  5. 0.9999999999999999
    5 |6 O4 ?$ _- D& R8 d% s, g; R
  6. >>> math.tan(math.pi/6)! d6 y; P3 J- j( M: R# j
  7. 0.5773502691896257
    . P* s0 Y  s8 M
  8. >>> math.tan(math.pi/3)9 ^+ Q1 C- i( d# L- i
  9. 1.7320508075688767
复制代码

% N/ G0 R6 {% Y: M" B' {8 mmath.degrees(x)  把x从弧度转换成角度
0 S8 E* M/ o: a6 p7 Q3 z  ^
  1. #把x从弧度转换成角度: E: }# V% d! G0 a9 {6 ]
  2. degrees(x)* @/ U, F3 n; ?. k# K' k4 {3 z. I6 O
  3. Convert angle x from radians to degrees., u. M+ s9 D' n6 m& X2 u8 C9 i
  4. 8 k* d8 ~. N1 ~
  5. >>> math.degrees(math.pi/4)- Y% G+ G+ Z3 R  R  o
  6. 45.09 E. \( c1 \% S' _0 I
  7. >>> math.degrees(math.pi)
    & y* V7 Q5 w' e0 R" l8 }
  8. 180.02 m4 j6 ^; R- U5 j) \( q1 D7 m0 C
  9. >>> math.degrees(math.pi/6)9 j% b+ z8 z5 x  i4 v
  10. 29.999999999999996
    , x+ F' |  ~% z5 p* t/ n
  11. >>> math.degrees(math.pi/3)! i6 f/ H6 s1 x* n. I: G
  12. 59.99999999999999
复制代码

/ }! P  Z  l8 ~. b3 w+ N( e; Vmath.radians(x)  把角度x转换成弧度( Q; E& S; [% a% U3 g: S
  1. #把角度x转换成弧度
    2 L# ~" F7 t1 K" F: l' Z2 E
  2. radians(x)( V. a# _2 b3 F$ [3 R. Y, l3 g
  3. Convert angle x from degrees to radians.: \# E% F0 H7 t0 I8 Z
  4. >>> math.radians(45)
    , x3 z3 |: h3 E/ r2 j. O, O- v1 w
  5. 0.78539816339744832 w8 c1 ^7 d/ O" h& ]4 w5 o& s! f: D
  6. >>> math.radians(60)
      _0 C3 @$ L5 w% t0 j
  7. 1.0471975511965976
复制代码

. O3 W; ^; y8 H7 d1 cmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
6 S/ A1 z2 w: V! L4 G" l, b
  1. #把y的正负号加到x前面,可以使用0
    ' W+ y' Z/ d( Z. o' c7 S
  2. copysign(x, y)
    " ?" u2 N/ K1 ^6 g
  3. Return a float with the magnitude (absolute value) of x but the sign
    ; e; A4 H/ \, `/ w& h8 Y( G
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 8 s3 K6 _- y' j' g* y; k
  5. returns -1.0.3 k& {5 {* [: f. J( }: E( M; t

  6. 1 K' b. F) j: R, P. f! M5 ?# z; S
  7. >>> math.copysign(2,3)5 L) r! x5 q! Z# T% x) t; `4 g! t
  8. 2.07 L. {' G- _7 y% K: P$ R
  9. >>> math.copysign(2,-3)4 @8 h3 ]& m) A( W; ]2 g6 y
  10. -2.0
    . u. ?! S7 n/ z9 D: ~
  11. >>> math.copysign(3,8): J/ \7 _  M. a
  12. 3.0
    ( A% t" g7 D. }. e
  13. >>> math.copysign(3,-8)5 ]0 w+ S+ }- Q0 ~9 i
  14. -3.0
复制代码
4 R$ `4 B- I0 Y& V8 W9 i- u
math.exp(x)  返回math.e,也就是2.71828的x次方
8 Z8 T- C$ w) G+ ]2 Q0 d" q
  1. #返回math.e,也就是2.71828的x次方
    ' g8 q0 a4 L2 J; J* p
  2. exp(x)
    * w! L. d  e+ y, X
  3. Return e raised to the power of x.
    9 Z6 v. ?4 U3 l; X
  4. : r4 T" P9 h2 }2 I' v( Q4 }5 A
  5. >>> math.exp(1)9 F) ~% A6 ?8 k, t4 c, K  m" m9 I
  6. 2.718281828459045
      k% s% h2 W5 E7 n) |+ ~
  7. >>> math.exp(2)
    - Q2 B: a# M, @: F5 V. j) _
  8. 7.38905609893065
    , c3 k5 {( A; X3 i
  9. >>> math.exp(3)
    $ X, V& \+ A% l. s; i, Q3 d
  10. 20.085536923187668
复制代码
3 b; `& [3 a  I: S
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1& J! M! Y7 h5 G& v1 S7 U2 u
  1. #返回math.e的x(其值为2.71828)次方的值减1
    - n; f; ?4 Q0 j9 u
  2. expm1(x); Y# K, F6 ~- v3 k2 v. E$ m) U
  3. Return exp(x)-1.% W/ R: N7 N: l, {  x/ J  R2 m
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    " T& h6 l8 W6 h+ ~
  5. 6 e' _1 O9 b) ]% U# [& b$ |
  6. >>> math.expm1(1)# ^2 _* A: \2 U3 x3 H4 l
  7. 1.718281828459045, m) j) p# ?8 `+ T6 Q  g
  8. >>> math.expm1(2)
    8 M( @/ S1 A5 w; V
  9. 6.38905609893065
    0 L- f, ~4 U9 b+ f9 f- Z$ y
  10. >>> math.expm1(3)
    2 ]4 f4 H1 e. ~# E# H! w0 j, h
  11. 19.085536923187668
复制代码

$ v9 n: d( j2 K0 d  |8 l' o7 _math.fabs(x)  返回x的绝对值! D2 k( T; q" I
  1. #返回x的绝对值
    / A4 G% U: p* t+ P8 u1 |' S
  2. fabs(x)3 T7 Q! \$ u0 |
  3. Return the absolute value of the float x.( K: a$ e* Y9 d+ N4 T
  4. 9 s; ]: y! n- J5 T4 B
  5. >>> math.fabs(-0.003)
    # b8 |( N7 Y& J
  6. 0.003
    % e9 D% S: }* e0 v. d
  7. >>> math.fabs(-110)
    & [0 M) ]( K: Q+ S
  8. 110.0
    : ~, x/ j* r. n8 y# V$ Q
  9. >>> math.fabs(100)+ j$ _7 r/ S+ A& V& l
  10. 100.0
复制代码

$ v, S$ _% E" w2 Smath.factorial(x)  取x的阶乘的值4 I' Z+ x/ h, X# w4 l4 b
  1. #取x的阶乘的值
    2 p, `2 l$ ]& k1 F; `7 k
  2. factorial(x) -> Integral, @% r! m2 L$ f+ S  u0 H" @% Q( i
  3. Find x!. Raise a ValueError if x is negative or non-integral.& Y$ t# y1 S3 P- n  x
  4. >>> math.factorial(1)
    9 I! k1 h5 H+ `
  5. 1
    7 \8 J- X! o0 L" g5 g  O' }
  6. >>> math.factorial(2)
    : P2 f4 L2 u4 M8 z  k* A( }( S( ?
  7. 2
    : v* v2 g; [7 o; C+ h
  8. >>> math.factorial(3)! J% c' ]# m% Z! O% \* N# x
  9. 60 a- w% O( m  N/ k' e
  10. >>> math.factorial(5)
    4 ^/ _3 Q: b  H3 N( B3 h& z8 \3 l& t
  11. 120* [2 ~, p! s& \4 F# E) O! D
  12. >>> math.factorial(10)
    # r4 @" U" m  R* n$ f- q
  13. 3628800
复制代码

# P9 Z& g& d1 |! L+ bmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
7 x" m6 C/ Z! N
  1. #得到x/y的余数,其值是一个浮点数' d& _. g/ j& [6 v. N
  2. fmod(x, y)" R& M4 [9 x$ m' ]
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    8 r8 V/ l" y# q2 q9 B
  4. >>> math.fmod(20,3)
    9 c2 ?% [5 ?0 t
  5. 2.0
    7 t! g# t' D6 Q2 H1 \, e
  6. >>> math.fmod(20,7)9 r3 `% K, h6 ~6 L- Q
  7. 6.0
复制代码

1 B. k1 [% W8 B! Y9 fmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 V  t0 _5 o6 S6 Z2 E) p8 b
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' Z7 ], K4 ^) K# u& K" l0 O% ^
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; T  H. u8 k: h1 B
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ a8 h$ B; j: \
  4. frexp(x)
    : h5 P1 p- E2 H$ q. Y
  5. Return the mantissa and exponent of x, as pair (m, e).6 {# ]  }0 K3 w4 c
  6. m is a float and e is an int, such that x = m * 2.**e.# ]9 [: t, J; L, I' O% |8 G  j2 ?
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.& y, x( E% b/ ]5 e) _9 l
  8. >>> math.frexp(10)
    1 f! }- d* o; |
  9. (0.625, 4)
    : _. b  S" f2 G& s$ o- [4 o! r
  10. >>> math.frexp(75)
    % e+ L  [& w% b: R
  11. (0.5859375, 7)
    / j& n6 b$ M+ W3 K# r4 y- r
  12. >>> math.frexp(-40)
    1 M0 _; W$ X, }2 w: |/ t( P# F
  13. (-0.625, 6)3 k9 M$ Z% T. n! t
  14. >>> math.frexp(-100)
    4 j% W( M6 |3 [
  15. (-0.78125, 7)- d9 ]  S# [9 J9 N3 b) |8 ?
  16. >>> math.frexp(100); N: \/ [5 a1 C! ^! V$ q) D: D3 T
  17. (0.78125, 7)
复制代码

' m) z1 W6 w) j: z6 O* hmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, k- I$ G0 Y" [
  1. #对迭代器里的每个元素进行求和操作
    0 r4 p! t# E- F& d) a- a
  2. fsum(iterable)3 y: Z0 S: m9 S* K) i5 \
  3. Return an accurate floating point sum of values in the iterable., Z2 N; R- [( h0 q! I& ]$ u4 f( A
  4. Assumes IEEE-754 floating point arithmetic.+ w: S0 R. s* {. F: u) B/ A/ f) w2 v
  5. >>> math.fsum([1,2,3,4])
    0 J& ?5 p- m5 _
  6. 10.0
    4 r- X. w, M0 i
  7. >>> math.fsum((1,2,3,4))
    & I; t$ Y) j0 _7 @3 e
  8. 10.0% }6 X" n2 o" J; R
  9. >>> math.fsum((-1,-2,-3,-4))) M% T4 g8 r3 C- M
  10. -10.0* M; N5 Q% H5 i- s1 y+ O/ h  l
  11. >>> math.fsum([-1,-2,-3,-4])
    8 c; l1 ~" r' L5 d
  12. -10.0
复制代码
; o9 ?' u7 I3 V) v) O* ^
math.gcd(x,y)  返回x和y的最大公约数
# @, a% M: n5 i- K; d# w
  1. #返回x和y的最大公约数; d, A, ?9 L) N
  2. gcd(x, y) -> int7 z, T1 C) U- q- W' z8 h3 j& K. k
  3. greatest common divisor of x and y
      s+ k; B2 w8 B% A- Q3 {
  4. >>> math.gcd(8,6)+ B4 z) e, M) r- ^! P& i; a2 C
  5. 2
    2 \* V6 g, E- N  G% g9 j. T& b
  6. >>> math.gcd(40,20)
    ! q2 }7 C2 N: ~8 v: X- w
  7. 20( b, t. f+ `6 F7 r! A/ [3 U5 r
  8. >>> math.gcd(8,12)3 R( ?% z' s6 O$ T
  9. 4
复制代码
8 E& i  J3 K3 A9 L
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* l/ x5 u" r: H6 M8 e- v7 C1 `8 ?
  1. #得到(x**2+y**2),平方的值
    # @# _' ]0 @: E/ v( t: z
  2. hypot(x, y)
    ! ~7 }4 E8 M& P- N( G/ Z
  3. Return the Euclidean distance, sqrt(x*x + y*y).- L7 W0 b" O7 T7 p% e. Y9 @' k* l
  4. >>> math.hypot(3,4)% e( L# w* S1 Z- ]0 {
  5. 5.0
    : S2 G3 s1 c' z" v* W' |, f- i$ U$ a8 b
  6. >>> math.hypot(6,8). J1 c. C( x7 y7 F, ]+ l
  7. 10.0
复制代码
, y2 p8 T/ e( `: m0 q& d  L
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
- Z) a* g. x+ Q( c
  1. #如果x是不是无穷大的数字,则返回True,否则返回False0 M7 Q  B4 t) Y1 x
  2. isfinite(x) -> bool
    9 l8 k! w3 G2 f: ]3 d/ j5 U" T' s+ W
  3. Return True if x is neither an infinity nor a NaN, and False otherwise., P0 ]- \0 O4 n! Y% w
  4. >>> math.isfinite(100)
    $ s$ G$ N3 J3 V9 l1 W# B% ?9 u& J8 A
  5. True3 R. x0 B) X& W1 G( s+ z, u
  6. >>> math.isfinite(0)
    4 R8 p$ u' K2 G
  7. True0 {- j$ m. e& H( N
  8. >>> math.isfinite(0.1)  `# B- Y, F9 |% w
  9. True
    % y8 `6 g2 y- M# `; b
  10. >>> math.isfinite("a")
    8 g0 F3 j+ t: @9 i
  11. >>> math.isfinite(0.0001)% u( S* Y" y$ W. j" @
  12. True
复制代码

$ u+ |: ?; e: U6 Omath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False: ~9 x3 J, m, _
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    3 H. U2 g2 j8 D# }
  2. isinf(x) -> bool( X8 D( z* K4 k$ }2 P+ \9 a
  3. Return True if x is a positive or negative infinity, and False otherwise.
    - q% }9 X9 \- B7 R1 ]
  4. >>> math.isinf(234)
    $ X% Z  J3 K" H
  5. False  L" a5 }; N. Y- v7 o; t
  6. >>> math.isinf(0.1)5 }7 [4 d0 s# [, v
  7. False
复制代码

' O. T" K! p1 O6 a9 M; Dmath.isnan(x)  如果x不是数字True,否则返回False. C- c/ c: x6 T6 q4 Z2 i& o
  1. #如果x不是数字True,否则返回False
    + S5 G( A+ ^! M/ ~. {
  2. isnan(x) -> bool6 x" Q& s* d5 I' ~) Z0 _! g9 w
  3. Return True if x is a NaN (not a number), and False otherwise.# J$ D2 X0 G9 Q
  4. >>> math.isnan(23)' x, v1 T0 q. `4 m2 U. P
  5. False8 C4 ?  X' f$ b- x& e$ c8 m2 T
  6. >>> math.isnan(0.01)8 W' c* i' r9 u$ x7 ~
  7. False
复制代码
: y& Y; M* y4 I3 P% H- s" _" l
math.ldexp(x,i)  返回x*(2**i)的值
0 ^" v4 t" l$ ^1 p3 J! o( B7 v( t
  1. #返回x*(2**i)的值+ a1 n, e2 F" n
  2. ldexp(x, i)
    " W( i2 O% S; d$ ]
  3. Return x * (2**i).
    " U% M" j- m  p. g
  4. >>> math.ldexp(5,5)
    - U+ r0 A% T& ^2 U5 C5 I
  5. 160.08 k6 w8 j9 r" l8 w
  6. >>> math.ldexp(3,5)' o$ U" D3 y+ k
  7. 96.0
复制代码

& @3 s, \$ q$ Hmath.log10(x)  返回x的以10为底的对数- M3 a: e+ v" H
  1. #返回x的以10为底的对数
    " R- T% d% X7 V9 G  ]  Y
  2. log10(x)
    - k$ H) V; X' @4 a
  3. Return the base 10 logarithm of x., Y4 e$ }% C# {: C% [
  4. >>> math.log10(10)
    2 W! J" W" i7 x' [2 |$ L
  5. 1.0
    $ ^, o7 C1 F$ J/ f! `' D
  6. >>> math.log10(100)
    ' E; N5 G8 a1 h- A5 B0 I
  7. 2.0
    4 U, j4 ?3 v" i& Y7 z2 Z
  8. #即10的1.3次方的结果为20
      q6 D2 _0 ]- |' M' ?
  9. >>> math.log10(20)
    0 W9 J% z1 a$ D6 Y9 E2 t" V0 W8 S
  10. 1.3010299956639813
复制代码
: _' `+ }3 `$ c- q' Q6 i3 T' i
math.log1p(x)  返回x+1的自然对数(基数为e)的值
9 q# z* [6 n( i" d; x$ y& P4 V
  1. #返回x+1的自然对数(基数为e)的值
    0 M- A* |4 k: }  I% T. i6 ?
  2. log1p(x)1 {, n; |* q- G0 W( h
  3. Return the natural logarithm of 1+x (base e).; l8 H9 n- A0 T
  4. The result is computed in a way which is accurate for x near zero.
    9 y- E) l4 x5 p! U) N1 w+ P. W1 ^
  5. >>> math.log(10)7 w. [. L1 x) [9 s' Q' ^- w( E
  6. 2.302585092994046+ g& A! N/ k! I9 G; J7 S. F( Z
  7. >>> math.log1p(10)0 _/ d! h2 ~  y- Z" }1 E8 l9 l
  8. 2.3978952727983707
    % e. }8 A# ]7 y
  9. >>> math.log(11)# c/ u" _. j$ b9 H% p- {
  10. 2.3978952727983707
复制代码

. |! `0 q( I4 d9 h. e5 r- z; Omath.log2(x)  返回x的基2对数
- S7 r' w, `" S2 N. x
  1. #返回x的基2对数7 }% A6 M$ f  Q+ Q
  2. log2(x)8 l- h. \# p; H. b6 _2 ?# R
  3. Return the base 2 logarithm of x.% m1 c# S) d9 s' M3 o
  4. >>> math.log2(32)
    ! {8 S* @8 t9 g& F
  5. 5.0: l8 o) `# d% j- w  B; u
  6. >>> math.log2(20)
    8 h% w% Z1 ?# K, I9 j# Q8 y3 ?8 Q
  7. 4.321928094887363
    2 H& I3 a6 A5 R% v7 z. N# Z' ^
  8. >>> math.log2(16)
    1 b  V2 S, n0 S0 j& h$ a0 Z( k
  9. 4.0
复制代码
9 j3 n/ d* S6 T$ ]. M& r
math.modf(x)  返回由x的小数部分和整数部分组成的元组
: w$ F# a- x# o/ s9 U, X, w# _
  1. #返回由x的小数部分和整数部分组成的元组
    / _+ t0 c6 m( t4 y2 S" `4 b7 q
  2. modf(x); t7 l0 n  m8 v
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ; i2 w5 g% f- l2 Y  M$ M, M
  4. of x and are floats.
    ; d! B. F. A2 N# @0 V" _8 Q' p
  5. >>> math.modf(math.pi)% u% |. V' Q2 b# g
  6. (0.14159265358979312, 3.0); N. u) k) L+ L* W! {$ V' }
  7. >>> math.modf(12.34)% N! a0 e+ ]7 Z9 c3 ?
  8. (0.33999999999999986, 12.0)
复制代码
: `1 v! k4 e& j+ B
math.sqrt(x)  求x的平方根  s) C" J) k+ X& T- g& x& Z
  1. #求x的平方根
    / [9 W0 _- y' z$ M/ ~" l
  2. sqrt(x)
    5 m; ^5 U. b, L) r4 Y4 K' K5 x1 T
  3. Return the square root of x.3 f& Q/ U, ?- k1 w! S, V: W/ m
  4. >>> math.sqrt(100)
    ! m& m; T9 E2 B. r
  5. 10.0
    . S+ r$ |) S% z$ N# X. A
  6. >>> math.sqrt(16)! y; |" q( p( N, |
  7. 4.0: {, F& Y/ V- ]  G( C
  8. >>> math.sqrt(20). z  G9 I0 a8 S2 K5 S! h
  9. 4.47213595499958
复制代码

1 J/ \0 `" E( T. G: L4 H) E9 Pmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; t2 J, x/ ^1 M
  2. trunc(x:Real) -> Integral) F' \' v. k5 S2 @
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.6 s6 J, [" _0 J, E% i+ z7 N; k
  4. >>> math.trunc(6.789)% }9 U' h- j5 q
  5. 6
    / E3 k8 E  w7 `( W
  6. >>> math.trunc(math.pi)
    / a1 Y( r" k6 i" I; h7 P' u
  7. 3
    ' E2 ^1 V) R, Z6 E+ m. |
  8. >>> math.trunc(2.567)
    , {) r4 ]  [2 O- I; x1 ]7 A/ j
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-14 17:08 , Processed in 0.082159 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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