新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
+ Q5 n$ X0 y5 Z# {1 n4 V  R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
6 [1 _. l5 _/ X; G, P
) v3 g, F  o$ T; _
方法1
1 a8 \' T6 B/ i4 |, I
  1. >>> import math2 F; T, M' D7 W5 j& b
  2. >>> math.sqrt(9)  h) l- |/ ^) l8 x
  3. 3.0
复制代码
方法2' p% O3 U3 r5 k3 O" f0 f! B* C7 z
  1. >>> from math import sqrt' q: y+ b. H( Q! E' y+ @
  2. >>> sqrt(9)3 {% m! n: X; z3 d
  3. 3.0
复制代码

  u; p, z% M: Q3 Y  W2 e

; x3 n; p6 Y) Y( I7 G  m
math.e  表示一个常量2 v3 y. i1 e7 |/ F. V
  1. #表示一个常量$ [; a. G. F2 e/ R9 q
  2. >>> math.e9 `( V0 V$ I$ h( ^7 |5 T1 |
  3. 2.718281828459045
复制代码

8 \6 c! n* v: d% E# Jmath.pi  
数字常量,圆周率
$ F$ S9 @; Q0 P' b4 V; h+ w
  1. #数字常量,圆周率, I, s# C- v, n. L( h. }, P6 O
  2. >>> print(math.pi)
      J, d" j8 P7 @, ~. u
  3. 3.141592653589793
复制代码
/ J3 e6 F2 E3 s# Z
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
$ G; U7 ?+ V; ?
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x$ C4 W4 O7 P! V" \4 K& ~# |
  2. ceil(x)7 T( u' O: e0 Z% H4 U; b8 F
  3. Return the ceiling of x as an int.+ W" b' }) ~9 V# n5 H/ {
  4. This is the smallest integral value >= x.8 H7 T: P) e" w# p) F5 x
  5. 5 ^8 p6 z* _0 K; H& b
  6. >>> math.ceil(4.01)/ z4 ?! w, O3 _
  7. 52 w$ G6 h* }. J/ h1 K" L' S! F
  8. >>> math.ceil(4.99)* q8 f8 K$ u6 t3 D
  9. 53 J; ]( ~$ n) h" `  Z" P! X) x
  10. >>> math.ceil(-3.99)- [/ w0 z+ i* {
  11. -3, L& o7 D2 b1 u/ \: \  q
  12. >>> math.ceil(-3.01)6 L: n' c& R, ]! Q+ E8 ~
  13. -3
复制代码

3 c8 p& V9 A5 D) R" r1 [6 Lmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 T0 _: Z* y. L4 u
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# ]  V) b5 S0 [! Y8 x. h3 S
  2. floor(x)
    $ A* J+ {! p3 b' |  G
  3. Return the floor of x as an int.
    1 r6 m2 z$ y" v% S) m
  4. This is the largest integral value <= x.( [0 V% B- S* L8 ]. ?
  5. >>> math.floor(4.1)9 P+ e% c$ H6 J; }  ~; \
  6. 4  \# t7 ?( K0 ^8 V5 C& Y
  7. >>> math.floor(4.999)
      a+ {1 h$ @7 Q. R: w2 t; s6 L
  8. 4$ u  [# P% K- ~' e# n; X6 _2 q
  9. >>> math.floor(-4.999)
    4 L9 t* P: B; f/ J8 {: k8 }( [
  10. -5' b  P' a, f, C# T) l
  11. >>> math.floor(-4.01)6 |# F- }( Y  w9 X4 h# h3 R
  12. -5
复制代码

2 X3 r  k+ f3 f; H7 n6 Bmath.pow(x,y)  返回x的y次方,即x**y
( T& t7 ?' w" X# N$ b8 z! m; i
  1. #返回x的y次方,即x**y3 W( w4 m6 B( l2 [  i* @3 _
  2. pow(x, y)  w% Q) R: U. v
  3. Return x**y (x to the power of y)., O9 O( ]6 `7 {3 \& q5 ]
  4. >>> math.pow(3,4)
    - p5 f6 {) H' p: ~# k
  5. 81.0
    ( t' I* S1 T, W" w: C) T9 R7 Y/ {6 f
  6. >>> . l7 D* W2 d9 M: N
  7. >>> math.pow(2,7)3 w( r4 |) i! a, e- C+ d4 M" X8 h
  8. 128.0
复制代码
; p! t- s' d1 }# L$ L+ f8 s
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 P% x5 E! F: W: [% z" J  p
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base). L9 J* a$ J: u( s
  2. log(x[, base])
    6 I( S( W2 z2 O% J
  3. Return the logarithm of x to the given base.' L2 m; q+ c7 i: P" i; ?  y% Q9 `, \
  4. If the base not specified, returns the natural logarithm (base e) of x.( n1 y& W; R6 U: S6 p" W
  5. >>> math.log(10)) Q3 R) J( m) F1 _9 P: n
  6. 2.3025850929940461 o9 Q% W* T& k6 L: G9 @! m
  7. >>> math.log(11)
    ' {" W( r4 i6 s' c  Y1 O! q
  8. 2.3978952727983707
    . K: d7 n# s/ F
  9. >>> math.log(20)
    3 z( T4 f; A% Q
  10. 2.995732273553991
复制代码
# w; w; D) B9 J% P6 g: ?8 V
math.sin(x)  求x(x为弧度)的正弦值! ^  D; C+ J9 T
  1. #求x(x为弧度)的正弦值/ |) W) e; |" ]( g4 U- h
  2. sin(x)
    ! B5 i% Q7 z2 z# ?0 s
  3. Return the sine of x (measured in radians).+ A7 g$ s/ o1 a3 Q2 C
  4. >>> math.sin(math.pi/4)% D0 e; }, P* a# G
  5. 0.7071067811865475
    2 N- ^7 ]8 G( U; f; c9 {& u
  6. >>> math.sin(math.pi/2)
    , _0 {# Q$ Q4 S4 P; `+ h+ v$ f
  7. 1.0
    # k; x5 G+ v. a! @
  8. >>> math.sin(math.pi/3)
    # T& n" }& N1 A# g3 J1 u4 v& i
  9. 0.8660254037844386
复制代码
) l" X5 M8 l' V
math.cos(x)  求x的余弦,x必须是弧度
# x- v' n! }+ e, [  O
  1. #求x的余弦,x必须是弧度2 `; v+ u1 W% _
  2. cos(x)& a: j6 w. K3 |& u1 \% U
  3. Return the cosine of x (measured in radians).8 p) p! D0 b8 \! d1 ]3 y8 K( g
  4. #math.pi/4表示弧度,转换成角度为45度) M) e5 o; Z0 \- ]' f
  5. >>> math.cos(math.pi/4)* O1 f. t; ~3 b- i
  6. 0.7071067811865476
      j4 _8 z9 S8 A- M4 C6 S% i) S+ D
  7. math.pi/3表示弧度,转换成角度为60度
      [' q; j, L9 Q2 {6 \1 u( d
  8. >>> math.cos(math.pi/3)4 F- \: ?6 L7 e  }8 x6 n0 w
  9. 0.5000000000000001; ?5 C/ D, B" ~3 {6 Z0 j& l' s' v
  10. math.pi/6表示弧度,转换成角度为30度
    . |1 C7 T& T! o7 V' i3 a# {/ @+ a0 P
  11. >>> math.cos(math.pi/6)+ \6 \2 m9 B$ A+ F( h
  12. 0.8660254037844387
复制代码

% c, `& N( z7 v" Y2 ^$ o! Nmath.tan(x)  返回x(x为弧度)的正切值
) J( x+ l( n: c8 h8 q- d4 j
  1. #返回x(x为弧度)的正切值
    * i) K3 L: |! h# O5 Q' A
  2. tan(x)
    + z+ T! X8 X6 ]6 F* }$ O
  3. Return the tangent of x (measured in radians).4 a5 f  j! T. ], t- H0 F# y
  4. >>> math.tan(math.pi/4); G: _! ^) v: E; E7 S. \
  5. 0.99999999999999999 e. ?! _6 C4 x) g
  6. >>> math.tan(math.pi/6)
    4 Y6 P; A' B! H/ \1 i8 u( \3 O
  7. 0.57735026918962571 @$ z  Y$ l2 J$ ^
  8. >>> math.tan(math.pi/3)
    / D  ?6 {" g- |1 @6 X
  9. 1.7320508075688767
复制代码
6 w  G" j* q( E) R
math.degrees(x)  把x从弧度转换成角度8 P' G! |/ w1 D
  1. #把x从弧度转换成角度
      f% B: V7 ~: O$ b
  2. degrees(x)3 C+ }; E2 i' w* h7 G
  3. Convert angle x from radians to degrees.
    3 w) w0 g7 `* W5 O; w, [/ f/ F

  4. : H) [) S( h- K# v
  5. >>> math.degrees(math.pi/4)
    4 _0 i$ I" X# o2 ~* b$ B
  6. 45.0
    ) P/ W) C6 N* e' q
  7. >>> math.degrees(math.pi)& N1 E9 Q  X) c  I0 r
  8. 180.0
    ' ]  t( p5 H$ D" U$ w
  9. >>> math.degrees(math.pi/6)
    - R; r: J- Z) e' I( |1 h( _6 \, y, M! m
  10. 29.999999999999996
    # q% s% g4 J" b/ m# ~7 h5 t5 u
  11. >>> math.degrees(math.pi/3)
    5 `, v) n; n' m$ H/ \6 A7 Y
  12. 59.99999999999999
复制代码

9 I4 U& v# S' k8 U! Zmath.radians(x)  把角度x转换成弧度
5 d: e4 i) N: u
  1. #把角度x转换成弧度! T+ a8 R& B% p* v
  2. radians(x)0 I* N0 Y& k; l6 f
  3. Convert angle x from degrees to radians.0 |7 b3 V  P. z" s8 F% d0 f& E
  4. >>> math.radians(45)
      V% g' t0 J* i  a
  5. 0.7853981633974483
    9 X2 W# k! s. `# D5 |( Y, _
  6. >>> math.radians(60)- }( E6 u' o) i) w4 J. y3 G5 R
  7. 1.0471975511965976
复制代码

5 \4 L/ b" c1 umath.copysign(x,y)  把y的正负号加到x前面,可以使用0
6 u' K3 m3 X- W  W) \2 K
  1. #把y的正负号加到x前面,可以使用0
    - b) \) q2 M. I2 M3 j( E
  2. copysign(x, y)
    & A+ `* j0 X9 k' W/ C, y: `
  3. Return a float with the magnitude (absolute value) of x but the sign 9 a  @: F* ~8 e9 ^5 H
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & \9 O' O' a/ Z1 j8 K/ ^- }
  5. returns -1.0.
    # C, j# C$ `$ ^0 \9 K

  6. $ K; `' }# [) B
  7. >>> math.copysign(2,3)
    $ g0 V0 x: V+ Z( N
  8. 2.09 H3 w4 W; h0 E$ F
  9. >>> math.copysign(2,-3)
    ! i: p, C& j3 Z9 a
  10. -2.0/ X% u& N. S1 S/ J9 A
  11. >>> math.copysign(3,8)1 [  s8 ^" X$ Q3 }; ]1 P
  12. 3.04 G) Y) s' f; A1 f4 W
  13. >>> math.copysign(3,-8)) Z  N  k6 P' V3 O! V
  14. -3.0
复制代码

5 H4 V& A, J* Bmath.exp(x)  返回math.e,也就是2.71828的x次方
' a! b& D9 o" m) o- Y- ^
  1. #返回math.e,也就是2.71828的x次方
    / @9 Z* ~* s- h' R/ X
  2. exp(x)
    1 x' ^. {/ W4 G$ o8 d9 d* n
  3. Return e raised to the power of x.
    " g8 ~' p. _9 F% T
  4. $ A) P8 A. w( z
  5. >>> math.exp(1)- [+ p/ a/ e/ \& \- E
  6. 2.718281828459045# Z, t1 m" y2 M$ R
  7. >>> math.exp(2)
    & G5 b, o% @  `
  8. 7.38905609893065
    ' [9 H- `* R' x1 n9 R0 d
  9. >>> math.exp(3)
    3 @# [0 B- k8 z2 A) r
  10. 20.085536923187668
复制代码
3 a/ J# t' j, w  ^% @1 S
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1, g; t, y# m" {( s
  1. #返回math.e的x(其值为2.71828)次方的值减14 }( |$ [$ f# g7 J, S
  2. expm1(x)
    0 W' i/ u) L8 L& A, V9 U
  3. Return exp(x)-1.1 C3 c2 p3 j: g+ L& ?& V
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." D# L. u6 l+ r5 n( y5 a! V; Y

  5. 5 c- x- T2 v' J. |
  6. >>> math.expm1(1)+ V! f7 P$ k- G4 T& O: H
  7. 1.718281828459045
    ! z' \9 e/ e3 }' \" K. ^  p
  8. >>> math.expm1(2)
    * O# @* m# @. s9 N5 @' J
  9. 6.38905609893065/ B( S- M' [3 U$ e
  10. >>> math.expm1(3)) Y6 v: B0 W, M7 B5 G$ o1 o
  11. 19.085536923187668
复制代码

- g+ |1 t# g& }' v+ hmath.fabs(x)  返回x的绝对值. H- R% @. _7 ]8 \5 u
  1. #返回x的绝对值5 G* A) x1 I( R1 z
  2. fabs(x)
    7 ?0 i9 A' |1 q* P1 s
  3. Return the absolute value of the float x.+ \! H; V6 M, T, M

  4. , w# D# _. u2 n$ }/ I  [
  5. >>> math.fabs(-0.003)* o' ~+ o4 B% o$ N# o
  6. 0.003
    9 `& F4 e) c; r/ a
  7. >>> math.fabs(-110)7 H( _3 G; y, M, @- ]) r& U0 w
  8. 110.04 H0 B' @! @; X; S  g; V5 O; a4 H
  9. >>> math.fabs(100); l; i3 m( V) h
  10. 100.0
复制代码

) ~6 i% A5 I" Z) h" X7 I  L: H; y: zmath.factorial(x)  取x的阶乘的值
2 k4 ~  Q" g. l0 Q1 b. i
  1. #取x的阶乘的值' ]" Y+ f0 I) e2 ]
  2. factorial(x) -> Integral* D/ }# _& j" F! w* F& p  A
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    / z% P( b6 Y% l) M  ^
  4. >>> math.factorial(1)* A3 b* c5 o  ~, m* _
  5. 1! d! D* J1 y. D# L
  6. >>> math.factorial(2)6 ^- [6 f* ]4 t* o
  7. 2
      M' e- n; b+ R3 d
  8. >>> math.factorial(3)
    ) o, V) L. a( V4 Q0 Y% x
  9. 6
    + I! Q3 r8 e& ]' P( d9 @( a
  10. >>> math.factorial(5)+ z4 M6 O2 E  e% g( l
  11. 120# K2 u! t9 ^  z1 T5 {; e
  12. >>> math.factorial(10)4 b4 a7 r, Q4 P. |- b; @) W2 t3 ~
  13. 3628800
复制代码
  _( ^% o/ Z2 ]$ W" Q- o
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
% ]5 r0 v( m8 t
  1. #得到x/y的余数,其值是一个浮点数& e- i- I5 z9 f- i& \5 s& H0 l
  2. fmod(x, y)
    1 B, \8 W( i* `; b. Y
  3. Return fmod(x, y), according to platform C.  x % y may differ.; D; V7 T5 A) C6 S$ @
  4. >>> math.fmod(20,3)6 y. H$ g2 Y# F6 y; v) {' p
  5. 2.0% L; f' Z0 }  O' E. L3 b8 E% a
  6. >>> math.fmod(20,7)# Y  `7 i2 U2 F
  7. 6.0
