新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

/ c2 x* E5 x0 \5 J【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ q$ k+ Q' V* c: c

3 J1 N! q2 n5 R. S. i0 ~方法1
4 k0 J% s) s% b
  1. >>> import math. Y0 G0 @7 p, ]. w5 v
  2. >>> math.sqrt(9)
    ; Q+ c% r4 y) V9 e! {/ O# z3 H
  3. 3.0
复制代码
方法2
3 g; Q" D3 d5 N0 n4 n# k/ B5 L
  1. >>> from math import sqrt
    % J! b" R" a, t: Z& E
  2. >>> sqrt(9). M! r8 M) H9 Y* N
  3. 3.0
复制代码

5 }" S% S$ s9 o

- e# y6 D2 {3 C3 L2 ]& n% U
math.e  表示一个常量
) N5 [$ F- ~9 s8 H5 K( M
  1. #表示一个常量
    8 p. h6 [  C% a! Z$ F7 ]& x
  2. >>> math.e
    & H; b' E& a# K4 b7 S3 U! Z
  3. 2.718281828459045
复制代码
! z" |  j  G. m3 u+ H- ~
math.pi  
数字常量,圆周率

& n  b6 C" M  i% V  X
  1. #数字常量,圆周率
    6 c$ |( o' z" b% G0 [- ^! n
  2. >>> print(math.pi)4 q+ C' m+ T! y& R( E
  3. 3.141592653589793
复制代码

6 q1 _; p0 I& Nmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

3 Y  y. c4 Z: t  `8 ~! y
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    / O' P7 T) E1 Z5 Z. v+ d5 q
  2. ceil(x)5 s8 F3 D" h. V
  3. Return the ceiling of x as an int.
    - ?/ t! \0 ~2 |
  4. This is the smallest integral value >= x.
    ) M6 f$ M0 u" X7 x8 A6 b$ A

  5. + p  v% C! \: Y3 x# r3 r
  6. >>> math.ceil(4.01)
    * K7 s# T8 w% W0 c' q4 l% s5 v5 ^
  7. 5: o7 C: y* |1 x' C4 P
  8. >>> math.ceil(4.99)
    - c. ^- l$ L* _' G5 r4 {- s% ^
  9. 56 G; K- p0 M" Q1 a8 {- @6 m
  10. >>> math.ceil(-3.99)
    ! h& t" T/ a& S
  11. -3
    % ~: L$ `0 }; P( b- j" `8 _9 Q
  12. >>> math.ceil(-3.01), g& R+ Q: j- X$ Q
  13. -3
复制代码
7 q  p: q* W% S% H
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身! q7 k, E$ r8 `6 P1 g
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 n  T2 X( B6 ^+ T1 _8 I$ V9 ]$ K- \8 j
  2. floor(x)3 i( g0 X; ]9 c
  3. Return the floor of x as an int.
    8 c0 g& m9 x- P3 p5 D1 t
  4. This is the largest integral value <= x.
    ! Z; R6 ]1 J% _$ F
  5. >>> math.floor(4.1)' w6 J* _, X& x) v8 F
  6. 48 [: O! W, k6 v" Y
  7. >>> math.floor(4.999)9 R$ B4 Q3 j# O# C/ Q2 R7 @  z
  8. 4
    . O$ g1 H2 {: v3 K. e
  9. >>> math.floor(-4.999)6 E  C$ G4 w. j
  10. -5- z( O! A4 U$ w$ l! D& Q2 _7 W) N
  11. >>> math.floor(-4.01)( k; j) \* C9 J+ P: W0 g- z6 t/ f
  12. -5
复制代码
3 B. a/ t- u: z8 q
math.pow(x,y)  返回x的y次方,即x**y, M! ~+ j9 i+ l  _3 E' B9 P. F' V
  1. #返回x的y次方,即x**y
    + V6 E! V5 e- z; w% ~# q" U
  2. pow(x, y)0 q) N' w$ x+ V5 J4 ~
  3. Return x**y (x to the power of y).
    ! Q% ?. g3 }- C9 O8 G: Y2 `
  4. >>> math.pow(3,4)
    5 M' m/ I+ l9 c" ?3 a8 s" `
  5. 81.0" J$ t8 e9 X; i8 y
  6. >>> " n& Y& g. G" }) m2 n$ o
  7. >>> math.pow(2,7)4 e2 Q# ~' I1 L( c; N
  8. 128.0
复制代码

; L  y6 k" e# B- C3 t/ G6 Pmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
7 x0 ?( K! M2 y- I" R! s5 n
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    4 L3 ^+ n6 m' B' x' B
  2. log(x[, base]). Y; g' E4 H  ^) d/ T3 u
  3. Return the logarithm of x to the given base.
    * w* c- C3 A, z+ Y( t) [
  4. If the base not specified, returns the natural logarithm (base e) of x.4 R/ e9 r/ X+ r& E  y
  5. >>> math.log(10)( \$ g7 Q2 m+ Z5 _  p/ Z% W5 u
  6. 2.302585092994046
    : Q' f) Z0 K# N$ j2 r  w
  7. >>> math.log(11)
    9 h9 F  W7 L% @# P$ n
  8. 2.3978952727983707
    ( f1 b& `8 S/ z6 s8 ^! g: j
  9. >>> math.log(20)
    . E$ L" I: |, G6 @# a3 Q5 J8 M
  10. 2.995732273553991
复制代码
' ?; z  Y5 R' b5 m0 W$ F
math.sin(x)  求x(x为弧度)的正弦值& d9 i' Y: B# p5 A% d4 }
  1. #求x(x为弧度)的正弦值7 D# h* y1 s) M8 k! A" ?
  2. sin(x)- P- l2 l% h3 Z# Q# \) P/ O. O
  3. Return the sine of x (measured in radians)." ?$ X: w; z! C
  4. >>> math.sin(math.pi/4)
    ' K+ L( K  C# h* |- e
  5. 0.7071067811865475
    " Z# g( L4 d0 r" O, J
  6. >>> math.sin(math.pi/2)
    - [, q$ h7 n# x$ Z5 v6 C) ?
  7. 1.0
    4 Z, `2 @$ O* w9 z4 L
  8. >>> math.sin(math.pi/3)
    9 F7 m( M9 H  z  H  v8 U
  9. 0.8660254037844386
复制代码

) U1 s( \+ f  U, Wmath.cos(x)  求x的余弦,x必须是弧度: j2 T- h: E+ }& Q
  1. #求x的余弦,x必须是弧度
    $ C4 ?8 K0 h4 \- d
  2. cos(x)! z; m* L: n, v( h$ Z" D3 x
  3. Return the cosine of x (measured in radians).) ]* Q; O2 i5 m+ G# G
  4. #math.pi/4表示弧度,转换成角度为45度
    9 u% |4 n5 P/ }0 T, U5 y% p
  5. >>> math.cos(math.pi/4)
    ( j4 a4 \0 E7 D0 U% J7 O# }
  6. 0.7071067811865476' V! h, S: }* W. Y
  7. math.pi/3表示弧度,转换成角度为60度1 E% @2 ]8 q; j; W& i
  8. >>> math.cos(math.pi/3)
    ) d+ Q. _& R% P/ [
  9. 0.5000000000000001
    3 n6 B$ {1 b$ P  H, c( r6 ^; ?
  10. math.pi/6表示弧度,转换成角度为30度
    * j9 }* ^/ e* U
  11. >>> math.cos(math.pi/6)$ ^+ k. Q% o3 ^: a" a" r
  12. 0.8660254037844387
复制代码
$ }! ^# @$ Z" b1 c: n7 `3 Z
math.tan(x)  返回x(x为弧度)的正切值& |$ E, Z8 q7 A
  1. #返回x(x为弧度)的正切值6 K. s0 R* u) b  K
  2. tan(x)
    5 y% O8 B' v. D& H# o
  3. Return the tangent of x (measured in radians).3 \/ a  P2 X7 W9 k1 Y7 [& Z
  4. >>> math.tan(math.pi/4)
    ; E: M- c! K2 ~! F8 J% M! C
  5. 0.9999999999999999. r5 J! ]" ~6 r- X2 u2 {
  6. >>> math.tan(math.pi/6)5 O8 o3 r  y5 s
  7. 0.5773502691896257
    ; t  ?6 Z( C  z- A2 b3 S. r, Z8 C
  8. >>> math.tan(math.pi/3), x# x  H: Y* F- y; f
  9. 1.7320508075688767
复制代码

+ W( J* C5 ?9 D" e- M5 W/ wmath.degrees(x)  把x从弧度转换成角度
* w) n" O% Y6 P/ s
  1. #把x从弧度转换成角度
    % e, O9 l$ t3 [0 N0 r8 o8 i
  2. degrees(x)
    2 x- H( t+ t* V& W. A; R
  3. Convert angle x from radians to degrees.1 ~8 D5 m  q4 N$ S! M* F
  4. 3 L& X( v* I/ K6 {; W) X
  5. >>> math.degrees(math.pi/4)
    0 u/ N* t. ]) V/ t) D7 W2 p' U
  6. 45.0, t3 z& ?2 H, S0 K6 J- ~
  7. >>> math.degrees(math.pi)  G8 A3 d; d+ M: L: H, S3 Y% A
  8. 180.0! w; Z+ S% {; Y8 x$ a
  9. >>> math.degrees(math.pi/6)
    $ Z% O. U8 `: x  ~; k
  10. 29.9999999999999961 E& p: ]4 J% d6 M( a
  11. >>> math.degrees(math.pi/3)( [' A, _" [3 J8 w& w6 h
  12. 59.99999999999999
复制代码

6 _$ Y4 [1 k/ T4 N& emath.radians(x)  把角度x转换成弧度
1 C# V. l. q# C3 J5 q
  1. #把角度x转换成弧度- x- {8 Z9 y! E& e1 l
  2. radians(x)! T, k5 S! p; ~- O
  3. Convert angle x from degrees to radians.# J2 r5 ^( _9 Q) d  l+ u
  4. >>> math.radians(45)$ r" ^& {& Y0 N& H
  5. 0.78539816339744839 p' L& V7 [, K& G# l
  6. >>> math.radians(60): N6 B  h) f) ^0 D' {
  7. 1.0471975511965976
复制代码

  ?  I3 h- v# ~% O( Nmath.copysign(x,y)  把y的正负号加到x前面,可以使用0. ]1 p# v; s: F) {
  1. #把y的正负号加到x前面,可以使用0
    * o9 W! w+ B8 e
  2. copysign(x, y), o5 V- y; r+ K% w
  3. Return a float with the magnitude (absolute value) of x but the sign 9 T0 E/ {; U9 h  w, q% d( m
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( o9 _- K. {$ M& J# A# S/ }+ `" u2 U
  5. returns -1.0.
    ; c! v4 A% }% C6 v5 F

  6. . I3 N6 l( Z" S- B7 U/ u
  7. >>> math.copysign(2,3)6 |- B  J/ i. ~# k/ m
  8. 2.0
    # p  ]7 C2 q+ r6 x: `/ J7 B
  9. >>> math.copysign(2,-3)! x, m% e+ J/ v! e
  10. -2.0
      J3 I* Q, i0 H) e. b- A
  11. >>> math.copysign(3,8)
    * t- k5 Y/ ?6 {6 j8 Q2 H2 D  i
  12. 3.0
    $ z% B3 Y6 G, a2 j
  13. >>> math.copysign(3,-8)
    " k  O2 n  v' R) ~
  14. -3.0
复制代码
6 S7 e. U0 c) f. y& ?5 Q8 ~
math.exp(x)  返回math.e,也就是2.71828的x次方7 C9 k9 K8 U/ N$ d7 c5 j
  1. #返回math.e,也就是2.71828的x次方
    " B% Q  J: q) ^  x! V' D2 [
  2. exp(x)+ ~2 Q1 O* O2 x2 [( o
  3. Return e raised to the power of x.% z; a  ~% K, \) F$ }1 Z* @
  4. + _  C' Q( }6 ]* P2 ]+ v
  5. >>> math.exp(1)
    8 H5 ~! a: r% Z5 V7 v5 _
  6. 2.7182818284590452 E* _( }  e$ S& e4 Y$ R) ]7 R
  7. >>> math.exp(2)
    0 B& z* Y4 H4 d, x( i
  8. 7.38905609893065; L' J& }- A0 l$ ]
  9. >>> math.exp(3)
    ! g: _; J6 r5 O2 P) Z
  10. 20.085536923187668
复制代码
& a4 _  d! u( }" R3 ~
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减18 T* g& z6 Y. |2 G  M# M' m
  1. #返回math.e的x(其值为2.71828)次方的值减14 n" H( R) G- f8 r* d  o- Y
  2. expm1(x)
    ! b. H$ t7 r) ]8 i
  3. Return exp(x)-1.
    / P0 W8 r2 [$ E/ H. m1 n
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.8 c/ m" K& w! ?6 B) c  L
  5. & B; M& Y9 h% j2 H; c" A  q/ b
  6. >>> math.expm1(1)1 S8 N0 T4 |" C0 e* m& T
  7. 1.718281828459045
    ; g% h8 o8 n! H) i8 \4 w! t5 V4 s% x
  8. >>> math.expm1(2)- m, ^" v% Q1 m: P$ ~
  9. 6.38905609893065
    9 a9 X! J" Z$ M+ Q
  10. >>> math.expm1(3)3 q4 H. h- u! J& n, l' S
  11. 19.085536923187668
