新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
1 |+ G( Q. E. i& t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
) k( ]2 \3 U- b% C( f
+ `/ ]+ }& ]  u4 |# V' e; s# w
方法1
7 u7 ~( _- J4 l  ]8 H* J. T, F1 k
  1. >>> import math( M; t: o8 u) H# p! r9 e' r
  2. >>> math.sqrt(9)
    ) s. T: Y- N6 ]( T1 c3 G3 P  @6 v
  3. 3.0
复制代码
方法2
; S; I1 c# e& a4 {2 C6 U
  1. >>> from math import sqrt
    - o/ T+ W0 p3 N  X8 {, k
  2. >>> sqrt(9)
    ' `2 Z" t, A, X. _) W7 {
  3. 3.0
复制代码

5 I; i) i& ^; O: O; C/ m

5 @: g' W0 F, H: o: q1 @" p
math.e  表示一个常量
: q" y8 y8 D9 Y+ \. l3 A
  1. #表示一个常量# `0 Y! @0 X" l* p2 H2 ^! I
  2. >>> math.e& ?2 H% _. Q9 ^
  3. 2.718281828459045
复制代码
" ?" w; Y$ G9 E" i6 ^) u# ]
math.pi  
数字常量,圆周率

9 I6 S* V" x  s4 H/ o
  1. #数字常量,圆周率
    8 ^% X+ k4 A6 v
  2. >>> print(math.pi)
    ) s( f5 k% R5 `1 ?
  3. 3.141592653589793
复制代码
, H; K; p0 r; Z% ]* c+ ~2 e  x+ K, \
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

- J. ^; j% \3 O  j' [1 l1 W: Z/ n. `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x" B: ~: I# @* t+ i
  2. ceil(x), S/ M2 L/ Y1 j
  3. Return the ceiling of x as an int.8 b8 c2 N' _# w. q* t/ l
  4. This is the smallest integral value >= x.' _( X! q! ]; m) _/ Q; x* L8 q
  5. + S- C$ X  Q1 ^: W- s% X" Y
  6. >>> math.ceil(4.01)# m/ C, m' Z" o8 k# R) b$ ?) E
  7. 59 |- B$ r: Z1 n8 R4 ]
  8. >>> math.ceil(4.99)
    : E& x% q; [* i& T- ^) X
  9. 5
    " a9 V0 ]: _1 C7 _) _( y1 M
  10. >>> math.ceil(-3.99)
    8 ]4 ]# a5 G" L: S$ \
  11. -38 F. V5 C4 u0 R  r
  12. >>> math.ceil(-3.01)
    . s! r) a4 C- q. C, I" s( e, |: T; q
  13. -3
复制代码
2 Q9 }8 l- ^# ]- R& y
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身) ]4 s# k% O/ D$ a# ]# c" g& `
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    , x$ {( ~2 e0 x! }: d4 C
  2. floor(x)
    # W, P" \: @6 c
  3. Return the floor of x as an int.
    : w' h( `8 v. ?
  4. This is the largest integral value <= x.( ~1 c. o3 w  H! h. K
  5. >>> math.floor(4.1)
      o  f7 E/ o9 k
  6. 4
      @7 V0 |, o. q% n
  7. >>> math.floor(4.999)
    & L- {: Q: h9 ?$ R  V
  8. 4
      |8 }3 J) p2 A9 ?6 m" K
  9. >>> math.floor(-4.999)
    ; a8 a5 d, H: J3 f4 N) L
  10. -5
    $ w' ]$ r2 f$ x
  11. >>> math.floor(-4.01)
    " a% p3 U) P; ~- u
  12. -5
