新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

* }* e7 c6 D# L7 H3 P, t【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' c9 i. q8 q0 S# \8 |& u

1 r0 M$ Q  G  p. `7 ?方法1+ U, `) a6 |# U# w6 j% H. M
  1. >>> import math2 d) Z4 ?. ], R# P, }
  2. >>> math.sqrt(9)  x  p0 l4 u& y
  3. 3.0
复制代码
方法2
2 A# {) W$ m- {$ o- Z4 K( j
  1. >>> from math import sqrt1 w1 e0 s  f: n# f$ s) _: {
  2. >>> sqrt(9)
    9 }4 ?( y. A  d; h
  3. 3.0
复制代码
$ i0 {2 e$ Z( O: n

( ]; b. F; z' S% Y5 H! G8 J
math.e  表示一个常量( g( r4 f9 R0 V
  1. #表示一个常量
    9 E) {% D/ `" _+ k6 w, c+ m
  2. >>> math.e
    1 t3 n: e# ?* P4 V7 C1 s# |
  3. 2.718281828459045
复制代码

5 J# ~" K% z  o. i% rmath.pi  
数字常量,圆周率
! ^1 O' T6 @( x  c2 E
  1. #数字常量,圆周率
    " V# Z5 w: W: ^5 m( \: |
  2. >>> print(math.pi)
    3 y( L4 M7 [) M# i
  3. 3.141592653589793
复制代码

, v; O; \" R6 L/ Z1 h4 A+ gmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
) d- K$ O2 @- E$ p- P
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    9 \6 ?, x3 E! L9 P1 @$ D' ?# g+ a
  2. ceil(x)
    7 E9 }0 J! i& W, s8 a( T6 K
  3. Return the ceiling of x as an int.
    8 R4 j* J9 W& q# i7 h( C
  4. This is the smallest integral value >= x.
    " Q% M) S6 Z6 a1 y6 r" H
  5.   R% g0 k. I: P7 `  [' ?/ O
  6. >>> math.ceil(4.01)
    ; x, l7 ~  K, k. k& Y; M! f/ P
  7. 5
    " k9 y; m" u# p& e; T; H
  8. >>> math.ceil(4.99)
    5 S4 M5 S, j6 v! }8 X8 A
  9. 5
    0 n0 c# }0 `# q4 ?7 u
  10. >>> math.ceil(-3.99)& a  A3 N. r) o) ]' E
  11. -3
    ) e: V  z3 v  H( h7 p0 F# o
  12. >>> math.ceil(-3.01)+ I: S% l. c% K+ F. ~0 P
  13. -3
复制代码
  P  i3 L7 u  w, _. ^: W9 ?
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 C2 _- j% Y+ D# Y
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 ]. t5 z; u2 n
  2. floor(x)
    ) n* ]  r/ D- d* I
  3. Return the floor of x as an int.* i/ b- y0 n4 y. R/ O" N: R5 g; Z1 M
  4. This is the largest integral value <= x.8 `2 U  F3 Z0 Y& h! N2 v! P0 y, z
  5. >>> math.floor(4.1)7 X" s" [  N3 g( o. W7 j8 F# T
  6. 46 k" o/ P* H2 r+ z8 U: Y
  7. >>> math.floor(4.999)
    * i6 C3 I& t7 C' y( A. S
  8. 4. L. L5 d" G4 ~9 W- w
  9. >>> math.floor(-4.999)
    % q% I: {- g3 G0 l
  10. -52 u8 t/ \+ m' j2 S
  11. >>> math.floor(-4.01)% f7 b- e4 s' V3 G! |! p) U# K
  12. -5
复制代码

: R3 z8 I* I. R$ d' \0 P) h  K! {math.pow(x,y)  返回x的y次方,即x**y* Y0 m) Y7 j* {( s) |
  1. #返回x的y次方,即x**y
    4 R! k/ u: J2 t# a8 u
  2. pow(x, y)
    : a' ?- p2 L7 w! T
  3. Return x**y (x to the power of y).$ k% e8 N0 Y8 _% ^7 D& E
  4. >>> math.pow(3,4)6 L/ u# G, t9 I
  5. 81.0, s! X( @5 J9 H! Z, I  o
  6. >>> 8 R: o$ y' @1 x. m/ `) F# h
  7. >>> math.pow(2,7)5 h' s' u: O4 N/ L! I
  8. 128.0
