新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
- B; v4 [( g3 a: }, h. t
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
  n6 O0 I3 X  R( `. T; L- {
5 W# a7 O4 x: v  c, k
方法1+ f4 T# \( Q  X
  1. >>> import math* i8 Z/ _0 D! n9 h" \
  2. >>> math.sqrt(9)1 \, b* r( {- H: s( u( O% g
  3. 3.0
复制代码
方法2) e9 _0 Q+ x' t6 x
  1. >>> from math import sqrt# _& R; m" O  y) n" R
  2. >>> sqrt(9)
    ( l3 j8 \1 F1 ]/ A0 ?
  3. 3.0
复制代码
" D: L+ a! J- r" E


# w* {' u! U% W" I
math.e  表示一个常量
3 c& u6 F2 L) L) D5 \
  1. #表示一个常量$ n# _: O" k+ h& `
  2. >>> math.e6 m5 J2 d5 E1 x/ W: D8 u
  3. 2.718281828459045
复制代码

/ Y5 E+ J, l3 }& u% {/ nmath.pi  
数字常量,圆周率

9 h; o% F; Y, ~0 h& @2 t9 z2 [; P
  1. #数字常量,圆周率' _# Z6 W9 k; ~$ H* }  X
  2. >>> print(math.pi)
    4 V4 r  w6 L+ k# {
  3. 3.141592653589793
复制代码

' V' [' T- b5 p' I) ?4 T$ Amath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
: \7 W" o( N) `0 c* J
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    . [) n/ i3 H) a7 j& W* _# u
  2. ceil(x)% M0 [$ y) ~$ j. ?8 s/ K
  3. Return the ceiling of x as an int.
    $ F6 f) S  i/ q* y0 M
  4. This is the smallest integral value >= x.% R" P* M9 c$ v6 l% e# U1 H: z

  5. # [+ W/ s" Q1 Y, Q( q/ r# R: m
  6. >>> math.ceil(4.01)( {( p% |: w8 o, G1 p( C! A
  7. 5" w/ v7 u# v7 P1 \2 i, ^
  8. >>> math.ceil(4.99)4 _, Y6 u/ ~) d* P4 N
  9. 5
    / P, B, v# j& H- }. ]. t
  10. >>> math.ceil(-3.99)
    , O7 q2 D1 x! ]
  11. -3! y" I* |5 S6 F" N8 C
  12. >>> math.ceil(-3.01)
    , ?' ~" V) O$ P1 P, A9 P0 m2 Q
  13. -3
复制代码

0 B0 j0 t& ?1 S! L) R5 Z) D3 ]5 ?: {0 Gmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 C! Y& q, W+ c8 B6 {
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ' ^9 d9 p0 v+ s- K
  2. floor(x)
      T* {  b' g; }) q- M
  3. Return the floor of x as an int.
    7 b0 K$ p( y: t- j# n+ a
  4. This is the largest integral value <= x.
      |1 R7 ?' k; y2 c8 b/ n7 }# ^
  5. >>> math.floor(4.1). N4 ~5 z" e2 g5 I9 n
  6. 4: W1 G: B2 Z3 s8 l4 h
  7. >>> math.floor(4.999)
    ' d  |; W  t+ e9 i' o4 \
  8. 4
    $ ]* \$ Z2 h. J# u/ n1 Q1 v5 f
  9. >>> math.floor(-4.999)1 Q! R* u4 |; A. J8 k7 H3 `
  10. -5; c$ r7 a: A7 G8 ]
  11. >>> math.floor(-4.01)
    , p/ G: T, Y( u9 o9 v& O! j
  12. -5
复制代码

4 ^0 @- M  }+ \6 a1 @; D, zmath.pow(x,y)  返回x的y次方,即x**y7 z. U. t. x/ P2 G. F8 N
  1. #返回x的y次方,即x**y
    , p4 ^1 \$ @4 n8 m( ]& T; G
  2. pow(x, y)5 w+ a; C9 v4 F$ n
  3. Return x**y (x to the power of y).
    1 e( D. Y6 _+ W5 F8 l
  4. >>> math.pow(3,4)
    6 [- ^. P: W" a5 |. Y
  5. 81.0
    # g/ L& O- e% M" i& S
  6. >>>
    . V" \& X6 E& |4 j( G6 ?4 Y
  7. >>> math.pow(2,7)
    & c; W& m- r( K
  8. 128.0
复制代码
2 {2 Z4 g" S$ F5 f8 m$ O0 V7 E" o
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)6 ?* k& l5 |. b" j7 u( Y9 Z
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    / {1 ]  s" \1 N) |2 x" O
  2. log(x[, base])
    $ C' m5 ?% U2 R6 ?. Q
  3. Return the logarithm of x to the given base.
    6 V; p7 Z5 J6 w- @
  4. If the base not specified, returns the natural logarithm (base e) of x.( b* g. ?$ W  K  f7 U/ E
  5. >>> math.log(10)
    5 n) Y' R# r0 {2 h
  6. 2.302585092994046
    ) \0 ]' A+ j: L8 ]" \
  7. >>> math.log(11)  Y' D  E6 \8 N: F
  8. 2.3978952727983707
    . G3 M- S+ r  O1 b6 \0 R' F2 P
  9. >>> math.log(20)
    & B( s& b5 X. I; S; ^; \& i* e1 k
  10. 2.995732273553991
复制代码

0 a) H0 a+ c& \7 X6 N+ rmath.sin(x)  求x(x为弧度)的正弦值
# r. d) E4 h8 O- S1 |; S
  1. #求x(x为弧度)的正弦值
    + p4 D( w% e# B9 s
  2. sin(x)' `( k( {: }" V
  3. Return the sine of x (measured in radians).
    ( h$ w. p/ D# M
  4. >>> math.sin(math.pi/4)
    $ e2 {0 C; M- f/ l' [7 N/ C- x& u2 Q
  5. 0.7071067811865475: Z1 m6 k1 S/ ]1 r) G% ?+ \5 M, c
  6. >>> math.sin(math.pi/2)
    1 N2 T- v, }/ c0 q
  7. 1.0
    $ `& i6 \  }9 v7 B( `
  8. >>> math.sin(math.pi/3)
    + b- Z1 V5 ~! n+ _1 E
  9. 0.8660254037844386
复制代码

" e) m7 A! m3 P+ W) o) \  Mmath.cos(x)  求x的余弦,x必须是弧度4 k$ n3 o9 ~$ V8 j' ]
  1. #求x的余弦,x必须是弧度
    3 D, l) Q: p* L' V5 y
  2. cos(x). L2 V) ?( i, X4 m
  3. Return the cosine of x (measured in radians)./ o: n+ t- y/ x8 P3 K
  4. #math.pi/4表示弧度,转换成角度为45度( Y8 N, [( j/ }5 v" a5 r
  5. >>> math.cos(math.pi/4)7 ^: @4 F6 i7 _* x3 W7 ?
  6. 0.7071067811865476! [. `) H( f/ [2 u- y
  7. math.pi/3表示弧度,转换成角度为60度
    : R; J" j$ u/ H- v, X1 h" ], p
  8. >>> math.cos(math.pi/3)
    & [/ V( `- X. E5 @
  9. 0.5000000000000001
    , [6 i& o% i: V+ [* r- w1 O7 L
  10. math.pi/6表示弧度,转换成角度为30度
    $ \7 E/ l  W3 c1 b
  11. >>> math.cos(math.pi/6)# Q1 }" H, m" h4 [# i/ N
  12. 0.8660254037844387
复制代码

; @- W6 M7 q. J  t9 Zmath.tan(x)  返回x(x为弧度)的正切值
+ U' t2 Q9 B3 D* q  c/ X* T
  1. #返回x(x为弧度)的正切值
    + r3 Z% z7 W/ I4 Y: z2 P) N
  2. tan(x)
    " f8 l- v! ~+ D( C$ H3 x0 w3 `
  3. Return the tangent of x (measured in radians).. _6 O+ c* }+ g; d
  4. >>> math.tan(math.pi/4)
    + ], M( c+ A/ E2 ?( a
  5. 0.9999999999999999) x; o: q3 D- |; h+ Z; \6 T
  6. >>> math.tan(math.pi/6)" k+ @& V/ [: u' z' N- s! y
  7. 0.5773502691896257  y# X, h/ [; r1 _6 p/ T
  8. >>> math.tan(math.pi/3)
    + v0 m7 _) j9 H) D& G; V
  9. 1.7320508075688767
复制代码
8 }$ G  f7 t2 L
math.degrees(x)  把x从弧度转换成角度
9 X& ^1 K8 R! O, |& s4 d  Q
  1. #把x从弧度转换成角度/ k9 z, I. Y. ^7 K/ Q: w" h9 z
  2. degrees(x)
    1 Q8 M4 c8 K3 E5 U6 ~3 a' Y) l$ {
  3. Convert angle x from radians to degrees.
    5 S" X# m  G% ]- B) Z
  4. 4 T9 Y3 y4 i+ Q8 l5 {2 ]
  5. >>> math.degrees(math.pi/4)! R* Y+ l" e, d+ S
  6. 45.0* O8 h, Y( x$ D( s
  7. >>> math.degrees(math.pi)- g" u7 `* h& P) k- ~, o
  8. 180.0
    0 J* z" p) x8 V/ p8 h% ]3 `. u! n& ^
  9. >>> math.degrees(math.pi/6), z) a6 F$ J) A
  10. 29.999999999999996
    - @* K1 C. t% @* x1 H, i9 A$ W( x
  11. >>> math.degrees(math.pi/3)# P2 e5 Y" u0 k7 Q1 v
  12. 59.99999999999999
复制代码
* g2 R( l1 Y1 Z/ ?- }
math.radians(x)  把角度x转换成弧度) [) z) m9 G7 Y- }
  1. #把角度x转换成弧度* z% I2 i4 C& d9 G. s
  2. radians(x)
    2 S! e' z$ S! x6 L' D/ c' r
  3. Convert angle x from degrees to radians.
    # d* W1 P) M" T* a! [
  4. >>> math.radians(45)& Q: S1 R" @3 s/ [( |! z2 Z* @
  5. 0.7853981633974483
    4 I) U+ v3 }, ]+ `' K# X
  6. >>> math.radians(60)
    8 @8 l  k8 l9 B+ S$ p5 O
  7. 1.0471975511965976
复制代码

# h% I* y# Z, D: O" `9 [0 W8 Amath.copysign(x,y)  把y的正负号加到x前面,可以使用0. C8 ]. H4 r* t5 p6 _- M0 x2 y$ a
  1. #把y的正负号加到x前面,可以使用0  V7 F! D1 ?3 \- q
  2. copysign(x, y)
    & K  n, f2 K# G/ e! c
  3. Return a float with the magnitude (absolute value) of x but the sign 1 Q# d$ s+ ?0 h5 I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ; _* |* r0 ?% z  i
  5. returns -1.0.2 t2 M5 B" I* O, p- Y' v
  6. ; p+ B- p6 j  U3 _2 i
  7. >>> math.copysign(2,3)
    3 R% ?9 t8 C  t$ ~
  8. 2.0* W8 ]3 J. b! [( ]. n
  9. >>> math.copysign(2,-3)
    ! I3 R+ C2 l5 B- a' S/ [* P5 ]
  10. -2.0
    ( K; R" m$ Z8 v7 h  D$ l, f) v# B( G
  11. >>> math.copysign(3,8)) k" n. y- X  S) V; \; M' B
  12. 3.0
    2 S7 Q4 f7 M* ~0 j
  13. >>> math.copysign(3,-8), }, [' |% R$ z
  14. -3.0
复制代码
8 K% [4 x0 C9 ^
math.exp(x)  返回math.e,也就是2.71828的x次方7 C3 D& P# i2 @3 r& z3 Y5 e6 r5 H  ?
  1. #返回math.e,也就是2.71828的x次方
    9 `/ z8 G2 }& ^
  2. exp(x)
    9 O2 C) Y( G2 Q; m* ?
  3. Return e raised to the power of x.
    3 p0 ?, ?  E+ J5 W# U9 p. p$ {: l
  4.   |, ~* X- R! X( J+ ^/ x1 g7 D* k+ s
  5. >>> math.exp(1)
      T# C$ u/ J, c' h8 `( m# |) T
  6. 2.718281828459045
    ; }3 i  E. H  P6 a
  7. >>> math.exp(2)! p; Z8 x- y5 F0 n  t
  8. 7.38905609893065  h' m+ P9 g) ~. u
  9. >>> math.exp(3)/ m$ r7 v! t1 L& b6 L4 k$ e. M
  10. 20.085536923187668
复制代码
5 A  V1 K+ }# w8 N+ b2 }% |) C# y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
1 i6 o% e: D0 O9 F) l) A( h
  1. #返回math.e的x(其值为2.71828)次方的值减1
    % r) c0 J+ \/ V: {
  2. expm1(x)
    : f, P2 U* J* i
  3. Return exp(x)-1.
    4 R2 e' o0 X/ |  G0 U/ d- J
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    0 ?# j1 d/ \( Z: q/ N

  5. ( d8 t9 t+ F% I
  6. >>> math.expm1(1)
    9 Q4 s: D, K' z/ q
  7. 1.718281828459045
    $ \( H5 X- X8 [$ [( J4 o' p. g' ~
  8. >>> math.expm1(2)
    , q) C, P" Z- P7 B
  9. 6.38905609893065
    ' P" Y1 T8 y0 f: V
  10. >>> math.expm1(3)
    2 O, S% v( ]1 f- a- R7 @; u1 b( q
  11. 19.085536923187668
复制代码
" Q' _5 D% z9 P& s  _) ~) l, R0 v
math.fabs(x)  返回x的绝对值' X2 }$ z( X; k9 {; `
  1. #返回x的绝对值
    , f4 \: }3 j- q9 E% M+ o
  2. fabs(x)
    5 x, ~! c' o7 O3 x% k# z
  3. Return the absolute value of the float x.+ D4 Y# r8 `& t& w! |. g9 [6 m
  4. # j# y0 v% j5 I, G& K& h2 E
  5. >>> math.fabs(-0.003)
    3 P/ Y6 q1 [, |+ G2 J* m4 c, U, Z
  6. 0.003
      L0 a- T  Q) X' z- m) n* c7 _) i
  7. >>> math.fabs(-110), X- A, l* D: {& X5 t
  8. 110.0: C  x3 C; i6 e$ T0 X' H+ P: o" ~
  9. >>> math.fabs(100)
    2 Y0 r0 C. k/ W1 s2 m; g
  10. 100.0
复制代码
4 c+ O4 _: s, E2 X, D
math.factorial(x)  取x的阶乘的值2 ]8 m4 [$ v  t
  1. #取x的阶乘的值( N0 B6 U  y" m: A. S% S
  2. factorial(x) -> Integral6 L% h$ i/ }9 O/ H
  3. Find x!. Raise a ValueError if x is negative or non-integral.% w8 V. x8 H- S1 w  |1 h: _
  4. >>> math.factorial(1)8 o$ J4 y) e/ g, h: J* V& X
  5. 1
      c% X3 f% [: ~+ k
  6. >>> math.factorial(2)
      x7 a$ I7 f2 u/ e+ y5 z4 X" L
  7. 2* U" [8 c$ D2 C8 T) T
  8. >>> math.factorial(3)
    2 C! X0 ^8 a* X& S) @
  9. 6
    ; Y+ k) W( y% J& D, C
  10. >>> math.factorial(5)
    7 l3 [0 {4 T9 z6 i6 t$ s
  11. 120
    8 V5 V9 N( n2 ~$ @) z2 O1 B' b
  12. >>> math.factorial(10)) C1 ^2 w6 y8 A7 ~. b: w- h
  13. 3628800
复制代码
! i- B/ n% [) P, ?8 K
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数7 f$ g" L# s7 q9 V5 b1 l4 C$ |3 I
  1. #得到x/y的余数,其值是一个浮点数
    0 Q9 V6 L2 B6 }- E/ s: Q2 O
  2. fmod(x, y)
    0 {# g" K  G' l7 l" _
  3. Return fmod(x, y), according to platform C.  x % y may differ.) V, r6 w0 W  |& X
  4. >>> math.fmod(20,3)
    # n8 C% q) C; b; B' \6 Z
  5. 2.0/ u/ Q* ~# g9 p0 A) L
  6. >>> math.fmod(20,7). p& T# l, E: z8 d% i
  7. 6.0
复制代码
/ \) h- ]& B4 O7 b9 E
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
0 \% Q  W0 n* e( y4 G' P
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    $ f( E2 C8 e3 p( Q( D! C1 x6 X4 ]
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" ~) r' H# j+ F. N5 I) d
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ! i6 f5 F  K& s! r  r6 \1 w
  4. frexp(x)
    5 j# Y) x- W! \9 W' n
  5. Return the mantissa and exponent of x, as pair (m, e).
    4 r" b$ e5 {: [) h
  6. m is a float and e is an int, such that x = m * 2.**e.
    5 B" a) f  B9 `9 R9 O0 _  O5 S' Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.' ~; |8 W- N' g* E, R) u, M% `
  8. >>> math.frexp(10)
    1 I* u9 T* l2 C0 ]
  9. (0.625, 4)
    9 f- }8 o0 R' t- K
  10. >>> math.frexp(75)5 I; ~$ [) I, k
  11. (0.5859375, 7)
    ) s+ \4 v0 D. w' b; B! A9 f! w0 K
  12. >>> math.frexp(-40); p' ?1 o4 h. Y% |+ i; X) U
  13. (-0.625, 6); `( B" }4 E* v! h
  14. >>> math.frexp(-100)9 q: Y- E3 z1 S9 O# G0 ]. U; b/ n
  15. (-0.78125, 7)
    / }# |" m. ]: L" z' d
  16. >>> math.frexp(100)' m+ h3 L- }5 Q+ o* v
  17. (0.78125, 7)
复制代码

! P& P  Z+ e% ~" Umath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列: X# Z5 H, h$ K
  1. #对迭代器里的每个元素进行求和操作
    : W2 t0 S. D2 Z- S' D1 O7 [
  2. fsum(iterable)
    + A# a; {, P" k9 _% w. r3 T. X* d
  3. Return an accurate floating point sum of values in the iterable.
    & ~6 Z: m6 B$ \# }( H# i4 o! {
  4. Assumes IEEE-754 floating point arithmetic.& G; |6 B6 p7 S: ^* F
  5. >>> math.fsum([1,2,3,4])
    & D5 E  W3 f/ ~' B! z) {
  6. 10.0
      ]  J7 c/ B0 N# W4 [$ x+ W5 f9 e
  7. >>> math.fsum((1,2,3,4))
    5 {6 d& i4 A" u
  8. 10.0
    9 H5 [/ {2 m3 o4 u& L
  9. >>> math.fsum((-1,-2,-3,-4))
    4 L4 J" I3 K* S% c; S. ^, P+ Y8 G
  10. -10.0
    6 M7 |2 ]1 T) o- o8 b
  11. >>> math.fsum([-1,-2,-3,-4])
    5 H* ]2 X  L5 E3 M
  12. -10.0
复制代码
9 Q, L3 F; X5 j3 @' b# g* h
math.gcd(x,y)  返回x和y的最大公约数
, P/ v: ?2 m& r& }+ w4 U% ]
  1. #返回x和y的最大公约数
    ! C7 ^& p+ I6 U: Y
  2. gcd(x, y) -> int, ?' `% Z) Y" D' K
  3. greatest common divisor of x and y9 r5 \1 l4 `( G1 N$ {  }! J
  4. >>> math.gcd(8,6)" U. m3 ]. m% N' d# n4 A8 u: p, r( _
  5. 26 ?2 o2 r& b' H2 q8 C7 F8 j# C7 {
  6. >>> math.gcd(40,20)  W. u  |5 M4 ~* k
  7. 20- H1 }1 j" K$ \  K, D2 [
  8. >>> math.gcd(8,12)
    " x7 u+ N( j# c( Z5 b: U3 F4 x
  9. 4
复制代码

7 _5 ^8 R& @; \* Smath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
6 d; w" w8 |& e4 c' x, _
  1. #得到(x**2+y**2),平方的值0 a- j( }7 w- P& B' x
  2. hypot(x, y)
    $ C* v8 }. I+ U' C9 x: }& V7 G& `1 X
  3. Return the Euclidean distance, sqrt(x*x + y*y).% B; Z: Z5 v$ U2 {- t4 ^) N
  4. >>> math.hypot(3,4)" f( d, r/ `# B: P
  5. 5.0
    1 _# x) t8 [& q4 W6 Y* a# z. h! j
  6. >>> math.hypot(6,8)
    , g* y1 h  a* w9 ^9 E7 v
  7. 10.0
复制代码
  k& P( k, ~7 A/ z) L
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False2 C) E' b) u8 l* f8 I- j4 N
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 F0 |& A/ J4 O
  2. isfinite(x) -> bool/ m5 U5 P) u, Z3 ?/ R: k
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' b* l. _& c# T( p9 J* X
  4. >>> math.isfinite(100)
    * f! {9 u- L& \+ r  Y5 {7 n
  5. True
      K3 o, E- P; ?- u4 V! _0 ]. ^# a0 h
  6. >>> math.isfinite(0)
    , u4 I) p9 e: g8 e# i  K- i' J
  7. True. N* ^; O/ n. q
  8. >>> math.isfinite(0.1)
    9 J  Q' W# S/ b/ R" Q9 r2 O
  9. True
    ! p6 h$ y- Q4 Q
  10. >>> math.isfinite("a")
    ' {% b6 x9 Q$ K
  11. >>> math.isfinite(0.0001)
    9 t0 u  i5 Y% f1 y8 m
  12. True
