新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

6 ?( N( w  l/ n) R) }【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 h: v/ b( h: z, V7 Q9 O5 p
! m9 N) C. u) ?) D  A. A
方法1
+ a- r6 X, W" J( H+ S2 v
  1. >>> import math
    ) T9 ^: j, v2 G) ~5 l* Q& Z
  2. >>> math.sqrt(9)
    , R9 i6 X# _& G+ t  m+ X% w
  3. 3.0
复制代码
方法20 v  V6 ?! `3 `5 l! W& d
  1. >>> from math import sqrt
    0 z( M8 L0 _0 e
  2. >>> sqrt(9)8 ]8 f# `  W3 Y3 h( A0 |
  3. 3.0
复制代码
9 ?  ?3 x  A" k( r5 ]7 m: a* {

- V2 G/ D( K$ w
math.e  表示一个常量
) P) f) V1 X. Y' l& `2 C3 S
  1. #表示一个常量
    4 E( N8 a' Y- L9 |1 S
  2. >>> math.e
    . `& N9 Y1 U5 G
  3. 2.718281828459045
复制代码

! X' O4 j* R2 b, R4 I' _% ]math.pi  
数字常量,圆周率
0 }" Z: P' b9 S: N
  1. #数字常量,圆周率; q; H0 P' U( ~7 z: Q
  2. >>> print(math.pi)+ s. H7 U7 Y- H  r* W5 C
  3. 3.141592653589793
复制代码

1 v# r4 y0 A/ Xmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

( \6 t; ?) V8 n8 X
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ Y) x$ H* j) f* T; G
  2. ceil(x)
    * `" W. P; s+ t
  3. Return the ceiling of x as an int.0 n0 t' f- |: C  j4 g
  4. This is the smallest integral value >= x.
    1 P7 ]2 @2 }6 W, ?" X! p2 H
  5. 0 O1 ^) B% f$ m0 E
  6. >>> math.ceil(4.01)
    ; [/ A& p, P: G
  7. 5$ p4 S" h7 \9 F' ^: \* z
  8. >>> math.ceil(4.99)! L4 ]/ u! C; [* ~7 \6 ~1 _: @
  9. 5) L) M1 j( B  u: ]
  10. >>> math.ceil(-3.99)
    1 J, |/ o# ^' X% m
  11. -3
    0 b6 _. V, T- C
  12. >>> math.ceil(-3.01)% _6 @. ~9 G# R0 _
  13. -3
复制代码
5 b( x& u$ Q3 a. k: P. n
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身; f! u, R" Q, y9 w
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    . |" G8 S2 l1 O' `+ @# {
  2. floor(x)
    : x% l  `' q) Z6 I. t2 R
  3. Return the floor of x as an int.
    & [" [1 N* u, \& z9 Q' f# Z
  4. This is the largest integral value <= x.+ U4 K- v5 k/ m6 f
  5. >>> math.floor(4.1)' n( v0 H" P6 [1 j
  6. 4
    . _. T6 {3 l& m& S
  7. >>> math.floor(4.999)7 ]* W% ~$ R) `5 L: \, B% S. W0 [
  8. 41 |8 ~. M* f# \# b
  9. >>> math.floor(-4.999)- ]/ X( Q4 Q! v9 |) u: |! _. ~+ O  i
  10. -5
    , m# I% A+ `( u2 `1 a7 `
  11. >>> math.floor(-4.01)4 m* @6 t# S+ C. S
  12. -5
复制代码
. t1 S+ N: y# F! ?, O2 k* B; E& L
math.pow(x,y)  返回x的y次方,即x**y) L1 Y6 }! o: s8 x% h  `8 t9 W
  1. #返回x的y次方,即x**y
    " W% @3 |. H; D5 ?$ J. M* @* _% `
  2. pow(x, y)
    5 @! U" k; n% \9 t# [' T
  3. Return x**y (x to the power of y).& B/ R3 Y9 U' @1 ]
  4. >>> math.pow(3,4)& X; {$ z- a, ~( G  U8 h
  5. 81.0" U( ^- e  C$ P
  6. >>> 4 R* w  F7 R% S1 j7 U1 W
  7. >>> math.pow(2,7)
    $ r0 v( k: }0 C- n( s) F
  8. 128.0
复制代码

  Q: M6 i- I: Q8 ]8 F7 Cmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)4 M1 e, \, i: S
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 _0 r- m7 ^$ S8 T. \. B; i
  2. log(x[, base])+ o: _2 F+ W3 `6 q# w8 ]. Y, r
  3. Return the logarithm of x to the given base.
    ' }) p7 c- C. c8 }8 ^; k
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ! Z9 r! O) C9 H) x7 _( C
  5. >>> math.log(10)
    9 G& b/ ~  Y* D& T
  6. 2.302585092994046% A1 z( C* W: s, }
  7. >>> math.log(11)
    9 B* Q6 Z4 I3 W9 `/ b
  8. 2.39789527279837075 P2 n  a7 g$ d8 ]: O( }; Q
  9. >>> math.log(20)
    " \) o* o; C+ J/ g
  10. 2.995732273553991
