新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

! {! v& O' Y( m0 i' L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
6 G% }. c0 V+ n  K. i1 `4 Z+ n

9 E2 V  k9 v0 }6 T# D方法11 T  t$ D- C2 L: [+ b5 N+ y9 J
  1. >>> import math
    3 r$ R" N( i) x, M3 Q1 i
  2. >>> math.sqrt(9)
    . T- g- Z* G$ n' @
  3. 3.0
复制代码
方法2$ T) t1 A; r( c- d; V
  1. >>> from math import sqrt" H4 e' O/ a, Z1 P) s0 g* a
  2. >>> sqrt(9)
    : B" L2 s" g0 b3 s) F, \
  3. 3.0
复制代码
4 C; X& S- z4 K" Y: n2 I2 w


' x( N  r1 p* a" B  }* H( R4 e2 o
math.e  表示一个常量
: {# p. n6 ^( ?6 D" u
  1. #表示一个常量
    7 ]2 \- B! E! B+ m/ e% l
  2. >>> math.e1 E! P, A5 Z0 {  s( c
  3. 2.718281828459045
复制代码
+ S+ ?3 v% K9 |8 L$ c( W: X6 i
math.pi  
数字常量,圆周率

) D/ l8 X/ N" U" {- a: G& M' A# W) X
  1. #数字常量,圆周率+ j$ K" B9 v! @  ]( \2 {
  2. >>> print(math.pi)
    - n+ c% c6 U. J9 Z3 I! E0 w! m) t
  3. 3.141592653589793
复制代码
. j! g) }: {' d" H8 q
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

" u" i! d& _: [0 O5 x
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    - K; x* {$ W, X( S# D1 [
  2. ceil(x)6 J3 ?8 ~" r9 `& I% `9 H
  3. Return the ceiling of x as an int.
    $ D) F! D/ E( ~4 Y/ D
  4. This is the smallest integral value >= x.3 o( F8 M4 x+ z, ~  Q

  5. , S) W" y5 }/ o! B
  6. >>> math.ceil(4.01)6 e9 Y: x9 X/ |& [" C$ B$ O9 j
  7. 5
    / _7 n! ?' ?- u% g) t
  8. >>> math.ceil(4.99)
    / m- Y; A/ @; x- Q2 R6 k! ~
  9. 5) d7 G; q8 m4 e8 H
  10. >>> math.ceil(-3.99)/ B- Z" H2 Y4 m8 C3 l
  11. -3* k6 I& R; X5 c) y
  12. >>> math.ceil(-3.01)3 q  A) H* ~' B: N/ F$ u
  13. -3
复制代码
7 b; p% C- @: l3 a0 H: w
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ s/ m, l  z( h( f9 x
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    8 S1 i' K* O+ o+ K6 k3 j6 H
  2. floor(x)( G" w0 s' l0 |6 t$ t2 r
  3. Return the floor of x as an int.
    0 `- O+ @* M( D, t* G
  4. This is the largest integral value <= x.
    6 q: b4 \3 L* |8 a" D: n, z0 c
  5. >>> math.floor(4.1), e, V: `: d1 s, z  V, ?' ]
  6. 4+ v$ o9 P  S* o. X5 r& i9 _  p
  7. >>> math.floor(4.999)- S0 y  g% Q$ `- |6 i
  8. 4
    4 f/ h2 v8 E8 U0 P
  9. >>> math.floor(-4.999)% d1 T2 |. Y0 R, R) D, `: P& E
  10. -5
    2 F7 w% E0 A5 O3 ~
  11. >>> math.floor(-4.01)" [% k  ~0 D' v4 o" J# L: Q/ N
  12. -5
复制代码
! a2 L+ l2 q! [
math.pow(x,y)  返回x的y次方,即x**y
2 w6 D$ E7 L/ s7 z3 t6 Z: y
  1. #返回x的y次方,即x**y
    0 I' \% Y8 L* e9 l& y) `
  2. pow(x, y)2 L6 |: N' f: I4 u
  3. Return x**y (x to the power of y)./ G4 V0 \% `* F; s  p' ?! h
  4. >>> math.pow(3,4)
    $ h5 D6 M+ i) e4 _$ E
  5. 81.0+ E0 F- I; t, r1 B+ W3 k+ i
  6. >>> 7 N/ |; ]2 l* i/ }5 r
  7. >>> math.pow(2,7)9 ]  j' t, t9 b
  8. 128.0
复制代码

) P; `7 w* |" Y1 W7 R) W0 imath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 \2 O% q8 z' g2 _* ^
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 v, l& a2 m; Q+ N! e0 B$ `
  2. log(x[, base])) u" _! Q* {' h" ]+ {
  3. Return the logarithm of x to the given base.# o! O% X) P  a- m
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ' g. l0 t6 d" ]/ p8 R6 _
  5. >>> math.log(10); m2 u, y5 i0 q8 Z; o7 @
  6. 2.302585092994046
    4 ]. K% b4 E1 t( P9 ~' w3 o# d
  7. >>> math.log(11)
    4 b$ {9 z( ?5 C
  8. 2.3978952727983707+ o6 m; h3 M, a3 v4 P' V) ]. |8 o
  9. >>> math.log(20)% G. i8 A" A. g: J( v! h
  10. 2.995732273553991
复制代码

; y* p, s4 V3 f; tmath.sin(x)  求x(x为弧度)的正弦值6 V$ H7 `" z7 r0 |. K/ }+ @0 p5 H- Y
  1. #求x(x为弧度)的正弦值
    $ F6 x# Q' F% |7 N+ }$ h9 v; q
  2. sin(x)  j; D! l8 u4 u& Z1 Y
  3. Return the sine of x (measured in radians).1 Z8 Q  l( e/ Q
  4. >>> math.sin(math.pi/4)! D7 I) m/ |# r  t$ v
  5. 0.7071067811865475, T; P2 |2 d) W" M& [  k
  6. >>> math.sin(math.pi/2)6 A7 E* ~# L5 e4 y; |6 f
  7. 1.0
    ( A6 p( a' q: y7 }. }, p
  8. >>> math.sin(math.pi/3). y& n% n# P/ \; K9 Q5 s% P$ n
  9. 0.8660254037844386
复制代码
: s# y+ P+ c. c; J  J' s
math.cos(x)  求x的余弦,x必须是弧度
2 _* t' R. x! P; F7 |
  1. #求x的余弦,x必须是弧度6 t# x& r6 X7 [5 t- W
  2. cos(x)6 n& f* `3 O; b5 r: Z! o! o
  3. Return the cosine of x (measured in radians).
    ( O/ Z! S3 o0 C0 |4 l- _: y: t
  4. #math.pi/4表示弧度,转换成角度为45度
    0 o, H* E$ i% |/ L, g: _+ {
  5. >>> math.cos(math.pi/4)0 d0 m. C$ N/ Y" x, d/ O
  6. 0.7071067811865476+ F; ]+ e, L" h
  7. math.pi/3表示弧度,转换成角度为60度" \4 z) }( @; ?3 @
  8. >>> math.cos(math.pi/3)" I# _  o) t8 F0 Q# f
  9. 0.5000000000000001
    ; Z8 K" t7 w  J) F1 b- e' i
  10. math.pi/6表示弧度,转换成角度为30度
      f9 h+ [3 Z; L+ L$ J- S
  11. >>> math.cos(math.pi/6): h. e/ s; v" Z# ~8 ^/ l
  12. 0.8660254037844387
复制代码

0 W3 P7 ~8 j5 }% Smath.tan(x)  返回x(x为弧度)的正切值
4 _% O- b/ r' g7 Z; M5 R
  1. #返回x(x为弧度)的正切值0 [: h' |) f# Z' j- X+ a, y  `
  2. tan(x)" E! n7 N" L3 t0 c$ T. t
  3. Return the tangent of x (measured in radians).
    / W& _8 o6 B) \2 o. ^* A  ^3 y- h9 U
  4. >>> math.tan(math.pi/4)* Z, \; B. h+ _2 ?3 e8 X
  5. 0.9999999999999999
    " s5 v" g% U5 `; O  a$ D! I% E
  6. >>> math.tan(math.pi/6)
    1 t$ N3 }0 m* H; M; A. _' }. C1 K
  7. 0.5773502691896257+ k0 y8 j* t7 P! \
  8. >>> math.tan(math.pi/3)1 W. ]6 Q, }$ E+ A" ~
  9. 1.7320508075688767
复制代码
0 `% o! b  d2 M9 T
math.degrees(x)  把x从弧度转换成角度
3 \8 v* J( I" ?* V" h9 f
  1. #把x从弧度转换成角度  e9 |/ k3 |% O
  2. degrees(x)
    5 ?. a5 C: Y. v# Q. p( E
  3. Convert angle x from radians to degrees.
    0 K  _; a& n- F( p

  4. + ^( \4 L' `/ N% F) h
  5. >>> math.degrees(math.pi/4)
    1 ~$ q% F+ z0 N2 U2 `( ^2 M
  6. 45.0/ w0 P' t( W. h1 F8 _; \
  7. >>> math.degrees(math.pi)
    % t+ \; ]- ~. L# Q
  8. 180.0) z3 @. [( F6 d8 h% B2 B6 _
  9. >>> math.degrees(math.pi/6)4 u" y: I$ {' v8 r) C; m+ l$ `" l
  10. 29.9999999999999969 \- ]$ e( g7 a3 y5 I  Q
  11. >>> math.degrees(math.pi/3)
    & z. d1 ~/ E- d  c1 D) R& L
  12. 59.99999999999999
复制代码

- ~0 c4 F8 f/ |' v5 V$ |  gmath.radians(x)  把角度x转换成弧度
8 I, u9 f0 i; x1 C9 z# P, b
  1. #把角度x转换成弧度$ j+ o$ {; x# i/ r5 O
  2. radians(x)
    ( x! N8 W% N. h' {4 `! j
  3. Convert angle x from degrees to radians.
    5 j% V4 w4 ^) |' k
  4. >>> math.radians(45)( P9 }6 c6 Y  }0 t/ l
  5. 0.7853981633974483* t6 ?, X  z& O; N, j2 A3 |9 i
  6. >>> math.radians(60)# Z2 s7 n8 J6 [% ^5 Y1 q
  7. 1.0471975511965976
复制代码
* v1 E& E0 b" y4 Y0 X
math.copysign(x,y)  把y的正负号加到x前面,可以使用0! O! t4 p7 ~6 u: ^" }
  1. #把y的正负号加到x前面,可以使用0
    8 M- g' }* }. h6 w: _
  2. copysign(x, y)
      k8 y# `5 J- ^8 y, `4 g
  3. Return a float with the magnitude (absolute value) of x but the sign
    ) ~9 n4 c0 k) w5 y! }0 C" Y* x$ `
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ' A6 `% u& s9 T3 V+ W
  5. returns -1.0.
    : A& [* S6 u  }0 W0 F# N

  6. % k8 j4 e( u* g4 D3 s9 |7 h" b1 B% N* |
  7. >>> math.copysign(2,3)  `) a5 r- m3 J. i
  8. 2.0  I9 R7 q% M/ S7 v# Z5 F9 {" I
  9. >>> math.copysign(2,-3)( f: v. d/ g2 `$ i8 ~9 j
  10. -2.0
    7 L9 }4 ^; w1 X9 N
  11. >>> math.copysign(3,8)0 w7 V9 C3 o% M  {
  12. 3.0
    5 f5 l$ h$ K7 d3 w2 h7 X' K
  13. >>> math.copysign(3,-8)
    - U& M, v5 H# M" v" o6 f
  14. -3.0
复制代码
4 r  i- o( \3 ?! j
math.exp(x)  返回math.e,也就是2.71828的x次方
) F5 u" e" d! b( Z
  1. #返回math.e,也就是2.71828的x次方2 ^7 y" ^$ @/ @3 u4 x
  2. exp(x): B" F- M' k! Q% [1 Q
  3. Return e raised to the power of x.) k$ S& [) y# d9 A! J: q6 H
  4. " B( @+ `# w3 I3 Y, u  y( p
  5. >>> math.exp(1)
    ! Q- l% b+ S4 S0 w6 i- S# {& i# d
  6. 2.7182818284590457 f- Q4 N! z, j/ f1 z
  7. >>> math.exp(2)8 W9 v3 o7 M8 s" k. |! W3 z3 M
  8. 7.38905609893065$ E" @0 a1 D% n
  9. >>> math.exp(3)3 V* _9 ]# K( P  x4 ]5 {
  10. 20.085536923187668
