新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

! {+ W3 ^& }8 ~3 E4 [【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" _7 X: u. k8 \# p3 w4 m8 L
9 ]* [. N* B/ @' z9 C4 g  W
方法1
1 c3 E0 o: P/ N5 e
  1. >>> import math
    % |( q7 e, T8 f
  2. >>> math.sqrt(9)7 L4 \% T, _4 f8 s
  3. 3.0
复制代码
方法2
/ ^, P* @7 p* v% _1 m
  1. >>> from math import sqrt
    ! m& k5 v8 y9 x7 H- u
  2. >>> sqrt(9)+ l8 x; ~4 p; y( P+ H" h- d
  3. 3.0
复制代码

3 d9 z- q5 n* h8 Q& q; b% ]  O

5 ]" ^$ \9 M8 I% x! f: K0 z
math.e  表示一个常量
2 Z. w! K5 p4 O* z% [6 }
  1. #表示一个常量+ o7 x- w, Q% }7 x- p% k
  2. >>> math.e
    7 i0 k: o" \$ j2 `6 M# U& [! H
  3. 2.718281828459045
复制代码
! R# r5 S. o% M$ a/ ^' s/ Q( t2 C) v" X
math.pi  
数字常量,圆周率
" M) p/ y9 r, \+ D$ R' p' H4 W
  1. #数字常量,圆周率
    & [7 |1 ]0 y, I+ a
  2. >>> print(math.pi)
    " m1 O8 s( o: ]% z) E7 W0 y  y
  3. 3.141592653589793
复制代码
, a0 O, V  w: J+ G+ K
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
% p1 o$ F$ D4 `- B% x$ l3 _$ A1 d+ M8 G
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 H6 ]& q1 R$ P* N5 Q
  2. ceil(x)
    8 ~( V5 v/ _3 `' r9 C
  3. Return the ceiling of x as an int.
    % w! x/ [( [. E* F, x
  4. This is the smallest integral value >= x.
    " H: o" {5 e! }& @
  5. & \- c: V$ |% `8 @
  6. >>> math.ceil(4.01)$ W4 N. U% L  K2 I1 A8 W; T
  7. 5
      T$ S* R; r  u
  8. >>> math.ceil(4.99)
    - {2 n$ [3 h5 F) M
  9. 5
    9 @: b- s# b8 Z9 ~* d* _! L
  10. >>> math.ceil(-3.99)9 b4 t. D( G; E% ^5 J
  11. -36 T) L4 U3 h4 [7 \; a5 {
  12. >>> math.ceil(-3.01)
    1 I$ H! }' J( ^- E! |
  13. -3
复制代码

# O; s: O' A/ B3 q: kmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 X) ~. {0 ?1 ]7 M  v
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    # u/ }2 B7 J% G9 ^$ b
  2. floor(x)$ `3 x+ o% P7 W9 w2 M4 Z
  3. Return the floor of x as an int.
    & e  \" p) r) `2 Y1 \) Q
  4. This is the largest integral value <= x.' s. L8 ~; J  V! I/ C4 h5 Y1 |: z
  5. >>> math.floor(4.1)6 j( r/ ], v& n0 M, |9 q/ d* u$ y
  6. 4
    6 {7 O/ l6 ~( M# L6 }% F1 _
  7. >>> math.floor(4.999)9 c. q% Q# J% L, n" G
  8. 4) L6 W! Y- k; T5 z" W
  9. >>> math.floor(-4.999)
    ; D, T% k! }5 n! _. v2 b2 Z8 V* ~
  10. -5# H; g0 b) L$ q; R; p' {
  11. >>> math.floor(-4.01)9 d( j3 e2 ~& k. Y
  12. -5
复制代码
: n4 Z' I6 H. j" h
math.pow(x,y)  返回x的y次方,即x**y* K9 |* V7 F9 P: f( ~1 ^9 Z9 Z
  1. #返回x的y次方,即x**y
    0 B& Z5 M% t' ?# g5 f
  2. pow(x, y): o: V. Y" B0 B) R5 H* x
  3. Return x**y (x to the power of y).
    & Z# U- N& v1 Z: j/ M* W
  4. >>> math.pow(3,4)
    . \& r& u" Z9 ]- i! Y% L
  5. 81.04 h# z: u) R1 t
  6. >>>
    / K% X; z! {7 ]3 S6 f
  7. >>> math.pow(2,7)8 a* L! @5 H7 z% G' B0 ?5 l
  8. 128.0
复制代码
  i' i' ~! j: ]( }- ?
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' a7 s3 W" ^4 }/ U5 f- ?9 @' _
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    $ S* W  B. e$ W, j0 [# Q: B
  2. log(x[, base])+ f* f* k9 j8 x- f$ M7 S; f
  3. Return the logarithm of x to the given base.4 a6 l' P6 G, {/ o9 @( i/ F0 I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 v8 {! @  z: d, p6 j( f3 I5 }
  5. >>> math.log(10)
    9 `# E% ?6 [+ E7 s3 ~  I
  6. 2.302585092994046
    1 r5 u& C( h3 F. c# U( q+ L0 Q: v
  7. >>> math.log(11)% K( Z7 ]7 @% C  r8 f/ o
  8. 2.3978952727983707
    : {! e8 w- I/ b) _- @# ^3 g
  9. >>> math.log(20)0 x; C& s* @3 {8 ^
  10. 2.995732273553991
复制代码

& H6 W; o  X8 [& ?8 lmath.sin(x)  求x(x为弧度)的正弦值
% J3 N. I" L6 }
  1. #求x(x为弧度)的正弦值* C; ~2 c( |$ L; W0 ?9 k  i# m
  2. sin(x)2 U4 W1 d3 B* f) {; D5 [) d
  3. Return the sine of x (measured in radians).
    , s' d( D" y2 m  ^* B
  4. >>> math.sin(math.pi/4)! w, p) Y4 _, p, U. Z; u
  5. 0.7071067811865475
    7 _3 R& O8 H+ |
  6. >>> math.sin(math.pi/2)
    1 h$ x# u( m. @- P8 [6 o0 |( [
  7. 1.04 d5 s! Z4 F" v: y
  8. >>> math.sin(math.pi/3)& ?- q$ J' L0 g: [. P
  9. 0.8660254037844386
复制代码
4 s5 L; u0 z2 e
math.cos(x)  求x的余弦,x必须是弧度3 o; K$ V3 ?% n! `! ], t
  1. #求x的余弦,x必须是弧度
    ( o1 B/ D% v# o; a
  2. cos(x)
    : }* [! z  r+ X1 B0 z  t
  3. Return the cosine of x (measured in radians).3 I/ u9 u5 x4 Q; j9 R
  4. #math.pi/4表示弧度,转换成角度为45度
    8 t3 O3 Z- a4 k  j; q# F8 A# D& k
  5. >>> math.cos(math.pi/4)
    % f: x; O4 a% z( q$ B
  6. 0.70710678118654764 H- m3 e- S2 G/ G
  7. math.pi/3表示弧度,转换成角度为60度
    5 Z) w' @6 H* a9 ~$ Q- O
  8. >>> math.cos(math.pi/3)# r8 M% k2 \9 X% V  j
  9. 0.5000000000000001# J" [, F. w5 g' |5 ]$ ]
  10. math.pi/6表示弧度,转换成角度为30度
    8 P0 L( W# ~( N( d7 m1 {  _
  11. >>> math.cos(math.pi/6)
    ( }3 e# V* R% d/ ~8 }
  12. 0.8660254037844387
复制代码
# X2 P: \3 O+ P9 w! Y1 ^
math.tan(x)  返回x(x为弧度)的正切值  h$ l/ Q" I7 Y2 ^
  1. #返回x(x为弧度)的正切值/ X% z& N' l$ ~! m
  2. tan(x)
    ( i7 w/ I  g: B& v4 u2 i
  3. Return the tangent of x (measured in radians).
    4 l2 ]0 ]8 |  c8 k' R' V0 y
  4. >>> math.tan(math.pi/4)# V5 Z" c1 ]* h" B/ o; ?
  5. 0.9999999999999999
    8 Z8 A2 M2 k$ u+ C3 h( c0 H0 C
  6. >>> math.tan(math.pi/6)
    : n' g' q9 O" B- D
  7. 0.5773502691896257" S, F) C/ E5 X0 x  m
  8. >>> math.tan(math.pi/3)
    6 R- a. g3 H+ z
  9. 1.7320508075688767
复制代码
7 q1 q9 k# s7 Q, v. H( X7 b& c
math.degrees(x)  把x从弧度转换成角度
& v% F; w2 I& G+ [( F! m0 U! \; ^
  1. #把x从弧度转换成角度
    ; c' d% C) P- ~8 \  F+ t# L8 i
  2. degrees(x)
    + S( u' l; r; ]0 ^4 u1 b
  3. Convert angle x from radians to degrees.
    $ ?* M# f, t5 y6 {1 U, j
  4. & \9 A: C( u* t5 {. ~) E
  5. >>> math.degrees(math.pi/4)6 v" w- C& L! L) G8 r3 a: z
  6. 45.08 |; k8 K+ u" D; G2 U$ V- f$ g) M
  7. >>> math.degrees(math.pi)
    ( f2 a$ [! K  r& y- E3 f7 |9 ?
  8. 180.0
    " j) A# a  X+ ]
  9. >>> math.degrees(math.pi/6)
    : y1 |0 v2 X6 i0 F
  10. 29.999999999999996  I* n$ m' G; n% [/ ~* {" \
  11. >>> math.degrees(math.pi/3)* D* C. q- Z  S
  12. 59.99999999999999
复制代码

% q- q) n0 C. @4 j; bmath.radians(x)  把角度x转换成弧度
1 a* b' d# q  f) [( Y% ?* L; J
  1. #把角度x转换成弧度9 w7 K( b5 x& q6 f* L
  2. radians(x). u( o. P% y4 f2 N9 @& f7 w4 T
  3. Convert angle x from degrees to radians.
    0 P6 k8 q8 D2 B# [  d; O. `$ }- g
  4. >>> math.radians(45)
    1 d# k+ f9 ?8 e9 s
  5. 0.7853981633974483
    . M. Z5 k6 j- e$ x2 `& P4 T
  6. >>> math.radians(60)
    2 X* a2 [! ?+ ^# o
  7. 1.0471975511965976
复制代码

$ f7 _6 m9 q8 O1 tmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
  l/ f1 L; V! ]5 t2 z
  1. #把y的正负号加到x前面,可以使用08 W& i) o$ D" g9 T
  2. copysign(x, y)' ]* I! Q$ K5 A  d% O' Q# N
  3. Return a float with the magnitude (absolute value) of x but the sign
    $ E* O/ x, N9 r" _+ {& T% E
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    / S2 @+ p- o5 ]% |7 [
  5. returns -1.0.
    5 z& B  A( P" N& q( Z
  6. 5 \# A# |# Y; Q% f
  7. >>> math.copysign(2,3)
    7 b; T4 i& F* t5 r
  8. 2.06 t4 E3 z" l& {* q: r, C
  9. >>> math.copysign(2,-3)' Y& W# x9 {+ E" o" ]  g
  10. -2.0
    % A  ]$ c9 B. r+ O5 x
  11. >>> math.copysign(3,8)2 A0 w4 t' m$ m& I( A+ u
  12. 3.0
    : T. B8 |+ X  _) J) F$ z- ^+ X
  13. >>> math.copysign(3,-8)! l; o  F+ B1 i; v& ^, d
  14. -3.0
复制代码

: u4 n1 L4 @: f% q; a' r* Vmath.exp(x)  返回math.e,也就是2.71828的x次方
+ \: o+ U% T- F+ P; K4 F0 ~) k3 Z
  1. #返回math.e,也就是2.71828的x次方
    % u# n% `7 d& W3 Y
  2. exp(x)" \; w7 F: \$ {: s4 b
  3. Return e raised to the power of x.
    + n5 y: U% }8 |3 P/ m1 U* b7 t
  4.   ^( n% R5 v/ F
  5. >>> math.exp(1)
    * W$ }2 i; v7 t! e
  6. 2.7182818284590456 B& n( L' n6 [* w
  7. >>> math.exp(2)6 C# t& o5 @: \) d1 R
  8. 7.38905609893065
    - N- ~4 _; V4 w8 _1 x
  9. >>> math.exp(3)5 l) S# l% {$ j/ e2 r! R
  10. 20.085536923187668
复制代码
4 i" t/ [' x2 k. ]; n1 Y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1, i5 ~2 b0 s, j* {9 v  h/ L
  1. #返回math.e的x(其值为2.71828)次方的值减13 V: G) o5 J) o- }$ c0 O
  2. expm1(x)+ C% }$ {0 m5 ~
  3. Return exp(x)-1.' u* H( @: Z( [& A( \. E
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.4 _1 a" D4 B* Y6 r5 E

  5. 7 C! L+ j# S& ^
  6. >>> math.expm1(1)) O8 G  W0 B" C6 m7 g# m8 q
  7. 1.718281828459045
    3 W% l, U' D1 Y
  8. >>> math.expm1(2)8 z, U  X! B& s' c- g
  9. 6.389056098930656 A8 V9 @. l) ?2 h- J, i9 \& X
  10. >>> math.expm1(3)3 P% o( Z; Y/ E9 t% _2 n
  11. 19.085536923187668
复制代码

# {8 G- X" P/ Z6 t9 Umath.fabs(x)  返回x的绝对值! ~8 s1 r) r/ C( O
  1. #返回x的绝对值8 Y0 X, R( a& q" I. D
  2. fabs(x)" t5 I7 {" g) ^9 h- D
  3. Return the absolute value of the float x.
    + @  {/ [: L  r6 w' X) G5 ^- D
  4. 5 s7 n& s: w) w, P, {5 x
  5. >>> math.fabs(-0.003)
    : t4 K/ V% N$ }
  6. 0.003
    # l0 r5 D7 ~3 x9 a
  7. >>> math.fabs(-110)6 p( f" K9 _& j' l
  8. 110.0/ g: Y  `; H' n5 Y2 T; R" N
  9. >>> math.fabs(100)) x" o! x  h; d: i: y% {& }# J
  10. 100.0
复制代码

3 C  ~- A( B5 V8 vmath.factorial(x)  取x的阶乘的值7 _' |, P% j; N" h2 V# D
  1. #取x的阶乘的值% L* z+ N8 |" F0 c0 U, ]
  2. factorial(x) -> Integral
    * y1 d0 Y* X1 f# H* W/ n& g% W- ~
  3. Find x!. Raise a ValueError if x is negative or non-integral." ]; u8 G7 J- ?% g
  4. >>> math.factorial(1)
    ) p; m0 `4 D7 S- P
  5. 1; V+ T' L( e5 K& l/ Q& w' y3 v4 _
  6. >>> math.factorial(2)2 W; J* i* I0 R8 K, E  I
  7. 2! e9 k: {  b+ P' |
  8. >>> math.factorial(3)
    2 y! {+ @& }2 k+ \, o; `- r$ Y) I
  9. 6! k6 ~0 \9 {8 g) t; |% r
  10. >>> math.factorial(5)$ ~" K. A: s1 _2 g, E- x
  11. 120
    * n+ T1 [% O1 J9 m  S$ I
  12. >>> math.factorial(10). V9 E4 Y/ M# l) {7 V; u) o
  13. 3628800
复制代码
% N, g& e+ r( {# |
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
' D  n8 L, c4 u2 b! T
  1. #得到x/y的余数,其值是一个浮点数; `6 F, g* `- g% s0 o
  2. fmod(x, y)
    ' O+ h4 A7 |! h5 j' f; K
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    3 q# N' Q8 |$ ?+ c+ ?: E
  4. >>> math.fmod(20,3)& I, D. u% F2 u  P; F9 y
  5. 2.0
    , z8 m0 ^# |) N% ~0 F
  6. >>> math.fmod(20,7)
    - ]0 m4 ]0 ~- i( L
  7. 6.0
复制代码

  K$ T7 S: J5 y3 ]4 @+ D" imath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围6 D. ^  {; m# y4 \
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    5 f+ d1 P8 n$ ?0 a
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ K% F; x, h# ]9 R. s# `2 x
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    1 `& x7 J' Y$ w2 s! W
  4. frexp(x)
    % w/ r/ ]  o% C$ M) E6 x" e6 u
  5. Return the mantissa and exponent of x, as pair (m, e).& q! E; p& W* ^' X- Y; }
  6. m is a float and e is an int, such that x = m * 2.**e.6 B5 H- ]* |7 O2 p7 r
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0., b, E7 V" ^, T8 M+ @
  8. >>> math.frexp(10): s3 V/ y' q  c
  9. (0.625, 4): q1 y+ V) c, r( [& K: ]; n
  10. >>> math.frexp(75)2 z! W$ d: a( s/ j5 y
  11. (0.5859375, 7)
      w% g4 b4 r4 b; D1 N
  12. >>> math.frexp(-40)) K0 a1 B5 D  D" w8 c% g; }
  13. (-0.625, 6)
      d' b% e9 E; e% o1 e' x
  14. >>> math.frexp(-100)
    6 a8 O. [! ?7 f3 \* M
  15. (-0.78125, 7)4 B& h# h% \9 {2 b8 y* I
  16. >>> math.frexp(100)( I7 Y! ]6 O2 t# D5 E* S/ l
  17. (0.78125, 7)
复制代码

+ `6 F6 w8 s7 u% emath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
* u9 C) e0 _, m- o0 h/ o
  1. #对迭代器里的每个元素进行求和操作
    . f! c/ w  H. {$ S. W
  2. fsum(iterable)
    / \& G$ a+ {$ x8 N5 t, G) m  \+ P
  3. Return an accurate floating point sum of values in the iterable.. i* m  w" b3 j. p- s: K# d
  4. Assumes IEEE-754 floating point arithmetic.- e( I3 F# w2 M% [' K, P3 _
  5. >>> math.fsum([1,2,3,4])
    0 c" M3 }6 E$ |+ P  T
  6. 10.07 `9 U; f$ y* Q3 @- {. B& U
  7. >>> math.fsum((1,2,3,4))
    ! g  T8 ]' v& W- G$ P
  8. 10.0. x/ ~1 H) j% C8 j
  9. >>> math.fsum((-1,-2,-3,-4))8 v9 p. l" l- u1 _2 w
  10. -10.0
    + |% M4 i7 q! i( O6 X. r1 Y' ]
  11. >>> math.fsum([-1,-2,-3,-4])
    2 p- u5 A# J) m% _  W4 V
  12. -10.0
复制代码

- _! R& f- j" I$ o% o2 @1 Imath.gcd(x,y)  返回x和y的最大公约数
; Y$ R; T! U5 O9 @4 V& r
  1. #返回x和y的最大公约数2 B% ~; w" Q4 K# c
  2. gcd(x, y) -> int
    5 a# {0 {) j3 S1 `
  3. greatest common divisor of x and y& R$ w5 k2 c7 q, {
  4. >>> math.gcd(8,6)
    4 f# _: {! a+ F) t2 S7 N
  5. 2
    % R0 O6 c3 Q, g
  6. >>> math.gcd(40,20)
    " a/ T* K# y6 M" _
  7. 20
    3 c8 J% ?# R1 [
  8. >>> math.gcd(8,12)
    $ ?0 W: U. U5 U' s0 w
  9. 4
复制代码
# F; g& z+ R1 ~& X; e) m
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
0 e( s# Z/ W) R6 p
  1. #得到(x**2+y**2),平方的值
    $ j) V, R6 |5 d
  2. hypot(x, y)
    ' U& {. h, S8 v
  3. Return the Euclidean distance, sqrt(x*x + y*y)." G% T- c1 x7 j, V: Q) {1 I$ Z
  4. >>> math.hypot(3,4)( c. o) H0 @: m$ ?( c
  5. 5.0
    1 ~) [' z: q; e
  6. >>> math.hypot(6,8)0 @8 g1 X  K$ _1 r
  7. 10.0
复制代码
6 a# x) r- n) S8 p2 T0 e2 u- L
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False5 _* i' s  F% b4 ^
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 {# g$ a4 h% i- I
  2. isfinite(x) -> bool7 O+ O# {2 V6 K: D8 \9 [6 h
  3. Return True if x is neither an infinity nor a NaN, and False otherwise." u+ }0 k  F& C8 e# k
  4. >>> math.isfinite(100)+ q/ u. W$ C; b) ?3 M
  5. True0 l; S$ h  a* L5 E! Q$ m0 D
  6. >>> math.isfinite(0)
    # H- w0 C0 R* V9 I" b4 _
  7. True
    - t3 n7 v) }/ `4 ~" L! i& _: ^; M
  8. >>> math.isfinite(0.1)4 K6 w: F0 P& x* Y4 y! Y1 }7 m
  9. True! P: q/ ~/ [) @# J; t
  10. >>> math.isfinite("a")( j# ^% d5 c6 _# F- j; r
  11. >>> math.isfinite(0.0001)
    1 n+ {; Y; v, ^! \
  12. True
复制代码

: k" v# B6 @4 P4 z2 R' y' f4 xmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
* x! ?/ _+ r% k
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False% v! @1 T" d0 y3 |0 S, ?: p3 H1 |
  2. isinf(x) -> bool
    7 c" M7 X, C: n/ `0 [) H/ z% V
  3. Return True if x is a positive or negative infinity, and False otherwise.( H3 h& K, w; I. l
  4. >>> math.isinf(234)5 S1 O% b# l4 x, c
  5. False  f8 h" J; ~' t4 m- x' R" s' Z" b: T
  6. >>> math.isinf(0.1)) k1 }0 @! g. N) F$ Y8 t
  7. False
复制代码
9 z3 H' ?, o/ p* J7 L+ l+ }
math.isnan(x)  如果x不是数字True,否则返回False$ V5 S9 d! b$ _# f2 s
  1. #如果x不是数字True,否则返回False
    ' e1 E+ ]7 M* x5 D# Q6 l# _4 |8 W( e
  2. isnan(x) -> bool
    : [% U3 [' m1 K3 }2 |
  3. Return True if x is a NaN (not a number), and False otherwise.! t( A! j% `9 E: t! \; w: D0 S
  4. >>> math.isnan(23)
    ' B9 b: O, t3 Y- t) |; j5 A- A
  5. False; w0 L& s$ t1 T7 s
  6. >>> math.isnan(0.01)* m4 Y  ^( H) G* @& a9 |
  7. False
复制代码
' W) C. b+ k& C/ w: X5 o
math.ldexp(x,i)  返回x*(2**i)的值4 a# E% ]# R& c: r1 L! z% U
  1. #返回x*(2**i)的值! l2 P) j8 z+ v( d; [& y/ m
  2. ldexp(x, i)( e" N8 F$ m1 k
  3. Return x * (2**i).4 B6 B& @' N9 V
  4. >>> math.ldexp(5,5)( N0 f! H  D# P) I( r3 C
  5. 160.0
    7 j: i2 Y5 h8 p/ e6 Y2 s8 S' X5 O
  6. >>> math.ldexp(3,5)
    7 o* M# `, [+ E" N# V
  7. 96.0
复制代码
  k- y* q# ~7 d# r
math.log10(x)  返回x的以10为底的对数! M3 {" \6 E5 ~- b( L$ B0 f
  1. #返回x的以10为底的对数& {( U- r% x: X: `5 J# i
  2. log10(x)
      k6 c% N% B) d7 Z2 j4 c
  3. Return the base 10 logarithm of x.
    7 R  V9 V3 p5 Z. y1 r& ?3 g# j
  4. >>> math.log10(10)) U3 O/ X* N" g: k& C
  5. 1.0; `# ~5 Y$ j: `' B
  6. >>> math.log10(100)( `, j% T9 q3 R/ u) ]3 p0 k
  7. 2.04 U- m1 ^8 I7 B
  8. #即10的1.3次方的结果为20
    ; e, w4 `+ D# g$ c
  9. >>> math.log10(20): k% n" W- e3 Q& O! m/ z# E  ^( ~
  10. 1.3010299956639813
复制代码
: p$ z# M- F( I: E$ m
math.log1p(x)  返回x+1的自然对数(基数为e)的值
0 d& o: p! G/ _% p" w" q
  1. #返回x+1的自然对数(基数为e)的值
    : A; q% J5 i$ E# u# x' N
  2. log1p(x)
    ) [4 T% {1 _$ B$ P5 q* n
  3. Return the natural logarithm of 1+x (base e).: x& s4 }  U1 ]
  4. The result is computed in a way which is accurate for x near zero.- n& B% l# _8 Y4 k' P7 D1 a
  5. >>> math.log(10)
    ( s! r5 _+ D3 L3 G$ L6 l  i# }: g" j2 j
  6. 2.302585092994046$ R. z: H6 q+ \# r) ^2 W6 ?
  7. >>> math.log1p(10)
    * D! s/ z8 _6 @! ~2 O9 t
  8. 2.3978952727983707) o, W, B; d$ a% y2 {' j
  9. >>> math.log(11)
    : m9 f5 m  \6 ~) q1 w1 _0 t
  10. 2.3978952727983707
复制代码

1 w( E1 H4 y9 F/ P; I! Fmath.log2(x)  返回x的基2对数5 ?& a4 A. B1 P/ l3 s
  1. #返回x的基2对数
    $ S  o; w7 R( @! ^5 n, V
  2. log2(x)  M, M/ s5 |3 ]& i6 O( P4 C
  3. Return the base 2 logarithm of x.
    . w1 b* W8 |, z: T
  4. >>> math.log2(32)6 O7 `! }/ ^9 s6 |  {. M
  5. 5.0& |- ^1 B6 e% ^) M% B
  6. >>> math.log2(20)6 `6 U5 C) D5 a  ]
  7. 4.321928094887363, X& G& p; m) c. L% O/ {
  8. >>> math.log2(16)7 m" p$ C+ |: ^7 [6 ]' R+ @4 E+ e
  9. 4.0
复制代码

" f# a: @; T! S2 g2 A+ Ymath.modf(x)  返回由x的小数部分和整数部分组成的元组0 K& P$ G2 s$ l
  1. #返回由x的小数部分和整数部分组成的元组: b' H" A2 P7 ]- A5 t  M4 ^% R
  2. modf(x)
    4 S4 r2 W- A* ]* {  x' W
  3. Return the fractional and integer parts of x.  Both results carry the sign
      s* o3 y% M7 Z( f; g; o: B/ n: y
  4. of x and are floats.
    4 ?, u: ~, _$ f+ J0 p4 n
  5. >>> math.modf(math.pi)
    ' [9 A5 p) d$ _, v. O! A
  6. (0.14159265358979312, 3.0)2 t! i" b( S% E: x
  7. >>> math.modf(12.34)
    / [) S5 J3 l+ C, R+ m
  8. (0.33999999999999986, 12.0)
复制代码

6 w0 ~/ @( }8 P; I: |7 ?! Gmath.sqrt(x)  求x的平方根
' r7 a7 U) K& s# j# M( v: ^
  1. #求x的平方根) X, |' g' Y# u, n$ C+ `* h* y
  2. sqrt(x)/ Z4 U# K1 z8 r5 y
  3. Return the square root of x., Y! ~' F: N( i" b' z. g
  4. >>> math.sqrt(100)/ w, a& v  @( F3 S2 S5 D; v8 s3 z- C
  5. 10.00 O& Z9 a/ y7 K$ M' p5 y1 m  l
  6. >>> math.sqrt(16)
    / [+ r7 B7 n" G7 V, P
  7. 4.0* m( Q8 o- f  Q$ O8 z9 T1 ~
  8. >>> math.sqrt(20)( o: G( g. c7 ^* g0 r
  9. 4.47213595499958
复制代码
9 D3 z$ B! q% p( N6 t$ B  l( i5 l+ v
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    6 \# P: N0 O- m/ c/ x  j5 W5 @
  2. trunc(x:Real) -> Integral7 q" V: T6 z, s6 n( Z1 m
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    9 V4 Z5 e/ ]+ u0 i: p6 I5 U7 R
  4. >>> math.trunc(6.789)& x- g4 q3 K+ t' c7 Z
  5. 6
    " ^; _  p, w, E/ v6 J5 N0 J! l
  6. >>> math.trunc(math.pi)+ y1 T* ~  n: u3 j& D
  7. 3+ Y* ^3 ]  p7 r0 y9 \
  8. >>> math.trunc(2.567)
    5 i- o: M: v6 Y$ C* O
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-3 08:03 , Processed in 0.078231 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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