新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
+ r6 T1 V2 @" F5 X% Y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。! _+ \' @3 `8 i- A( t4 a* }$ V

" M- O* f2 ?- B+ w; `  F方法1
: c, M+ y1 w. o8 {* m& D% O
  1. >>> import math
      ^- D- j0 a) H- C+ ]
  2. >>> math.sqrt(9)* a8 `. b: A: z7 }+ A* C  i# S6 {
  3. 3.0
复制代码
方法2; |9 W" a" U; E+ O
  1. >>> from math import sqrt
    % L; V9 H3 X" d& Y
  2. >>> sqrt(9)3 V$ n0 M  d" s3 J7 l
  3. 3.0
复制代码
" \% @2 b* F1 A7 T# E

8 ^! I* q: U9 h# f
math.e  表示一个常量7 N* Y0 }8 T# k! I8 r! F: S3 g
  1. #表示一个常量
    3 G; p# x6 H0 F/ c# B& I
  2. >>> math.e5 O, M. T/ p& M- s7 V
  3. 2.718281828459045
复制代码
( a$ k% o" q/ y# A3 t
math.pi  
数字常量,圆周率
2 h8 M$ I  h) r/ d
  1. #数字常量,圆周率
    : {6 G" h1 w5 G2 M2 ?
  2. >>> print(math.pi)
    ' h+ G* L6 f$ X9 u$ I
  3. 3.141592653589793
复制代码

; u5 T: J; K( H# k# omath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 F* p- d& l) V% T0 P( L" L
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    / q$ Z8 R: n; z3 I
  2. ceil(x)% v6 c6 T6 g/ r! B; U
  3. Return the ceiling of x as an int.7 R- E3 |7 H6 F- L' Y
  4. This is the smallest integral value >= x.; A0 H4 U. ?5 r

  5. / ^6 {8 X* X1 T- n
  6. >>> math.ceil(4.01)
    1 g& e2 K2 [, ?' x& F1 X+ F
  7. 58 u0 V# V' F8 k6 U5 |! a
  8. >>> math.ceil(4.99)
    . Q; O7 O; {/ p( J
  9. 5  y4 k; A2 c1 f* z$ \' x: Q# D
  10. >>> math.ceil(-3.99)
    5 C8 Y0 v8 M$ o1 v8 W7 d
  11. -31 Y: e6 J4 c. p* p  c8 c
  12. >>> math.ceil(-3.01)- d2 C7 V3 ^$ a3 H5 c
  13. -3
复制代码
3 T$ M6 T: r* Z! H  }/ |
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& f) [* d- Q1 ]: z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身! W% z% n2 {  Y% B1 I; ?
  2. floor(x)
    : f/ x! m3 \( ^
  3. Return the floor of x as an int.$ }3 J! |7 s2 q# ~$ m9 J
  4. This is the largest integral value <= x.
    ; @0 h. o# Q, M+ x. g* b
  5. >>> math.floor(4.1)* k% f8 f8 h1 J! B# ]
  6. 42 H, M$ B; R3 Z+ [
  7. >>> math.floor(4.999)" z( T& W* q* T$ ]: l; r
  8. 4, U+ u( m. G( e4 r. z9 X* j9 R1 V
  9. >>> math.floor(-4.999). u9 `. ~2 S3 Q. c; C+ V
  10. -5
    ( ?" u! Z7 A) F# F
  11. >>> math.floor(-4.01)5 }" v: ^' `" @- @4 z
  12. -5
