新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
/ b" N! j7 g, i3 R& S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
, Q$ `5 ]9 m  ~4 T

# M4 K( F. ]. t$ U- q6 T/ T5 d, Y% \方法1
7 H; O) h( N; M
  1. >>> import math
    0 K1 R+ Q/ |" C9 N( U1 e
  2. >>> math.sqrt(9)# t) o0 V  A- m2 ~0 \  a7 m
  3. 3.0
复制代码
方法2% }" D- l; s4 E
  1. >>> from math import sqrt, T0 e- S* f. U) r5 j8 K
  2. >>> sqrt(9)
    2 ?& s$ A$ m  P7 R7 ]0 `
  3. 3.0
复制代码
1 E3 A9 k5 v& U2 D

, Q. e( l( u6 T' a, \& S
math.e  表示一个常量  ?4 V  n  ~0 C0 D- Z0 n
  1. #表示一个常量9 T, C* e; ~: ?( }2 w
  2. >>> math.e
    7 k4 f0 c5 z) e9 B% f: n/ x8 J
  3. 2.718281828459045
复制代码

6 b) @/ {$ b* w& l2 qmath.pi  
数字常量,圆周率
- X0 {' V6 V. s, N# M3 h; r
  1. #数字常量,圆周率
    5 V; ?" m, l$ @/ t1 Y$ E: N
  2. >>> print(math.pi)
    " s1 F) a3 b# _5 }2 ?* j: [
  3. 3.141592653589793
复制代码
- ~9 U. V% m4 I1 |4 C0 ]- \9 ]
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
4 y9 O$ c- ]8 d! V$ d6 s
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x% U# x9 K: N$ m5 b7 x( O/ I, c. Q
  2. ceil(x)3 X1 _' y0 [7 _& g' }7 R/ Q5 V7 @5 X
  3. Return the ceiling of x as an int.
    5 H8 M% ^! e" K8 Z
  4. This is the smallest integral value >= x.& h: n( P- i1 c4 a4 R3 j$ h

  5. 4 Z7 M. y9 i2 N) C7 R
  6. >>> math.ceil(4.01)7 j7 j9 l$ x) \$ G$ q( Y# J% Q
  7. 5  C. S9 Z2 H( `/ E% T7 e6 |
  8. >>> math.ceil(4.99)* Y+ B, `3 k0 p$ i1 ]. v
  9. 5
    / C* ]9 `0 @4 }- v) x+ I
  10. >>> math.ceil(-3.99)+ D& ?1 d0 }6 z) W- G; s
  11. -3% I2 \2 A6 ^) D, e: w3 E$ `5 p
  12. >>> math.ceil(-3.01)
    ) k. [& J$ n! K( k
  13. -3
复制代码

( r- b3 w( V9 Q& b  Hmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: d) j8 E$ F4 g' B( R& c2 P
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) l. _) r# q/ s
  2. floor(x)
    , ?; c! I$ [4 S# a3 _- n# S
  3. Return the floor of x as an int." e3 p* ?: G- w* h" }1 W6 w0 }
  4. This is the largest integral value <= x.& K: r' {# I$ K  W- J
  5. >>> math.floor(4.1)- n( U' x% [2 m# h, v1 K6 j; R# v
  6. 4
    " Y! t6 C% P( R$ r) X8 h" R
  7. >>> math.floor(4.999)
    0 G1 E0 {$ [; G& l: N/ ^2 M
  8. 4
    ) t$ L- G) x. W7 l8 L! D
  9. >>> math.floor(-4.999)
    % K8 a* T% d  }0 [6 c9 A
  10. -5
    6 N. K8 \. o" h( w) a$ |/ ^0 s
  11. >>> math.floor(-4.01)
    ) k( y- B( C+ ]6 e
  12. -5
复制代码

5 o2 d$ x4 U! d0 h; g/ _math.pow(x,y)  返回x的y次方,即x**y
: H9 U& \# R5 a, h
  1. #返回x的y次方,即x**y. u) y9 x8 d6 Q3 y# ]
  2. pow(x, y)
    # ]) l* y- i1 m& R
  3. Return x**y (x to the power of y).
    " `" Q% X3 R* Z9 K
  4. >>> math.pow(3,4)
    1 g- S% x" e6 g9 s
  5. 81.0
    ( C! J7 v2 m) U5 l" Y  ~& r. m
  6. >>> % p$ p  x/ ?+ [. o
  7. >>> math.pow(2,7)
      X: |' @- B. c. R- |5 t
  8. 128.0
复制代码
2 e# p- Y5 c  l
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" N9 r7 A% d( @8 j' J
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# e! s& W5 w% I7 P) L8 x7 V
  2. log(x[, base])
    % y$ Q  a. O" V3 E/ o7 L% X' f8 Q
  3. Return the logarithm of x to the given base.
    $ }$ v% U. L5 H- v
  4. If the base not specified, returns the natural logarithm (base e) of x.: n. c5 q, y5 u4 O
  5. >>> math.log(10)$ M/ _9 h& c) s7 F) Z0 X( J  Z- O
  6. 2.3025850929940465 |) O' o+ p' h$ d/ g5 y
  7. >>> math.log(11)
    ' r; b& o. L4 s' j
  8. 2.3978952727983707
    3 m9 T. t8 m% Q
  9. >>> math.log(20)
    * Q! b9 C8 z* [( F: l
  10. 2.995732273553991
复制代码

/ I  F$ M9 V: f! x/ h* dmath.sin(x)  求x(x为弧度)的正弦值8 `- K5 O* ^) U: s" X7 r5 E8 s, [
  1. #求x(x为弧度)的正弦值3 @9 q* h5 g3 j; L
  2. sin(x)5 R& w1 ?8 H. H; G; z, F* l$ ^/ t* w
  3. Return the sine of x (measured in radians).
    $ ^4 D" Z8 e! B( R) o* ?
  4. >>> math.sin(math.pi/4)
    ( o( x% l1 R8 Y6 B
  5. 0.7071067811865475- f1 w2 W4 X$ X0 ]6 o% C7 H
  6. >>> math.sin(math.pi/2)
    3 M, V& O( h+ M5 f: k3 I
  7. 1.0# z. L5 q2 C+ b; b
  8. >>> math.sin(math.pi/3)
    7 M( b  z" d' d4 y: N2 o. C
  9. 0.8660254037844386
复制代码

; E9 b: g8 F; z' Hmath.cos(x)  求x的余弦,x必须是弧度8 i0 E& M1 ]5 N$ S: y6 u# x) T9 y8 f
  1. #求x的余弦,x必须是弧度
    / d1 e# ?3 r' v  n, r
  2. cos(x)
    0 D# P2 {9 [7 |: y0 b
  3. Return the cosine of x (measured in radians).
    * z3 H. j. K$ l# E4 Y& s& V
  4. #math.pi/4表示弧度,转换成角度为45度
    ( z0 g. U, \- b9 D" F9 `
  5. >>> math.cos(math.pi/4)
    $ O4 x. V  Q/ l  c& u4 s
  6. 0.70710678118654760 a) y1 [. h7 i. d* j
  7. math.pi/3表示弧度,转换成角度为60度
    / I7 V4 ^; m6 a( t2 Z" s
  8. >>> math.cos(math.pi/3)
    $ k. U; P' i9 [+ e7 [! p
  9. 0.5000000000000001! h9 m( j/ K3 _; C
  10. math.pi/6表示弧度,转换成角度为30度
    * n6 {, Q  T; T: a* j- u
  11. >>> math.cos(math.pi/6)
    - Y( }$ u- k! Y
  12. 0.8660254037844387
复制代码

. X) C1 Q7 s% r6 `math.tan(x)  返回x(x为弧度)的正切值
0 C/ }% Y2 w* G. S0 J
  1. #返回x(x为弧度)的正切值9 }# h# }* T) f5 c. E
  2. tan(x)8 |: R( B% n- L
  3. Return the tangent of x (measured in radians).
    4 z- {8 [/ o- _
  4. >>> math.tan(math.pi/4)
    1 g/ h( b. _! D$ B, Q9 g8 `5 q* J* E
  5. 0.9999999999999999- q! f+ s3 Y+ M, {  U- ~
  6. >>> math.tan(math.pi/6)
    $ I$ `( n9 u' U) J* T2 h; X
  7. 0.5773502691896257
    4 k% b$ c. R: W$ r
  8. >>> math.tan(math.pi/3)8 i& `- L, o; U: }) }+ d+ A
  9. 1.7320508075688767
复制代码

4 y, f4 h5 M) w9 ]6 h& Pmath.degrees(x)  把x从弧度转换成角度
. o, C2 R) u5 D; ]& Z# G  ]
  1. #把x从弧度转换成角度
    % O; {% _% J7 q2 a+ h5 a1 y( i: k3 h
  2. degrees(x)& h. o2 @5 H/ s6 E' p4 R
  3. Convert angle x from radians to degrees.
    - P+ f+ F9 m3 G( w6 @3 d5 y
  4. , N& z9 O6 i" G! Z
  5. >>> math.degrees(math.pi/4)
    & `6 S$ ^* W. G4 h
  6. 45.0
    ) h; m: C8 _0 S* ?+ W. E
  7. >>> math.degrees(math.pi)' x, u+ E: G0 K$ b8 x3 ~5 `/ O
  8. 180.0
    6 |3 ~- J2 G% v4 \
  9. >>> math.degrees(math.pi/6)
    # u' s2 l5 l1 N2 G4 {
  10. 29.999999999999996! x. {* `9 `& T1 a
  11. >>> math.degrees(math.pi/3)( U( `2 B8 o3 P2 b
  12. 59.99999999999999
复制代码

- o4 J# }2 B  T3 Y* i0 q* H7 K# Ymath.radians(x)  把角度x转换成弧度, `- s, X1 t3 z$ z5 R
  1. #把角度x转换成弧度
    2 @& l! C1 x: ]  E# V
  2. radians(x)
    , a2 Y+ d) ]' B" a+ Z
  3. Convert angle x from degrees to radians.' v- a) K# u" X; ]
  4. >>> math.radians(45): X  B* K( l% a5 q1 J8 R+ J& U/ C5 h0 e
  5. 0.7853981633974483
    + c5 i+ V" I" ?7 ^' r  `
  6. >>> math.radians(60)
    / Q6 o7 j. t" D3 ]
  7. 1.0471975511965976
复制代码

" f" g4 |: U/ b% R: Q' u0 Mmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
9 g: |! H9 g, P, a
  1. #把y的正负号加到x前面,可以使用0
    ' _  a, k7 c4 L4 K4 s8 t& B
  2. copysign(x, y). e, k% |5 u, C1 o
  3. Return a float with the magnitude (absolute value) of x but the sign
    0 y2 ]$ t% y% f, l
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    8 |% u  J1 \& \3 e; k
  5. returns -1.0.
    ' r" o! ^' o2 u: t1 m: o7 F1 T7 ?

  6. ! g# a* h* T: {" [1 O1 S' C# ]
  7. >>> math.copysign(2,3)# Q6 t9 Z; Y+ \
  8. 2.0- E" I8 ^7 `3 q5 S9 x7 d
  9. >>> math.copysign(2,-3)) r1 P: J7 k% C+ j4 o* b
  10. -2.00 Q' w) Y# H4 r1 |6 y
  11. >>> math.copysign(3,8)' J. I( P, S' W* f, Q/ Z0 \
  12. 3.0$ I$ F, t8 A8 n% x$ a) d2 A
  13. >>> math.copysign(3,-8), }5 `1 X- i) _
  14. -3.0
复制代码

7 {' O% D' K9 U& A2 g( U6 pmath.exp(x)  返回math.e,也就是2.71828的x次方9 M5 D/ @5 ~9 e, Q1 M1 F+ k2 l
  1. #返回math.e,也就是2.71828的x次方0 A8 c" B2 Q+ V! T# U
  2. exp(x)
    3 i0 K; t# w5 |6 l
  3. Return e raised to the power of x.; X9 z3 [. M0 ~- i- }

  4. ! O* F& W! N9 w9 a
  5. >>> math.exp(1); v2 @9 c; y8 z
  6. 2.718281828459045
    0 E) E5 x% {- L5 d$ X3 ~2 l! ?
  7. >>> math.exp(2)
    4 U" z& k3 ~( v
  8. 7.38905609893065
    3 [0 j/ E6 |% x( a4 E
  9. >>> math.exp(3)
    1 _9 ^' E& k1 Y
  10. 20.085536923187668
复制代码
& Z# L$ ]8 j% ?7 b- Z$ [2 _
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
' |+ F1 x2 P* v8 r7 Y2 j  Y" O& \
  1. #返回math.e的x(其值为2.71828)次方的值减1% A* ^5 l* X. c5 I/ M
  2. expm1(x)
    8 N# n( U) ?/ a: X5 k7 z/ I
  3. Return exp(x)-1.; y: o8 V; L, j6 v  Z0 R% q
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ b+ P. b5 l0 ^+ F

  5. , d. F( N5 o' S0 G$ o$ `
  6. >>> math.expm1(1)
    , Q& ?" b( w* i" @* u  _/ [8 E; c6 l2 Z/ Z
  7. 1.718281828459045- ]+ I" V2 j. N
  8. >>> math.expm1(2)+ ~; C/ Y- m; B( k( ?  G
  9. 6.38905609893065
    8 ~9 N: c+ T9 O* ~' v! {
  10. >>> math.expm1(3)
    % D/ K0 I" e) M, |( ?6 G! r
  11. 19.085536923187668
复制代码
' m* D3 A6 Z4 U$ s. W8 k
math.fabs(x)  返回x的绝对值1 M% d6 o; Z+ G! @/ O7 o! F$ l8 p8 t
  1. #返回x的绝对值% I2 b( o: e0 J) k- ]- _/ u
  2. fabs(x)! _3 g0 I& q* |  j- ?3 n5 z
  3. Return the absolute value of the float x.! j- G) u% x. a2 `/ V+ z

  4.   P6 }5 p  ]" }9 M; Z/ u
  5. >>> math.fabs(-0.003)
    3 [" c* x* y8 C2 l9 P
  6. 0.003
    9 I+ U. s: M2 T: C
  7. >>> math.fabs(-110)
    . g: _5 x0 e! X9 N' W& n# m3 b" B+ u
  8. 110.0
    7 S/ R9 v5 i# F6 f& r: R0 W$ G
  9. >>> math.fabs(100)
      F; E* B5 |* w7 i
  10. 100.0
复制代码
5 A7 I: p; i9 D5 M# \
math.factorial(x)  取x的阶乘的值
) r  G) H4 y+ \& t5 w
  1. #取x的阶乘的值- Z  {. D7 N. S1 j% M0 V& {2 B# A' [
  2. factorial(x) -> Integral( g  t! [) `2 E, [" l9 p8 c
  3. Find x!. Raise a ValueError if x is negative or non-integral.$ w5 m" ?( y) Y  o, X$ i
  4. >>> math.factorial(1)
    # _$ I, k& y# b! {' w
  5. 11 H6 v% X- u* X0 q8 i
  6. >>> math.factorial(2)$ R2 u+ y; \% i2 f. w
  7. 26 j  _6 `" _2 G3 T+ q4 h
  8. >>> math.factorial(3)
    6 \7 r# }$ n1 H2 r
  9. 6: @9 w  Z0 ~) ?4 P
  10. >>> math.factorial(5)
    ; X8 `5 Y" }7 A! P
  11. 120& D/ |* n  R; t" ^( h0 `: `& `
  12. >>> math.factorial(10)
    % ~8 o7 u6 f+ I) h) G+ B  w
  13. 3628800