复制代码

) W8 f! [  N" ?8 hmath.pow(x,y)  返回x的y次方,即x**y6 \) d+ O+ g+ F; x- u3 V1 J' M
  1. #返回x的y次方,即x**y
    & h* E7 n( t' k' n
  2. pow(x, y)7 u3 D* d, }6 Z5 k) r
  3. Return x**y (x to the power of y).
    # c( X. O9 G* i9 `& X$ z( h( L
  4. >>> math.pow(3,4)7 z1 j4 q$ R" |# F) u* _; K  Z
  5. 81.03 ?, z8 w! z  W* D1 e9 P: l
  6. >>> 9 l) V4 X* K* u0 x5 }2 y( ?
  7. >>> math.pow(2,7)# Q" W, n# N3 }
  8. 128.0
复制代码
8 \. i& P$ x* b% ?3 A0 \( B
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: B5 J2 S  M' d! N( T' i
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! Z$ E4 D$ z+ k' a$ i/ i- ?+ I
  2. log(x[, base])  H$ f- n5 M! r  T: h( n0 m
  3. Return the logarithm of x to the given base.
    2 ?& `3 }. R- S
  4. If the base not specified, returns the natural logarithm (base e) of x.+ c% G. {7 O7 E2 E  b: u$ h4 \
  5. >>> math.log(10)
    2 X3 I5 a8 v( v  v0 [% ~
  6. 2.302585092994046* u% n3 `* Q6 N6 {& B: J3 G
  7. >>> math.log(11)
    2 x4 Q7 ~  g( F4 ?5 g! g0 |2 K; R
  8. 2.3978952727983707
    7 C) s1 M% O3 J- k5 U: h3 S' H
  9. >>> math.log(20)) Z* h- u$ Z" |4 u$ l: T
  10. 2.995732273553991
复制代码

2 [1 J6 Q; K" ~. Z' Nmath.sin(x)  求x(x为弧度)的正弦值" U* s$ w# b+ @7 Y0 C
  1. #求x(x为弧度)的正弦值# ?1 Z' ?" }7 D& W9 h
  2. sin(x)
    ( K) N, A- s7 c& Q  q& M
  3. Return the sine of x (measured in radians).5 I2 f$ o+ P# k
  4. >>> math.sin(math.pi/4)3 h' u1 `6 a- [8 s
  5. 0.70710678118654756 E; I' [( ?( D% ]- k0 \) Z2 W
  6. >>> math.sin(math.pi/2)
    % u/ Y  m# ?, C0 u/ j
  7. 1.0" h; B. i( A+ c( H2 w5 k8 \5 T* J* ^% J
  8. >>> math.sin(math.pi/3)' a: T- ~* L- h: F2 i  C5 t4 f
  9. 0.8660254037844386
复制代码

2 r: b9 d- U/ k& t. s  s, k$ T  }& gmath.cos(x)  求x的余弦,x必须是弧度! p+ @8 z5 e; t
  1. #求x的余弦,x必须是弧度
    * g& U9 `8 Y9 ~3 w0 A7 ~8 \# @
  2. cos(x)
    $ w7 n: O; R7 ]
  3. Return the cosine of x (measured in radians).- C/ w0 q, T9 t- V
  4. #math.pi/4表示弧度,转换成角度为45度
    # V2 p) W$ w1 x# _; n( q, |
  5. >>> math.cos(math.pi/4), {" j0 K6 s& e* u% {1 N; r
  6. 0.70710678118654762 L+ p9 S$ Z8 l  _
  7. math.pi/3表示弧度,转换成角度为60度2 o5 W! o. c' T( `; U+ J
  8. >>> math.cos(math.pi/3), D2 O- a2 n% a/ n) L6 [( o
  9. 0.5000000000000001/ A0 a2 ]4 O" c# P
  10. math.pi/6表示弧度,转换成角度为30度
    . ?) y7 X- e3 u2 Z+ F
  11. >>> math.cos(math.pi/6)
    4 s. Q' u; z9 b
  12. 0.8660254037844387
复制代码
0 [) L1 i+ O& ^" @
math.tan(x)  返回x(x为弧度)的正切值
& @7 L+ ~, w! N3 _5 i/ q
  1. #返回x(x为弧度)的正切值
    . g; [1 A+ d9 }# O
  2. tan(x)
    : I7 Q. N  X/ X: G% _% N
  3. Return the tangent of x (measured in radians).
    4 D8 t! k: V, ~  y$ B
  4. >>> math.tan(math.pi/4)
    , S% {/ Q/ q, u, L2 d
  5. 0.99999999999999992 B* V. }% O1 L
  6. >>> math.tan(math.pi/6)
    ) v( p- Z# O( J/ `, j4 j
  7. 0.5773502691896257
    ) ]! N7 J9 w- |1 Q3 r
  8. >>> math.tan(math.pi/3)
    0 S# [0 O4 z1 l( D8 V
  9. 1.7320508075688767