复制代码
8 l+ C+ ^5 e* G8 ]6 K
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False+ L4 m; \* D9 p3 `
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False4 s1 z" l. T; D* j
  2. isinf(x) -> bool
    5 W& Y1 |* |6 ]" L# Y1 l3 a
  3. Return True if x is a positive or negative infinity, and False otherwise.
    , K6 v3 k# Y! V! E" {
  4. >>> math.isinf(234)2 a8 w# U5 G. j& B/ O1 p; b& x
  5. False! j# n3 t7 y8 f% P+ T
  6. >>> math.isinf(0.1)% Z8 i3 _, v0 F8 a# W
  7. False
复制代码
& ?% P3 Q& V% C$ g
math.isnan(x)  如果x不是数字True,否则返回False
7 k) E! u* Z3 ^6 M: G
  1. #如果x不是数字True,否则返回False' P7 W% s5 r" S9 ~5 r* E  p3 w7 F
  2. isnan(x) -> bool% `9 [. d. G% k/ n
  3. Return True if x is a NaN (not a number), and False otherwise.' u+ H" {1 ~% L" @
  4. >>> math.isnan(23)
    , l  _+ u; y  U( j5 j2 u
  5. False. [! N; `- o" ]- E# y/ j3 p, s2 }  O
  6. >>> math.isnan(0.01)
    $ T& e1 A; X4 _4 t# S4 F2 m
  7. False
复制代码

7 e' P, K' m" r. Y8 i6 f% ^math.ldexp(x,i)  返回x*(2**i)的值
( n  K. Q1 G& }5 S
  1. #返回x*(2**i)的值
    , l8 T$ X; L( _4 M' R
  2. ldexp(x, i)7 V2 U; a) j- _( v: m" |9 b
  3. Return x * (2**i).8 \, z( o  L; }! W7 E9 p- Y% o! S9 `
  4. >>> math.ldexp(5,5)
    & o3 _& }7 s7 F7 Y5 {
  5. 160.0+ J3 [& z+ J8 [: v
  6. >>> math.ldexp(3,5)) m* f2 O/ u* k5 P! t' w
  7. 96.0
复制代码

) ]: N+ I. E8 O4 p0 f; N7 ~math.log10(x)  返回x的以10为底的对数7 [8 k( R/ X2 G) t/ j# p
  1. #返回x的以10为底的对数: W; x& N9 E( s' M; R3 H4 i9 Y
  2. log10(x)0 T  z8 ~8 N  A/ b( D9 D' ?
  3. Return the base 10 logarithm of x.9 d9 b1 F; O/ C! [' R
  4. >>> math.log10(10)
    6 }2 ]* e1 s4 m, \
  5. 1.0, y: B9 w; Q4 D8 `
  6. >>> math.log10(100)+ Z* f0 |' C2 R, a: d' C; ]" Y* R' Y; [
  7. 2.0
    " S6 y8 x8 ?/ e; E
  8. #即10的1.3次方的结果为20
    7 Z9 j. S1 c- }" l
  9. >>> math.log10(20)& m, t" U; U; g% O% g: O1 x( s
  10. 1.3010299956639813
复制代码

3 f) n# @/ h6 t$ r3 T  Ymath.log1p(x)  返回x+1的自然对数(基数为e)的值
7 N* Q  O( e0 c+ o- d/ ]
  1. #返回x+1的自然对数(基数为e)的值) K" E' L5 f) O
  2. log1p(x)
    , K3 D" q' W7 Q( [% u7 W
  3. Return the natural logarithm of 1+x (base e).3 g1 p* t" O" V& b5 B5 [% u9 A
  4. The result is computed in a way which is accurate for x near zero.
    8 h# @6 E$ f! g# ]
  5. >>> math.log(10)9 |9 \, x- U/ E  z
  6. 2.302585092994046
      r" q, ^; G" G: B# {# x/ ?2 M0 X
  7. >>> math.log1p(10)
      o6 M! {" z) H( w  `5 P
  8. 2.3978952727983707
    ( V6 o( X! s5 T/ ^; |4 g
  9. >>> math.log(11)& o& d& m# q& r! _! j1 C
  10. 2.3978952727983707
复制代码
0 ]+ n1 q  E/ l! v" Q9 i
math.log2(x)  返回x的基2对数
0 o8 G( ~: d4 K5 P
  1. #返回x的基2对数
    5 E- q. ~! k. G0 V) d
  2. log2(x)! {; k$ g2 N" V2 N
  3. Return the base 2 logarithm of x.
    2 @% N# S0 H$ G4 `( w! r
  4. >>> math.log2(32)4 [: r7 J/ }  v/ H# a. K
  5. 5.0
    8 K% v* w& }) ^) P' y6 f
  6. >>> math.log2(20)
    , t. q' \# o+ f- h
  7. 4.321928094887363: q2 R$ {  o/ h$ r4 f7 |; q' D
  8. >>> math.log2(16)& O7 S% E7 J( N0 ]6 [) ^* r& `
  9. 4.0
复制代码

3 q$ }4 D8 {) D/ vmath.modf(x)  返回由x的小数部分和整数部分组成的元组% W6 y: H, q" N6 S4 D* X
  1. #返回由x的小数部分和整数部分组成的元组
    # D0 ?. K9 }2 B7 L1 V
  2. modf(x)9 g/ M* k7 O: K  V9 k
  3. Return the fractional and integer parts of x.  Both results carry the sign
    % S5 F8 n+ x7 V
  4. of x and are floats.; s: I$ n  d5 q- V3 q+ Q
  5. >>> math.modf(math.pi)
    - r1 ^7 c6 `- c4 K" L( D. ~
  6. (0.14159265358979312, 3.0), ^2 c. _( T! C
  7. >>> math.modf(12.34)
    * a. z" V: c+ f* x; R, L; x3 c' d
  8. (0.33999999999999986, 12.0)
复制代码

- k* S0 V9 N3 ~0 k+ S3 hmath.sqrt(x)  求x的平方根
* T* J' i$ t" H- T
  1. #求x的平方根
    ( C' ~- c  b$ z
  2. sqrt(x)
    ) U% ~$ s- {* k, q0 o
  3. Return the square root of x.
    , A% u+ F! g5 |
  4. >>> math.sqrt(100)+ S4 y0 |- D  a9 E- l* `% V! n
  5. 10.00 Q* ^, V  D, p' S, Y( t  x
  6. >>> math.sqrt(16)
    ) g  A9 i. @: p
  7. 4.0
    : j+ Q! T' P  [% g8 r* t+ u3 U
  8. >>> math.sqrt(20)2 V. G+ n' H3 {' l; X! E
  9. 4.47213595499958
复制代码

6 t: ~6 [4 ]( a3 S/ }7 f* g; ^math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    1 g5 J( x% ^( F* M3 Y4 y
  2. trunc(x:Real) -> Integral$ A% a) J: G5 J
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    2 Z6 y/ i! K$ C' U
  4. >>> math.trunc(6.789)
    - l& c+ e% }4 H
  5. 6
    / a0 y6 s$ Y/ s9 x5 N- w
  6. >>> math.trunc(math.pi)* q8 L- L/ a, f, l
  7. 30 M6 |6 S& m; n9 k
  8. >>> math.trunc(2.567)0 ^" q$ ^8 q* l
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-5 23:53 , Processed in 0.077762 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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