新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
% b( ~3 N& l# U$ F( W2 P" J4 F
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
1 u/ p4 t& n& \$ c
, H4 h) P; T. o) G
方法10 n; H, y$ o6 }
  1. >>> import math! X9 C4 Y+ J( V+ w
  2. >>> math.sqrt(9)
    . Q7 Q8 E/ u7 o# O1 U4 R
  3. 3.0
复制代码
方法2* Z7 v1 ~, }5 j1 X0 w1 M7 Z
  1. >>> from math import sqrt
    8 @8 y, w, P& T
  2. >>> sqrt(9)- f" W9 P* ]! e* A0 a: S
  3. 3.0
复制代码

. s( w, M1 Y1 r5 ^
& Z  i9 ?0 D2 a7 r/ r; n5 Q, {! m
math.e  表示一个常量3 Y5 ~* F9 w, p# Q8 _7 O0 c
  1. #表示一个常量$ d5 v0 C  q9 }0 r# y  ?% @- ]% C0 d
  2. >>> math.e7 _9 t( c6 S9 f9 v
  3. 2.718281828459045
复制代码

9 f1 s* l$ K& |6 E0 Smath.pi  
数字常量,圆周率
: P( ?4 f3 t3 s4 s9 K# K7 d
  1. #数字常量,圆周率
    6 I: ~. k  n. H& X- ^: P' l! A
  2. >>> print(math.pi)
    6 |/ @1 k' f. O
  3. 3.141592653589793
复制代码

' b/ C) X+ V- ?# o2 Amath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 t/ v, j1 Y% ^/ e: f
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    , O7 w/ B% N. W, u: u- p
  2. ceil(x)
    . c( e2 P$ |" S8 h4 m
  3. Return the ceiling of x as an int.
    + t9 Z1 N( A/ }! p- P' a
  4. This is the smallest integral value >= x." G* U' p, e4 H" S1 L

  5. / k: [8 ~; _) s( S/ j
  6. >>> math.ceil(4.01)
    9 i0 K! ^$ E3 l
  7. 5
    ) j5 b' l6 ]5 q  r- x2 [
  8. >>> math.ceil(4.99): h: ]( I3 E% t& o9 _! v
  9. 5
    ) o* F( O* |+ k, A- I, p! {
  10. >>> math.ceil(-3.99)# `/ W+ s: q+ M. J, D+ Q
  11. -3, V8 M( e, R/ c- D1 g4 `* D1 Z2 d
  12. >>> math.ceil(-3.01)( o7 O. U* d' q; O$ s0 B
  13. -3
复制代码

+ n  X& d7 T% o' h9 a# N7 z9 o" ~math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
  e& G) K' Q$ k7 z% L4 q
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身: a( f; @% [# |, q$ r5 i
  2. floor(x)
    4 @, x! {: D. `9 Q! V
  3. Return the floor of x as an int., t: I6 x& x7 Y1 C; J8 }- D5 s
  4. This is the largest integral value <= x.
    $ a  [3 h- O' ~& [2 o+ T
  5. >>> math.floor(4.1)
    / b6 `6 G4 O+ l6 i
  6. 4& k; _1 K# v" L. G' Y& m
  7. >>> math.floor(4.999)
    1 t$ ?! r* x6 b$ e- l
  8. 45 q% p$ ~# Q. F: T5 o
  9. >>> math.floor(-4.999)* e6 N' b. b0 Z. c, @0 S
  10. -58 D* a% r  [: J% `. A( t
  11. >>> math.floor(-4.01)! N8 E: _% k" k5 I# ^1 g# A& p& C
  12. -5
复制代码

$ [9 ]3 ^* _! v, ^6 }math.pow(x,y)  返回x的y次方,即x**y, E3 t; N# b6 K+ h6 v
  1. #返回x的y次方,即x**y3 p  |/ Q0 W$ \& S8 A4 _
  2. pow(x, y)
    8 [# x) t" ?/ A% V* q, o! Y
  3. Return x**y (x to the power of y).
    ) N/ L  u8 d, `6 t8 d9 F2 l
  4. >>> math.pow(3,4)
    9 n: X2 [1 S. s# F: z" z, w
  5. 81.0
    9 M: b2 f% n0 v; d  f% l7 U
  6. >>>
    6 ?9 ?% D6 Y2 S/ s5 I" m/ U. }
  7. >>> math.pow(2,7)
    - Y6 D: T' Q9 a, U+ ~. B
  8. 128.0
复制代码

5 r7 G' {! R3 k0 o3 f9 ?  I# E0 vmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
; o1 V; C+ U, r; |/ X8 ]$ a
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)4 z. d$ z9 l) w" o
  2. log(x[, base])
    & s* a- g- O4 Z4 e
  3. Return the logarithm of x to the given base.
    1 S6 J# E6 j$ S
  4. If the base not specified, returns the natural logarithm (base e) of x." ]* c: T. I- k# g
  5. >>> math.log(10); |. y; F, N! X' X
  6. 2.302585092994046+ T5 q' @* U# @# b- w8 y
  7. >>> math.log(11)
    # O8 L7 R1 _4 o; W7 x: a
  8. 2.3978952727983707; O6 h8 n& c8 @7 `- h
  9. >>> math.log(20)% y- P+ X+ {- d8 m, E
  10. 2.995732273553991
复制代码

3 R/ u; ]* B. Y& Tmath.sin(x)  求x(x为弧度)的正弦值/ ^2 n% v* Q- N6 d1 t/ v
  1. #求x(x为弧度)的正弦值8 @" R2 |/ y' \. q
  2. sin(x)
    : @( r. ^9 P  p/ Z' w
  3. Return the sine of x (measured in radians).6 R' ^% q! _1 T: B
  4. >>> math.sin(math.pi/4)
    + @) g8 V: a: ~! k! u; P, j
  5. 0.7071067811865475# p; G- g0 K8 y, T* l
  6. >>> math.sin(math.pi/2)" M+ A) r* g# q, Y
  7. 1.0
    ) c; I% F5 f1 F1 @
  8. >>> math.sin(math.pi/3)9 M$ I- `6 z2 w* d/ _& E! M  T
  9. 0.8660254037844386
复制代码

! A9 |# g: X2 Emath.cos(x)  求x的余弦,x必须是弧度2 T; Q( U* n3 d4 o1 |1 g
  1. #求x的余弦,x必须是弧度
    # z# Q$ ~; y7 ^5 R  R7 I
  2. cos(x)
    4 M3 r# h# p! c9 V9 x9 s) L" t
  3. Return the cosine of x (measured in radians).
    4 q2 P; v! h- N- P: e# }
  4. #math.pi/4表示弧度,转换成角度为45度
    - V3 J) B$ v4 D: f1 ^
  5. >>> math.cos(math.pi/4)
    4 V4 H2 W( S$ o% M0 p6 M8 d$ E" ^
  6. 0.7071067811865476
    6 b$ g$ v6 N. n0 C* {" h# S
  7. math.pi/3表示弧度,转换成角度为60度& X1 x% E. ~7 w
  8. >>> math.cos(math.pi/3)
    . X, S2 L0 x4 \
  9. 0.5000000000000001# ]+ x' G7 H( R) S
  10. math.pi/6表示弧度,转换成角度为30度9 L  w' ]8 k% [! w) v* I. B2 P
  11. >>> math.cos(math.pi/6)
      S0 v. N& ~' J0 ]6 l# R, t5 ]
  12. 0.8660254037844387
复制代码
. e% G) ^6 h3 j: y
math.tan(x)  返回x(x为弧度)的正切值
. R; {  K+ L/ F
  1. #返回x(x为弧度)的正切值  B- V: V# h2 H0 E, [% M
  2. tan(x)
    ! j/ l" a. m; V3 F/ e# Y
  3. Return the tangent of x (measured in radians).
    ' r) j- P7 X, _, F+ d- f& r
  4. >>> math.tan(math.pi/4)2 q6 r, G* S- b$ w: T) L
  5. 0.9999999999999999, J# k: a: g, a3 ^
  6. >>> math.tan(math.pi/6)
    & M( n) B1 \# K$ |4 Z1 P3 i' k
  7. 0.5773502691896257
    , H$ A( Q3 v& a# L3 e+ k$ f9 g
  8. >>> math.tan(math.pi/3)
    3 ~# U! H' g& M3 j4 ?
  9. 1.7320508075688767
复制代码
1 f, p$ z, z4 o
math.degrees(x)  把x从弧度转换成角度
4 a+ v2 A3 E2 K( j0 \
  1. #把x从弧度转换成角度3 X3 {- L# X5 e
  2. degrees(x)
      Y- y$ t2 B, w. f! H  P
  3. Convert angle x from radians to degrees.
    5 H. j% j: C5 ^5 ^' ^9 J; B
  4. " M; J& ~9 n7 W( R
  5. >>> math.degrees(math.pi/4)
    $ F" O9 z! v2 j' W" M
  6. 45.0
    * A1 r6 F+ e' ~8 Q7 s) w) t' i
  7. >>> math.degrees(math.pi)# c) P/ v- D4 m- Q/ ]' P
  8. 180.09 n9 G7 ~0 r- K0 f: j# ~
  9. >>> math.degrees(math.pi/6)7 w& {( S- u7 a- g; ^8 {
  10. 29.999999999999996  H8 q' |$ p1 s/ G
  11. >>> math.degrees(math.pi/3)5 X9 i: H( S1 j* h- Q7 F8 o
  12. 59.99999999999999
复制代码
+ K' v9 c5 P+ h
math.radians(x)  把角度x转换成弧度0 K/ m" b" Z# i- ?
  1. #把角度x转换成弧度
    " b0 x& x" U  ~
  2. radians(x)9 B% p  n5 f/ s& I/ v$ N, h. W
  3. Convert angle x from degrees to radians." c) b" d# n4 r' v1 L$ d
  4. >>> math.radians(45)
    " d- h' |; I1 d- H9 `1 U6 e2 H& E
  5. 0.7853981633974483' P. ?9 W* }! W. k5 B" u
  6. >>> math.radians(60)8 m+ x1 {; x& c' ^: n( I7 z
  7. 1.0471975511965976
复制代码

+ u# N2 a+ ?, j& ^! [! Qmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
6 d# R+ R9 u9 X
  1. #把y的正负号加到x前面,可以使用0
    $ f- k# h: m* x; a' Q
  2. copysign(x, y)3 P( X8 U1 @+ p: f+ Y) L
  3. Return a float with the magnitude (absolute value) of x but the sign % g. H8 c4 u. ^& |% ?0 l3 e
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 9 p- ^3 Q$ M$ r. I
  5. returns -1.0.
    3 G# S1 Z8 w& }) V/ N

  6. # ]+ f3 |  O. M$ U+ o/ U& F# i) m
  7. >>> math.copysign(2,3)
    6 K  k( A' k* w/ s
  8. 2.0# b- a; y& `# n% c
  9. >>> math.copysign(2,-3). q7 M' M4 \& I1 V, b' \. c5 F) [: m1 J, u
  10. -2.0
    " S/ Q4 q9 A6 Q9 Z% L% U
  11. >>> math.copysign(3,8)
    7 S1 l& X# `9 T
  12. 3.04 V/ V( A7 ~+ v3 e" A) l+ _. i/ }
  13. >>> math.copysign(3,-8): D  x- I# ]+ J1 D) `
  14. -3.0
复制代码

, F! Y* C+ ]- \% c# l7 r0 V, Kmath.exp(x)  返回math.e,也就是2.71828的x次方
; I$ {' }, ]2 o$ y
  1. #返回math.e,也就是2.71828的x次方
    4 A, {" ?4 s9 n6 M, }; W$ j: m" L
  2. exp(x): t! W4 Z6 t7 M$ S5 A$ D4 ^
  3. Return e raised to the power of x.* |+ A0 T: z' D* q2 r% V

  4. 6 ?' L! q3 l! m7 ~3 ]7 t4 }
  5. >>> math.exp(1)/ }% I0 E6 k% r* d$ q: f9 s/ h
  6. 2.718281828459045( r5 W0 c, m! w6 w( p) c) Q
  7. >>> math.exp(2)5 [! W. k5 m& U
  8. 7.389056098930650 o3 G4 |8 V% ~& P
  9. >>> math.exp(3)5 L1 \8 n* {( \+ m- v9 U7 g. \8 S+ a- C
  10. 20.085536923187668
复制代码

1 \& d7 W* a) Q/ Wmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1) U$ b7 u2 [2 O" ?( l( {
  1. #返回math.e的x(其值为2.71828)次方的值减1
    " }( }3 M! a/ R- t( X
  2. expm1(x)
    # s/ U4 I8 i5 a4 u6 Z% x5 I
  3. Return exp(x)-1.! H$ m$ J0 p" k$ a: Z9 Y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x., `! y5 h( j6 S" q7 ^( ^; G
  5. 8 l7 a" e9 _$ x
  6. >>> math.expm1(1)
    3 T) i0 e7 t1 U
  7. 1.7182818284590451 o; U# G1 S" `3 l
  8. >>> math.expm1(2)
    9 C- K( c2 M- }0 g, H$ x
  9. 6.38905609893065+ T* z8 V9 e& J, ?5 |
  10. >>> math.expm1(3)2 O9 d. G. U) K# L+ b& j
  11. 19.085536923187668
复制代码
6 g5 Q6 V" \8 V) N
math.fabs(x)  返回x的绝对值
9 M' w' E5 A$ f" n' ^  K) g
  1. #返回x的绝对值
    # h- h- E- U4 t. A
  2. fabs(x)
    ; \! d" Y9 |! R" z1 W6 Y
  3. Return the absolute value of the float x.
    ) |4 t) v& C) t2 a) T+ C/ n/ `. x1 n

  4.   ^( B. x/ A" B
  5. >>> math.fabs(-0.003)* v. f) O6 a% K7 y
  6. 0.003( ?2 b* }1 O- t
  7. >>> math.fabs(-110)
    0 z4 k& D5 Y+ n; c+ ~: h  w
  8. 110.04 `1 q' r. o+ f' r6 f
  9. >>> math.fabs(100)  w, K# `/ L  J
  10. 100.0
复制代码

6 P$ j* O2 U0 u1 n. ]) j. [( jmath.factorial(x)  取x的阶乘的值
( A5 e# I" c3 d, ?; E
  1. #取x的阶乘的值
    , V" _7 }$ D7 [% n5 b$ l4 V/ R5 v9 ?
  2. factorial(x) -> Integral
    6 u. |7 u' k& y# |) M. O# ]
  3. Find x!. Raise a ValueError if x is negative or non-integral.$ ~. m" [" m( q/ w3 L4 Q
  4. >>> math.factorial(1)% L2 _' |! V$ K5 |
  5. 1
    1 x2 Y! \! }2 B9 z. D, f0 _
  6. >>> math.factorial(2)
    8 ]% w, o, D. B; S% ?1 f
  7. 2, c" g; u; ]2 u0 Z: m" t1 a2 k# E
  8. >>> math.factorial(3)
      T) o3 T2 I% }  |" L" G
  9. 6' \% r6 {- h# E& P" k; v
  10. >>> math.factorial(5)
    6 k1 Q. I- `8 u0 ^6 q- ?. q8 V
  11. 120/ X8 n" @3 K9 E8 Z, x6 @
  12. >>> math.factorial(10): j0 P* L1 s) T
  13. 3628800
复制代码

( l6 _2 h5 w& F+ ?, l- cmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
* H: v+ D; G( l# ~- c
  1. #得到x/y的余数,其值是一个浮点数
    9 \3 e) d' r  h; C
  2. fmod(x, y)
    - F. }# v( H. i' R; f
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ; I2 m. ?% a; g0 i
  4. >>> math.fmod(20,3)* Z+ `, C9 P3 Z7 d/ F; I( W
  5. 2.0
    % A) B6 b5 Q! Y8 Q. B
  6. >>> math.fmod(20,7)
    2 X" G' x6 s" [0 _
  7. 6.0
复制代码

) w/ ~$ m1 H( q9 u1 zmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围) U% N, `: [5 f* m* j
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( n# d2 \# V- w- ~& j
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 d4 E8 \3 {$ u3 H: m: i
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    0 N! }% d. a5 i& r2 T1 K- d
  4. frexp(x)7 N0 G4 B% B. `' Q4 B: o% \# v
  5. Return the mantissa and exponent of x, as pair (m, e).
    1 W! ^# q0 R$ V& r: ~" ~' \1 C
  6. m is a float and e is an int, such that x = m * 2.**e.
    + s9 p5 l; F/ ~) M6 u
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 S) Q3 a6 {8 E3 _: F
  8. >>> math.frexp(10)
    6 {, Z, I: i; g( s* p
  9. (0.625, 4)1 X' E, k: m( D/ o
  10. >>> math.frexp(75)
    ) D" G$ T9 X+ v- b7 `& k3 w
  11. (0.5859375, 7)
    ' C- A! M4 _- b) C) V' c
  12. >>> math.frexp(-40)0 S  p0 d1 U0 l$ A, q. X( l" t
  13. (-0.625, 6)
    ) H: `: z/ ~! t: K% X6 T: F
  14. >>> math.frexp(-100)# L5 B* T. [: _& W
  15. (-0.78125, 7)$ Y, k& @3 c0 @0 ^6 x4 Q# ^# W: s
  16. >>> math.frexp(100)" e" s& e% @4 v* F( H+ t
  17. (0.78125, 7)
复制代码

& t( y# f/ T0 b1 g* Bmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
, }" H3 ?: a0 F$ J( h
  1. #对迭代器里的每个元素进行求和操作
    & w# M' `, F$ h  S/ [9 R% @# n2 ?
  2. fsum(iterable)+ B9 ^5 R" S, ?. ^
  3. Return an accurate floating point sum of values in the iterable.
    # Z/ A) O1 a3 ^- S
  4. Assumes IEEE-754 floating point arithmetic.
    / k' \0 k3 N8 I, a6 ]
  5. >>> math.fsum([1,2,3,4])
    " I* T  G: Y+ j0 ]
  6. 10.0
    . I8 h6 m4 Y- j) x4 W
  7. >>> math.fsum((1,2,3,4))) O0 d0 Q8 i8 C& A0 G$ c& P+ |
  8. 10.08 ~# _; L: \+ m) ]* }6 X  A
  9. >>> math.fsum((-1,-2,-3,-4))& i6 r) T" J. R6 B2 o
  10. -10.0
    4 D% ^, a$ {- l* _. `9 @9 O
  11. >>> math.fsum([-1,-2,-3,-4]): Y, `5 J! _5 b' [
  12. -10.0
复制代码
6 p( c3 S# v# Y: n! T
math.gcd(x,y)  返回x和y的最大公约数
* T% q8 k) |3 ]. N
  1. #返回x和y的最大公约数
    * n# J/ L6 k  P) d
  2. gcd(x, y) -> int2 e) k) I- ^5 U; _. B8 y7 S9 N, }
  3. greatest common divisor of x and y
    3 T$ x( _8 O" z) `4 k7 h( B# ]3 W) {
  4. >>> math.gcd(8,6)$ W, m& Z7 s! i
  5. 2
    7 c+ q# y6 b& D
  6. >>> math.gcd(40,20)! }$ _1 y% }* Y3 b
  7. 20# B% ?8 ~5 a3 s- Z5 y& P% \
  8. >>> math.gcd(8,12)! X# o9 |2 H9 m' n
  9. 4
