新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

$ O& v* K3 Q$ r) l【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 v% O/ r2 t* t+ s+ H1 d/ l
) H) G+ l% O  X! [* W
方法1* V/ }, d/ K9 p$ H& q. C  C8 }
  1. >>> import math. {& b0 f( r8 Y- D
  2. >>> math.sqrt(9)
    % u% s( b' }1 c; v
  3. 3.0
复制代码
方法2! m1 {9 S, F* I+ o
  1. >>> from math import sqrt
    ) D9 N) D& m' L
  2. >>> sqrt(9)* G0 b- \- {! e
  3. 3.0
复制代码
3 I7 N4 b1 H' \" P% ~& b5 Q' Z


) }" o' u4 G' ?" D1 X
math.e  表示一个常量/ C7 H) ]2 W9 |0 m5 j4 \, U
  1. #表示一个常量+ v! L. k; `  c8 D5 i9 W
  2. >>> math.e
    , l& K( ]5 [+ ^7 h" \6 F9 m- |
  3. 2.718281828459045
复制代码
6 c" _, }4 T4 z! E) \' G
math.pi  
数字常量,圆周率

& F1 W+ _+ {8 g0 E. r( W% h! V
  1. #数字常量,圆周率
    ) c  w  K( [" J  [# M! H( r) O4 R
  2. >>> print(math.pi)% ]% r' u2 ]# ~. p
  3. 3.141592653589793
复制代码
3 G2 @# t9 W0 L1 w/ K2 T
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 s5 L. J( p* }6 A
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ) {) n3 b; l* k/ ^) H9 Y
  2. ceil(x)
    0 J+ M) I0 ?& o5 M; Z  `. N: H- V9 M
  3. Return the ceiling of x as an int.. X( y0 j' g& Z$ q' Y6 Z  _
  4. This is the smallest integral value >= x.% I2 E% O5 M9 U1 V
  5. 5 W& @1 W  r5 s3 r. Y  n
  6. >>> math.ceil(4.01)
    9 |  ~( R- u5 y9 b8 z% I
  7. 55 m/ g2 N* @4 O
  8. >>> math.ceil(4.99)( B, R; g1 \+ l+ f. S" F
  9. 5
    1 W6 n; V8 Y$ C+ H  |4 G, I
  10. >>> math.ceil(-3.99)
    * g! o/ A6 {* b1 M
  11. -3
    . ]8 P. N' {$ \0 {- @( @) u
  12. >>> math.ceil(-3.01)' R% s+ J% ?* {' `& n) h3 K7 s
  13. -3
复制代码

  B. q5 o+ N9 Umath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身% a, Q5 b: l. P2 r6 Y; w
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    3 o  v7 w# u1 b' `' i  N( ?# w
  2. floor(x)
    " r# l9 O. h6 V$ j6 L
  3. Return the floor of x as an int.6 \# w# k! k" ]" W  p5 Z
  4. This is the largest integral value <= x.
    ; m* y- g6 T4 v! |3 l& r
  5. >>> math.floor(4.1)
    4 v- Y* Y/ I4 x9 S# s
  6. 4
    . j# @4 T7 @/ O3 k! i5 U
  7. >>> math.floor(4.999)( J8 L) S6 T: t7 `" R, ~
  8. 4( L8 j+ A; Y( A2 D
  9. >>> math.floor(-4.999)1 P. f* Y9 g  I& X5 N" d  {
  10. -57 o/ {: ~- s) p( Y8 K
  11. >>> math.floor(-4.01)
    , T* E* O$ I( ~4 l5 T# x
  12. -5
复制代码
/ H  E/ d; s/ L: j2 P" {
math.pow(x,y)  返回x的y次方,即x**y  ^* O  ?, l. E6 w) q1 j: V2 l
  1. #返回x的y次方,即x**y& V7 |" s  J9 N+ t6 [
  2. pow(x, y)6 ^) y( E' Y: H0 F! C+ O2 e" U5 n
  3. Return x**y (x to the power of y).- a) K: [5 N+ m$ k
  4. >>> math.pow(3,4)5 r( Z0 E: g) L/ X8 a6 j
  5. 81.0
    5 `+ _! A  p3 Q7 v
  6. >>>
    : f- g: ^: r0 e! q5 h# X! \
  7. >>> math.pow(2,7)/ l: M  O7 Z- i3 q# ~% c
  8. 128.0
