新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
% B) h5 W! K- E# o4 t! d4 a
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ y2 i2 z/ h) A: j& x, `
- e; w, Q5 a7 I* {  h1 y0 l4 _
方法1
/ ^5 u  }$ S# W9 B8 L0 _4 I
  1. >>> import math$ i9 S; p+ M/ @) I& c# q1 x
  2. >>> math.sqrt(9)
      ~  A; @" ?6 O
  3. 3.0
复制代码
方法2
% d2 K9 S8 i$ p8 x* k
  1. >>> from math import sqrt) @# X$ `! Q3 \/ u6 \3 n
  2. >>> sqrt(9)
    ) [1 P2 t% _7 y+ R, m
  3. 3.0
复制代码

1 H, p7 v9 i3 l3 C7 h/ m
* W" A6 X: M1 i# h6 d0 F% k
math.e  表示一个常量
( z9 n9 f- l% V& K
  1. #表示一个常量2 M( P( Q4 t8 b1 F. M: G8 Q0 f
  2. >>> math.e3 D3 Y5 b6 V7 A' v- h, ~
  3. 2.718281828459045
复制代码
0 e* C/ J$ O4 B3 M
math.pi  
数字常量,圆周率
; A. U8 Q7 q+ F' j2 Y
  1. #数字常量,圆周率
    ! y3 y: J. ^, i2 Y% b* O+ D9 I
  2. >>> print(math.pi)! U2 D* i7 C; H6 ~5 d% v
  3. 3.141592653589793
复制代码
, N1 v5 C- ]5 ?! U' u: M5 p
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

$ U+ I# ^2 _) u7 y, T
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x9 O- F7 b$ Q/ u) `( [  O4 m% ]6 V
  2. ceil(x)
    * Z( {, E: \  S6 L
  3. Return the ceiling of x as an int.0 V* u7 c8 s5 m/ P
  4. This is the smallest integral value >= x., J& T; S8 d1 Q6 x
  5. 6 U, E8 ~2 w/ V: [
  6. >>> math.ceil(4.01)
    ( i( d7 }  r6 O. U' m/ H
  7. 5
    # H: D7 e- P/ l% U
  8. >>> math.ceil(4.99)
    7 t6 V0 \: l- o. }8 k5 q9 l$ y! t8 R
  9. 5" q. S/ S) V/ P" f- T
  10. >>> math.ceil(-3.99)+ ^# c2 `% C4 _1 r8 c2 `! G
  11. -3" Y4 w* y6 K9 e+ |& f
  12. >>> math.ceil(-3.01)& G9 g" T8 C- h: }" O
  13. -3
复制代码
0 {0 d; R7 I0 \: Y4 y6 h% W/ ?
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
4 P& m8 ?" h; {# I6 Z6 Y9 e' U
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 K% E1 k! z$ A
  2. floor(x)
    ; Z( L& S" M: y5 B( y
  3. Return the floor of x as an int.
    ; ]  T) k4 @: s1 Y5 q6 a1 k
  4. This is the largest integral value <= x.) ^3 a4 b% T% U: e7 j
  5. >>> math.floor(4.1)4 n( D  p, V% N) v" a7 \$ e, a
  6. 45 L7 f4 N7 y4 s# }4 K4 }; c8 }' u
  7. >>> math.floor(4.999)1 v6 d! r; l8 [  J8 k
  8. 4
    6 }4 q4 F* S6 A
  9. >>> math.floor(-4.999)
    ; |2 B/ h) Y* i4 e) o5 L
  10. -51 o  j9 m2 p$ K- d! ~3 w
  11. >>> math.floor(-4.01)9 u" {' `; \& r) D; x! j/ K
  12. -5
