新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
* _5 u" y% [; }' Q# U" M
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" g( ~& @& M2 C* E" D! h

/ p2 V( f* s) w, Z方法1
6 p) t( B1 X. [5 o) g4 X7 |: k( D
  1. >>> import math8 }- F; x  x2 R6 w+ V- S* ^
  2. >>> math.sqrt(9)
    # M  p7 Q. e, T0 h
  3. 3.0
复制代码
方法2
+ W' t% [+ N& k4 h& X) y: z6 F5 G3 B
  1. >>> from math import sqrt
    5 Z0 J' C) _$ F' l5 [: R7 Y
  2. >>> sqrt(9)% y, m( `; D& ^5 Z
  3. 3.0
复制代码

$ B/ F" M' ~3 K' t0 Z# u9 N
; w7 T* P$ ?! o2 m4 \9 ]* ], S" g
math.e  表示一个常量
* `- ]6 m' q: r1 k+ d# a! K
  1. #表示一个常量
    1 [$ [4 A5 \2 k2 Y
  2. >>> math.e
    5 ?  W, c+ g3 t1 K
  3. 2.718281828459045
复制代码
3 u3 m7 B* q- Y# j/ p; c
math.pi  
数字常量,圆周率
4 a: H0 N2 N0 X: f0 m
  1. #数字常量,圆周率* o  _7 E% D0 W1 b/ }
  2. >>> print(math.pi)
    # l: P# R/ [% |4 ?% e
  3. 3.141592653589793
复制代码

( a. X/ s' `, D5 gmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

, S7 d( ?2 ^. P9 Q; k% i1 F/ Z& ?
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    " @7 ]. {) W8 s' L. d# L9 F% j* U
  2. ceil(x), I9 n( E' N( |6 R2 u
  3. Return the ceiling of x as an int.
    / i2 w/ j/ l2 q3 N+ i$ R0 ]% O
  4. This is the smallest integral value >= x.0 w/ r9 Q8 ~$ \" q9 V* q7 T8 t
  5. 9 j& E+ C% X7 g6 X* ^5 C; {
  6. >>> math.ceil(4.01)
    7 ^) U* g+ e+ }+ D6 v$ O
  7. 5
    6 _5 O7 e+ a; Y% M! Q9 y# k* T
  8. >>> math.ceil(4.99)
    0 R/ D3 f! P2 [9 h
  9. 5' Q2 k3 o. ?+ i1 b7 P8 e! k7 {
  10. >>> math.ceil(-3.99)8 i; A/ ~" J3 d/ M5 P7 x
  11. -3
    ) x- }$ Y2 ?' ]4 x9 }/ E
  12. >>> math.ceil(-3.01)$ e$ r4 [* E& z" Y5 y, M: Q
  13. -3
复制代码
1 D3 m3 w( d. a' ~# r6 K1 u
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 q! o  B: d9 A5 d3 D1 r' V
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身  P+ y0 e& A0 M! j5 i' N$ ]- B
  2. floor(x)7 [: }7 x- a+ ]- s6 C4 p
  3. Return the floor of x as an int.0 X' I* a9 J5 ]- X0 m
  4. This is the largest integral value <= x.
    2 S: W5 p6 b& p/ a# X
  5. >>> math.floor(4.1)
    + @3 ^  ^8 J& `8 O" I
  6. 47 l  Z4 S* r- r8 u
  7. >>> math.floor(4.999)( v, g) `9 p/ @2 s! e! }& x
  8. 4* L4 R2 f3 V' k- V9 i
  9. >>> math.floor(-4.999)+ J; S4 l$ E- h. b( e+ j% h' g5 v
  10. -5
    . m, n0 |( g& n8 K5 o+ I; w
  11. >>> math.floor(-4.01)/ A; `1 D& ]% o& Y' A; d
  12. -5
复制代码

* K" q  w$ P* _% O( Imath.pow(x,y)  返回x的y次方,即x**y
1 s, f( J1 B3 p
  1. #返回x的y次方,即x**y* d2 V/ u7 z; K
  2. pow(x, y)! R" k4 Q6 Q& m/ s, O$ B
  3. Return x**y (x to the power of y).9 U, i# {; t& x6 d- U
  4. >>> math.pow(3,4)  ^' g# |) }* T
  5. 81.0
    ( p; |5 N4 `0 _7 ]
  6. >>> , C% W2 e. Z# f) `7 {% N2 j  Y1 \5 y4 Q
  7. >>> math.pow(2,7)# X2 P( Y8 K) Y- C
  8. 128.0
复制代码

2 v# t- M. r7 Umath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)+ \9 G. k# A# X3 r
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    # T1 W( @1 Q6 z6 a1 m" o
  2. log(x[, base]). e" P# E9 I/ i* b
  3. Return the logarithm of x to the given base.- a8 }# S$ b5 v
  4. If the base not specified, returns the natural logarithm (base e) of x.) i9 \5 f: F  L% M* b& x
  5. >>> math.log(10); i6 B" y8 }! @# d* V
  6. 2.302585092994046" i8 p3 k2 B8 y) b1 E
  7. >>> math.log(11)
    # G8 E' |; {4 U6 S+ Y, V
  8. 2.3978952727983707$ f7 p% k2 j7 g3 S8 ]3 ?
  9. >>> math.log(20)* [( ?0 }- g/ d) o" |( X5 B7 g
  10. 2.995732273553991
复制代码
' ?/ u: ?9 @7 a) i0 N. z2 U. _
math.sin(x)  求x(x为弧度)的正弦值
' `, A3 ~% c1 I' c# b; N9 I1 s
  1. #求x(x为弧度)的正弦值
    $ p4 C, k* z6 D* N
  2. sin(x)! c0 X3 P4 A6 m0 j* ^+ h
  3. Return the sine of x (measured in radians).' u6 @4 s3 i7 B7 {0 r
  4. >>> math.sin(math.pi/4)/ y* p8 g: f1 m; T
  5. 0.7071067811865475
    1 J% v5 g2 L. w* U
  6. >>> math.sin(math.pi/2)2 W* |# K* G+ v7 M, J
  7. 1.0
    - }2 L( B- t+ n0 g0 C
  8. >>> math.sin(math.pi/3)
    ' b* K5 t6 B! u' y+ Y' J
  9. 0.8660254037844386
复制代码
2 b" Y6 m3 {) y, P5 o, C
math.cos(x)  求x的余弦,x必须是弧度' v  n5 I0 A& B/ x) }  G
  1. #求x的余弦,x必须是弧度
    " g. k( ]* ^  Y) t, ~
  2. cos(x)
    : _1 ]! W1 J1 n9 u+ q, w
  3. Return the cosine of x (measured in radians)., @# o. n; g, \$ R, i
  4. #math.pi/4表示弧度,转换成角度为45度
    1 ^: B7 }& U, U' C
  5. >>> math.cos(math.pi/4)
    # P6 q+ R  [& H. s/ j1 d
  6. 0.7071067811865476" k" [% H  V$ z) X& k! {# F7 T
  7. math.pi/3表示弧度,转换成角度为60度
    0 j. M, v2 \- V0 _# ?& {
  8. >>> math.cos(math.pi/3)) r$ J0 v, P3 |3 b, u, U" @
  9. 0.5000000000000001
    , t5 o5 ^# b3 W6 J
  10. math.pi/6表示弧度,转换成角度为30度
    ) X; T0 b; [0 @) o/ [) X8 e
  11. >>> math.cos(math.pi/6)
    9 [# @5 M; M- C
  12. 0.8660254037844387
复制代码
5 @7 n) r( E2 `& S2 o9 z
math.tan(x)  返回x(x为弧度)的正切值
/ Q( {0 \/ t5 W! c! `% N( s, ~$ X
  1. #返回x(x为弧度)的正切值5 u# v0 e/ L5 A1 y
  2. tan(x)
    ( Q: V9 H5 f; l( q% K+ n5 w5 g- ~
  3. Return the tangent of x (measured in radians).6 \. o) Y# ?  C3 q9 {9 ~8 M
  4. >>> math.tan(math.pi/4)
    " N, ]' b! |4 E, P( ^- V" y) v% X
  5. 0.9999999999999999
    + _% }, K6 S" k* j2 y6 t& t
  6. >>> math.tan(math.pi/6)" H5 w* X6 a, G2 w7 N% M9 ^2 p
  7. 0.5773502691896257* t! b& X0 `/ }
  8. >>> math.tan(math.pi/3)
    . Z  o  z8 r6 `
  9. 1.7320508075688767
复制代码
9 t. q( e, e; [  x/ W, A
math.degrees(x)  把x从弧度转换成角度
. B: e' ^) `/ M7 m, O+ c1 e
  1. #把x从弧度转换成角度# E" ]' Z' d/ g' d
  2. degrees(x)# U4 z- x" r/ M6 Z& `, E
  3. Convert angle x from radians to degrees.( ~) G9 q/ l3 M3 Q0 k

  4. 5 Y( r1 n' L& o: X
  5. >>> math.degrees(math.pi/4)
    * e( B7 z7 j& K
  6. 45.08 E" z( a+ B2 ~$ ~
  7. >>> math.degrees(math.pi)* Q. d' `; n" w
  8. 180.0
    3 i5 U' ?8 {7 c& w' i* ]" I* a
  9. >>> math.degrees(math.pi/6)
    % G1 |0 F/ K1 d
  10. 29.999999999999996
    # v; ]! a. c" d* V- M
  11. >>> math.degrees(math.pi/3)7 P* X6 n; A, m; n, \' @
  12. 59.99999999999999
复制代码
) l9 |) t0 j" T
math.radians(x)  把角度x转换成弧度/ Q6 [% g( L, U
  1. #把角度x转换成弧度. _4 |  K: V; L& {5 I0 M( [
  2. radians(x)
    # O3 S0 U: i$ a5 z: `. ~5 t, }
  3. Convert angle x from degrees to radians.' k- {. T. [" S+ O: _
  4. >>> math.radians(45)
    5 h. i8 m1 h  i8 K2 F) B  Y
  5. 0.78539816339744834 k% X' i5 ~( K' }0 {" S. Z6 Y, }, Y/ k
  6. >>> math.radians(60)+ P( J* b5 _; ~# a; k" n9 D
  7. 1.0471975511965976
复制代码

7 _: g8 l, X  X( X" X0 l8 u( B7 mmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
. C. P: |  O4 F' d7 p  P6 z3 r
  1. #把y的正负号加到x前面,可以使用0
    ; O1 l' T' a* [, ^. W- ^9 Y
  2. copysign(x, y)
      ^: l9 S) p2 }2 Q3 u
  3. Return a float with the magnitude (absolute value) of x but the sign
    ) n1 `1 Y; u3 T  N
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) R9 b6 a+ k+ W" `8 a% _1 Q
  5. returns -1.0.6 ^4 w! O! ?& W2 G% I6 o

  6. $ o0 D0 I! g% Y: d
  7. >>> math.copysign(2,3)
    & A0 {3 t  I& z1 j
  8. 2.0
    $ O* {/ f! D4 D
  9. >>> math.copysign(2,-3)1 D8 w0 O$ G1 d7 h  L. F
  10. -2.0
    9 s2 L- g& K% h0 `5 G+ ^% d4 C
  11. >>> math.copysign(3,8)0 b8 W7 @' d' ^9 `
  12. 3.0) l8 X  ~' ^+ J4 S( X' `' v  R1 Y
  13. >>> math.copysign(3,-8)  e" S+ A6 m) F# b% p: }$ R0 d
  14. -3.0
复制代码

" H$ C. Z/ ~" ~# k- m, N0 t: U* ~math.exp(x)  返回math.e,也就是2.71828的x次方' g& ]2 A  ?& S  Q4 o% [- X% I
  1. #返回math.e,也就是2.71828的x次方5 \4 ]$ T+ x/ i
  2. exp(x)  A  E. [. X% s. O' n! R
  3. Return e raised to the power of x.3 G' e( P! K3 {3 T& F

  4. . U: ?3 v/ G( ]8 |7 O
  5. >>> math.exp(1)7 X+ {0 Q+ d1 e) ?8 U3 T
  6. 2.718281828459045
    4 d8 i3 b( d, I' p( t
  7. >>> math.exp(2): J  p! o: v* z9 W) d. E/ p
  8. 7.38905609893065
    3 M4 c+ t% C; Y3 b& x+ D! o' K
  9. >>> math.exp(3)9 ~, \( l; v  {8 {) c2 M
  10. 20.085536923187668
复制代码

9 \$ @( D9 L: u) _math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
. l5 k  c9 w, ?5 ]5 G8 n
  1. #返回math.e的x(其值为2.71828)次方的值减1
    " ]$ K$ t5 N0 N& A" e
  2. expm1(x)
    ( y  M) t. A4 h, ^% c/ ^
  3. Return exp(x)-1.% `0 i- H  `/ W' z5 |- b9 x. D7 y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.) w6 F4 c6 z+ x
  5. ( ~1 X" v) f# K( N4 p  m  c3 c$ H
  6. >>> math.expm1(1)
    5 g' D0 i0 v7 B: u
  7. 1.7182818284590458 d6 n7 @! s1 r6 S2 d
  8. >>> math.expm1(2)
    ! ^9 U, H7 _! j
  9. 6.38905609893065# }! ~& m& [( G4 U: \2 h
  10. >>> math.expm1(3)9 ]4 s& }& Y7 K9 N0 L( X/ m- B
  11. 19.085536923187668
复制代码
/ W# J: e) B+ s1 v
math.fabs(x)  返回x的绝对值8 g- @6 Q- e& m1 J1 g9 e* J
  1. #返回x的绝对值+ J9 Q- H# R. ?
  2. fabs(x)& ~/ x4 f( t$ b8 Q$ t
  3. Return the absolute value of the float x.
    ) x1 Z% h' h) u. X: I$ F% i8 r

  4. 8 q8 P, I4 c+ ]6 L( n
  5. >>> math.fabs(-0.003)
    9 l% s4 d2 Z- }) f
  6. 0.003! [9 r& Q; M: p
  7. >>> math.fabs(-110)
    8 B" {$ @0 V, D
  8. 110.0
    % G4 g  b0 o/ M6 k1 o$ M
  9. >>> math.fabs(100)
    $ \6 I# |! M3 a# |, f7 b
  10. 100.0
复制代码

2 T- B( q" R8 A9 r4 }7 h: r2 zmath.factorial(x)  取x的阶乘的值
0 R& q" u/ u7 I( ~* `8 n
  1. #取x的阶乘的值* V3 `, ~9 p& A, f' d! [/ _( d- q
  2. factorial(x) -> Integral7 S9 |- J5 U! S/ Y# |! t7 C8 z
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    6 v1 z4 s1 e  a$ ]" G# ?& B
  4. >>> math.factorial(1)
    ( ^7 H' [1 n4 @
  5. 1! F; w2 A9 a5 r
  6. >>> math.factorial(2)8 b: j5 m2 s7 h2 E3 S
  7. 25 R3 D& N0 z! }, f- }0 k6 w
  8. >>> math.factorial(3)
    6 S" Q0 \2 J7 n5 ]/ z1 L0 \" V
  9. 6  H/ I/ s2 l! Z! N
  10. >>> math.factorial(5)
    ) ^' q! H* w9 D3 r) G- y2 ]
  11. 120
    $ f  c1 f' O2 ?  s7 M2 a' H8 G
  12. >>> math.factorial(10)6 `3 ~: z7 \$ i8 \
  13. 3628800
复制代码

( A7 I  y8 f2 e7 tmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
- ^6 [" `, w6 b- Z8 N
  1. #得到x/y的余数,其值是一个浮点数! p) R1 j/ q% ~8 c# E4 G& K% ~9 o9 {; t
  2. fmod(x, y)
    # ?1 ]+ ~8 T# P. X) |4 o. C3 S
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ( o, O9 x! T! J; `8 y* O
  4. >>> math.fmod(20,3)
    . I3 y8 J* o1 @9 ~2 o; Y
  5. 2.0$ z6 l' v! \) t- s5 Q
  6. >>> math.fmod(20,7)
    ; @  M' x' M$ s: G
  7. 6.0
复制代码

9 L. u& f1 g( Kmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围" o8 N- u3 ^& P/ z
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' {% L) @% q% _# B( G1 S
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值+ Z* t7 M1 H, e' j
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1% T* Q3 B/ e# e9 v4 H+ i
  4. frexp(x)
    : e% r: H1 Q( ~
  5. Return the mantissa and exponent of x, as pair (m, e).- q/ s4 e' l4 P) h
  6. m is a float and e is an int, such that x = m * 2.**e.# j0 s9 K  G2 C) ^" }7 r) `: b
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0., O5 I+ B6 Q: i' K8 J1 \
  8. >>> math.frexp(10)
    ! `  r, r! x* D
  9. (0.625, 4)- T7 X7 w6 h2 l. G1 w
  10. >>> math.frexp(75)9 W: Y2 W8 T$ L  I& M
  11. (0.5859375, 7)
    ' h' I, n: K( n# V
  12. >>> math.frexp(-40)
    / Y2 U: L, C) G3 C8 b: V. y
  13. (-0.625, 6)
    ! T4 \% Z8 z9 P! ~
  14. >>> math.frexp(-100)  k& D$ c' q5 }2 e. D
  15. (-0.78125, 7)& r" z4 _3 T1 k3 k1 v/ G
  16. >>> math.frexp(100)
    # B3 P/ }3 o- |' @
  17. (0.78125, 7)
复制代码

: w* H% X6 ~5 v3 p( tmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列# F; e, x! U) z4 i+ g# A: D$ x
  1. #对迭代器里的每个元素进行求和操作  n6 t' Y7 \& v. R5 ]3 u0 l3 c
  2. fsum(iterable)& i/ g6 d9 b- ?+ `! C/ |; a& G5 Q" _2 h
  3. Return an accurate floating point sum of values in the iterable.
    # t1 h, R' V2 J* g; m. B, e- ~
  4. Assumes IEEE-754 floating point arithmetic.+ r7 f4 y" h# `# a
  5. >>> math.fsum([1,2,3,4])# v# V% Q/ y, l
  6. 10.02 C0 a5 _/ c1 g
  7. >>> math.fsum((1,2,3,4))# v' b. q5 Y2 f& I& S. D8 p$ }
  8. 10.0. ^$ d' J% q- i& I, O9 I
  9. >>> math.fsum((-1,-2,-3,-4))5 g+ {4 }9 x9 {2 Y- A4 Y4 j
  10. -10.0
    : Q- e9 j9 X+ |2 [3 a
  11. >>> math.fsum([-1,-2,-3,-4])- Q5 d' @1 @4 f# J0 I
  12. -10.0
复制代码
- s$ Y% V! ]  s" E9 _
math.gcd(x,y)  返回x和y的最大公约数
' R+ m# E7 }5 N$ Q- R5 h
  1. #返回x和y的最大公约数
    1 y6 L0 x* N# M9 E! I4 @6 u
  2. gcd(x, y) -> int
    / z; X  O2 j2 N. m( T5 u
  3. greatest common divisor of x and y
    0 |1 P4 A; A4 ^# O; @$ Y% ^
  4. >>> math.gcd(8,6)
    7 R: n, x8 N0 y  M& S
  5. 2
    / F0 Z4 C5 g8 h% H4 I, J2 z
  6. >>> math.gcd(40,20)
    , l+ {6 P% |3 M7 K& |
  7. 20( R  j) y$ J% ]* U3 F& ~& x: ?8 ]
  8. >>> math.gcd(8,12)
    * x% f5 Q) h4 P2 h
  9. 4
复制代码

5 n  G2 C2 w9 j7 q' Xmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
. g. E2 V# M5 h6 u! n( d& k! R
  1. #得到(x**2+y**2),平方的值0 d1 S' [! s/ s, V
  2. hypot(x, y)  c& o/ e. [+ l0 Y
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    # i& j% d" z( z+ I, B- u
  4. >>> math.hypot(3,4)
    " c; [" O: u, g. `) z# N. X' R, b; u
  5. 5.05 s  a0 a6 S% m" c% g
  6. >>> math.hypot(6,8)! p7 V% c* y/ j: y0 ?
  7. 10.0
复制代码
( l+ y- c# W1 [! @" r. h, V
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
; a  s6 X" Q" H  A
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    9 _) C" V2 ^/ e9 h0 t
  2. isfinite(x) -> bool
    . J/ s% Z: b' O3 }
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.0 ^2 R- B4 _* H0 |& g
  4. >>> math.isfinite(100)- U/ {. F3 A& C1 z  a3 x
  5. True
    0 h, ^9 L& n6 f1 P
  6. >>> math.isfinite(0)
    * s% J2 h# p8 R2 e) V6 w
  7. True
    7 [" v0 R" U# p! ]) p; A5 E
  8. >>> math.isfinite(0.1)6 m$ r9 J4 N  i0 }  H2 e
  9. True
    * Z" J7 P) @5 N
  10. >>> math.isfinite("a")$ b! B  H* j4 R% X" G0 O1 h. F
  11. >>> math.isfinite(0.0001); L) I8 _; I$ U/ g8 Y
  12. True
复制代码
2 O5 D, r3 ?  h+ K& P1 M
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
" S! v& i/ B6 b0 {
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    # M" Y, f7 v: \7 e. L
  2. isinf(x) -> bool" M( _- D- K) B' x
  3. Return True if x is a positive or negative infinity, and False otherwise.& d+ n2 d8 Z/ W; b! I
  4. >>> math.isinf(234)
    # x( I# K) h9 P* x
  5. False
    3 K, r( ?! y3 d$ {. @; ~
  6. >>> math.isinf(0.1). G  T0 L2 a- m0 M1 H
  7. False
复制代码
( |: W+ Y: Z  A8 b( B6 w; G
math.isnan(x)  如果x不是数字True,否则返回False) I$ ?: v+ a# z8 v: o
  1. #如果x不是数字True,否则返回False
    ' T* V+ y: c0 U/ H) C& E8 A
  2. isnan(x) -> bool& K+ E: w" V) a! j
  3. Return True if x is a NaN (not a number), and False otherwise.
    , h: d2 {) c9 w5 u
  4. >>> math.isnan(23)
    ! w! U8 {6 m$ y8 t
  5. False  E7 F  c8 }: ^' e( P; I1 ~' o9 H
  6. >>> math.isnan(0.01)
    9 |- i9 V  K8 L/ `& N1 R
  7. False
复制代码
; J5 T, Q, [. g- g6 D3 P
math.ldexp(x,i)  返回x*(2**i)的值
3 X- W" O( T5 x) E9 [
  1. #返回x*(2**i)的值3 ]' a; ?% p7 ?0 Q5 v) `
  2. ldexp(x, i)6 P$ F+ i6 l, B/ O8 H6 Z7 k) j
  3. Return x * (2**i).
    . @0 M( N- f" e# }
  4. >>> math.ldexp(5,5)
    * k5 j$ \+ b9 {: @8 x2 G+ M. t
  5. 160.0" S3 a+ B% T0 B, \7 S/ ~
  6. >>> math.ldexp(3,5)9 e1 y# {0 Z( o# L
  7. 96.0
复制代码
" O5 |% ~% N2 V
math.log10(x)  返回x的以10为底的对数9 t, @1 e+ w0 P, Q* ~  J
  1. #返回x的以10为底的对数
    " M( o2 C' h" r. Y
  2. log10(x); ^9 ^# g* Q! {' H# D% L
  3. Return the base 10 logarithm of x.9 ?/ |3 p/ v( M1 n* I
  4. >>> math.log10(10)
    : G* ]" e7 c. U" k" x7 {
  5. 1.0) v6 I+ a9 g  Q) y
  6. >>> math.log10(100)
    , |2 k& p3 B, P1 q  r* Z
  7. 2.0
    * R& i% P  X' z5 s! Z( J
  8. #即10的1.3次方的结果为20* D+ a' Y* ]: y
  9. >>> math.log10(20)
    ! D1 E! S6 M. S4 n
  10. 1.3010299956639813
复制代码

' p( ]6 n2 q) g5 l: n/ }math.log1p(x)  返回x+1的自然对数(基数为e)的值5 i) X$ p/ e: b/ T
  1. #返回x+1的自然对数(基数为e)的值
    8 P' r3 H8 A8 P. V
  2. log1p(x)
    9 K& h4 _* p% \, U8 a. o
  3. Return the natural logarithm of 1+x (base e).! ^' \+ o6 x7 M% f4 C
  4. The result is computed in a way which is accurate for x near zero.
    * Z) ^' s2 Y" Y1 m
  5. >>> math.log(10): @7 \$ ?6 d% j; K7 }& }' @. K! t
  6. 2.302585092994046% S- a) g" W* c% l( S4 v6 [$ ~5 V
  7. >>> math.log1p(10)
    - A& }5 U. }! l* G; s
  8. 2.3978952727983707  t% v$ t/ S/ _1 s
  9. >>> math.log(11)
    7 f0 V* b3 Y+ ?% E+ O& }
  10. 2.3978952727983707