复制代码
- [% |/ q6 o2 b+ h' j: \3 Q
math.fabs(x)  返回x的绝对值
  \5 P( N5 N7 W  n. ~
  1. #返回x的绝对值
    4 A/ G. S4 A  l$ k, \$ |
  2. fabs(x)
    & ]2 H$ B$ N1 S
  3. Return the absolute value of the float x.
    6 p5 J1 m1 z% l" K4 Y6 I

  4.   b; A) ?% N5 G2 B1 s& t
  5. >>> math.fabs(-0.003)
    $ c4 Y: X8 Q- X. l) W$ i
  6. 0.003; u0 _* ~% w8 F7 G, d4 Z
  7. >>> math.fabs(-110)
    $ W# R5 X1 h# T
  8. 110.07 u" u& ]7 n4 c: A" n/ y
  9. >>> math.fabs(100)
    3 F* d: ?1 H% `2 r7 }7 d
  10. 100.0
复制代码

) D1 L5 `  ^# y" G* d1 Lmath.factorial(x)  取x的阶乘的值
# ~- q/ o) {/ M* H! V
  1. #取x的阶乘的值
    % s; }+ K* h6 y
  2. factorial(x) -> Integral
    2 L, J# k' C* ?& L
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    - U: K# R+ @  a" S1 s  |
  4. >>> math.factorial(1)" h$ N8 v" t. z9 v
  5. 1
    " G6 I# W3 G" Z: @" f# L, ^
  6. >>> math.factorial(2)/ Z* y/ d$ \, @/ H0 c
  7. 29 L6 j  O2 u2 E% B
  8. >>> math.factorial(3)( [& X6 R- U7 o8 ?+ y. Y
  9. 6
    / J- Q# ?$ _5 K3 b& z( j
  10. >>> math.factorial(5). b* Z$ S% _, B; ^. Y' ^6 `6 A
  11. 1203 C1 d6 j9 U9 u/ V
  12. >>> math.factorial(10): A9 L' R4 a* w. h* |7 l
  13. 3628800
复制代码

5 J# ]) T  K) X8 zmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
) C$ O. i6 Y. d# c
  1. #得到x/y的余数,其值是一个浮点数1 ^0 v0 }  H- Q
  2. fmod(x, y)
    ; z9 a9 ]) p! s- N
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    , A7 I2 A" V3 X  @' \/ B
  4. >>> math.fmod(20,3)
    - s$ a' n( E: T7 y) [! F
  5. 2.0
    % s6 y( g7 i" y' c- J: ^7 o$ F
  6. >>> math.fmod(20,7)
    ! V! C" z! [2 B* c4 H! Q4 l
  7. 6.0
复制代码

+ D0 B0 M6 z4 J# {math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围! R$ J- [& `- H9 l
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 q  u2 x2 F3 X1 i; B/ J) {
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' r/ i6 e) s. S
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    5 j$ m- _) ^! x& s
  4. frexp(x)# H( L5 \- z* d5 E" w
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 Y' o! E* F- G1 L: R4 I
  6. m is a float and e is an int, such that x = m * 2.**e.6 `( r8 ~' K7 i% o. h
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.5 N; z  k9 S+ o4 N- J
  8. >>> math.frexp(10)
    " d# x% ^6 ^$ k
  9. (0.625, 4). p7 K% V) I, G
  10. >>> math.frexp(75)
    " R4 |: [" x) O9 K9 y4 i; k
  11. (0.5859375, 7)
    6 Z1 V8 a! c* j' D# L
  12. >>> math.frexp(-40)
    - L. Y: E. e3 `
  13. (-0.625, 6)
    ) y; o  C# C0 t% X( e" s# e' A
  14. >>> math.frexp(-100)/ e% |* p- T7 x* D" b
  15. (-0.78125, 7)
    " G  H: ^" J. H, Q
  16. >>> math.frexp(100); k% ?& Y* c; l3 `$ @% q
  17. (0.78125, 7)
复制代码

3 l2 z0 [; V, d& N  rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 i. O9 \% F$ l
  1. #对迭代器里的每个元素进行求和操作
    ; E( u& U5 U) ]$ F
  2. fsum(iterable)$ f  H: n! E" U# b
  3. Return an accurate floating point sum of values in the iterable.
    , O% M( }8 U4 I
  4. Assumes IEEE-754 floating point arithmetic.. J! n4 C/ P7 X, S. b
  5. >>> math.fsum([1,2,3,4])
    ; v4 q) y  S/ c
  6. 10.0
    ( Z0 Q5 G* Q- [* X7 k8 W; O- x% q
  7. >>> math.fsum((1,2,3,4))
    & y$ c( @% w' s" c! b. l
  8. 10.0
    5 o4 f- p2 j$ l% \
  9. >>> math.fsum((-1,-2,-3,-4))
    - ]- A& }  p" u  }+ Y
  10. -10.0
    - N2 R/ W- h6 \6 w( @% h( |! v1 y$ h
  11. >>> math.fsum([-1,-2,-3,-4])
    $ L$ h: E# M& t, d6 I
  12. -10.0
复制代码

2 Q% ]' D" ?1 _3 |math.gcd(x,y)  返回x和y的最大公约数
- }( l1 _& I/ h+ h) N2 {- j
  1. #返回x和y的最大公约数  R6 x9 _: t0 d0 K" Z
  2. gcd(x, y) -> int
    ! N0 n" B: ?. l& @+ {+ o9 t
  3. greatest common divisor of x and y
    4 p7 f. L3 b6 @7 [) M, @% h* R
  4. >>> math.gcd(8,6)" _2 e. h, f+ [
  5. 2
    : \. E$ B* ?: R
  6. >>> math.gcd(40,20)
    ( ^, y. _1 o9 z/ o% ~7 E
  7. 20# t( o. R; W: m. g  o1 [# w
  8. >>> math.gcd(8,12)9 N) \1 l5 J$ `$ z0 Y
  9. 4
