新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

& ^) \6 Y0 [) o+ U0 s" R9 O【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
: d' t  I) Z, ^7 D. Z( O+ L/ g2 E

8 D" G2 g1 G+ T! I, W: _% J方法15 x( i5 u9 ^0 i0 I. Q$ t) Q) c7 s
  1. >>> import math; }+ S, [* p6 ~5 o
  2. >>> math.sqrt(9)
    2 K. Q8 h, K2 W/ O5 x' |3 Y. O' T
  3. 3.0
复制代码
方法2* Q6 {8 _  p3 N, r5 y# ]) {/ q/ E
  1. >>> from math import sqrt
    ; r# ~% k' m9 z' N# j. Y& V
  2. >>> sqrt(9)3 A% G) y! U. K- V; i
  3. 3.0
复制代码
2 E; \! x; M! f, U

: W" a$ F- S) Z. u
math.e  表示一个常量
+ L" [. m1 `4 z! \7 |7 D) V
  1. #表示一个常量
    5 a3 F9 \+ s- Y. c. Q5 }- c2 c
  2. >>> math.e- }* }  U/ v9 O
  3. 2.718281828459045
复制代码

& x, B  E5 W- a, Bmath.pi  
数字常量,圆周率

9 B" P: `6 J9 Y% _0 U6 I
  1. #数字常量,圆周率0 R  A, H8 |% I
  2. >>> print(math.pi)
    # k& j6 I& N+ H6 L" ^" T/ V
  3. 3.141592653589793
复制代码

( D3 Q- m5 w6 ^# _! |9 Z& c( z. Mmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
: J* Z; c) W$ n0 G; z# O2 w
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 J, u+ e9 b# l
  2. ceil(x)0 U& N* X8 E/ ~. {+ b
  3. Return the ceiling of x as an int.: g1 V$ k+ f% W
  4. This is the smallest integral value >= x.
    ! K' P, q3 e. O$ x! n5 x2 }
  5. 8 v' k0 z3 X8 S9 C/ X! t( |
  6. >>> math.ceil(4.01)
    ; ?2 k, R+ v$ k+ U
  7. 5
    ( o6 n% P/ R! x( H) |( P
  8. >>> math.ceil(4.99)
    2 [1 A9 u+ H9 }% {, w
  9. 5+ t3 |3 U  g+ N4 {. ^1 n
  10. >>> math.ceil(-3.99)/ H, T1 E$ Y% q4 q4 Q1 a5 ^/ P$ Q7 B
  11. -3
    ! R. _: V: a' j% X% ?
  12. >>> math.ceil(-3.01)
    6 e% e. X0 L, ^- G' M# H2 Z
  13. -3
复制代码
9 }4 G0 o& }' R* d9 v% L! P
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身% g7 u8 @# f* @& ^* S  `
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身, ~, l* Z" Z+ A
  2. floor(x)
    & j  z4 I- Z, h" X6 y0 f
  3. Return the floor of x as an int.3 n+ C; \/ N7 L7 V7 R
  4. This is the largest integral value <= x.  X4 G4 C- L0 s
  5. >>> math.floor(4.1). N0 X$ K% b! M+ w# T7 @
  6. 44 N- R9 K' Y: F7 R* Y/ J  t# Q
  7. >>> math.floor(4.999)4 N, e, a% G5 n; `5 D' D
  8. 4! G2 `( _. K$ p, k
  9. >>> math.floor(-4.999)2 L7 M7 g2 v4 o! y# g/ |
  10. -5* u) ?# W8 c/ L5 z& d. v9 O) J' S
  11. >>> math.floor(-4.01)
    - P" ^+ u- {# @; x7 h5 a
  12. -5
复制代码
2 N" P* J( G% V/ e" }9 ?4 n
math.pow(x,y)  返回x的y次方,即x**y7 \! y2 ^6 F8 W9 W  u1 X
  1. #返回x的y次方,即x**y
    ' |" |; |5 A8 {1 T8 v+ C
  2. pow(x, y)
    + G/ ^: K2 s3 I( Z- s. M4 s1 [& w
  3. Return x**y (x to the power of y).
    3 ]% q8 @1 Z6 u/ ^) }
  4. >>> math.pow(3,4)
    7 e+ E* L4 E  c* N, p% q( }; w
  5. 81.03 n# d. h: i: l+ N! Q; Y
  6. >>>
    , l8 I: f2 D) A' R
  7. >>> math.pow(2,7); d7 Z. W# l' @% H- n
  8. 128.0