复制代码

; q' a) b) S: g0 o: e* a% nmath.sin(x)  求x(x为弧度)的正弦值
7 S( Y, E- A3 B2 K
  1. #求x(x为弧度)的正弦值. j/ r6 o3 A9 T) E4 _- y& I
  2. sin(x)
    ' c5 n( ~! T8 s. S/ I
  3. Return the sine of x (measured in radians).' r: V. E! P: p/ D
  4. >>> math.sin(math.pi/4)% \  T/ A$ V" y7 S% Z1 @' t
  5. 0.7071067811865475
    6 d9 y# V- x1 N$ p$ e" G! ~
  6. >>> math.sin(math.pi/2)# N$ ^) ^# P( c, h' Z- a; H$ r1 P
  7. 1.0( x$ X& M1 ?; B+ ^. z
  8. >>> math.sin(math.pi/3)% @1 i$ N% l, k# I- m' ?# h
  9. 0.8660254037844386
复制代码
) ?& W/ Y- i! ?' B# ?9 ~
math.cos(x)  求x的余弦,x必须是弧度
/ R/ Z! Y( q( Z1 e2 }( A
  1. #求x的余弦,x必须是弧度
    + S) Z6 H. O' D% I: _
  2. cos(x)
    / [6 c1 |( i8 ~! E: F$ b7 T
  3. Return the cosine of x (measured in radians).
    : ^, ]) G6 H, @. T8 h) Y
  4. #math.pi/4表示弧度,转换成角度为45度- p/ e- q2 R2 X4 n
  5. >>> math.cos(math.pi/4)
    6 H$ A* p1 G9 k# l4 b! h- d
  6. 0.70710678118654764 w& Q5 W, W) i: n* ^5 p% t
  7. math.pi/3表示弧度,转换成角度为60度
    ( x7 N$ s  ?3 J
  8. >>> math.cos(math.pi/3)3 w7 ^2 k$ N. r9 M  V$ H
  9. 0.50000000000000012 B- S+ K2 u+ E) j& R
  10. math.pi/6表示弧度,转换成角度为30度
    - ?* E* Q0 Z" ?" W
  11. >>> math.cos(math.pi/6)
    / @& t/ c* \/ A/ H" d5 B& I
  12. 0.8660254037844387