复制代码

# K4 E* W/ r: [# ?$ L8 Rmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& G9 g0 N8 p2 d+ Z! [% ~& i
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) |7 k3 N5 v9 a" r4 k! r; b  b
  2. log(x[, base])
    2 n0 W; ^- t3 O3 L- @/ C* B) g( X
  3. Return the logarithm of x to the given base.# O  J! c3 l- K9 I
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ! V2 j: @+ O3 }/ t  H2 `& a
  5. >>> math.log(10), [8 l, g6 q# }. F" d
  6. 2.3025850929940462 V" c1 ]+ }* U: a
  7. >>> math.log(11)# p& f( X4 T/ C5 j) V" o
  8. 2.3978952727983707$ f3 n0 N* z% b; Q
  9. >>> math.log(20)2 Q/ ?2 f5 e, U$ H/ ~
  10. 2.995732273553991
复制代码
1 ]6 p; v5 n+ s; |+ H2 ~5 R
math.sin(x)  求x(x为弧度)的正弦值
) z0 Y3 C0 m! ?
  1. #求x(x为弧度)的正弦值- k' z. A; V. J- \% O) \, J( _3 u- B
  2. sin(x)
    8 Y; k6 S$ b9 d, K$ x
  3. Return the sine of x (measured in radians).  X2 N* _& S9 ?! f
  4. >>> math.sin(math.pi/4)# c* f) {& h9 J# m
  5. 0.7071067811865475
    9 e) C- ^# I. d. k$ o" g2 H
  6. >>> math.sin(math.pi/2)
      F$ ~! r' I7 ]' N: t
  7. 1.0) \% u# w  o0 D) p0 k" O: k8 b: A
  8. >>> math.sin(math.pi/3)! n* T- u% l- e; e5 }) Q
  9. 0.8660254037844386
复制代码

/ D( n1 x6 E0 [2 i. C9 {+ `math.cos(x)  求x的余弦,x必须是弧度9 C1 |$ Q- \9 S! s  d& O
  1. #求x的余弦,x必须是弧度
    . `. i+ y5 X) J
  2. cos(x)4 o0 ^! }  S1 \$ _' p# [
  3. Return the cosine of x (measured in radians).4 `, l8 h* l+ k# o( Y% ]" K- l4 a
  4. #math.pi/4表示弧度,转换成角度为45度
    ' \" _  ?0 n+ G( ~; B% v
  5. >>> math.cos(math.pi/4)
    ( ]9 X  C$ K, p3 z
  6. 0.7071067811865476  S1 E  X# M& Q8 v
  7. math.pi/3表示弧度,转换成角度为60度3 k; D4 ]- _/ F
  8. >>> math.cos(math.pi/3)
    4 U* \4 j7 R; `0 }# d$ W
  9. 0.5000000000000001
    # i3 L. B6 J! }9 U$ J+ E. |
  10. math.pi/6表示弧度,转换成角度为30度) D1 _) `" L6 u
  11. >>> math.cos(math.pi/6)
    ) |+ @) N# c; T! r  K) ~
  12. 0.8660254037844387
复制代码

