新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
. g/ E9 K) O9 {' Z" ~. M. G
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# R% X4 N- g8 E0 e6 n2 z- H
* w2 f! t2 [6 Y* _3 }/ q) [1 C
方法1
" x' ]) C3 \, T5 C
  1. >>> import math0 y+ ]% y' \& w+ |, O% B/ j* P
  2. >>> math.sqrt(9)
    # `% x5 M9 ^' ~- `/ @2 J& Z  W: p
  3. 3.0
复制代码
方法2
4 T3 j0 h# U( l) r" w0 r
  1. >>> from math import sqrt! Y! ^: V, F, V9 }  x- x
  2. >>> sqrt(9)  B* u& Y0 R, ?  ^; g$ A1 N
  3. 3.0
复制代码
$ U7 X) m/ C$ c. w) K& d# T. A

! r2 T3 Y. A  S* C
math.e  表示一个常量" @, E2 ~" J. ^
  1. #表示一个常量% r& J' G. ^: ?( X5 ?2 u3 ]& @
  2. >>> math.e8 o2 p! C: M) a9 |5 n
  3. 2.718281828459045
复制代码

) Y; q; {/ w+ l/ {/ |math.pi  
数字常量,圆周率

6 U, c* L* p: h* ~6 T
  1. #数字常量,圆周率1 x; u0 h; c/ V$ ~9 q0 T* P% b
  2. >>> print(math.pi)4 f* I- X& T0 g  r# }
  3. 3.141592653589793
复制代码
5 @7 H- D" V0 ]
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
( ^3 J# @* q4 g/ P; Y
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 z! W+ k' u0 K6 M; r( V" b3 s. ?
  2. ceil(x), r) P5 n0 r. G6 R& X, R" t
  3. Return the ceiling of x as an int.) h4 ~+ ]( [( M3 a% t) h( i  T
  4. This is the smallest integral value >= x.( ~9 l& w3 ~# _
  5. 8 z5 s' t! n' s
  6. >>> math.ceil(4.01)
    0 y: i  ~! v" b! N
  7. 5& }# K3 W3 i* U, A* }/ K7 B
  8. >>> math.ceil(4.99)
    2 n( F9 F( a, L- \
  9. 5
    2 C+ m9 g! I) S
  10. >>> math.ceil(-3.99)
    ! M1 h+ a* d" P
  11. -31 K; Z. i- j' ~
  12. >>> math.ceil(-3.01)! x& F: v; ~6 w6 k$ x
  13. -3
复制代码

8 X4 @9 }4 j! n6 e4 n/ P* l. Lmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
* y7 X: i. I. c& e+ }
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身  ]) s2 f  Y2 n7 g0 E' Y: N( K; \, l
  2. floor(x)1 r! }! {  q" n
  3. Return the floor of x as an int.
    4 s% K& V* }4 [8 u+ j
  4. This is the largest integral value <= x.
    5 \% H0 W/ Z  q5 K* C
  5. >>> math.floor(4.1), x, ~; Z" A; a5 [7 X, g" x. d* c
  6. 4) \! s7 X- [' a' j  o" k
  7. >>> math.floor(4.999)4 G8 ]; l* v4 {, Y( {0 X
  8. 4
    , _5 G1 W  A9 E6 r& G0 f  V
  9. >>> math.floor(-4.999)# e" @% f- e# p' |; j2 ]& m% \
  10. -5
    - P( b* p3 e/ F# {# ^
  11. >>> math.floor(-4.01)* ]$ d* Z! `. P
  12. -5
复制代码
8 `  w4 u0 D: ~' w: e. J
math.pow(x,y)  返回x的y次方,即x**y
6 W! @+ A# I; B: }6 x7 {$ @
  1. #返回x的y次方,即x**y
    . s0 _' t! F5 F& C3 r( e5 X
  2. pow(x, y)' R9 L' Q3 P4 I! A; b0 _; |0 V
  3. Return x**y (x to the power of y).2 a2 M- [7 s. M) z' `
  4. >>> math.pow(3,4)# Z$ T# w/ x! G
  5. 81.05 a, _7 M: V7 G9 g) _$ e
  6. >>>
    4 J* f; N3 M  N* B3 w2 R' r1 A7 x: I
  7. >>> math.pow(2,7)
    : S: x8 x1 `9 U: B8 f
  8. 128.0
复制代码

, ^: B+ F8 _& M- K4 ?- @. {" wmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ F/ D  x, ~6 n7 f0 R
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) b! S! m* u3 [
  2. log(x[, base])
    2 S8 L' Q! J) W. C
  3. Return the logarithm of x to the given base.
    # h! h! c: z0 |. [/ Z- E
  4. If the base not specified, returns the natural logarithm (base e) of x.! \2 u; u" v. I, s1 Y* o2 \$ r
  5. >>> math.log(10)
      }: u& }5 C) N; R7 p/ A& Y- q
  6. 2.302585092994046
    $ ]) C: _& d6 e3 f2 N. c4 j) f- _& e
  7. >>> math.log(11)+ N2 l! v$ k& m8 q3 i. n6 w
  8. 2.3978952727983707
    " L. E0 |8 e+ I( M
  9. >>> math.log(20)$ O+ |: \. S8 A& `, {! P; ]
  10. 2.995732273553991
复制代码
0 K6 O5 q! h6 D1 ~- C/ R
math.sin(x)  求x(x为弧度)的正弦值4 W0 j! c6 f7 D. n: i+ S, _
  1. #求x(x为弧度)的正弦值
    0 Q, Y- j- r/ g) }2 K
  2. sin(x)% S/ t2 F9 d: ~8 p1 k
  3. Return the sine of x (measured in radians).2 O7 w; r% m( ^  Y8 K) @! [/ f
  4. >>> math.sin(math.pi/4)
    : j' t7 y* D# ~0 G, R* C
  5. 0.7071067811865475$ J5 y3 N5 z5 [
  6. >>> math.sin(math.pi/2)0 q9 H3 |3 F: @# a+ Y' T" b( K/ T
  7. 1.0: _/ v/ C2 Y, Q, ^
  8. >>> math.sin(math.pi/3)- C0 n# D) f0 S5 u! D9 U
  9. 0.8660254037844386
复制代码

: C. C" E! O  m; |( n1 bmath.cos(x)  求x的余弦,x必须是弧度
7 \. ]7 h: X+ ]6 K2 j( d, w
  1. #求x的余弦,x必须是弧度
    " R6 I: w1 e+ U" ]& n( B: U
  2. cos(x)
      n; z/ S. @- C4 x8 a3 B
  3. Return the cosine of x (measured in radians).& u1 I# K* O1 ^' d
  4. #math.pi/4表示弧度,转换成角度为45度2 Y* \3 V/ Z2 e- k
  5. >>> math.cos(math.pi/4); D; |  Y: x5 i/ Q& M- F9 o
  6. 0.7071067811865476
    2 C% o9 b: J, C2 M; V
  7. math.pi/3表示弧度,转换成角度为60度5 z% c& J+ d- y+ @
  8. >>> math.cos(math.pi/3)
    8 t) t9 V5 c  W+ V8 T& c0 M, I3 g
  9. 0.5000000000000001
    ) O( [% {" R5 H4 z! P
  10. math.pi/6表示弧度,转换成角度为30度  M, y8 L1 T) {, C6 Q8 k3 M
  11. >>> math.cos(math.pi/6). |) ?7 t6 m7 }8 ^7 |
  12. 0.8660254037844387
复制代码

; b( {1 \" l* F0 b( M5 ~math.tan(x)  返回x(x为弧度)的正切值4 E' U' x0 A* j. h1 `; O/ Z
  1. #返回x(x为弧度)的正切值
    , E- {: ]9 g+ y
  2. tan(x)
    ( k0 c. \& O4 _* q; o+ C4 S; n
  3. Return the tangent of x (measured in radians).1 I% W- _5 {7 ?3 {! n5 m
  4. >>> math.tan(math.pi/4)  Y/ G, S# b) Z& n: S3 r4 }
  5. 0.9999999999999999- R  Y8 y4 B+ ^) S( B1 i
  6. >>> math.tan(math.pi/6)
    # ~$ [1 S4 q( O/ K; y/ A
  7. 0.5773502691896257
    - Y( Q# e6 k4 c+ f: W# H+ v/ ~
  8. >>> math.tan(math.pi/3)4 N' K  m) K1 d) M" m+ k! r
  9. 1.7320508075688767
复制代码
  J( n9 Z- E: _$ B
math.degrees(x)  把x从弧度转换成角度
2 S$ T* }) L3 F& p! R8 C& W% T
  1. #把x从弧度转换成角度
    5 ]0 D, a" M+ G4 @2 X
  2. degrees(x)
    0 R  [! I. P/ s4 J. A0 [8 Y' i+ _
  3. Convert angle x from radians to degrees.
    8 Z" Q7 |9 \. Q3 H9 O5 Z9 \) a
  4. 3 [0 |5 c2 e7 `: \/ K4 i; @5 x
  5. >>> math.degrees(math.pi/4)" F1 `+ g9 H" T8 T  P
  6. 45.0
    ! b" U* H* ?0 w9 f
  7. >>> math.degrees(math.pi)8 Z. S4 ~$ K4 Y* G- q2 `  B
  8. 180.0# P5 i0 ?2 q  T! _
  9. >>> math.degrees(math.pi/6)6 M/ d  r4 ^+ ]4 l0 w1 p
  10. 29.999999999999996
    : M2 U$ S+ y- B) @$ \* e
  11. >>> math.degrees(math.pi/3)# h6 H) y9 H! [3 X) r7 u
  12. 59.99999999999999
复制代码

3 }4 d) l  ]% N% z6 umath.radians(x)  把角度x转换成弧度1 V: y* X: T# M. f
  1. #把角度x转换成弧度7 I: ^& q  z* V6 H; y7 z# l4 S
  2. radians(x)
    5 F, q% e0 C7 Z' h$ P  E. k' C8 ^) e  z
  3. Convert angle x from degrees to radians.
    8 N2 `+ V7 J  @9 U& t. F
  4. >>> math.radians(45)' x% h2 P( \3 f0 y6 \6 b8 D; z/ u
  5. 0.7853981633974483& e* W9 Y7 {( {8 U6 M' A, P) [
  6. >>> math.radians(60)' B# _2 M( z; O# _! X  b4 o
  7. 1.0471975511965976
复制代码
( F! A, k( E$ U) t! |
math.copysign(x,y)  把y的正负号加到x前面,可以使用0: o4 B1 g1 {& X* E! r* e6 f
  1. #把y的正负号加到x前面,可以使用0
    : f& d0 x* H6 e( p# Q' O7 a
  2. copysign(x, y)# k! F, P% u) a# y* z' N& e" \
  3. Return a float with the magnitude (absolute value) of x but the sign 0 i& g. ^* i+ }! z. e% G* U( Z7 I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    " g4 }% Z9 l4 q, W. j7 M
  5. returns -1.0.
    4 u" T4 _7 B( L) n8 [! N
  6. ( i! G( P6 Z2 l( v+ Y; s
  7. >>> math.copysign(2,3)2 E; ~- C9 k/ Y& `  g. |' w' P
  8. 2.0
    # e2 k0 T) E5 u8 y* [4 c2 w
  9. >>> math.copysign(2,-3)# ]& [5 z1 Y/ @$ R5 S$ l
  10. -2.0
    " ?8 g: S- G8 s, T
  11. >>> math.copysign(3,8)! k$ U! {; q9 _, K' E& u
  12. 3.03 F5 c4 `. e" C& @) R& O( L
  13. >>> math.copysign(3,-8)) w: P% Q& w% t1 G
  14. -3.0
复制代码
/ ^# s& ?! ?! n# u: k; ^- N; C
math.exp(x)  返回math.e,也就是2.71828的x次方
8 b) f. Q- n% c( s7 Q
  1. #返回math.e,也就是2.71828的x次方( l; T4 v: h! L) ^
  2. exp(x)8 N" T/ Y  u1 X+ p+ G: c
  3. Return e raised to the power of x.
    5 i2 V  h  S/ r* K
  4. 5 A# t$ ~0 ~: b2 t8 A
  5. >>> math.exp(1); I' |- p9 E  R1 u/ I/ v
  6. 2.7182818284590452 g4 w( W1 H% C4 N
  7. >>> math.exp(2)
    . F( x4 y8 E( p! D& j9 L( m" S# @
  8. 7.38905609893065" q+ d3 n' _) z2 Q
  9. >>> math.exp(3)% |; `) N4 y) p+ u
  10. 20.085536923187668
复制代码
, e! J5 w: |! w. n
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1* _$ Q4 t9 B% U9 ~9 }/ W
  1. #返回math.e的x(其值为2.71828)次方的值减18 N$ v4 ?9 P' q! M" K" n
  2. expm1(x)7 Q' N! B, H1 [6 }2 h' X  O
  3. Return exp(x)-1.$ V6 C% X( O+ t
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x." V" I7 n3 b' a3 s7 @% E, M

  5. ; U5 s: n- P- O0 ^4 \2 L9 O
  6. >>> math.expm1(1)$ [/ a& }# g: {6 H: r8 x! F% [
  7. 1.718281828459045+ A! a3 T$ Z% k. L9 M
  8. >>> math.expm1(2)- O' ~, R+ R$ K
  9. 6.38905609893065
    % w* c( ^/ x. `% a9 b0 v! G# T
  10. >>> math.expm1(3)+ y, u, H! ^+ C' a1 m6 _" C
  11. 19.085536923187668
复制代码
3 e7 A. x! `" R' c- s0 Z- C7 V
math.fabs(x)  返回x的绝对值9 M; ^" J) `& q- o* @7 s% ~4 G
  1. #返回x的绝对值; S3 `! C* i7 _% A" I0 ^: L' u
  2. fabs(x)
    $ G) n! A, O% B- ?0 C& a9 H
  3. Return the absolute value of the float x.. X; N# |- J) w2 s& Z- L
  4. ' }# S( B4 Q1 }% }7 Z1 V
  5. >>> math.fabs(-0.003)
    & J' F  F0 R  M
  6. 0.003
    8 N: b4 D$ H9 s$ c: ~
  7. >>> math.fabs(-110)% ^; ?0 K0 ?; c( n
  8. 110.06 |$ J: u5 I7 L/ j3 ^( r1 g2 a
  9. >>> math.fabs(100)* e# j2 O; a4 [+ v: ~1 F
  10. 100.0
复制代码
" _4 p, s: o" f' a4 P
math.factorial(x)  取x的阶乘的值
/ E: l$ Y$ |# M
  1. #取x的阶乘的值0 i- Y+ o! m4 T
  2. factorial(x) -> Integral) l( A3 R& F  e( d+ @3 Q- V, d
  3. Find x!. Raise a ValueError if x is negative or non-integral.- a0 n; D- Q4 V5 J
  4. >>> math.factorial(1)' r" V, V5 @; y, @6 S0 t
  5. 1& O6 o2 V* u: H. c# E% H
  6. >>> math.factorial(2)4 J/ K9 O/ L2 }$ y* G1 H
  7. 23 S+ M0 }" e4 d% P7 w: x2 b
  8. >>> math.factorial(3)3 q+ ?+ `* u# y- q4 U
  9. 6( T/ q1 A3 Q+ c4 ~5 k- f
  10. >>> math.factorial(5)
    / h4 K! i. U) A
  11. 120* S; g- j4 b+ N' |$ S
  12. >>> math.factorial(10)
    4 v# H# v8 w. A# f! e- {9 R
  13. 3628800
复制代码

* H4 r$ o2 J7 m1 z5 A1 ]math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
6 U, `3 x6 v* Q" r  c' z  \
  1. #得到x/y的余数,其值是一个浮点数, d7 ~  n1 v. u( }" C$ y
  2. fmod(x, y)/ R/ p7 M+ e4 j9 Y
  3. Return fmod(x, y), according to platform C.  x % y may differ.5 y+ a$ J0 F, J; _! b
  4. >>> math.fmod(20,3)
    8 {/ k' D& n8 q
  5. 2.0# x: b! |* P) h7 C3 h( N0 X: P' S
  6. >>> math.fmod(20,7)
    6 N0 F7 l3 o  J" z( r
  7. 6.0
复制代码

, m( C8 ^. i+ hmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; m8 K8 B$ I! m% b# E
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,' |4 h6 M9 o  L
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
      O) o/ {9 X6 o' W) @
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1, o, Q* N5 [: L# x. }2 ?
  4. frexp(x)4 C9 b- J6 w: O( ]- i# v9 R
  5. Return the mantissa and exponent of x, as pair (m, e).3 C6 C5 A* ?! q  L/ k* E8 X
  6. m is a float and e is an int, such that x = m * 2.**e.
    + a" @4 ]8 R% N. H  A0 a# S- p: k
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 b4 Q7 l( a6 D& D# N& e5 [' ]
  8. >>> math.frexp(10)( M9 d! F7 R& ], D- |. E
  9. (0.625, 4)0 p/ L) u! |! U. h/ W6 x
  10. >>> math.frexp(75)1 [* k6 Y* r7 c: _" F8 \
  11. (0.5859375, 7)
    : I! L6 R' l+ n
  12. >>> math.frexp(-40)3 z( z$ Z( V7 z3 X$ c% j' S
  13. (-0.625, 6)
    ! E5 b. g2 ^1 f' |4 U, h
  14. >>> math.frexp(-100)! g/ m. O' f4 E  k& ?
  15. (-0.78125, 7)4 k' \" k5 I; m! U; v5 E, }4 w
  16. >>> math.frexp(100)9 v  K" f! X2 [- b$ t
  17. (0.78125, 7)
复制代码
, S: ?. x. g. y2 V( `! q
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列' X7 {. k' |* F3 ~
  1. #对迭代器里的每个元素进行求和操作5 m8 c3 B) o0 C" L0 q
  2. fsum(iterable)
    0 C# \; r. b: ^' X7 u
  3. Return an accurate floating point sum of values in the iterable.
    ) H$ T) b0 t0 Q( H& l
  4. Assumes IEEE-754 floating point arithmetic.6 R  b3 N! |  K. f% [
  5. >>> math.fsum([1,2,3,4])8 F5 B" K, k3 k7 w! g+ L
  6. 10.0
    # x( {( x  @7 b. Z; g! Y
  7. >>> math.fsum((1,2,3,4))1 I1 g# Y6 ~0 l* z4 G- U4 z
  8. 10.0
    $ B6 U0 ^# e9 P* u5 b
  9. >>> math.fsum((-1,-2,-3,-4))
    . T2 I7 h2 x. e1 k
  10. -10.0
    1 r# D3 q+ r3 l  s  P' Q
  11. >>> math.fsum([-1,-2,-3,-4])
    / _% R& Z9 ], y0 k: ?
  12. -10.0
复制代码

8 g4 \; H' I) v  e* G4 [/ Amath.gcd(x,y)  返回x和y的最大公约数
/ s9 ?( i8 m: ?
  1. #返回x和y的最大公约数
    ; |, G( k/ m5 Q7 w' `
  2. gcd(x, y) -> int; n1 _: i6 L5 P; E* T+ h
  3. greatest common divisor of x and y5 Y) `0 B$ A, c. ^! |, u
  4. >>> math.gcd(8,6)
    6 q! J% ^* B6 e- G. p$ V
  5. 2# L6 K! O7 _* D7 b/ |( M
  6. >>> math.gcd(40,20)' {) A1 m! S) j1 Y" P4 @+ m
  7. 20
      z1 k" |& l8 E( G$ b
  8. >>> math.gcd(8,12)0 c) p  o3 S3 A' a  d& n
  9. 4
复制代码
9 ~2 L* P: o; h1 A! A
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False( H: E% q# m% w
  1. #得到(x**2+y**2),平方的值
    1 n% N3 P$ O  r- M% P' ~
  2. hypot(x, y)
    ; }7 ^! e7 z' S( t, D
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 T7 H& `) v' D8 G: D# Z' Y8 @/ }7 |
  4. >>> math.hypot(3,4)
    - T# o6 o' p/ s! ]& h
  5. 5.0. n" A4 v& c5 L" Q8 W
  6. >>> math.hypot(6,8): j) l; |% k0 m
  7. 10.0
复制代码
, x- }. l# t) \6 J
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False1 ^; p: J0 V; J7 [
  1. #如果x是不是无穷大的数字,则返回True,否则返回False2 {6 Y  c$ \" s3 `/ c- S
  2. isfinite(x) -> bool7 T9 M- {3 T0 E8 j0 U) c
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.2 m, \. f1 }+ }. l' `2 D2 I
  4. >>> math.isfinite(100)% E* ~9 }- o& v; k* S* R1 W
  5. True+ T; H6 r7 B/ H/ a
  6. >>> math.isfinite(0)
    ) a9 D$ _5 U' z8 Y: }
  7. True
    7 X  J* F8 N% e, W
  8. >>> math.isfinite(0.1)3 R. A2 l" c2 y+ o# U0 h
  9. True
    ! l  Q( l" P' [% l9 J
  10. >>> math.isfinite("a")* s2 A' T7 p+ D1 P1 }
  11. >>> math.isfinite(0.0001)
    * o) y: k% y3 ^% D' |
  12. True
复制代码

7 D2 O: J# n( ?& Mmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False! j+ ]- `' q3 B( U: a
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False) Z- ~' h2 a8 P9 S4 K3 z" `) @7 M: l
  2. isinf(x) -> bool
    8 a* C/ i: `  `
  3. Return True if x is a positive or negative infinity, and False otherwise.
    + I* Q* S8 ~& k" W1 j6 ^
  4. >>> math.isinf(234)
    # x- |) g2 O( D8 x& n  S
  5. False% F' W& l6 N& J" W5 S% E+ S% y! D
  6. >>> math.isinf(0.1)
    $ q# U* K9 ]: Q% O" Q) H
  7. False
复制代码
6 f' l# I7 e5 A  Q/ q, \
math.isnan(x)  如果x不是数字True,否则返回False
' c5 Q0 {; S* H7 t1 D
  1. #如果x不是数字True,否则返回False
    * t! L9 G: v% J) M  h9 P
  2. isnan(x) -> bool! L) h) t+ I$ y- \6 r2 v3 Z, t
  3. Return True if x is a NaN (not a number), and False otherwise.
    ( t2 H/ l% u* W# R; q7 S
  4. >>> math.isnan(23)- J; j4 \. h5 @- Y1 x" D% i0 P
  5. False
    4 Z, ]- y' _/ e/ d5 S. Y
  6. >>> math.isnan(0.01). i1 {4 J" d: [6 _" a
  7. False
复制代码
! T6 ], t' d1 D2 d; b
math.ldexp(x,i)  返回x*(2**i)的值! `5 D3 N# L- T% _) O
  1. #返回x*(2**i)的值
    0 d# b9 G. Z3 {0 H0 H
  2. ldexp(x, i)
    3 _' R" G' H! B% \
  3. Return x * (2**i).
    7 P( Q- x9 A' m5 j9 ^; J- r" [+ A. E
  4. >>> math.ldexp(5,5)
    & R& x% q0 a: l( E9 G, i- I. V: u  _1 N
  5. 160.01 S2 ~9 m1 V- o2 T
  6. >>> math.ldexp(3,5): F0 {' X" G- z; D$ ?
  7. 96.0
复制代码
* W/ ^. w: h/ w' s7 T' ]& G1 U
math.log10(x)  返回x的以10为底的对数
% |+ F, E& t. Y0 N3 E4 Y. z
  1. #返回x的以10为底的对数
    : A1 Q+ h' Q+ C" n( Y
  2. log10(x)
    ! p7 j4 e( J) a2 \6 }
  3. Return the base 10 logarithm of x.
    # L# g4 m3 u9 }5 n, D
  4. >>> math.log10(10)5 B0 B: T' O1 s; \! J$ f) C
  5. 1.08 K; [' U' X1 J0 H- J
  6. >>> math.log10(100)
    3 O0 {5 }; e- F0 W  ^# S9 I
  7. 2.0
    ) n! h: n. m2 M9 s' R5 |3 i" p  J
  8. #即10的1.3次方的结果为20- ?* ^2 _% q# t8 a2 ~! s% G
  9. >>> math.log10(20)" H. e! q: I7 u: G
  10. 1.3010299956639813
复制代码
! t/ L' f% ?3 w9 T3 e
math.log1p(x)  返回x+1的自然对数(基数为e)的值
5 g% N3 o* O( y
  1. #返回x+1的自然对数(基数为e)的值
    6 d! U6 V* F& ~  C
  2. log1p(x)0 U! D% S, z+ Z% h( |; U2 N! s  @5 ]
  3. Return the natural logarithm of 1+x (base e).1 k/ V. W, d) `0 O3 n7 |0 {  |7 m
  4. The result is computed in a way which is accurate for x near zero.6 }$ j! R# d: {; |: a
  5. >>> math.log(10)
    # S' D7 `7 u% p# k, R# h; Z
  6. 2.302585092994046
    0 k5 l' ]/ o/ ?7 {6 s* S) r
  7. >>> math.log1p(10)) Y1 I: Q; ~9 B) l& |% x
  8. 2.3978952727983707
    2 {/ Q, K6 o  p: G- y
  9. >>> math.log(11)" Z5 L# I7 ^) ~8 n5 T6 F
  10. 2.3978952727983707
复制代码
+ @3 e- U! S+ o* }5 B( n
math.log2(x)  返回x的基2对数# i7 A/ o6 @2 x( V( D
  1. #返回x的基2对数
    ) X' g* B9 d7 W4 o5 z2 ]: P" f6 l
  2. log2(x); V2 x3 m- ^, q) c( g1 j7 X
  3. Return the base 2 logarithm of x." u6 K* c8 L4 M$ |3 }! A9 W# W. b
  4. >>> math.log2(32)
    4 V$ l# U' X  ]
  5. 5.0+ K) d2 C3 M4 }  g9 Z2 R
  6. >>> math.log2(20)) C# p# T( d0 ^8 X0 d$ {% C' i
  7. 4.321928094887363
    1 h$ A( k$ L/ h  u+ c; P
  8. >>> math.log2(16)4 G2 B- D) R$ m, _/ f9 U# a
  9. 4.0
复制代码

# S& J0 w" Z% W) r; K; T5 ?5 fmath.modf(x)  返回由x的小数部分和整数部分组成的元组
+ \& y. O8 f8 p
  1. #返回由x的小数部分和整数部分组成的元组: x5 X( S, m' k$ l, S- }8 g, F
  2. modf(x)
    ! a  x* c, \- Z) }% H1 `% e! e6 T
  3. Return the fractional and integer parts of x.  Both results carry the sign
    & b3 l+ O+ _- J
  4. of x and are floats.9 T! T1 h1 ?& X
  5. >>> math.modf(math.pi)2 s9 M- n2 \/ t* Y' L
  6. (0.14159265358979312, 3.0)+ A+ r% ]6 T) P. R
  7. >>> math.modf(12.34)
    + H& _! l, z: g. {: G
  8. (0.33999999999999986, 12.0)
复制代码

+ V/ R8 o4 q+ }/ w9 Bmath.sqrt(x)  求x的平方根
  m+ \* X/ W* L% o( f, ?
  1. #求x的平方根
    " `, w, z9 G8 d5 f: b- g* J
  2. sqrt(x)$ l/ o$ b0 |7 a1 i( v) m
  3. Return the square root of x.: u# \( O# h" Z, |' s; Q4 s
  4. >>> math.sqrt(100)
    ! i1 o- V  M( h; E' R
  5. 10.0
    + C5 F# o7 ]4 F& B8 w: j
  6. >>> math.sqrt(16)
    # o$ ]) V. m3 c
  7. 4.0& ]5 f3 G% p/ u7 Z* ~
  8. >>> math.sqrt(20)
    $ W7 N1 L4 ?8 _0 g2 g9 u
  9. 4.47213595499958
复制代码

. T! g! x% A( P+ ^" Emath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分5 ^' t0 w0 u* b$ c+ r3 }* q
  2. trunc(x:Real) -> Integral! R' w+ C9 w2 T, Y
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 M- `) X" y; I! O
  4. >>> math.trunc(6.789)
    ; l' }: |  m; d" _+ l6 k2 E. i
  5. 6% ?1 R2 d5 @3 ~6 U8 j: Q
  6. >>> math.trunc(math.pi)
    & G- P" w! [  H! \
  7. 3: C: `: x! N' u- V
  8. >>> math.trunc(2.567)6 ~* g0 k$ O( n0 e8 @3 ~
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-14 10:36 , Processed in 0.091089 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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