新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
! V5 R! N9 y) _$ B8 C0 s0 r2 }3 |- X: c
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。* e6 j5 H* S+ M% S$ A: {
5 z* t  v' M) j5 c6 k
方法1
0 R4 N3 H4 `) Y" G; u$ U
  1. >>> import math7 W4 V! ?' h% s6 ^3 f
  2. >>> math.sqrt(9)
    4 M+ ^( W; u, {, p* z$ B
  3. 3.0
复制代码
方法2* U2 @! N( j8 }" z! }5 D6 e/ j
  1. >>> from math import sqrt
    % _9 \$ M/ j- y" q* o, l
  2. >>> sqrt(9)
    7 p9 b  U9 c3 N
  3. 3.0
复制代码
& W7 P* D1 O. o" _( k


2 Z' N5 ^& W0 u
math.e  表示一个常量3 N; K) Z! `! {' h! a( G( h5 M
  1. #表示一个常量
    ' h- p1 J$ w8 u# A$ ~; \
  2. >>> math.e" K4 |$ L) h, o. O  q4 v6 _
  3. 2.718281828459045
复制代码
" T! D1 w# ~1 U  r
math.pi  
数字常量,圆周率

- i9 h: P# y1 v) N5 \& @
  1. #数字常量,圆周率4 E3 x; D4 v" ^. [. X2 ^' J
  2. >>> print(math.pi)
    0 p6 b! j8 f+ k' `6 \9 F
  3. 3.141592653589793
复制代码

6 O! ~9 e  O" j7 P  x2 ?( gmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

2 h, Q) `( @: u# Z2 K: |& p
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x2 L1 O" q) [9 x# D, `0 G+ p
  2. ceil(x)$ C/ P: E- P) Q: V
  3. Return the ceiling of x as an int.
    9 M" u- c, ~+ A( S
  4. This is the smallest integral value >= x.' k2 ]) h8 V/ Q. O( T8 F
  5. ) d) [! Q' Y; ^/ J# a
  6. >>> math.ceil(4.01)
    " v( L8 W% U* T; |
  7. 5( O/ N5 D- H0 T( Z9 R6 n
  8. >>> math.ceil(4.99)' n, ?8 T) H7 v" I% g+ D8 A; X
  9. 5
    # K  q( V7 W/ p) b
  10. >>> math.ceil(-3.99)
    7 ~8 v! ?$ ^$ x8 I# @
  11. -3
    / i3 j/ Q7 e9 e
  12. >>> math.ceil(-3.01)
    " Y5 |+ s$ e+ O4 o! _! f' r' S
  13. -3
复制代码

+ _. f& o4 }/ z; w( ], m) ]math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 B5 e) Q# b6 W$ l; y1 Y5 W& o- W3 b+ A* b
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    5 s; J1 v) ]! N
  2. floor(x)% W7 G3 }6 o' |* t5 g
  3. Return the floor of x as an int.
    3 [- Y& f  m' i% e$ T! b
  4. This is the largest integral value <= x.
    7 a1 ^9 E" P7 }( I
  5. >>> math.floor(4.1)
    5 i# _3 d# {' l- Q2 t8 _
  6. 4- H# r, o* D5 E8 h. I4 o0 ~- k
  7. >>> math.floor(4.999)" H* D1 S/ u+ h* a2 u4 }: V1 b
  8. 4
      O' D# U% I* e- S8 r8 u  b+ x1 D
  9. >>> math.floor(-4.999)
    9 F# U+ v1 ^) C# G$ _7 J# c) I4 e
  10. -5$ x5 f5 f$ P6 n) _
  11. >>> math.floor(-4.01)
    $ X5 @( \; ~) f# O9 p% d) a
  12. -5
复制代码