9 z: R) z6 K8 smath.tan(x)  返回x(x为弧度)的正切值
2 w+ L  [9 S5 X' r
  1. #返回x(x为弧度)的正切值
    0 O2 Y( J% a6 D; m
  2. tan(x)
    # b$ l7 n; R7 L: ^3 i* m
  3. Return the tangent of x (measured in radians).
    " E1 S; P0 l& r# C" T, q
  4. >>> math.tan(math.pi/4)" c" A  M/ s$ g( K2 {4 C/ B
  5. 0.9999999999999999
    2 M& X  z, T5 H5 }. f/ t
  6. >>> math.tan(math.pi/6)
    ( G+ x5 B" B) e+ k0 ?$ Z
  7. 0.5773502691896257
    7 i+ M' \$ U) C; A- O! i! Q) G
  8. >>> math.tan(math.pi/3)1 M% C3 t% ]$ y2 j
  9. 1.7320508075688767
复制代码

5 q* @+ `) L+ t9 Y! p' q4 W' {, Fmath.degrees(x)  把x从弧度转换成角度
  ^4 g) t1 j' ]7 h2 l
  1. #把x从弧度转换成角度
    / `2 J2 s2 o8 k- D  u9 T* y
  2. degrees(x)
    ; q& p0 f4 m- c4 P- U
  3. Convert angle x from radians to degrees.
    $ K% Y6 D6 j9 o6 R2 `

  4. % W( I, g. h0 x2 H0 q1 t
  5. >>> math.degrees(math.pi/4)6 _7 C0 {; ~$ s1 T+ l' s9 y5 N
  6. 45.07 @9 C6 C( B, C$ q
  7. >>> math.degrees(math.pi)
    2 M( E( M* g8 K  A  r
  8. 180.0
    2 j; [% m& f  |, L; o) y. l# F
  9. >>> math.degrees(math.pi/6)$ w8 j/ g( l+ H( d+ h
  10. 29.9999999999999968 ]8 Q# Y+ j  q! z
  11. >>> math.degrees(math.pi/3)2 a  F! @8 J6 E0 R* U
  12. 59.99999999999999
复制代码
( m+ p8 `3 a- n5 f
math.radians(x)  把角度x转换成弧度, c/ Z! @: }& v$ d! J* s/ ?) R
  1. #把角度x转换成弧度
    . b, s, r# Z' D) S0 C9 l9 u+ s
  2. radians(x)0 a! J; K. W; `( F8 O
  3. Convert angle x from degrees to radians.' ?* h6 E$ |* i: N) T" V( X
  4. >>> math.radians(45)
    ) A7 M' e3 R# T  o" _6 L9 _1 b
  5. 0.7853981633974483; p8 e& q! i" Z  t$ G
  6. >>> math.radians(60)
    / @8 m( p' a- x; u8 f9 d. I
  7. 1.0471975511965976
复制代码
3 d' m  s7 J  I8 d; G- W. i. o
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
4 H/ q! l: z, {
  1. #把y的正负号加到x前面,可以使用0# d: [1 o1 Y4 [) c
  2. copysign(x, y)) M  n) j, v7 E) B- E$ q
  3. Return a float with the magnitude (absolute value) of x but the sign ' B: J' o7 j$ Y3 P4 }& @
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) + {& L. \# E( D+ o8 g3 J/ j  A
  5. returns -1.0.7 i4 e3 P/ t2 d+ O! l: H! ^2 L/ y
  6. 1 {+ J  K( Q! M% Z
  7. >>> math.copysign(2,3)2 b5 E0 O: N5 ?5 a4 f& J
  8. 2.0
    ; ^) T- I- ~! A7 N. G
  9. >>> math.copysign(2,-3)
    7 m( H* J' g; q# ^7 {5 T
  10. -2.0) I( m% T& `* o
  11. >>> math.copysign(3,8)6 R" u& R4 ~. E3 @* Z
  12. 3.0' C) u1 s% _6 b/ i
  13. >>> math.copysign(3,-8)
    ) M  i; K1 ]0 x3 J. J0 g% O
  14. -3.0
复制代码