复制代码
% a* M) q9 V  z$ T5 k) z
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
. Z+ K: V% T) h) z, w3 B8 X( u9 o
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ! r6 w9 ~5 _0 d1 G8 d; |) [! e' Z
  2. expm1(x)' t. z: j, i6 P$ W" l
  3. Return exp(x)-1.4 q( S8 e2 n4 F
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 M7 X. ~$ i8 i" e$ Y" u9 V& x

  5. * G' ~' V3 K8 P
  6. >>> math.expm1(1)
      D' d' C8 C$ z! e
  7. 1.718281828459045
    . ]% g$ A: w3 }5 C
  8. >>> math.expm1(2)5 [2 S9 P. e7 X& v! e4 x
  9. 6.389056098930658 g! B$ R8 ^! J
  10. >>> math.expm1(3)
    ( Q  u: }. ~6 ^" U
  11. 19.085536923187668
复制代码
4 y% K; v4 t, p% ^' l
math.fabs(x)  返回x的绝对值
2 i; a# {+ I* E6 _( H" [# v
  1. #返回x的绝对值- k1 C- P8 x) V
  2. fabs(x)
    ) u5 ]# k' H: t% g
  3. Return the absolute value of the float x.2 w% u1 @8 B4 Q9 W
  4. , D  \" c/ t# T* D) j
  5. >>> math.fabs(-0.003)% I, q7 i% G" t% m
  6. 0.003
    $ i$ ^* o' \: n& A# ?
  7. >>> math.fabs(-110)
    - y3 x0 a' I8 o! d" p, B6 b2 g8 e
  8. 110.0" X' }, n! F8 S. C& B. Z$ v
  9. >>> math.fabs(100)1 ^1 P9 U! P/ N) @
  10. 100.0