复制代码
; n: a, X+ F" W2 _
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
% [+ I, u0 i+ u4 G* t6 i
  1. #得到(x**2+y**2),平方的值
    1 `( v, Y8 t( l9 c
  2. hypot(x, y)  z9 t8 {0 F$ R$ u7 J4 Z& a- n
  3. Return the Euclidean distance, sqrt(x*x + y*y).6 u! o; e, N7 l1 h- q4 d  _
  4. >>> math.hypot(3,4)
    ) @- D  N* v: ^
  5. 5.09 R* i" c9 D/ X
  6. >>> math.hypot(6,8)6 a3 d  h' }% W$ d$ E
  7. 10.0
复制代码

7 o; e$ X2 h( n# Gmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
  c! X4 r) k' G0 b1 j2 @; z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % j3 ?6 h+ t% x8 Y) g! a
  2. isfinite(x) -> bool/ X+ v/ g4 r+ N9 ^4 ~% T& i
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.5 B! Z  g3 ]$ m$ V
  4. >>> math.isfinite(100)
    0 P: M. {) p, r/ b; S% C5 q
  5. True
    ; i4 i/ J, ]# R: T7 }
  6. >>> math.isfinite(0)
      p. z! ~+ g- _
  7. True
    ) ~* A3 V" s8 j% Z9 f
  8. >>> math.isfinite(0.1)
    / n) s+ h& s7 D1 h, n/ J" b" `
  9. True) ?( g& T" N, s$ h* E1 @/ x3 `
  10. >>> math.isfinite("a")0 L4 f" F3 H, P6 I% H
  11. >>> math.isfinite(0.0001)* Z; {% d0 \8 g, p1 E: [  @
  12. True
复制代码
, t( K* n  g' P' d
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
: Z6 S/ s! P$ _3 D# @  u% D! g! `
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    + y+ a4 r, k  F7 G" O! Q
  2. isinf(x) -> bool
    $ D$ e$ _. P* X. z
  3. Return True if x is a positive or negative infinity, and False otherwise.
    # \1 v" o! l! o: A5 ~% f
  4. >>> math.isinf(234)  z! {: |/ k2 M- v2 |3 x" P4 _3 e3 I
  5. False
    4 \# w$ p: g' Z/ o
  6. >>> math.isinf(0.1)
    # @8 g9 V9 n  B7 s) X" @( ~
  7. False
复制代码

; Q2 J! l9 B0 `9 smath.isnan(x)  如果x不是数字True,否则返回False, m3 }) i% |$ q. B
  1. #如果x不是数字True,否则返回False$ l1 v$ H1 A5 T, n
  2. isnan(x) -> bool
    4 z8 w7 L* r1 |. u7 a
  3. Return True if x is a NaN (not a number), and False otherwise.% g' k6 ?9 Z3 e, q5 s- R
  4. >>> math.isnan(23)
    % t1 l; S  r8 o/ c" w4 ]6 ^
  5. False
    : k2 Y/ Y- j, q' U& G
  6. >>> math.isnan(0.01)- j) k; w, G; w' ~, N
  7. False
复制代码

/ @5 g- l! e8 i1 a# v! K* emath.ldexp(x,i)  返回x*(2**i)的值6 [* ~5 a' V& `, d" @' l- h
  1. #返回x*(2**i)的值
    * R, _/ e: t. O9 R* i
  2. ldexp(x, i)) J& M; s) [8 x: w
  3. Return x * (2**i).6 `! G( p! d" l) |5 G8 v- @& R
  4. >>> math.ldexp(5,5)
    9 \2 e: F; D" z6 l
  5. 160.0
    $ V4 U" s" \9 C, I+ D7 J6 t* ~
  6. >>> math.ldexp(3,5)
    8 `6 L; l- _5 L7 T6 D
  7. 96.0
复制代码
0 U, H1 ~; z# M, o; E3 c3 P
math.log10(x)  返回x的以10为底的对数
+ W9 m" w* Z0 W
  1. #返回x的以10为底的对数
    5 S4 m# k0 W( b' b6 |; |: |
  2. log10(x)( z. G; l0 \4 Q, U  n. ?
  3. Return the base 10 logarithm of x.1 J' n5 K% ~' v5 o
  4. >>> math.log10(10)% P* v! r3 ]% p- U" A
  5. 1.0
    & E* X9 a. P5 ]3 A0 ]
  6. >>> math.log10(100)
    ' u+ w2 A7 }- I: j7 M  @
  7. 2.0
    # N" d5 r) n8 \
  8. #即10的1.3次方的结果为205 z" r& e; ^, G+ E
  9. >>> math.log10(20)4 m. @- I0 D. T4 M2 L% z" r0 h
  10. 1.3010299956639813
复制代码

% h0 s: K$ D# }math.log1p(x)  返回x+1的自然对数(基数为e)的值
" m8 a) m7 m9 |
  1. #返回x+1的自然对数(基数为e)的值7 M5 Z. P5 l' T( p3 l
  2. log1p(x)" n) A* t* y! p' S0 ]5 I
  3. Return the natural logarithm of 1+x (base e).2 b+ X5 m9 ?/ x: U! p
  4. The result is computed in a way which is accurate for x near zero.
    ; W7 h# K' r- m9 ?
  5. >>> math.log(10)9 H& k" e- S1 M
  6. 2.302585092994046
    3 T( y4 R' ~4 ~6 G) l$ y  r0 U, y
  7. >>> math.log1p(10)2 d, @9 ?7 m+ z8 G: r; |
  8. 2.3978952727983707! g( B# g" R3 G1 L
  9. >>> math.log(11)+ Q+ t0 Y6 B2 b7 F  B
  10. 2.3978952727983707
复制代码
, d; ?8 O  X6 o9 G  V5 X
math.log2(x)  返回x的基2对数
$ ]/ L" X* j7 n7 r
  1. #返回x的基2对数
    + o3 z. s$ n6 X2 a
  2. log2(x)8 C" i/ g) s* r
  3. Return the base 2 logarithm of x." b% P5 {" M6 X- ?& n. [4 o9 H
  4. >>> math.log2(32)
    2 ]# ^# E- w7 n2 j% j  \+ Q# u
  5. 5.0$ b% K5 ]" L3 {7 X# n' C" Q" y
  6. >>> math.log2(20)
    ) }; ~* z% t9 x7 Z" b9 @0 r
  7. 4.321928094887363$ f# B4 j. K  O! r$ R' ?# N
  8. >>> math.log2(16)
    # ~" S3 t' s) x6 l
  9. 4.0
