新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
) D7 I/ y. i  i* \+ w. q% ~0 O
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& H* e# @2 J+ b) R
( w( F2 X" m4 c  R: ^- M& u
方法1
% m9 v  T! V* W% x3 v. T/ y* r
  1. >>> import math
    ) e: F& F( b7 J, z0 G4 }
  2. >>> math.sqrt(9)
    , f- @( ~8 j* w2 C5 s2 x5 q$ ~
  3. 3.0
复制代码
方法2
0 L* G8 ~$ W1 u9 x
  1. >>> from math import sqrt
    # P% Q5 p* t6 @  m
  2. >>> sqrt(9)1 _0 u: o8 v1 P6 p9 B/ E0 z% h
  3. 3.0
复制代码

1 w& _2 e- B5 w6 S0 g  S, p( Q
  X8 y9 s5 G+ v# l
math.e  表示一个常量
. V) ^7 W; c. e% r0 k
  1. #表示一个常量$ m; ]% l+ g$ G
  2. >>> math.e
    . m7 O0 J$ {4 m' L' f2 D4 x
  3. 2.718281828459045
复制代码
' l6 }2 r, |0 J% k& W
math.pi  
数字常量,圆周率
# Q. N% Y$ `9 i7 ~% H# ~, p
  1. #数字常量,圆周率7 @7 Q6 s% J+ _7 `" D5 [2 E- G" \
  2. >>> print(math.pi)+ ]4 S, X' p# v! Z1 N  }  k, d
  3. 3.141592653589793
复制代码
1 X- g" a- h6 R+ L9 |6 \: T5 ?: @' A* U3 d
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

$ x( \1 }6 V; o" i
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x3 Q& G2 e7 ^1 V$ G& Q
  2. ceil(x)
    5 O9 q6 G7 U8 ?/ \7 ~% |% @" y
  3. Return the ceiling of x as an int.
    " U7 y+ c6 P! e4 K
  4. This is the smallest integral value >= x.
    5 t, Z# ~* K  G8 R
  5. ; k" ]. f, U* J8 ?* E% C
  6. >>> math.ceil(4.01)
    ' D3 \# S! W/ X2 \: j$ G+ N+ R9 {/ o
  7. 5
    ' D* O1 F2 P) n$ H( M
  8. >>> math.ceil(4.99). C/ O$ F+ O; B5 k! s$ Z
  9. 50 R/ A/ {  x1 P  R1 Z/ k/ l% W
  10. >>> math.ceil(-3.99)
    + Q- }4 t# `6 Y5 x! K
  11. -3
    , U6 `) A/ p3 b3 H8 I
  12. >>> math.ceil(-3.01)( R# A$ v% I1 C3 G4 \- X+ P' o
  13. -3
复制代码

9 Q  w/ N  ^0 J: Nmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 W* {; w! z$ q; j/ [4 E
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身& j% b- U# ^5 `# }
  2. floor(x)& W. X* Q1 S! [, f, Y% j
  3. Return the floor of x as an int.! Q7 V' S' _9 v1 q$ @) i
  4. This is the largest integral value <= x.
    , _4 ^' O0 m# D. T( i* z4 e
  5. >>> math.floor(4.1)2 a* y( c! _" y( A
  6. 4* Y; l; R# N! n# R, g3 [) T( K: V
  7. >>> math.floor(4.999)' p" m4 d- i+ ~- t3 `9 H3 G
  8. 4
    3 t' j6 L8 }( Y# }
  9. >>> math.floor(-4.999)1 a- @; I8 f6 \
  10. -55 p. n5 I( i4 i6 B
  11. >>> math.floor(-4.01)
    # C5 S9 q* t3 u9 i9 n3 P! B, n
  12. -5
复制代码

% }# m6 F5 f3 ^; Imath.pow(x,y)  返回x的y次方,即x**y
& @8 H/ B. d* w% e0 e+ {
  1. #返回x的y次方,即x**y5 Z9 `4 q  m& j' f) m
  2. pow(x, y)9 i3 J6 f- w" ^! L4 V- O& P1 J( r
  3. Return x**y (x to the power of y).
    + S: S& F4 @/ I, o6 D9 `
  4. >>> math.pow(3,4)+ ]" S6 I3 I& ^. y# V
  5. 81.0
    ( S+ ^) B! O, h: Z5 T
  6. >>> 2 k' y" v" O2 a9 @/ u9 j& Z  S
  7. >>> math.pow(2,7)( s& w# ~0 }2 ~& y9 ]
  8. 128.0
复制代码

% ~" c; i! A7 ^" t/ qmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 c8 S" b, ~) x
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' h) P2 ?/ P6 u  i
  2. log(x[, base])  l" |7 R7 i8 b# d3 |, W* q  N
  3. Return the logarithm of x to the given base.
    ; {3 b& ]2 E; x' A' y
  4. If the base not specified, returns the natural logarithm (base e) of x.
      ^9 V/ i* M# P) A
  5. >>> math.log(10)
    ' F6 L+ x9 X* R. z
  6. 2.302585092994046
    ; s/ k- H3 I  e# x+ d0 ?' ?
  7. >>> math.log(11)
    . I- D! e: e% A8 f. L
  8. 2.3978952727983707) g4 j. ^4 t: k6 [
  9. >>> math.log(20)
    " a8 Y. ^' `" T/ w0 M/ S" l9 i
  10. 2.995732273553991
复制代码

! _" W4 `% l" o' J2 \. q0 amath.sin(x)  求x(x为弧度)的正弦值
/ s4 v5 V( n# l/ O" [% Z) i
  1. #求x(x为弧度)的正弦值
      {- q4 U3 @1 X. _/ ?
  2. sin(x)/ B1 Y" n( f4 x
  3. Return the sine of x (measured in radians).
    ' f" C3 X$ ^  ~
  4. >>> math.sin(math.pi/4)
    * _" E6 ?) C- }. F! M0 `
  5. 0.7071067811865475
    ' ?0 j% \. D: y6 E& q% F
  6. >>> math.sin(math.pi/2): _4 q9 H, K7 k
  7. 1.07 h- w( ]2 V8 C2 H
  8. >>> math.sin(math.pi/3)
    8 s# b$ M/ v0 g) Q
  9. 0.8660254037844386
复制代码

0 z6 H$ U2 E2 Qmath.cos(x)  求x的余弦,x必须是弧度
0 e! C. ~. A8 W1 g
  1. #求x的余弦,x必须是弧度
    ( }* h, h) x: t
  2. cos(x)
    1 m2 i5 q2 Z- S! E# d8 O
  3. Return the cosine of x (measured in radians).
    2 T# s, i# b! _
  4. #math.pi/4表示弧度,转换成角度为45度
    ' @" O* r/ ?+ {$ U+ I
  5. >>> math.cos(math.pi/4)) v$ P% K* y( [1 T
  6. 0.7071067811865476# x+ [. b; ?+ P9 e% g5 i8 H+ {( s
  7. math.pi/3表示弧度,转换成角度为60度: }( l9 L: M% s+ v
  8. >>> math.cos(math.pi/3)
    ' J# F/ c/ ]% a6 O- D
  9. 0.5000000000000001
    ; u2 W3 V, U3 c5 n8 v! ?
  10. math.pi/6表示弧度,转换成角度为30度
    ; U% j# F+ Z2 h7 z* \
  11. >>> math.cos(math.pi/6)) r% a$ F+ n9 y
  12. 0.8660254037844387
复制代码

- c) E, S' G* c4 p# u3 Tmath.tan(x)  返回x(x为弧度)的正切值* w8 x3 I( S" L3 \6 X& Z
  1. #返回x(x为弧度)的正切值
    % D% ~5 M& ?( X
  2. tan(x)" E. \$ K9 O( l/ @# l, C
  3. Return the tangent of x (measured in radians).
    ; A' ~3 W) u: O/ d# m6 Y, i
  4. >>> math.tan(math.pi/4)7 _' k/ V1 Y, ]8 V! E
  5. 0.99999999999999992 }: N: b# W1 A; |
  6. >>> math.tan(math.pi/6)% s6 }8 H' l9 V
  7. 0.5773502691896257
    - D7 {3 @) a) Q
  8. >>> math.tan(math.pi/3)8 M$ [6 a4 }- g8 j5 }. e$ {6 V
  9. 1.7320508075688767
复制代码

4 _* r7 f  C3 p- E  w6 M% O* @math.degrees(x)  把x从弧度转换成角度* J. f: ?4 Z; P% [* K
  1. #把x从弧度转换成角度
    3 a( Z5 g$ P8 ?5 ]" F7 B* ^
  2. degrees(x)
    6 a$ o& c+ R7 H! _. e. d
  3. Convert angle x from radians to degrees.  p, Y" l) W! E$ o

  4.   u: v, e' {/ u( o
  5. >>> math.degrees(math.pi/4)4 r# o/ s, O% i- R. y1 P
  6. 45.0& D( ]8 @5 Q7 r8 U* r# Y* q
  7. >>> math.degrees(math.pi)
    / j  ?' @* M% {7 g6 o8 u( _
  8. 180.0+ q3 K9 ]9 m! w$ E" c# G3 @  j  g; K
  9. >>> math.degrees(math.pi/6)
    5 m% h! T5 {: g% r: a$ s
  10. 29.999999999999996# o7 f3 p: \: _, Q$ ?
  11. >>> math.degrees(math.pi/3)
    + }9 C( N( s7 w
  12. 59.99999999999999
复制代码
" @/ [+ M7 ~0 v, f  B+ f
math.radians(x)  把角度x转换成弧度
5 M, F: X2 b0 `+ S
  1. #把角度x转换成弧度5 J4 n5 J9 ?9 d' \, {) @& a* ^8 ]% i3 B
  2. radians(x)
    7 X7 y0 o( A* {1 a; x
  3. Convert angle x from degrees to radians.8 I0 }. {; B& _" d- m9 n/ P
  4. >>> math.radians(45)2 H! H6 y  k; P9 `" d
  5. 0.78539816339744836 E% h$ Q+ P5 T6 n5 A$ t
  6. >>> math.radians(60)! a3 Z+ e; y# Y) j" L" m2 p
  7. 1.0471975511965976
复制代码
$ P- A. N9 J8 A% j/ q* s
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
1 ?) |4 H/ y  J8 I% s
  1. #把y的正负号加到x前面,可以使用0
    + ?# ~- ~" g' E! D1 d. ~
  2. copysign(x, y)) Z1 M' `. F6 C
  3. Return a float with the magnitude (absolute value) of x but the sign
    . i! i  r; \3 d
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 0 V# g' p/ c  U# a, i7 y; S. {
  5. returns -1.0.
    2 ?( R8 `% Y+ Z) A

  6. ( T9 M# ?& K9 j( ~: o3 E+ r0 ?2 a
  7. >>> math.copysign(2,3)% z1 F( G! ^. k" u$ Q
  8. 2.0
    8 s: g! T$ T3 {4 ]% U, `
  9. >>> math.copysign(2,-3)6 _" \$ j8 ^' d2 U
  10. -2.04 m/ H( z- ~- Y8 |  D6 i
  11. >>> math.copysign(3,8)
    4 k; i1 r4 D& s# M6 s
  12. 3.09 J# T( q2 A* G+ Z0 ?& g2 M
  13. >>> math.copysign(3,-8)
    5 N" P. @( _7 F* ?  s; C
  14. -3.0
复制代码

; N9 E9 s$ ~) n7 R2 Hmath.exp(x)  返回math.e,也就是2.71828的x次方; @$ J$ j* U! D* v7 w# E2 v5 U9 W
  1. #返回math.e,也就是2.71828的x次方
    : p. V0 |* d. ~
  2. exp(x)
    3 y( m' v5 |' k8 x: _
  3. Return e raised to the power of x.0 f; Z  ?4 g. L. ?
  4. 9 v. `. M3 z; V' x6 m) \# E1 I
  5. >>> math.exp(1)$ Y- ?: y1 S7 |: ?0 C0 @
  6. 2.7182818284590454 p5 W$ z+ R1 T0 B
  7. >>> math.exp(2)
    % q! S% B9 c5 w. i. Q$ f
  8. 7.38905609893065* G: m1 Q- H+ _- ~: n9 ^
  9. >>> math.exp(3)! c' _+ m- Q; b9 o3 l
  10. 20.085536923187668
复制代码

; f8 x. @1 X- g4 `' A5 tmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1. s% q9 R  k$ @  [  R, a! w, c% W
  1. #返回math.e的x(其值为2.71828)次方的值减1
    2 \% u& Z9 X2 E! ^% w
  2. expm1(x): ^3 Y( {; S: N  \2 g6 ]9 A7 u
  3. Return exp(x)-1.
    * t; ?: j% W6 ^6 B& E* k
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ h$ n6 \  c6 W( p
  5. ! Y# Q/ ?# P5 o
  6. >>> math.expm1(1)
    1 B- k& ?3 s/ c" P( |
  7. 1.718281828459045
    ! Y" I: O* G  l2 B, V8 I
  8. >>> math.expm1(2)
    * g# _9 c: M: M" I% F7 h- J, A4 @
  9. 6.38905609893065
    . v' j+ a, g0 W
  10. >>> math.expm1(3)
    ) p) d  }6 s) l" Z: T3 \
  11. 19.085536923187668
复制代码
7 U+ i3 ~9 @8 |8 c* f0 B+ Q
math.fabs(x)  返回x的绝对值
7 a8 j) X* w+ |5 C
  1. #返回x的绝对值
    , v& o% d) I; Z" J& V
  2. fabs(x)
    6 Q/ ~) Z' Y2 g$ w
  3. Return the absolute value of the float x.2 G) n( }/ P. c+ B

  4. 9 [" o: C2 J  d; Y
  5. >>> math.fabs(-0.003): J- N  _+ G! Q  V/ N
  6. 0.003% K& G) e; B3 Z
  7. >>> math.fabs(-110)2 k& D+ B0 X$ [$ m9 E
  8. 110.0
    , z, U+ O5 V8 [# l- K8 S( M
  9. >>> math.fabs(100)
    ( Z7 [( Y) z2 V; `; d
  10. 100.0
复制代码

! w( W7 s$ e& [math.factorial(x)  取x的阶乘的值! @/ v7 E( U8 c
  1. #取x的阶乘的值" c: r, I9 ?$ i7 O
  2. factorial(x) -> Integral. j% n1 ~) M* O. W5 B( G
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 ?/ X6 f' o! w  F
  4. >>> math.factorial(1)
    " F' \# E- _: t5 v
  5. 1# E2 l8 J" W& v4 b! `
  6. >>> math.factorial(2)
    5 G3 M1 L  j3 P0 C; q
  7. 2
    0 V* I! |4 D5 a5 W! e; a* U
  8. >>> math.factorial(3)
    ( {: j: @" y, K! {: U% ?1 k$ e9 f1 p* q
  9. 6
    ; r5 O( _" `6 y$ @; T
  10. >>> math.factorial(5)
    5 a! @6 d1 X0 }4 i2 ]
  11. 120
    2 I9 I! E. P3 y5 t1 Y" B
  12. >>> math.factorial(10)
    1 y! h$ x1 Y8 U  Z$ s% @: O7 G! g
  13. 3628800
复制代码

0 E# a* @  k8 a6 Fmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数' [8 t4 V1 T% J  n" t' M- M# b6 Q
  1. #得到x/y的余数,其值是一个浮点数1 V4 n9 f0 R" A6 `5 d7 F( v
  2. fmod(x, y)
    & R: y8 P9 p; G" F' W
  3. Return fmod(x, y), according to platform C.  x % y may differ., B, ]5 U# D3 a  f4 b
  4. >>> math.fmod(20,3)! J. h3 F$ b( N1 V
  5. 2.0( }& X7 I, J9 a4 o( j; {2 ~) j
  6. >>> math.fmod(20,7)- h( v, a, Y3 s* D  }9 q' `
  7. 6.0
复制代码
% ?6 g) T0 N* ?2 ^# R" x5 A
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围. c0 k/ Y' F# ?3 n/ ]; C% i
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ) l! M% _2 a1 \& c/ m1 P1 }
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ; n4 A3 s4 j9 O9 q5 T; G, F
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1; I5 p& H, _4 n8 g# S
  4. frexp(x)/ u& `( H0 A6 I/ I% }
  5. Return the mantissa and exponent of x, as pair (m, e).
    / z% n8 r1 |1 Q1 C
  6. m is a float and e is an int, such that x = m * 2.**e.
    9 G* h# A1 L; x& m
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.4 q) L/ r2 ]4 u0 Y- P! i- r
  8. >>> math.frexp(10)
    7 Z9 X8 @+ a  u* ]9 }
  9. (0.625, 4)
    " K: T6 w8 {- \3 R
  10. >>> math.frexp(75)
    % W- E' V" Y, ]9 D% E; Y: N- P
  11. (0.5859375, 7)
    - d/ r1 t0 d) K" e3 I- R
  12. >>> math.frexp(-40)) g9 k+ N( I: G( C- w
  13. (-0.625, 6)) I4 r6 q" O7 u6 D/ k* w/ Z
  14. >>> math.frexp(-100)
    ( \) b( D3 I! w2 G
  15. (-0.78125, 7), K% f0 ~0 z  V
  16. >>> math.frexp(100)
      l0 a! _/ L) R; z
  17. (0.78125, 7)
复制代码

* p. r1 J/ u' X9 B9 X* N8 |" Wmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列( u5 D- o. d/ [
  1. #对迭代器里的每个元素进行求和操作
    1 ^: z5 e8 F, E+ d# g( A
  2. fsum(iterable)8 S% q" E, \0 Q8 F. ]' B) O1 X
  3. Return an accurate floating point sum of values in the iterable.7 J8 A! ^0 H5 ^3 f3 a# f/ t
  4. Assumes IEEE-754 floating point arithmetic.! ]: ?9 M$ K; V" [
  5. >>> math.fsum([1,2,3,4])
    5 w$ _: h0 w# [) }0 R) v' k" ]4 B: Y
  6. 10.0
    , w% M) F! y2 r, p4 C  P4 r0 a
  7. >>> math.fsum((1,2,3,4))7 e* c2 q/ D4 R. z3 n1 b' W
  8. 10.0- N! ]' t5 w: y# S3 x- Q. b* J
  9. >>> math.fsum((-1,-2,-3,-4))
    0 y; V/ |+ I: w. L/ U: e5 `7 F; e
  10. -10.0
    2 F7 t8 V: b, D
  11. >>> math.fsum([-1,-2,-3,-4])2 B% j' A5 f' m. \  c; F
  12. -10.0
复制代码
: ]7 ?. p" e0 y8 }" N" h& A
math.gcd(x,y)  返回x和y的最大公约数: T; _4 W- v" @" _! K6 b. B2 U
  1. #返回x和y的最大公约数
    0 u; R' N0 X4 G* q- Y
  2. gcd(x, y) -> int$ A' F3 P/ T1 J' A* J4 T' S
  3. greatest common divisor of x and y$ ^) M% H# O' Y/ E
  4. >>> math.gcd(8,6)
    3 i* C8 c. x# i6 a2 m
  5. 29 Z7 S- T: F) e1 K
  6. >>> math.gcd(40,20)! R% j- z4 d1 R9 I1 r: W
  7. 20" V' i) s. c) p$ I0 }
  8. >>> math.gcd(8,12)" N; F; c* F7 M2 n) J5 C; }; ~6 I8 i
  9. 4