复制代码
( W9 X5 D& d$ q; i4 X/ ~: \" r
math.log2(x)  返回x的基2对数
+ ~$ A  a& m# G3 a3 V) U
  1. #返回x的基2对数$ ~) v& \0 p9 L" w# T2 U) q
  2. log2(x)
    ) m0 d* {+ s; t; g! u, R( j; h3 f
  3. Return the base 2 logarithm of x.
    4 _8 Z4 u* j0 W( v" @
  4. >>> math.log2(32): r+ o9 N& u0 N6 n( w' D
  5. 5.0. W6 I% U' C* P* v  e
  6. >>> math.log2(20); h& q( @& x' U
  7. 4.321928094887363$ l  E5 Z( ]. e) O2 p& s
  8. >>> math.log2(16)- X! {, O2 P0 f9 |( G) o% I8 d3 w
  9. 4.0
复制代码

5 y! u" O/ F3 l1 b) J/ Kmath.modf(x)  返回由x的小数部分和整数部分组成的元组
* }  {+ T1 \- L! R: z
  1. #返回由x的小数部分和整数部分组成的元组" y3 I' D) N! l+ Q/ h* R+ x: n
  2. modf(x)" x1 n' W) _5 ^; F) I
  3. Return the fractional and integer parts of x.  Both results carry the sign
    4 h- d4 ^' m  r) j, q' r
  4. of x and are floats.. `4 o7 p/ t* B% q$ t: p2 S
  5. >>> math.modf(math.pi)
    1 V+ U- p. @% e  I; f
  6. (0.14159265358979312, 3.0)
    1 u* _4 D3 I2 U  T4 Q1 q0 O
  7. >>> math.modf(12.34)
    # `. M  ]' w: c. z
  8. (0.33999999999999986, 12.0)
复制代码

8 S* H: ?( z, o4 ^2 V7 C! C3 tmath.sqrt(x)  求x的平方根
& P- H: M1 c5 X2 w' S, L% V9 O/ r. d
  1. #求x的平方根
    + C  b" d% Q0 e
  2. sqrt(x)* J* I8 d# m- {+ C* D1 `3 B
  3. Return the square root of x.  u& U1 s  S0 G- f
  4. >>> math.sqrt(100)& y/ e3 b. K. D2 I
  5. 10.0$ V# f/ ^2 g* i- R! j4 ~5 j7 P
  6. >>> math.sqrt(16)! Q% c8 D. A& \0 m- m: b, p
  7. 4.0" D1 U/ C/ L! ]* i
  8. >>> math.sqrt(20)
    5 I% k7 K" @1 j& }
  9. 4.47213595499958
复制代码
# ^: _' l3 K1 l7 j. |3 b
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分- @! w: b# {5 e  K4 p* m
  2. trunc(x:Real) -> Integral
      b9 d: {0 z: m
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    8 p! H4 i0 \" N; H
  4. >>> math.trunc(6.789)) F9 M+ j7 ]" P/ Z7 E
  5. 6
    9 c0 Q. K- k" B$ S& x" W
  6. >>> math.trunc(math.pi)" u6 a9 ?  A. R! d4 l8 B
  7. 38 b- r# Z+ M8 J
  8. >>> math.trunc(2.567). w4 V, N* n' A8 ^$ t; K
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-9 09:23 , Processed in 0.081410 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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