新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

# p( R4 }* H. k' N【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。* E5 a# _6 H" }/ U6 u+ [/ n
2 L: w/ R/ t* K' ~+ X! E
方法1) L7 e+ ?8 D; Q
  1. >>> import math
    ! u; j4 b, L* ^" F9 F
  2. >>> math.sqrt(9)
    % S2 u$ I9 y8 r
  3. 3.0
复制代码
方法2  {6 J3 l  {+ {8 e: F* y
  1. >>> from math import sqrt, L0 s4 t! x3 v  c
  2. >>> sqrt(9)" [8 Z& L+ ?8 L. ]3 j8 L4 a- ^
  3. 3.0
复制代码

! |- C/ ~, K9 |5 c

, {0 f% ^: `8 y2 y% i7 y
math.e  表示一个常量" Q% r$ z. E" C* }
  1. #表示一个常量3 _% S9 ^4 `) r: [& n5 v
  2. >>> math.e/ Q' R7 n: t3 _/ c) Y7 }5 m" A
  3. 2.718281828459045
复制代码
7 C( h' p0 C* k) p. s' e' \' i  J
math.pi  
数字常量,圆周率

, `" k5 J5 v& R! }
  1. #数字常量,圆周率2 I2 @' w# @; B, n5 }0 K
  2. >>> print(math.pi). W. A+ t. K4 L/ k8 R+ F. s7 p
  3. 3.141592653589793
复制代码

; b( J- y1 D0 C: x. |. ~math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

! g6 |" I: h. b3 }6 }' h
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    / I+ f6 i5 G4 C
  2. ceil(x)
    9 \+ c; `3 C  Q; ~6 D- T
  3. Return the ceiling of x as an int.
    & y8 H- `% v+ S0 ~. t" F
  4. This is the smallest integral value >= x.6 \  t/ \7 T3 H0 Q
  5. ; Y, u9 l. ?5 S1 D
  6. >>> math.ceil(4.01)/ a9 W% }! C: e4 u0 v: @* o" V
  7. 5
    - q) u* f5 J$ F* u
  8. >>> math.ceil(4.99)& g& M+ |; j: w) h$ a7 o+ j  w
  9. 5
    ! a+ t* q# i$ P1 J  B3 ^7 n
  10. >>> math.ceil(-3.99)/ `% y0 }& d! K
  11. -30 ]7 W5 y. \2 I3 O" H" p1 t
  12. >>> math.ceil(-3.01)5 w# l( ]3 u8 _
  13. -3
复制代码
8 S3 r" \( c: S* @' m
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) `! g0 W% k" f
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    : [$ h3 H' s. h1 R* h( F
  2. floor(x)
    / @* s0 ?: Y8 D4 E1 }% g* {9 e
  3. Return the floor of x as an int.+ r# b) q" I- k* e
  4. This is the largest integral value <= x.
    $ |# H; z! B* K$ c1 M$ u$ v( w5 s
  5. >>> math.floor(4.1), v8 F. }* T- j( u$ S, ]9 H9 |
  6. 4
    , g# a; G2 `: p+ L- H
  7. >>> math.floor(4.999)8 ^' e! w8 V  _! `/ I& S6 `" |
  8. 47 V, d! x* O9 R5 b+ ~2 x
  9. >>> math.floor(-4.999)6 H# q  r9 c0 T4 O; g
  10. -5
    ; \4 V. m, |, H& E$ T7 n! c9 ^
  11. >>> math.floor(-4.01)
    " D* R1 O* ^& k. s% ]: A
  12. -5
复制代码

0 C# x3 F( {& r1 G7 j9 O7 omath.pow(x,y)  返回x的y次方,即x**y
/ ~7 Q! O& Y9 R* E7 e- M$ K! S: u, [
  1. #返回x的y次方,即x**y7 E. |# E/ C+ P! F; ?
  2. pow(x, y)
    3 M+ a- A$ k7 p  a' a) }
  3. Return x**y (x to the power of y).
    ; v% n9 N% T) B1 Y% a
  4. >>> math.pow(3,4)
    ) s4 j" N; K6 R3 t# B  o7 B
  5. 81.0
    , e$ ]* u: ?/ o4 f8 H' H
  6. >>> " F( [% i: {) L) G
  7. >>> math.pow(2,7), ]/ w8 n6 W, @  y
  8. 128.0