复制代码
9 s! I( o4 g$ {% Q1 a7 ]3 k
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 S8 X" Y. Z. m1 C
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,* f' p( s' M9 a
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    5 t5 i6 {) D; ~+ B& X/ z, K
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1# c" ^  }8 ~) N1 r" f6 \% o5 b
  4. frexp(x)
    2 z( ~2 y  `7 |  C. W6 L1 W
  5. Return the mantissa and exponent of x, as pair (m, e).& y4 ^3 U3 y* G4 \9 O' K' ^3 r, V+ E
  6. m is a float and e is an int, such that x = m * 2.**e.
    + D9 V0 h3 x: U# ]
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    9 V% _% \+ b8 F, }7 ~2 r0 \
  8. >>> math.frexp(10)
    ; V6 t6 @7 c# o7 W& Q5 ]) w6 D! L7 `7 g
  9. (0.625, 4)2 O# Z' e" s3 L) V
  10. >>> math.frexp(75)/ q5 ]3 q1 l# w2 p' j: K* W1 c
  11. (0.5859375, 7)
    ) l. N. @' E' D! r
  12. >>> math.frexp(-40)% p9 H6 A" X  O- s: q
  13. (-0.625, 6): A6 m8 t6 C& T! b
  14. >>> math.frexp(-100)% s- W! [" G, N: J/ @4 [
  15. (-0.78125, 7), Y9 ~4 w! W+ N/ x6 \3 u; I/ \
  16. >>> math.frexp(100)
    + t! D6 y7 g9 P8 f
  17. (0.78125, 7)
复制代码
; A2 O1 K' t# C8 C/ g6 Z% G
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
" P6 k8 z7 v8 L( D5 l
  1. #对迭代器里的每个元素进行求和操作4 ]8 X. L! m, p9 u: V7 @. l, I
  2. fsum(iterable)
    0 A3 Q. F( D6 Y/ E. j0 a' ?
  3. Return an accurate floating point sum of values in the iterable.
    . G/ x5 P# p8 X0 |6 z
  4. Assumes IEEE-754 floating point arithmetic.
    8 z5 ~0 s+ H8 v7 g" O6 k1 g
  5. >>> math.fsum([1,2,3,4])
    & @: M! n" B, ?: m/ L+ r/ i
  6. 10.0$ O% u3 K, c: J4 \& d/ B
  7. >>> math.fsum((1,2,3,4))6 ?- l$ N1 F1 \0 y& p! a& j
  8. 10.0( ~* p% h- n3 T. t
  9. >>> math.fsum((-1,-2,-3,-4))' N+ o+ W! R# {( S) O# a1 ^3 Y
  10. -10.0
    3 O8 z# }, ^- |0 Q& O2 j/ Z! ?# j' t0 W
  11. >>> math.fsum([-1,-2,-3,-4])$ A1 q5 G; P) ]0 W
  12. -10.0
复制代码

& g& R: t6 D% o- c2 }" Bmath.gcd(x,y)  返回x和y的最大公约数
, n. ]$ [* E6 m# B! X2 k5 \) m
  1. #返回x和y的最大公约数
    , v4 }% e/ v# R5 k
  2. gcd(x, y) -> int' y% K7 n# d( l, @$ U% G* q2 y, T
  3. greatest common divisor of x and y
    # x3 b( |7 `5 f2 K7 }
  4. >>> math.gcd(8,6)- ^6 e8 v9 a* ?8 q3 s. r$ _' `* t% P
  5. 2
    2 ?7 J! x9 P7 P. m
  6. >>> math.gcd(40,20)7 A: g! M# e- P3 z
  7. 20. w# Z9 g$ g/ p/ J! ~
  8. >>> math.gcd(8,12)
    , Q7 g. e4 q+ r. z. I9 x. G
  9. 4
