新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

) E4 \& c1 J& |, U( l+ m; w【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 j" z  E4 u) j$ g9 ?" P

$ m4 N+ j# m5 ^方法19 U! t+ f0 _) }2 l& w; H
  1. >>> import math
    $ T3 }: |0 X/ F% }9 |: B+ J% R" @
  2. >>> math.sqrt(9)* j* M1 H0 m( C1 N: B& k
  3. 3.0
复制代码
方法2
* E2 X9 k5 E* Z  Y
  1. >>> from math import sqrt
    8 c. z1 B, l7 N1 g
  2. >>> sqrt(9)
    8 _' ?/ q3 o# g# W# e: C" \4 T
  3. 3.0
复制代码

6 F. f+ Q$ @0 q$ j5 ~

1 {; K$ Y+ b! j* s! H& J/ A& ]
math.e  表示一个常量
/ G1 j+ U* |& f2 I4 A' A9 g0 {) e
  1. #表示一个常量9 c* t) J9 [; T" [
  2. >>> math.e( r' D6 s- [0 d2 w% T  x+ X
  3. 2.718281828459045
复制代码

: ^9 T' J2 E$ j4 U' ~6 wmath.pi  
数字常量,圆周率

: f2 C3 Q4 y2 ~! l- l. d+ p
  1. #数字常量,圆周率
    3 Q, `% b3 V, k
  2. >>> print(math.pi)# n  h1 o7 _4 t- O- b2 h
  3. 3.141592653589793
复制代码
" f* x" d9 Z! M
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
) ?* e; ^" u* m5 k4 F
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x. v, U! f; O# b3 w! \8 V
  2. ceil(x)* W0 U+ F" F* t
  3. Return the ceiling of x as an int.9 F0 F' _1 p! |6 n& Z4 k
  4. This is the smallest integral value >= x.
    ( G$ B0 p8 W& @; w/ u

  5. ( k8 }2 I6 V. ~, n! I- @1 ^
  6. >>> math.ceil(4.01)/ Z6 h; p7 U1 m
  7. 5
    & I% V- b3 c0 b. \# V' S
  8. >>> math.ceil(4.99)
    ( ]4 q; O( W9 P! d) g
  9. 55 k6 Y. W. Z/ l, g! r8 b
  10. >>> math.ceil(-3.99)8 W9 R2 ?4 ?8 F# A, ~8 E- v, W3 a
  11. -3+ m$ }  a- b% S9 s' P
  12. >>> math.ceil(-3.01)' ?: ]9 x6 m0 r
  13. -3
复制代码

* o- w" g$ Q" @2 ]2 \7 B: M9 qmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
2 B1 n7 L* J; R1 T
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    8 Y9 \7 y5 I: k# B
  2. floor(x): Z% `) p- q9 @! _
  3. Return the floor of x as an int.  D- ^9 @3 `" J; T" \1 C8 r
  4. This is the largest integral value <= x.: D. A* F- u) _8 L1 t
  5. >>> math.floor(4.1)3 @; C3 V# G; D* E' t" L
  6. 4
    ) L- I% m$ {3 S; T! X1 x
  7. >>> math.floor(4.999)
    ! b! T* }. f5 L$ L3 s
  8. 4
    3 g4 S. @: v# k0 v/ \( Y
  9. >>> math.floor(-4.999)/ V8 d, r- n9 {0 h
  10. -5* L7 q# q' |* y# @# Q) a
  11. >>> math.floor(-4.01)
    6 H6 I# X4 P) J, m" h3 w
  12. -5
复制代码

7 y8 \; `& }' b1 r& X4 ymath.pow(x,y)  返回x的y次方,即x**y% M0 C8 g6 W# `; c( _) h& I: U
  1. #返回x的y次方,即x**y9 \8 b+ f4 e2 @! S4 y7 i. A
  2. pow(x, y)! _! f; I# Z! t6 I* j7 p3 I+ f
  3. Return x**y (x to the power of y).
    . _1 w' j8 i3 Y$ y" Y6 V# M: b
  4. >>> math.pow(3,4)4 l+ E3 l2 J$ z; a; j5 q
  5. 81.0
    / y; j# c+ D& b8 D
  6. >>>
    % e# w- O8 D0 ]# f0 A
  7. >>> math.pow(2,7). F# \9 r- A7 T2 X! A  t0 F, e
  8. 128.0
