新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
9 a7 k8 w  y5 D- K" B4 }
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。# l1 n3 \# i. j6 n

1 ?% }8 p+ X+ K% @0 Z% A方法10 u6 c  L, p) L
  1. >>> import math
    5 V) k  r( h5 a* i% f1 Z0 A3 G4 h
  2. >>> math.sqrt(9)# Y) M3 P5 S' J% s% p/ I
  3. 3.0
复制代码
方法2& y% O7 C0 ?. H2 w
  1. >>> from math import sqrt
    / i. @# D/ z8 c( E9 }8 n
  2. >>> sqrt(9)
    . _$ H$ R8 S7 `: V& m' H
  3. 3.0
复制代码

" k2 F4 j8 z1 V& a! R( Z

8 T& Z, f: v# C& I
math.e  表示一个常量; A0 _2 Y. t4 x7 p; }5 c
  1. #表示一个常量
    # [! A( S. N' `0 E  ^3 ~" X5 D) d
  2. >>> math.e/ ]: {) V& S* B
  3. 2.718281828459045
复制代码

# G% e6 C5 w0 |' i( C5 D2 umath.pi  
数字常量,圆周率

) c) Z( n' G  j1 ~: P5 q8 S
  1. #数字常量,圆周率
    1 t6 S9 Z$ j: }' f8 g
  2. >>> print(math.pi)
    " A. |6 }. h0 K, X, Z  J( R
  3. 3.141592653589793
复制代码
3 a# K: O4 S9 a" c0 S- I5 G
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