复制代码
$ F6 q# P- X" E' C
math.pow(x,y)  返回x的y次方,即x**y
& ]7 R! |' C$ A8 P6 L6 X
  1. #返回x的y次方,即x**y2 A, S* K4 _2 x' L/ o" a  P1 C$ P) q) {
  2. pow(x, y)* L& V' g5 X- z
  3. Return x**y (x to the power of y).
    8 U+ V( r7 Y/ j9 y! H: c
  4. >>> math.pow(3,4)
    ; P7 j8 N7 D* M* @9 m4 T8 y% l9 d
  5. 81.0
    2 S; o4 ~4 t7 v2 J+ I
  6. >>>
    . `( b: |0 l# Q% }$ K! A
  7. >>> math.pow(2,7)$ E# T/ N5 {2 i* ?" ]
  8. 128.0
复制代码
! r: |9 v9 v! C6 \
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
% {4 }4 L+ Q& }! B: {8 I2 C9 D
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  W6 {1 C8 h; S' Y8 q
  2. log(x[, base])
    4 Y+ T# [/ ^6 `6 v" U
  3. Return the logarithm of x to the given base.6 |9 c" J( Z  Q
  4. If the base not specified, returns the natural logarithm (base e) of x.
      \; w8 T1 \) e+ l  [  S
  5. >>> math.log(10)! x$ K% H4 c, W$ A
  6. 2.302585092994046
    9 W% X$ z- W) Y
  7. >>> math.log(11)
    7 W& b3 z! ]2 W0 S
  8. 2.3978952727983707
      j# i) I1 d2 Y* `
  9. >>> math.log(20)8 S4 S+ M) h, `0 B
  10. 2.995732273553991
复制代码

0 S& ]/ k7 C* y) f2 Omath.sin(x)  求x(x为弧度)的正弦值; ?( w! b* g+ L$ e6 s& u4 H
  1. #求x(x为弧度)的正弦值
    % v" C' n/ k  G) {( V9 v* C
  2. sin(x). f8 A% e7 K! Y$ e$ f
  3. Return the sine of x (measured in radians).
    4 z( |+ V0 T' A7 C2 A3 f% o* A
  4. >>> math.sin(math.pi/4)% U& a  W$ T8 U2 l' _
  5. 0.70710678118654750 o9 Y. X6 h6 g2 Y* n
  6. >>> math.sin(math.pi/2)5 g3 T2 {& K  O0 h
  7. 1.0+ ?4 |3 V5 a# c3 H- ?) P% }1 \& k+ Q
  8. >>> math.sin(math.pi/3)2 C, Q1 _( E: E) m& N
  9. 0.8660254037844386
复制代码

5 g( n: Z/ ?2 A% a, rmath.cos(x)  求x的余弦,x必须是弧度7 I9 e* s( e: G& _
  1. #求x的余弦,x必须是弧度
    ' ]5 m$ y( A+ |/ q3 J/ x. u
  2. cos(x)4 t* L7 `' h8 U; Q
  3. Return the cosine of x (measured in radians).
    6 \6 ~& }+ {! ^4 A# o
  4. #math.pi/4表示弧度,转换成角度为45度& S1 J" O) q2 ?  E- v( g
  5. >>> math.cos(math.pi/4)
    6 I  I: f3 U: H* S$ D6 \
  6. 0.7071067811865476
    7 o" Z" F# Z. [- r
  7. math.pi/3表示弧度,转换成角度为60度. u/ w( i1 j% h/ x& \& B, j2 Q
  8. >>> math.cos(math.pi/3)8 f$ W, T& l$ @  X8 w% U# W6 j
  9. 0.5000000000000001$ m* A4 X3 L$ M/ Q  G( i6 r. O" s3 ^' A, ]
  10. math.pi/6表示弧度,转换成角度为30度8 W; R) \- T$ F4 |' e
  11. >>> math.cos(math.pi/6)3 l: s; d# F) F) l+ h' }# ?
  12. 0.8660254037844387
复制代码
( x. \8 w3 f( e9 V
math.tan(x)  返回x(x为弧度)的正切值% G# \2 B" ^4 O
  1. #返回x(x为弧度)的正切值- l0 ~) s% a; t2 d1 e
  2. tan(x)! W$ o6 W9 H: @
  3. Return the tangent of x (measured in radians).
    ! Z3 @  K0 Z) h" D
  4. >>> math.tan(math.pi/4)
    ) N/ s2 Z2 y1 E4 `* V# w, Z" J
  5. 0.9999999999999999; {4 i* y0 H) c' |" w7 ~9 ]+ `
  6. >>> math.tan(math.pi/6)
    % x. U0 t' F8 @% t4 N- f
  7. 0.5773502691896257. F. W$ N4 g# D, k  E, J
  8. >>> math.tan(math.pi/3)
    8 O: c4 p  x7 [3 B& W" A
  9. 1.7320508075688767
复制代码
" \* e$ b5 Q: m) t* l- B9 c
math.degrees(x)  把x从弧度转换成角度. h/ r$ [# T% }/ l3 `5 j2 t
  1. #把x从弧度转换成角度
    2 J0 R( L8 _$ o) D1 w
  2. degrees(x)2 R9 W: ]/ @" _) n4 f* a4 a: s
  3. Convert angle x from radians to degrees.
    " ~+ J9 J/ w5 O# D. L- h9 T
  4. ; {- v# B' \  [# B, z4 ^% _- c
  5. >>> math.degrees(math.pi/4)
    : U8 n& y0 s7 F
  6. 45.0
    ' F1 E5 U$ z2 g2 ]0 c
  7. >>> math.degrees(math.pi)
    4 {6 E, q6 {4 Q6 _
  8. 180.0
    * c6 Q; h" t* w! Y8 h, i
  9. >>> math.degrees(math.pi/6)8 P& ?8 I$ o! Z3 C) q$ Y( Y
  10. 29.9999999999999964 h. N5 \1 {% T5 e/ r( F
  11. >>> math.degrees(math.pi/3)
    + Y# ?% g; ?9 D$ _0 u. k2 L
  12. 59.99999999999999
复制代码

) F3 e1 }- q( \8 _- tmath.radians(x)  把角度x转换成弧度# }- g) e0 r5 P( ]5 L1 G# ?
  1. #把角度x转换成弧度8 Q* V- W6 C& e/ F
  2. radians(x)' G6 F7 s- o0 _1 [. x  v* m; a
  3. Convert angle x from degrees to radians.! b$ n( d% e5 D( u9 w+ T
  4. >>> math.radians(45)
    5 |+ R6 ^& T" N( O1 ~7 W
  5. 0.7853981633974483
    $ ]  N  O4 m# T- j& J
  6. >>> math.radians(60)7 L, e" s' o' W. }( G1 s8 N
  7. 1.0471975511965976
复制代码

6 O- }+ m0 o6 F4 ]$ b# xmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
3 b2 o  E1 H; f6 T) h7 F4 B* v
  1. #把y的正负号加到x前面,可以使用0
    ' C, F$ G6 i& l8 s0 c
  2. copysign(x, y)! X) B. O& a3 P  e/ ^3 N
  3. Return a float with the magnitude (absolute value) of x but the sign / @& _: }6 b: x) q
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) " [0 L% O$ Y: ]# k" n% I
  5. returns -1.0.
    , h& C5 s4 |' O0 x' L  s, ~- @
  6. ! g" E- B. }( c* _
  7. >>> math.copysign(2,3)
    ; _% U5 K4 r# `1 Q. S
  8. 2.0
    - r9 A9 P+ R1 K: _' k( d$ b9 G
  9. >>> math.copysign(2,-3)
    9 @3 F3 z/ a" }. F/ c! J
  10. -2.07 X# h+ z+ V) j. a6 O
  11. >>> math.copysign(3,8)/ `# V$ I: ?* K! G1 d: G
  12. 3.0/ E% d1 V0 j. c6 R/ w( k4 w  S
  13. >>> math.copysign(3,-8)) s4 J. x- |. l6 Z( D
  14. -3.0
复制代码

1 b4 p$ r, {2 u/ m% _% z: b. M. |math.exp(x)  返回math.e,也就是2.71828的x次方
9 Q, B/ m6 p- o' e1 p2 R. C& g5 e# D
  1. #返回math.e,也就是2.71828的x次方$ M5 i) H8 F; `2 ^3 G/ s: S
  2. exp(x)
    9 y! \6 C3 R; y4 x6 x" N
  3. Return e raised to the power of x.3 G$ [/ l) Q+ w& C
  4. + ?1 i! i2 k! n/ D
  5. >>> math.exp(1)9 N1 ~% J) C) s. c! j4 N, Q
  6. 2.718281828459045* u- q) W: [% i% @& i- G
  7. >>> math.exp(2)4 x8 B. M+ F) Q
  8. 7.38905609893065
    ' q2 W& Z/ R# e. m' \3 o- b0 q
  9. >>> math.exp(3)4 }8 B% A& V( C9 B0 v
  10. 20.085536923187668
复制代码
$ [8 U" d3 F5 O" H) n& A: v( d
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减14 a) [3 R2 J! i. @0 ]# S: w& v; g# A5 R
  1. #返回math.e的x(其值为2.71828)次方的值减1: [. Y9 T  x% W- }1 `7 n
  2. expm1(x)
    % w8 I, d0 U( q3 T, X2 S# g
  3. Return exp(x)-1.
    3 J1 U) k' o8 C
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    6 }3 \& E/ ]% o$ _& }, D# n& L
  5. " T! O: O3 ?+ l# c' O3 v
  6. >>> math.expm1(1)
    ' x* ^9 ^) Y/ M3 h  j- q; i2 {& X9 Q
  7. 1.718281828459045  ~" F; y( v, m4 J. U% v: I
  8. >>> math.expm1(2)
    2 U! s* Y4 `/ J" H* Y
  9. 6.38905609893065
    " w8 `3 [8 ]+ J8 A/ ]
  10. >>> math.expm1(3)- f0 l# h" [% T# Y6 B6 w
  11. 19.085536923187668