复制代码
4 o6 q9 O/ \+ y! D
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
% o; [7 b6 N  P2 F/ W9 S: @
  1. #得到x/y的余数,其值是一个浮点数
    - {7 W0 q4 }: x( l" f) D9 m4 o
  2. fmod(x, y)- m$ _2 _. O* F6 a
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    . j' M& r; M4 S2 _7 |& z
  4. >>> math.fmod(20,3)
    7 p# b2 w( z, I' i5 b$ r. l4 R
  5. 2.03 m1 B, P; F" l1 T' m4 y0 ]
  6. >>> math.fmod(20,7)
    6 i% c9 W9 K0 n& @
  7. 6.0
复制代码
/ t  s$ T2 a7 X( {$ x
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
( N( L: F- t% X4 f8 `6 i' w
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,% |0 N+ _7 C# o( v5 j
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    : Z" Q) d% l; A5 `
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 c* [& t# K4 m: i! J
  4. frexp(x)5 v  V: v( Q# v- _
  5. Return the mantissa and exponent of x, as pair (m, e).
    " l- O$ a* l0 E8 e
  6. m is a float and e is an int, such that x = m * 2.**e.0 Q4 O! c- g! _5 ?) b% W2 L1 r9 J
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    % j% L( n2 A0 L. w0 h
  8. >>> math.frexp(10)' u5 j4 d. `+ z  h% Z% F
  9. (0.625, 4)
    - `0 i& I) u& M: o: L0 B) L
  10. >>> math.frexp(75)
      a' }: F" p' G! U* }
  11. (0.5859375, 7)" M. m. e+ \1 J# Z: u; b9 U8 N3 v
  12. >>> math.frexp(-40)
    , v& M) z: \9 ]! Z% Z! C' e4 G1 S
  13. (-0.625, 6)
    % |/ L: [8 W- p& C% c& E5 h
  14. >>> math.frexp(-100): m$ W0 |  y7 c) N
  15. (-0.78125, 7)* g' I6 _+ H3 S# l$ ^4 Q! |( q9 R7 A
  16. >>> math.frexp(100)6 E' r) C! g) A5 B! i
  17. (0.78125, 7)
复制代码
: a% b' f; @- D1 `
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列( I) x( p- b- \* M
  1. #对迭代器里的每个元素进行求和操作6 ~" q1 Y6 e& U( x4 O" h
  2. fsum(iterable)
    - i/ _) l8 ~; _( \
  3. Return an accurate floating point sum of values in the iterable.
    & k- ~2 H7 Y  X: N
  4. Assumes IEEE-754 floating point arithmetic.
    6 O( D( P; i1 G: z: P5 o
  5. >>> math.fsum([1,2,3,4])
    6 s: g  b8 b2 E
  6. 10.0
    , R( ?/ C9 f6 q7 x: X
  7. >>> math.fsum((1,2,3,4))
    3 Y) Q1 z- ?& P  l
  8. 10.03 _! Y! Q; A$ @# @
  9. >>> math.fsum((-1,-2,-3,-4))
    ! t$ W5 s. S. F
  10. -10.0
    , W7 Z! G: J( G+ c
  11. >>> math.fsum([-1,-2,-3,-4])
    / s0 }8 |' C1 d( Z6 k
  12. -10.0
复制代码

( _# Y+ {+ G* P% a2 x; U3 Ymath.gcd(x,y)  返回x和y的最大公约数( X# b) k2 w$ j" j9 ^
  1. #返回x和y的最大公约数# m, B1 A6 d" s. s2 p; E  ~
  2. gcd(x, y) -> int; ~& g" g" I" [. D
  3. greatest common divisor of x and y
    * k7 G, U" p8 z3 F" O; ~$ u
  4. >>> math.gcd(8,6)
    ; d1 s* W: ~$ F8 b% p6 f0 J# b
  5. 2( v1 |9 @; G* m7 C$ I) ^. Z
  6. >>> math.gcd(40,20)
    1 v0 b  c6 v( `9 W
  7. 20! U; C* ]8 B; y$ z
  8. >>> math.gcd(8,12)7 {) C; J& a7 {( h
  9. 4
复制代码
( |; `4 I  X+ x! M) t: S5 E, l, _
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
5 d) k% f8 m0 w( P, M
  1. #得到(x**2+y**2),平方的值
    # f" W, p9 R5 i: B
  2. hypot(x, y)
    - e; t5 K# J9 _2 y$ O! H
  3. Return the Euclidean distance, sqrt(x*x + y*y).- C: p, f" I6 J: y  {" y4 s+ I
  4. >>> math.hypot(3,4)
    + P; x8 j. z- S! e/ ?! Z
  5. 5.0
    8 ?& p9 d& d  `# `; ~4 [0 v3 M/ K
  6. >>> math.hypot(6,8)3 R/ g" |3 p4 E: e2 a% M9 W& H
  7. 10.0
复制代码
9 u8 N0 M" C8 }( _7 i
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
1 s. P  A0 |" ~8 W' P" l
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
      |/ E# X) f; I% Q, H7 h/ R) p8 u
  2. isfinite(x) -> bool
    3 U# ]  q  h  f+ S' c
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    9 f5 P4 f9 O1 M( U! M
  4. >>> math.isfinite(100)
    ; F3 i8 ~" a: f8 m2 w! X4 O6 J
  5. True
    ' x% u( l4 q9 [" Z4 o6 s
  6. >>> math.isfinite(0)# z4 X! _4 }# G" j$ e3 L
  7. True
    4 U: [5 f* c; F  a" I/ v
  8. >>> math.isfinite(0.1)* J+ G3 c( _8 O, H* J
  9. True% ^$ }1 c6 q/ ^  m
  10. >>> math.isfinite("a")
      O4 t& a' [$ z- _: G# Z
  11. >>> math.isfinite(0.0001)7 i% h; j. h! m% n
  12. True
复制代码
0 S- i* r' Y* H! D. I
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False8 s4 b6 a2 G/ R3 F# S/ F8 h9 [+ B
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False- @( R" H( W6 g3 ~: y  S
  2. isinf(x) -> bool+ D! G% s& b  c" Q5 @) |
  3. Return True if x is a positive or negative infinity, and False otherwise.0 T0 b! _# P2 b$ x, `8 i
  4. >>> math.isinf(234)
    ' \( F% y9 `4 Q' I( Q1 M' U$ W3 Z; E
  5. False
    : ~5 C: a6 q- o6 d+ S" }
  6. >>> math.isinf(0.1)* ?( j" M' ~4 d% ]! T+ ~
  7. False
复制代码
( q' T# ~6 H7 x
math.isnan(x)  如果x不是数字True,否则返回False, U: G! ~  `$ C' z8 l( P
  1. #如果x不是数字True,否则返回False
    - k9 p7 o( G$ [6 k
  2. isnan(x) -> bool7 e# f3 j1 B) e2 d2 B
  3. Return True if x is a NaN (not a number), and False otherwise./ \- F7 H; B5 I+ O0 m9 a' _
  4. >>> math.isnan(23)1 u8 Z+ A! C/ K( c/ S8 t
  5. False" B% |  D4 v5 x9 q8 g
  6. >>> math.isnan(0.01)
    ) `6 T1 H; h) \3 {$ ^8 X
  7. False
复制代码
* o, R( ?6 L) Y( v$ k4 v/ F
math.ldexp(x,i)  返回x*(2**i)的值) m! j0 n5 R* d6 s+ w
  1. #返回x*(2**i)的值
    / \& t- j. A; S) Y1 o% J3 a3 Y  \
  2. ldexp(x, i)* J) X+ _; y( N( ~+ Q. }) _8 p
  3. Return x * (2**i).
    1 a( u6 a5 g2 |. e& g2 J$ [
  4. >>> math.ldexp(5,5)
    ! x1 K" T- x9 f& `! p9 R  k" \
  5. 160.0% h. e8 u: Z/ x5 S) x& Q2 x
  6. >>> math.ldexp(3,5)
    ( _* C; o0 j2 H% J( S) t) z
  7. 96.0
复制代码
9 o! U5 y6 t  e: z, f! L8 T+ I$ o
math.log10(x)  返回x的以10为底的对数
( q; H: Z2 M2 A3 a- n
  1. #返回x的以10为底的对数
    7 d' Z* I: w+ h9 a4 d6 M: _& H. [
  2. log10(x)
    $ ?9 ]. F1 F, d; q
  3. Return the base 10 logarithm of x.
    / |- Y  ~- \  O
  4. >>> math.log10(10)6 ?* W( X0 \- G  x3 c$ b7 b
  5. 1.08 D1 m9 u: L" i+ \
  6. >>> math.log10(100)
    ' d0 U; R* c6 ~4 @1 R* Q
  7. 2.0% W6 Y& ]3 ^& v# y
  8. #即10的1.3次方的结果为20
    % O( V8 Y: C4 g6 L0 A
  9. >>> math.log10(20)
    1 Q: m$ M) I2 r1 l. m3 M
  10. 1.3010299956639813
复制代码

" l) v" `' H6 C& W0 d8 W3 t1 W0 w; G6 rmath.log1p(x)  返回x+1的自然对数(基数为e)的值7 N& T  ~+ A! h: o* H
  1. #返回x+1的自然对数(基数为e)的值
    , m& {) y: Y$ n$ b: e! ?
  2. log1p(x)
    " R( I) ?8 S. Z! R, [3 b
  3. Return the natural logarithm of 1+x (base e).. d3 q$ \+ o2 k( D$ l+ F; c6 l$ @
  4. The result is computed in a way which is accurate for x near zero.& E" U5 K! j: i6 L, h+ @* g1 G
  5. >>> math.log(10)6 \6 U; X% m4 Q! t2 I
  6. 2.3025850929940461 V+ [7 s9 w8 E! c  {. i; _
  7. >>> math.log1p(10)1 ?+ r+ {3 q. J6 d0 C7 \
  8. 2.3978952727983707; m: \, a, x- d+ a* j
  9. >>> math.log(11)
    : b; u, x; s8 E6 D1 s/ S
  10. 2.3978952727983707
复制代码
) I% t) g& C% t2 p1 R7 r+ k
math.log2(x)  返回x的基2对数
  _9 v! \: H! Y; [
  1. #返回x的基2对数4 ]0 i5 }5 Z& W* c# V/ M
  2. log2(x): s$ @- E5 g0 o) v7 `6 |6 u' {' Y9 n4 V
  3. Return the base 2 logarithm of x.
    ( ?; Y/ g5 s  a% d  B% M' p
  4. >>> math.log2(32)* A$ T$ W! I$ @& K0 Z5 t8 g+ z
  5. 5.0, S6 w5 e8 X0 U4 U6 Q2 S+ H
  6. >>> math.log2(20); f1 Z$ @; `7 U& \. r' D' b1 Q
  7. 4.3219280948873639 L; O& n9 w! z/ ~/ C7 n+ y. q
  8. >>> math.log2(16)! t( K3 u, Y* E5 y5 }
  9. 4.0
复制代码

2 {% h; d- d, ]' l" i6 \9 rmath.modf(x)  返回由x的小数部分和整数部分组成的元组
; D. l( @. }# I7 D  l
  1. #返回由x的小数部分和整数部分组成的元组
    6 \; {8 o9 U6 i6 R: M
  2. modf(x)5 G$ k* I. y$ s/ R$ @5 n
  3. Return the fractional and integer parts of x.  Both results carry the sign
    % T0 V, S7 D4 ~8 c# T7 Z
  4. of x and are floats.3 y8 [  T$ M- p/ m- i5 p' q" n2 c
  5. >>> math.modf(math.pi)
    % D' }2 S# J* g4 I) S/ L
  6. (0.14159265358979312, 3.0)
    1 n$ e! b0 m6 _
  7. >>> math.modf(12.34)! o- y+ l1 }$ t) Z
  8. (0.33999999999999986, 12.0)
复制代码

$ t! w8 _- C1 h4 M6 K3 Wmath.sqrt(x)  求x的平方根
* }1 U# j/ \/ K
  1. #求x的平方根
    6 @- Z# h% o6 G. o
  2. sqrt(x)9 h0 O" f+ g7 \% }' l  z4 @
  3. Return the square root of x.6 `  u5 d/ @( D- E0 p+ @& A
  4. >>> math.sqrt(100)1 V$ i3 U6 d; l# z2 ?
  5. 10.0
    / l! `8 @4 M  n1 p
  6. >>> math.sqrt(16)
    & D, E3 l& g7 g; m/ s6 {4 o, t
  7. 4.0: L' i8 ?/ r+ V9 I
  8. >>> math.sqrt(20)
    * d) G& G# x& k. q) q, p
  9. 4.47213595499958
复制代码

/ H9 J3 [( o: @" r. vmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分* K$ K$ B3 r: ~" H
  2. trunc(x:Real) -> Integral
    * R/ C- h1 o' G9 g& L( {) V
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ' {+ a" B/ u' W4 t$ c6 ^: c; ~' ]
  4. >>> math.trunc(6.789)
    ; ~2 Y  P- A. O/ x( z
  5. 69 @: ?* R# w. t8 {* |8 e2 N" Y
  6. >>> math.trunc(math.pi)
    ( O. U- w6 P6 T
  7. 3* F1 C# y5 U3 h) S2 ^
  8. >>> math.trunc(2.567)
    3 V0 S8 s+ n% h) R2 C
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-14 08:18 , Processed in 0.081492 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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