新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
3 o% ^9 q2 w: f% o% ?0 _
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ @9 e/ H" U% Z# R$ G6 m4 i# o
8 P0 M/ C9 a! B8 v
方法1
% B- A" g0 x! p4 a- n4 Q+ ~
  1. >>> import math
    / p. l  B# u0 _' L- V7 c* f
  2. >>> math.sqrt(9)
    4 P* ?, j6 @* M6 J% g! c4 T
  3. 3.0
复制代码
方法2
! M. g3 x3 Q0 i; D; D+ E
  1. >>> from math import sqrt
    1 B  [2 V5 l- V8 N9 _2 M+ r
  2. >>> sqrt(9)
    . `# R; p$ u# T) f$ y2 T6 ~
  3. 3.0
复制代码

, X$ L$ F3 s  ~0 d

8 ~0 b' u2 V2 V. z
math.e  表示一个常量( w% h5 ?  W$ l; w/ F+ X
  1. #表示一个常量5 O7 n: s& `. ?1 T( A- s" `8 }
  2. >>> math.e
    : X: D0 q0 t3 m* v# o/ X3 i1 K/ Z
  3. 2.718281828459045
复制代码

) n  R, I% |% A* N0 d+ U. hmath.pi  
数字常量,圆周率

+ y9 _8 D( k7 E/ l6 r
  1. #数字常量,圆周率0 a. T2 W5 {. l3 }
  2. >>> print(math.pi)' a% C. j" ]0 a9 ]( x) y
  3. 3.141592653589793
复制代码
2 U2 [8 ^. `1 C
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
, G6 Q: J6 Q: ]- l' U$ ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    5 w7 {( A* o. i, U0 A! S6 K' _' r
  2. ceil(x)
    # _% J% y2 V) B
  3. Return the ceiling of x as an int.- m- w' ?. z  J8 P
  4. This is the smallest integral value >= x.
    2 N( R% w" Z; L9 b! _- }$ j% K# }

  5. & k0 x% i# o9 t5 z
  6. >>> math.ceil(4.01)
    % }+ b( T9 Y5 `( S
  7. 5
    : u" n5 Z6 }6 G/ d
  8. >>> math.ceil(4.99)$ N1 w' `2 v: Y/ ?
  9. 5
    " e3 o; I  n  c' D
  10. >>> math.ceil(-3.99)- f# J. P$ o. w. h
  11. -3- F  b0 a0 E8 P: w
  12. >>> math.ceil(-3.01)
    $ F" u8 r; M8 J) B# L& n) ?- G
  13. -3
复制代码
, P( W$ ~; h2 u; [  N$ J
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. j% k/ j! J8 X8 f5 Z' H
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    # u% _2 Z. e' Q. Z
  2. floor(x); _) i- X8 n* J% `" q4 P  p1 Z
  3. Return the floor of x as an int.
    ; C5 C+ {% d) R8 S
  4. This is the largest integral value <= x.- c' l8 S  I6 y; E! h7 Y& g
  5. >>> math.floor(4.1)& Z' r" S  ]1 y
  6. 4
    9 \* Q4 k4 H, r+ v- K* _
  7. >>> math.floor(4.999)
    , [4 s9 a7 v! x5 {4 q
  8. 4
    2 G/ b; o3 n* t3 h4 v7 s
  9. >>> math.floor(-4.999)2 H0 @; c# L, e! ]
  10. -50 s5 C) B; s9 }" h0 ~
  11. >>> math.floor(-4.01); v  M8 J1 H" c1 j. i; n+ k
  12. -5
复制代码

* G: r7 R* {/ X; _math.pow(x,y)  返回x的y次方,即x**y/ ?7 k- ~1 I! ^, f7 k! F) g- k4 f
  1. #返回x的y次方,即x**y9 X3 B9 f6 }* i2 i& g; p0 l
  2. pow(x, y)
    + {- N" S/ a* i& [: J! S
  3. Return x**y (x to the power of y).
    7 B' i! ]" G) v/ f9 O9 X
  4. >>> math.pow(3,4)/ A/ V( F& f# K9 y7 T" H
  5. 81.0: g2 K7 P6 G8 h; U( m  B
  6. >>>
    ) T  f, h3 q2 r! m- `
  7. >>> math.pow(2,7)
    ! H2 u( a# P) Y) O% H- ?
  8. 128.0
复制代码
2 a' f7 P7 n" G  @
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 {* x- j( n$ ]- ?9 Z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), \7 u+ m/ t4 E
  2. log(x[, base])
    ; h! m8 u3 x& [
  3. Return the logarithm of x to the given base.5 X! ?: [; S3 b0 O4 M' _* w) x
  4. If the base not specified, returns the natural logarithm (base e) of x." \/ d  i: S' b$ L
  5. >>> math.log(10)! f: i8 T2 n6 O
  6. 2.302585092994046
    4 i. I1 D! e# \
  7. >>> math.log(11)) {" \4 F& H+ ~& g5 i
  8. 2.3978952727983707' |/ E3 f# t+ a" }+ ]2 }
  9. >>> math.log(20)
    , }) T& ^1 T& w# W# j
  10. 2.995732273553991
复制代码

# p8 {$ `% x/ i4 b6 D6 r+ x* R: Smath.sin(x)  求x(x为弧度)的正弦值( P% ~" e) a5 v. B% }+ B5 I3 H
  1. #求x(x为弧度)的正弦值" \! A. i5 P' ]6 G, `$ C
  2. sin(x), O. g0 L- g1 I- W
  3. Return the sine of x (measured in radians).
    % j" T0 g( i9 f4 q7 N- M+ l6 e
  4. >>> math.sin(math.pi/4)1 q* h7 @0 x6 O; ^# b7 }
  5. 0.7071067811865475
    0 v0 ^; x% G" R
  6. >>> math.sin(math.pi/2)1 i) h: h0 c" ?0 n# ]9 }
  7. 1.0" |  g# S5 a5 M' d8 ?
  8. >>> math.sin(math.pi/3)  n2 m( A4 x4 E, u
  9. 0.8660254037844386
复制代码

+ y" \/ ?* k  K# P' wmath.cos(x)  求x的余弦,x必须是弧度
; C! a$ H% \3 K. {+ l
  1. #求x的余弦,x必须是弧度
    - n8 x) f% [0 y: a2 w
  2. cos(x)
    5 N' W% ?) d: s; J6 c5 f& q2 s/ O
  3. Return the cosine of x (measured in radians).4 @) ^6 q& ?% \) M4 O
  4. #math.pi/4表示弧度,转换成角度为45度" K: B+ f: J0 ?" u
  5. >>> math.cos(math.pi/4)
    " G, }; M4 k$ t- z
  6. 0.7071067811865476
    0 p, v4 h( h8 ?- z% V/ E
  7. math.pi/3表示弧度,转换成角度为60度
    " q6 V: N: K- r
  8. >>> math.cos(math.pi/3)
    . ~+ ^4 p5 H! A2 L- |5 Z
  9. 0.5000000000000001
    : v6 R6 _- y0 x# a8 C4 ?! O
  10. math.pi/6表示弧度,转换成角度为30度7 M: R3 x/ W/ k
  11. >>> math.cos(math.pi/6). k* ?& B9 I- Y- \2 b9 |
  12. 0.8660254037844387
复制代码

& f/ d& V* |( _math.tan(x)  返回x(x为弧度)的正切值
1 O3 Y, a+ p+ Z8 V# F
  1. #返回x(x为弧度)的正切值
    4 |# Q, D: G0 ?
  2. tan(x)3 [5 s( }( ?) ?: w+ F; e
  3. Return the tangent of x (measured in radians).& |, C9 A7 M# F* m- O' K( V
  4. >>> math.tan(math.pi/4)" \1 y5 f6 d' l+ L, Z; i% O8 ^
  5. 0.99999999999999991 P" x' Z+ D' v9 n( Y* @. m+ {
  6. >>> math.tan(math.pi/6), l* M* V' s/ {1 E. W8 l
  7. 0.5773502691896257. v9 Y( B) k/ b* z: @
  8. >>> math.tan(math.pi/3)4 M$ ?6 ]3 `5 e" H3 p) b* j
  9. 1.7320508075688767
复制代码

, {! a( e& v  C, K9 e0 j3 _: Vmath.degrees(x)  把x从弧度转换成角度
1 G) s. |1 ~# P1 d+ U
  1. #把x从弧度转换成角度# R$ _# o7 d! D* |2 E0 N, b% o
  2. degrees(x)# A& s4 w7 f* D; Y" J
  3. Convert angle x from radians to degrees.
    ) V) q& @- U. g9 N/ `, H, _# s

  4. + w% h! F% H" ?
  5. >>> math.degrees(math.pi/4)+ [4 s% s  \# Z) p0 d
  6. 45.0+ k7 u! F# A# @& v5 z* h8 E
  7. >>> math.degrees(math.pi), i4 A; ?! ]( u) g
  8. 180.0
    , ^' i) D8 R8 O  S( \
  9. >>> math.degrees(math.pi/6)5 C5 c1 [: ~4 j4 L6 N8 M
  10. 29.999999999999996/ y) |/ D( L( y( q
  11. >>> math.degrees(math.pi/3)
    9 v) o* J" T8 D: Y
  12. 59.99999999999999
复制代码
% v: ~" _. j0 N
math.radians(x)  把角度x转换成弧度, l: L. ~' Z7 x/ l8 @: Q" ~; D
  1. #把角度x转换成弧度
    ; l) R9 ~+ [* l' Z" H0 x) Y" [
  2. radians(x)& G0 |) z& ~" \
  3. Convert angle x from degrees to radians.+ B* X8 z2 e4 e/ ^' v$ ^; w  b
  4. >>> math.radians(45)( y: S4 @' m8 `
  5. 0.78539816339744835 V0 c4 B4 c- W' z9 K
  6. >>> math.radians(60)2 d! z% e" A2 O, R3 Z3 h
  7. 1.0471975511965976
复制代码
; Q0 L$ g: E* z4 h: i
math.copysign(x,y)  把y的正负号加到x前面,可以使用0: O5 H, t) {0 T/ w; n! n  A
  1. #把y的正负号加到x前面,可以使用0, [& K# J7 L* b0 h( l6 f
  2. copysign(x, y)$ m- q! o& f7 u
  3. Return a float with the magnitude (absolute value) of x but the sign
    7 @( O+ A4 o5 L4 w9 e4 F! L
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    / R' G% j' U! \2 t8 a
  5. returns -1.0., x* R' U6 ?9 |" ^
  6. 3 c3 |! M( _8 v( H/ B
  7. >>> math.copysign(2,3)# a# H& |5 G$ B$ X. C
  8. 2.0% j6 D' z6 J6 r% a+ x! t9 m: K! \
  9. >>> math.copysign(2,-3): J: E8 ?( S% U) g% U4 G  b
  10. -2.0  v2 y+ p' R) k! b3 ^! z* _
  11. >>> math.copysign(3,8)
    + Q6 n4 {- i0 m+ D1 @1 A! ~  s
  12. 3.0
    ! a$ j/ F! s/ n- A) ]: w7 e3 B# d% ?3 c9 B
  13. >>> math.copysign(3,-8)9 q1 M0 v9 Q; @* p* A3 c. ~
  14. -3.0
复制代码

" G" y; w) E8 J' B1 Kmath.exp(x)  返回math.e,也就是2.71828的x次方9 v. n* O" [/ d) m5 W3 f& m( B' _/ m
  1. #返回math.e,也就是2.71828的x次方
    ( n7 Y' z) b2 o* U: Y  a* U! B3 V6 m
  2. exp(x)/ }# f4 |' d5 A2 |8 O4 [
  3. Return e raised to the power of x.7 \' ?3 s' m3 |  F

  4. / V5 D0 k, A7 l8 B, l0 v1 V
  5. >>> math.exp(1)
    ; l) j4 ?. h: c( T- m
  6. 2.718281828459045) }! i1 Z) P" s; C0 ]/ _
  7. >>> math.exp(2)0 e5 @5 L) K) F5 P4 v
  8. 7.38905609893065
    3 _, c+ }9 e; F. M/ W9 ^1 D
  9. >>> math.exp(3)" q7 a8 L: f* v
  10. 20.085536923187668
复制代码

! i! z& g9 M6 d5 q# H6 imath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减11 b# _) K% e0 g( E" L) l7 k( Q
  1. #返回math.e的x(其值为2.71828)次方的值减12 l  F# ^8 A& F7 q0 r
  2. expm1(x)1 H. v/ X! Z1 H
  3. Return exp(x)-1.& _2 j8 L* Y9 W' m( F5 a
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    . {* g: `3 t& M& F$ B0 X
  5. . ?1 [; M# S3 w; S7 Q9 a) {+ R6 @
  6. >>> math.expm1(1)
    3 j& e0 h& |1 D2 [0 o; j$ l1 p7 L" Y
  7. 1.7182818284590450 U1 y2 Z. {9 @4 V
  8. >>> math.expm1(2)
    7 i" Q3 c' X' C. C9 E
  9. 6.38905609893065
    # `0 x, g. M) O4 n7 D1 g
  10. >>> math.expm1(3)
    % i# W4 `2 f* \' B: y- S+ W
  11. 19.085536923187668
复制代码

, v/ s; v) Z6 `math.fabs(x)  返回x的绝对值
$ @. O+ f" ?4 Z! b7 k, r, Y6 c
  1. #返回x的绝对值8 O8 x0 ~+ ~- y9 }6 v- G( E7 ^) Y
  2. fabs(x)
    : I8 D$ N9 C/ V( ?. r( d4 T9 |/ r: m4 M
  3. Return the absolute value of the float x.
    - t& ~$ ~/ v/ V8 ~% |: q+ H# d

  4.   W. u$ O6 u: e, z7 C. y
  5. >>> math.fabs(-0.003)7 E: @  F# {; ~- R# L* C& r
  6. 0.003' G9 P9 G- R% y* |1 o
  7. >>> math.fabs(-110)
    2 {0 D" I; q6 i; }& D8 I
  8. 110.0
    % l) ~: T9 |$ K+ S1 N
  9. >>> math.fabs(100)# W" I6 _2 a( j
  10. 100.0
复制代码

3 J. g, c! O# a# A, @math.factorial(x)  取x的阶乘的值8 P: O8 O, ~& x
  1. #取x的阶乘的值
    5 T; u- ]$ h& b
  2. factorial(x) -> Integral6 b0 K# i- d5 ]
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    6 ~0 J% O5 T* E* Q8 f, N
  4. >>> math.factorial(1); G1 p9 C7 S5 u6 f7 @7 E; B
  5. 17 R3 T& [7 r4 V% M5 U) f+ I+ t# N
  6. >>> math.factorial(2)4 h  O9 A; j9 }$ ^. n
  7. 2
    " e) i& _' a" y
  8. >>> math.factorial(3)
    9 |& }5 i( h5 v& O; c
  9. 6
    $ a% W9 X: L' o# g1 r% `. l" u
  10. >>> math.factorial(5). s! d  I: x! G& V$ n, U4 F
  11. 120# g, L& d% W5 S( V% V; r* j! E! d
  12. >>> math.factorial(10)
    8 E4 Q2 n( I2 I
  13. 3628800
复制代码
2 V" Q4 y* w! w) E9 H4 J/ t
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
: e/ ^. O; @( K" e( p: u' R, f
  1. #得到x/y的余数,其值是一个浮点数
    9 a5 _) R: h: H9 x
  2. fmod(x, y)8 n- ?! @: l* ~- q
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    # U3 |/ w' q" N0 X' H
  4. >>> math.fmod(20,3)
    + Q# Z. Z5 A1 b. O$ W, J0 p
  5. 2.05 `1 A5 m( l5 v7 l$ f
  6. >>> math.fmod(20,7)
    $ ^! P% I0 G) B- Z, H: p9 G
  7. 6.0
复制代码
6 P+ y: v: L, c
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围; [! Q! C) s- M6 G$ N- T
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ r6 b6 j+ v3 C
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值; @" c9 @5 r9 b6 W& r9 Q, G; [! z
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    1 X' v  i2 u" E& d
  4. frexp(x)$ h7 z+ S9 |/ g, f
  5. Return the mantissa and exponent of x, as pair (m, e).
    " K' Y& p9 l; p
  6. m is a float and e is an int, such that x = m * 2.**e.
    $ p' J4 L+ m" J, Y# ]; A
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.( @' G3 J, S4 Y( ^
  8. >>> math.frexp(10)& a+ }& ~, g/ n, k6 N8 M
  9. (0.625, 4)
    # R0 D5 J5 c8 D' q) L3 T
  10. >>> math.frexp(75)
    % G) [9 |+ w$ @  l2 M
  11. (0.5859375, 7)! L/ n% ?7 p' f4 V3 u
  12. >>> math.frexp(-40)
    3 q( r, H& b5 F) m0 j
  13. (-0.625, 6)
    ( h( E$ A; k& L6 E" b
  14. >>> math.frexp(-100)+ ], J" a5 `- L0 `- X8 g
  15. (-0.78125, 7)
    6 |1 A3 [, J' N, A7 r. X
  16. >>> math.frexp(100)
    9 N" S4 I, n! U# |
  17. (0.78125, 7)
复制代码
5 }+ w, }& z; J7 F6 i
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列" r0 m2 r) O0 k1 L4 b2 d: N9 x
  1. #对迭代器里的每个元素进行求和操作8 s# [) H% e% f/ B2 a* M) b5 P
  2. fsum(iterable)" k% t- j% Y" l# e* N1 `$ O! h
  3. Return an accurate floating point sum of values in the iterable.0 u. v  J9 N  l5 ~- \: [2 G7 `* Q
  4. Assumes IEEE-754 floating point arithmetic.
    - D# o( x* ?. W4 {/ s' u
  5. >>> math.fsum([1,2,3,4])( _+ f1 N8 R5 C# ~: i
  6. 10.0- X; K8 b# Q, O, T
  7. >>> math.fsum((1,2,3,4)): X' ~# f- C; c- @& |  i$ a
  8. 10.0% Z8 n8 D, m. H# E: R
  9. >>> math.fsum((-1,-2,-3,-4)). a6 q6 i9 r1 X
  10. -10.0
    0 C% H8 g. l( f5 z9 x' n% U8 q
  11. >>> math.fsum([-1,-2,-3,-4])6 @- }  a0 w, d3 z4 A
  12. -10.0
复制代码
8 f% p# `; k4 p# s) i
math.gcd(x,y)  返回x和y的最大公约数: ]8 c. j/ A6 }4 c- V8 z" r- z- T
  1. #返回x和y的最大公约数
    , H7 ?0 b2 v0 q3 x( \
  2. gcd(x, y) -> int
    6 Y6 K1 f9 K% [( e: t2 i
  3. greatest common divisor of x and y
    0 p  @( l8 [) X& v6 s" x  \2 ?# X
  4. >>> math.gcd(8,6)& J) D' |1 X: B: G$ |  ?
  5. 23 ~) j3 Z7 ~8 e6 |: q2 g' t
  6. >>> math.gcd(40,20)3 {( O& {% Q8 C+ x" o" ]
  7. 202 @: T1 w8 u/ s" r) o" b- ~; Z
  8. >>> math.gcd(8,12)
    # b3 \7 h2 `7 Y! r8 b
  9. 4
复制代码

7 I1 n3 Y- @. Q, smath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False( \) s% `6 s. m4 g1 h. y" g6 |
  1. #得到(x**2+y**2),平方的值5 S5 I+ e* B5 A0 i
  2. hypot(x, y)8 ?& w: [6 J4 `
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    , G6 Y: B6 R' q$ x8 ?1 u
  4. >>> math.hypot(3,4)' t3 L! X' r2 P2 J
  5. 5.0
    1 C7 d3 i6 U/ C" W, U1 a/ }
  6. >>> math.hypot(6,8)
    ; ^4 |. Z, m8 j
  7. 10.0
复制代码
( A" B) Q3 |( T- V& U
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
: ^: p3 m  b. h: u9 n' \* z7 \
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    " j0 a# T6 B) R( J& A4 ^
  2. isfinite(x) -> bool
      k; g, V1 X3 f! n0 D
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
      B6 B4 d4 |$ f/ ]  r
  4. >>> math.isfinite(100)
    2 f. }- h3 Q, P( D
  5. True
    4 y% {9 f% X2 g; ~# ]
  6. >>> math.isfinite(0)
    7 G; p# d1 Y2 Z% E- r/ w( ]) P
  7. True8 M( }7 v- d' b7 d4 ?" m# @
  8. >>> math.isfinite(0.1)/ X3 |4 L8 e: ]; y3 s# y6 i
  9. True
    * u3 k. e8 a$ r, P
  10. >>> math.isfinite("a")
    $ s1 Y, F6 U& |0 D. i2 A
  11. >>> math.isfinite(0.0001): R6 a6 ^5 H" B4 z! n7 H5 ]
  12. True
复制代码

5 C" ?" m  i- y1 V0 |$ Fmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False- |8 r% Z& [# ~" v
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False: p, ]" W$ J4 b/ h2 I1 j7 ?' K. R6 q
  2. isinf(x) -> bool
    . k2 K  r3 [8 Y6 c" e( T
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ; }3 }+ _  M% t2 C
  4. >>> math.isinf(234)" u$ R; Z3 M3 b" _' G4 D/ X7 M9 y
  5. False( p$ X( j* Q- f% j# T
  6. >>> math.isinf(0.1)
    - M; ~4 D5 Y, x3 z: S# r
  7. False
复制代码
2 s( t  \: |3 E2 _4 J3 I
math.isnan(x)  如果x不是数字True,否则返回False
, o9 k" s- t, I# `4 U$ V% o% u# d
  1. #如果x不是数字True,否则返回False
    8 O* b, v7 e( I6 W9 i7 O
  2. isnan(x) -> bool5 G- e- l' M4 @1 w. v
  3. Return True if x is a NaN (not a number), and False otherwise." O1 l6 B5 o3 Z* p0 v" ^3 w' Y
  4. >>> math.isnan(23)3 U, |, _3 Y$ \5 V
  5. False
    " V, P8 [+ s8 P" A2 }3 q+ l6 a+ E
  6. >>> math.isnan(0.01)* q' b% r- `& c  n+ l/ ^
  7. False
复制代码

/ g- M' e8 N' m3 I/ l0 fmath.ldexp(x,i)  返回x*(2**i)的值
+ c7 `% v$ u$ S2 x
  1. #返回x*(2**i)的值
    ; k1 C, T  b0 t4 Z3 o
  2. ldexp(x, i)* C, E# R) C6 T
  3. Return x * (2**i).
    & o* f3 z* N) ~( W  N' b
  4. >>> math.ldexp(5,5)
    % i# r% |+ P& |' Q% u0 ?
  5. 160.0
      ]/ J( T# z' n
  6. >>> math.ldexp(3,5), M$ P) h2 f4 G! d5 a
  7. 96.0
复制代码

) j; f. E& b; w' `- Qmath.log10(x)  返回x的以10为底的对数8 t% Q9 j9 f( @& W. x- o  X
  1. #返回x的以10为底的对数/ O7 M  y( E+ m2 D+ H0 Q! y
  2. log10(x)
    - z6 _* t5 i! m1 X$ V
  3. Return the base 10 logarithm of x.0 |4 t3 g7 b5 q) O2 s; @
  4. >>> math.log10(10)  _0 M* @8 G! |% t( \
  5. 1.0
    ! ^1 u, A; {( q) ]1 C. ^
  6. >>> math.log10(100)
    8 \. [, r5 F4 h7 o; z
  7. 2.0
    ( M) _/ _+ i! G( F
  8. #即10的1.3次方的结果为20! q* [0 x; y, y# }5 b/ @6 l/ b
  9. >>> math.log10(20)+ ^1 g+ \# M5 X7 `: S
  10. 1.3010299956639813
复制代码

1 a5 t7 o% o/ O# c# Xmath.log1p(x)  返回x+1的自然对数(基数为e)的值& q# m/ k: a" ~6 U) m+ `4 ]
  1. #返回x+1的自然对数(基数为e)的值
    - v+ y6 `2 }6 d% X' d6 |6 N7 \6 p
  2. log1p(x)
    & P" e6 q$ M, R& F* O
  3. Return the natural logarithm of 1+x (base e).
    - G: t3 J. x9 O; e! ]! T
  4. The result is computed in a way which is accurate for x near zero.
    ' d5 g  U9 w" {) b; l
  5. >>> math.log(10)3 K  K+ U0 v% p. a6 X9 b6 P, T
  6. 2.3025850929940465 `& u4 j2 ?1 T# z/ F% B( q! X
  7. >>> math.log1p(10)2 ]* F2 e. v! Y- |- u2 p
  8. 2.3978952727983707& \) g  I( y+ _$ [+ U. w
  9. >>> math.log(11)
    ! c! U- J  o6 ?& X* a8 N
  10. 2.3978952727983707
复制代码
6 u0 k: k* V2 K8 k$ S
math.log2(x)  返回x的基2对数- ^! A6 M9 q: ]* n" S. ~! i
  1. #返回x的基2对数+ e; W$ n. R( L& ^$ d: ], q4 D5 v
  2. log2(x)
    $ {( z; c: e/ g7 a! ^5 G! X2 ^8 a7 \
  3. Return the base 2 logarithm of x.
    : H% B1 n' J& Y  E  m3 V0 [
  4. >>> math.log2(32)
    1 a4 _. R7 b, J' `. }! Q% |
  5. 5.0
    " I4 G1 v9 Z$ f) [- o" O2 [9 y0 Q
  6. >>> math.log2(20)
    6 E9 P- o5 R% B3 J- e! E
  7. 4.321928094887363
    ' W! f' ]. n# u$ Y
  8. >>> math.log2(16): Y* s' c, o. x) i+ K4 ^
  9. 4.0
复制代码
' E% P! W+ q: q# M* u
math.modf(x)  返回由x的小数部分和整数部分组成的元组. j4 c& {& `# a3 T1 X5 K; F( T
  1. #返回由x的小数部分和整数部分组成的元组
    4 F/ I3 Y( y9 `0 Z' c. x+ ?5 A
  2. modf(x)
    7 ]  v% j( a, s! t
  3. Return the fractional and integer parts of x.  Both results carry the sign
    - `2 ?8 ~3 k. j) @( h, \5 n' e
  4. of x and are floats., ]# K  U* c; d* i4 Y2 g% q7 Q
  5. >>> math.modf(math.pi)) l4 g- c3 U7 K9 N/ c) i
  6. (0.14159265358979312, 3.0)" l) a, j4 K: G5 l7 e- k& L" H  w
  7. >>> math.modf(12.34)) C) M% s* f- U# T. n' Z5 {) i
  8. (0.33999999999999986, 12.0)
复制代码
# a: o* U7 O5 D: m% T- t
math.sqrt(x)  求x的平方根$ ]$ ?+ ~9 \9 x
  1. #求x的平方根/ }' h/ z, k0 ?" @( G$ Y! l- z
  2. sqrt(x)
      n, ]" y- ^& g, {) {3 u
  3. Return the square root of x.
    1 Z- K% o9 m" p8 b1 k
  4. >>> math.sqrt(100)
      N% h) b9 [, ^8 b, F4 J7 ?
  5. 10.0/ Q' T* z/ O* K
  6. >>> math.sqrt(16)
    1 x" f% i7 H  B2 U: d( l
  7. 4.02 K, ?0 @( f& _9 l! L) Y2 m
  8. >>> math.sqrt(20)  m* D' u* x9 ]0 Z
  9. 4.47213595499958
复制代码
1 F" a2 B: R' I' Z
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; Q2 @/ w4 e! b/ ~' w; f
  2. trunc(x:Real) -> Integral
    - h: n" g2 q' o; K3 `
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.: L' n7 [- O/ s8 C$ r
  4. >>> math.trunc(6.789), r5 u7 s( `( `3 [. d
  5. 6
    # T. Q! W' {" ]2 X% j6 q7 v; U: [
  6. >>> math.trunc(math.pi)
    * U0 [* X1 X" Y" O7 r& A
  7. 3
    6 a7 x3 a2 P$ w1 D8 k" F# [
  8. >>> math.trunc(2.567)
    - H+ \' X+ ?' _
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-5 07:52 , Processed in 0.077951 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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