复制代码
0 b- y/ Q! X, z" R8 y' w8 W
math.factorial(x)  取x的阶乘的值  }% N9 J% D0 }9 q4 i5 W* e( S
  1. #取x的阶乘的值
    $ i9 k6 _8 m  S  t
  2. factorial(x) -> Integral
    $ o% \4 k7 B0 f! |; R
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    7 G9 D- V, N& P6 U
  4. >>> math.factorial(1)
    - \0 a! }" Y$ ]3 F
  5. 1* j1 V# r+ U; i$ H3 X
  6. >>> math.factorial(2)* B7 L; A9 q9 R# \7 g
  7. 2$ R* U, O- i: z
  8. >>> math.factorial(3)$ x5 f# J, D7 \$ ?7 Q9 j/ B6 w
  9. 6& c1 a# O& G9 P4 x. f; z
  10. >>> math.factorial(5)
    $ w- [) |8 {# K6 r
  11. 120
    ' ], d; A& ^" x3 j
  12. >>> math.factorial(10)2 ?9 {% i% R; k- f9 N
  13. 3628800
复制代码
% f, d' i2 @3 E! x' k4 n
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数, |; z0 Y" p: Z" d' `
  1. #得到x/y的余数,其值是一个浮点数
    + n9 b6 [8 F+ Q7 R/ {- C2 Y
  2. fmod(x, y)
    % P; h+ L$ x  V7 E! b7 l8 k
  3. Return fmod(x, y), according to platform C.  x % y may differ.' j' o# u# U: w  B+ k
  4. >>> math.fmod(20,3)
    - A* o+ @- {7 }4 r& O" E& g
  5. 2.0$ z+ t! v9 ]1 N* \# Y6 u" K2 b& n
  6. >>> math.fmod(20,7)
    5 p7 y# o! x+ b
  7. 6.0
复制代码
$ H2 J  k0 P. n3 |& k+ h: D& G
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) L5 A) m0 F7 }& O0 ^
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    9 e- ~: }+ }6 X, I
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
      s1 [: I9 D. b- h- {+ G  T
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    / ^% h! e: ]& R( q- N5 v
  4. frexp(x)
    * N8 \! ?$ ~5 S" u7 o
  5. Return the mantissa and exponent of x, as pair (m, e).
    / h5 e% Q. w1 B
  6. m is a float and e is an int, such that x = m * 2.**e." ?  Z: W8 y; W$ q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! _6 q2 s! p3 Y  v
  8. >>> math.frexp(10)
    6 j: q% z3 @; b* ]
  9. (0.625, 4)4 v4 V- t; ~, ^; D& }: ~+ Y
  10. >>> math.frexp(75)7 f8 {8 B8 `/ [: v% y
  11. (0.5859375, 7)) v* R+ n( f7 C% \3 g
  12. >>> math.frexp(-40), K; p1 Z5 m. m8 s7 C/ ^. a1 I! t; G
  13. (-0.625, 6)
    + I4 ~( d; y9 {
  14. >>> math.frexp(-100)
    1 s, m# g! N5 [  ?
  15. (-0.78125, 7)9 Q' [( X- s. j$ r2 U
  16. >>> math.frexp(100)
    3 s( l$ S' ^8 H' ^
  17. (0.78125, 7)
复制代码
8 `: }: e& x3 u; N: k
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
: R' {; X- @, b) b
  1. #对迭代器里的每个元素进行求和操作
    - z! ^; o& D/ ~, h2 I* ^$ _  }5 v
  2. fsum(iterable)1 X  K$ }6 h8 u3 @2 p
  3. Return an accurate floating point sum of values in the iterable.) n' v2 A+ N( F4 m; F8 {+ y
  4. Assumes IEEE-754 floating point arithmetic.! B5 K0 S# x  x4 u/ {' V
  5. >>> math.fsum([1,2,3,4])
    % k8 G$ q" s, ?( c" ^6 Q
  6. 10.03 V; G) I) T2 D8 U, s
  7. >>> math.fsum((1,2,3,4))8 ^5 Z2 s, Z; D) Z
  8. 10.0( w# r: U( e8 W/ p- e9 q9 d7 f
  9. >>> math.fsum((-1,-2,-3,-4))8 _, D2 m0 {9 e/ s  T
  10. -10.0; ~  J" T' {& m/ R3 j6 ]( B
  11. >>> math.fsum([-1,-2,-3,-4])0 ]+ C1 R0 P9 b" {- Y! C$ t+ l
  12. -10.0
复制代码

6 l0 T' l& v5 b6 g2 u! g1 kmath.gcd(x,y)  返回x和y的最大公约数
5 B6 ~& j# o- P  i" f4 ~
  1. #返回x和y的最大公约数
    $ w5 l/ n. T5 W' k/ Z+ n
  2. gcd(x, y) -> int0 ~$ }3 _! u) s: V1 S% R
  3. greatest common divisor of x and y
    9 w+ Z7 C3 i+ c: Y! c
  4. >>> math.gcd(8,6)2 a4 B) j* `7 u3 ]7 W
  5. 24 P8 Y  v1 l6 b) y' a5 |
  6. >>> math.gcd(40,20)
    * C8 F1 X* L: c' E" N
  7. 202 p3 a( i) F+ R. r& D
  8. >>> math.gcd(8,12)
    3 v1 n' v% Z7 G: Z) Q0 `$ l
  9. 4