复制代码
" P4 b3 M; U  ~# O1 Q' d
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
$ U" ]# M6 q- D
  1. #得到(x**2+y**2),平方的值% I8 }, P  G7 t- Q% z7 q
  2. hypot(x, y)8 w5 c9 k. T# T
  3. Return the Euclidean distance, sqrt(x*x + y*y).
      X# y, w: h+ s  K& e$ v) }
  4. >>> math.hypot(3,4)0 N2 n/ e; L6 k0 w) @* S1 I4 C/ v9 ?
  5. 5.0
    # y# S# n# \1 k( s4 z- ^
  6. >>> math.hypot(6,8)
    5 @; S6 u8 w8 I+ G* L' k1 t5 e
  7. 10.0
复制代码
9 r) G) p6 H6 N
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False  k( z: v7 @& |/ L" `5 X6 k
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    % {2 f$ y0 y2 D) v/ t
  2. isfinite(x) -> bool: h! g# A+ o$ e" O! r2 v
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ( W0 o6 v+ H2 e# r
  4. >>> math.isfinite(100)/ b: ?# A% l3 Y. b
  5. True( w: Y/ m. s; p, p
  6. >>> math.isfinite(0)
    ) R# }* m5 E2 m4 E* f( |- h
  7. True
    / b7 j, t3 a8 L. O$ C9 v
  8. >>> math.isfinite(0.1), D. A* u8 P" b; ~
  9. True
    . S/ K0 S# O; O0 N# v" t% p4 p
  10. >>> math.isfinite("a")
    & K0 G# l( u" b  c5 n/ x! h$ g
  11. >>> math.isfinite(0.0001)0 E6 R$ {- K8 L* T8 }- f
  12. True
复制代码
( X5 c) `9 W4 }  `- r! E5 r
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False" i4 ^1 S" e) o' q9 ?
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False4 s2 m& ^5 i! t0 Y* \
  2. isinf(x) -> bool
    $ Q1 S2 |2 [( m
  3. Return True if x is a positive or negative infinity, and False otherwise.7 x1 Y; ~3 B6 \) X4 Q& Q1 U8 Q
  4. >>> math.isinf(234)
    3 y% I9 O' E1 Y. K
  5. False  o9 k( p9 ^* f* I
  6. >>> math.isinf(0.1)5 z* _3 J; a& P4 s! i+ Z
  7. False
复制代码

  J0 [* q' x8 ]% F4 X' zmath.isnan(x)  如果x不是数字True,否则返回False
+ d: ]" V: |" N7 e5 n
  1. #如果x不是数字True,否则返回False
    $ e, P& p& t  x. E, W7 t: Y. L5 Y' C
  2. isnan(x) -> bool
    8 [8 C$ p) E; b9 h2 \5 \1 W5 G
  3. Return True if x is a NaN (not a number), and False otherwise.
    5 k: R3 `+ @8 s: s+ S  J/ m  h5 p8 B
  4. >>> math.isnan(23)3 `" G8 I9 X! G  X/ T
  5. False
    2 {/ z8 p; l/ |5 F5 A: G: _. h% z
  6. >>> math.isnan(0.01)
    . A  s. s9 ~. A
  7. False
复制代码

# o9 V. Q: y8 M9 H  nmath.ldexp(x,i)  返回x*(2**i)的值
% N; x' T1 x& U7 f
  1. #返回x*(2**i)的值
    2 F9 Y+ H1 {1 o4 E  g
  2. ldexp(x, i)0 a, v( Q: B: b3 L
  3. Return x * (2**i).
    + q& D# G0 W7 n  G) k3 J8 ?
  4. >>> math.ldexp(5,5)2 X/ U5 u6 w5 V$ G5 F
  5. 160.0$ [# ?; I$ @0 {' ]
  6. >>> math.ldexp(3,5)
    / e: H' g( N3 i4 P  Y( v$ V, y
  7. 96.0
复制代码

- C# b; B! ^* B9 lmath.log10(x)  返回x的以10为底的对数# n( C' c5 c# G" |8 U
  1. #返回x的以10为底的对数
    * x+ i  R+ J7 D" w. q
  2. log10(x)
    $ m" W2 G2 T+ E! }" ^+ M9 T
  3. Return the base 10 logarithm of x.6 E6 |" M- G' p/ Z+ ~
  4. >>> math.log10(10)- u( c% j0 D& y& W: l
  5. 1.0
    1 {6 t$ \1 g& C2 o. Q
  6. >>> math.log10(100)9 z8 ~6 R0 U& r3 K) `
  7. 2.0
    , C& Q0 }% o: U' x
  8. #即10的1.3次方的结果为20. o& o- t( e1 W! T9 u
  9. >>> math.log10(20)
    6 f- R' V0 o9 _! \' d$ F! {6 \
  10. 1.3010299956639813
复制代码
* f3 ~3 `" v1 t5 g# g
math.log1p(x)  返回x+1的自然对数(基数为e)的值
3 h) T) C( `$ ], r/ T
  1. #返回x+1的自然对数(基数为e)的值2 B) N; L  J. Q
  2. log1p(x)0 K; F( n! Y0 ]9 J, H2 Q/ \" U1 v* ?, X
  3. Return the natural logarithm of 1+x (base e).' a# A( N" h$ |  y+ }
  4. The result is computed in a way which is accurate for x near zero.
    * k% ~! W' @6 t; l* R* n) p- A- s
  5. >>> math.log(10)( Y# z, J) q6 J+ N. r
  6. 2.302585092994046, E" p! k; w. Z0 r0 a! O$ W
  7. >>> math.log1p(10), a; U% m/ F% c% H8 _% c) Y
  8. 2.3978952727983707/ C  j( q4 j9 V
  9. >>> math.log(11)
    / |, J' d1 G  z* `. P/ U3 t
  10. 2.3978952727983707
复制代码

  l' {/ u5 p$ s$ o1 S' @; U2 `math.log2(x)  返回x的基2对数6 b# V  h( c9 {7 @9 v5 a
  1. #返回x的基2对数0 [  L/ n0 Q' W/ ?
  2. log2(x)
    % j+ u) R+ l7 i5 r
  3. Return the base 2 logarithm of x.
    1 X# W( D3 s. }6 F$ Z+ Q9 O% L
  4. >>> math.log2(32)
    - l+ T! t1 e+ F7 G% B' |
  5. 5.0
    7 F+ }5 j6 b  i+ t- |6 |, B3 _& `
  6. >>> math.log2(20)
    * T; j, a$ i# q: o& T
  7. 4.321928094887363
    % z+ b7 U# m0 n1 a" C9 [: i$ g7 s
  8. >>> math.log2(16)8 r! p( E1 _7 @: e: R+ T
  9. 4.0
复制代码
- a2 Q7 F: C* y4 [0 h( y3 I
math.modf(x)  返回由x的小数部分和整数部分组成的元组" G' N8 ]! n  H! c
  1. #返回由x的小数部分和整数部分组成的元组
    0 ^+ k4 e( f8 K7 j9 G5 P" g
  2. modf(x)5 S/ Q# F$ g1 G$ f) d8 Z
  3. Return the fractional and integer parts of x.  Both results carry the sign  @7 Q  \% v! D+ n! D8 r
  4. of x and are floats.. y  m9 O% _8 U4 ]. ~
  5. >>> math.modf(math.pi)" [3 a* d% J6 I5 i5 m
  6. (0.14159265358979312, 3.0)( F7 B1 ~. T3 q; Z! P8 T6 x- ^
  7. >>> math.modf(12.34)1 W/ z+ n! s8 ?, j1 Z
  8. (0.33999999999999986, 12.0)
复制代码

" |5 V- U% K3 [. ^math.sqrt(x)  求x的平方根" d4 W/ g  N! o' T( v
  1. #求x的平方根" P& D$ L- \: O( a9 U  J1 j
  2. sqrt(x)8 X4 h' Y- w5 D( [% J
  3. Return the square root of x.! N# J) x1 F4 r+ L2 |
  4. >>> math.sqrt(100)0 C0 S" U* J% _
  5. 10.0
    ! A1 j* {' [: B' o
  6. >>> math.sqrt(16)
    # y& V/ ]* q0 Y" v% C: y
  7. 4.0( k% b8 ^  \8 Y' X$ j, l, Z* `! ?+ T5 V8 c
  8. >>> math.sqrt(20)
    " F$ u4 g9 k( K" t% x  }
  9. 4.47213595499958
复制代码

' \: ~3 Z0 q. _% H) I( Rmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    % N4 R% n; m* s
  2. trunc(x:Real) -> Integral
    - D+ J9 O. B0 L4 S
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ; y- g3 K7 E: V% R5 [" G5 ~
  4. >>> math.trunc(6.789)3 o/ k. D2 w1 B0 U
  5. 6! S" U8 k+ n; d
  6. >>> math.trunc(math.pi)
    ; W4 j5 Q9 h5 z! `
  7. 35 ~& {' {; k+ C
  8. >>> math.trunc(2.567)" v% L, V3 g! p( N
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-24 06:04 , Processed in 0.085654 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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