& f! Y0 v+ P2 X  Q2 |, Y$ s' Ymath.pow(x,y)  返回x的y次方,即x**y
; z% `( g) l( j" v1 j
  1. #返回x的y次方,即x**y
    & P: E) [( }; ~1 Z
  2. pow(x, y)6 u! r& z6 o2 q4 w% R$ j$ ?: q) y" L
  3. Return x**y (x to the power of y).
    $ E6 e4 I4 `/ m( D
  4. >>> math.pow(3,4)/ A1 J8 P& Y, c4 t
  5. 81.02 z0 a$ a. ~. v$ b6 Q! R, Y5 L1 K
  6. >>> 3 q$ F% k; n( E: i( C3 x
  7. >>> math.pow(2,7): n* ]' b7 U4 b' a" z7 w- H
  8. 128.0
复制代码

6 d( ~. |4 h5 U' }, s8 J% j1 pmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, {% T: X  s  @, x7 H
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    * @: ^0 A* L  g( z& F5 ^" m" `: c
  2. log(x[, base])( a& D4 K- }! h4 N' i
  3. Return the logarithm of x to the given base.
    / ~# g5 c2 W  z* ]6 g) @$ W
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 L7 _* t# k2 j+ K# S; s
  5. >>> math.log(10)
    9 I. Z$ F- b1 x
  6. 2.3025850929940465 U% C8 s; x. ], x( j6 C5 {
  7. >>> math.log(11)$ H; `7 @2 b' `# B7 _
  8. 2.3978952727983707
    0 z9 A' d- K+ i4 I- }' X& D
  9. >>> math.log(20)0 p- A& k  b! A2 [* |
  10. 2.995732273553991
复制代码

0 D( v, k6 d( Y+ c/ E; Q: Kmath.sin(x)  求x(x为弧度)的正弦值
9 M8 Z" t- v# L" N1 n8 |1 N
  1. #求x(x为弧度)的正弦值
    * R2 K& l8 f# N' f1 G$ e" H; h
  2. sin(x)
    2 o1 O- @. Q/ B( ]# M: o
  3. Return the sine of x (measured in radians).( f" W# ]; c! f5 B+ `
  4. >>> math.sin(math.pi/4)
    9 |8 b7 N4 W$ ]3 d
  5. 0.70710678118654759 _- U6 x* Z* F$ t, p
  6. >>> math.sin(math.pi/2)
    ( T! A' J5 d* i9 W& U
  7. 1.0
    : g; d5 Q' P, Z9 }! G: d0 c
  8. >>> math.sin(math.pi/3)$ l/ K0 o/ W1 y# z. {7 h. Y
  9. 0.8660254037844386
复制代码
" }* u1 s; l* D% @, J, B+ I6 @
math.cos(x)  求x的余弦,x必须是弧度
- ~  P* P9 F& \( o/ P9 b
  1. #求x的余弦,x必须是弧度- ^8 j$ }! j( X4 j6 \
  2. cos(x)0 I6 C- ]' Q7 I8 _5 h4 f; n
  3. Return the cosine of x (measured in radians).
    ( w& x! M) I. @) A8 \
  4. #math.pi/4表示弧度,转换成角度为45度
    , K/ j- j& K3 {( |
  5. >>> math.cos(math.pi/4)
    8 h1 V6 a# ?& }  E; ~& _
  6. 0.7071067811865476
    1 e( ?. N) s/ @. ]
  7. math.pi/3表示弧度,转换成角度为60度+ V; B+ z  w% T) U  G1 \( l9 J4 T
  8. >>> math.cos(math.pi/3)
    . M( t$ U8 o, o$ b1 Z( }. J
  9. 0.5000000000000001
    5 L- m: E/ q! Y$ l# ~6 R
  10. math.pi/6表示弧度,转换成角度为30度. I, p- J) ]% t' p% U4 {; n9 Z
  11. >>> math.cos(math.pi/6)& n  q% q8 a8 `1 [- e3 \
  12. 0.8660254037844387
复制代码
# R( j) h9 E6 H* {
math.tan(x)  返回x(x为弧度)的正切值8 h( K2 _- J6 N* G: \" M5 `
  1. #返回x(x为弧度)的正切值
    ! V8 G9 j" Q$ f: K
  2. tan(x)
    , M  g0 E" s- ]0 T
  3. Return the tangent of x (measured in radians).
    8 D$ X- N6 s3 ]  ^; I
  4. >>> math.tan(math.pi/4)4 l- r1 Z( T* |" n
  5. 0.9999999999999999! I$ _) C  P" {3 t3 U0 \
  6. >>> math.tan(math.pi/6)
    5 s+ j5 k& f- ?. V% e" L& _& C
  7. 0.57735026918962571 Q" _! y. O9 I+ D+ v
  8. >>> math.tan(math.pi/3)
    3 F9 k9 h) m! J$ m
  9. 1.7320508075688767
复制代码

( t3 a2 S3 Q2 |math.degrees(x)  把x从弧度转换成角度
' o& [0 h* t) _& n, s
  1. #把x从弧度转换成角度' _. c! T& @  U1 w5 u) j. P- z9 s
  2. degrees(x)
    . K# ?  Q4 W/ j) P0 F' u
  3. Convert angle x from radians to degrees.6 X0 F8 q9 q) }/ Z/ p, L
  4. ! d' y% P9 D* \: J: M  u* }$ A
  5. >>> math.degrees(math.pi/4). X& c5 I7 `5 m2 l  L7 Z9 Y
  6. 45.0
    1 i' q% m0 S2 p$ c* v/ S
  7. >>> math.degrees(math.pi)3 N- t; J: O" H. x, L" R* ?4 e
  8. 180.0: y( t  f! k3 G2 X
  9. >>> math.degrees(math.pi/6)
    + X) Z7 p. i9 {  b% e
  10. 29.9999999999999968 m8 R( t4 N1 l
  11. >>> math.degrees(math.pi/3)
    ' l) }0 d2 {" n) c; C: x
  12. 59.99999999999999
复制代码

7 u( y7 ^) ^" o/ ~! E& R0 Umath.radians(x)  把角度x转换成弧度
# x! V( Q5 A& S8 N1 y4 S
  1. #把角度x转换成弧度
    & v; t' Y  p/ q1 W! z
  2. radians(x)8 z: d$ F( w$ A9 j$ w' s
  3. Convert angle x from degrees to radians.
    - }% I3 m, o$ G& N; d6 u0 |
  4. >>> math.radians(45)
      [; {/ O( t- f2 |
  5. 0.7853981633974483
    ; y/ n/ N. R! x. R
  6. >>> math.radians(60). ~1 z( Z+ {8 o
  7. 1.0471975511965976
复制代码

& B* V; x& C6 p' f* E% f: k( l1 [math.copysign(x,y)  把y的正负号加到x前面,可以使用04 c) ]2 {% \* x
  1. #把y的正负号加到x前面,可以使用0
    ! \/ y1 p" k; o5 u- T3 p
  2. copysign(x, y)9 c" N4 C' k! o4 m
  3. Return a float with the magnitude (absolute value) of x but the sign 2 }- N7 U% V3 q, V# m
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) * x: X2 [' K. P8 t6 G) y- d* ^
  5. returns -1.0.
    3 r/ ]1 e$ y. c  ]
  6. : O7 J% n; k  V% r1 ~. a+ w
  7. >>> math.copysign(2,3)
    & j# ~  K8 j9 o0 X- [5 ?% U
  8. 2.0
    # @( x& `4 Q1 C. P
  9. >>> math.copysign(2,-3)
    # X; L; ]# Z% J7 H1 P7 l. D
  10. -2.0
    % ]7 W4 h; D9 J/ I
  11. >>> math.copysign(3,8)4 J* ?8 Y% Z' ~
  12. 3.0
      S; i) `" D5 M/ H# O3 {2 F7 s9 \( E
  13. >>> math.copysign(3,-8)
    + }/ S# {4 D" z0 ~5 {
  14. -3.0
复制代码

* _% j! U9 Z# H! L% g# [1 Omath.exp(x)  返回math.e,也就是2.71828的x次方( k7 p% y8 {+ ^' K* Q/ v1 J
  1. #返回math.e,也就是2.71828的x次方
    " `% W& u) L; }. ~. l
  2. exp(x)
    ; ~$ k. l7 [8 X# D2 \( y7 P
  3. Return e raised to the power of x.; S! U: q( }2 f7 L: h; ?5 X% E
  4. - ~- j4 K' M! z5 Z6 a
  5. >>> math.exp(1)8 V& V/ k# t" }) H! a
  6. 2.718281828459045
    0 m& f# r; M( N( n. w
  7. >>> math.exp(2)
    ; R* j9 K+ i- u9 V
  8. 7.38905609893065
    & ]+ @3 f2 T4 K5 k$ C
  9. >>> math.exp(3)
    & K" m2 Z8 |8 x3 a- U4 G
  10. 20.085536923187668