复制代码
* f$ U# Y- H! N- I& k
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False8 g. f/ W$ ?. M" n+ n1 T
  1. #得到(x**2+y**2),平方的值! u9 m: W# Y6 E. P1 Z5 P& Y! V3 r
  2. hypot(x, y)
    & _3 M; u6 Y# n+ Z
  3. Return the Euclidean distance, sqrt(x*x + y*y).; Q3 {, w& @4 T0 n/ R2 N* h5 ]
  4. >>> math.hypot(3,4)
    , X/ F+ Y% @. m5 J4 l
  5. 5.0
    : {. @4 e( r9 }( u. L; D
  6. >>> math.hypot(6,8)
    * B8 C0 ~0 s: |; ]0 L
  7. 10.0
复制代码
( f/ G! e% K$ Y; Z2 Z
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False- X# Z5 B; n( l/ n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
      C4 C, R" S8 O# y+ P. v1 y) j' B' U* J
  2. isfinite(x) -> bool( j* l* M0 J1 X) a* g* K& \
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    0 L, T2 i* o3 c4 W
  4. >>> math.isfinite(100)
    ' n  M+ y  }! l9 I6 y4 |) T
  5. True2 {1 v7 N. h$ x4 Y. O/ A7 s
  6. >>> math.isfinite(0)
    5 W. D% T: W1 ^3 M% y
  7. True# \2 g* r  V1 p' f' C
  8. >>> math.isfinite(0.1)
    ! \' [0 y/ x& _# H8 p# S
  9. True
    & E2 ]* t" V9 S, n. Q6 \' g
  10. >>> math.isfinite("a")- m+ S! l/ @# u$ b3 v' R2 U3 t& F
  11. >>> math.isfinite(0.0001)
    6 D7 y% q8 }" V
  12. True
复制代码
7 p2 `" e7 u8 @8 q. U7 F
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
' u1 f6 p, C& f% w& o
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False( `& P* J& v+ D& N) E( L* @
  2. isinf(x) -> bool
    " i' I6 B/ ?8 v7 Q4 _: O, F3 \, D  k
  3. Return True if x is a positive or negative infinity, and False otherwise.+ s' R9 G: c8 Z' ?3 C
  4. >>> math.isinf(234)) g- `% @5 f' j9 ?& B. ^" s
  5. False
    ) B8 P, L- v0 {& ]
  6. >>> math.isinf(0.1)8 J* g9 l4 w1 E
  7. False
复制代码

3 y' P5 A) Y2 j5 D# ?4 N0 W" G  Z: `math.isnan(x)  如果x不是数字True,否则返回False& Y6 }# G6 Q7 ]
  1. #如果x不是数字True,否则返回False
    . C% F0 L/ |: q5 K4 f
  2. isnan(x) -> bool
    : B; ^3 P: }5 t( W0 ]* q
  3. Return True if x is a NaN (not a number), and False otherwise.
    1 o8 |% c4 O5 M* P  o3 d( R
  4. >>> math.isnan(23), q! O; [# J# ~
  5. False
    % S% T+ W6 t5 n
  6. >>> math.isnan(0.01)& H+ Q; s3 }  L8 k
  7. False
复制代码

8 g2 |) @! ]2 u% u' e$ Amath.ldexp(x,i)  返回x*(2**i)的值9 ~6 S* \) o$ b0 H* A
  1. #返回x*(2**i)的值
    , z+ B$ p+ P, A$ c  F. f3 j6 ~2 U
  2. ldexp(x, i)
    # ~5 i  X) `) [- n. s! I/ S1 r3 }$ X
  3. Return x * (2**i).
    1 R+ x8 o3 i7 s; p+ u6 E$ y$ ^
  4. >>> math.ldexp(5,5)
    " o" C# F- g. R- g$ _0 b
  5. 160.0
    + D+ a7 E/ s1 n3 X
  6. >>> math.ldexp(3,5)$ a0 P+ M. n  V
  7. 96.0