复制代码
: H' y! ?' y% m
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' ]) i: c: C6 a5 O, @/ K6 J; }
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ( O% \. \* m% U
  2. log(x[, base]): z" M$ `( }) a, B; r* j# Q
  3. Return the logarithm of x to the given base.
    . n' U0 L# s+ ^4 y8 I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    # U2 F1 _5 G% n. U& ~5 Z
  5. >>> math.log(10)
    6 ~- r. J9 b3 D/ C
  6. 2.302585092994046
    . b- Y  a) D' J7 `
  7. >>> math.log(11)
    ( f. r  [7 X2 x& |; e( C- r6 U; |1 e
  8. 2.3978952727983707
    7 I7 r. V6 o# i/ S& R  \: k, n1 F
  9. >>> math.log(20)* y# D( e! r+ k$ K7 R( e9 X
  10. 2.995732273553991
复制代码

- a) N0 \) k0 F2 y; r$ ]! Mmath.sin(x)  求x(x为弧度)的正弦值( m: x- L( y* M- ?
  1. #求x(x为弧度)的正弦值
    . b* A8 v% t7 M4 x  ?8 v+ n. `
  2. sin(x)& K8 ]5 I* h- `) X) p
  3. Return the sine of x (measured in radians).& u6 Z1 Z' H. _6 N: ^* q0 B
  4. >>> math.sin(math.pi/4)/ v% i+ p# H" }; N: t
  5. 0.7071067811865475
    " a7 U, s( Z1 \: n. T& y
  6. >>> math.sin(math.pi/2)2 b; o3 i: _9 M' A( L4 l  T
  7. 1.0
      `' {' ]" C$ A7 Z- B. D
  8. >>> math.sin(math.pi/3)
    ' k/ |2 v  V, s6 F" {  s4 L
  9. 0.8660254037844386
复制代码

- U4 [7 t% w1 |0 |math.cos(x)  求x的余弦,x必须是弧度
; {; _+ L( L/ P% u
  1. #求x的余弦,x必须是弧度
    4 }* w/ i% d8 X  g
  2. cos(x)' ]+ ]* x2 b8 q/ L
  3. Return the cosine of x (measured in radians).- [# `3 ?  L2 `* K& W' u) E' |: a
  4. #math.pi/4表示弧度,转换成角度为45度, o9 V5 t0 l; G% X) ^0 v% E
  5. >>> math.cos(math.pi/4)
    8 m8 c$ P) |( A( N# m( {6 k
  6. 0.70710678118654767 ?; d" z3 w9 w0 y. ~
  7. math.pi/3表示弧度,转换成角度为60度
    4 {. T2 _0 {* N/ m) C: `9 D0 u
  8. >>> math.cos(math.pi/3)
    , a+ H7 o) C% l! Q$ B
  9. 0.5000000000000001
    7 d; w8 c. Q( i8 p, R( }
  10. math.pi/6表示弧度,转换成角度为30度
    ! X4 a7 b8 F3 I6 J6 B5 ~
  11. >>> math.cos(math.pi/6)7 J3 J8 M9 n" O. H0 y2 T4 ~
  12. 0.8660254037844387
复制代码

7 c/ G, ~* ^; B# Q1 ]* y3 d: Cmath.tan(x)  返回x(x为弧度)的正切值
. Q: A. b" D3 v9 D' G6 T7 Y& |* G
  1. #返回x(x为弧度)的正切值0 _5 s+ L. m. X3 K
  2. tan(x)$ K2 K4 b2 y$ e2 S& M' n7 ?
  3. Return the tangent of x (measured in radians).
    - @3 e! u0 D$ g5 r5 d% Q
  4. >>> math.tan(math.pi/4)
    # j, L5 z! D. m" G2 W! o
  5. 0.99999999999999993 ?, o) ]" t" L- K+ w
  6. >>> math.tan(math.pi/6)
    " \8 l. [+ V5 I$ Y: P6 J
  7. 0.57735026918962572 s( W4 R$ x& M5 t; B8 }, z
  8. >>> math.tan(math.pi/3)
    ; j- }! S# w7 S6 U8 `6 B2 V8 l/ E
  9. 1.7320508075688767