复制代码

, o2 j& d' y' T5 U4 [/ P' H3 b" Umath.fabs(x)  返回x的绝对值
& ~7 _( ^( Y- h6 T6 k
  1. #返回x的绝对值
    - [: k! ~; V8 u
  2. fabs(x)' A0 x9 p( x/ V7 [, ^
  3. Return the absolute value of the float x.# I- q- m4 L7 `( T  w3 j
  4. 8 z% i9 E7 L2 k
  5. >>> math.fabs(-0.003)
    0 E/ T# e$ o3 }# y
  6. 0.003" |5 F8 g: B; J+ S+ b
  7. >>> math.fabs(-110)5 E4 }* q7 ?; t9 j; m% A9 w
  8. 110.0
    5 _  y4 I/ W3 C7 j& F
  9. >>> math.fabs(100). s! i* _% O: @0 c' n
  10. 100.0
复制代码

8 I7 U4 O" @' |+ Q6 ^+ i/ ymath.factorial(x)  取x的阶乘的值1 ?) D! i$ ^$ M5 t; g9 o5 S& q
  1. #取x的阶乘的值
    5 j! x( f" N: ], F8 d
  2. factorial(x) -> Integral
    9 K5 O, z& @: C  Y6 ~
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    . E8 H7 g2 `2 n7 H
  4. >>> math.factorial(1)1 k) |. |" `3 r* N* z
  5. 1
    ! m: o5 ~  q( {* H$ i( w
  6. >>> math.factorial(2)3 {' u- {1 A$ m! |) W/ j* n. ?
  7. 2
    # ?9 r9 h: Z  k3 ~
  8. >>> math.factorial(3)1 F% E: f8 x% \. o8 z) T
  9. 6
    / S5 @; \2 e9 b$ f5 a" b
  10. >>> math.factorial(5)/ e$ [5 a  T' |/ R' ?9 i
  11. 120' x  G  d) A! g. P
  12. >>> math.factorial(10)
    + p7 f$ x* [( R6 o$ o& e0 r
  13. 3628800
复制代码

0 ?: L' U2 _, \& a1 ^: Vmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
; z) g) v4 @& A8 S. }
  1. #得到x/y的余数,其值是一个浮点数& R0 E& r- {; ~# q7 C8 z
  2. fmod(x, y)
    6 J- N6 c3 _) k( A1 k. B- D0 j  e4 \1 X
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    $ I/ B2 ^  m) j- s; |* D
  4. >>> math.fmod(20,3)$ S" u9 M. ?1 s3 d4 w8 ^) g3 ~
  5. 2.0, J5 l" _  z0 v5 l
  6. >>> math.fmod(20,7)
    3 c$ r. {# |4 s+ w" m5 T; R
  7. 6.0
复制代码

+ I& ~8 X. _+ kmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围6 Z! g6 m, O# ?- J* G' e8 w
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,/ r& D1 K6 ^8 M! C! S( O
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值! L  V; ~$ O- v2 @
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ! X; M2 q6 @  j  i: p( z
  4. frexp(x)
    / c% F/ H( @4 N6 N8 B( p
  5. Return the mantissa and exponent of x, as pair (m, e).6 p9 R1 t2 X) _) n- ^5 f
  6. m is a float and e is an int, such that x = m * 2.**e.& R+ V: x& ?" f9 \
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    " ~( l$ L  P1 F6 o
  8. >>> math.frexp(10)  @: F3 j; E7 K6 [4 }( [4 b
  9. (0.625, 4)
    5 A! V, V  Q5 n' Y) j- L9 B6 g- h* F
  10. >>> math.frexp(75)
    - M( N2 I" h% G& k( e2 _2 q+ f
  11. (0.5859375, 7)' _6 x4 B7 r% U0 p1 c  S) B( ^  a
  12. >>> math.frexp(-40)* o& B+ A' V- ^" q, p; @0 `
  13. (-0.625, 6)8 v  ^6 n. [2 S
  14. >>> math.frexp(-100). k4 V/ b& m3 l+ c8 g) G2 ~
  15. (-0.78125, 7)
    / u6 K: d( W" Z! j
  16. >>> math.frexp(100)
    * G% j) v! O: \* l7 k
  17. (0.78125, 7)
复制代码

' M; d2 h1 l3 C. K; L: xmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
# k1 k' h& Z$ \
  1. #对迭代器里的每个元素进行求和操作
    9 l+ x3 m, J+ W2 H$ w, W
  2. fsum(iterable)
    ) L5 I. ]  R& L7 ~! e% ?2 ~$ y4 |
  3. Return an accurate floating point sum of values in the iterable.
    , _1 M+ i2 c8 Y) L* Z+ d
  4. Assumes IEEE-754 floating point arithmetic.( M" y1 h+ z: B0 i% U
  5. >>> math.fsum([1,2,3,4])
    1 o$ z& p# P9 ~" d: h
  6. 10.02 ]& [+ q% J  u1 P/ l
  7. >>> math.fsum((1,2,3,4))3 S" a0 U! J5 r6 _7 M- M* F
  8. 10.0( G& G6 l' t! h* F& U$ S
  9. >>> math.fsum((-1,-2,-3,-4))
    / ^" _* Z' p5 }& X
  10. -10.0
    8 v: B( E" F. \' D. l: ]
  11. >>> math.fsum([-1,-2,-3,-4])  \; `: q- Z4 a/ d; N
  12. -10.0
复制代码

$ I. e4 G. @" E. ~* f$ \math.gcd(x,y)  返回x和y的最大公约数* [6 {) b% P9 I; {+ Y$ W
  1. #返回x和y的最大公约数
    % Z9 G1 m2 {" ^9 r, g: |$ U; B
  2. gcd(x, y) -> int8 C$ K: p* ~0 b! Y! ]( ~
  3. greatest common divisor of x and y
    ) P: Y1 X7 {6 v; u! |1 I4 F5 b
  4. >>> math.gcd(8,6)
    6 X- S: u6 p8 f- w6 G) J
  5. 2
    9 b* Q0 u3 E& P; D0 }+ @. {  A2 z
  6. >>> math.gcd(40,20)+ X" |3 D8 B% ~/ O5 o
  7. 201 u2 s; e3 H2 V; j
  8. >>> math.gcd(8,12)
    . N/ h6 s& ?* K
  9. 4
复制代码

$ L6 h1 u5 q! cmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
" w/ y1 z+ H/ m, [# V# ~; z# ^( }1 W
  1. #得到(x**2+y**2),平方的值( c9 R7 d" [1 l  `
  2. hypot(x, y)) F2 Y2 b, v9 P
  3. Return the Euclidean distance, sqrt(x*x + y*y).' P8 F. U- e! P9 s3 s
  4. >>> math.hypot(3,4)0 m/ a& o  u- [9 D- U$ p- V- x; F
  5. 5.0
    2 f- P$ q0 v" E. g' h  Y1 U
  6. >>> math.hypot(6,8)
    + }8 R2 k: {+ m  v; M
  7. 10.0
复制代码
' n) v0 C" M9 s: s) H
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False4 ]5 g9 @) J8 P6 B
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    0 S- k8 A7 h0 P2 J) M* j
  2. isfinite(x) -> bool
    / [: q" P2 R) d( {, I; }
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.- q0 X$ u; a& y' c( b% |
  4. >>> math.isfinite(100)
    2 Q9 g' y& f  I
  5. True
    - o3 A" G! b5 h8 S( |: N
  6. >>> math.isfinite(0)
    ' j! ?$ c" x* J
  7. True
    3 R4 Y; b5 \  y4 z+ Y
  8. >>> math.isfinite(0.1), D8 o8 x7 ^# E  J/ T, W4 R# m/ K
  9. True/ b$ F/ D6 j" i5 J: f
  10. >>> math.isfinite("a")
    8 m0 L/ |4 _# r+ c% j! |: d$ T
  11. >>> math.isfinite(0.0001)
    # N* u( f+ Y2 k$ X4 ?( ^
  12. True
复制代码
$ e) x6 o8 U+ y: }
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
3 ~3 z/ J! ~$ b* }  b! ^& s
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    " U# G0 q& Y- o+ O8 F( \
  2. isinf(x) -> bool. D2 r6 h6 W* w/ W. c! U' E
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ( Y: Y2 C8 ~* V  x! `6 S7 [
  4. >>> math.isinf(234)
    , H' |5 _: C! _) j" K) L6 Z
  5. False9 X8 @3 K3 C" D! W- f/ w1 S3 t
  6. >>> math.isinf(0.1)2 G+ D) Z* a  @  M1 a+ g
  7. False
复制代码
. J/ }; u# _% J7 e% M' L
math.isnan(x)  如果x不是数字True,否则返回False
4 G( d9 e* X# d# ?- m4 Z
  1. #如果x不是数字True,否则返回False1 d" w7 G2 d7 l4 M8 h1 M1 l7 u
  2. isnan(x) -> bool* F# t- J( ~; A! ?0 K) B5 i5 v- s
  3. Return True if x is a NaN (not a number), and False otherwise.
    $ r* i* B+ I, q
  4. >>> math.isnan(23)8 k2 i& _2 H. V
  5. False
    % k0 Q. G: [$ v( h
  6. >>> math.isnan(0.01)
    . v( Y2 i/ w" ]* E$ S# c
  7. False
复制代码
/ P0 i" B1 x  H( n
math.ldexp(x,i)  返回x*(2**i)的值4 J* M# I$ k  n
  1. #返回x*(2**i)的值! L% D" J$ Z0 W. h
  2. ldexp(x, i). @+ C1 j. a. J* L# M2 |
  3. Return x * (2**i).
    ( @3 e7 q1 F" A; B) E
  4. >>> math.ldexp(5,5)2 r8 q2 X6 r" W4 g8 H; h- }5 I7 u
  5. 160.0% D1 d+ Q8 J; e# Z/ u7 c
  6. >>> math.ldexp(3,5)
    $ V9 {+ h& [9 x* h
  7. 96.0
复制代码

4 f- o3 w! h" }, b7 M1 Mmath.log10(x)  返回x的以10为底的对数
+ D8 |% ]1 _& a7 @6 a6 x
  1. #返回x的以10为底的对数9 w6 p! S; P0 ^8 A
  2. log10(x)
    * X4 D# X2 G: N& E' Z8 T
  3. Return the base 10 logarithm of x.
    8 p( Q3 b4 U' P) m& v7 i, Z6 a& |
  4. >>> math.log10(10)/ f6 ^; n4 h9 p, `
  5. 1.0
    $ Z2 K+ n- X& d) V
  6. >>> math.log10(100)8 \3 Y& W) ^% a( v2 A  S( l2 b
  7. 2.0
    7 T9 |3 V+ j7 w, b
  8. #即10的1.3次方的结果为20# P3 f5 g' o5 v, c
  9. >>> math.log10(20)9 V9 l8 |+ o  `2 l/ G6 ?
  10. 1.3010299956639813
复制代码
% \9 ^$ K7 N4 H) L
math.log1p(x)  返回x+1的自然对数(基数为e)的值
" E2 D) r$ g: a2 H- x! d0 ]6 w3 c
  1. #返回x+1的自然对数(基数为e)的值7 X* L  [1 x1 I( U  h6 N
  2. log1p(x)( X3 `+ u; _8 c# d' P0 u
  3. Return the natural logarithm of 1+x (base e).# x/ j$ ]& F0 j' ]! k; }" z
  4. The result is computed in a way which is accurate for x near zero.0 F# O6 u, V1 j" x. Z
  5. >>> math.log(10)
    6 C$ o  b: J% d( H
  6. 2.302585092994046
    * y6 t  l- i, U4 c" B3 y! G
  7. >>> math.log1p(10)8 Z. |! Z  G# p' {/ c2 v5 j: B
  8. 2.39789527279837077 m1 {) p/ x, F
  9. >>> math.log(11)
    # W6 J7 m4 E$ {5 @# G+ f& P
  10. 2.3978952727983707
复制代码

1 D* ?' c( Q$ n; |* |" }4 Imath.log2(x)  返回x的基2对数
! R- ]% K! l- R$ Q- W
  1. #返回x的基2对数
    % i  r$ U( c, K/ e% _6 ?6 _
  2. log2(x)) v# s! X2 m6 M9 r9 [
  3. Return the base 2 logarithm of x.
    / [) {# h+ S9 k2 E/ @
  4. >>> math.log2(32)
    5 }4 V, @( I( b9 \: b5 J
  5. 5.0/ Y& g) D# r9 ?7 x0 |; U# x+ s5 R) q
  6. >>> math.log2(20)& z9 m" f& J/ E/ _# U* R
  7. 4.321928094887363
    1 Y* J& K0 E/ E' M9 |" E
  8. >>> math.log2(16)
    8 j5 N7 _9 t- x- P& W" q
  9. 4.0
复制代码
" y6 H6 [, b7 P# z1 C+ l
math.modf(x)  返回由x的小数部分和整数部分组成的元组
5 M  E" A7 s8 s& i9 ?  Q3 l
  1. #返回由x的小数部分和整数部分组成的元组- H7 j4 B# h8 D$ b4 Z: B- H
  2. modf(x)! f5 j- \$ [/ U" U7 s5 v" a1 m
  3. Return the fractional and integer parts of x.  Both results carry the sign0 D% s8 y" @2 ?2 i$ N+ S
  4. of x and are floats.
    , C- t3 a; N1 i% E1 ~4 w) ~' |
  5. >>> math.modf(math.pi)4 j, ~9 I& t9 k
  6. (0.14159265358979312, 3.0)
    . Z& |, u& [: I! g9 @6 a
  7. >>> math.modf(12.34)
    ; p) B( u0 U1 P, E+ Y# }
  8. (0.33999999999999986, 12.0)
复制代码

3 j5 d; W! {1 H" ?math.sqrt(x)  求x的平方根3 {( I( o8 y: a1 W. i) ~
  1. #求x的平方根8 V! c5 [- v& p  C" B  I4 g
  2. sqrt(x)
    # q* G$ l4 H1 V- H3 X$ x
  3. Return the square root of x.
    ' w6 P7 V% _& `9 x* H. S7 \  e
  4. >>> math.sqrt(100)% q7 N( w; p1 H6 ^
  5. 10.0" y" Z/ U! D0 N, @
  6. >>> math.sqrt(16)* q! z/ ]* @" y: m3 N. H
  7. 4.09 b: ^% M* H: K# N
  8. >>> math.sqrt(20)' P7 l6 c& c; s8 z! D# E) J* r
  9. 4.47213595499958
复制代码
' J/ u9 W. K; ^" Y, S
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分) V1 E+ }0 s7 d' {* H' T
  2. trunc(x:Real) -> Integral
    . m' c( B2 c4 j8 z- `& H3 Z
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    % x7 D1 a. t  E% U% X5 W2 r; R
  4. >>> math.trunc(6.789)3 f6 h" J4 a5 W# P: m
  5. 69 \! f" a$ ~5 B6 S" \4 N  E: N1 M
  6. >>> math.trunc(math.pi)
    . X5 ^; e- k( U4 q5 _2 x
  7. 3% }1 i) L* [7 }% l/ W5 T' A, Y: ^
  8. >>> math.trunc(2.567)3 g" N' ?% k" n  H! p% W0 q" F
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-28 08:11 , Processed in 0.080139 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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