新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
! _' K4 Q3 M4 b& y+ P
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 `0 i1 I6 N9 z

1 n; e  L( }4 a9 @* a方法1; d7 V& ~- ?4 @8 w8 @
  1. >>> import math
    9 L( C/ ^2 G/ L8 y
  2. >>> math.sqrt(9)8 s8 Q% s; P* c  S5 x
  3. 3.0
复制代码
方法2# ?! G+ A1 ~6 s# l/ z
  1. >>> from math import sqrt+ i1 F/ N( R" E: H7 S/ C( N
  2. >>> sqrt(9)! p; C! A- |8 A. |4 v" E
  3. 3.0
复制代码

' o3 O- _# l* W3 V3 R! T& x

1 k; Q5 Q4 H4 e; Y1 ?
math.e  表示一个常量
2 c" m7 u  ]  i" X! n. C" k
  1. #表示一个常量4 ~* m$ O8 u' ?, K; A$ c0 v
  2. >>> math.e
    ' g! P6 y# m$ p+ ~( Z/ I! `$ u' V. h
  3. 2.718281828459045
复制代码
" L& |' E0 L3 q2 F0 U
math.pi  
数字常量,圆周率

+ V, x; }) G0 A0 d( N
  1. #数字常量,圆周率% O( O) |$ ?9 {4 E% q
  2. >>> print(math.pi)
      e# c6 X8 W0 ]7 P4 U+ B
  3. 3.141592653589793
复制代码
( x2 w$ z% n$ |8 ^+ p" Z: Z
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

! F) l( m/ z+ q2 G& T$ O
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x- z5 |  o9 W/ z# q1 v0 I/ t
  2. ceil(x)- M1 b8 g% W0 o/ ^
  3. Return the ceiling of x as an int.
    + ]- K; K  e& ^* K3 Y% Z
  4. This is the smallest integral value >= x., _! y8 R2 }) N" i  b; ~

  5. # c2 w' K9 v3 g3 G2 y
  6. >>> math.ceil(4.01)6 K/ r4 C* u9 O7 r
  7. 5: \# H5 F2 Z2 U2 r3 d
  8. >>> math.ceil(4.99)3 r: A1 e3 m* Z4 _5 i, x* Q+ s
  9. 5$ D6 i6 }2 l* E
  10. >>> math.ceil(-3.99); f" Q2 m2 X( b: w9 D$ G' r* M; R
  11. -3
    3 Z- l3 e$ }! z+ L/ _$ ~8 }
  12. >>> math.ceil(-3.01)
    & D) m% X! t& O
  13. -3