7 B7 Z. K1 D# ?# }& e  E$ o# Amath.exp(x)  返回math.e,也就是2.71828的x次方4 a: S( p3 H7 o# F; v8 h
  1. #返回math.e,也就是2.71828的x次方: s5 h% z2 f' J5 O
  2. exp(x)+ x$ x4 {. y; [0 a
  3. Return e raised to the power of x.# U* \* }8 J6 p# }8 d
  4. $ d6 {& k$ B, c7 p2 j
  5. >>> math.exp(1)
    6 W2 A6 C+ z6 @6 w5 }3 `
  6. 2.718281828459045$ \0 m8 {9 b1 o1 w. z! D- D6 l
  7. >>> math.exp(2)2 @0 f/ q. k1 n+ S# g
  8. 7.38905609893065
    6 v0 D& ]0 j( {) ]* l
  9. >>> math.exp(3)
    0 Z2 B% N! o- i5 Y, m
  10. 20.085536923187668
复制代码
9 ~! T$ o2 ~9 M
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
3 b  q" U7 B* t0 j' P8 X  p
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ( Z7 u4 W5 F. Q, C3 c: b) |. u) O
  2. expm1(x)
    3 Z* G) a! p1 J' G" f: K
  3. Return exp(x)-1.
    2 c; v' Y0 N) m- l( \0 R) v: V
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    3 ]0 e& w0 C: s) y" T: ~$ O/ F! R% W

  5. ) M; i) u# N; E7 l4 L
  6. >>> math.expm1(1)4 L2 X% n( F! ~( y+ h3 G
  7. 1.718281828459045
    1 b) I0 ~8 n- w
  8. >>> math.expm1(2)
    9 v1 z2 v, l9 Q& _; I  ~3 F
  9. 6.389056098930653 t/ q) ^# J; u& E) P
  10. >>> math.expm1(3). Y6 K4 f% f  k, @' t
  11. 19.085536923187668
复制代码
  t+ r6 n$ C. O$ A% B8 R
math.fabs(x)  返回x的绝对值
1 D0 J! O$ ~5 C! ~8 Z
  1. #返回x的绝对值
    7 h- x2 a2 ^: _% v+ B( i, ~. Z
  2. fabs(x)9 _1 ~1 `) P& @. S/ V
  3. Return the absolute value of the float x.
    / M1 O3 w! W8 F) h  u, @$ Z  U. B
  4.   ~" y% `0 I+ X' l% H
  5. >>> math.fabs(-0.003)
      v+ R' P' P; U4 v/ D( V" ?" k. s) l2 V7 G
  6. 0.003# ]8 T4 X/ [" p4 O
  7. >>> math.fabs(-110)3 e8 s8 f/ D- j5 S8 Z% L/ x6 f
  8. 110.00 r+ e  X. S  Y& r6 Z: Y
  9. >>> math.fabs(100)  D8 c5 c, }, [' p; N
  10. 100.0
复制代码
# B; r. J* p$ s' D
math.factorial(x)  取x的阶乘的值
7 p; n* V1 G9 ^3 _# P3 E
  1. #取x的阶乘的值- d$ Y5 y4 p5 ^  `% o' K
  2. factorial(x) -> Integral2 H" W6 a! N1 h
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    0 K/ a& k' X5 Z6 s+ a" l; _
  4. >>> math.factorial(1)
    ; F3 Z: U! q! e2 e: ^  {1 j
  5. 1
    $ S- D" y8 d( s" e( M) s
  6. >>> math.factorial(2)
    ( j+ ^$ u( V9 i$ k3 l% O" z6 Y
  7. 2
    " @, x$ B1 F4 O- Z! o( A7 c, f9 h  Q
  8. >>> math.factorial(3)
    6 `9 G/ C0 `) j1 j
  9. 6; y7 F- o: a" }4 w, U" X8 O
  10. >>> math.factorial(5)6 a9 f. j' c' E2 V* y( _
  11. 1206 B2 N8 X/ i, q  W- d
  12. >>> math.factorial(10)
    / w7 v2 `4 R- K! v
  13. 3628800
复制代码

: {3 }' c  A* N9 {$ Kmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
9 ~; S( z1 a* V5 \' ?" f* ^9 V6 r
  1. #得到x/y的余数,其值是一个浮点数( _4 y! {( Z" y
  2. fmod(x, y)- I$ Z& w# H4 p- h/ ]
  3. Return fmod(x, y), according to platform C.  x % y may differ.# u7 t/ K# y2 w; A* x
  4. >>> math.fmod(20,3)
    , q, P2 r+ w# m
  5. 2.0
      I+ N( K, `, e5 ~. D4 ]' `
  6. >>> math.fmod(20,7)& ~# I8 \2 l# ]4 T; D
  7. 6.0
复制代码

3 z' _. `) A7 K/ K* ~math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围: s* y' b9 `: e! X. m1 J
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    0 G) r* w5 v. v. H8 M
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值/ e; x2 b  ]# V* u7 s- h, j
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1* U) j6 A4 d1 h3 u1 |
  4. frexp(x)
    - H5 t( Q( }4 F3 I' q. W! Z
  5. Return the mantissa and exponent of x, as pair (m, e).
    * ?( O$ R2 [  x% m2 _
  6. m is a float and e is an int, such that x = m * 2.**e.: V! k) o$ ?2 S' d5 n" \2 V$ d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 z' g; }* v" O1 T
  8. >>> math.frexp(10)* j3 F' V) w9 q0 d2 p
  9. (0.625, 4)) A1 B1 ]0 Q, a, y
  10. >>> math.frexp(75)8 f, ?+ l9 d- j1 P. \
  11. (0.5859375, 7)! B" N1 ]  V# I3 p' Q+ r' c
  12. >>> math.frexp(-40)3 ]: i% m$ P6 m0 m& ?% n" Q
  13. (-0.625, 6)' n' P$ H  k" s$ r
  14. >>> math.frexp(-100)6 ~8 J# B, v4 ~
  15. (-0.78125, 7)
    + h2 ]9 n# _$ Q
  16. >>> math.frexp(100)
    . V; }* f( T6 t% `3 G
  17. (0.78125, 7)
复制代码
5 B( l  q  \. S% y/ t. g
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
9 T. t$ J' N! F' S
  1. #对迭代器里的每个元素进行求和操作6 K$ b9 a1 S7 ~2 m( z% _
  2. fsum(iterable)
    ( N2 ~" j' A. X% r5 B; I1 T
  3. Return an accurate floating point sum of values in the iterable.
    & R) Q5 f' J* q* w) q$ ?1 x
  4. Assumes IEEE-754 floating point arithmetic.$ ]9 @+ \; ]- @' o$ `
  5. >>> math.fsum([1,2,3,4])
    ( O" x! n" o8 I8 N; @8 S( M
  6. 10.0, V7 D' b6 f1 m& M
  7. >>> math.fsum((1,2,3,4))
    . y  h- X- y- n
  8. 10.0
    ' O+ I( X/ K  |% Y+ _: ^& K
  9. >>> math.fsum((-1,-2,-3,-4))! @. E/ @5 U$ Q; o$ a# O+ R) j
  10. -10.0
    ) n5 h/ z5 J' Q: Z9 |+ X+ T6 x
  11. >>> math.fsum([-1,-2,-3,-4])5 q- A$ I  W9 t: f# a/ c1 t
  12. -10.0
复制代码
! t' V( ]; H4 W/ P1 p& {
math.gcd(x,y)  返回x和y的最大公约数
6 Y: P9 m8 o" s7 r
  1. #返回x和y的最大公约数3 q2 F6 _$ y6 [- [6 E  A
  2. gcd(x, y) -> int
    . u/ |) O: l2 w! w5 v: U9 u
  3. greatest common divisor of x and y1 m1 k' s: P) g+ I6 X( E
  4. >>> math.gcd(8,6)
    6 p: b+ }. e6 h5 z
  5. 2$ G: f0 ]$ U/ u& E5 t
  6. >>> math.gcd(40,20)
      \- Y: t5 a2 {) U" E2 L7 C
  7. 20
    3 j- O2 P8 V; \1 J% I' M4 f
  8. >>> math.gcd(8,12)
    & x- c6 u7 c. V- z
  9. 4