复制代码

$ R" ~' t9 P# G1 W: v# }math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: o4 w7 f& t, h& [: y! ?( F+ W
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)3 u' Y" F$ T; H, R3 ^  b1 c5 f
  2. log(x[, base])
    % r& B1 L$ w* x* p$ Q2 |
  3. Return the logarithm of x to the given base.
    - c. {  c4 `: Z* ], R8 H
  4. If the base not specified, returns the natural logarithm (base e) of x.
    & [9 V9 C1 ?' U) G) E7 W$ l% h
  5. >>> math.log(10)
    5 q9 C8 e6 e8 _& |1 s) l! X- |! e
  6. 2.302585092994046
    / p4 u  R9 P3 Q. l3 Y
  7. >>> math.log(11)
    0 @" j8 l) R  m$ s" X8 `, a
  8. 2.3978952727983707
    7 F$ Z& u4 t! P& c! t4 ?% a; }! l
  9. >>> math.log(20)$ H; ^- N+ |6 E) H) t! U
  10. 2.995732273553991
复制代码
# ]2 Z5 M: l5 s2 E6 w
math.sin(x)  求x(x为弧度)的正弦值
- @6 z  S8 L" I  d' V# d
  1. #求x(x为弧度)的正弦值
    $ r. y# U$ f% \( D3 B
  2. sin(x)
    5 ?3 V" f! c% K: O; \
  3. Return the sine of x (measured in radians).- [* Q6 a- p, |. `. U  f
  4. >>> math.sin(math.pi/4)& \+ H' S4 ?; b. g  @& \" y+ I3 @
  5. 0.70710678118654758 S0 Q6 L; g3 [/ J1 O& ?% S  M! x
  6. >>> math.sin(math.pi/2)4 q0 J1 C' j* O8 j. F* `
  7. 1.07 V& S3 C/ Y6 I. O' k: U/ s$ ~
  8. >>> math.sin(math.pi/3)
    8 S2 f: {7 N" t9 t$ P% y9 {
  9. 0.8660254037844386
复制代码
5 R- Y% H/ w0 j. s8 a. \2 o& r# G
math.cos(x)  求x的余弦,x必须是弧度5 o5 y$ ~  f" ]( m& V; d' x5 G
  1. #求x的余弦,x必须是弧度# U; a5 d2 j& ^% [6 ]9 h2 w
  2. cos(x); U4 ?  o7 K7 T2 ]
  3. Return the cosine of x (measured in radians).7 u0 b6 J: o% E7 D+ V8 o. H
  4. #math.pi/4表示弧度,转换成角度为45度" ?- G" b5 s' @! o1 d; ]
  5. >>> math.cos(math.pi/4)
    8 R# V8 {, _9 s' g+ r* A8 J' u/ d. V
  6. 0.7071067811865476
    4 U2 {8 e5 v0 S; n; Z5 d! s
  7. math.pi/3表示弧度,转换成角度为60度
    , c2 H$ }8 K1 k  P6 p
  8. >>> math.cos(math.pi/3)# E1 R$ [! ^" p6 r7 S
  9. 0.5000000000000001
    0 Q+ v  y2 k( v3 y$ @. M# M5 g2 M
  10. math.pi/6表示弧度,转换成角度为30度
    , H( b$ |( |) m$ F9 h, q
  11. >>> math.cos(math.pi/6)2 V4 J6 Z( {6 K$ V
  12. 0.8660254037844387
复制代码

" E& H3 H( }3 }math.tan(x)  返回x(x为弧度)的正切值
- `3 O# a$ g" P' y8 U# D
  1. #返回x(x为弧度)的正切值
    * j0 N7 o5 u, J4 {$ G* f
  2. tan(x)1 ]0 p) O7 `( ^( B4 n8 c
  3. Return the tangent of x (measured in radians).
    : l& U1 U, U% G2 \
  4. >>> math.tan(math.pi/4); o, V) i2 J; F) n- y3 p
  5. 0.9999999999999999/ Y- d. F- B; G9 M. }" _
  6. >>> math.tan(math.pi/6); x( U+ Q% n- D" _5 u
  7. 0.5773502691896257
    0 P+ r) i; X+ J' s) F4 u& p8 m
  8. >>> math.tan(math.pi/3)- X1 n' U9 l6 s5 L
  9. 1.7320508075688767
复制代码
: C' N8 g( R0 j* S
math.degrees(x)  把x从弧度转换成角度
$ I0 N% b! F- E5 c# {0 T+ ^
  1. #把x从弧度转换成角度
    . G+ k: Y- L4 f4 i+ A) \5 e6 E* W
  2. degrees(x)& H' M' I  T- X2 Y0 B5 v. R$ ^  Z
  3. Convert angle x from radians to degrees.: k4 u- h$ n6 ^5 _1 i! _

  4. / Z$ p( @  l0 H8 w; d
  5. >>> math.degrees(math.pi/4)
    , I: U5 r$ `8 }+ x% \6 v, b9 a! f
  6. 45.0: M' r* ^* o9 Q! ?. o: h# l
  7. >>> math.degrees(math.pi)
    5 k3 l" y9 ]/ Z! {9 t, E
  8. 180.0
    : [0 B2 ^- Z7 c' j5 M% W8 @
  9. >>> math.degrees(math.pi/6)
    7 W; J" E9 [4 F; Z
  10. 29.999999999999996$ D/ ?& W* Z) W; D
  11. >>> math.degrees(math.pi/3)
      X# O9 `" m# T+ F" w6 A; v
  12. 59.99999999999999
复制代码

# k5 Q/ u  i+ x6 [0 ]4 `" amath.radians(x)  把角度x转换成弧度
: h0 Z- m& Y% M) r! K4 S' J
  1. #把角度x转换成弧度
    % e& {- u$ v# l, Y, N4 t4 v
  2. radians(x)" x* L5 [6 ?# I7 f2 S& z9 j) Q
  3. Convert angle x from degrees to radians.
    ) H  @0 l$ i$ ^0 {& V! k
  4. >>> math.radians(45)8 Z1 q2 D: O! {' J5 p* l8 z9 \9 x
  5. 0.7853981633974483
    , \2 c& \. S9 x. _; I9 b) v
  6. >>> math.radians(60)
    3 D) t  s8 m% f# H: D. m
  7. 1.0471975511965976
复制代码
6 _) H9 Q1 ~; W% C
math.copysign(x,y)  把y的正负号加到x前面,可以使用06 f/ b! m* C! q2 E( T6 r
  1. #把y的正负号加到x前面,可以使用02 A& H9 E1 I( x& ?, U2 m/ x
  2. copysign(x, y), i9 U4 K& C7 r! e6 A5 [
  3. Return a float with the magnitude (absolute value) of x but the sign
    + R  f# r# P" l
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) z5 F6 ]8 V0 U  O; p! Q
  5. returns -1.0.9 a1 W- [: f! O) ^

  6. " |( z/ s) w+ \9 z4 q
  7. >>> math.copysign(2,3)
    ' O" P0 r/ w; z, M7 X8 S
  8. 2.02 ?/ \' s+ s7 T+ d
  9. >>> math.copysign(2,-3)4 x# i+ j2 v# I) g
  10. -2.0
    9 p9 X  c( C, q: O  b! f
  11. >>> math.copysign(3,8)0 W+ A! h& p0 `+ S& T
  12. 3.0
    2 m- e4 g+ N( r# q
  13. >>> math.copysign(3,-8)) V7 {+ X* y5 _, L+ f! ]2 q
  14. -3.0
复制代码

( X% I' G5 e2 P: B6 p4 Dmath.exp(x)  返回math.e,也就是2.71828的x次方
2 G: t& w) u3 {! H
  1. #返回math.e,也就是2.71828的x次方
    4 y: t! d8 c, \5 g% S
  2. exp(x)
    " U4 K$ {& r/ W; D% \6 T  w/ ~
  3. Return e raised to the power of x.$ b: }2 w  d: K  L- Q. Y$ r% ?
  4. ! D: ^4 W. ^5 X: p; _9 f4 S
  5. >>> math.exp(1)1 U  `2 T* n* I1 P2 i( I5 L
  6. 2.718281828459045
    ! V1 h/ e7 ~$ A, F2 ~6 _3 f# q& Z/ X
  7. >>> math.exp(2)
    8 E: V0 K" K2 d0 X& ~
  8. 7.38905609893065
    ! t! x! A3 s& A; z3 i! F( Y
  9. >>> math.exp(3)
    % }  t1 _. c$ z+ W+ Z6 v
  10. 20.085536923187668
复制代码

4 W' J5 e4 h# D4 p1 m# v. P) A# nmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# U) m( J+ }2 [: g
  1. #返回math.e的x(其值为2.71828)次方的值减1
    0 Q- g1 r* f& z) Q/ s: n
  2. expm1(x)
    & |" _& X% F  K+ Q& i) @; x0 S1 w
  3. Return exp(x)-1.4 r& }. c% _8 o5 _! [2 O
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.0 z$ a+ F8 e. r1 p+ T& ~

  5. * _7 f. K% [# P) F7 h
  6. >>> math.expm1(1)! K# |, O% H+ u8 K7 m
  7. 1.718281828459045
    ' d# ?& B! ~6 P' s4 `2 U, @4 F
  8. >>> math.expm1(2)
    1 _: j8 @( u0 V' s( x% n: E
  9. 6.38905609893065& R4 n( u9 O( v) R0 k$ Q7 W
  10. >>> math.expm1(3)
    * ^# C$ K, B8 y! {
  11. 19.085536923187668
复制代码

& y, P) ]7 e3 P4 n: `5 K) Smath.fabs(x)  返回x的绝对值' S0 h) O: X) p6 Z& I$ s" h1 @
  1. #返回x的绝对值
    / |6 C* v! {  Y' q+ I
  2. fabs(x)
    ; a5 [* S9 s6 S: b' C
  3. Return the absolute value of the float x.
    . L# _/ m& m4 W) Y
  4. * B, \( A! h7 Z
  5. >>> math.fabs(-0.003)
    ) z$ m. c: I1 v: q0 l
  6. 0.003
    0 L: E& t7 D3 R! L/ U  e
  7. >>> math.fabs(-110)( m1 P9 s( B- w( \% _  }
  8. 110.0$ P* @$ p  h; O- z. m9 |2 U
  9. >>> math.fabs(100)9 W! N. d$ o5 r' M0 I. z+ c
  10. 100.0
复制代码
" o! _( U/ J2 ^& u1 n  D# X
math.factorial(x)  取x的阶乘的值
! z* A: g) E9 F# Z: |8 B
  1. #取x的阶乘的值
    - x) {+ S) k3 l& H" {
  2. factorial(x) -> Integral
    # Q8 E& G0 X4 s1 q8 O7 V& w! g
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; w2 }! U; |3 @, w0 j1 p
  4. >>> math.factorial(1)" |7 A: p! _/ Q9 s4 E
  5. 1
    ( H: R* z" l: z5 t: K+ v8 P
  6. >>> math.factorial(2)
    0 n9 y' R; W# l+ y% H
  7. 2
    $ T0 L3 L. }, @( a( b
  8. >>> math.factorial(3)
    $ b9 Y0 g1 n. n' W
  9. 6
    , e. H: S- [  |5 c  W" l9 e
  10. >>> math.factorial(5)
    3 @! W& ?. J3 ^8 b2 X! z9 g9 U
  11. 1204 q- r, S4 ]. B% i$ F; L: k
  12. >>> math.factorial(10)
    * p- F- Z5 u# U. }
  13. 3628800
复制代码
* ?6 O3 @+ a8 U1 K
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
5 u2 S$ n# i3 d
  1. #得到x/y的余数,其值是一个浮点数( I" [( A9 j1 c
  2. fmod(x, y)0 K6 N4 F& W: H& t2 I
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ! J0 V) `6 G/ x
  4. >>> math.fmod(20,3)6 E: x* E  E$ B
  5. 2.0
    % n7 x4 X: @- z- \9 B- O8 _  s
  6. >>> math.fmod(20,7), Q' F. h5 q# V; Z1 l4 g: M
  7. 6.0
复制代码
1 N; K% ~) o, r3 Z3 J; c2 Z; r
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围  k4 j3 {& H4 d5 R) \
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' Q; W3 m+ S* B
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    5 i% E" m6 Q) |$ g4 x
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, \! v, O# h( d0 p& v" o" `0 b$ J
  4. frexp(x)) I( O9 X- x/ S3 X) e0 U9 m
  5. Return the mantissa and exponent of x, as pair (m, e).
    : R; o. c2 b" g9 F( T6 W4 a
  6. m is a float and e is an int, such that x = m * 2.**e.
    ; |2 s% I! g, D' @
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    2 `( @6 {/ V: O# {; G: K: h7 W
  8. >>> math.frexp(10)
    * D% _: J+ a" i) n4 W+ Z
  9. (0.625, 4)
    * Q! v/ a7 p. g9 M. ^# R' e
  10. >>> math.frexp(75)% W1 u) [' Y& O5 I) K0 w# h
  11. (0.5859375, 7)
    ; e5 |2 E7 O  H3 B  K, L! R
  12. >>> math.frexp(-40)
    5 ]. r0 G% o5 d2 s! b( n
  13. (-0.625, 6)
    9 g, h9 r9 a% J1 _4 {
  14. >>> math.frexp(-100)
    * L5 C2 d  n5 }% \
  15. (-0.78125, 7)
    3 ^% b" n8 a) ]8 z# ]; B
  16. >>> math.frexp(100)# U( r1 ^0 n/ P' `0 y9 j
  17. (0.78125, 7)
复制代码
& p0 w  s( u* Q. D3 {7 |
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
- |. ^7 R4 f# G6 G
  1. #对迭代器里的每个元素进行求和操作
    " @+ h4 O. Y$ M9 {) g9 t
  2. fsum(iterable)
    " a: m1 o/ Z3 |" q
  3. Return an accurate floating point sum of values in the iterable.
    0 i# }* B( I8 }( {, `0 s9 Q3 Y
  4. Assumes IEEE-754 floating point arithmetic.4 p( D7 A4 Y; M! D! O' |) K  n
  5. >>> math.fsum([1,2,3,4])
    4 y; _  r7 s2 s) p; t, Z2 Z( k! ~8 `
  6. 10.0) H5 @$ j  o$ H/ v  h; S
  7. >>> math.fsum((1,2,3,4))& m; |, O$ H3 p0 I
  8. 10.04 ^0 s4 V/ V5 K
  9. >>> math.fsum((-1,-2,-3,-4))
    0 \' j- b2 o2 y. D% m
  10. -10.0  u; Y2 f4 o1 g4 _' u* F
  11. >>> math.fsum([-1,-2,-3,-4])
    0 `- j5 F8 b- G6 K' M8 z8 p/ A
  12. -10.0
复制代码
0 v7 b; w) D3 b3 j' k
math.gcd(x,y)  返回x和y的最大公约数
2 x0 r3 {$ t# n/ i, q
  1. #返回x和y的最大公约数
    3 b, C7 n/ `( h
  2. gcd(x, y) -> int5 c6 ^) _- I) Q8 D
  3. greatest common divisor of x and y
    0 `' C0 _9 s& n/ X' A3 o* R' Q$ K
  4. >>> math.gcd(8,6)
    0 C& a* y% F' |& f. x" R# O" m
  5. 2
    & z0 o/ ^1 F0 h* ]
  6. >>> math.gcd(40,20)) |4 N7 d+ A& R: ~  C) |$ ?" b
  7. 207 ?) b4 E9 S$ S, w3 `# k
  8. >>> math.gcd(8,12)8 C3 U0 ?, D% w6 L( o6 ^0 M: L6 @8 ]$ v
  9. 4
复制代码

5 |; W( C5 c( J' gmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False6 c% x6 \9 j! Z  ?5 y
  1. #得到(x**2+y**2),平方的值  g' ~# m* U3 b2 Q
  2. hypot(x, y)0 N- t# S9 y0 S1 T
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ; z; r! J# D$ W3 {0 N; b
  4. >>> math.hypot(3,4)
    6 j& I8 v4 n1 Y7 ?6 U
  5. 5.0" c/ V! k) a( [; S
  6. >>> math.hypot(6,8)
    ( I, m: Y( b- Y. L: n4 `$ c' F
  7. 10.0
复制代码

. t- }5 e2 g: G. n$ r+ J6 Ymath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False! G+ y9 a0 G8 `2 S  b
  1. #如果x是不是无穷大的数字,则返回True,否则返回False) w: j& U6 H. u& Y
  2. isfinite(x) -> bool( C$ u/ X; h6 h& V
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    1 {; [0 s4 N' y  K4 D
  4. >>> math.isfinite(100)% Q/ k+ `. @( `
  5. True
    5 U1 I! L; L& E# N
  6. >>> math.isfinite(0)
    + Z0 s6 N5 h) Q0 ^5 N! p/ n$ @' {
  7. True
    1 E# C& y+ i3 G! L2 r2 k
  8. >>> math.isfinite(0.1)
    % P) B- E- z# t) s6 v* L
  9. True. y0 N$ [4 Y4 m: i5 n
  10. >>> math.isfinite("a")! S8 _, `8 P7 e$ `
  11. >>> math.isfinite(0.0001)3 b* ?4 P2 z* h1 y
  12. True
复制代码
* ~5 O3 h% _  e; i
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False3 O. A: N4 |+ q/ k" X2 s
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False. g0 b$ |  s; {5 Q$ h
  2. isinf(x) -> bool* K0 l" u* ?2 l  A) j
  3. Return True if x is a positive or negative infinity, and False otherwise.
    / |; T9 w& l" K# N0 D3 z, x+ `
  4. >>> math.isinf(234)
    ! S. `  G: i! c2 f; o* W+ X2 u
  5. False; |  T/ F+ R$ o4 E0 `
  6. >>> math.isinf(0.1)
    $ Q/ W2 b( q4 X2 @: h
  7. False
复制代码
5 e  r$ J- w( N: e
math.isnan(x)  如果x不是数字True,否则返回False1 h; h5 m  ~/ z/ n( W7 O
  1. #如果x不是数字True,否则返回False* H# n' r) F& M; d
  2. isnan(x) -> bool
    % d! u  V2 r. p' g( T) |! [
  3. Return True if x is a NaN (not a number), and False otherwise.( q; S4 v  r# ^2 t* `+ m0 @/ {
  4. >>> math.isnan(23)) S. e8 E: i1 c0 J8 B' C) f" A
  5. False! \4 |" ]) h  _( Z
  6. >>> math.isnan(0.01)
    ) K$ ~' z9 Y# n! [3 E! L' I
  7. False
复制代码

& M/ y4 U1 x0 \* D. Rmath.ldexp(x,i)  返回x*(2**i)的值$ q, V9 D4 m9 ]) D
  1. #返回x*(2**i)的值
    - E; W0 e; ?+ m3 ^( L4 A1 H$ |
  2. ldexp(x, i)1 u- s0 \  n6 o( k
  3. Return x * (2**i).
    3 Y4 O6 h. [# O$ M: W! Q4 A- ~$ N
  4. >>> math.ldexp(5,5)4 }6 C( O' Z; N2 g) E; b9 M, h7 f+ ^
  5. 160.02 u" i% u9 h8 V
  6. >>> math.ldexp(3,5); v5 u2 Z4 m( H% S/ W: W* P
  7. 96.0
复制代码
' U* Z9 W' L) {/ {
math.log10(x)  返回x的以10为底的对数
& d; j1 ]  r0 m/ ~: u! x8 D+ M
  1. #返回x的以10为底的对数
    # m+ ~" e5 A, E
  2. log10(x)9 _5 B% Y5 }' d9 Q  }% S
  3. Return the base 10 logarithm of x.* n7 j! u' O4 Z: O$ r
  4. >>> math.log10(10)# V2 `- A) r, o# f) [
  5. 1.0. ^% i/ b' v) g
  6. >>> math.log10(100)
    , G7 d5 s6 Q( r
  7. 2.0* S& W! O7 t9 S/ Z8 a$ B  i
  8. #即10的1.3次方的结果为20$ z" |8 u# i( [$ @: @( p4 X
  9. >>> math.log10(20)
    1 g1 F9 r5 {& r, G' d' _* X
  10. 1.3010299956639813
复制代码
1 G; {) v. D5 S3 D: M1 y5 `  V
math.log1p(x)  返回x+1的自然对数(基数为e)的值
# n) C& {( {- f; ?# e( v
  1. #返回x+1的自然对数(基数为e)的值$ V! s0 a) `) s. M& z
  2. log1p(x)
    0 f5 z) G1 S% v1 z6 c5 _2 Q6 Z
  3. Return the natural logarithm of 1+x (base e).
    " s7 [" i/ z4 ?9 J) F! @1 i
  4. The result is computed in a way which is accurate for x near zero.
    + P: f/ @5 z& E0 ^4 y- X2 u8 r
  5. >>> math.log(10)5 i$ b2 s9 M% ^8 h: P9 L. l) h, {
  6. 2.302585092994046$ k5 z$ E: L5 B3 d6 p
  7. >>> math.log1p(10)% Q0 S) v8 j2 v1 s) n( s/ p; A
  8. 2.3978952727983707
    3 I& O7 m7 P& S/ x/ a4 S
  9. >>> math.log(11)
    # u6 Z' ~/ q& r: s6 O$ F
  10. 2.3978952727983707
复制代码
; R6 U. m7 S+ _% ^+ r
math.log2(x)  返回x的基2对数! d3 X9 j3 |  G6 |+ S
  1. #返回x的基2对数
    . X/ l7 o2 ?! ]* \) d9 f6 V
  2. log2(x)
    7 |6 a2 q& c2 D0 |
  3. Return the base 2 logarithm of x.
    6 v" z: z" q/ {4 N9 e! u8 ~1 a2 v
  4. >>> math.log2(32)) Y9 n' v0 |* k9 j; z' s8 V$ M
  5. 5.0! I) B, o: f: B9 |
  6. >>> math.log2(20)
    6 ^! {% R+ @+ l; _" }9 z$ y
  7. 4.321928094887363- M" W% h' p1 p3 H5 @: [
  8. >>> math.log2(16)  ^8 O. N+ A# f! L6 D( l) Z
  9. 4.0
复制代码
% T- x2 G; T& M$ @8 O. p0 B& }
math.modf(x)  返回由x的小数部分和整数部分组成的元组2 o1 _/ H+ R/ p/ p3 s1 p+ c
  1. #返回由x的小数部分和整数部分组成的元组4 A/ c& Q: c' s$ I
  2. modf(x)
    5 I9 h% ]  U8 X) ^( L8 T3 C
  3. Return the fractional and integer parts of x.  Both results carry the sign- Z9 V/ L- L, Q8 Y6 b! |9 O% ?
  4. of x and are floats.
    3 B  J  Y) g. g' ~0 m
  5. >>> math.modf(math.pi)3 _9 P+ Z/ [! J& `6 Y( O3 D& o$ M1 V3 s
  6. (0.14159265358979312, 3.0). _! H, |' L8 J4 d. X
  7. >>> math.modf(12.34)
    # H4 E4 Y4 B$ _9 g
  8. (0.33999999999999986, 12.0)
复制代码
9 q2 m" }: s- j# k4 S. E
math.sqrt(x)  求x的平方根4 P8 V5 M  T- R: g5 a$ `
  1. #求x的平方根
    ; Z1 \+ j( ^  m+ \1 P2 M
  2. sqrt(x)0 J% l! p" Z  `  |
  3. Return the square root of x., n1 t# }$ B! n6 n  m5 B5 Y
  4. >>> math.sqrt(100)0 |4 j. r* A' s+ y- }
  5. 10.0: \  q5 F( G! G& F7 x0 D% r- N
  6. >>> math.sqrt(16)1 L5 n( S4 h! M/ N% h4 D/ u
  7. 4.06 v2 s7 x7 i$ l  T& l
  8. >>> math.sqrt(20)
    : A  o0 b7 z: R$ u
  9. 4.47213595499958
复制代码
; V+ \9 a# U) V- W8 c7 b% F
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分: y* W2 I: K" c
  2. trunc(x:Real) -> Integral* q) t" s; ^- Y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    , N6 i% a* h' m. \& z4 V: C! d' c
  4. >>> math.trunc(6.789)0 X9 L6 o4 Q% v5 y7 i9 }
  5. 6
    * t/ {: ^+ |4 I/ j2 n
  6. >>> math.trunc(math.pi)
    & [( ^9 D4 h* f: p: w7 ^; k# w+ l
  7. 3) T' U! i. a0 [2 `  F* j
  8. >>> math.trunc(2.567)
    " s/ [6 X0 t6 v: j* n* E) ^
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-24 13:48 , Processed in 0.075012 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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