复制代码

6 h. E8 V6 b0 S9 P: cmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
! T' O  Y8 F2 U9 C/ w: w2 Z( @
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    / V1 {  w; M" C. C! P
  2. log(x[, base])0 C) j; R4 x" |+ v# |
  3. Return the logarithm of x to the given base.
    / D. Y+ }! J: X
  4. If the base not specified, returns the natural logarithm (base e) of x.
    " s  }6 _$ [8 l2 K( s1 |1 [
  5. >>> math.log(10)8 ~5 Q$ @+ A% x
  6. 2.302585092994046
    ' e) h: f& |2 [0 r  f5 q. @" R% u
  7. >>> math.log(11)
    ' y2 m1 X% |# r+ D2 c$ ^1 K$ U& R1 E
  8. 2.3978952727983707* d6 z' j+ {% g& A
  9. >>> math.log(20)3 R  [$ x/ w7 Q5 h4 r: F9 k: [
  10. 2.995732273553991
复制代码
3 o; Z# {" b- R
math.sin(x)  求x(x为弧度)的正弦值
+ r! Z/ |# q+ o) {
  1. #求x(x为弧度)的正弦值+ ~$ \; G' O( w7 y( E; H' |
  2. sin(x)8 A0 [0 d# p% j
  3. Return the sine of x (measured in radians).9 ?3 U; \- k9 V7 q$ e0 A( D( v: X
  4. >>> math.sin(math.pi/4)1 a. |! k, u' x- T1 f* W$ n. K3 ^
  5. 0.7071067811865475& F' s% H- `8 k* s
  6. >>> math.sin(math.pi/2)0 o/ T. T' x* Y' y/ s& x
  7. 1.0  m8 O2 [8 l0 j9 w3 ?% K0 J
  8. >>> math.sin(math.pi/3)  c2 y0 W) |% Y! u
  9. 0.8660254037844386
复制代码
: B' o0 b; A2 p; K: R- J( U
math.cos(x)  求x的余弦,x必须是弧度3 U+ v) p1 x( m$ t7 W' Y$ I
  1. #求x的余弦,x必须是弧度  Q9 z; V" x! Y$ d, d3 t
  2. cos(x)
    6 h+ L$ ^* {! f- l7 B
  3. Return the cosine of x (measured in radians).
    ' o  X+ G9 @3 t2 Q$ n- _  j) d
  4. #math.pi/4表示弧度,转换成角度为45度2 }8 ?% j8 w0 m- F; x
  5. >>> math.cos(math.pi/4)
    ' u/ f; G0 D. X+ _
  6. 0.70710678118654761 ]: d0 Y5 Q) t( d# l
  7. math.pi/3表示弧度,转换成角度为60度) n6 |2 R. d' U+ d8 m  d% F( d
  8. >>> math.cos(math.pi/3)
    0 L, l( E/ E$ P' P$ k4 g
  9. 0.50000000000000017 |/ k! P  N  H  ]% L
  10. math.pi/6表示弧度,转换成角度为30度% }+ U0 I: E- z( S
  11. >>> math.cos(math.pi/6)
    ; I4 [5 N) F% u/ r% ^4 L( i. q5 N/ f
  12. 0.8660254037844387
复制代码

3 g6 A( W( a, Z5 K4 B9 vmath.tan(x)  返回x(x为弧度)的正切值3 e% E$ V; ^& s( r
  1. #返回x(x为弧度)的正切值  a! Z% D& a! z/ r# @, X" O# B
  2. tan(x)
    & B9 Y( y5 \. X% d4 h" m
  3. Return the tangent of x (measured in radians).
    ! L9 G: U0 H( @: B
  4. >>> math.tan(math.pi/4)
    ( C# q$ \% P. @2 }
  5. 0.9999999999999999
    ( t- M) {0 b* t. W4 B
  6. >>> math.tan(math.pi/6)  S3 j3 Y/ H7 Y- D( @# p* H0 _3 r5 n
  7. 0.57735026918962575 Z9 o! H: U1 x9 y. Z- {1 R
  8. >>> math.tan(math.pi/3)
    2 u$ C+ m3 J+ |) A) ]* t
  9. 1.7320508075688767
复制代码

$ r/ N5 ], e# c- V. I! X$ Imath.degrees(x)  把x从弧度转换成角度
: C/ }$ u! z; n) H# T9 s  @
  1. #把x从弧度转换成角度4 i/ n* \3 a6 I' B! S- [: `
  2. degrees(x)
    5 l! X3 f. M0 g; X& O
  3. Convert angle x from radians to degrees.
    , J+ f: p! N: H
  4. & e, Y$ Y4 K, E9 m: {
  5. >>> math.degrees(math.pi/4)
    ! e8 q' Z, F6 u3 O
  6. 45.0
    + i; C; @- N! E
  7. >>> math.degrees(math.pi)
    : D* Y/ Y; X2 T& ?+ W
  8. 180.0
    , d* L& r- V" b9 N
  9. >>> math.degrees(math.pi/6)
    . Y0 z5 N% r. T+ h8 _
  10. 29.999999999999996" x) d' i" u, c) l) x
  11. >>> math.degrees(math.pi/3)
    ! C1 S$ u1 `& T0 p
  12. 59.99999999999999
复制代码

# \1 V: |, u7 X) O: h6 kmath.radians(x)  把角度x转换成弧度) d$ _) J  M: k4 |7 K4 q. x- [+ o
  1. #把角度x转换成弧度
    - N$ E- E$ t4 S/ E2 S1 @! t1 p
  2. radians(x)  t6 p  R* o, F: y& I8 x
  3. Convert angle x from degrees to radians.: S% b+ K5 ~1 d+ A. k$ g
  4. >>> math.radians(45)( m8 m- c( M4 K( N  X1 o# J
  5. 0.7853981633974483! g, s4 X" y: j
  6. >>> math.radians(60): y- _& {7 E, w; V5 A7 g3 g* D( H
  7. 1.0471975511965976
复制代码
  c$ z0 u7 ]0 o" @( a: n+ g
math.copysign(x,y)  把y的正负号加到x前面,可以使用03 s8 ~& y! ]8 h/ f6 n2 x; X
  1. #把y的正负号加到x前面,可以使用07 \5 A5 C" Q0 T3 m' T" w( d
  2. copysign(x, y)8 u/ X' d" M8 h; P! \, p
  3. Return a float with the magnitude (absolute value) of x but the sign 7 p, F% H9 l7 x$ }8 [( @) q
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ( G/ W3 ^8 \) @& {
  5. returns -1.0.
    5 W- I0 }; a2 _

  6. ! i, e; n* n" W+ K3 e5 U
  7. >>> math.copysign(2,3)4 W) c7 c# V9 ]% b
  8. 2.0) M/ X* H/ \. a
  9. >>> math.copysign(2,-3)$ R7 u9 `( d7 `
  10. -2.0/ U& _% p3 F  }
  11. >>> math.copysign(3,8)
    - {. R; X4 M3 P' G6 V  R7 s) W* Z
  12. 3.0
    8 _1 w% b4 d7 X0 g4 M! }6 M
  13. >>> math.copysign(3,-8)  e$ N: u' s) p$ S( s
  14. -3.0
复制代码

9 c" D* T- H! H0 @  Smath.exp(x)  返回math.e,也就是2.71828的x次方- @5 l) T. H9 S& Y5 |! q& V+ I9 K
  1. #返回math.e,也就是2.71828的x次方% V( W# P" [/ q
  2. exp(x), }  X' ^. a) |  @* \3 Q
  3. Return e raised to the power of x.
    + Q. v: ]. g& j1 y2 T: e9 Q1 n6 C. {  K2 e' W

  4. ; N5 U4 e+ d( e9 L. X
  5. >>> math.exp(1)
    2 f3 i$ h8 \5 Q: S" p
  6. 2.7182818284590458 h4 r, f7 ]' B. n9 y! j
  7. >>> math.exp(2)) Y( a) s  e5 q8 C
  8. 7.38905609893065
    + Q+ C. i  ?' K; o& q
  9. >>> math.exp(3)
    ( \: o' n2 T, V
  10. 20.085536923187668
复制代码

8 f2 _7 z* Q) x5 L1 B* g0 a/ w) F5 Fmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1" E' Y% J8 q4 n* e' U( F) ]
  1. #返回math.e的x(其值为2.71828)次方的值减1
    , w# ^7 U( \/ V9 ]) x* l
  2. expm1(x); _3 C$ M4 T! v  d5 O, @, a3 W
  3. Return exp(x)-1.
    ) ]# T; q4 y1 Z7 a' ^
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ; w  I: d; I: r2 v$ }* r% N# Y

  5. ! d* _* Q1 U3 W0 B- @1 c
  6. >>> math.expm1(1)) l4 O7 B, g' K7 Z5 I
  7. 1.718281828459045
    / w0 O- _# P, u2 h0 g) ~" i
  8. >>> math.expm1(2)
    , O  ~- z% f' I. a& J1 ^4 q% z
  9. 6.38905609893065
    ; `4 o6 G. x3 U* i
  10. >>> math.expm1(3)5 v( Y+ H$ V% v7 {% P7 f
  11. 19.085536923187668
复制代码
. l! R6 G" N4 ^) v0 y; \, I
math.fabs(x)  返回x的绝对值4 K/ e5 E" O0 l9 ?
  1. #返回x的绝对值
    0 m% C- Y, M7 d# o
  2. fabs(x)6 j, u7 N# }5 w$ _5 I6 \; \/ v4 \- d
  3. Return the absolute value of the float x.
    * x) g3 ?# k) X  U& {$ w
  4. 6 H; ~' b/ h5 ^' ~" \8 e2 y' ~
  5. >>> math.fabs(-0.003)  \- f- x: Q" ]4 F) W4 t$ _6 o: @
  6. 0.003' k! E3 ^, |0 H
  7. >>> math.fabs(-110)
    " A. v" E7 t+ ^6 C
  8. 110.0
      d0 U; O, }2 `6 T+ u
  9. >>> math.fabs(100)
    ' s* [, G: o# @& b7 \3 I! Q
  10. 100.0
复制代码
. ^8 s! ~2 V; G: `7 [+ w
math.factorial(x)  取x的阶乘的值& T1 Y' F4 |1 u7 R# n1 _. N
  1. #取x的阶乘的值8 A, @( V9 F" L
  2. factorial(x) -> Integral
    # _7 q% ~$ }0 V( n& \  f
  3. Find x!. Raise a ValueError if x is negative or non-integral.4 u" |: |0 ^& G  M- |1 o( }
  4. >>> math.factorial(1)5 Y" x/ p$ x6 D8 N
  5. 1
    " G. F, F9 y7 S
  6. >>> math.factorial(2)9 [( B9 r1 \3 _3 O
  7. 2
    - f  o. o. U& t' Q; d
  8. >>> math.factorial(3)2 X% h% i# h' x2 f- p$ l7 z' e
  9. 6
    - E2 t# ~! M0 [" h9 j* n- T6 B$ }
  10. >>> math.factorial(5)
    * t& a; N  f. Q: G, E$ w
  11. 120
    9 P  s* n! n; r" I
  12. >>> math.factorial(10)
    5 x  z+ E+ y/ N% _7 d( C
  13. 3628800
复制代码

5 M; p2 L* [# R; D0 Umath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
8 p  n8 @5 x" \7 H9 Y
  1. #得到x/y的余数,其值是一个浮点数
    . i+ W$ d+ |& f, [4 `
  2. fmod(x, y)
    * u5 h6 W- ^$ G) p
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    7 K+ `! r2 ^' @& {1 D0 x' w( {
  4. >>> math.fmod(20,3)
    + A" F) g1 J9 R0 F* e# ?; @' f
  5. 2.0
    ! i" i. x. B5 u' W
  6. >>> math.fmod(20,7)
    ! o9 h) g% z# b- a" C& B/ P' G$ ?
  7. 6.0
复制代码
. I. ~- r& e2 n( e( I% ?% q
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
) T* B# |+ t. P# X$ c
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    3 p1 B2 C; Y2 }2 b+ M8 l& q* R% x
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ y  B/ @) c2 P  _4 f
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ |  e. u  |8 Q( V6 q
  4. frexp(x)$ n! o& M4 [+ \
  5. Return the mantissa and exponent of x, as pair (m, e).
    $ E, S$ H! j6 M7 J. o# K- T
  6. m is a float and e is an int, such that x = m * 2.**e.
    / L5 q6 m$ j2 `" P+ \8 ?9 y
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 z0 F* `5 _6 w
  8. >>> math.frexp(10)% o7 x& o# @: `
  9. (0.625, 4)
    , T, k; _6 {& x0 z/ A  H
  10. >>> math.frexp(75)
    % p9 E- u& d8 W+ e/ u! L* ~" S
  11. (0.5859375, 7)& t( a# Q5 V6 ~0 j) E
  12. >>> math.frexp(-40)
    5 N7 u8 m% Z. b5 S  [
  13. (-0.625, 6)
    # R* W8 {% @7 l9 S) O
  14. >>> math.frexp(-100)
    ' w3 i6 n; k; T( @7 l
  15. (-0.78125, 7)) c" v* R: {) X$ e0 X
  16. >>> math.frexp(100)0 X0 o4 \9 u8 T4 U" s# S
  17. (0.78125, 7)
复制代码

3 W3 ~6 Y5 e- _math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
5 @- n  e) W& q  o6 o+ k
  1. #对迭代器里的每个元素进行求和操作
    4 d+ K- S' u. n: A
  2. fsum(iterable)7 v+ T; q+ G' _' l2 ?3 i' ]
  3. Return an accurate floating point sum of values in the iterable./ n+ T% Z" y/ _
  4. Assumes IEEE-754 floating point arithmetic.
    8 w% [/ i3 `: o. k& O/ g
  5. >>> math.fsum([1,2,3,4])
    . ~) F; f& q& g4 h
  6. 10.0
    1 [: x4 z* r8 _9 m
  7. >>> math.fsum((1,2,3,4))% C& A3 M1 E/ t0 v% m9 Y
  8. 10.0
    ! B: a$ t  r6 z  V: o
  9. >>> math.fsum((-1,-2,-3,-4))4 q# X% i1 `' H  q" j
  10. -10.0# h) N6 c. k% {. C8 M/ C$ C. ?7 U
  11. >>> math.fsum([-1,-2,-3,-4])
    ; H5 g- }( g" m& p3 Y
  12. -10.0
复制代码

: S9 l9 E" y! ]* Q1 Gmath.gcd(x,y)  返回x和y的最大公约数( q. G; t" [, F4 q7 a8 A
  1. #返回x和y的最大公约数" x' U, W# g% m" x5 S9 w
  2. gcd(x, y) -> int
    2 [+ L) y& H6 M8 y
  3. greatest common divisor of x and y
    5 E( _9 z" L* {# }6 z
  4. >>> math.gcd(8,6)( G, ~* k" L/ Z: T
  5. 29 j4 g5 y0 {. c& e
  6. >>> math.gcd(40,20)" }: F) m. |2 T7 {7 e
  7. 20
    $ H9 ?( v* q2 G& @
  8. >>> math.gcd(8,12)# a, u- @* J$ f1 `7 I( \
  9. 4
复制代码
. D3 m8 }5 z7 Q; n2 w& v% x0 ?! Z
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False1 m% y( R& T9 L, R" _( |- _1 ^+ r
  1. #得到(x**2+y**2),平方的值
    4 S# C% Q8 c8 i8 H
  2. hypot(x, y)
    * }4 `* a0 s( V" V
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    " G8 N7 D. \% h6 B+ c
  4. >>> math.hypot(3,4)
    + O! h- g; y) {* M: F; p0 U
  5. 5.0
    / r! A; ~0 t1 L' {4 _# h1 M
  6. >>> math.hypot(6,8); a9 |4 n. ]2 p5 r
  7. 10.0
复制代码

& s' S' A' _9 _. T0 Tmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
. s) g- g4 v0 M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False/ g, p5 k' _! F: }: u
  2. isfinite(x) -> bool! w! N4 q" y1 E
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
      Q2 @+ M9 \1 e4 P6 `3 q! p% H
  4. >>> math.isfinite(100)8 f( h& a- {2 v! }, g
  5. True
    2 \4 \! w  R6 s; v
  6. >>> math.isfinite(0)
    ( F* R$ l# T* _  l* k1 x
  7. True5 \) L  e' b8 h7 b$ C
  8. >>> math.isfinite(0.1)
    : g4 b+ K) `  z8 h; ~
  9. True. c8 @5 }' P+ i4 W
  10. >>> math.isfinite("a")3 t: n0 |+ q5 @6 }  n1 H
  11. >>> math.isfinite(0.0001)+ P/ V0 S1 z2 L( v5 t
  12. True
复制代码
$ R( d7 s, u; V: I: ]; h
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False+ y$ p( a# M; T2 j8 ^! f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False- ?3 W' g& Y' H" P: O
  2. isinf(x) -> bool. ?. c- D' Y/ i: p; @
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ' Z6 |- ^4 u( Z3 i$ K& F+ l
  4. >>> math.isinf(234)7 D' E( z1 T8 Z
  5. False/ K+ O% P! u! ^, K
  6. >>> math.isinf(0.1)) _8 u0 E% R7 f! w" k
  7. False
复制代码
1 e$ H4 N& b: F( [2 A: y
math.isnan(x)  如果x不是数字True,否则返回False
$ V9 v* F8 a# G! }6 Y: E
  1. #如果x不是数字True,否则返回False
    1 f. [; g# V6 C* Y0 f3 C, H7 t. ~
  2. isnan(x) -> bool2 K. Q5 `  {4 o  l* q
  3. Return True if x is a NaN (not a number), and False otherwise.
    ! y% N& [& B% h! W, V
  4. >>> math.isnan(23)
    1 {  Z$ j) m' Q3 L
  5. False
    6 U! X0 l' B. G5 `) B. j& p
  6. >>> math.isnan(0.01)7 K  x% n8 s/ x
  7. False
复制代码

' y- l* d* q6 m! ]) I2 pmath.ldexp(x,i)  返回x*(2**i)的值
, H* a8 r& g' g0 N
  1. #返回x*(2**i)的值3 h# O; y* _8 v; d6 v- E' K9 `" Z
  2. ldexp(x, i)" @6 W, Y. s& @: X& R0 |5 ]
  3. Return x * (2**i).$ U* J" n& ~/ K6 l) E3 J/ R* N
  4. >>> math.ldexp(5,5)
    - ~1 F8 V& H6 R% w( {% l# L. C
  5. 160.0
    & S1 B- f9 L1 S+ F
  6. >>> math.ldexp(3,5)4 |8 Y2 G& T: r* N2 r9 V
  7. 96.0
复制代码
7 O# ^% x2 x/ A5 F! e* T5 C
math.log10(x)  返回x的以10为底的对数  P. |: M0 Z* x- G. e
  1. #返回x的以10为底的对数
    ' p' c, M0 _+ \! d) B2 E' p
  2. log10(x)$ @: M8 E2 C1 C% h
  3. Return the base 10 logarithm of x.! l: }1 j" M* S! N7 f* d
  4. >>> math.log10(10)
      @" ^0 J0 P% E# V9 K+ u+ d  Z0 t
  5. 1.0
    ' y* w7 b. D9 i( v6 {2 l0 Q4 L' _
  6. >>> math.log10(100)
    ; D5 I, s* Z* m' y& b6 l4 {1 V
  7. 2.0
    5 ~& Y, n6 C  W2 J
  8. #即10的1.3次方的结果为20, N6 ^& J: ^7 i: r+ V6 U: k
  9. >>> math.log10(20)
    % T; k& i" Q+ \# c7 ]& W9 ~6 n5 K
  10. 1.3010299956639813
复制代码

) W0 a1 Y- P2 wmath.log1p(x)  返回x+1的自然对数(基数为e)的值
2 F" }; M$ M# i, L" d/ F
  1. #返回x+1的自然对数(基数为e)的值+ R3 L* ^0 a$ p  E/ u  H2 U
  2. log1p(x), _5 d7 o; H; ^# ]( A
  3. Return the natural logarithm of 1+x (base e).
    6 D1 ~; \* E. m6 k
  4. The result is computed in a way which is accurate for x near zero.
    ) A) _0 c' d; ?2 @4 ^  D
  5. >>> math.log(10)
    9 V+ M( i( ~" U# F
  6. 2.302585092994046
    0 r, N; K8 s' R7 j' T7 \$ x+ q$ S: p
  7. >>> math.log1p(10)+ A1 J$ ]! d$ G1 V
  8. 2.3978952727983707
    ) c. ]" N6 }, A
  9. >>> math.log(11)- A# X* r3 p( V
  10. 2.3978952727983707
复制代码
2 N) I# A) a2 ]. X
math.log2(x)  返回x的基2对数
" {. B! g& [" L% w
  1. #返回x的基2对数# M  Y5 w  c5 v* W
  2. log2(x)
    ' u- b# |2 v: i
  3. Return the base 2 logarithm of x.% M4 [9 I0 q3 R, k/ G4 x
  4. >>> math.log2(32)7 F4 Z2 N$ M" ^( b
  5. 5.0
    8 y  e7 E' h; Y. E% W" T& y+ ?* j7 M
  6. >>> math.log2(20)# y7 N; i( \6 X  C3 a' c# |
  7. 4.3219280948873635 ?9 r1 l/ C% j) ?
  8. >>> math.log2(16), j6 n% R  i1 |2 B6 n' N" H
  9. 4.0
复制代码

1 j8 j2 W' S9 [# S' I$ `math.modf(x)  返回由x的小数部分和整数部分组成的元组* X2 M. Z8 t- C/ _
  1. #返回由x的小数部分和整数部分组成的元组5 A7 U3 I% Z; a  o$ a1 ]
  2. modf(x)
    % M* R6 \. A0 M8 d8 P
  3. Return the fractional and integer parts of x.  Both results carry the sign
    & f$ x8 w  }% W- S6 {  g% a; B
  4. of x and are floats.
    ( ~9 }6 f; x6 L; k# g* L4 D7 i. P0 i
  5. >>> math.modf(math.pi)$ q8 O5 Y3 I6 q( B: }( v, y
  6. (0.14159265358979312, 3.0)
    4 Q" I8 S/ g  {  z: E( M/ R6 }) ^8 T+ u
  7. >>> math.modf(12.34)# a9 m$ L3 L3 v" u$ H
  8. (0.33999999999999986, 12.0)
复制代码
. z6 L" `. V/ O& Z
math.sqrt(x)  求x的平方根
7 B* w) s7 _. c( Y
  1. #求x的平方根
    8 _$ i3 h% v! t" W, L6 o6 F( B
  2. sqrt(x)1 _6 [- @$ j' @
  3. Return the square root of x.9 ^, E( r& a7 B8 V, Q
  4. >>> math.sqrt(100)
    ) v! v; j  @2 R
  5. 10.0+ Q& m* M  w/ M- Z) K
  6. >>> math.sqrt(16)9 I0 ^- I1 k* x7 W8 Z
  7. 4.0% m( v" @8 J8 I+ Z: Q4 m1 l0 B
  8. >>> math.sqrt(20)
    ' F4 F: f/ X8 G5 N1 O* @
  9. 4.47213595499958
复制代码
/ k% d2 f8 Y3 }+ W, n/ B
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    , ?0 ]9 i2 M* o1 N( r
  2. trunc(x:Real) -> Integral5 N  c' x7 _7 B  ?3 |* |2 R5 _
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ( L+ g: C  `% H& ]
  4. >>> math.trunc(6.789)
    ( P! O0 O: j: ~
  5. 61 D7 a& Q# ]9 ]- d& u% Z  Z/ ]
  6. >>> math.trunc(math.pi)  [# ^7 g6 e0 `* D
  7. 3
    % _- r9 e+ O. C
  8. >>> math.trunc(2.567)
    3 a/ N' N( h7 F' p! ?; h  Y+ L" I
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-2 18:22 , Processed in 0.083211 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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