复制代码

- ~. l% ?- M5 X' ^math.degrees(x)  把x从弧度转换成角度  s# H( M8 L# G' P
  1. #把x从弧度转换成角度# ?! E: V1 M( [3 A; {# v3 _
  2. degrees(x). L6 N  C3 r" t) Z' \5 I
  3. Convert angle x from radians to degrees.
    / b' }; s6 y, g/ U1 [/ x

  4. 0 T/ T; ^, ~$ [7 S/ k" a, W9 @
  5. >>> math.degrees(math.pi/4)
    & u# i6 u/ g  v+ z2 e2 O! w
  6. 45.0
    # B, ]+ c# v4 x& `4 K" x; J
  7. >>> math.degrees(math.pi)
    ; F4 E. b& p- u: c& U  z4 b8 O
  8. 180.0% x2 E/ p5 V) W0 I0 S* A- l
  9. >>> math.degrees(math.pi/6)2 x  Z( e1 F7 G3 ]/ `$ P$ `- L
  10. 29.9999999999999966 ^) [) M7 J- J  t8 m
  11. >>> math.degrees(math.pi/3)
    2 h/ C* y0 L. L1 |! |# W9 H4 ^2 C
  12. 59.99999999999999
复制代码
; h; y5 Z! c- s3 j0 `
math.radians(x)  把角度x转换成弧度
# t7 ~% Y( S0 l
  1. #把角度x转换成弧度
    & n+ ~/ Y* j$ W. }* Y) G9 N6 `
  2. radians(x)( D) B% a. @2 g! C3 C
  3. Convert angle x from degrees to radians.
    , T9 o9 K6 X! t. ^8 I& d
  4. >>> math.radians(45)
    " I3 g2 a8 N7 T
  5. 0.7853981633974483
    ' H/ ]" R. q; ~- W9 k: o  `
  6. >>> math.radians(60)" ?# Y- i0 E; [6 I
  7. 1.0471975511965976
复制代码
' ^+ b" p5 i) W( @6 n( M( ^
math.copysign(x,y)  把y的正负号加到x前面,可以使用07 t' S! L1 m& j; K
  1. #把y的正负号加到x前面,可以使用0
    2 i2 K8 \% b" B
  2. copysign(x, y)2 n8 I4 G9 p) J3 h+ \" }3 b% M
  3. Return a float with the magnitude (absolute value) of x but the sign
    0 O4 e3 [; y( R% E9 r  _
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 L; v& P0 }5 ]# p4 ^+ s3 x
  5. returns -1.0.% `- b9 \2 ?% x* H9 ~

  6. 9 ?# J; `7 e. h9 I- M
  7. >>> math.copysign(2,3)+ }* K$ }: L; ~
  8. 2.0
    0 I/ Q) V. B. }% h  @5 n( I7 l
  9. >>> math.copysign(2,-3)
    9 \: h- [6 R" k9 V7 L# b
  10. -2.05 B: y# |  r3 I
  11. >>> math.copysign(3,8)' r) g5 A9 d4 s2 R1 k
  12. 3.0" g* S+ j2 H: V$ E$ l
  13. >>> math.copysign(3,-8)
    5 O6 n! b9 m0 G+ X5 K( D
  14. -3.0
复制代码
/ T4 P% T5 Q6 u/ N
math.exp(x)  返回math.e,也就是2.71828的x次方, a$ w- q* s" X- R4 c/ L% {* S
  1. #返回math.e,也就是2.71828的x次方6 n- V2 ^$ t! @2 d8 y+ k
  2. exp(x)
    - c. B8 Q4 B0 G- |
  3. Return e raised to the power of x.3 J6 A; J  P! ?6 v

  4. ) E8 {; i2 R' y+ D3 U# Q
  5. >>> math.exp(1)
    - J* D$ E. u8 O9 x$ T" A/ d
  6. 2.718281828459045
    . Q# i% l- W0 G
  7. >>> math.exp(2): b  B/ G( J. j( f" K; \
  8. 7.38905609893065
    5 V4 i2 z) S0 ?# {$ x7 k; c
  9. >>> math.exp(3)* e% n. v6 b  Q7 T4 K! c
  10. 20.085536923187668
复制代码
  y' a! K8 d" M: C+ ?) M7 t
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1- ^1 G5 q. F5 i& u! Y
  1. #返回math.e的x(其值为2.71828)次方的值减1
    7 x" ~& t  d$ F" `
  2. expm1(x)
    $ r* [: Z1 a  J1 ^/ e$ ?
  3. Return exp(x)-1.
    9 U% ]# B* l" R
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.: N$ k# s2 U( U/ r: I

  5. & h& [. Q& R( x
  6. >>> math.expm1(1)7 a0 p- d& H6 R9 G% ?" Q; V8 c, b* S( q
  7. 1.718281828459045/ P& t0 f8 C- Q
  8. >>> math.expm1(2)4 p3 P" K' X8 w& `/ g
  9. 6.38905609893065, Z% ~7 q8 A- w! _" C
  10. >>> math.expm1(3)4 ]& G8 N' y! e$ w- T' F& B
  11. 19.085536923187668
复制代码

" g( o7 ~) K1 v- Dmath.fabs(x)  返回x的绝对值& A# q. O/ i6 @( f
  1. #返回x的绝对值3 H9 c9 C% Y& L) A
  2. fabs(x)1 N' {# T9 Y" p1 X
  3. Return the absolute value of the float x.9 ~4 l: c4 @, m

  4. . V( M$ R# [9 {( z' F* p8 R4 Y, o& u
  5. >>> math.fabs(-0.003)" |7 n! A& |* _, h, n. I" L
  6. 0.003) q0 R0 a# T! z( c" I4 \3 e
  7. >>> math.fabs(-110)
    9 W) ]& n+ y% @
  8. 110.0
    ( I0 ^8 l1 c  }
  9. >>> math.fabs(100)% d: i3 R/ z) a3 A
  10. 100.0