复制代码
# K5 B$ v! D  w& k4 W$ [4 u
math.log10(x)  返回x的以10为底的对数
" `; H' m1 n* E6 ]; u
  1. #返回x的以10为底的对数5 ^# ?! O  |% o& D
  2. log10(x)
    , o8 N2 g1 N0 T8 I: i
  3. Return the base 10 logarithm of x.& v. @# t, z4 @/ M+ W
  4. >>> math.log10(10)
    . n0 Q9 M+ u$ |4 Z) n7 g
  5. 1.0) Z7 D( z) i6 q# l
  6. >>> math.log10(100)
    9 U' A, {0 O& p( ]2 l
  7. 2.04 I3 ^% g& |0 j8 C2 B- V
  8. #即10的1.3次方的结果为20) `% `  Z5 r: V: j+ T
  9. >>> math.log10(20)
    4 m) l) j' u+ K8 o% T$ Q
  10. 1.3010299956639813
复制代码
# E+ d7 }0 [: S$ d, l9 L
math.log1p(x)  返回x+1的自然对数(基数为e)的值
7 b* P- [; X" ^$ J) l% R
  1. #返回x+1的自然对数(基数为e)的值
    # L' w: o  C, W- M& y
  2. log1p(x)
    ; `- e- [- K+ O
  3. Return the natural logarithm of 1+x (base e).
    1 D; f6 r% s7 ^
  4. The result is computed in a way which is accurate for x near zero.. H' N8 m6 ~. x/ p, ^
  5. >>> math.log(10)
    4 z3 V; a# }% g+ I6 W& M& d
  6. 2.302585092994046
    2 k7 D: P/ P9 M2 W
  7. >>> math.log1p(10)3 I- T( `$ z7 J0 {
  8. 2.3978952727983707
    5 m3 j7 u  L% k
  9. >>> math.log(11)
    ; n6 t; _  {% ?4 l! w
  10. 2.3978952727983707
复制代码

5 `8 h3 f( q& @+ k* i/ Cmath.log2(x)  返回x的基2对数
+ D8 ~0 |3 N2 x, {$ P
  1. #返回x的基2对数/ B$ p6 K1 _0 W! _3 h: O
  2. log2(x)4 r3 y& `7 w2 X4 ^& f$ w% A% q
  3. Return the base 2 logarithm of x.
    ' u" e8 _/ V& x5 P+ `: M2 a7 `
  4. >>> math.log2(32)' \# L' J+ W  S: ^6 L1 s
  5. 5.0
    6 ?" B$ F; `( w! V$ s) f
  6. >>> math.log2(20); |( \+ x6 B  A7 \" R
  7. 4.321928094887363
    , `0 w- l0 W- O6 c6 W( V# |
  8. >>> math.log2(16)
    ) `5 }5 i+ h+ E4 i
  9. 4.0
复制代码
; J5 F3 ~, h$ F& B8 M; G
math.modf(x)  返回由x的小数部分和整数部分组成的元组  P3 Y% Y8 _+ e7 g- }
  1. #返回由x的小数部分和整数部分组成的元组
    ' y8 I1 S( m& W! R& M
  2. modf(x)0 j! g. m& @* p1 C# r
  3. Return the fractional and integer parts of x.  Both results carry the sign9 x. D" R$ ?3 c
  4. of x and are floats.
    " ]" N; m* J; W: q  j
  5. >>> math.modf(math.pi)
    + T- c" F8 }; F5 m$ }9 V% [
  6. (0.14159265358979312, 3.0)
      Z. q- g! o& \  x3 ^. K6 H
  7. >>> math.modf(12.34)
    9 b: [  B- e$ r; W7 A
  8. (0.33999999999999986, 12.0)
复制代码

+ X( G- J0 {) r1 }math.sqrt(x)  求x的平方根; `  R2 P" H9 n+ R
  1. #求x的平方根4 U5 ^- \6 e8 f
  2. sqrt(x)
    * H. e8 @& R8 H( {3 {6 k
  3. Return the square root of x.
    9 }' E1 G1 a. j" w
  4. >>> math.sqrt(100)* d3 W% j3 c1 r6 k5 ^3 a0 f- }
  5. 10.0
    * P6 T, P9 K# {( S+ g
  6. >>> math.sqrt(16)
    9 i" v. k* Z" S6 q- I
  7. 4.0
    * \6 |6 s7 s3 x: o8 H" k& S/ x. \
  8. >>> math.sqrt(20)
    & }4 g3 @9 b' N8 {4 }0 J
  9. 4.47213595499958
复制代码
$ k% a3 @' W& T
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分2 E: B. a9 Z) i- r8 L! M: \! ?
  2. trunc(x:Real) -> Integral
    5 N5 I& L! N8 }+ f( Z+ x& I
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    & T2 d+ o9 y! [* _! o( \4 i) K. @
  4. >>> math.trunc(6.789)& _, v) |- a0 V
  5. 6
    " w% Y: |4 `4 O, }6 l! x. q3 ^
  6. >>> math.trunc(math.pi)
    / r' z* N/ h6 U9 w+ f: U& J* D
  7. 3- _/ p- S+ n* @6 F1 i; j
  8. >>> math.trunc(2.567)9 h8 l0 C) A$ Q/ j* G, j: T) T$ G- ]
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-5 07:19 , Processed in 0.087555 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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