. ~& _! K) A! Z& R  `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x7 U3 T% a7 N, Q
  2. ceil(x)
    * Q. U1 `0 B7 `
  3. Return the ceiling of x as an int.) H) p9 l8 A, {5 q
  4. This is the smallest integral value >= x.8 ~4 @8 H2 e/ E, b( ^# P  v+ @
  5. 3 x6 }4 g: a+ N# j
  6. >>> math.ceil(4.01)
    " E% L" O, G2 A$ y1 o8 n( {, {
  7. 5' J+ H# I0 J5 z
  8. >>> math.ceil(4.99)' o& H: Y2 A+ e- l. ^" L. S- W
  9. 53 U0 \- ^# O. J* F0 f1 V
  10. >>> math.ceil(-3.99)
    6 |2 {6 d2 E' L6 O- X: F
  11. -3( y* ^! I" K$ i( R3 f# R9 P' R7 s3 W
  12. >>> math.ceil(-3.01)
    + d, O: e6 K: c- E1 Z5 @! r
  13. -3
复制代码
$ J- y6 `) q1 ]9 k7 a' |2 z7 K
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身& w; C+ X* }+ x# n
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 L4 ?( ]* X6 a5 I9 y) Q$ f2 r- W9 f
  2. floor(x)
    8 x/ E: y% y  ?& B
  3. Return the floor of x as an int.
    0 L$ w2 }/ F% N1 o' x) U4 P7 q$ K
  4. This is the largest integral value <= x.8 N* _  T9 R' I" z9 X# V
  5. >>> math.floor(4.1)
    $ W/ O7 A+ g, a3 ?" J$ v  O) m
  6. 4
    : G9 l( g7 v% s, e& C
  7. >>> math.floor(4.999)8 p1 x# @8 l* @4 y* D
  8. 45 {/ k0 E# I; q* v
  9. >>> math.floor(-4.999)
    " C# [, i: n- U! L8 H! Z. F# U
  10. -5, @# b/ o( b2 {: R* H6 V- e. |
  11. >>> math.floor(-4.01)$ x% q4 }2 t" Z$ H2 n0 ?
  12. -5
复制代码
! R( o( _0 [4 g" U0 @8 x" m' I+ s
math.pow(x,y)  返回x的y次方,即x**y2 z- L5 P! W, T8 W; n, q
  1. #返回x的y次方,即x**y
    8 I$ A! K: d6 \6 ~7 z; v7 G
  2. pow(x, y)+ f+ v$ Q% s6 O8 d: _* G( N- `5 R
  3. Return x**y (x to the power of y).3 |4 \/ v2 o" t$ c" f. Z, n) N
  4. >>> math.pow(3,4)
    # D4 |. E( |1 K! S
  5. 81.0
    * E. @# h0 _# U- R& K+ h5 F9 e
  6. >>> ( C' {& B0 Q  T
  7. >>> math.pow(2,7)7 y( X! [; D: i; b( m: I
  8. 128.0
复制代码

% {/ I, r3 Y4 q* g7 g8 mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 `' F& V- J5 \/ r: D% Y; ]
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ! S' z' N+ Y4 n) o
  2. log(x[, base])/ Y, g# o4 U7 k6 o; G
  3. Return the logarithm of x to the given base.
    2 I! \$ k. U) W) q5 e) `8 y/ e
  4. If the base not specified, returns the natural logarithm (base e) of x.1 B1 E) S' [4 T3 G+ T% T8 n( a3 {
  5. >>> math.log(10)1 W. o3 `  `! P0 P- c
  6. 2.3025850929940467 \8 F+ W: X  A( B9 u: s' ?9 W
  7. >>> math.log(11)
    - ~! L3 [3 }8 n* q
  8. 2.3978952727983707
    2 {% _( L$ o$ K# ?# M
  9. >>> math.log(20)
    ! E; n8 H! {, g7 F3 {! ^( W, e
  10. 2.995732273553991
复制代码
/ g3 j5 J+ z  x2 R0 F" s
math.sin(x)  求x(x为弧度)的正弦值) K4 z; \# t1 N3 ?0 q$ Q8 Y) Z
  1. #求x(x为弧度)的正弦值) [+ U! v% X) Y# `' s  K! M, U# U
  2. sin(x)& ~2 W% ~; K7 H  l: f3 B, ~8 ?
  3. Return the sine of x (measured in radians).
    - ?0 g$ O, r8 e3 ^" j( P: X
  4. >>> math.sin(math.pi/4)7 X& F% z" I, a& D: F# l% K5 o
  5. 0.7071067811865475
    8 s( P- Y: {/ V$ M
  6. >>> math.sin(math.pi/2)7 F! j& k" k0 H9 \" U3 h6 z
  7. 1.09 o" b; }( Q; z( O0 X
  8. >>> math.sin(math.pi/3)$ |9 c! b' ?) ~8 M
  9. 0.8660254037844386
复制代码

" L9 T% Q: _7 _! d5 @6 J1 o* amath.cos(x)  求x的余弦,x必须是弧度
& _, x6 M6 q2 |( h
  1. #求x的余弦,x必须是弧度
    4 X7 D' K0 j9 Q
  2. cos(x)
      F# J& d& N+ V4 l9 \
  3. Return the cosine of x (measured in radians).2 a% D" A" ^2 z4 i) ]1 W
  4. #math.pi/4表示弧度,转换成角度为45度
    ( }0 q/ }! X0 |
  5. >>> math.cos(math.pi/4)
    ) |, E: {1 ?1 ?5 f7 p( `: |2 W2 Q
  6. 0.7071067811865476
    ) c4 Q' B4 U! N: Y' I7 [" y% v
  7. math.pi/3表示弧度,转换成角度为60度: T$ I, v8 L1 E  o% X
  8. >>> math.cos(math.pi/3)$ s/ v+ I( S3 ^! E
  9. 0.50000000000000016 H. E! {8 F. p$ ?4 i& l0 @
  10. math.pi/6表示弧度,转换成角度为30度
    , L9 V1 b  }. t' ^9 v: @$ x
  11. >>> math.cos(math.pi/6)
    7 x! `1 {& B# ]1 Z1 g
  12. 0.8660254037844387
复制代码

) |) E2 z$ h+ U3 u7 vmath.tan(x)  返回x(x为弧度)的正切值
. L; C1 j  j5 h1 Q- T+ T) \6 h
  1. #返回x(x为弧度)的正切值
    ) ?* Y% l3 h- |( G3 y7 ^, I4 V! \
  2. tan(x)$ n! B% f( n% X- v( k2 |5 P& {
  3. Return the tangent of x (measured in radians).
    $ i$ X1 M& O' N2 W0 F: |2 `7 V) _
  4. >>> math.tan(math.pi/4)
    9 s' k( g4 c8 B9 U) N0 m* e6 P$ O
  5. 0.9999999999999999
    5 P) z7 c$ o' T
  6. >>> math.tan(math.pi/6)1 X2 ^8 H" j7 _6 K% T# l/ e6 m& b
  7. 0.5773502691896257/ z4 h  T  K* k1 q$ N. s
  8. >>> math.tan(math.pi/3)
    ; i8 l2 p/ b2 d7 ]1 K# M
  9. 1.7320508075688767
复制代码

! `- q2 S# b' b1 wmath.degrees(x)  把x从弧度转换成角度
/ P! Y+ k: L! a+ N6 h( t9 |
  1. #把x从弧度转换成角度* R& a! ]0 ^: K. P0 M, N- }
  2. degrees(x)
    : n! y- C0 d0 ~4 S7 v: |; i' I
  3. Convert angle x from radians to degrees.
    + I' u$ a' S7 q( Q5 `. U- Q+ |

  4. ' {  }! N% J! t0 j, H$ D$ R
  5. >>> math.degrees(math.pi/4)
    $ ?* n" o" u3 w$ A( Z
  6. 45.0- T; z8 q. s1 e0 X4 C5 a
  7. >>> math.degrees(math.pi)! z/ x& `) ~$ l8 Z0 F+ _
  8. 180.0
    - |. l2 a4 y" W* R
  9. >>> math.degrees(math.pi/6)8 ?/ r5 \: v6 O3 c; s( \& `7 `1 g
  10. 29.999999999999996
    % ?( ?4 I# z$ B) B% ~  M2 k" n3 u8 @
  11. >>> math.degrees(math.pi/3)
    8 o9 D$ q$ v5 A$ [" G
  12. 59.99999999999999
复制代码
* m4 y3 D; g+ Z& y- r- d( o
math.radians(x)  把角度x转换成弧度
! d: n; B( {/ l8 \9 O- Z% W6 ^
  1. #把角度x转换成弧度
    * H) g, c0 m: @
  2. radians(x)
    # }- F: T! u9 D7 B  R' R1 x( t
  3. Convert angle x from degrees to radians.* R9 \1 Z- G4 V0 M2 t6 y
  4. >>> math.radians(45)! r. N% X7 e6 n9 @8 N
  5. 0.7853981633974483
    - u  _* v+ P; l9 g+ h1 b6 _
  6. >>> math.radians(60): d3 |- A3 L2 B& e4 V4 ?
  7. 1.0471975511965976
复制代码
6 R7 B. f. b' W; e
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
! l- K. s- _" `. U* x2 B; E- u
  1. #把y的正负号加到x前面,可以使用0
    + Y" Z+ \/ \7 [
  2. copysign(x, y)
    ! Q$ D8 Q9 r0 {- ?( ]* U3 ^: y+ F6 `/ }* S
  3. Return a float with the magnitude (absolute value) of x but the sign ( R$ p/ Y" v/ U# }/ H% `& W8 g
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ) T2 g' Q8 I* f# c: ]2 p/ a
  5. returns -1.0.
    # P4 x6 }+ J8 T7 k* U
  6. / c2 g2 C9 i+ V. T2 V
  7. >>> math.copysign(2,3)! Q/ n8 h4 p9 [/ l
  8. 2.09 L* u$ I1 i+ U, V' H
  9. >>> math.copysign(2,-3)
    2 @+ u8 I# j/ |; K/ V2 ]# G
  10. -2.0
    . b, b( l: ^& U  c. D7 K
  11. >>> math.copysign(3,8)2 d+ l: l# X, [, E2 I) g* P
  12. 3.0' ^* i! c8 E9 @
  13. >>> math.copysign(3,-8)4 I3 T1 e9 E2 }1 y4 \
  14. -3.0
复制代码

+ S0 P- J2 ?0 T7 K7 vmath.exp(x)  返回math.e,也就是2.71828的x次方0 v/ ?: H9 n' s/ h% j- z1 B
  1. #返回math.e,也就是2.71828的x次方
    % W8 b+ Z* b- X# W! _
  2. exp(x)
      }' V$ w, C1 d  T7 W
  3. Return e raised to the power of x.
    + Z. L0 V2 L3 b/ n: N
  4. - W. \6 l# o; y! y! {& q- m; O; l
  5. >>> math.exp(1)
    2 B' l) J6 y: R# F  ^' w# m
  6. 2.718281828459045
    # s% F0 Q1 D% c( h3 c" G
  7. >>> math.exp(2)
    ) V9 w& z0 i& G! [$ c
  8. 7.38905609893065
    * e7 ^; D& u7 |
  9. >>> math.exp(3)3 {8 |% i3 u) P+ X8 j/ U
  10. 20.085536923187668
复制代码

8 P7 E' h% y. x* P* t0 x7 Gmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# z% T" c, P9 `3 `( I) f
  1. #返回math.e的x(其值为2.71828)次方的值减1! u/ b  X7 u- _' W% {& e  J
  2. expm1(x)
    . q) a+ M& `. g- C2 k
  3. Return exp(x)-1.  ^9 T. Q: Q  L! f- ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    # _) W) t4 a+ h2 `

  5. ! J4 H4 d4 ]7 y- C& ?* i- Y
  6. >>> math.expm1(1)
    ( r4 b% w- s4 y) L+ i+ b$ p
  7. 1.7182818284590459 b2 s- D6 a2 O# T
  8. >>> math.expm1(2)3 n" C+ Z, N+ r; }& ^2 {, m
  9. 6.389056098930650 `- k- x! h3 r, u
  10. >>> math.expm1(3)5 l4 t. H1 a* k- u; t  B' Z8 n& J
  11. 19.085536923187668
复制代码
: p9 k6 D( X; x3 s
math.fabs(x)  返回x的绝对值" M" r$ }0 P" U' @
  1. #返回x的绝对值& a+ E7 t. N9 q7 [
  2. fabs(x)% v& Z9 O% p4 D2 z. d0 ?& X
  3. Return the absolute value of the float x.2 V' d# Y  ^0 u* U( q
  4. , \3 h* E6 Y: p0 [8 j( _
  5. >>> math.fabs(-0.003)
    6 h3 @# M4 Q! S7 f5 }- \! ^: V
  6. 0.0034 f+ j$ X  Q/ z: ~! c: n# v8 s
  7. >>> math.fabs(-110)
    . n% i2 e1 a& M+ c2 I. i
  8. 110.0
    / t1 P! Q1 _0 V( g, R; z
  9. >>> math.fabs(100)
    : U2 {, ?% u9 u9 W: J! D4 @
  10. 100.0
复制代码

( I5 Q9 w3 d! `! _math.factorial(x)  取x的阶乘的值/ L* L$ e, K- E' E/ W7 z
  1. #取x的阶乘的值  S4 U, Q# u$ W0 B: y0 _
  2. factorial(x) -> Integral. a& p5 n* h7 f! e
  3. Find x!. Raise a ValueError if x is negative or non-integral.! Z( n; H) ?: A& G( K
  4. >>> math.factorial(1)
    , y7 o4 C& i, c2 q! g
  5. 10 L( ^4 L& I' n. w+ q1 h) T
  6. >>> math.factorial(2). \/ B  [" ?2 S, z
  7. 28 w; u" I' c& l+ b! x0 E
  8. >>> math.factorial(3)
    ( G) k3 T* I- W
  9. 69 [  L8 z9 R/ ]* I
  10. >>> math.factorial(5)
    3 d' T! P; _( y/ B1 w: b- `
  11. 120
    7 t2 Y3 n- c/ \, g
  12. >>> math.factorial(10)" K6 o: C: ^  ]  w% q% M+ }
  13. 3628800
复制代码

2 H( M  W- h: J- j8 X1 Omath.fmod(x,y)  得到x/y的余数,其值是一个浮点数5 P( l/ R) W9 c6 A$ o8 @4 L
  1. #得到x/y的余数,其值是一个浮点数
    3 P) K8 G3 N3 I! O* [
  2. fmod(x, y)' D: ]' }# a% s5 e" N
  3. Return fmod(x, y), according to platform C.  x % y may differ.
      P: ?4 _8 E: I) {& A% U
  4. >>> math.fmod(20,3)1 C8 M) D* _) L3 o4 n0 J
  5. 2.0
    8 t) p( r1 V. L- y4 \4 l) [& x- K
  6. >>> math.fmod(20,7)5 F9 |5 P' w' X. d9 Q
  7. 6.0
复制代码

$ r' G! v  k$ x) ~6 pmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围5 _5 C' }. D. \6 y
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,: D% A7 b0 g1 c/ ^  P: Y
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' p0 N' O+ {# @! S% ?* ?
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1/ P  C5 e7 W) b4 d! j/ F! [/ o
  4. frexp(x)+ W, K1 v+ b- _) p6 ^
  5. Return the mantissa and exponent of x, as pair (m, e).
    ; p" f& N) Q0 t- g7 O) k2 I) v
  6. m is a float and e is an int, such that x = m * 2.**e." p" f* R( S# j' z  }
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 b: r7 C5 _6 F  ]
  8. >>> math.frexp(10)
    5 s# M* n3 R3 A
  9. (0.625, 4)
    $ s3 T, O. Y+ O5 }
  10. >>> math.frexp(75)" g8 u4 A( h% x; `
  11. (0.5859375, 7)
    . U+ i7 }! a8 ^8 s& D4 W
  12. >>> math.frexp(-40)0 @5 w0 q; x- o
  13. (-0.625, 6)
    & ]2 o+ F6 Z8 b; _$ c
  14. >>> math.frexp(-100)
    6 d2 M' f$ H* Z' ?4 v/ \" o# ^
  15. (-0.78125, 7); F; S$ z) J2 a: `8 K# V& ]- D  A
  16. >>> math.frexp(100)! M7 I- g2 G. |4 M: X
  17. (0.78125, 7)
复制代码
  p/ z3 V  l$ z9 e4 w& b7 I
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列1 R  ~4 L" \- \/ Y0 g, R
  1. #对迭代器里的每个元素进行求和操作: y% l) H7 n+ s- R- E! R
  2. fsum(iterable)
    - m/ ^  y& U( E1 O
  3. Return an accurate floating point sum of values in the iterable.% q+ V& R6 s- e; Z" _) e
  4. Assumes IEEE-754 floating point arithmetic.
    8 y7 v& \/ z  Z$ y8 v7 p; r
  5. >>> math.fsum([1,2,3,4])
    $ v% l: k. ^* P" l8 i: C2 q8 t
  6. 10.0  U0 u( V7 ~. i2 o2 T' x
  7. >>> math.fsum((1,2,3,4))( ]9 {( M! S! C1 q5 x
  8. 10.0
    ; A% J+ t' p7 z
  9. >>> math.fsum((-1,-2,-3,-4))
    ! k) g/ Q- W# F5 \, E7 h* G
  10. -10.0
    / q) @" T/ T! d: H
  11. >>> math.fsum([-1,-2,-3,-4])
    / E& G, f4 J7 @$ ^
  12. -10.0
复制代码

; Z2 B- X9 O2 |/ kmath.gcd(x,y)  返回x和y的最大公约数
) e( h; O- D+ l( O# |3 b
  1. #返回x和y的最大公约数: l( Y# S$ u4 D6 I" [2 G
  2. gcd(x, y) -> int
    + e/ z1 D% u' d7 T5 j# G) X
  3. greatest common divisor of x and y
    9 E' g) B( i, l, J
  4. >>> math.gcd(8,6)
    0 g5 u* G1 R/ Q  ]
  5. 23 @% C3 {" Y2 ^, U9 [6 K( Y
  6. >>> math.gcd(40,20), j, Z9 g5 Q; ?$ D+ D" O
  7. 204 ?! ]' g  f7 I2 i
  8. >>> math.gcd(8,12)
    ; G0 P! F( Y0 p
  9. 4
复制代码

% ]. {9 O# J0 [- V; Wmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
8 _: w+ i' |" j4 z
  1. #得到(x**2+y**2),平方的值. s- E* t; p5 @+ Q; \0 w2 z, F
  2. hypot(x, y)
    , v: j5 `& U! p3 d. C
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    # y# v, ?- A+ U- x6 @; @  |5 Q
  4. >>> math.hypot(3,4): H4 Y6 Y& a' l% K$ |: T  Y
  5. 5.0
    2 o4 g  }( G& f* h
  6. >>> math.hypot(6,8)7 i) m' Y! c3 a& h# O; q3 b3 _9 N/ Z
  7. 10.0