复制代码

, k2 K4 l  H5 a3 F7 S* S" omath.factorial(x)  取x的阶乘的值
& d) m/ a9 }: I% D( `% R+ S
  1. #取x的阶乘的值2 G( E6 L5 i- ^% d" I; e
  2. factorial(x) -> Integral
    2 F8 ~. m0 D/ Q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    8 q) F+ t- a  p
  4. >>> math.factorial(1)
    0 B& t' F& ]* ]* F% c& n* o% i% p7 C7 O
  5. 1
    . L# M/ M' f+ A" M$ K& Y
  6. >>> math.factorial(2)0 s' X! N. l( I3 z" U9 Y% F
  7. 2( t9 L2 _3 @( }6 t. F
  8. >>> math.factorial(3)
    * v) H* w, ~5 t$ r6 E0 w+ ?
  9. 6
    # n4 a7 h+ H( |& Z' i3 I# P- @7 y) S
  10. >>> math.factorial(5)
    * n$ A' i& j1 @8 f4 Z1 d$ t
  11. 1203 ~0 Q; Z0 U( ~
  12. >>> math.factorial(10)& a: R1 p9 q9 V) r5 B( Y) _
  13. 3628800
复制代码
* i: k% f5 o" ~# u" G
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
5 ^& Q5 w0 I$ J2 k+ E# ~
  1. #得到x/y的余数,其值是一个浮点数
    $ z+ C, U0 f" n+ ], m2 ?
  2. fmod(x, y)
    0 N% o' S8 E/ \: _3 O4 K! h
  3. Return fmod(x, y), according to platform C.  x % y may differ.- \6 t) N1 b0 c8 h: X+ e
  4. >>> math.fmod(20,3)
    4 q1 C, W; ]$ K# D# |5 [- s. E
  5. 2.0
    * I% ~2 l! R4 @: `& K. Y
  6. >>> math.fmod(20,7)
    6 Q# V8 d: |4 E
  7. 6.0
复制代码
. j; r% X7 ^8 D: i( C; l) @' [7 H! c
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围  Z; _# E5 X9 N/ S' |  P& r
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* r$ B: x! P  F' w
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' D- u" }: n- c' @3 ]0 D
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 H) q5 T. S  m! [% ^; Y- J
  4. frexp(x)
    4 ]" V6 j9 ~) d- c
  5. Return the mantissa and exponent of x, as pair (m, e).
    ' |: M+ j  k, _& H5 L  Q
  6. m is a float and e is an int, such that x = m * 2.**e.( N* @. |  c8 b. d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
      v/ K' _, f0 f# e) h2 E/ m
  8. >>> math.frexp(10)
    ( E) T3 y  I, `0 m
  9. (0.625, 4)
      ]0 N; Z0 j* v* l3 {: D8 O$ T
  10. >>> math.frexp(75): p; W2 T. P4 G
  11. (0.5859375, 7)
    - \2 l( D: s* E, W& _
  12. >>> math.frexp(-40)4 u5 c9 t# e! K& B
  13. (-0.625, 6)* n- [8 t2 t% B
  14. >>> math.frexp(-100)% k7 s# ]! Z4 l7 `+ E9 W+ Y" T8 J
  15. (-0.78125, 7)7 v5 `1 j: Y1 a( A" [3 v- e
  16. >>> math.frexp(100)
    9 f8 a) @2 b( \) Q
  17. (0.78125, 7)
复制代码

" J) l9 p+ _7 @0 Omath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列, ]; z7 u# ^5 }! n$ o5 V( A$ ?8 ]
  1. #对迭代器里的每个元素进行求和操作& T& u) D" M1 i! o( T7 j9 f
  2. fsum(iterable)6 f2 L9 Z  k8 E4 `0 K) f0 j
  3. Return an accurate floating point sum of values in the iterable.' d  y  K6 n- f2 ~& |$ z* ^
  4. Assumes IEEE-754 floating point arithmetic.0 }" n- C2 }& p" ~+ ^$ y
  5. >>> math.fsum([1,2,3,4])9 v7 M* R) S2 A# K( o' x' \- D0 D
  6. 10.08 o$ x" C9 h0 I6 N
  7. >>> math.fsum((1,2,3,4))" b. V1 H9 f8 H. Y2 G" v! e7 Q& O$ {: n
  8. 10.02 \* }9 l  ~1 ^: |8 ]7 c) K
  9. >>> math.fsum((-1,-2,-3,-4))
    : Y1 D& p/ i& T. X/ b. [9 \
  10. -10.0
    6 `; n- x4 w) c
  11. >>> math.fsum([-1,-2,-3,-4])9 T- p- G" \* s" Z. v! _4 k
  12. -10.0
复制代码

: Z2 M. N4 X- z) A" Emath.gcd(x,y)  返回x和y的最大公约数
- L: ?2 U( U, I" |! m
  1. #返回x和y的最大公约数
    # S0 o$ h- l; |$ c2 y9 y
  2. gcd(x, y) -> int
    * L* D5 d: `3 ]( M! P
  3. greatest common divisor of x and y
    3 c+ L! G  i6 S4 I
  4. >>> math.gcd(8,6)& p- D2 S, T. z8 y- v
  5. 2
    0 G! U( Y0 ~1 I, C/ v
  6. >>> math.gcd(40,20)
      ?- V: R2 L" g4 l
  7. 208 {" r# x: u8 e8 J
  8. >>> math.gcd(8,12)6 d+ V3 T8 v4 d. s# x
  9. 4
复制代码
( x) g, \; m/ b5 C9 f( d
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
, f+ M) [: t4 L: I
  1. #得到(x**2+y**2),平方的值# |! V4 Z5 }7 D9 Y
  2. hypot(x, y)
    . e0 w  r- Y" \9 B/ X
  3. Return the Euclidean distance, sqrt(x*x + y*y).& v8 t& j' p7 d- s/ u# k. N
  4. >>> math.hypot(3,4)
    ) R) M& B9 P. S- }  {
  5. 5.0
    9 w0 w( Q1 S, G  }+ t& O
  6. >>> math.hypot(6,8). w+ k5 O/ s2 L' C6 Z
  7. 10.0
复制代码

; D' h4 B: U# d+ n% S$ u" E- f  Tmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
4 c8 o- k- w: Q& b1 Q. Y- E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    & ?% \8 d  _- q1 t/ p
  2. isfinite(x) -> bool- d) [; M' ~9 ?5 R) m  M6 X
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.6 C6 ^1 |) X8 k4 i
  4. >>> math.isfinite(100)
    1 k8 B5 x' I. W  ?( }5 H
  5. True& S- |, k9 N1 [- K5 q% N
  6. >>> math.isfinite(0)
    / d" T/ }' a" J9 S/ ^: K! k& p4 A5 B
  7. True
    7 w* a5 Z9 X7 B# X
  8. >>> math.isfinite(0.1)( z" j2 I- I) q2 u, w
  9. True
    ( `7 m! F$ C5 D5 q% [4 A; Z
  10. >>> math.isfinite("a")+ f9 N( h+ n* U- a. Z, s6 D
  11. >>> math.isfinite(0.0001)
    1 u/ g; e, E" Y
  12. True
复制代码
  [, K# l9 M4 @# i; J7 H  D) Y
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False- s# @, k4 k/ m& ~* [. A. u
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ! k. n7 z& Z% X  P1 f. F
  2. isinf(x) -> bool( h- X4 b8 H' P) ~; }+ r
  3. Return True if x is a positive or negative infinity, and False otherwise.
    0 \- z1 o% I$ u2 F  k
  4. >>> math.isinf(234)
    1 ^8 Y$ K# S, e; z
  5. False) F* V" d. E* ~( m$ I$ F
  6. >>> math.isinf(0.1)
    . Z" @" g6 S. G  E( {( j
  7. False
复制代码
! ~: ]3 w' l9 k2 Q# |
math.isnan(x)  如果x不是数字True,否则返回False' c; X/ [) F! E0 c4 u9 c5 a, q
  1. #如果x不是数字True,否则返回False/ d3 c8 U, H  R, Q! i% j, V
  2. isnan(x) -> bool6 j$ j1 `! H. M+ r
  3. Return True if x is a NaN (not a number), and False otherwise.
    # [6 w" r) @2 |) j( N
  4. >>> math.isnan(23)
    7 Y( h  ^2 Y/ t  y
  5. False6 [  T( a- v1 Z) Z7 Y
  6. >>> math.isnan(0.01)2 ]' ?! k* V3 J
  7. False
复制代码

  Z+ ]3 v) L8 i6 D/ ^6 ]5 _math.ldexp(x,i)  返回x*(2**i)的值2 x6 e2 X# Y: r! i: k6 W, G9 H
  1. #返回x*(2**i)的值8 {, \! i+ _- Z8 e; I: H3 M
  2. ldexp(x, i)- _3 o+ y" R) ^; \! k
  3. Return x * (2**i).
    & H8 B) B* y/ j3 g! S# A: @
  4. >>> math.ldexp(5,5)6 l2 k8 C) C' f, s4 k
  5. 160.0* i3 B( N4 J3 i) b4 g" c
  6. >>> math.ldexp(3,5)
    & x' q+ J; F8 _# Y
  7. 96.0