复制代码
' N7 |9 k% l& `. B& p$ m
math.tan(x)  返回x(x为弧度)的正切值
( F8 S& ~' L0 Z+ C: H. ~* l
  1. #返回x(x为弧度)的正切值$ @0 \* A) u, X$ o' `5 ^
  2. tan(x)
    $ c2 R1 U" c8 ]& ?+ E9 m
  3. Return the tangent of x (measured in radians).8 j+ U7 X" @6 n7 T' }4 Y
  4. >>> math.tan(math.pi/4)
    0 E" N9 I/ l) t& @
  5. 0.9999999999999999
    + U2 V5 @; N0 S! t0 b
  6. >>> math.tan(math.pi/6)
    $ `* P9 L" r( _) g% h6 J- t' g$ L
  7. 0.5773502691896257
    # S+ R9 w) o+ d1 m; k+ E+ p
  8. >>> math.tan(math.pi/3)9 O  ^  p) d: V) `
  9. 1.7320508075688767
复制代码

/ k& m3 {8 g$ }  imath.degrees(x)  把x从弧度转换成角度. w7 e# Z- e" a2 j2 I# L
  1. #把x从弧度转换成角度
    & \3 h! v- c! G# e  F! B* G4 d
  2. degrees(x)
    ) s; ~' F( }7 K4 b7 C! u3 X
  3. Convert angle x from radians to degrees.+ d6 ]6 Y. A; r. [1 f# n( d

  4. 6 ]5 V2 [" X: f! }4 v# n! {
  5. >>> math.degrees(math.pi/4): g! Q, @/ x# g, e, Q+ c
  6. 45.0
    ( K0 r  @. f- d9 d3 Q9 q
  7. >>> math.degrees(math.pi)
    ' P$ P& M% m, M0 c$ W
  8. 180.0
    ; D2 c0 C2 d! e$ b: Q5 O' g
  9. >>> math.degrees(math.pi/6)( g9 A) O+ W# G0 P
  10. 29.999999999999996
    " O" e& e3 X( f# m9 m3 N2 p& m6 e
  11. >>> math.degrees(math.pi/3)  |$ \1 ?1 e7 N5 ~, F$ s
  12. 59.99999999999999
复制代码
$ b; B6 K5 H- A. j  Y
math.radians(x)  把角度x转换成弧度& Q1 \4 @5 _' }: k$ b' m0 ^- m8 R
  1. #把角度x转换成弧度# _+ D* ~/ {7 j; p6 [
  2. radians(x)
    / v( S5 L! F0 i, j
  3. Convert angle x from degrees to radians.
      g& C3 U3 h: h1 ?3 E
  4. >>> math.radians(45)+ s6 E! X" d$ u1 V
  5. 0.7853981633974483& _0 n3 {5 A  i, `
  6. >>> math.radians(60)
    / `7 G( D1 o/ B9 k
  7. 1.0471975511965976
复制代码
+ D0 ^8 z' m. _, N
math.copysign(x,y)  把y的正负号加到x前面,可以使用0  n: a! d! i* U
  1. #把y的正负号加到x前面,可以使用0
      R  ^8 j( z# y$ v" Z7 N
  2. copysign(x, y). p( |! k0 N: ~' ~3 S8 l* X
  3. Return a float with the magnitude (absolute value) of x but the sign
    # }  K. x% f; v3 I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ; k7 _. \  k# R2 r! y
  5. returns -1.0.
    , y# X( W) [1 r5 \+ c% H. }
  6. ( h$ G  I+ M* V
  7. >>> math.copysign(2,3)# i9 U$ I4 J& d4 w
  8. 2.0" ~% R- O( R# U5 Y
  9. >>> math.copysign(2,-3)0 V. D2 K1 ]$ l
  10. -2.0: o( K$ S/ i; v5 J* M
  11. >>> math.copysign(3,8)$ \$ Z- x' Q' y6 K
  12. 3.0
    % t- |# Y5 V" _- f6 ]2 [" ~
  13. >>> math.copysign(3,-8)
    # y5 _+ P/ F) v5 f! y
  14. -3.0
复制代码
+ f- c( x, Z( g. |7 g
math.exp(x)  返回math.e,也就是2.71828的x次方
+ V, l9 o- Z$ c9 y- w  \3 z1 G
  1. #返回math.e,也就是2.71828的x次方% W* o" L5 u/ Y. P5 }9 Z
  2. exp(x)+ I0 U& @. M8 o% M; \3 F
  3. Return e raised to the power of x.. f8 `- @) y1 \  R; C

  4. 5 p( c) L& O8 p6 J
  5. >>> math.exp(1): ^! k) M" i1 f( W8 Z0 e
  6. 2.718281828459045
    # T1 S! w. ~: G- r9 w# K2 F. Y
  7. >>> math.exp(2)2 t5 X2 M7 F# |. e! g& n
  8. 7.38905609893065
    : F+ m- k8 [! I" d' t
  9. >>> math.exp(3)
    5 n' ]2 S6 a4 m; Y: }
  10. 20.085536923187668
复制代码

: U7 Q5 U6 {2 k$ n; m. qmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1; {# l. Y  ]% |8 W; @9 D
  1. #返回math.e的x(其值为2.71828)次方的值减1
    . ~3 Y8 k/ U' D+ B, i1 ^
  2. expm1(x)
    7 J3 p6 y4 ^/ }1 c% p! j2 v
  3. Return exp(x)-1.
    6 N1 c' G8 L1 W
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 L3 Q7 c" D( \% F5 U5 u3 d

  5. % Z! I4 Z0 G. h9 f, a
  6. >>> math.expm1(1)! p- s$ T" E; E/ p2 f+ l
  7. 1.7182818284590454 x$ |0 {- `: I7 Y
  8. >>> math.expm1(2); I' q6 r5 o4 @
  9. 6.38905609893065
    5 ~0 Z: }8 c0 [% L; a! R+ j+ L
  10. >>> math.expm1(3)3 }+ O* l, r, K$ x, Q9 F$ ^
  11. 19.085536923187668
复制代码

5 G3 ]+ N& y& I) R/ {math.fabs(x)  返回x的绝对值
+ n6 V5 O0 x& E4 u) _. V
  1. #返回x的绝对值( \* `$ b, M; Z: S
  2. fabs(x)6 D9 u$ _2 ^: c  y# t
  3. Return the absolute value of the float x.
    6 E: y$ ]( Y6 T

  4. $ _  d1 ^; [+ A+ n# |
  5. >>> math.fabs(-0.003)
    4 T4 p' ~1 c# y) o  x. j0 @
  6. 0.003
    0 r% q6 m$ z; B# s- S
  7. >>> math.fabs(-110)0 c9 {, j8 J5 k  V9 u& s/ N
  8. 110.04 W, B! j0 E2 U* \- W
  9. >>> math.fabs(100)
    & q* l1 b: y$ |* m
  10. 100.0
复制代码
! s. w( I* y& e0 J6 E1 o5 O
math.factorial(x)  取x的阶乘的值8 [# p$ w" d) v* z- r- X8 R
  1. #取x的阶乘的值, j4 P+ s% s& F- d! v) U% j9 D
  2. factorial(x) -> Integral1 G+ L, z8 p2 \. Z
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; z( c- B; y7 G" G# U
  4. >>> math.factorial(1)
    ( x1 v+ o1 D# h# t4 d  a' r& z
  5. 1
    7 ~' E% K6 I) H( @7 n3 M0 x( U
  6. >>> math.factorial(2)
    , i- {8 u: a5 u2 x) L: w+ F9 L
  7. 24 o% ^, p" n- ^
  8. >>> math.factorial(3). W5 q  Q* y* v& }
  9. 6( Z  b: Z( F7 u
  10. >>> math.factorial(5), B' Y8 M" k/ x; }$ e2 G# S! q
  11. 120
    , d& H1 g2 I7 Q# j9 r3 l. c4 y
  12. >>> math.factorial(10)
    $ f: Z6 K" W: B- |. |! B: ?
  13. 3628800
复制代码

1 I1 ^, h* G" c: [math.fmod(x,y)  得到x/y的余数,其值是一个浮点数" T. M! C  ?3 G. ~
  1. #得到x/y的余数,其值是一个浮点数! v9 `+ `0 s% L4 H+ {% }" v% t) ?% s
  2. fmod(x, y): Z+ D0 g- r: ]0 {! |
  3. Return fmod(x, y), according to platform C.  x % y may differ.9 a0 a6 x- R6 W& v) ]
  4. >>> math.fmod(20,3)
    ; r; G0 K4 a6 C
  5. 2.0
    ( n: G, s" k) m; }
  6. >>> math.fmod(20,7)
    1 j' U7 T8 k$ `  I' G+ @# T- u- _0 `3 }
  7. 6.0
复制代码
3 \( w4 W7 w! i
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
) J" B  [7 |  Z5 i- c, F4 a
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
      |) O' u- K- q$ W2 t4 u  a
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 [9 t$ F, Q3 k: i+ Q7 P& S
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    , t/ @9 A# n$ |  |8 b$ p
  4. frexp(x)' |$ [! \% l1 A! K  W  A
  5. Return the mantissa and exponent of x, as pair (m, e).
    , N9 X& j6 W/ }. q! O
  6. m is a float and e is an int, such that x = m * 2.**e.4 _3 m4 U' B$ m6 s; g1 p
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.5 B' Z' j1 e( O, @1 T; o
  8. >>> math.frexp(10)4 h7 R0 W3 a6 c3 `7 E
  9. (0.625, 4)
    0 z7 P, a' Y6 ^
  10. >>> math.frexp(75)6 i' f/ c/ E' m8 j% q
  11. (0.5859375, 7)" M; Y: x9 W  k, d9 w% {* K" e
  12. >>> math.frexp(-40)2 H  v7 _0 l0 a, L6 s; ?
  13. (-0.625, 6)' s$ K6 O1 B1 l9 o  [
  14. >>> math.frexp(-100)
    * l0 x" U4 k) i+ x5 w: E" Q! D
  15. (-0.78125, 7)
    8 U1 C8 g& g  Z  o/ P5 N8 D& k
  16. >>> math.frexp(100)
    & H7 a+ g0 H) ]% L
  17. (0.78125, 7)
复制代码
5 M% |- k) D, b
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
7 f) [$ s8 P$ L) D: N* M
  1. #对迭代器里的每个元素进行求和操作$ @! R9 x$ c4 v1 G) f
  2. fsum(iterable)
    # _6 w4 h, X4 H
  3. Return an accurate floating point sum of values in the iterable.' B& s4 ~. o6 }0 C
  4. Assumes IEEE-754 floating point arithmetic.
    ! J3 W: @$ A( K7 _# v
  5. >>> math.fsum([1,2,3,4]). D8 Y5 ^* [# q$ P+ s, ^$ h
  6. 10.0
    : @; [1 ]* x0 w- ^/ u  L# U
  7. >>> math.fsum((1,2,3,4))
    9 T' B: j8 \% T5 O
  8. 10.0! z. T0 ^8 B, {) X* T
  9. >>> math.fsum((-1,-2,-3,-4))0 ]0 P  r7 b0 c9 g
  10. -10.0. L% ?9 F* c7 @
  11. >>> math.fsum([-1,-2,-3,-4])& I2 ^. [) a9 ~+ f
  12. -10.0
复制代码
/ g9 r% o* v/ i/ S# q$ X& i+ [9 G, \
math.gcd(x,y)  返回x和y的最大公约数5 o8 Q( g% _" N% F/ K
  1. #返回x和y的最大公约数5 [) K  S4 P* s; O8 j6 r, ?7 O
  2. gcd(x, y) -> int: Z9 n! ]7 W+ U2 q! C
  3. greatest common divisor of x and y
    ) g8 r6 M! Z  o! P' w3 M
  4. >>> math.gcd(8,6)
    6 E  V  u' h9 p/ O4 Q" V- m" X) G
  5. 2: W& L) O8 h, P& }2 r; p
  6. >>> math.gcd(40,20)
    7 O* R! z# [: P6 Z$ \
  7. 20
    0 k) m( y1 t+ Z) h5 H
  8. >>> math.gcd(8,12)
    6 W0 h1 n: I. a$ I: i9 K
  9. 4
复制代码
! R# p2 M" o4 b  g1 G: B, b
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False" p) H! A7 g% o% V# q! b
  1. #得到(x**2+y**2),平方的值
    2 O% K* s' g& I# E
  2. hypot(x, y)
    ; c& A# V2 P; r& [- z: S$ \5 B
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    8 Q  K- J, Z% n; s0 n* U
  4. >>> math.hypot(3,4)8 t9 V/ I, r9 F% x; \- @
  5. 5.0! k% P0 Y. u+ L/ f9 a- ]
  6. >>> math.hypot(6,8)
    1 z: T7 k/ B) F8 [9 o
  7. 10.0
复制代码
& ]: G, d: v8 ]9 I' Q# H' e. C/ a/ a
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False% j& ~! t9 p0 ^9 a  b/ I7 M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    2 T4 K% x0 Q8 r8 Q- z! O. f
  2. isfinite(x) -> bool
    " P2 M; @& t' {, t; U  J% W. P
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.( M) x+ s0 k% f* A1 |
  4. >>> math.isfinite(100)5 r- t' V9 U2 V. A" I6 U7 ~
  5. True
    % n8 S' s, @5 i+ k+ ?1 L% ]
  6. >>> math.isfinite(0)+ Q/ ~2 C  M- ~3 ?4 `2 h, T8 Z
  7. True
    & d3 w  N) \1 W3 N. ^1 z/ ~+ y' [
  8. >>> math.isfinite(0.1)2 w+ G" e- t4 M5 B
  9. True
    % R2 U2 |' }  B" ?+ C. N: S4 v& i
  10. >>> math.isfinite("a")
    ; Y6 h3 E4 g! y/ \# L7 q
  11. >>> math.isfinite(0.0001)4 Q6 t* s* e- q1 T7 G$ m- b; r
  12. True
复制代码

- D9 M+ e1 q& t8 I# v9 r) Umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False% g; x, _! D' ~! ^; `* l1 A- o
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False- H- f9 D% F8 x+ [& R
  2. isinf(x) -> bool
    - ?: B, `$ z: z( v  @4 _, e
  3. Return True if x is a positive or negative infinity, and False otherwise.
    . i6 m0 B; @; A+ O4 u
  4. >>> math.isinf(234)
      b6 `7 {- H8 ]( h$ x
  5. False
    5 C; q3 [" H2 M; d1 S; @
  6. >>> math.isinf(0.1)
    ( }4 f- L$ Y+ D' D5 a: G
  7. False
复制代码
4 w4 d% p/ ?% V( I. w/ `
math.isnan(x)  如果x不是数字True,否则返回False
6 R. n, b+ ~1 k9 f
  1. #如果x不是数字True,否则返回False6 s; G) J0 d4 X5 W
  2. isnan(x) -> bool
    ; e4 K7 D/ y0 J
  3. Return True if x is a NaN (not a number), and False otherwise.
    9 z" r4 [4 I" S) b- R+ d' z
  4. >>> math.isnan(23)
    ' n1 `# M4 `2 k" k! {! U/ J
  5. False! \4 ]: y# B9 U0 g9 _
  6. >>> math.isnan(0.01)8 U, q" X7 c% v
  7. False
复制代码

- T% x/ p( j% a8 y1 rmath.ldexp(x,i)  返回x*(2**i)的值$ |0 a8 A; T' [% S& ~
  1. #返回x*(2**i)的值- _) `7 N9 ?1 G8 s8 Z1 K7 s6 d
  2. ldexp(x, i), N7 d& D( S3 K# x, p" E3 A
  3. Return x * (2**i).
      q; h& v5 @( A4 Z( P# G
  4. >>> math.ldexp(5,5)
    ( |7 d5 |6 @: [1 [
  5. 160.0
    1 a% e( S; l( W' n4 E6 ~, N, Z1 i% P; N
  6. >>> math.ldexp(3,5)
    8 Y3 j. b* K# ]- Q" a* Q  C
  7. 96.0
复制代码

! b' Q1 u% C/ e) @6 F' x5 v( m* ^math.log10(x)  返回x的以10为底的对数
* T: R$ H/ P: e  }* @
  1. #返回x的以10为底的对数
    8 V- g, g; {( g+ e3 W
  2. log10(x)
    # b& X1 ^0 z. x7 |* r3 R+ B' x# W
  3. Return the base 10 logarithm of x." G" p+ K, g! z
  4. >>> math.log10(10)
    / Z) @& F) D9 k8 w
  5. 1.0' d7 @" D- C6 Y: C9 K
  6. >>> math.log10(100)
    ) S9 t! G- C5 i$ X) J
  7. 2.0& I" L; ]* M. h/ I+ y! |
  8. #即10的1.3次方的结果为20+ V3 F$ y7 t; t- l$ S
  9. >>> math.log10(20)
    2 ]/ g& X. ~' J, B
  10. 1.3010299956639813
复制代码
: T! g( w& q0 w8 D& X5 L2 z
math.log1p(x)  返回x+1的自然对数(基数为e)的值& J& R! V* }2 b( N" u: i: Z/ o
  1. #返回x+1的自然对数(基数为e)的值
    ' U6 X0 q! X  D- d) g
  2. log1p(x)7 ~+ s* H% J- ?6 x" @  C2 g
  3. Return the natural logarithm of 1+x (base e).
    & E: H- U4 t# S- I& W3 k8 L6 Q/ ^
  4. The result is computed in a way which is accurate for x near zero.
    + Q) [; t7 f2 ]; b% q# Q' ~) ?" O
  5. >>> math.log(10)
    7 _: E' e0 z. q$ M; t# b! i
  6. 2.302585092994046
    # T, K! P( Q( l6 q
  7. >>> math.log1p(10)7 n( e- K4 w5 Z7 _" @7 k
  8. 2.3978952727983707
    / A, a$ _9 w6 q- ~3 J1 E( d
  9. >>> math.log(11)
    - d" E; J* r' R
  10. 2.3978952727983707
复制代码

4 b- W3 o; r$ _" Xmath.log2(x)  返回x的基2对数
( Q& D' ?$ D! x; Z& V0 q0 K
  1. #返回x的基2对数
    ! j( v. n# C, g4 C$ k6 o: e. ]
  2. log2(x)
    4 x. U! t& x, p* c/ G- c# \
  3. Return the base 2 logarithm of x.9 ?/ c; M6 a$ M! d# e( s+ D
  4. >>> math.log2(32)
    . R" k5 w" X) o% |
  5. 5.0: F' N2 A; o4 |. [4 K3 P2 ?/ h2 _
  6. >>> math.log2(20)$ l; K' ]0 I7 ]8 p5 v; J
  7. 4.321928094887363
    2 Y2 a1 r( Y* N* q
  8. >>> math.log2(16), h# A1 `& \3 C8 X. K
  9. 4.0
复制代码
9 b, |( B3 I# B4 B4 B
math.modf(x)  返回由x的小数部分和整数部分组成的元组
4 y: n2 ]9 N; z! g  P2 D4 O% d
  1. #返回由x的小数部分和整数部分组成的元组0 w0 H0 a* E/ N) C5 f% K! N
  2. modf(x)
    ; w" v  O1 m3 N. u9 A9 Q' C# N
  3. Return the fractional and integer parts of x.  Both results carry the sign/ b. Y* X% t3 k7 v) q
  4. of x and are floats.
    - r* T( u3 B$ o. |
  5. >>> math.modf(math.pi); m) w! D# n- ^5 c
  6. (0.14159265358979312, 3.0)+ K( I) t' U/ B+ [8 Q
  7. >>> math.modf(12.34)+ r! k5 j3 k# H  o
  8. (0.33999999999999986, 12.0)
复制代码

! ]$ }; t. J: ^+ z- A, ]# B' umath.sqrt(x)  求x的平方根
# n2 o, k- k' a# {  P$ n
  1. #求x的平方根
    4 ^9 l2 K  ?6 l) {% a0 d& l9 j+ f
  2. sqrt(x)
    # L) o2 U" Y1 C& c, F, y
  3. Return the square root of x.
    $ J& e$ }7 w3 ?
  4. >>> math.sqrt(100)5 m9 M: K; m. e; ^
  5. 10.0
    / h3 D! t* S! L3 {& ~- {
  6. >>> math.sqrt(16)
    : ~, x+ _) S7 l/ C$ Y
  7. 4.0
      r6 L7 v4 G  g, E
  8. >>> math.sqrt(20). S, v5 Z) \9 H  k
  9. 4.47213595499958
复制代码

6 b( H! P+ M- S% M" `+ ^4 w/ e4 v& smath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分% |1 x( L& p, U
  2. trunc(x:Real) -> Integral
    : s+ C( b+ y. v5 D
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* M: ~6 j  g0 @$ H6 @9 e
  4. >>> math.trunc(6.789)8 M- x* c/ n2 z4 @1 m' X
  5. 6! ?& t4 q4 Y: f0 j
  6. >>> math.trunc(math.pi)
    ' W2 c8 B/ L# g3 k$ P4 n
  7. 3
    9 y2 x4 |  B9 C; v/ f6 E
  8. >>> math.trunc(2.567)/ g$ d0 a+ f+ b& f$ D: q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-20 06:38 , Processed in 0.074915 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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