复制代码

0 n1 \" s8 f; Amath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False7 V: u, u$ s5 [# `5 ~! B! [2 v
  1. #如果x是不是无穷大的数字,则返回True,否则返回False. ?, [0 q& ~4 a
  2. isfinite(x) -> bool; J, q0 o) @& r7 o
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    9 R/ |5 v* c) ^4 ^4 p7 ^2 j, G6 u
  4. >>> math.isfinite(100)
    1 s2 t7 `+ k. |0 h
  5. True# p' c# P$ n' G; x  h. a( m
  6. >>> math.isfinite(0); F: z* x& N7 F2 I. O
  7. True7 v2 O& k4 a& O5 f
  8. >>> math.isfinite(0.1)
    0 x1 e6 n2 a. K3 x
  9. True% R. L' e$ x6 Q6 @6 A& f8 W) a; ]
  10. >>> math.isfinite("a"); m0 S, W1 O$ m/ m  _. l, K$ [. g
  11. >>> math.isfinite(0.0001)7 ?: f" g) U2 ]2 `8 B3 [+ h( S& D
  12. True
复制代码
+ d& Z5 r1 K/ g' H
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
. d% c, f  V* A$ f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    . H8 @. ^! B5 @; o  |  o2 l
  2. isinf(x) -> bool6 @$ Q- o, I9 H+ j3 j* O$ F
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ( m0 i8 {! P2 @
  4. >>> math.isinf(234)) F' \$ I4 `3 R, z
  5. False4 j% |, k0 `+ a# L0 q  B( z
  6. >>> math.isinf(0.1)
    4 c7 ^/ {2 v1 y, j, o
  7. False
复制代码

, A( m' @& w( I& dmath.isnan(x)  如果x不是数字True,否则返回False. @1 t# i) E" Y! `$ c9 N& Z3 s
  1. #如果x不是数字True,否则返回False& i* S2 W  [  \* A
  2. isnan(x) -> bool+ b- a; q# ]$ C2 Y$ I
  3. Return True if x is a NaN (not a number), and False otherwise.
    " R2 _* U$ r; o3 d" s5 P5 m
  4. >>> math.isnan(23)
    : [" A  B: R; `" @6 i
  5. False4 A" D* q% y" ?6 a) @
  6. >>> math.isnan(0.01)
    * y0 u* M" q! \1 e
  7. False
复制代码
5 F& d2 n1 Y  f* ?3 G; N) J8 {0 Q2 G- E
math.ldexp(x,i)  返回x*(2**i)的值
3 N2 z$ u8 n! D  ~& I6 U( u& @4 |
  1. #返回x*(2**i)的值2 t# y  g8 W& d' y* O. M+ A
  2. ldexp(x, i)
    : e0 l+ p8 ]4 j
  3. Return x * (2**i).# @- x# Y# \7 G- j8 q! x" M
  4. >>> math.ldexp(5,5): }, Q, b" s) M8 Y& u9 `
  5. 160.0
    # y8 Q0 H0 R. h1 r, U% w- z
  6. >>> math.ldexp(3,5)
    $ o. b( F6 ?4 w7 L: N9 f, p% J' [0 a
  7. 96.0
复制代码

$ b/ B2 x8 C" b) y2 s' ?" ?5 Cmath.log10(x)  返回x的以10为底的对数+ y" t% Q5 Q, Y  o7 i: {- L0 a
  1. #返回x的以10为底的对数
    9 l, i8 h/ ?  S3 O8 I1 p. [% C
  2. log10(x)
    / F7 K8 q/ }1 I. G/ |+ `- S) c
  3. Return the base 10 logarithm of x.# s) a4 ]% M7 p! h" J  B
  4. >>> math.log10(10)9 M& m. L: e. @
  5. 1.0
    , ]! c/ Q3 Y* ~7 V
  6. >>> math.log10(100)
    3 p6 J; n4 r4 `, N9 X, Z
  7. 2.0) r) d7 D0 V) D, E* P( v' b* K
  8. #即10的1.3次方的结果为20
    1 o0 o$ Y# Q- \+ @. U
  9. >>> math.log10(20)6 |7 E4 X2 N$ b4 T
  10. 1.3010299956639813
复制代码
/ U( O8 l! O3 q( p: l+ R
math.log1p(x)  返回x+1的自然对数(基数为e)的值- G9 M. p- h! o5 z
  1. #返回x+1的自然对数(基数为e)的值
    3 H4 D4 i& s# {& m& G
  2. log1p(x)
    - d. B9 }3 K1 I5 S0 C7 c. x$ R
  3. Return the natural logarithm of 1+x (base e).- p/ H7 @9 ]' ?* D; w/ M5 Z6 ]
  4. The result is computed in a way which is accurate for x near zero.
    ) ~- T# C$ l0 Z' ~+ @
  5. >>> math.log(10)$ J' l8 Y+ A1 H3 w
  6. 2.302585092994046% O% L, \% V) Q' d: e3 u% A7 \
  7. >>> math.log1p(10)
    ( `$ O* v& R* E' W: g/ d- [
  8. 2.3978952727983707
    / e9 Z( Y9 Q5 @* N
  9. >>> math.log(11)2 O/ q: q1 P9 Y/ y% N; Y( C
  10. 2.3978952727983707
复制代码

+ O0 F+ q4 `3 hmath.log2(x)  返回x的基2对数- j# m" P+ }: a% o: M
  1. #返回x的基2对数/ u! V! s( o# l, Y* v
  2. log2(x)
    , s6 K! h5 J: d: u" I! F
  3. Return the base 2 logarithm of x.+ }7 d' F# A9 ~* i3 }
  4. >>> math.log2(32)
    " V9 r% o: {' c1 X
  5. 5.0
    0 D4 s) R  w* H9 m
  6. >>> math.log2(20)
    " c2 j$ c$ Z* P9 G' R9 B, Z
  7. 4.321928094887363
    - Y/ b7 M. Y: f
  8. >>> math.log2(16), E9 t% N$ i% N( A6 j* s% f9 a! [
  9. 4.0
复制代码

1 W9 ^# b8 X& R' Mmath.modf(x)  返回由x的小数部分和整数部分组成的元组
" V8 K. G. ?6 h0 X4 d% c" B2 L& r
  1. #返回由x的小数部分和整数部分组成的元组
    - l  c7 ]! r0 k/ S
  2. modf(x)
    , V5 w& [. V4 }2 B
  3. Return the fractional and integer parts of x.  Both results carry the sign' v% W4 ^& j* a; J  r2 V9 P1 d& H
  4. of x and are floats.
    . I; Y  {' l( L/ H
  5. >>> math.modf(math.pi)! R7 f- K5 G- u* z7 {
  6. (0.14159265358979312, 3.0)0 e/ F  k% j8 D
  7. >>> math.modf(12.34)
    1 W8 }1 Z0 {7 F1 L& Q( _
  8. (0.33999999999999986, 12.0)
复制代码
* f$ o3 D0 A# [: C
math.sqrt(x)  求x的平方根- @: T9 h4 g/ w$ |; W0 J2 n/ ^
  1. #求x的平方根
    ( f0 z/ r+ b( L8 ^9 \: }
  2. sqrt(x)$ ~2 X) \! \. q$ d2 N
  3. Return the square root of x.
      Y# @& p  D% B* j( ^
  4. >>> math.sqrt(100)
    ! b, v; \# _7 O8 W; c
  5. 10.0( w& y1 p  V1 U3 Q# Y
  6. >>> math.sqrt(16)
    ! p% s! P) }; R3 }
  7. 4.0) ~& C) c$ H( h7 q
  8. >>> math.sqrt(20)
    ' o# C$ M8 p# ?9 i9 K9 g
  9. 4.47213595499958
复制代码

6 f! P* _3 \- d7 ]) l. s8 |math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分/ O% ?% n: Q; O# p
  2. trunc(x:Real) -> Integral
    5 `  G$ X' I  M3 Z) x8 n6 N3 [2 }
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    5 R* w' c# u! t# `# S
  4. >>> math.trunc(6.789)3 v. }% T1 K. H. K. o& |! i
  5. 6
    ! Z4 v& P6 q4 y# S" r
  6. >>> math.trunc(math.pi)
    6 g. y' \, y' q* M6 M" r4 `
  7. 3
    9 G6 l7 ?3 I  r  |6 P
  8. >>> math.trunc(2.567)- ?( r( @- t/ l
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-5 15:36 , Processed in 0.086885 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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