复制代码
# i8 s& [5 D4 K% M- Z2 t$ p
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 N# {! S7 S: y4 @/ `- w1 v: K
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! |5 O5 _* R5 |% E# r1 j3 C
  2. log(x[, base])
    4 @6 H& Z  v# ?3 ?$ R  j; q. u
  3. Return the logarithm of x to the given base.
    ! H; G8 B% W$ h6 W4 L
  4. If the base not specified, returns the natural logarithm (base e) of x.
      V* K5 A- }+ D* D6 m; Q
  5. >>> math.log(10)
    7 S. X1 g3 a2 O  O
  6. 2.302585092994046
    . X5 B1 O, {+ x$ \! P
  7. >>> math.log(11)2 |7 b& b; F/ a( Z8 O
  8. 2.3978952727983707/ B( b+ [% g6 A. T; A
  9. >>> math.log(20)6 _' |( R5 q) V; u7 V& R8 V
  10. 2.995732273553991
复制代码

. \) J; P+ ~/ O6 Wmath.sin(x)  求x(x为弧度)的正弦值
- X* _5 X7 M  ?
  1. #求x(x为弧度)的正弦值! C9 z0 W2 J. P- C! _0 k# G" M
  2. sin(x)! n- Z) k  i( t5 {
  3. Return the sine of x (measured in radians).
    0 g; {/ m7 c! D( f1 i  o
  4. >>> math.sin(math.pi/4)- }# D1 [  n  ~0 b4 g' U0 z
  5. 0.7071067811865475
    ! M4 \9 Z1 B: a$ F
  6. >>> math.sin(math.pi/2); G6 J' m! b, y% K7 r& m
  7. 1.0
    + }* E- w1 l& Y8 ?# x: I. _/ k
  8. >>> math.sin(math.pi/3)
    ( K* T! S- _! W. E
  9. 0.8660254037844386
复制代码

1 f* a- _8 q1 l8 jmath.cos(x)  求x的余弦,x必须是弧度* M+ ~4 ~% N# ^# F
  1. #求x的余弦,x必须是弧度! f9 B0 I+ X+ K2 O0 i- U
  2. cos(x)6 B/ y2 W3 |3 e
  3. Return the cosine of x (measured in radians).1 t! [& o" L2 M5 h
  4. #math.pi/4表示弧度,转换成角度为45度
    ; k: \# n0 b3 t7 S- h  u, Z6 L
  5. >>> math.cos(math.pi/4)
    2 K- x) U3 ?+ h) k/ j
  6. 0.70710678118654762 F% R# o9 c7 n9 y9 V& Z- D% O" J
  7. math.pi/3表示弧度,转换成角度为60度
    1 z5 f/ i2 y7 ~
  8. >>> math.cos(math.pi/3)
    ; U2 l7 ]& i( m# I0 [
  9. 0.5000000000000001* ~0 q. L- E) H* x
  10. math.pi/6表示弧度,转换成角度为30度4 k$ J" Q3 l$ W0 J* u& I
  11. >>> math.cos(math.pi/6)
    ' x; T5 w2 v. i5 e7 v4 E
  12. 0.8660254037844387
复制代码

0 Y# c% H4 h. b0 k/ w/ _, vmath.tan(x)  返回x(x为弧度)的正切值
' q4 n6 Q; @; _4 ]# B3 j- f
  1. #返回x(x为弧度)的正切值
    ' Y/ |' z: R4 R. W' e$ M+ Z
  2. tan(x)
    2 R2 y/ u+ n$ C  o, P* I# p5 u
  3. Return the tangent of x (measured in radians).
    8 Y. F& c% k$ ~6 S+ T; `
  4. >>> math.tan(math.pi/4)
    % z& s  U+ A9 D) o" \; Y
  5. 0.9999999999999999( \& d! W$ u0 F5 R# r
  6. >>> math.tan(math.pi/6)
    ) x4 v8 J' ?& U: e
  7. 0.5773502691896257' s8 k/ }3 |0 h
  8. >>> math.tan(math.pi/3)# G! |, x3 w' z5 N7 b( s1 N
  9. 1.7320508075688767
复制代码
/ w7 k& \) V2 ~8 U0 M9 g; Y
math.degrees(x)  把x从弧度转换成角度' l9 ^* g' c8 d* J' T0 ^
  1. #把x从弧度转换成角度2 d0 f3 K/ f7 Z/ v
  2. degrees(x)* Y/ S% a* e! d6 w8 u& z& N7 W
  3. Convert angle x from radians to degrees.7 s4 ^! ?9 o! C% M7 l% k, q

  4. $ P. R% x& P  t0 }
  5. >>> math.degrees(math.pi/4): ^' t4 t! Y! M* O+ t7 ^
  6. 45.0# ~/ h* G) B; q+ A
  7. >>> math.degrees(math.pi)
    0 E$ P2 a/ B1 Q  a' F6 b% f
  8. 180.0
    + D5 d5 u% l2 B$ J& V& Y$ Z
  9. >>> math.degrees(math.pi/6)
    3 Y2 C0 _8 N, Z, l
  10. 29.999999999999996! d0 W" Q4 |2 P3 a, Z' @
  11. >>> math.degrees(math.pi/3)# c) k: K; X  Y! ^0 G5 m
  12. 59.99999999999999
复制代码
/ i( E0 k7 d' k" p  o5 V7 ~
math.radians(x)  把角度x转换成弧度$ p$ a$ F+ @7 Q1 b
  1. #把角度x转换成弧度
    : T6 K8 X+ Z, X) g! Q% h2 G
  2. radians(x); S1 X% ?# j. A" L+ ^  l" _
  3. Convert angle x from degrees to radians.8 q( _0 x( }2 K* U0 p  |* S
  4. >>> math.radians(45)! Y3 J" G' o$ u1 r! I
  5. 0.7853981633974483
    ; H( O! J  |- g$ R; i2 S2 D7 N; x
  6. >>> math.radians(60): m5 I  L* a3 s, `: o) z
  7. 1.0471975511965976
复制代码
* C/ _2 W1 {  e
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
4 F8 ^# H% {- ?
  1. #把y的正负号加到x前面,可以使用0
    . q3 p4 _2 o3 Q' G' `& u. q9 h
  2. copysign(x, y)3 g; B# g5 l2 P; @+ x9 P! J6 y
  3. Return a float with the magnitude (absolute value) of x but the sign
    3 @; e& t* _  A( q  C4 k4 N
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) " W% ~8 K7 D/ N4 g5 t, k) A
  5. returns -1.0.  `" B  N2 r# t% z+ i/ n/ ~! h

  6. " k" q3 L- {3 P  ~
  7. >>> math.copysign(2,3)7 m( q2 }6 ^$ @4 b
  8. 2.0
    + a0 \, @4 ]- t, Q/ y3 R3 R
  9. >>> math.copysign(2,-3)
    ! T  H8 e" S4 F6 f0 Z& K
  10. -2.0
    - a4 [% w! A' i" ]/ f" E
  11. >>> math.copysign(3,8). N7 e6 s( C# _3 D9 z" s
  12. 3.0& ?+ t# F6 k7 H( b
  13. >>> math.copysign(3,-8)  F( G: }9 R- d. K# N9 ^
  14. -3.0
复制代码
7 r% g' j& ?" v5 M
math.exp(x)  返回math.e,也就是2.71828的x次方8 S" [9 C& j) [, X  M
  1. #返回math.e,也就是2.71828的x次方) l1 \7 \: i& y6 j+ T9 {
  2. exp(x)
    1 O5 {+ Q1 j7 \& X
  3. Return e raised to the power of x.
    " n& _3 l! B0 k& ?! U; A! n/ ^+ Z

  4.   c3 j! k( m; ~1 J. c1 t
  5. >>> math.exp(1)
    0 ~: u" N7 }( M* ]: v& }
  6. 2.7182818284590451 S! S. k2 Y" G, i+ V" P- x# {
  7. >>> math.exp(2)
    8 n5 @# C8 z1 I4 p( a* K7 ^
  8. 7.38905609893065
    + C' B' h0 m) f% g* O% I
  9. >>> math.exp(3)
    , e9 [- N0 M9 R+ ]' q$ P: W, i/ s% F
  10. 20.085536923187668
复制代码

& Z3 P0 F0 i$ emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
; A; P3 b, F* f5 d/ t8 O
  1. #返回math.e的x(其值为2.71828)次方的值减1
      G1 g& H" u7 Q
  2. expm1(x)
    3 d- E- o& e) J; k  a
  3. Return exp(x)-1.
    1 g, K6 n1 M7 X# g- z9 ?
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ ?$ m+ i; T, A& s+ s. h# O# L

  5. ! m& q7 O1 s, T; D0 L
  6. >>> math.expm1(1)# _1 x: s1 T8 v0 C; p3 x
  7. 1.7182818284590452 w- Q! ]7 J  L! o: L, x' g
  8. >>> math.expm1(2)
    1 y: ]4 I' M1 G/ V3 f6 q
  9. 6.38905609893065# M% ?+ _4 ~7 F4 h
  10. >>> math.expm1(3)
    7 k) k; P! _# [6 H' c( Q! q
  11. 19.085536923187668
复制代码

4 d$ c, i8 B1 fmath.fabs(x)  返回x的绝对值9 A% w2 y, j1 z+ Q
  1. #返回x的绝对值" m. {; F, h( C& W' U
  2. fabs(x)% Y* c5 M# T5 M+ ~, H9 j' m  ?, `
  3. Return the absolute value of the float x.- r. k& X6 l; D2 D7 p( S. u
  4. % o: _- E, O$ N4 w7 h6 U) [
  5. >>> math.fabs(-0.003)  C- U0 X# Y' H* z. @
  6. 0.0032 S- p$ M0 _& Q; ?. ?( I- ^
  7. >>> math.fabs(-110). {% ~' E: V* A; [& n( O% q. h* c
  8. 110.0; s0 e3 }: a" T/ W8 ]4 w
  9. >>> math.fabs(100)2 m+ X9 v- x: d# T, l
  10. 100.0
复制代码

+ K( b. |  o* x2 f; i! v& r; qmath.factorial(x)  取x的阶乘的值3 R5 S" C1 l' A  O6 u2 x3 p
  1. #取x的阶乘的值" B8 g4 F3 j" x9 x
  2. factorial(x) -> Integral
    ; e+ w+ L; O" Y4 P- G: H( e7 C
  3. Find x!. Raise a ValueError if x is negative or non-integral.1 Z6 S, E) b9 ]/ m1 _: G; N
  4. >>> math.factorial(1); h$ R4 w0 V" a- S
  5. 12 y2 c1 u) k; H# A8 H- C
  6. >>> math.factorial(2)
    # ^5 P% y  X/ }9 r% c
  7. 2
    4 }: A( j; U' n7 H3 _
  8. >>> math.factorial(3)
    * e+ Z' q& A9 F$ m: _& ~8 f
  9. 6/ v% U$ j2 O! \3 M1 f. r5 s% e
  10. >>> math.factorial(5)$ N' L+ b; W1 }! f$ }! y! k
  11. 120, `' h8 d( d1 T/ P1 V6 J: U  ]
  12. >>> math.factorial(10)( F! u: D% k% U# L5 Z" F6 [6 [
  13. 3628800
复制代码

% i8 q: l! N# f6 ?9 k6 Umath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
# `1 _) ^! R, i
  1. #得到x/y的余数,其值是一个浮点数: ?  h6 `) D7 A6 m
  2. fmod(x, y)
    ! _5 w9 X( j- Z) X
  3. Return fmod(x, y), according to platform C.  x % y may differ.8 W0 }' Y, r( z$ P: U' H$ V
  4. >>> math.fmod(20,3): F' r0 q9 e- U/ s
  5. 2.04 a: {! F! `7 P5 v6 F! Y) a7 S9 r  Q
  6. >>> math.fmod(20,7). `- [. x$ `, W& l+ g
  7. 6.0
复制代码

# S  `' ^# D) T+ z3 N; `( {$ l! @1 hmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& X& L- v  Q9 s( v/ l2 u1 W4 ?& q% _
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ( d1 O) E" R/ Z2 {
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" p3 d( ?9 @5 e1 C
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1$ C+ W0 N+ M7 w, Q( C
  4. frexp(x): z) W( i+ |) X2 p; z
  5. Return the mantissa and exponent of x, as pair (m, e).
    $ t% D$ L, Y+ ~# F5 x: x+ j
  6. m is a float and e is an int, such that x = m * 2.**e.
    ' H1 i  {( J9 p* B$ H
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    " h% k+ y( l2 I4 {/ o  J
  8. >>> math.frexp(10)
    " {# U' ^4 i3 X: Z" _% l5 Q
  9. (0.625, 4)
    % p0 C# v; M4 E6 V3 V
  10. >>> math.frexp(75)
    . [! f- V% q) P* m
  11. (0.5859375, 7)# \! q+ M: s7 H
  12. >>> math.frexp(-40)
    " F7 B9 _0 w2 x5 K3 l, ], j8 h
  13. (-0.625, 6)& Y0 |$ m- B& R
  14. >>> math.frexp(-100)
    . A! p; }5 G# r, f& \6 E/ m
  15. (-0.78125, 7), h  z. H9 G8 L/ L! A
  16. >>> math.frexp(100)) W* A6 X( X" ?) l; ]
  17. (0.78125, 7)
复制代码
' A  _% X  Y" r% x1 N
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列6 f3 C7 O+ Q+ I
  1. #对迭代器里的每个元素进行求和操作
    2 {  J4 v0 N+ \5 K
  2. fsum(iterable)/ d+ m' p# M; u0 M  e1 c" t4 w
  3. Return an accurate floating point sum of values in the iterable." s( L% t4 o  Z" B* u
  4. Assumes IEEE-754 floating point arithmetic.
    ( w7 @, \9 Q. v: A# m
  5. >>> math.fsum([1,2,3,4])
    9 V% n  }9 b! ~7 z7 N8 Z, C
  6. 10.0
    4 [0 c  f* J5 _* p
  7. >>> math.fsum((1,2,3,4))
    * K/ W# Y" C7 i
  8. 10.0' c% |7 `/ E0 b4 g( i
  9. >>> math.fsum((-1,-2,-3,-4))
    3 t; W0 ?& q7 K% N, K% q" j
  10. -10.0/ y/ n8 A3 b. V) h
  11. >>> math.fsum([-1,-2,-3,-4])  u, n. M3 z: R8 H, E( F& l
  12. -10.0
复制代码
# w/ l+ F, w6 ?3 [' x# L3 ]/ l
math.gcd(x,y)  返回x和y的最大公约数
+ m% z1 ^3 z# [' c# K: _/ C8 ?- ?
  1. #返回x和y的最大公约数
    - R1 b% R0 w- H
  2. gcd(x, y) -> int
    . `: }8 S0 y3 o* @( @" H
  3. greatest common divisor of x and y8 g- }$ I. }& n" `9 g
  4. >>> math.gcd(8,6)# t! c" W) C6 u& W0 `% G
  5. 28 {+ f+ T+ f: D5 V/ F
  6. >>> math.gcd(40,20)
      f) t- o/ l! M7 w! f8 A+ L# n, N7 F# p
  7. 20
    $ J0 P  s. x- o+ a& ]" S
  8. >>> math.gcd(8,12)6 Y: w1 D& l$ A9 a+ p$ m) P; z
  9. 4
复制代码
3 }( z. q& }' r9 f" l
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False' i  a& z- {( X+ f/ D
  1. #得到(x**2+y**2),平方的值
    $ n2 X: D7 o6 T& U" O% ~9 ^
  2. hypot(x, y)8 ?9 @/ I4 b7 y
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 c& a( D( |7 X; E4 c
  4. >>> math.hypot(3,4)
    9 _: s% K% g# C* O% d$ x2 W. p
  5. 5.0
    ; l9 u" r6 x( A. G
  6. >>> math.hypot(6,8)
    + @  E/ M* L( o, y
  7. 10.0
复制代码
: r  B: y" g, j
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
% g5 L2 ^! l6 Y' J/ s
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    2 i! M  B+ q! S' Q  s8 B3 L! |" [, K
  2. isfinite(x) -> bool/ I( j, t7 S% |7 g! V4 G& m, }' F% ~
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.! h% Q: q% @8 ^3 r( E% L8 k4 h' S
  4. >>> math.isfinite(100)
    - y3 N4 s2 _) h$ h  Z% g7 s; k+ \
  5. True0 U( V& l4 r0 f( F* x
  6. >>> math.isfinite(0)4 J- V8 \; q& L" @8 [
  7. True
    6 t" v; |7 d/ b+ z
  8. >>> math.isfinite(0.1)/ Q$ G6 Z) s0 r8 U. _+ w! f: A* w
  9. True! r7 ]% h# z" b' D
  10. >>> math.isfinite("a")4 }7 M/ R. n) [" ^. n) w
  11. >>> math.isfinite(0.0001)
    ; y, s4 N8 w' K9 V' r
  12. True
复制代码

) n4 G6 P) N) z3 S  Y1 C) \math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False" {% c" H% R6 w" Y0 @+ ?6 b# K
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    / {+ G7 ^' M- N4 |" s
  2. isinf(x) -> bool
    ! ^! {1 V1 d/ m. p% j; Q
  3. Return True if x is a positive or negative infinity, and False otherwise.
    3 R; ~( W4 P0 h3 \
  4. >>> math.isinf(234)
    1 V1 h# U' i5 R$ [# X; Z& B) Q9 Z
  5. False
      x9 f; t$ I+ {: ~  [+ E. J: m; n  y) Q% @
  6. >>> math.isinf(0.1)
    " j& x: x4 L# J4 k. a
  7. False
复制代码

3 Y* i! ~7 H8 }/ v6 Fmath.isnan(x)  如果x不是数字True,否则返回False  B4 X% r% e8 y
  1. #如果x不是数字True,否则返回False9 P) h. {9 ?0 m, D% H, M; _
  2. isnan(x) -> bool
    # ]$ b5 s, e0 s
  3. Return True if x is a NaN (not a number), and False otherwise.7 s3 \! P; d# e' A, L& ^- s
  4. >>> math.isnan(23)
    % E/ J, X) u8 `& [5 p; ^6 O- h
  5. False4 _* i' f0 t' z2 V
  6. >>> math.isnan(0.01)
    6 w! m  b1 Q  z6 d+ X  l
  7. False
复制代码

* k! A+ V# `. g2 k6 ^) Nmath.ldexp(x,i)  返回x*(2**i)的值) w! U. N& S2 J# }# O, R
  1. #返回x*(2**i)的值5 ]2 `- t* [# W8 M! T
  2. ldexp(x, i)% ?6 Z: E/ l  s% j6 O$ B5 i7 G
  3. Return x * (2**i).
    : v. {' ^  r6 |. i* Q, V# [. s
  4. >>> math.ldexp(5,5)
    ( `2 w: ^- Z# ~1 V' j, V% \, x
  5. 160.0
    % `: s- S8 A8 i8 W  E
  6. >>> math.ldexp(3,5)
    8 T4 _5 y" y: u# h* t
  7. 96.0
复制代码
# X) ]1 H  ]" {: r
math.log10(x)  返回x的以10为底的对数
( p0 S! ], E% f9 |. O3 u( L' K
  1. #返回x的以10为底的对数3 @; W! e/ |% O$ D/ w; c2 N9 I
  2. log10(x), I3 C9 U: {3 x$ [
  3. Return the base 10 logarithm of x.
    : F2 N6 {- p$ ~( }# c, |6 c
  4. >>> math.log10(10)/ m& h; S" e9 ?% A4 f* X
  5. 1.0" F8 Y9 \. f, R6 R! R6 K
  6. >>> math.log10(100)+ v; B) A+ u  @% e$ n/ e
  7. 2.0! K; F; x* ]# I- ?
  8. #即10的1.3次方的结果为20" h) m/ e5 @1 e( }. F: A/ G
  9. >>> math.log10(20)8 B; H; F- Y( W! E
  10. 1.3010299956639813
复制代码

1 J8 o6 Z3 W$ P. Imath.log1p(x)  返回x+1的自然对数(基数为e)的值$ \7 h9 d. v/ Z" }- q6 A: U
  1. #返回x+1的自然对数(基数为e)的值
      x: i, Z5 @4 }3 C/ y" U4 b
  2. log1p(x)0 a9 h8 A: [' I- O/ B% _1 f
  3. Return the natural logarithm of 1+x (base e).. w. {, `! l9 `
  4. The result is computed in a way which is accurate for x near zero.  }- f  G, C3 s1 @
  5. >>> math.log(10)
    : s2 V. I9 |) y0 t. J  q; T
  6. 2.302585092994046/ k- ?2 G9 S9 b2 b4 Y9 e; K( {4 {& ?
  7. >>> math.log1p(10)
    ; K, i5 r, u: v: g4 A; `7 @
  8. 2.3978952727983707- O& B6 {8 L6 d/ U/ N
  9. >>> math.log(11)* Z8 M3 I. h# W* b) z
  10. 2.3978952727983707
复制代码

8 o) |5 k8 ]/ F) {" g6 Smath.log2(x)  返回x的基2对数$ l2 Z9 e2 l5 E  K
  1. #返回x的基2对数# Q5 e5 u% X6 E. K9 p
  2. log2(x)  n: a5 {% P6 o, S  u
  3. Return the base 2 logarithm of x.
    + a/ J9 ^" M+ e8 j( m$ \0 x4 C0 u
  4. >>> math.log2(32)
    0 O6 m6 c# r0 ~. ^+ U0 e0 x6 w
  5. 5.0
    / P3 `, I) y8 O% b3 q9 h
  6. >>> math.log2(20)) T5 V- Q9 F+ O! b
  7. 4.321928094887363$ W3 g# e5 ?( t% a* y- D" E
  8. >>> math.log2(16)
    % K' _9 F% B+ L$ @( w1 J
  9. 4.0
复制代码
7 F2 v9 v0 U. ?* _
math.modf(x)  返回由x的小数部分和整数部分组成的元组
3 K+ R% G4 b; x( V% W& F/ H
  1. #返回由x的小数部分和整数部分组成的元组: V2 F, y1 J) x3 E& V' c4 l/ ^
  2. modf(x)
    2 e- M: Y- F0 i5 i
  3. Return the fractional and integer parts of x.  Both results carry the sign+ ^) c/ Q+ s% H* u6 l1 G$ ?7 z
  4. of x and are floats.; w- ^4 n  S7 f* @$ e' y
  5. >>> math.modf(math.pi)5 c2 G% ~0 t, m; y, G
  6. (0.14159265358979312, 3.0)
    1 t9 L9 c3 i7 O- R# q5 ]. `0 M
  7. >>> math.modf(12.34)
    ' j  D) J! e& D. D3 c
  8. (0.33999999999999986, 12.0)
复制代码
" J: }, b4 ^  X* D1 l
math.sqrt(x)  求x的平方根
1 @& `: Y% j$ v  A
  1. #求x的平方根( J# h4 A/ o8 ~' `8 G
  2. sqrt(x)
    % f# z6 l2 b9 a& O
  3. Return the square root of x.
    # X  b& N4 @# V' i% a, U7 G2 U4 E( e
  4. >>> math.sqrt(100)! |  U2 W" n  P
  5. 10.0
    5 \, G/ N5 y: e5 H, Q2 a3 m0 p
  6. >>> math.sqrt(16)
    * t+ _. T+ `( ~
  7. 4.0
    $ R. v0 W& ~( }
  8. >>> math.sqrt(20)$ Y6 \1 I4 _1 Z# s( L) x
  9. 4.47213595499958
复制代码
+ D* ?- f/ G  W, p$ \* h6 O4 N/ m
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分+ C0 O* Y9 h0 I/ v. F8 t
  2. trunc(x:Real) -> Integral
      t, E$ X% H: X. E7 F6 ?
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 [2 j3 }' N: V& m
  4. >>> math.trunc(6.789)1 }" M3 ~4 ~3 [
  5. 6
    1 ~+ s- }; X7 S3 Z4 A; d
  6. >>> math.trunc(math.pi). p. M0 @# }2 {# c: I4 I2 ^4 E
  7. 3( Y8 @. v' X( R5 \
  8. >>> math.trunc(2.567)8 Y$ A6 o) j8 p) _6 h1 x- d' g
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-7 08:55 , Processed in 0.081401 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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