复制代码
0 n- d" R* H. _8 ^. J' O, E
math.log10(x)  返回x的以10为底的对数. Z5 f2 q, V1 z6 u) Y( C
  1. #返回x的以10为底的对数+ \" T2 Q3 t4 w& Y4 m
  2. log10(x)4 H  L' j6 t) r$ v8 n  F8 M
  3. Return the base 10 logarithm of x.
    ( V  G# w3 O; l+ X6 ]. O' [
  4. >>> math.log10(10)
    ; ^4 k! S4 W$ h) O. t/ v3 o- ]
  5. 1.0
    # b( p2 ]0 p! p3 r) _' F
  6. >>> math.log10(100)
    - P0 {% z' a. Y- d9 R; H5 o
  7. 2.0
    2 T: H" X6 F8 P4 F6 a
  8. #即10的1.3次方的结果为20
    $ C4 R- e7 ?- |
  9. >>> math.log10(20)
    4 t2 t' i( D# d: }
  10. 1.3010299956639813
复制代码

1 y4 q5 G8 b) r5 e5 N+ `. vmath.log1p(x)  返回x+1的自然对数(基数为e)的值
' p2 c. R3 a0 N
  1. #返回x+1的自然对数(基数为e)的值
    6 i1 S6 ~* A0 y% Z. Y
  2. log1p(x)4 P+ E: N6 P6 U: W
  3. Return the natural logarithm of 1+x (base e).1 E0 n9 a/ b4 f5 Z+ h0 M2 R5 z
  4. The result is computed in a way which is accurate for x near zero.+ f  X+ Z3 v1 q- z' }
  5. >>> math.log(10)% H- T- @) i" M
  6. 2.302585092994046
    ! \( \! C/ S( Y  ~7 S$ U# Q
  7. >>> math.log1p(10)
    2 g) u9 B, d0 a5 b4 ]5 Z
  8. 2.3978952727983707: d8 Q1 Z% `7 r: s, \
  9. >>> math.log(11)1 m4 x4 q8 P2 [: r$ W
  10. 2.3978952727983707
复制代码
- B! k, |8 s! F' x( z
math.log2(x)  返回x的基2对数& H1 t  F3 K+ }: Q; ~
  1. #返回x的基2对数
    * S# [& u  `0 Y) T
  2. log2(x)
    7 r: A4 T" |1 H4 ]
  3. Return the base 2 logarithm of x.
    2 J0 ~3 N; F6 @( [, ^
  4. >>> math.log2(32)
    3 w7 d/ @* S0 ?: o8 v, W) `
  5. 5.0  ]* O2 V  A% v$ x& Y0 S& p
  6. >>> math.log2(20)
      R3 ~: V0 K$ x+ C- `  M  ~# K9 c2 i
  7. 4.321928094887363: O  G$ Z+ v3 d- ^% o" _( V
  8. >>> math.log2(16)
      h- ]; a- I: i! b3 A6 H' N9 U
  9. 4.0