复制代码
* S3 A, D7 S% L/ {
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" o7 Z0 k: v) O0 D! U
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身* P# d' w2 T0 J+ {3 V
  2. floor(x)4 G2 {( u7 L, b! \& C0 x
  3. Return the floor of x as an int.
    " u* ~! N" o3 b
  4. This is the largest integral value <= x.
    # O6 k) P- P: o
  5. >>> math.floor(4.1)
    . G7 ?7 g! a8 P7 I1 z
  6. 48 D# S4 B/ B# }  P; C3 ~/ A
  7. >>> math.floor(4.999)- u- Q4 M3 D1 x; e/ w
  8. 4
    3 S0 t' t7 ]# S" Y
  9. >>> math.floor(-4.999). a2 Y! o# M, G
  10. -5- Y4 G6 t: i. X/ ]1 E, `: C
  11. >>> math.floor(-4.01)
    : C' x9 [% o$ k
  12. -5
复制代码

* I5 p1 x/ H7 Nmath.pow(x,y)  返回x的y次方,即x**y3 _' H& A+ Q$ [/ p, N
  1. #返回x的y次方,即x**y
    9 ]* S8 b, |* q/ R. O4 {* X9 F1 A4 G
  2. pow(x, y)
    1 F8 j4 d- `3 U- {) i
  3. Return x**y (x to the power of y).: F  B' P3 v- R7 w8 f4 ]! }; M6 g
  4. >>> math.pow(3,4)
    3 n) t$ j% S* q5 E5 r- V
  5. 81.0
    9 Q3 i4 b2 U$ {& [* r7 H
  6. >>> / E8 S) s' w1 q+ e: R1 |! m
  7. >>> math.pow(2,7). x  N; r9 G8 `
  8. 128.0
复制代码
8 Y, I3 V9 m) _. ], a. I
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% U7 ~) Q9 U: v
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* _6 n. z) A( n
  2. log(x[, base])$ h0 j5 n/ ?4 @2 z0 L; k$ t
  3. Return the logarithm of x to the given base., P4 c. n) b( r/ Y6 `: @- W( j
  4. If the base not specified, returns the natural logarithm (base e) of x.
    / C$ }3 n* `% O& E* D3 L! q- g
  5. >>> math.log(10)/ S! f2 d3 c) I9 T6 {1 v; q3 ^
  6. 2.302585092994046' F4 V7 G7 a6 e0 @5 t6 }8 b
  7. >>> math.log(11)9 J& u+ ]/ d0 S0 n
  8. 2.3978952727983707" q: I& S, u. J. b' r& _5 Q6 |$ j
  9. >>> math.log(20)4 V. a- O* s9 p4 T2 F
  10. 2.995732273553991
复制代码
5 y$ K  O# u7 j5 k: x
math.sin(x)  求x(x为弧度)的正弦值
1 N/ @+ s$ b+ j8 G$ E6 ^7 d
  1. #求x(x为弧度)的正弦值- Q  O9 C4 H0 X
  2. sin(x); j' ^4 q! u9 w; A' W. A
  3. Return the sine of x (measured in radians)./ U5 M3 v$ D& k- F* c- L0 g$ W
  4. >>> math.sin(math.pi/4)) q7 _7 }- N0 U+ i5 m1 H# J
  5. 0.7071067811865475
    & s& D9 V* \( W4 h' l; g
  6. >>> math.sin(math.pi/2)  w1 ^& L- n. Z& F/ \0 B
  7. 1.0
    9 b3 _( `! D! _7 l2 ^: R7 u
  8. >>> math.sin(math.pi/3)* o7 `0 G6 k& B- E: K5 M
  9. 0.8660254037844386
复制代码

$ d: w5 u6 E& \! cmath.cos(x)  求x的余弦,x必须是弧度
3 c% P; Z& q; Y0 J9 I" w6 O# A
  1. #求x的余弦,x必须是弧度: x& R8 ]6 E1 v4 Q1 \
  2. cos(x)
    ' z1 `: p- Y" `; \1 p
  3. Return the cosine of x (measured in radians).
    7 |. A' n4 X2 v2 ?3 K
  4. #math.pi/4表示弧度,转换成角度为45度) B7 {2 c; L" K- w" r
  5. >>> math.cos(math.pi/4)) Z4 [7 Y2 N7 F; v, K
  6. 0.7071067811865476
    % S  p' \3 X. d) U$ l8 F
  7. math.pi/3表示弧度,转换成角度为60度  _% @4 ^, E8 S& E# |! ?
  8. >>> math.cos(math.pi/3)" p5 s6 G  k% I8 W7 O4 U
  9. 0.5000000000000001) `1 d1 k; n; Y
  10. math.pi/6表示弧度,转换成角度为30度& L  D" v8 _+ a& H4 R  h$ n/ C
  11. >>> math.cos(math.pi/6)* Y2 a' I' ?7 \" t- S7 }
  12. 0.8660254037844387
复制代码
, y& g! M; D2 w
math.tan(x)  返回x(x为弧度)的正切值3 D3 c% ]) D. o) R: l
  1. #返回x(x为弧度)的正切值% m: e' W7 e7 |8 ?
  2. tan(x)9 ?+ V  u' ^! _
  3. Return the tangent of x (measured in radians).
    $ ^7 Q; f5 {2 h
  4. >>> math.tan(math.pi/4)
    ; e# j, J# }3 U: O8 _$ A* p
  5. 0.9999999999999999
    % [7 G& L0 Y; s" E
  6. >>> math.tan(math.pi/6)5 `1 t9 [' }% R3 t" J9 j
  7. 0.5773502691896257
    & V+ j9 s0 b. h  Q3 a% h: m
  8. >>> math.tan(math.pi/3); R4 S/ K/ p5 ?) b$ n! B8 w
  9. 1.7320508075688767
复制代码

. i2 N" {9 r! @3 ]" T9 }  }9 s! Hmath.degrees(x)  把x从弧度转换成角度' A' r; l2 D2 b
  1. #把x从弧度转换成角度
    + l, ^8 }; ]+ ]* U
  2. degrees(x)8 d6 O. t4 L0 L) N0 d- D% U
  3. Convert angle x from radians to degrees.
    5 Y3 i  U6 d! X; f

  4. # I5 T% j: i$ g$ m
  5. >>> math.degrees(math.pi/4)" X% r& p0 A. x7 ?! }" u3 B: T& d
  6. 45.0
    6 u2 u$ H# N8 A& H' c
  7. >>> math.degrees(math.pi); {6 p( R' \  L' j3 ]! Z
  8. 180.09 n4 J; x6 a$ q0 F2 I5 N
  9. >>> math.degrees(math.pi/6)
    $ O# w; `8 y; k+ @" U$ T2 {
  10. 29.999999999999996
    2 D0 D" G! T+ j: r
  11. >>> math.degrees(math.pi/3): x# _' \) ]( Q  F# H
  12. 59.99999999999999
复制代码

; L3 h  j) H$ d3 ?) mmath.radians(x)  把角度x转换成弧度& q% [1 m, o1 s
  1. #把角度x转换成弧度
    2 F" s1 a$ C+ @- L& H6 G0 b
  2. radians(x)
    & @; T% F& P# H4 `8 \) ]5 V7 v1 P
  3. Convert angle x from degrees to radians.0 `( H+ |1 ~8 Z  x/ N. m$ K! t. E
  4. >>> math.radians(45)8 d( r7 V9 C7 h4 |" f1 \6 c. Y0 P
  5. 0.78539816339744836 c( h8 B& j& K0 ^
  6. >>> math.radians(60)# I+ B" }1 B- k; F; j5 @
  7. 1.0471975511965976
复制代码

. }3 k) ?8 I1 Hmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
" v- P  h2 ]1 A: L
  1. #把y的正负号加到x前面,可以使用0
    # z! E$ X$ K2 q
  2. copysign(x, y)
    # ]9 v" G: R! T8 l
  3. Return a float with the magnitude (absolute value) of x but the sign $ F7 G& i+ w; C5 k. k0 [
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ; A1 q; L$ q# n0 j0 m
  5. returns -1.0.
    6 G9 b3 c1 ~+ b( F" I0 P* P

  6.   y7 [- n  \8 |8 X+ O
  7. >>> math.copysign(2,3)! L6 d* Q8 e2 g4 o
  8. 2.0& S6 a& B/ d8 i0 P) y3 ?3 f
  9. >>> math.copysign(2,-3)
    , f) D+ B2 N4 @$ |+ M& @
  10. -2.0; b$ Q9 \' R' c1 T! f) @
  11. >>> math.copysign(3,8)
    9 b  @$ M, f: a! e& t9 Y, p
  12. 3.0
    + {. l- D0 ?# P2 d* e3 a
  13. >>> math.copysign(3,-8)# @5 J" a( ^/ {" T
  14. -3.0
复制代码

# B9 J8 C- E( K  M( |/ T0 o% ]7 Fmath.exp(x)  返回math.e,也就是2.71828的x次方9 D8 t1 P( k! B" E. v  P; h
  1. #返回math.e,也就是2.71828的x次方
    $ K# l7 c6 j" @$ {' B  M
  2. exp(x)
    2 i: D4 C' y6 X0 l  S
  3. Return e raised to the power of x.
    + ^/ D* M5 {2 }7 V; F/ [  J* i

  4. 5 g+ n3 b5 j% l- B! G& ?
  5. >>> math.exp(1)% R8 }) ~% |1 l' z
  6. 2.718281828459045
    : S, O8 K/ v+ o) ~& F
  7. >>> math.exp(2). U; z) R4 @  E: E/ v
  8. 7.38905609893065) f* K, {, [# `( ]8 @$ R3 e6 v5 f
  9. >>> math.exp(3)1 s3 b2 c, o, `: k
  10. 20.085536923187668
复制代码

6 Y* E4 l+ V- ^8 X# _$ H$ ~math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1) ~# ?3 b3 O1 ]. G$ ]
  1. #返回math.e的x(其值为2.71828)次方的值减1: B9 W* k# \) f" ^0 x$ K  P- Y
  2. expm1(x); L' O8 Q$ \2 I/ ^
  3. Return exp(x)-1.
    " T4 l' r% p4 o/ m+ D0 n, n
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.9 ?1 b  u3 x% A7 F5 }( h& n2 c' |- U) k
  5. 3 ?# D( M) R  T6 q
  6. >>> math.expm1(1)
    8 U# d- H1 G7 Z- C
  7. 1.7182818284590457 l" e5 @% i' Z% _3 n3 z# y7 p6 v
  8. >>> math.expm1(2)
    0 Y; T, ?4 T" R, e/ ~1 K
  9. 6.38905609893065! ?* L0 U) @% ]8 p
  10. >>> math.expm1(3)
    + n0 m, ^2 a; q1 a" E& F; t
  11. 19.085536923187668
复制代码
9 v. K, u$ g2 R. W0 p, A% W- h
math.fabs(x)  返回x的绝对值+ I! _; E% a; r( ]
  1. #返回x的绝对值. K- [6 |' F& u! ]. x3 i! t% P; I! }- s
  2. fabs(x)
    4 ^) s9 ]' j. H2 v, }% V1 T
  3. Return the absolute value of the float x.1 U, o" ]/ v; @% F$ S. {% o+ I, g
  4. : U2 K* t- r9 t, T7 e; ]
  5. >>> math.fabs(-0.003)
    ! x5 V! ~9 q" T% J
  6. 0.003
      s9 o' W  |! e
  7. >>> math.fabs(-110); ~3 r6 `. K5 f6 A" G2 {% S; [0 F5 N& t
  8. 110.0! n1 J* J+ @* m
  9. >>> math.fabs(100)
    4 f! d" t/ v6 z1 Q( N3 F' B
  10. 100.0
复制代码

& W; j- O0 E; H* E1 t* @math.factorial(x)  取x的阶乘的值0 _3 Q, t/ z& {* O4 D+ v8 Q
  1. #取x的阶乘的值1 p# p# Z& r# P. ^3 j! T3 S. d7 O. f
  2. factorial(x) -> Integral3 D* Z; U) v% c9 F% `2 c0 c$ w
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    4 R: y) }0 r) |' f# t
  4. >>> math.factorial(1)
    ' @1 ~+ m2 N4 _5 ~( k
  5. 1
    7 y8 \3 ]; W9 T- ]! v
  6. >>> math.factorial(2)8 L4 `2 W3 S; Y, }
  7. 2
    : O2 V; C. Y. ^8 w6 X( p( k
  8. >>> math.factorial(3)
    ( x& h. n. c2 {5 S
  9. 69 n1 Q$ U- j6 d2 \  X2 \
  10. >>> math.factorial(5)* A" X' u" M" Y: @; ]& ^) y/ }
  11. 120. W6 A8 ?  `. c6 ~! s. T
  12. >>> math.factorial(10)! ?  C  N2 p3 `: u
  13. 3628800
复制代码
$ H, b& t7 A0 @$ O9 y; k3 H
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数7 M- H8 Y6 Y- l5 G
  1. #得到x/y的余数,其值是一个浮点数! F) A$ C; M4 [1 Y, m' P
  2. fmod(x, y): n" k1 p, R8 \" u3 S) H
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ! A, {- d$ f* x4 l: u& a2 {
  4. >>> math.fmod(20,3)
    ( I4 h( O# J' T: \; k2 i
  5. 2.0
    8 E# J4 P8 F/ M5 @) P
  6. >>> math.fmod(20,7)
    6 [' v7 a  Z% a  K. w: q
  7. 6.0
复制代码

0 v$ p, E+ i: ]" K( M# U2 C9 P8 fmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 S) p, i9 r/ ^* B2 \( ~
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,1 N3 A) s4 A, w0 S, S  W; O3 O
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值$ P0 Q/ ?6 w# k4 B, M; U1 H
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    1 g/ _. C3 Q8 [- S! A
  4. frexp(x)! }: d2 t% ^% A5 X5 D
  5. Return the mantissa and exponent of x, as pair (m, e).
    - u, A' J2 {% |4 X5 O
  6. m is a float and e is an int, such that x = m * 2.**e.! C8 f8 P" d! D: Y6 ^( w$ k. F2 ]
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    * f; E. r2 R5 j, z- Z
  8. >>> math.frexp(10)
    ) E" M. s2 Y# g3 y6 T
  9. (0.625, 4)# ?0 w( w& W4 D5 T1 ~: @  B$ C& W2 ~
  10. >>> math.frexp(75)
    ; H. Z1 L4 C  g! e3 V& _6 F
  11. (0.5859375, 7)9 ], V& b! |! t0 c
  12. >>> math.frexp(-40)
    + V4 |. ^6 ]2 H3 W0 W6 y
  13. (-0.625, 6)3 L$ Q( @7 h5 E5 s4 n- h
  14. >>> math.frexp(-100)
    5 E+ g$ o, s% g
  15. (-0.78125, 7)
    4 F' ~* ]3 Q, E0 x! W6 J% T4 J4 {! j
  16. >>> math.frexp(100), t/ j) g. _. d
  17. (0.78125, 7)
复制代码

3 A! b+ L: H9 lmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列9 I1 x8 _( a' {% x( i
  1. #对迭代器里的每个元素进行求和操作
    7 ^- l, O1 ]. @7 |8 y
  2. fsum(iterable)
    1 L- B' V1 u3 a# P/ \1 n
  3. Return an accurate floating point sum of values in the iterable.
    4 A3 Y9 f" p: j' L$ D
  4. Assumes IEEE-754 floating point arithmetic.0 L  ^% N4 u  J2 R2 E# S! z
  5. >>> math.fsum([1,2,3,4])
    ( U3 Y3 ~3 s0 @% w' v) {: t% _
  6. 10.0
    ; K$ Z1 ]3 a3 Z) m2 D' ]! @" t/ n
  7. >>> math.fsum((1,2,3,4))
      Q) c/ V% ^% T: V
  8. 10.0
    # E' v7 |3 v0 O4 Y7 q! K0 Q
  9. >>> math.fsum((-1,-2,-3,-4))
    * a# A/ v5 [# L( p
  10. -10.0
    8 o4 ~+ L, a8 M# f% Q3 c/ G
  11. >>> math.fsum([-1,-2,-3,-4])
    3 q9 E2 b2 K8 v, n, l& E
  12. -10.0
复制代码

! o$ Z# N# r" j6 emath.gcd(x,y)  返回x和y的最大公约数
& t4 J1 F7 {, c0 s
  1. #返回x和y的最大公约数
    . Z2 n! T! P, B; E
  2. gcd(x, y) -> int
    ' O9 g  ?9 Q& ~1 E4 c3 L: W
  3. greatest common divisor of x and y# c2 j2 o( x& e& [8 Y! B* ?4 v
  4. >>> math.gcd(8,6), h0 }1 C, ~1 I$ Q8 h4 {
  5. 2
    " I# v- _& T( P& `  z' ~0 O
  6. >>> math.gcd(40,20)
    0 Y" K% M/ ~# F5 b& @" k  w; C
  7. 205 v/ @# k0 G+ R. O4 Y7 ~
  8. >>> math.gcd(8,12)+ f1 g% [& ?+ a: d7 l# e
  9. 4
复制代码

8 [& s0 |1 ?( Y5 d+ {math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False! i. f' {7 B8 \% X  Z) q3 s
  1. #得到(x**2+y**2),平方的值5 k/ n  f! @4 K2 J+ ]
  2. hypot(x, y)
    : u( w  I2 z/ A! B$ k: G/ C- L! A
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    4 V9 Z% k' |! h: z( u! y7 V( Z
  4. >>> math.hypot(3,4)9 f- I1 q0 D$ G! p. r" P
  5. 5.0) x# ~9 B$ {. \) L7 Q( A! W
  6. >>> math.hypot(6,8)
    : }4 e; u" O: w8 ^/ B" W
  7. 10.0
复制代码
  t9 v: n  S6 ~9 L* I: D; L
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
5 ~0 e* S" F+ X# ?
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" U6 Y. Z* g, Y6 X
  2. isfinite(x) -> bool& G* {: E7 `4 M) h
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    , F- K9 M5 I+ f2 t
  4. >>> math.isfinite(100)
    5 [7 \4 y& W  Z; n& U6 m
  5. True+ N2 q) q5 Y" a
  6. >>> math.isfinite(0)1 ^* h, A: l* G; m
  7. True
    % l! d! o+ R( ~: a2 [7 o5 X
  8. >>> math.isfinite(0.1)
    1 X; F9 a- V' s: T( w5 L1 S  q
  9. True
    $ F2 h* l/ H  r5 a' W
  10. >>> math.isfinite("a")' @6 h" }- Y6 M6 T4 @* D7 @
  11. >>> math.isfinite(0.0001)
    " V* ]" P0 Q/ h3 Q- i- K3 n0 T
  12. True
复制代码

) K+ o( m/ w/ Q5 h! A9 X! cmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False" n5 @) z% D% M* H9 \# v# T
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False9 O# V# ^. n- b4 M" p) j' \
  2. isinf(x) -> bool3 @$ v% t* p; ]7 e  L  N4 I: Z
  3. Return True if x is a positive or negative infinity, and False otherwise., m5 G0 M/ d3 B3 V1 i7 x, X( w5 {
  4. >>> math.isinf(234)/ f# N; y$ w& z) o' K2 p  g3 t2 C
  5. False
    ! X$ C: ~) o' u8 x9 }7 P2 [. M
  6. >>> math.isinf(0.1)
    + y% t9 y6 p+ [/ V$ h& T
  7. False
复制代码

7 W, m$ C5 S  L* fmath.isnan(x)  如果x不是数字True,否则返回False) d8 B# q& r8 @
  1. #如果x不是数字True,否则返回False' L( x% T# ^( {3 z2 t
  2. isnan(x) -> bool8 ?# V4 X& C8 O5 n
  3. Return True if x is a NaN (not a number), and False otherwise.
    0 z5 U8 ^0 a! h3 K( l) e
  4. >>> math.isnan(23)
    . Y5 i( L. u5 O1 M
  5. False
    : Z2 t  {0 J7 [. C6 b0 x( V
  6. >>> math.isnan(0.01)
    9 @* j+ G1 T  {# s0 o
  7. False
复制代码

2 f: a/ v* Q+ Y# v  u; |math.ldexp(x,i)  返回x*(2**i)的值
) M1 D: c: I4 r1 Q% q+ y. t( R# i
  1. #返回x*(2**i)的值
    ; `$ L8 A; z7 p( ^
  2. ldexp(x, i)# c; e( Q% f) ]; v7 h- z. I& r5 u  |+ y
  3. Return x * (2**i).
      H8 [( O6 \4 ^! T0 U9 d
  4. >>> math.ldexp(5,5)# `: G6 z* k8 r3 R  H: ?
  5. 160.0" {5 p: f5 D. H" n4 c
  6. >>> math.ldexp(3,5)
    ; F+ f( c  V# p; g0 y! f* L7 ]
  7. 96.0
复制代码
' ^4 R* E' d; n" M+ u7 ~5 ?
math.log10(x)  返回x的以10为底的对数
' [3 B) u! O: c7 U
  1. #返回x的以10为底的对数- [) ^" R/ |! \$ C/ g7 T$ g. F
  2. log10(x)
    " ^# t% e/ a; |9 h) w! d
  3. Return the base 10 logarithm of x." U2 E% Q4 `0 U+ p) B" ]) C# h) _
  4. >>> math.log10(10)
    ) j4 t, S2 K- O8 Y! ^
  5. 1.0* F, M( v; F  K% p
  6. >>> math.log10(100)0 r- ^& y: u* g  ]
  7. 2.0+ _& G3 W2 q  R8 ?2 U6 ]
  8. #即10的1.3次方的结果为20. z# n. c9 k8 n. x3 x6 N3 w" h7 l
  9. >>> math.log10(20)
      D$ q8 z2 Y, L1 e# s1 i+ R7 E
  10. 1.3010299956639813
复制代码

+ Z0 S9 Z* B* `4 i4 H* N' W( }) Kmath.log1p(x)  返回x+1的自然对数(基数为e)的值
- A' |7 b9 ~8 o* z. x
  1. #返回x+1的自然对数(基数为e)的值
    ) j' o0 t6 W* h; n0 r  A
  2. log1p(x)1 J4 g9 l! e4 d! }% L3 S0 N# c
  3. Return the natural logarithm of 1+x (base e).# c+ ~$ e) f9 }
  4. The result is computed in a way which is accurate for x near zero.
    4 `( E; V5 b- p  ^. U8 k
  5. >>> math.log(10)
    1 {5 G4 K8 D) S. j  Q6 p9 J
  6. 2.302585092994046" z: D6 _  M7 T- `; l, F" \6 U
  7. >>> math.log1p(10)
    7 z+ q  Y! q, d$ J% h  \
  8. 2.3978952727983707/ _8 D! Z) n" F1 p4 V7 ^
  9. >>> math.log(11)
    ( I6 R2 M# N$ f
  10. 2.3978952727983707
复制代码
; u! [" W4 p/ i4 v2 j/ j8 t
math.log2(x)  返回x的基2对数" ~( ]0 s# S' ?
  1. #返回x的基2对数
    * B8 M3 g  d% u$ [4 g
  2. log2(x)& t( F9 J& t, U! S
  3. Return the base 2 logarithm of x.9 T1 d& Z, G- {$ s
  4. >>> math.log2(32)
    $ z1 Y0 |9 |+ R( n
  5. 5.0" H. `- w" y' B# o( L# D4 p6 J- Y: H
  6. >>> math.log2(20)  w! d9 `- K! c, d- Z
  7. 4.321928094887363
    . Y" M5 ?* I3 i! q$ m8 s6 C
  8. >>> math.log2(16). E" m7 u6 B: R: P
  9. 4.0
复制代码
( e' E0 X8 G. t$ ~, e5 _  }: W
math.modf(x)  返回由x的小数部分和整数部分组成的元组
( ?6 ~' }/ _1 }4 ~& M/ \. o2 J
  1. #返回由x的小数部分和整数部分组成的元组
    & j$ v  `3 F; Y+ E7 o5 k
  2. modf(x)
    # [2 F. S' w, O7 w& _
  3. Return the fractional and integer parts of x.  Both results carry the sign
    / \+ }8 m' \( S1 k5 O( w* |
  4. of x and are floats.
    ! F( S. l; s, h4 N/ t4 y6 _4 _
  5. >>> math.modf(math.pi)
    0 Z% S7 X5 a  p
  6. (0.14159265358979312, 3.0)% @+ L$ p5 f! F6 P( A
  7. >>> math.modf(12.34)
    ; K% h0 J2 \  Y, S! k( i
  8. (0.33999999999999986, 12.0)
复制代码

3 k. f3 M# u  K1 i& ?$ nmath.sqrt(x)  求x的平方根) b' z$ V4 Q1 {/ F+ \1 `  O
  1. #求x的平方根
    7 q  |1 P, X# ~2 v8 Z
  2. sqrt(x)
    + n6 k; A( f  I* p! f
  3. Return the square root of x." t! k& L. b' `0 g5 `" f
  4. >>> math.sqrt(100)
    : E9 ^. M2 L7 C+ Q- Z3 U0 w
  5. 10.0, f0 U& }; K3 Y# D$ }: K2 T
  6. >>> math.sqrt(16)$ s  k. `+ E6 D  \; f0 m+ `) @" ~
  7. 4.0- X" [; \- Y6 x& d* O* ~
  8. >>> math.sqrt(20)  U* I4 _- r. \; n
  9. 4.47213595499958
复制代码

! l: d" D, D8 T2 x' emath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 B6 M+ W: U( P
  2. trunc(x:Real) -> Integral, c) P# S2 \1 `: N* |
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 m+ ]; k: d7 B( A  s
  4. >>> math.trunc(6.789). |' q. Q: W2 o0 c0 ?$ i3 y' T6 G
  5. 61 p4 f+ \0 L0 l2 w6 p: h
  6. >>> math.trunc(math.pi)
    ( ~4 U. u. n6 B  u
  7. 30 s% s% {( {3 `7 V. b
  8. >>> math.trunc(2.567)
    * s$ p) I/ V0 r1 ~: M
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-24 18:31 , Processed in 0.087305 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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