复制代码
! _: v' ^% j+ b1 O0 B  @
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
" ~! R0 g4 e6 F) v
  1. #得到(x**2+y**2),平方的值
    ) o( T1 D& Z, L2 R7 c
  2. hypot(x, y)
    2 k7 z2 E$ N1 P) v6 u
  3. Return the Euclidean distance, sqrt(x*x + y*y).) q- H1 {2 d5 s! S! D
  4. >>> math.hypot(3,4)% E6 ]$ s0 G8 L) K% m
  5. 5.0
    * S9 ^7 R& Y/ Y. r7 I' M6 _
  6. >>> math.hypot(6,8)+ \0 h- I4 w9 j3 c! J8 B9 U
  7. 10.0
复制代码

; w+ I: G% e0 K' x. Vmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
$ n2 Y7 ~! P6 p# ~8 [5 u
  1. #如果x是不是无穷大的数字,则返回True,否则返回False3 U% j9 Z. `/ _$ }
  2. isfinite(x) -> bool. k9 V: T& p) g: X
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.( M. |7 ?9 T! w) r* x
  4. >>> math.isfinite(100)8 T, E% [; h1 W$ I" t& k
  5. True. C' ^: ]% p- j7 t$ }- n0 h$ n; r
  6. >>> math.isfinite(0)
    7 b* K+ C0 ^: J, y% [2 I
  7. True
    2 Q: u: j3 S2 E% ?. Z
  8. >>> math.isfinite(0.1)4 V7 z2 U  P# V* g( x; V: c
  9. True
    5 P  J3 ?9 A4 \0 b
  10. >>> math.isfinite("a")7 i. g! j; }5 G- H; I, l
  11. >>> math.isfinite(0.0001)
    9 t! z  Q6 \. @, J
  12. True
复制代码

( ]) i1 k2 n+ k( T- A* i3 rmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 g) Z" c9 Q7 Y! T# L* ?$ D
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False. g7 m9 ]( a( X7 U" P
  2. isinf(x) -> bool; ~- ^% D6 K; X; q1 ]
  3. Return True if x is a positive or negative infinity, and False otherwise.
    2 X1 B$ Z- ?# O, \
  4. >>> math.isinf(234)
    1 T# Q7 t! ?. v# w  l
  5. False
    * V6 ^' @9 I2 c, V
  6. >>> math.isinf(0.1)
    , Q9 l9 z" k5 b1 A2 p# P+ G2 Z
  7. False
复制代码
; _* @) a. X. P8 @: o2 s1 I% R
math.isnan(x)  如果x不是数字True,否则返回False
1 k: C; V: q& O+ F0 E/ a( }: e5 j
  1. #如果x不是数字True,否则返回False# o* x$ g8 A! m/ r; e1 M2 G3 s
  2. isnan(x) -> bool
    ; T7 f3 M8 `  R% p
  3. Return True if x is a NaN (not a number), and False otherwise.# \' A' y6 c5 a& V1 |& g' x
  4. >>> math.isnan(23)
    . }4 n& l( i# o! d9 x
  5. False- X7 @) l& ?' Q
  6. >>> math.isnan(0.01)$ |; u3 P7 Q* Z
  7. False
复制代码

5 i' @: m+ u0 v- ?# q2 Rmath.ldexp(x,i)  返回x*(2**i)的值
1 `( Q  N, P6 ?
  1. #返回x*(2**i)的值) Y- Q5 W5 ~0 |, F  o" }
  2. ldexp(x, i)4 Q  k! @0 U# z4 Z, ]: i
  3. Return x * (2**i).
    " C# e1 b8 o8 C  L5 r' Q
  4. >>> math.ldexp(5,5)
    4 l4 c% J' I6 R0 m" s
  5. 160.0
    # W. m' P& `4 k  _% r4 Z1 v7 F; t
  6. >>> math.ldexp(3,5)
    2 I5 i2 ?  ]9 J! @# n
  7. 96.0