复制代码
# {& x7 W0 J! z4 t, _" W1 U
math.pow(x,y)  返回x的y次方,即x**y
$ x" n$ W  ~" L" h) {2 I
  1. #返回x的y次方,即x**y+ \8 L- V1 U/ ~& l8 Y1 t, }
  2. pow(x, y)
    7 F) d( p2 d& b0 s- B2 E$ B
  3. Return x**y (x to the power of y)., }( p/ _" L; H( k) B* R' D8 s# ~
  4. >>> math.pow(3,4)5 ^8 R+ b4 P& u
  5. 81.02 C, d1 N/ u. Q
  6. >>> / K" Z& P1 v) e5 ~: R( ?) ?1 a
  7. >>> math.pow(2,7)" M7 C) [% g8 i/ r
  8. 128.0
复制代码

+ r  ^0 ^) F& E; c3 nmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 S+ {& W* a$ M% k( @  n8 A
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    3 f0 e: Z- [( W- N2 U
  2. log(x[, base])
    $ N! o; a$ X- G" O# d) c; e1 b; h1 ]
  3. Return the logarithm of x to the given base.
    6 A3 t' R7 K! C5 v# H% }
  4. If the base not specified, returns the natural logarithm (base e) of x.( [, G: r* H5 v$ C& `! @% X# |  Y
  5. >>> math.log(10)4 X$ o, K/ }) q# h4 `
  6. 2.302585092994046
    % l1 S2 }# `! \  w: T. z( E
  7. >>> math.log(11)
    5 _4 z5 ?( f7 X
  8. 2.39789527279837074 y! w- Q; ^! H) V4 v
  9. >>> math.log(20)3 h+ m  X/ P( D* y4 ^2 V
  10. 2.995732273553991
复制代码

1 p+ T4 p) ^" A, ~9 d4 Wmath.sin(x)  求x(x为弧度)的正弦值
/ e1 F) m4 s) }
  1. #求x(x为弧度)的正弦值
    " V* N% t5 U; {. `
  2. sin(x)
    " S- p1 w+ M0 S' C: r
  3. Return the sine of x (measured in radians).& [  M/ g$ r/ X& O, u
  4. >>> math.sin(math.pi/4)7 J+ s( O3 u& h6 [: x1 }, a
  5. 0.70710678118654754 F9 `% W; L  x
  6. >>> math.sin(math.pi/2)
    6 p* i1 y7 J/ h8 @( A9 F" ?1 j5 l
  7. 1.0% t4 Z* {# X$ [" a  _. C! B$ V
  8. >>> math.sin(math.pi/3)/ f7 |% z* ]; m; w1 C
  9. 0.8660254037844386
复制代码
7 P" o; U% q- K, N
math.cos(x)  求x的余弦,x必须是弧度' c, f  ]4 [' z5 A& l
  1. #求x的余弦,x必须是弧度  N# y: F8 I, _' K9 W- ?. z& q  [' C
  2. cos(x)  i) p) A# \+ B5 b5 D- v
  3. Return the cosine of x (measured in radians).
    ' z9 s% V- V3 k  R; k' t% C. j
  4. #math.pi/4表示弧度,转换成角度为45度
    : }) F6 n* Z: t$ N6 e
  5. >>> math.cos(math.pi/4)- d) U) \" f: X3 d, p3 K
  6. 0.70710678118654762 c- I6 r2 l  j  C
  7. math.pi/3表示弧度,转换成角度为60度
    7 q; w) Q2 |+ A0 m( F% Q/ W' ?0 S; {5 f
  8. >>> math.cos(math.pi/3)3 ]' ?% p/ m! g8 P5 f9 Y
  9. 0.5000000000000001
    ; R6 F4 `/ J7 [# b
  10. math.pi/6表示弧度,转换成角度为30度4 e) a; Y5 j4 s0 d+ s
  11. >>> math.cos(math.pi/6)
    2 q% [; y- _+ I4 Y* _
  12. 0.8660254037844387
复制代码

* C$ a8 y& u, B6 `! S6 h! Kmath.tan(x)  返回x(x为弧度)的正切值2 Y$ e6 k" k5 z3 z
  1. #返回x(x为弧度)的正切值8 C% X0 v1 c2 D- t0 h+ ?
  2. tan(x)* o! @0 ^. G- H1 J
  3. Return the tangent of x (measured in radians).' T' g2 ?) e( I/ b4 p
  4. >>> math.tan(math.pi/4)
    ) P1 g7 Q; w; S7 t
  5. 0.9999999999999999' _: [1 ]7 q6 I
  6. >>> math.tan(math.pi/6)% Z4 X, b, U; c/ Y' |& f
  7. 0.5773502691896257
    + \- E9 S9 e6 [) Y2 M, N2 U5 b
  8. >>> math.tan(math.pi/3)
    8 v/ D$ G, p9 I; s. o6 G  m, Q
  9. 1.7320508075688767
复制代码

/ M$ ~6 y$ d/ }math.degrees(x)  把x从弧度转换成角度& Q4 h/ q( R: J* o; ?2 t9 F
  1. #把x从弧度转换成角度
      y5 E, Y& n/ v
  2. degrees(x)
    & E. s3 J* g9 I2 |; a# ^
  3. Convert angle x from radians to degrees.( o: u5 |# @) p' p5 C& A7 k
  4. + S' |7 `2 K$ h! a8 V8 s
  5. >>> math.degrees(math.pi/4)' Y$ B5 x/ l5 h: ]8 }/ {
  6. 45.0
    . O+ {/ Q1 c2 o* m' J8 S
  7. >>> math.degrees(math.pi), T  k# Z% j3 m& n, y# f
  8. 180.04 E  |7 v4 D' A4 M3 f
  9. >>> math.degrees(math.pi/6)6 B' V& S  d, L; f  \8 A
  10. 29.9999999999999963 u; u( D; h7 p1 }, _5 O+ P* K
  11. >>> math.degrees(math.pi/3)! J1 U7 ]. n4 B: Z
  12. 59.99999999999999
复制代码

$ C4 Y& u3 @6 i8 [! ]math.radians(x)  把角度x转换成弧度4 _- L6 [6 ]5 G. i# H
  1. #把角度x转换成弧度) x6 m' k6 c0 J3 k; B: H- Z& P, K( y; v
  2. radians(x)8 O  }& G! `/ I$ b
  3. Convert angle x from degrees to radians.
    9 v; f4 B/ l! @) F7 Z$ Q* P
  4. >>> math.radians(45)" K6 M' y1 K; Z. f$ \
  5. 0.7853981633974483
    % r6 h5 ?. F$ Z4 L+ V/ z
  6. >>> math.radians(60)
    7 E: d3 e6 A0 c* u# o- {! x
  7. 1.0471975511965976
复制代码

" l' ?/ J1 p; I, N' V7 a& W; [math.copysign(x,y)  把y的正负号加到x前面,可以使用0
: U7 Q" V1 G* Q
  1. #把y的正负号加到x前面,可以使用0
    % q3 K/ [6 h, Y: E5 Q% e* P  x
  2. copysign(x, y)
    - U+ v6 b5 f4 ~6 {
  3. Return a float with the magnitude (absolute value) of x but the sign
    ' x, g( h4 [* Z6 u
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ! u5 @6 G: X" i: P- y
  5. returns -1.0.* h# h2 ^) S& z  M- L3 ?
  6.   {: ]1 C6 S) R8 V
  7. >>> math.copysign(2,3)6 V. o  O/ J% a# E$ ~7 Q2 u! v* a
  8. 2.0! ~  C4 k& S" h0 s0 a
  9. >>> math.copysign(2,-3)
    + t+ ^% P- E" [6 p7 y
  10. -2.0! ]1 @% f: {8 l2 g0 p: c/ Q
  11. >>> math.copysign(3,8); l& H4 I  M7 F8 R
  12. 3.0! o- x1 t9 G' L* r8 M: K
  13. >>> math.copysign(3,-8); _9 d. R9 R: d% K6 v5 B9 ?
  14. -3.0
复制代码

4 t6 a' W" \$ J+ ]# _/ p1 B0 emath.exp(x)  返回math.e,也就是2.71828的x次方3 d" @; t+ u# T& v* R3 B9 }% B
  1. #返回math.e,也就是2.71828的x次方
    ' v0 F9 }- H% X& T: Q! U( @! k
  2. exp(x)
    2 y8 D1 h; \8 i- H& k. j
  3. Return e raised to the power of x.) p! T/ w: b- H% R9 D! k

  4. - [2 h4 u% l( b( z& A% l
  5. >>> math.exp(1)
      O. l4 s4 \" f. q' ^4 \# H
  6. 2.7182818284590459 @$ D* w) {4 N# d$ {
  7. >>> math.exp(2)
    ) `9 l1 F# x( O2 C
  8. 7.389056098930657 Y  A* A: ?/ d- v) G
  9. >>> math.exp(3)# v, j  f1 ~( O  N7 T1 k0 ^
  10. 20.085536923187668
复制代码
6 @! B) I+ L, b0 e
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( d7 M+ @& ]7 J, a4 h" Q
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ' p& u0 C9 [/ C# z! i* z& {2 v& |
  2. expm1(x)
    : j$ r* j7 |7 k6 w) J5 Z
  3. Return exp(x)-1.
    2 z/ M% Q% m' s
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.. j/ l# H- {- F# d

  5. ( F; ^  n1 @* {8 {! v. L
  6. >>> math.expm1(1)
    . Q! r) J2 {+ h  r2 w) N
  7. 1.718281828459045
    + D7 v6 _; v; b
  8. >>> math.expm1(2)! k7 V% `4 v& V
  9. 6.389056098930657 ?, G- J+ d6 {6 o
  10. >>> math.expm1(3)/ e: ?3 W/ @0 b
  11. 19.085536923187668
复制代码

7 R  ^( a) P+ v7 V/ Z: B( Umath.fabs(x)  返回x的绝对值& q9 [0 p1 {. n0 K6 r
  1. #返回x的绝对值
    ) S) Z+ a, r4 O, r3 c0 d
  2. fabs(x): P* ]2 c) U2 F- j( F) J) z
  3. Return the absolute value of the float x.* f( u; X6 {- `
  4. % M9 K7 ]% M0 n0 C% u, q+ P2 B9 W
  5. >>> math.fabs(-0.003)
    : W! x5 j' a: E  a/ Z: C& R
  6. 0.003
    , O* Y/ g# j# E' V6 d
  7. >>> math.fabs(-110)
    1 ^4 e$ j# p5 g1 S3 x1 I; ?+ Y
  8. 110.03 c& G* Y8 S+ H- V/ Z+ o! y
  9. >>> math.fabs(100)) ?( h, j' e: i7 \& O1 Y
  10. 100.0
复制代码

, J, [( U, L# c1 P2 h. v7 }math.factorial(x)  取x的阶乘的值  q- }$ \, j1 B7 r* @  u$ ~$ _
  1. #取x的阶乘的值
    8 X# ?2 g6 A# ]- u2 c
  2. factorial(x) -> Integral
    . W( l' R; e4 [* u
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    , {" M  D7 B' Z/ y0 |) s
  4. >>> math.factorial(1)" t4 c  M8 [: P1 T
  5. 1
    : W' r3 i3 P. K7 T4 I
  6. >>> math.factorial(2)% B3 p. b' ?1 P3 a4 g
  7. 2% n4 x9 ~% A( E8 [: ?$ t! R
  8. >>> math.factorial(3)' D! L9 E( }3 i5 r/ P2 @
  9. 6$ f8 m4 |( A; ]) D! R5 P/ N
  10. >>> math.factorial(5)
    ! r' x. H( [, a8 G9 u# U& X! M
  11. 120, H$ a0 l0 P5 {* Q, X6 M3 o8 B
  12. >>> math.factorial(10)
    $ G4 ?3 _/ I- N0 O2 N: T
  13. 3628800
复制代码

& e' R% d! m/ ]2 O3 X3 T% Amath.fmod(x,y)  得到x/y的余数,其值是一个浮点数! I1 N; s4 }3 m7 r% G
  1. #得到x/y的余数,其值是一个浮点数) E& y% R8 S3 ^) w, \
  2. fmod(x, y). m9 P' }+ U$ B
  3. Return fmod(x, y), according to platform C.  x % y may differ.1 |6 U0 W" \. f) z# r! k- g" i5 l" N
  4. >>> math.fmod(20,3)- M& l# e! C( ?5 S) \* C
  5. 2.0' X7 q/ _7 g! }
  6. >>> math.fmod(20,7)3 p; ~9 R( ?4 ]9 K9 ]2 ]  C
  7. 6.0
复制代码

$ Y4 i0 {' Y; c1 Z# Gmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
# C9 f8 Z/ Z* K, [: k
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,2 M$ I, E4 C5 u4 g! C9 E
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ! |3 e6 |, {3 p+ F% m
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
      |  ~( r6 M/ j6 G, ^: v
  4. frexp(x)& G3 h/ R0 L3 q, f- Z' L
  5. Return the mantissa and exponent of x, as pair (m, e).& u1 i. g! b* [
  6. m is a float and e is an int, such that x = m * 2.**e.5 E3 }9 a: y$ V7 X
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    " C0 G' u% p( C9 u* p1 V
  8. >>> math.frexp(10)7 [: u1 `* V* n' ~0 k: X4 a+ O
  9. (0.625, 4)
    , l. F; y& R7 u& y2 N
  10. >>> math.frexp(75)+ [) `9 b' v" g
  11. (0.5859375, 7)
    6 D  ?; c' ~& y& v" s" C  w
  12. >>> math.frexp(-40)
    " m; o+ ]- w; G5 l5 s
  13. (-0.625, 6): W$ @% X* O$ @
  14. >>> math.frexp(-100)
    2 S7 W- x/ J* A
  15. (-0.78125, 7)
    4 s% B1 }) I. u& G! ]! E1 q3 }' }
  16. >>> math.frexp(100)7 ~; f7 l9 [/ U' R  E: g( x! ^
  17. (0.78125, 7)
复制代码
' Z- G+ f3 D, ^8 s' K" L; g
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
' R/ f7 M; y/ V
  1. #对迭代器里的每个元素进行求和操作* ~/ i! V1 A" U3 T8 [, W
  2. fsum(iterable)
    1 s9 h9 x3 p7 H( n' U: q
  3. Return an accurate floating point sum of values in the iterable.
    8 n  o) O# S; x" e" I! g0 \: Y
  4. Assumes IEEE-754 floating point arithmetic.$ ~1 t" f& G% _( l) O- W& M4 L
  5. >>> math.fsum([1,2,3,4])- [  f+ C, L* i+ c: l. J
  6. 10.0; n+ S! s1 w. X6 `
  7. >>> math.fsum((1,2,3,4))
    + K( [8 ?2 I; U& t. u8 _+ G1 }' G$ N3 r
  8. 10.0
    3 @' _8 R5 m  U4 {" Y! R: k
  9. >>> math.fsum((-1,-2,-3,-4)). f- I( O! ]& k( l! d
  10. -10.0( d4 \: E. B& R, A9 H
  11. >>> math.fsum([-1,-2,-3,-4])
    & m5 ]: y$ A( i" ?
  12. -10.0
复制代码
8 u, G0 c8 ^, N7 H5 \
math.gcd(x,y)  返回x和y的最大公约数
# r2 @) q0 o4 H0 r
  1. #返回x和y的最大公约数
    2 z) Y6 F& K% z8 W
  2. gcd(x, y) -> int
    2 Q: ?6 b! S2 u
  3. greatest common divisor of x and y7 M8 J% o8 n& Z, V
  4. >>> math.gcd(8,6)
    : h# v' z1 d6 z7 K+ |* a
  5. 2
    ! E/ L  K0 h& T: s9 w" j
  6. >>> math.gcd(40,20)
    ' O, m; F; \- |
  7. 20
    ) D  y8 O" x# V7 d
  8. >>> math.gcd(8,12)
    2 F/ C2 F" n( _
  9. 4
复制代码
* ~4 N% n3 y- [) \
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
1 Y6 ~  Z/ M' N6 E; u" |) r  n
  1. #得到(x**2+y**2),平方的值; y" G8 j! j$ }3 g3 n2 e
  2. hypot(x, y)
    . ?" @# @- G4 q$ d8 e
  3. Return the Euclidean distance, sqrt(x*x + y*y).4 _, _# i) `) P  C/ {
  4. >>> math.hypot(3,4)# ~5 k( ?  p% t1 Y$ M5 J- h* L
  5. 5.0
    & c) Q) j6 B+ ?  s* M# ~3 X' a- n
  6. >>> math.hypot(6,8)
    ) r' b* j) ?! }' }. k; O, K
  7. 10.0
复制代码
/ ^6 d* T! E0 U5 g3 e3 M! w9 O
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False/ K5 j$ P$ E! V( X/ e
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ' C9 g) Y% ]; d# v" L
  2. isfinite(x) -> bool
    * t2 b- k. O. U- ?" U
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' _8 X8 D- I8 j; r- X3 |& Q
  4. >>> math.isfinite(100)( u9 @  n6 B2 X, |! V, \$ F. E  p. [( m
  5. True" ?: O( e+ z- y
  6. >>> math.isfinite(0)* l4 ]& w2 Z4 `. I2 Q5 }) z* d" a) t9 @6 \: l
  7. True9 W5 j2 q( M, b1 a! _1 V
  8. >>> math.isfinite(0.1)
    3 m& O9 ?( [" @) J: o) v
  9. True/ i& L( v6 }. ~0 v# Y( W2 h5 o
  10. >>> math.isfinite("a")
    4 m1 x0 M3 m& P8 M0 K: X9 Y
  11. >>> math.isfinite(0.0001)4 m- Z/ b: V: ?% k( H
  12. True
复制代码

1 V& {0 ]( @, kmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
( c1 i5 N" N: `2 ~2 ^7 P
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    + ]# h, e& Y8 c" P" {
  2. isinf(x) -> bool, {+ F* n+ b7 z) \$ N
  3. Return True if x is a positive or negative infinity, and False otherwise.
    9 ?0 l6 o6 T; o' g+ W* @
  4. >>> math.isinf(234)
    : J3 \( c7 D2 w3 T  f
  5. False
    9 t- U8 x8 D  h
  6. >>> math.isinf(0.1)0 {7 H6 \" M  A1 Z: N
  7. False
复制代码
0 w( g# L; W  D/ I7 L
math.isnan(x)  如果x不是数字True,否则返回False
# Z" S8 i: E' r' T  h2 S+ G6 T+ L& Q
  1. #如果x不是数字True,否则返回False- i1 ~; Z& I! R9 i- [# I
  2. isnan(x) -> bool. H$ g  g$ `- ?) L
  3. Return True if x is a NaN (not a number), and False otherwise.
    3 W  @5 t) @& N- s' |* A
  4. >>> math.isnan(23)* P: ^$ f+ ^" o( P1 {3 i
  5. False
    3 L1 Q3 W" a' ~9 E6 B
  6. >>> math.isnan(0.01)
    8 x' D9 b# T$ W
  7. False
复制代码

, ]" c: N. S4 ]: ?( L6 imath.ldexp(x,i)  返回x*(2**i)的值2 H7 @8 E1 s7 c* H, F5 p1 J3 f7 B
  1. #返回x*(2**i)的值- Q( \0 C, l) u) u0 ^# ]# _/ L
  2. ldexp(x, i)
    : ~  S1 l1 n$ n& T+ Q4 e% j+ d
  3. Return x * (2**i).
    3 g% z6 c% P* z
  4. >>> math.ldexp(5,5)0 J# \0 x8 R3 d3 }8 H" I
  5. 160.0
    9 F7 i2 I& c8 u6 w
  6. >>> math.ldexp(3,5)
    0 ?# e8 Z' I' D2 I. t4 v7 d
  7. 96.0
复制代码
6 L3 J! g. N+ ^( L7 u
math.log10(x)  返回x的以10为底的对数& _( Y* p# |6 e' m8 w6 |3 j  J
  1. #返回x的以10为底的对数
    / J( k$ L$ q& R- I/ R, _8 n8 w
  2. log10(x)
    . A8 t4 ?. s$ w
  3. Return the base 10 logarithm of x.+ p$ i0 Z# T7 T  J; F
  4. >>> math.log10(10)
    & S7 J! E! @% ?, m2 B! p
  5. 1.0) l9 V5 v7 ?, O/ ~1 J2 {: I
  6. >>> math.log10(100)
    9 z0 z3 V/ j. G9 L% d( @
  7. 2.0) }  w3 o) B$ E6 n# t3 I5 o
  8. #即10的1.3次方的结果为20
    ( t9 }& D: Q' g* s
  9. >>> math.log10(20)0 D* W+ |5 x6 W3 T6 K) O  q
  10. 1.3010299956639813
复制代码

% Z4 i/ P! I$ d+ t2 q8 [, I: Smath.log1p(x)  返回x+1的自然对数(基数为e)的值
8 \: J1 h( x( p8 A, Z! E3 y
  1. #返回x+1的自然对数(基数为e)的值- D- N9 Y$ F( m" P% N
  2. log1p(x)- |6 m8 Q6 ^0 j. k* b6 a
  3. Return the natural logarithm of 1+x (base e).
    0 Y8 s' }* j: O; {3 |; p, E
  4. The result is computed in a way which is accurate for x near zero.% a2 P# K' n% Q
  5. >>> math.log(10)/ R# P# h% D$ l% o' R. m' }
  6. 2.302585092994046& Y+ D2 c2 v0 e
  7. >>> math.log1p(10), c3 k& e: b. D( H8 w* Z1 t# _* Z8 L
  8. 2.3978952727983707& n9 n: s* u9 W" w7 [- R
  9. >>> math.log(11)$ {0 u. T+ A/ o, P7 j
  10. 2.3978952727983707
复制代码

! U( x  ?, Y! T0 c- J. Gmath.log2(x)  返回x的基2对数
0 y5 l' N% Z$ K( k' ?
  1. #返回x的基2对数
    $ a  y1 I3 h( J
  2. log2(x)- Z: e# n( ]8 i
  3. Return the base 2 logarithm of x.( e0 q$ b7 n' ?4 b. C( E
  4. >>> math.log2(32)
    3 S5 b$ K" z% T5 I2 \% ?+ v
  5. 5.0
    " \, ?0 M! E5 A2 e+ d
  6. >>> math.log2(20)
    0 T& t4 v$ A) B% G# u$ d! c0 a3 m. _
  7. 4.321928094887363' I/ P8 Y1 }; H
  8. >>> math.log2(16)
    5 K# g8 k8 o: O% i
  9. 4.0
复制代码

! T6 x. t/ k$ A+ M' kmath.modf(x)  返回由x的小数部分和整数部分组成的元组2 I( `' ^  w* P+ G/ A+ j" K
  1. #返回由x的小数部分和整数部分组成的元组
    * ?0 a  d1 X1 _
  2. modf(x)
    5 n* W9 R4 U$ l- ~: P, L: v
  3. Return the fractional and integer parts of x.  Both results carry the sign/ r4 _0 o0 L' A) q9 d8 L
  4. of x and are floats.
    . B8 P' g) }3 F8 k1 U
  5. >>> math.modf(math.pi)+ l# N( [: R$ t5 k
  6. (0.14159265358979312, 3.0)
      V! e' G9 t+ S2 D8 K; G
  7. >>> math.modf(12.34): X# M+ {- R; p4 m
  8. (0.33999999999999986, 12.0)
复制代码
- ^" K$ ?1 E: }; ~! l% m% n
math.sqrt(x)  求x的平方根% I& z5 g4 J3 Q4 r; R/ ^
  1. #求x的平方根+ v9 @. W' q4 b( k& n. u' p' W) {
  2. sqrt(x)
    3 b6 o7 d: z4 e9 X! c
  3. Return the square root of x.
    5 a% ~$ [6 T  w# E% M0 u0 _
  4. >>> math.sqrt(100)
    5 }! ]1 |7 O  ^% s, t/ q" G/ C( V
  5. 10.09 K5 ]- T% e) n2 G4 Q9 p" C6 k
  6. >>> math.sqrt(16)
    9 c5 X' [9 y0 F# p6 h0 q9 k* ^
  7. 4.0
    # R6 T) K$ S- f4 H2 X0 v
  8. >>> math.sqrt(20)
    9 b1 G" e5 D) c# c+ W$ v( o9 d
  9. 4.47213595499958
复制代码
. o* A* O1 _3 e/ v
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分! o# M: T6 ?, [7 ~1 ]0 V
  2. trunc(x:Real) -> Integral
    % v8 r/ D: P3 ?2 a+ ^$ R) z; L9 m
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ) J) I. L6 y, q, @% W3 Q
  4. >>> math.trunc(6.789)
    . ?/ _8 |1 e" `' I
  5. 68 t  z8 H! ~0 u9 C
  6. >>> math.trunc(math.pi), ^: k( d* m0 C2 y1 f; o& S6 U9 H
  7. 33 p1 ^) Q6 A. E; ?- o
  8. >>> math.trunc(2.567)  i, \( N* G7 @- j
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-23 09:54 , Processed in 0.078867 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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