复制代码
" u- \) r) i! z; P, `& l0 W
math.degrees(x)  把x从弧度转换成角度
0 I4 F7 D6 i6 j: R
  1. #把x从弧度转换成角度
    9 k, \+ @/ T0 v/ v
  2. degrees(x)
    . G0 y! B# H/ O$ ^+ @
  3. Convert angle x from radians to degrees.
    ' N4 a; L# q8 G! w" y8 c7 X, C
  4. " X; ~. D! F( L3 R8 P
  5. >>> math.degrees(math.pi/4)
    2 W' w0 G$ N* l9 _, V) q! C
  6. 45.0" y3 S* N. G# F5 }: ?! N2 ]) a
  7. >>> math.degrees(math.pi)
    ; ~. O  P! G, m- }2 i
  8. 180.05 a' f+ q! G7 Z% y; n6 r- k  @
  9. >>> math.degrees(math.pi/6)4 W3 `# y5 ]" ?: J3 Z7 Q- c
  10. 29.999999999999996# h, k" j. A8 S/ b/ }) Q
  11. >>> math.degrees(math.pi/3)+ {& w# A1 |( ]+ s
  12. 59.99999999999999
复制代码
6 R- [1 [* x6 |7 x/ t
math.radians(x)  把角度x转换成弧度
' N! s# s( n% Q1 j
  1. #把角度x转换成弧度
    ; k" Z/ o1 [! G7 R* c( n0 G; F
  2. radians(x)
    $ B/ `& c3 l8 E; v' K
  3. Convert angle x from degrees to radians.
    & |% T3 g- a% ^& y1 c3 K
  4. >>> math.radians(45)
    & f* v, B0 {( m
  5. 0.78539816339744837 G) F1 J& G0 k6 A
  6. >>> math.radians(60)' V6 N0 N- E# h- X" e# Q" Y
  7. 1.0471975511965976
复制代码
' w3 O- g- F& J( [2 D9 Y; t' \
math.copysign(x,y)  把y的正负号加到x前面,可以使用04 M% p& `3 e% N3 B% t$ [
  1. #把y的正负号加到x前面,可以使用0
    * n. w; X$ K  B' E$ e, e
  2. copysign(x, y)  R& ]: G' C) W9 `& t! h# R& R2 a$ j
  3. Return a float with the magnitude (absolute value) of x but the sign
    * x, i$ A$ |' J# q3 e' p/ w+ I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) Q% G' S2 s; v* d1 S" U; L
  5. returns -1.0.0 D# F* r/ A1 T3 V2 x
  6. # h' E2 o" S, W( l9 c0 L& ~
  7. >>> math.copysign(2,3)9 H  P6 ]9 l/ R! C7 `
  8. 2.0/ N1 r7 j' l5 F8 y* @" }
  9. >>> math.copysign(2,-3)
    " B" y, {) h8 J
  10. -2.0- d1 G; \1 n: q0 O
  11. >>> math.copysign(3,8)8 O2 C! q7 Z2 z4 B
  12. 3.0
    - B( K) J! t% S4 [6 }0 ~
  13. >>> math.copysign(3,-8)- J% x* K. [3 A3 f: k
  14. -3.0
复制代码

7 X# J& g& o7 ?& g5 I8 d, e* {math.exp(x)  返回math.e,也就是2.71828的x次方
7 a1 [/ R  _! T, D: P
  1. #返回math.e,也就是2.71828的x次方
    , [8 K; W9 d" T( \% M
  2. exp(x)8 W0 I% u: ?! i8 ^5 j) c. ~: j
  3. Return e raised to the power of x.
    ' {8 Z  m& {  A% g2 z

  4. ; Y, F4 D' b. n) z- ?7 ]
  5. >>> math.exp(1); g  m. [' q, o. U! `
  6. 2.718281828459045. ~4 t) e; X0 ~& }( c1 L  I
  7. >>> math.exp(2)" g! f, V$ s  ~# j
  8. 7.389056098930658 N( R1 \) c! z' L6 s
  9. >>> math.exp(3)" V9 q7 `& o' l
  10. 20.085536923187668
复制代码

* u( o* t9 s! A2 s. }# pmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
( {3 u) @7 k1 c7 V& H
  1. #返回math.e的x(其值为2.71828)次方的值减1
    9 A- ]4 O" Q5 s
  2. expm1(x)# ]; c% r& R/ u" b
  3. Return exp(x)-1.
    ; Z; G9 ~3 m7 D. e8 y& P) Z$ O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    & f5 K; M  `5 \3 h
  5. : k9 w& O0 v: q* {! V
  6. >>> math.expm1(1)
    0 k4 W) j0 d7 Y# w4 \# U* P
  7. 1.718281828459045+ B! N& }( ~( A, G- p, y, c7 j0 {
  8. >>> math.expm1(2); }) z5 ?4 E" H5 G
  9. 6.389056098930655 |' u4 J. M/ L8 u5 C% j' A% O
  10. >>> math.expm1(3); w; b/ ?/ W( J8 B; P. b
  11. 19.085536923187668
复制代码

1 S" \1 q6 x; Hmath.fabs(x)  返回x的绝对值
+ E. d) e$ X$ B& N, e) S5 V- L
  1. #返回x的绝对值
    4 O* f' C4 n: i2 \5 _
  2. fabs(x)0 ^* {/ z' n  ?
  3. Return the absolute value of the float x.
    5 @* n3 i( F& L

  4. ' L/ d# B) e9 y( ?( }) m- J" `
  5. >>> math.fabs(-0.003)
    - R" \; Z5 e. N5 R$ P
  6. 0.003
      b: M7 l" d8 Y- n* [$ K
  7. >>> math.fabs(-110)9 D! D' y. ]( B7 `* y7 `% o
  8. 110.0
    3 ]! x. b3 ~" F* Z
  9. >>> math.fabs(100)  Q9 I( ?" C5 D% \3 _" a
  10. 100.0
复制代码

8 |2 h+ l; z, i( [. Emath.factorial(x)  取x的阶乘的值
' Z) L8 Z7 q$ ?/ i' V
  1. #取x的阶乘的值
    : p7 X0 p9 r$ O8 P$ {+ w3 H$ i' d
  2. factorial(x) -> Integral) z" w# G, B$ l, |) N
  3. Find x!. Raise a ValueError if x is negative or non-integral.( s' w9 p! A# o1 x
  4. >>> math.factorial(1)9 Y; t4 [; i' B3 A
  5. 1
    ( \: S* W  A* J& e" R9 q
  6. >>> math.factorial(2)0 C$ w# `1 d' J' S
  7. 2
    5 S5 K# q' B' j( e& [! z, y
  8. >>> math.factorial(3)6 e6 T5 W. [0 s" @; T# o
  9. 6; f: P) r6 x% W  |/ }, L
  10. >>> math.factorial(5)! E2 R1 p. u  H: T
  11. 120
    : F% v: `  Y% n; \7 G3 W  X
  12. >>> math.factorial(10)8 [$ c+ t' q' E6 v- T
  13. 3628800
复制代码
* b. I# p+ T) f  w
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数+ {9 T3 q7 m" T& E6 Q$ F
  1. #得到x/y的余数,其值是一个浮点数
    3 Y2 B- G! @+ d" K) v% e+ Z$ n
  2. fmod(x, y)5 D7 d) E# U. M( O4 x  o7 v
  3. Return fmod(x, y), according to platform C.  x % y may differ.1 U7 s  j. U8 u( [, k9 a
  4. >>> math.fmod(20,3)
    ' G  H3 u+ }+ M1 w: v7 p1 H
  5. 2.0
    ( X  A. C# r( J2 {, p! a" a
  6. >>> math.fmod(20,7)& J9 @2 k+ S% A! g
  7. 6.0
复制代码
+ P9 P! E1 X" S) j& d
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 m8 ^! G2 B5 A# g
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% y1 ~5 E# |2 D' Y; K: T  v
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    3 I. x2 L, ~4 b+ e0 ?7 n
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    5 c. x3 @! o' e# _
  4. frexp(x). r, K6 b# x, U+ R5 _' m
  5. Return the mantissa and exponent of x, as pair (m, e).
    ' I& B% F; r3 D0 W
  6. m is a float and e is an int, such that x = m * 2.**e.6 Z: R; }& r0 s- a( M& B  G$ ^8 v
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! a: v4 o+ H/ T4 ?+ J# }7 @6 B/ f
  8. >>> math.frexp(10)
    0 p8 ^: N6 c0 A1 x2 l# M
  9. (0.625, 4)
    6 Z+ D) ]9 }: T: }
  10. >>> math.frexp(75)+ K3 k- Y8 ?2 l4 C
  11. (0.5859375, 7)
    # ^, Q' C5 C) t% k1 O5 Y' W: q
  12. >>> math.frexp(-40)5 |6 T4 K. [  |- S3 s
  13. (-0.625, 6)
    4 d! W* @' J' R, L9 n
  14. >>> math.frexp(-100)" Y) s( Z1 W  a0 J
  15. (-0.78125, 7)
    1 n/ N- O* n' t& g' F+ q
  16. >>> math.frexp(100)  T3 y& V$ ]7 h: v
  17. (0.78125, 7)
复制代码
- H% P3 {2 i% W; x$ z( z/ G1 R
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
  y6 S( q9 I0 }1 m
  1. #对迭代器里的每个元素进行求和操作
    * d, ^- O' Q8 m3 O3 Z
  2. fsum(iterable)
    2 Y2 Y) l( ^$ F" c4 i& o
  3. Return an accurate floating point sum of values in the iterable.
    + E  ^4 Q% C; @& F4 D
  4. Assumes IEEE-754 floating point arithmetic.
    + R0 H. X/ G: j5 h0 u- J1 G; B$ Z
  5. >>> math.fsum([1,2,3,4])0 |. D" e1 d- }: j! \/ ^' \
  6. 10.0
    4 Z& B$ I# U' q+ e: K" R* @
  7. >>> math.fsum((1,2,3,4))+ }4 \' n: U0 |% c
  8. 10.0
    , ~9 v' \9 c. q9 d4 J1 R
  9. >>> math.fsum((-1,-2,-3,-4))( ~3 E& c8 [' h3 a+ H
  10. -10.0( y0 h2 b. a8 R) Y, Y% F4 f! C9 x
  11. >>> math.fsum([-1,-2,-3,-4])3 _4 e4 x$ |- n5 Y6 b& i4 h: B
  12. -10.0
复制代码

8 q6 s# r- L& R" R) Bmath.gcd(x,y)  返回x和y的最大公约数
! @. G( }) k2 c! Z5 U
  1. #返回x和y的最大公约数6 w5 X$ v- J2 f' c+ {
  2. gcd(x, y) -> int4 S9 O7 n9 V; D; j
  3. greatest common divisor of x and y
    6 M- Q4 b4 p% Y
  4. >>> math.gcd(8,6)9 Z8 I9 K6 ]3 v- f4 d! f; ~- R
  5. 2
    ) B! w$ D0 a. w& D0 [0 Q
  6. >>> math.gcd(40,20)% L+ n% c' L5 @9 i  b# S- ~
  7. 20; R  `: p5 e! X: _3 K# K
  8. >>> math.gcd(8,12)
    + e. b. h8 l. y8 R3 I# S7 [+ y
  9. 4
复制代码
0 R) {: E# t" F0 l! c
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False3 E* m& X6 |; C3 R
  1. #得到(x**2+y**2),平方的值
    8 q0 K. K" h, U# {) f# r5 Q4 x
  2. hypot(x, y)
    3 d2 t$ X0 k" b/ D; a7 i6 B
  3. Return the Euclidean distance, sqrt(x*x + y*y).( Q( S) x9 R( m4 k
  4. >>> math.hypot(3,4)
    ! C6 {$ d3 s% |' N# Y
  5. 5.0
    9 U' t6 b2 Z2 L
  6. >>> math.hypot(6,8)
    ! e! c( A: u# F' }4 P* z  u
  7. 10.0
复制代码
  j: o0 T3 B4 h! _& }: `7 o, d% D% h
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
& u1 S; h1 ^) b
  1. #如果x是不是无穷大的数字,则返回True,否则返回False7 P  P* J. C: ^
  2. isfinite(x) -> bool
    % J9 _! ^) s: a- ~( }8 H
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    & h& y! K8 j& ?& Y8 A* g
  4. >>> math.isfinite(100)) N' @" Q, w. c: }# W
  5. True
    9 S! m! E% `# |5 Y2 @/ \& O  \
  6. >>> math.isfinite(0)
    7 \- Z0 Q1 e, a
  7. True% U7 K' u& D# X7 D: v1 G4 f6 ~* p, M
  8. >>> math.isfinite(0.1)
    ' ~' b  T; U$ R' T
  9. True
    : L( i/ |. S* B/ o, \- S$ ?' Z
  10. >>> math.isfinite("a")- f' `- O6 J8 x+ `" w: k0 h/ r5 B$ G
  11. >>> math.isfinite(0.0001)$ w/ O) G1 x7 Y% o$ E, o
  12. True
复制代码

, X' L$ Q4 s! `$ \" Y) a/ C7 w0 umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 j3 X% c6 u; Z  I, q2 d  k0 F
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False- v, A. X9 M5 ?: I7 ~' Y# j9 `5 p
  2. isinf(x) -> bool
    ! ^3 k6 ^& Q: t6 r+ w& [
  3. Return True if x is a positive or negative infinity, and False otherwise.) s2 C1 K+ }0 Q9 X1 r
  4. >>> math.isinf(234)
    ; [" A  h8 p8 `/ V/ B0 u/ m( B5 ~
  5. False
    2 z! @* H- Y3 V" M' l6 n% H
  6. >>> math.isinf(0.1)
    : \6 Q, M* M" o% b5 l
  7. False
复制代码

. Q, d- H; p- K/ Ymath.isnan(x)  如果x不是数字True,否则返回False: {* ?0 b5 U7 A, ^( C& R# ~& F  p) E" ^
  1. #如果x不是数字True,否则返回False8 ?* ^! `6 h$ D+ a- L0 D
  2. isnan(x) -> bool
    & P! U4 d5 C' |9 E: \0 l
  3. Return True if x is a NaN (not a number), and False otherwise.$ R* x' P( t" I
  4. >>> math.isnan(23)
    & _. l! F# O' R! {, C4 ]
  5. False2 C) \/ a: c2 L$ h; [
  6. >>> math.isnan(0.01)$ \. M3 c, [) r0 C
  7. False
复制代码
4 k4 P6 u( C! K- r" J5 I
math.ldexp(x,i)  返回x*(2**i)的值
8 X7 _  F% K9 A9 s* ]( {2 g
  1. #返回x*(2**i)的值( q+ K* a7 [( g( u5 g
  2. ldexp(x, i)1 B. d: J# o2 W- O- G
  3. Return x * (2**i).
    $ i& }9 p$ I  W
  4. >>> math.ldexp(5,5)
    % O7 W5 F8 c3 L* @! Q9 O
  5. 160.0
    0 h) u0 H2 ]4 |
  6. >>> math.ldexp(3,5)
    8 [$ x/ V: D; L
  7. 96.0
复制代码

4 J  H+ N6 k6 Ymath.log10(x)  返回x的以10为底的对数
' Y& g" `9 y' F' C5 o& {/ p
  1. #返回x的以10为底的对数
    7 _: a. ?+ k; h: j+ B+ }7 f
  2. log10(x)
    / G! ?. v- u1 _! s
  3. Return the base 10 logarithm of x.. I. z1 ~* ]& O% U5 n/ w
  4. >>> math.log10(10); \" L; i$ e% O# e
  5. 1.0$ h2 W2 X  {2 K6 c1 A, m
  6. >>> math.log10(100)
    / q( [( {' _# A: w
  7. 2.0
    # l8 x  [1 Q* o" @; o, f: P( Y
  8. #即10的1.3次方的结果为20/ a4 ]8 F8 Y/ }, P, r( Q# q5 u
  9. >>> math.log10(20)6 O; q+ S- i" e7 J; f& a7 k
  10. 1.3010299956639813
复制代码

1 C, x4 [( o: [( Cmath.log1p(x)  返回x+1的自然对数(基数为e)的值8 y) o: E+ p2 r9 u9 J- }3 {" c' [
  1. #返回x+1的自然对数(基数为e)的值( f* i% O: E4 ~- u+ Z% }
  2. log1p(x)
    3 k& m1 j1 o( \1 r+ F6 _
  3. Return the natural logarithm of 1+x (base e).
    % a* O& @, r4 m% v
  4. The result is computed in a way which is accurate for x near zero.
      i  A5 q0 O2 R* v, k- \
  5. >>> math.log(10)
    $ |; r' S0 Y1 k+ C& ~
  6. 2.302585092994046$ T6 `! i% s: `* r. }! u: K* I) `* S
  7. >>> math.log1p(10)
    5 p7 s# b' e$ O% O; U1 ]$ k8 V* }
  8. 2.3978952727983707+ B3 ]8 c! T( Y9 L$ K! V% C
  9. >>> math.log(11), Q4 H) I' s# C. J
  10. 2.3978952727983707
复制代码
; r& k' ^( h; k- M5 Q8 R$ n7 P. ]
math.log2(x)  返回x的基2对数
/ I9 u0 `, A# s
  1. #返回x的基2对数; d3 ]. a2 |) i2 S( d2 \9 J3 R8 G
  2. log2(x)
    + K0 K/ n4 }6 v) [0 u( D- C
  3. Return the base 2 logarithm of x.9 T  a2 y% T& J! D/ v9 w" p& B
  4. >>> math.log2(32)
    4 o' F; `$ L8 _. t* }! J
  5. 5.0
    8 w0 g) a  L% j& J) c
  6. >>> math.log2(20)
    $ p8 \1 Y8 n5 h
  7. 4.3219280948873637 ]1 F1 a+ G, L* W6 Y
  8. >>> math.log2(16)# S( S, N  B  O" D0 x
  9. 4.0
复制代码
7 w) X9 V! X% a; S* m
math.modf(x)  返回由x的小数部分和整数部分组成的元组
/ u) h7 m7 X$ T! `5 N7 h# U4 U
  1. #返回由x的小数部分和整数部分组成的元组4 X8 {  u8 |" V9 k
  2. modf(x)5 ~& x8 j9 E/ y" }( e+ k
  3. Return the fractional and integer parts of x.  Both results carry the sign0 i: \) W: v# q2 l0 r* P
  4. of x and are floats.
    - u2 Y' S7 w" Q! D# ]
  5. >>> math.modf(math.pi)& a% V9 q& V1 h) W6 h
  6. (0.14159265358979312, 3.0)
    9 }3 G. U4 e6 N& g! Y' G, K8 Q/ i
  7. >>> math.modf(12.34)% E6 X7 l: \3 A/ m, t; P# j
  8. (0.33999999999999986, 12.0)
复制代码
( i& _/ F* [/ D- u# S
math.sqrt(x)  求x的平方根
' Z* u2 M+ h" R3 a9 ~% b0 z
  1. #求x的平方根5 @4 {7 C* K5 n9 k/ Y
  2. sqrt(x)# C( v! R  i6 |; H6 H; ^2 i: ~
  3. Return the square root of x.( l: t; D2 w  z/ ?
  4. >>> math.sqrt(100)1 [" K* h- W  X# m3 G$ j7 V
  5. 10.0
    ; O: J( h: T# [
  6. >>> math.sqrt(16)" B1 }- ]- q8 ^" d* F) [
  7. 4.0
    # k5 ?0 r1 S9 c6 f6 V" P9 v+ w* p
  8. >>> math.sqrt(20)* f" t4 U0 I& o
  9. 4.47213595499958
复制代码

# L! e- `- `: \: W, Cmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; S6 I9 a1 T" o1 f- k
  2. trunc(x:Real) -> Integral/ w, I6 i. d' T- z& D) @8 C
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    / |% @% v0 S  F/ E) D# U3 k# Q
  4. >>> math.trunc(6.789)8 q/ \; u# f9 ^2 F7 Q' C
  5. 66 m% L/ S( O: c! b
  6. >>> math.trunc(math.pi)8 Z3 c. }  v: ^* `3 T
  7. 3' C* E1 |- z, C9 k! I9 w
  8. >>> math.trunc(2.567)2 g0 Z( P* R# Y4 t7 Z8 d2 t! q* N
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-13 16:04 , Processed in 0.096798 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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