复制代码

& a: K. r" v# G( \6 Kmath.modf(x)  返回由x的小数部分和整数部分组成的元组
6 C7 i" I: H0 J$ h0 x  k
  1. #返回由x的小数部分和整数部分组成的元组
    5 k! j) y' C& U/ t, ~
  2. modf(x)
    7 I* B2 ]% ^" u5 |5 O9 f
  3. Return the fractional and integer parts of x.  Both results carry the sign& A; h! ?! t" H2 V
  4. of x and are floats.. ~: ]% a8 g" K! D, W
  5. >>> math.modf(math.pi)& U+ G% ^1 g4 G9 X! K
  6. (0.14159265358979312, 3.0)
    . m+ d" j' x5 T9 G
  7. >>> math.modf(12.34)0 {6 D$ B8 K2 t2 _
  8. (0.33999999999999986, 12.0)
复制代码

; H5 M- s$ c( A9 ^6 B* |9 M9 Wmath.sqrt(x)  求x的平方根& d* `* `" o- T
  1. #求x的平方根
    * L7 i3 _5 X' M+ f# C
  2. sqrt(x). F3 [' E1 h) J5 @
  3. Return the square root of x.4 H4 B5 }! V6 X
  4. >>> math.sqrt(100)
    5 l: a, T5 y9 w4 h  w
  5. 10.0
    9 H0 o2 f& }- L; Q# ]+ K
  6. >>> math.sqrt(16)8 L$ z; M. V, \) ?
  7. 4.0
    8 I0 Y5 R$ D. o5 I
  8. >>> math.sqrt(20)
    7 ?( Z2 z, n: c. u- K" n
  9. 4.47213595499958
复制代码

- Q1 O1 h, t, V/ A6 a/ Lmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分1 J' j1 M4 ~+ x4 R: Y) h9 ]
  2. trunc(x:Real) -> Integral: L4 s: a' j8 w1 v7 r3 Q3 |& U& [. \
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    + w( _! s% m& r7 E1 w/ B1 X% C
  4. >>> math.trunc(6.789)
    6 d) x7 K+ U8 `# d
  5. 6
    ) v7 v$ _" l4 M( d( ~! J
  6. >>> math.trunc(math.pi)
    ; u; }+ L2 N. j
  7. 30 U5 [4 u9 o$ S) M, ^) S
  8. >>> math.trunc(2.567)
    / k( v9 D1 E3 L" `$ k0 U
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-17 15:41 , Processed in 0.077996 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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