复制代码

) ?3 @" q; ?, b& Smath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False! M5 }: J& o% _) j7 Q, D# R/ P
  1. #得到(x**2+y**2),平方的值
    ' @# I  |) {# c2 u" p
  2. hypot(x, y)8 d9 F4 k2 m2 t4 d) q- J- L
  3. Return the Euclidean distance, sqrt(x*x + y*y).0 h6 M" c) I) c% f& Z
  4. >>> math.hypot(3,4)3 d* p  `- p2 P! M7 d7 _
  5. 5.0. Y: A9 \( w# l6 X1 |. v
  6. >>> math.hypot(6,8), m% b1 ]% R# _! F4 F4 m
  7. 10.0
复制代码

2 u6 n9 R; _( |* g) {1 ^& F: D' `math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
! e  Y. A$ t5 ^* i
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    8 C8 v9 I- K5 E, W3 r
  2. isfinite(x) -> bool
    % Q: r) M% A- |3 e+ i
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.2 }: X8 f+ e* t* K7 L) _" x: G0 @" R. v
  4. >>> math.isfinite(100)
    ( O. K9 q. \8 C
  5. True
    * l- A" y! {5 r# c( `2 x: V1 l) S
  6. >>> math.isfinite(0)
    , J2 i+ O1 X7 }0 I2 C
  7. True1 D( m' T, W: r2 W$ K0 t5 r: ?% u
  8. >>> math.isfinite(0.1)$ \) P" A# r% j3 v7 X
  9. True
    : x# O4 F+ H) |$ i3 V0 K! z+ B
  10. >>> math.isfinite("a")
    ( C+ p2 y: L* c7 M. u
  11. >>> math.isfinite(0.0001)
    # y8 U( T  K5 O
  12. True
复制代码

2 s, E2 V" F& C8 K# E7 [8 nmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
2 D# l3 f9 Y. D1 A# c
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    & Y1 a0 H2 }1 E. v0 K9 A
  2. isinf(x) -> bool
    3 [6 d9 o$ k, u6 d) |' E& b' j
  3. Return True if x is a positive or negative infinity, and False otherwise.
    0 Q8 H; Y+ Z+ g% A. z, U
  4. >>> math.isinf(234)& M, }  d7 H4 H
  5. False' v+ z" Z% j+ Z
  6. >>> math.isinf(0.1)! d9 Z0 l" M/ D  a' {
  7. False