复制代码

- _4 V/ B: G0 d0 r5 _5 Cmath.modf(x)  返回由x的小数部分和整数部分组成的元组
4 B% F$ X3 z1 I" r+ |* O# g
  1. #返回由x的小数部分和整数部分组成的元组2 x. z/ Q* Z$ C4 C! d
  2. modf(x)% d) k4 ]  e+ t) `/ `; k
  3. Return the fractional and integer parts of x.  Both results carry the sign
    6 ^6 D3 T. e' t3 C, a5 o4 d
  4. of x and are floats.! K4 k, S4 X: v, N; V4 r. U
  5. >>> math.modf(math.pi)( i$ o3 W; z9 ]: B2 F- p9 ^4 g
  6. (0.14159265358979312, 3.0)  {) ~' e0 ~- j: A0 K5 p
  7. >>> math.modf(12.34)
    " m+ @3 _3 h3 u1 c( k
  8. (0.33999999999999986, 12.0)
复制代码
4 ~: j4 c" i% H3 g( x, a
math.sqrt(x)  求x的平方根* X8 @8 z5 \6 s, Y' _8 f
  1. #求x的平方根
    + Z; s* d  K( U* ^8 n
  2. sqrt(x)+ J* x8 q/ R, J9 ]# j6 O
  3. Return the square root of x.
    % q; q( v- `  m& f
  4. >>> math.sqrt(100)+ I  t, ~; i. z( s6 b
  5. 10.0/ e+ ~0 P& E$ F. O+ n
  6. >>> math.sqrt(16)) C1 x8 D3 m- M/ s! ?
  7. 4.0; D! n  ]) o% S
  8. >>> math.sqrt(20)3 ~6 o& E( s9 o, V5 X
  9. 4.47213595499958
复制代码

# m# R& U0 u; V% l& y5 N. [8 e# bmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分# F, w& [7 D$ ^% m4 t  x6 y
  2. trunc(x:Real) -> Integral
    $ k5 L6 }' p; r4 o+ n
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.1 v' X* C6 g# x4 X* ]
  4. >>> math.trunc(6.789)& Z0 E* v5 b/ \: n8 h
  5. 6
    ! C8 o1 [+ ^1 G
  6. >>> math.trunc(math.pi)
    8 Z: P5 [; g! |- h' W$ _/ S
  7. 3
    " e) u: y1 N8 t3 y7 l4 D" e
  8. >>> math.trunc(2.567)
    / b0 ^2 v4 }3 C$ G  a  N! z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-29 22:39 , Processed in 0.097604 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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