复制代码
* M1 Q; b! e6 a4 m* b; u
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# f- [5 R/ L% x
  1. #返回math.e的x(其值为2.71828)次方的值减19 ~3 d. h& m8 ^/ e& f
  2. expm1(x), ^+ r  T! t9 ~0 V. N+ O# y
  3. Return exp(x)-1.
    " S- I' K% w0 T1 M9 z
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ) ]+ t% n' ]7 |+ `6 J; M- X5 P
  5. $ D/ X6 j. K, S" m( B7 V
  6. >>> math.expm1(1)
    ; \; U) C& h- f0 R* a2 x
  7. 1.718281828459045
    1 o! u+ F4 \$ v# I/ z, n4 @
  8. >>> math.expm1(2): K' y" {3 V, n9 m, \, c. O' m( `
  9. 6.389056098930650 l  E5 K" W  R: O+ W/ B
  10. >>> math.expm1(3)5 c; V/ k4 M( I- Q4 w# ?
  11. 19.085536923187668
复制代码

! l/ l! s- P7 nmath.fabs(x)  返回x的绝对值6 k: s$ l" W8 O1 u+ ~
  1. #返回x的绝对值3 V6 v5 U, M5 ~6 t4 [7 L/ M2 p
  2. fabs(x)
    + n1 h/ y  S: v- {$ ?! L' U
  3. Return the absolute value of the float x.' \! S& _% D6 e, h* ~- p

  4. 7 C4 S1 H1 Q$ m) h' h9 s/ S+ c
  5. >>> math.fabs(-0.003)
    - d9 C& |1 A; Y2 V; u
  6. 0.0034 ~, I4 {- m/ c$ T
  7. >>> math.fabs(-110)
    9 p; H, q8 U; P
  8. 110.04 P; q  {( m' V) d; [  }
  9. >>> math.fabs(100)/ L" H' L8 Z& b  s
  10. 100.0
复制代码

4 i: G9 |8 q- }& Mmath.factorial(x)  取x的阶乘的值
% q6 q2 l/ n. x% `1 q* f6 _4 L
  1. #取x的阶乘的值
    / I$ c; p3 w, P7 w' n# {" U% N) k) X
  2. factorial(x) -> Integral0 u0 l$ t, C* j" x5 A" U% ^* q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    + g$ B5 J; R( B6 i" f$ j
  4. >>> math.factorial(1)
    + f: Z/ ~. X0 s
  5. 1- Q  u3 i2 A2 M4 v5 V- t% z% s5 \
  6. >>> math.factorial(2)* c4 Y/ U9 Y# d0 C. T8 \/ G
  7. 2
    ! ^) o) m% c8 l4 H: _9 R$ p
  8. >>> math.factorial(3)0 S" S7 f2 i  E( G& W  k
  9. 6( V- _' b# s8 F# \/ b5 \
  10. >>> math.factorial(5)
    " }; A& I8 B. e# @' n) f
  11. 120
    ' a5 [9 u+ z5 h  t" [
  12. >>> math.factorial(10)
    + X9 D  N! ?  v! Z; Z
  13. 3628800
复制代码
# Z! |9 q0 O5 _: |  x
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数  j+ l; `0 n/ c: e4 j* @  ~+ [
  1. #得到x/y的余数,其值是一个浮点数2 ~# u# q4 Z( o/ Y4 ], }: I
  2. fmod(x, y)- A4 U" h) c/ P. W
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ; D% Q9 |) d) f1 R+ ~# \& c0 M
  4. >>> math.fmod(20,3)
    ( L& c, N" P6 v
  5. 2.0! K$ {+ v0 M$ F) y  L  E$ d
  6. >>> math.fmod(20,7)
    / O$ k. i6 S/ N5 K2 i/ h0 P2 L( y
  7. 6.0
复制代码

# Q2 [+ q; m1 a- N; H% Dmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围3 @. W' ~3 q* i4 J5 o. Q1 E8 g
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,( u: i* F* M& H6 d6 ~
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" }( U: q% |- u7 ?' R1 f
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1) q) E* w' r3 m3 A1 B6 Z
  4. frexp(x)/ L: B) {& C6 ^0 }+ N5 R
  5. Return the mantissa and exponent of x, as pair (m, e).
      i( Q" V* t$ n0 Z4 B
  6. m is a float and e is an int, such that x = m * 2.**e.
    , Y$ E# I+ ~" }3 N$ R( Q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    + H9 y) N* u, `3 D$ u
  8. >>> math.frexp(10): ^* Z% \; V. k$ K, {
  9. (0.625, 4)
    * {; ^- v% V0 l* `
  10. >>> math.frexp(75)
    + N! n" h5 p4 s: l
  11. (0.5859375, 7); K" H  Q' C3 E% `
  12. >>> math.frexp(-40)
    1 ]& m& Q" r& a
  13. (-0.625, 6)5 `: _3 l: C; o) G. B& }% e
  14. >>> math.frexp(-100)
    . o( Q- t1 a# N9 G' R% C
  15. (-0.78125, 7)( i) A0 B' g9 }/ X( u
  16. >>> math.frexp(100)' s" A; ?$ V, e% n
  17. (0.78125, 7)
复制代码

/ y1 ]# ]8 i' e  ?2 tmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
  r5 k( Y+ o' C$ H1 i& l
  1. #对迭代器里的每个元素进行求和操作3 J/ s# t1 A3 u" [( N1 u5 D% @
  2. fsum(iterable): q# w3 N8 Q; d7 t) {/ G! F  i
  3. Return an accurate floating point sum of values in the iterable./ ^. e) Z! R' @( @: `9 Q0 c- x% ]3 g
  4. Assumes IEEE-754 floating point arithmetic.
    7 C. J. g- K; H+ B# y
  5. >>> math.fsum([1,2,3,4])( J9 p4 M8 r. k) p6 l9 K: j3 f
  6. 10.0
    . o9 l3 N1 R- B  K" y- ?
  7. >>> math.fsum((1,2,3,4))
    , B+ z3 x3 Z/ T: z% P. Q  O  ^$ H
  8. 10.0
    + O$ X; ^: x' |  `
  9. >>> math.fsum((-1,-2,-3,-4)). \: u2 b6 |& p$ d6 E
  10. -10.0: ?, ]2 C! c4 z; t
  11. >>> math.fsum([-1,-2,-3,-4])
    ! b& o) D/ l0 K$ C- g. L4 `* A5 Z
  12. -10.0
复制代码

" |6 x6 o: X1 Z# C# omath.gcd(x,y)  返回x和y的最大公约数: K! u6 l5 r% ?# U8 t
  1. #返回x和y的最大公约数$ N) u1 p" y& i' J2 b) f! _
  2. gcd(x, y) -> int
    5 Z4 J3 \8 ?' G6 P/ N
  3. greatest common divisor of x and y
    ) q% @0 y% G6 p
  4. >>> math.gcd(8,6)9 f2 X+ W" {! ?7 }
  5. 2
    : u4 b' D5 G% G- m; ~
  6. >>> math.gcd(40,20); R7 ]: z0 G6 Y( R
  7. 20
    * M3 p( D/ T- ~3 O$ Z0 |" g, |
  8. >>> math.gcd(8,12)
    " S* d* i6 K9 s0 j+ H" l
  9. 4
复制代码
2 d+ n; G! Q- G" f' ^2 j4 H( D
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
9 n9 W$ }5 a, y9 [, u
  1. #得到(x**2+y**2),平方的值0 ~/ K2 \; H8 ?; E' A; A
  2. hypot(x, y)
    7 y% S$ c, G6 U
  3. Return the Euclidean distance, sqrt(x*x + y*y).; a- e$ G7 ^6 B/ ^2 A
  4. >>> math.hypot(3,4)
    7 t$ D( N3 M8 K( C4 a. k
  5. 5.04 C" k) y3 W* ~5 S; k3 o0 M
  6. >>> math.hypot(6,8). s  D, {+ ?8 j0 W" J/ ~& V  K
  7. 10.0
复制代码

+ w' |' j7 K0 h/ l9 ^/ Jmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False8 f9 t2 l) h0 ?) w& D: |$ z
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    + }$ |# U/ `: v- f. T7 I  K
  2. isfinite(x) -> bool
    7 j7 l! D" P; Z+ `* _9 n$ g
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    " o, r# x3 X' X3 K% Q1 y4 |
  4. >>> math.isfinite(100)( i: _8 Q8 H9 N' q; m% e3 l# Y2 z
  5. True
    ! Z$ b/ P5 @: q% ]) q9 c
  6. >>> math.isfinite(0)
      a# S; J. u# M& V- i9 L+ ?
  7. True
    * q% X. ^6 C1 D9 m
  8. >>> math.isfinite(0.1)
    # q0 W" W/ [8 s; t& n) \; d
  9. True' g, e1 C" a; t) }5 T) j  v, A
  10. >>> math.isfinite("a")! H3 Q$ f7 H* @9 |- u
  11. >>> math.isfinite(0.0001)
    6 b( Q1 R& H9 U2 J. w/ B6 G- ^
  12. True
复制代码

" W; }2 B2 F( q' w9 Pmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
1 |& }+ Q+ E* f7 D( k5 u7 r
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    2 W4 f4 I( F% u' s  a
  2. isinf(x) -> bool
    7 i5 f7 c7 s2 C5 {/ Q
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ( l9 S3 f2 h- g* Y
  4. >>> math.isinf(234)5 g6 c4 _7 a3 Q4 @  \- p3 _
  5. False  \' g9 b7 R* l% C* l: s& q
  6. >>> math.isinf(0.1)* W" ^  v4 W2 N, u* Q
  7. False
复制代码

. w' a3 ^6 D  q; I. U5 t' mmath.isnan(x)  如果x不是数字True,否则返回False
$ M- F9 h3 F6 l" }) g
  1. #如果x不是数字True,否则返回False
    8 R) W$ {9 _7 `9 c6 {4 [( n# v( Y
  2. isnan(x) -> bool
    1 |" C% Q3 b  o% `' W* g
  3. Return True if x is a NaN (not a number), and False otherwise.
    , ?! c, `4 n+ u" j- T, i/ J8 s3 C
  4. >>> math.isnan(23)
    % Y, l6 M$ e' {2 V4 r7 ^) b) s
  5. False7 a9 c7 m8 d& o) w
  6. >>> math.isnan(0.01)$ n8 t, c4 _$ t* j# r2 R
  7. False
复制代码
* @: O* W$ k# @' q7 U
math.ldexp(x,i)  返回x*(2**i)的值
- W0 j- X1 C* j- G: U
  1. #返回x*(2**i)的值
    : I6 J0 q4 v( S8 M0 C+ u8 @3 j
  2. ldexp(x, i)$ l# Q; [# w* R: v* A) I+ m
  3. Return x * (2**i).; k* u' @* E* {* V; `2 F
  4. >>> math.ldexp(5,5)/ A6 O. t0 h- O1 s% m
  5. 160.0
    3 B# M% l0 _' V  ]# t# W; L
  6. >>> math.ldexp(3,5)
    ( Q9 M  p+ C4 J( C. h* ~7 [. [8 k
  7. 96.0
复制代码

% H2 R' a( I- Q" [* Y, r) a) emath.log10(x)  返回x的以10为底的对数
" D* b3 m! P- W/ [. A
  1. #返回x的以10为底的对数
    8 H# T5 ~% `5 U# y5 E# U
  2. log10(x)7 |; C& Q: g9 V# X5 R# b/ W
  3. Return the base 10 logarithm of x.3 o9 ~/ j+ p1 T, W  e* P2 s
  4. >>> math.log10(10)0 Z  ?7 U# g  J3 W5 v: t, H
  5. 1.0
    ; M6 S6 P6 f9 H
  6. >>> math.log10(100)
      D& Q7 r& G, ^, _- [4 H
  7. 2.0
    , l! H+ F4 a  {$ t1 H; p
  8. #即10的1.3次方的结果为207 A( `' {5 ]! O, d6 J: |2 z7 H1 a
  9. >>> math.log10(20)
    : V% a2 |  Q" }: u9 J
  10. 1.3010299956639813
复制代码

7 H0 K+ W, C5 e: K5 o6 ~8 x% O. }math.log1p(x)  返回x+1的自然对数(基数为e)的值
' Q8 O; k! s4 E: F) X
  1. #返回x+1的自然对数(基数为e)的值
    4 d) ^. _" {$ l* a
  2. log1p(x)$ E2 j! D: e5 x; B+ y! A9 H3 {
  3. Return the natural logarithm of 1+x (base e).  c3 H. @/ i5 d8 }$ ^# H1 O
  4. The result is computed in a way which is accurate for x near zero.9 R. k! i2 N8 O+ k, ~2 a2 \
  5. >>> math.log(10)) ]- l5 W- f$ ?. u9 X
  6. 2.302585092994046: \- A1 _0 s. E- {; W8 n2 A
  7. >>> math.log1p(10)+ V* x0 s- W1 l2 U& S' J3 X( d
  8. 2.3978952727983707$ ^$ G9 R4 W2 o- M) V" C+ S7 ^; ~
  9. >>> math.log(11)
    % w6 [3 Y. [: w; h
  10. 2.3978952727983707
复制代码

+ K  B( k) f' @, V+ I6 O5 Wmath.log2(x)  返回x的基2对数
2 P5 m; ^: D8 O! D4 t- b2 q
  1. #返回x的基2对数2 H& y. d; L" c: u" i( U
  2. log2(x)3 X0 s& u2 I/ F% V& o' S# z9 d' }
  3. Return the base 2 logarithm of x.: I* D4 r/ k5 ?6 S+ I7 Q
  4. >>> math.log2(32)
    ; [! m9 \- b, }7 p# \! ?: t
  5. 5.0
    ' E& W  }4 S$ `& A/ m0 ^- F
  6. >>> math.log2(20)
    7 G7 K% z  n- F5 c6 Z, |
  7. 4.321928094887363/ `9 H5 b! U# N1 c1 G: {: a) Y
  8. >>> math.log2(16)! I8 V/ G, N& V
  9. 4.0
复制代码

" @: q+ L- J) r( R  g9 E3 H$ dmath.modf(x)  返回由x的小数部分和整数部分组成的元组
% l  z1 V9 J* z1 q. f8 L; a& O
  1. #返回由x的小数部分和整数部分组成的元组
    8 s0 h& Z: q7 {8 t# |1 ^
  2. modf(x)2 ^6 h* W+ L1 y
  3. Return the fractional and integer parts of x.  Both results carry the sign; m! L, Y) j, s7 Z+ x0 ?
  4. of x and are floats.0 I! A+ {: q# Z$ P3 C. _8 Q3 T
  5. >>> math.modf(math.pi)
    $ [0 _5 L" Z3 y: \8 Z
  6. (0.14159265358979312, 3.0)
    ! e; [0 Z1 A9 y  [
  7. >>> math.modf(12.34)
    ' `4 `, Y/ }( `* M- u
  8. (0.33999999999999986, 12.0)
复制代码

& b" K% A# R. _math.sqrt(x)  求x的平方根
  s% X  I' {6 S- @$ A' Q% i
  1. #求x的平方根
    + _' W+ `- ?9 k; t
  2. sqrt(x)
    : z- d8 j+ @( n4 |" R! X
  3. Return the square root of x.
    7 x  H; j# h  [, u5 ]$ x
  4. >>> math.sqrt(100)
    * X. W* v$ T6 L
  5. 10.0, V5 e, J4 m4 J
  6. >>> math.sqrt(16)8 `3 M: n" R6 z6 d$ E
  7. 4.0
    2 M( r! ~8 z% J* N" h
  8. >>> math.sqrt(20)
    & |8 w& p# L) b7 K; k6 x" l
  9. 4.47213595499958
复制代码
& j! N: u4 ?0 M# `% ?. o
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分  }* ~- Z! q) p0 m8 M
  2. trunc(x:Real) -> Integral
    9 L2 U, I% I& d. ~- r1 j/ s6 e
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# E) H8 O  g  e4 z. Y- E+ D
  4. >>> math.trunc(6.789)& c2 R- O: U) ]. `3 E
  5. 6
    6 V) A2 e0 [& `( g' C. ]; Y+ v
  6. >>> math.trunc(math.pi)
    ' ^2 p  B8 s7 c6 r( g3 B1 g
  7. 3# R5 P3 N  j! z6 G( J' m. E* A) L
  8. >>> math.trunc(2.567)
    4 |5 i, l7 ~; q0 e
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-1-3 08:25 , Processed in 0.076751 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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