复制代码

3 Q5 [0 C$ \( ^9 t5 U' h" g' ~+ |* mmath.log10(x)  返回x的以10为底的对数
" K' p$ U5 `: e1 s6 m
  1. #返回x的以10为底的对数
    $ }! k. N& f( z
  2. log10(x)4 C2 v$ `9 q% X4 q/ V" e9 F
  3. Return the base 10 logarithm of x.
    : c; F" j9 R! @
  4. >>> math.log10(10)1 P6 ^7 y- Y' Y  Y! V. }
  5. 1.0
    3 c" {9 J- V8 S) v+ Z( t' O: ]
  6. >>> math.log10(100)
    & S3 R6 e! p/ N/ F
  7. 2.0
    * T- x  ?% B9 F% P. y8 O" `( W: Q$ C* ^" r
  8. #即10的1.3次方的结果为20
    4 y5 f. _# ~+ r8 ]; y
  9. >>> math.log10(20)
    : T3 \( F1 e( {. B
  10. 1.3010299956639813
复制代码

2 s  O" u2 m% E/ jmath.log1p(x)  返回x+1的自然对数(基数为e)的值: Y0 B# G) c! `
  1. #返回x+1的自然对数(基数为e)的值9 ]: @; c( i) A' h) T) [
  2. log1p(x)/ f3 s  H/ T; q7 ]1 Z, x( q8 k. A  R
  3. Return the natural logarithm of 1+x (base e)., V. q) ]9 n4 t2 U8 ?, s
  4. The result is computed in a way which is accurate for x near zero.
    $ R0 G5 F: T# S: f& e/ h6 F
  5. >>> math.log(10)
    % l- g& H4 C5 y# [. g
  6. 2.302585092994046
    $ q/ t% E2 O" H  Y) u+ b& M4 D5 B' ^9 I
  7. >>> math.log1p(10)% B% p- ?9 w  F- j& [$ j
  8. 2.3978952727983707
    , I3 Z  z" w5 \* _/ h# X, s
  9. >>> math.log(11)* ?% F& h- {( P! b
  10. 2.3978952727983707
复制代码
- i5 b8 ~& k/ H( \: B# F. Q( T
math.log2(x)  返回x的基2对数1 }# g: e3 D! _/ A
  1. #返回x的基2对数7 H9 K: n8 W9 k9 ?2 U$ @
  2. log2(x)) [% Y1 {% J3 Z2 ]% s& \
  3. Return the base 2 logarithm of x.
    & n. V$ P4 h' R
  4. >>> math.log2(32)9 @( }, w# i3 A# T5 W5 K( ?$ m& g8 q
  5. 5.0  E; ?+ `$ [, W
  6. >>> math.log2(20), n- n- P2 }) n! y
  7. 4.321928094887363( r8 Z, B& ~2 B+ {0 ?4 |; g
  8. >>> math.log2(16)/ y% I) @) \9 f) ^& i" J
  9. 4.0
复制代码
' w0 Q$ ?9 e, E9 V3 T, b+ [
math.modf(x)  返回由x的小数部分和整数部分组成的元组$ x0 l2 F: {, A* J
  1. #返回由x的小数部分和整数部分组成的元组
    ( K& O5 @* Z* S9 L1 Z% P8 m6 }) B0 v
  2. modf(x)
    9 _7 Y5 ?, z6 l' _( N5 }
  3. Return the fractional and integer parts of x.  Both results carry the sign
    3 d) _! l, m" _: v! Q8 p8 f( t1 {
  4. of x and are floats.8 @& w  ~- v* U+ L7 N
  5. >>> math.modf(math.pi)
    ' F3 E6 }0 T' K8 K3 B. N
  6. (0.14159265358979312, 3.0)
    ( ]% Q, Y: L+ \# C8 Q7 C
  7. >>> math.modf(12.34)* V/ m; @! b* T, M! v/ D
  8. (0.33999999999999986, 12.0)
复制代码

( j4 U; i/ g: K- s0 V6 Z* R0 c. v( D9 ?math.sqrt(x)  求x的平方根
" G% R* z: w. ?; n+ M2 L: r
  1. #求x的平方根
    , b9 a* x/ S9 o+ N$ P5 @
  2. sqrt(x)
    5 R/ }: o" H) L6 |* }. p
  3. Return the square root of x.% X8 ?: x; k: d, D1 o( l& @% y1 I
  4. >>> math.sqrt(100)
    . ?6 L  J9 z5 p# j
  5. 10.0. W5 a' @& v) [+ Q: K2 G" j4 T- q$ u
  6. >>> math.sqrt(16)/ }5 i6 c4 t8 g/ k
  7. 4.0# [$ o/ b: u4 u! Q# N5 E9 A% d
  8. >>> math.sqrt(20)
    6 U7 f. `8 c. _# O3 F# R7 L7 G
  9. 4.47213595499958
复制代码
# S  k* W6 D) A3 s  k
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    3 G. G. `. u7 e: K0 _/ J
  2. trunc(x:Real) -> Integral
    7 z% G: [2 w  ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.$ s8 G/ r  Y8 _* X" ~! ^5 v
  4. >>> math.trunc(6.789)
    , z5 p% e: x8 A' t2 k) m
  5. 6/ C. B: D$ ]- t" E
  6. >>> math.trunc(math.pi)6 S3 r( k/ U9 S9 [9 ^1 N
  7. 3
    * ~8 a) K7 v- J7 l( O) z" }
  8. >>> math.trunc(2.567)
    5 ?9 g( P: z2 L8 Z/ V# Q" z& u- b
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-10 13:28 , Processed in 0.101171 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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