复制代码
7 P2 _4 |: L4 N, v4 H- X+ p
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False, f6 m. W! d# h! X$ m
  1. #得到(x**2+y**2),平方的值  [5 t8 M0 M$ ?2 C0 P0 ], M7 M
  2. hypot(x, y)
    7 k+ f7 {  W4 [: Z/ Z1 e+ e
  3. Return the Euclidean distance, sqrt(x*x + y*y)./ P! y8 f8 S  q& {+ j+ ?: b
  4. >>> math.hypot(3,4)0 P6 H! q6 k0 f( H1 y+ {) \
  5. 5.0
    # o3 R$ s/ L; q/ |) R
  6. >>> math.hypot(6,8)5 j' p6 K& h4 H# w' j
  7. 10.0
复制代码
1 {( J" j) G. V# w' o% j8 p1 _
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
1 h, q- W$ a/ Q! [$ a
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    2 |( L8 L1 Q6 \" v  \. Y
  2. isfinite(x) -> bool1 W& ]# P1 t5 V/ a  V
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.+ e# o" e3 ?% _/ L( t
  4. >>> math.isfinite(100)
    + }* C% b2 x) X* J: L+ j9 ]. c; q
  5. True
    & F5 C4 \- C4 ?
  6. >>> math.isfinite(0)
    : _5 k6 Q  ]% o
  7. True
    ' C0 i/ z% e$ n& ?0 l
  8. >>> math.isfinite(0.1)
      R  {' b/ h7 \
  9. True
    # C5 s3 [8 f- R9 ^' w1 t2 [
  10. >>> math.isfinite("a"), P4 x- s' d" i2 `% g4 D) W8 Z
  11. >>> math.isfinite(0.0001)
    7 K; q' M7 r% r* z! c& j, v) `
  12. True
复制代码
3 r/ z9 @+ S# l: G0 o0 O% |% h5 u, y
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
* ^2 z3 H, j. q' ?
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    5 W3 m4 [. @1 E0 `: w" \" r; f! E
  2. isinf(x) -> bool! b9 D" g' e: E+ N
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ! X$ D9 [* ?( n* ~  x/ V
  4. >>> math.isinf(234)1 u, O- f4 g& Z6 e6 j) Y
  5. False
    7 s8 L6 [  \4 ~! {
  6. >>> math.isinf(0.1)2 y3 S* ^! i$ u
  7. False
复制代码
# Z5 r/ P0 D* N& o/ {0 i- `( O# n7 i% B$ ^
math.isnan(x)  如果x不是数字True,否则返回False# [1 [7 F- |7 O! Y
  1. #如果x不是数字True,否则返回False
    ; ]9 G3 B( }& H: @
  2. isnan(x) -> bool
    ( r. @8 W# M( A) H( L$ Q+ F
  3. Return True if x is a NaN (not a number), and False otherwise.! \# @4 `2 d: @" z' D5 U
  4. >>> math.isnan(23)7 ?0 w( U3 o; q/ d6 [# m
  5. False: X# Y& v/ }+ i2 i8 m' K5 D& A/ U0 d
  6. >>> math.isnan(0.01)1 g6 C6 v5 W. W0 G& z' N
  7. False
复制代码
, h7 ~3 X  V- [% `! K
math.ldexp(x,i)  返回x*(2**i)的值
) `' H! u  ]& _
  1. #返回x*(2**i)的值2 M6 l5 W, K# \$ {5 j
  2. ldexp(x, i)
    6 C" Y. |2 h( W2 V5 h, M
  3. Return x * (2**i).$ z, [; R' W. y. W
  4. >>> math.ldexp(5,5)
    ) c2 E1 n0 h0 B" ~8 O' J
  5. 160.0
    / i# x+ X' r( r& ~2 _8 z
  6. >>> math.ldexp(3,5)
    ' A  k/ ?' n3 i0 _( I! N5 I
  7. 96.0
复制代码

* r. z2 g  w( N, f, V1 Smath.log10(x)  返回x的以10为底的对数* F9 A2 v2 o7 ]$ f1 i
  1. #返回x的以10为底的对数
    7 T& ^3 g' W2 Y& \9 A  w2 i1 a
  2. log10(x)& i  J! A) j. w, R/ b! s& a$ y
  3. Return the base 10 logarithm of x.
    ' u. u/ Z, J1 T! [0 d; R1 D
  4. >>> math.log10(10)
    " D8 d2 o& ?$ |9 q0 L: V! j3 Z
  5. 1.0  l8 v, U; l: G' O9 Z. R2 g. ~
  6. >>> math.log10(100)* x, m" H+ ~  |$ g! v! |" u
  7. 2.05 ]4 D9 U' ~1 h& B# V9 G8 U
  8. #即10的1.3次方的结果为203 f: q0 U7 j" J5 }+ j! u
  9. >>> math.log10(20)
    9 i  d# F  n0 K0 a
  10. 1.3010299956639813
复制代码

& T- d& W$ J" l' ]math.log1p(x)  返回x+1的自然对数(基数为e)的值" i( @+ n# T# o+ g+ B
  1. #返回x+1的自然对数(基数为e)的值& e, b/ m/ O) y" F( P
  2. log1p(x)
    1 t- a! N9 k% z4 X' }
  3. Return the natural logarithm of 1+x (base e).
    . u+ o9 @2 U: m+ f# M
  4. The result is computed in a way which is accurate for x near zero.
    , G- n3 ~7 u# f
  5. >>> math.log(10)2 u% U0 R' X1 C9 \3 [
  6. 2.302585092994046. @- P8 u% _. G$ u  p$ o0 l
  7. >>> math.log1p(10)# K2 e( P) T) ~' z0 }
  8. 2.39789527279837073 [  n/ [8 x1 f
  9. >>> math.log(11)
    0 I/ [( z) P" q) B4 }
  10. 2.3978952727983707
复制代码
7 Y/ E# i* G2 t8 p' q- v* L* q& ^
math.log2(x)  返回x的基2对数
+ `7 p# A0 b" `* T1 ^4 b) Z, j
  1. #返回x的基2对数  c0 W+ ]# @7 s8 v8 Q3 ~
  2. log2(x)
    4 V% l& h9 H7 @# k1 K  v, z: ~' z
  3. Return the base 2 logarithm of x.
    # Y* K' ?3 {4 ?1 p% U- e
  4. >>> math.log2(32)
    & a, ~+ q" d/ r& X
  5. 5.0
    " M* T7 g2 f0 f& L
  6. >>> math.log2(20)0 R4 H: r- F( S% \3 r5 z) S
  7. 4.321928094887363
    ' H/ y* U: V7 y9 A
  8. >>> math.log2(16)6 \4 W$ D1 `: @) i, J" v7 n* Y
  9. 4.0
复制代码
8 {; X6 D+ R: P4 Q5 K
math.modf(x)  返回由x的小数部分和整数部分组成的元组
* y7 p; q6 L, n2 [# F
  1. #返回由x的小数部分和整数部分组成的元组
    ( H" z, u0 H3 M
  2. modf(x)% \& J, |( }; G4 k& O8 D
  3. Return the fractional and integer parts of x.  Both results carry the sign8 ?, U+ `  `& S% u/ C
  4. of x and are floats.+ @$ v+ B. _& Z. [+ }: d, F
  5. >>> math.modf(math.pi). n! b& `( _, Z8 Z& p% @
  6. (0.14159265358979312, 3.0); e" p# l/ X, \; G  T. d5 g) Z) v# D
  7. >>> math.modf(12.34): a8 O4 B6 |7 ~  I' F' \
  8. (0.33999999999999986, 12.0)
复制代码
& p3 _8 X( K$ K- [6 f2 x, `
math.sqrt(x)  求x的平方根1 b' v3 V6 w6 A- L
  1. #求x的平方根
    . E0 w- e6 y  a: l6 {; r8 [
  2. sqrt(x)
    . }1 B0 J3 y  s8 ^% a; O/ j* e
  3. Return the square root of x.+ p0 D( V$ @! t  M
  4. >>> math.sqrt(100): Q0 B6 n5 u* }  |% K
  5. 10.03 D# T% h  Z9 p  F( O) z
  6. >>> math.sqrt(16)6 W% A+ B2 T* A* ]+ Q
  7. 4.0: k* B( Z$ x* B2 R( U
  8. >>> math.sqrt(20)! U, B! m- ^1 B5 J
  9. 4.47213595499958
复制代码
/ P+ L5 H8 i$ K3 c6 j& l- e4 e1 L2 n9 D
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分" G- K! g% v9 g$ P/ L
  2. trunc(x:Real) -> Integral
    ' v5 D' j/ B' V& w
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 n3 s: d8 U' J, x% C2 |
  4. >>> math.trunc(6.789)5 d/ c$ t# z! Y5 b9 G) U
  5. 6
      e/ x; R  C: W, J8 s
  6. >>> math.trunc(math.pi)
    + b  L* |- Y% O6 K  U+ U
  7. 3
    # R, U( `3 F. x, B
  8. >>> math.trunc(2.567)
    6 m) l5 u1 e& X" t4 g( [
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-26 16:16 , Processed in 0.212529 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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