复制代码
& T; J7 t/ X1 C# x, k; X1 Z
math.isnan(x)  如果x不是数字True,否则返回False; g( s8 {4 x) P% `
  1. #如果x不是数字True,否则返回False
    ; H# s! X* P* E
  2. isnan(x) -> bool
    4 [# X) U8 q' H
  3. Return True if x is a NaN (not a number), and False otherwise.
    " p6 B$ H9 B/ [) h) L
  4. >>> math.isnan(23)  q% [5 t/ m: P( A2 ^. v% B
  5. False
    # x5 d, |6 F7 F3 D8 E
  6. >>> math.isnan(0.01)! o$ ]! h: c0 A3 @7 a
  7. False
复制代码
5 z  J4 ]! G! d' p0 N3 s% ^
math.ldexp(x,i)  返回x*(2**i)的值9 H. ]" O, ^/ t. o  w' f2 o
  1. #返回x*(2**i)的值2 u' Y4 z' I- P) f! [6 K2 A0 n
  2. ldexp(x, i)
    # h4 ]  {8 W. H  F& E: z
  3. Return x * (2**i).4 ~* f3 }" t% O& d9 B
  4. >>> math.ldexp(5,5)
    " h$ F# R$ b/ v/ M( ?. @
  5. 160.0
    ) i; u9 t- p  T5 y/ c
  6. >>> math.ldexp(3,5)# E2 d9 p' Z6 e0 x) U
  7. 96.0
复制代码

1 |' b4 M8 q1 G- Umath.log10(x)  返回x的以10为底的对数" y6 W+ [' F8 A2 {! A
  1. #返回x的以10为底的对数! y" ^3 e# h* g
  2. log10(x)
    1 `  m) i# P: M; |
  3. Return the base 10 logarithm of x.
    0 L  N2 o4 z" [5 K0 U+ [
  4. >>> math.log10(10)
    0 c. J8 t' c2 v+ E5 Y  t; \+ S4 X
  5. 1.0: d4 m, e9 n" i# t2 z7 e3 `
  6. >>> math.log10(100)
    ; \' M+ L4 V7 ^4 e/ _# ?
  7. 2.0
    ) h& y: S0 r6 G/ z+ d2 u
  8. #即10的1.3次方的结果为20
    * n  r! h7 {# U% ?
  9. >>> math.log10(20)
    2 h* _8 C( C* b+ t
  10. 1.3010299956639813
复制代码
' ~- a' Y/ _6 O& n5 h( C
math.log1p(x)  返回x+1的自然对数(基数为e)的值3 t  c2 X4 y! J/ `
  1. #返回x+1的自然对数(基数为e)的值9 k5 ~4 o3 I' i
  2. log1p(x)
    4 {! B5 h' R  V7 w. x
  3. Return the natural logarithm of 1+x (base e).
    ' ]# z+ b. W) C6 h/ c2 g9 {
  4. The result is computed in a way which is accurate for x near zero.& f$ ]# }: ~5 u
  5. >>> math.log(10)3 x) _/ i- v5 C9 L& W# L1 p) g
  6. 2.302585092994046; [1 q( W' ^, A
  7. >>> math.log1p(10)
    5 v( Z' e  L# o- T( R! _
  8. 2.3978952727983707
    * }! S+ @' T. X, Z+ w
  9. >>> math.log(11)
    . ^% \4 P/ d9 W5 ?9 O5 h7 z
  10. 2.3978952727983707
复制代码

9 ?* ?9 O5 D* ^- `/ h+ Imath.log2(x)  返回x的基2对数/ o1 X7 V' ^2 u. k2 z/ K4 c% O
  1. #返回x的基2对数1 N: \9 |0 C+ f$ h4 W9 Q/ z
  2. log2(x)
    " N$ l. o0 [! y: J6 V' L
  3. Return the base 2 logarithm of x.
    ; P$ z5 \* ?8 \" d( g( E' c: p
  4. >>> math.log2(32)
    9 s$ Z4 E. Z  h7 ~4 U
  5. 5.08 W1 O8 @7 `+ m2 b, ?; t5 ]
  6. >>> math.log2(20)
    & n3 [: E+ w* h8 b
  7. 4.321928094887363
    3 _! s; S. d  c% c- U
  8. >>> math.log2(16)
    6 X# }. w7 U6 n* b5 t
  9. 4.0
复制代码

2 l$ ^) _2 C+ imath.modf(x)  返回由x的小数部分和整数部分组成的元组+ z, L( c4 X4 O" a4 F4 {7 L
  1. #返回由x的小数部分和整数部分组成的元组% j0 ~( Y  y* f  M* Q6 i
  2. modf(x)
    ( o# L* S7 G4 a! D+ ~( b
  3. Return the fractional and integer parts of x.  Both results carry the sign3 c+ A9 s& v3 ?% @8 \2 G
  4. of x and are floats., l. X3 d7 L( R' |
  5. >>> math.modf(math.pi)% P3 `! L6 T$ j5 w6 m) `1 p% S
  6. (0.14159265358979312, 3.0); p2 @/ k: _) u5 ^7 R4 q
  7. >>> math.modf(12.34)  W/ i. t  H5 m! t8 s3 z8 x
  8. (0.33999999999999986, 12.0)
复制代码
1 V- d: G! _2 H3 G3 C
math.sqrt(x)  求x的平方根
' `0 z4 r! U4 _# T/ Z+ ?5 K
  1. #求x的平方根
    ) K1 p" A1 n+ r6 }9 G
  2. sqrt(x)
    0 ]; U- ?8 }! Z$ J. s" ^+ j
  3. Return the square root of x.
    ( T: R5 a+ ^, Q5 y$ ]0 }! O
  4. >>> math.sqrt(100)
    " M2 V  i6 t# H/ E
  5. 10.0- U" Q4 _. ~* L
  6. >>> math.sqrt(16)
    ! x; h) T; R/ {8 B; I& r
  7. 4.0
    8 R2 B( n* Z- i7 \5 }1 c' D$ n1 ?( m
  8. >>> math.sqrt(20)- N% E  t4 r2 r* t+ Z) F! T
  9. 4.47213595499958
复制代码

  n4 d8 y. }; v( B0 jmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分3 _' A" ^$ {4 w, S* O
  2. trunc(x:Real) -> Integral
    7 o; d' b4 k- p! x% h$ \
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( r0 v2 X' i9 c7 {# r# k
  4. >>> math.trunc(6.789)1 G- _1 i' ?. I/ L) U9 [/ f5 d
  5. 6
    / d$ ]% S+ |/ b' u, F
  6. >>> math.trunc(math.pi)
    5 C+ P* a# B. n/ v; K) b" M) ]" w
  7. 3
      Y) l& \$ _- A4 R' }3 E
  8. >>> math.trunc(2.567)2 W% X: J. k$ m
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-3 11:17 , Processed in 0.085589 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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