新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

6 ~1 Z/ f; T* b5 E1 u* e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。/ A- r: u* U5 C: ~" E! A. V
6 V& G% a+ `  K. ~9 N
方法1
+ ^* S3 I  X& C/ Z8 f3 r
  1. >>> import math0 i  v. G: q0 `1 F
  2. >>> math.sqrt(9)# {% t! J( C2 t/ C  `
  3. 3.0
复制代码
方法2# Q. i- K1 n. {/ ~
  1. >>> from math import sqrt, P4 x# m( t" a
  2. >>> sqrt(9)
    0 k. l8 J% @+ t9 G. B3 U) {
  3. 3.0
复制代码

, h: }# M2 {* |# Q) H

) u' o7 X4 E0 e% y
math.e  表示一个常量
: c: x) \7 R, A5 y7 f
  1. #表示一个常量# n" B( |- m9 u2 z
  2. >>> math.e. W. B  I6 e) P! _0 X& P" L/ _+ s
  3. 2.718281828459045
复制代码
  u; q7 Q" G% ~* `
math.pi  
数字常量,圆周率
8 H7 n- U$ u3 K& K$ b7 C/ \3 L
  1. #数字常量,圆周率
    - g6 c) I- k, x4 P. d4 e9 y
  2. >>> print(math.pi)5 M' i4 M! J0 U. `5 N+ \
  3. 3.141592653589793
复制代码
# }, s8 m" v. H
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 G! Z( w+ [. L
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x- g1 E( Z; N' i1 L" \
  2. ceil(x)
    8 G! r. H5 a- R2 G" y/ f0 D! O; p
  3. Return the ceiling of x as an int.+ {- D+ }6 x) `
  4. This is the smallest integral value >= x.9 R2 P6 |2 B: q! ^. L) b

  5. 3 T. W6 d! O. ~- Z- F  ?) v8 Y
  6. >>> math.ceil(4.01)
    # Y; ?) e, h8 c8 N( L7 _6 O' O+ I
  7. 50 q) ]/ u; @: t) E  a1 ~( \4 c
  8. >>> math.ceil(4.99)& b1 F3 ?, n. e) r, t* ], K
  9. 55 {" M9 ?$ E+ {% W9 ]
  10. >>> math.ceil(-3.99)  s3 Y6 ]% u& G( P( h
  11. -31 _( f3 ?1 f5 j) O% e$ w
  12. >>> math.ceil(-3.01)
    0 x1 I1 e5 y- T
  13. -3
复制代码
/ b6 {2 |; ^/ f) V- {
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
" R3 {/ f1 m& G$ a  E4 A! Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身8 Q0 J2 M5 X3 I( G5 @
  2. floor(x)- d2 E" \& C/ ~! \  V- _& Q3 |
  3. Return the floor of x as an int.; k( w4 M6 d' @* l  \
  4. This is the largest integral value <= x.* n& f# w: O  A
  5. >>> math.floor(4.1)
    + y, |( @* b4 ^. c% w+ F
  6. 4
    - l% q7 ]1 Q$ e
  7. >>> math.floor(4.999)5 x3 U7 ?# Z5 ~  A# C: q' A
  8. 4
    9 q( Q+ q; B5 ^4 ]8 S1 C
  9. >>> math.floor(-4.999)1 N! y( v( z1 T1 {
  10. -5/ f0 H$ E* r7 u: o
  11. >>> math.floor(-4.01)
    9 R) c0 i% S/ u3 ~# [" n
  12. -5
复制代码
3 n& g- a* d' u0 u: p
math.pow(x,y)  返回x的y次方,即x**y
; m' w" K" j; Z- t9 i
  1. #返回x的y次方,即x**y
    " ?/ r) x  y7 D6 D% H- |
  2. pow(x, y)" ~) a" C- T. L" x' A8 w
  3. Return x**y (x to the power of y).1 n6 p0 P8 c$ G8 ^
  4. >>> math.pow(3,4)$ L2 ^* d; O8 |$ b
  5. 81.0; _6 {; h1 Q9 x+ a
  6. >>>
    : X0 t* B) e$ u, |% C( H
  7. >>> math.pow(2,7)+ k3 L! w3 N- B+ ]0 o
  8. 128.0
复制代码
" A' c, }8 r5 }7 Q8 s! F0 ^7 q
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)& k/ V3 Y: r) y- A
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 |/ d7 y( W- i* P! X
  2. log(x[, base])7 ~0 }. @. ]4 W1 v% q% K
  3. Return the logarithm of x to the given base.
    ! X/ s. T9 z; i4 a  A& r
  4. If the base not specified, returns the natural logarithm (base e) of x.
    ( u! t' ]. j. h8 U
  5. >>> math.log(10)
    & l+ r0 C3 f" N: K; _( S
  6. 2.3025850929940464 ]' `7 \  y+ W; G
  7. >>> math.log(11)
      K* y; e" z" }" Q! q3 k& N* }
  8. 2.39789527279837078 C# }) [5 ^  c. S7 h; N8 b
  9. >>> math.log(20)
    8 F) P9 _/ x) v/ _
  10. 2.995732273553991
复制代码
- x  V- f+ x* ~- X2 V% [0 k
math.sin(x)  求x(x为弧度)的正弦值0 Z- N: }  S) M5 \3 r% @* B! j
  1. #求x(x为弧度)的正弦值( A; Y! G- w6 }: d" w) y
  2. sin(x)7 U. _- B9 G" M, q& y
  3. Return the sine of x (measured in radians).
    # s/ i& y# p8 E' Y- ^1 N5 P
  4. >>> math.sin(math.pi/4)  ?: y% F* t/ }9 }. \, r
  5. 0.70710678118654757 R8 v4 {8 }3 g
  6. >>> math.sin(math.pi/2)
    ( F! [! A/ N6 P; W; E
  7. 1.0
    ) v. N) b+ e% R2 j  A
  8. >>> math.sin(math.pi/3)( u6 a1 |$ r( j' p9 x
  9. 0.8660254037844386
复制代码

$ D2 q6 n2 J; @$ p8 X, L/ \math.cos(x)  求x的余弦,x必须是弧度
- J7 C6 R3 Z. \, G2 ^5 Z
  1. #求x的余弦,x必须是弧度
    5 B" d, j- v$ u
  2. cos(x)" F( x6 y( ]( T1 h5 y* r3 t
  3. Return the cosine of x (measured in radians).
    ! E4 h& L3 p* i0 Q( T7 w% s
  4. #math.pi/4表示弧度,转换成角度为45度
    ' s# \% u0 w0 k7 ~- Q! k  a
  5. >>> math.cos(math.pi/4)
    ) M5 h  M; J# ~( Y: O. I
  6. 0.7071067811865476  u" ~5 l, Z! f2 Q+ b
  7. math.pi/3表示弧度,转换成角度为60度- Q5 f/ V5 t% o2 j
  8. >>> math.cos(math.pi/3)- Z. Q( K- ~* K' Y& g
  9. 0.5000000000000001
    " K6 B& E6 g6 n' q9 g
  10. math.pi/6表示弧度,转换成角度为30度7 j& W5 M  \0 z# B! p( e+ |, `
  11. >>> math.cos(math.pi/6)+ O$ t' f, U1 Z
  12. 0.8660254037844387
复制代码
6 O+ h; p* p/ h8 o  c, b2 W
math.tan(x)  返回x(x为弧度)的正切值
5 f' b* r' z" i: P' H! i6 u
  1. #返回x(x为弧度)的正切值
    ( J9 c# x: u5 r* d/ H  U
  2. tan(x)1 }$ H! n* z( U  Q1 S
  3. Return the tangent of x (measured in radians).
      ^) @# i7 E# v/ D
  4. >>> math.tan(math.pi/4)
    6 ^4 p! I1 d: z' x0 a4 Q
  5. 0.9999999999999999
      b+ M8 i2 @6 e/ t4 q
  6. >>> math.tan(math.pi/6)
    ) u6 U* B# d* ?& ^4 ]3 o# W
  7. 0.57735026918962575 ~& u3 _& l/ K6 N6 W
  8. >>> math.tan(math.pi/3)
    ; l( R5 L8 F; Q7 W
  9. 1.7320508075688767
复制代码

( _  v$ ]* f! |; L3 Fmath.degrees(x)  把x从弧度转换成角度
+ x5 r, b0 ?2 [( ~8 H; h
  1. #把x从弧度转换成角度
    6 L: ]# h# v$ v: C2 i
  2. degrees(x)! `! ~) Y3 U$ ~
  3. Convert angle x from radians to degrees., t# i, U) r# [9 B

  4. % e3 P1 \% \& h& x1 D* C# f$ ~0 J
  5. >>> math.degrees(math.pi/4)
    : z' s* l0 n5 d- W+ O# \+ D
  6. 45.0
    , V* e2 O7 l0 N, V& K
  7. >>> math.degrees(math.pi)
    + Q2 O$ K: U2 w# {. p$ t
  8. 180.0
      T2 s4 G3 i8 Y0 q
  9. >>> math.degrees(math.pi/6)
    2 I7 H3 A. ]# _/ O8 B: T
  10. 29.999999999999996
    6 X: I4 R7 d5 e( L; \: f
  11. >>> math.degrees(math.pi/3)5 c6 [8 z* a9 G7 f% R7 ^  K
  12. 59.99999999999999
复制代码
/ I* N& J4 ]! A; q
math.radians(x)  把角度x转换成弧度
+ G& U- W; k+ n
  1. #把角度x转换成弧度
    * r" r0 X/ p& J8 ^( g/ J: g
  2. radians(x)* X! j" `+ X1 A5 ~
  3. Convert angle x from degrees to radians.9 w1 q$ b6 F! ^) e: a) [1 V
  4. >>> math.radians(45). U* W+ @0 N8 v  G5 I
  5. 0.78539816339744836 w8 F- n# t* i+ g- }3 R3 u
  6. >>> math.radians(60)' Q! B3 K' Z8 p! p1 Z3 ]
  7. 1.0471975511965976
复制代码
4 r& [, `# k2 G. x7 M
math.copysign(x,y)  把y的正负号加到x前面,可以使用04 u. g( R: \* j
  1. #把y的正负号加到x前面,可以使用0# P8 _5 K4 U! l2 J
  2. copysign(x, y)5 f3 v" d6 f* D; j- ^' M. Z' `
  3. Return a float with the magnitude (absolute value) of x but the sign
    ; y: m9 h  W" B$ ^4 Y' n: D
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) + p' N0 r0 a: Y
  5. returns -1.0.: T* I: k8 \2 s& u! p) I% R$ f

  6. + T5 Q* T! ?7 x
  7. >>> math.copysign(2,3)
    0 M$ O. @+ Q% {  }& Q0 _; {0 Y
  8. 2.0
    ; H: C" ]6 ~( p
  9. >>> math.copysign(2,-3)
    ( w. `, V" e6 F( N
  10. -2.0
    ' i* J& q/ I7 C8 C, Q  J
  11. >>> math.copysign(3,8)
    6 F7 t0 E% |, I7 n
  12. 3.03 ]8 |- G. r) h0 f0 [
  13. >>> math.copysign(3,-8): q9 A/ g3 I7 E7 ~
  14. -3.0
复制代码
: A) W  W" E, \1 _* B
math.exp(x)  返回math.e,也就是2.71828的x次方: t: n* x8 A& F$ }  ^: \; s+ m
  1. #返回math.e,也就是2.71828的x次方0 Q. h. a, ]( S+ n3 O/ x
  2. exp(x)# O0 h0 ]% A" G# t) A& S  m* g) K
  3. Return e raised to the power of x.
    ; T& J; M3 u$ V4 d, a7 P0 ?

  4. & R. k0 F: W: [( d6 l* W
  5. >>> math.exp(1)
    & }6 n; D" ~( _, Z1 Q# `
  6. 2.718281828459045  H* e" u. u$ ~  |
  7. >>> math.exp(2)
    . C5 d1 o* _* N% i8 y
  8. 7.38905609893065& `- S+ q5 Z9 ^. B( k
  9. >>> math.exp(3)
    $ |0 ^3 v: ~, Y  P$ q: ^' [2 {" e
  10. 20.085536923187668
复制代码
' |3 X7 T! @( w- J
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减10 H, }- j9 E5 Z0 i
  1. #返回math.e的x(其值为2.71828)次方的值减1( _4 U) {' b& F
  2. expm1(x)
    . h8 N- _6 Z3 v- ~* c# u  p
  3. Return exp(x)-1.$ t6 I% K) G7 f2 a" a+ p+ a) O* \: d
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.+ t# B) u* b2 `: Z3 l4 I: L2 s

  5. + N4 I' J. W7 P" f& u6 z$ M" j$ i
  6. >>> math.expm1(1)
    & ~. l9 b% G& g0 r8 r2 W, \
  7. 1.718281828459045# O% a7 I) J" j9 r0 q5 y
  8. >>> math.expm1(2)8 ~$ [) N" C; @6 Q0 k: ^8 m. H+ _, N
  9. 6.38905609893065. E5 H9 R3 ^/ h# p5 |& A
  10. >>> math.expm1(3)$ `9 d8 h- M( e" A* n7 m4 o
  11. 19.085536923187668
复制代码
4 q9 x3 G& h6 ~" y9 Q- `8 `+ k8 z1 \
math.fabs(x)  返回x的绝对值7 _# P! O) O9 K4 }5 S" f( Q
  1. #返回x的绝对值9 S: R9 g4 H' `
  2. fabs(x)
    . f$ E5 X4 |  Z7 A8 \2 f7 P& k# `$ c) A
  3. Return the absolute value of the float x.
    0 W! Q1 ?1 i+ N8 z9 y
  4. * `; Y( ]% q/ M# @/ @% p) S
  5. >>> math.fabs(-0.003)
    / j3 M- J) v' D
  6. 0.003, {4 n3 e3 V' l& K* \
  7. >>> math.fabs(-110)
    " ?% I7 \# g; j
  8. 110.01 d& _2 w' K1 U# Z
  9. >>> math.fabs(100)! Q# V# R% X+ U& _* ~
  10. 100.0
复制代码
# k, Y5 }" b! R. t
math.factorial(x)  取x的阶乘的值
' e2 i* V' t9 P! I/ n/ U
  1. #取x的阶乘的值
    ( H3 Z4 a2 V0 i
  2. factorial(x) -> Integral- Y8 B4 \0 D# n" l
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    # p8 f$ y+ w: x: u  J& N: N
  4. >>> math.factorial(1)' a. }: I) @" l, c
  5. 1
    4 X! \( K5 V' W. Q
  6. >>> math.factorial(2)) d4 R0 b) M- X& y' L' {: v
  7. 2) f9 r- J4 `. ^8 g; s& V/ m5 H+ k  a) ~
  8. >>> math.factorial(3)
    4 M# _" i: N5 |4 I+ q) m1 x
  9. 63 C+ y+ ]0 k# t2 P# a
  10. >>> math.factorial(5)& V, `2 Z! E, [
  11. 1205 A# N  J6 J- a$ c6 R4 r
  12. >>> math.factorial(10)8 b/ U  v2 o5 a1 f8 d9 K
  13. 3628800
复制代码
1 P* i$ A; v1 R& o+ ?1 q
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
. _/ q4 d3 B3 @4 Y3 m
  1. #得到x/y的余数,其值是一个浮点数* z( Z0 a# |- S/ X
  2. fmod(x, y)
    " _2 O" f! D* A8 L( Z/ l( G6 X
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    $ c$ b' P4 L8 X) a5 d
  4. >>> math.fmod(20,3)
    6 H, V4 ?) F0 r( |/ p! K' a" J
  5. 2.0" T) J1 y$ N) {+ U( W& D
  6. >>> math.fmod(20,7)
    7 I& P% p- k2 Z
  7. 6.0
复制代码

6 X/ [: b1 p* t6 P! ?# j( c( Nmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 c9 P* E% B+ x) O, u, Q
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ F; m! ~  s$ b
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    / b& r4 Z8 u+ G2 h0 ^4 c' \% J% a9 |
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" \3 V/ ?% {7 d; R! w5 R
  4. frexp(x)
    ; o: R" n, V* q; M2 o$ l* t) m
  5. Return the mantissa and exponent of x, as pair (m, e).
    + Q" D! [- B7 o0 R. I
  6. m is a float and e is an int, such that x = m * 2.**e.2 P) _3 J" D. L' |4 _: n& F
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 r5 q4 @4 b5 _% P
  8. >>> math.frexp(10)
    4 }+ g  T( s/ ?1 o% e- j, [
  9. (0.625, 4)
    " l/ a( j, w4 D7 U" B, H
  10. >>> math.frexp(75)
    6 C& |; [, D9 c3 r4 ?  X0 ~
  11. (0.5859375, 7)) M* S7 W, X9 y" y( c9 L+ c. y
  12. >>> math.frexp(-40)# T$ g. f6 s8 j/ I: ^
  13. (-0.625, 6)
    ! q  w7 j4 w$ n- ^
  14. >>> math.frexp(-100)
    $ ?& g* X# R7 T4 M4 j; x0 K% [
  15. (-0.78125, 7)
    9 i1 S" T* _# p7 b* J
  16. >>> math.frexp(100)4 o5 `1 g% S" Q- p( u
  17. (0.78125, 7)
复制代码

1 A. j0 K2 B1 |1 @( c, l, k1 pmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
" U3 Y) }' }( a# @
  1. #对迭代器里的每个元素进行求和操作
    ( H) c7 w& L9 X2 A0 V8 D
  2. fsum(iterable)! Z- K6 m7 u& B
  3. Return an accurate floating point sum of values in the iterable.% U% t# r1 S: J3 ^0 L4 h1 S
  4. Assumes IEEE-754 floating point arithmetic.
    8 {. Y% G0 C- W6 y, N5 D
  5. >>> math.fsum([1,2,3,4])
    , a( J8 t6 C" w0 h3 }
  6. 10.0
    & V2 o$ U: B: G2 c
  7. >>> math.fsum((1,2,3,4))2 S  d; ~0 C6 x" {1 c$ ~, \
  8. 10.0
    / d/ W  x9 r& t0 d1 x
  9. >>> math.fsum((-1,-2,-3,-4))
    7 Q7 g1 p1 g( G3 N
  10. -10.03 h. q* j. O$ J& F, |9 |. B
  11. >>> math.fsum([-1,-2,-3,-4])
    / M( j8 z) B; a: x5 R4 O
  12. -10.0
复制代码
: Z+ O4 G* C! l0 A3 [
math.gcd(x,y)  返回x和y的最大公约数: V" Q' y- h- P# h2 Z  h; C
  1. #返回x和y的最大公约数/ f# d- ~! T/ h8 B" N( C
  2. gcd(x, y) -> int
    0 @5 V4 ?* b3 q% k
  3. greatest common divisor of x and y
    ( n; v7 Y: D& U
  4. >>> math.gcd(8,6)- w6 b- ^' C0 g1 y, {4 X# b; G
  5. 2& r/ a& N- ~& J
  6. >>> math.gcd(40,20)
    + {7 M5 ~4 k" R0 V% k: K3 ]
  7. 20
    ( ]8 f& t- j) B% a: O0 e1 Z8 R
  8. >>> math.gcd(8,12)0 y  Y& [: _  ~8 `7 K0 X! O
  9. 4
复制代码

! _  B2 w- @" Q4 C8 L! Mmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
7 [- m) n! J- d" v
  1. #得到(x**2+y**2),平方的值1 t" N5 h6 y8 Z, W
  2. hypot(x, y)# L6 b. \% [7 W7 H& h( G6 S  t
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    , R  ?3 x9 Z' Q: w. H9 d
  4. >>> math.hypot(3,4)
    0 ~, T. `+ J( U9 F5 u) _" N
  5. 5.06 K1 a) R0 Z; ?4 c2 w5 O
  6. >>> math.hypot(6,8)3 H" \; K% j. S7 ^4 j( p
  7. 10.0
复制代码
7 ?) _( E$ U; K
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 H( ~- |- y. `
  1. #如果x是不是无穷大的数字,则返回True,否则返回False9 y* P$ x) }1 S# Q' M* N7 M
  2. isfinite(x) -> bool
    " E7 c! W- G9 e  l9 G% j4 V
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ! Q1 `* k1 y4 ]9 U3 Q8 |& k; t' q. K( v
  4. >>> math.isfinite(100): Y6 Z3 }( ?5 L* G
  5. True0 N! a( |3 M' f0 y; C
  6. >>> math.isfinite(0)) N2 g1 {* ^! H
  7. True
    0 z4 k' \0 y! a' K- D  ]! Y8 y9 H
  8. >>> math.isfinite(0.1)( L* K( B- t( L9 _
  9. True
    $ p! y: ~1 O9 F
  10. >>> math.isfinite("a")
    8 a- I! p5 t) {( @" E8 b
  11. >>> math.isfinite(0.0001)
    1 ~1 R: E; {) {6 T9 K
  12. True
复制代码

- {% Q. }) G& _: \math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
; `) X. }  F; a6 a. ]0 Z8 m
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False1 O3 c8 \$ G* i6 Z! W& O
  2. isinf(x) -> bool
    " S# Q( R! z; O
  3. Return True if x is a positive or negative infinity, and False otherwise.
    $ f5 v" f6 Q4 C$ w# U; d- \8 A
  4. >>> math.isinf(234)
    ' L, v  r& x9 ?! G$ ?% m( ?
  5. False+ G# b1 j, l( U5 ^
  6. >>> math.isinf(0.1)6 p( c$ @9 M$ S4 x$ L
  7. False
复制代码
( [' S8 `' F6 ?4 b. d+ H* o4 {
math.isnan(x)  如果x不是数字True,否则返回False
" ^8 P) j% a8 g" ?
  1. #如果x不是数字True,否则返回False( P( p' a" n0 F6 d
  2. isnan(x) -> bool3 u% M3 q  y( d; B! W
  3. Return True if x is a NaN (not a number), and False otherwise.% v0 r9 A% ]+ P& G( V1 `$ F
  4. >>> math.isnan(23): V* O+ e8 o6 Q/ S3 d% a! O
  5. False
    5 S. |  S. |+ ]+ ~: z" g& w! w
  6. >>> math.isnan(0.01)
    : a2 L  d& [" X" |
  7. False
复制代码

7 Z5 \- i1 D  Q; U+ amath.ldexp(x,i)  返回x*(2**i)的值
9 y% ?% A0 ]/ F$ w, e
  1. #返回x*(2**i)的值
    ) h$ C, l1 K8 E4 d, i; {
  2. ldexp(x, i)6 z  g9 Q' d" K! i5 [
  3. Return x * (2**i).
    ' E9 G0 D. d* z4 b" z
  4. >>> math.ldexp(5,5)& i, p( f) c- j3 x2 p
  5. 160.0! E4 X9 c9 D( e- K
  6. >>> math.ldexp(3,5)' i! q7 m* {  c
  7. 96.0
复制代码

4 N: i! O: b6 s* B: G! I8 qmath.log10(x)  返回x的以10为底的对数
4 t7 ]* x" V! c3 o' |
  1. #返回x的以10为底的对数
    5 H' s0 A3 T  b/ S* z1 ^
  2. log10(x)
    + y  G$ V+ n2 k% r7 L5 L! N
  3. Return the base 10 logarithm of x.
    5 S: C! G$ ]" ]$ l
  4. >>> math.log10(10)- R, `6 j; t7 z% v8 C: n
  5. 1.00 C$ e: R" n$ s6 ~" f! s) M8 Q
  6. >>> math.log10(100)& p4 d6 O; q+ z' F
  7. 2.03 ~/ l. m) u8 z0 l# P! e/ C
  8. #即10的1.3次方的结果为20
    . t# h# z9 J" d
  9. >>> math.log10(20)
    . H) K3 ?, H, m, S  `
  10. 1.3010299956639813
复制代码
9 n( V) f, P+ J
math.log1p(x)  返回x+1的自然对数(基数为e)的值
% j' [/ m% {5 }% H1 ~! D* U% v
  1. #返回x+1的自然对数(基数为e)的值
    6 C  s4 ^( Y5 x4 [! W6 q. n5 p
  2. log1p(x)
    4 N: n2 C. H, @1 N
  3. Return the natural logarithm of 1+x (base e)." h) z+ U% Z5 z6 T! j
  4. The result is computed in a way which is accurate for x near zero.
    * _4 u% \6 n  p( i
  5. >>> math.log(10)
    9 {% b0 w7 i) u; R
  6. 2.302585092994046
    ; ]* K8 @: Y5 M
  7. >>> math.log1p(10)
    # v8 U" c- k# A3 z% m$ v
  8. 2.39789527279837076 V# H/ d) y6 h1 J3 E! g* Q
  9. >>> math.log(11)
    % W9 p; M0 R. L6 T1 H
  10. 2.3978952727983707
复制代码
% ~( G+ z+ O6 ]/ f5 n$ j% b
math.log2(x)  返回x的基2对数: K- Y+ F( o  @% l0 N. L3 C5 X
  1. #返回x的基2对数
    2 R6 }& Z7 c4 S! w& j
  2. log2(x)
    + l: a9 C! i& ^% s9 A* d
  3. Return the base 2 logarithm of x.
    $ J  ?: |; K0 O( @4 |
  4. >>> math.log2(32): l, t% Q, P0 K2 Y6 j$ y
  5. 5.03 [- h/ w1 O- d- e
  6. >>> math.log2(20)
    / r/ P, I- P9 z( U8 B: F0 R
  7. 4.3219280948873632 g. q- T+ `" l* G( f
  8. >>> math.log2(16)
    9 Y) k0 ^$ Y5 J0 R! d2 H
  9. 4.0
复制代码

$ L* I& j. D  X; o4 Umath.modf(x)  返回由x的小数部分和整数部分组成的元组
; |, O! \3 h+ h2 r  B# o6 [% L
  1. #返回由x的小数部分和整数部分组成的元组0 `# i' J; V  m, G# ?
  2. modf(x)
    , M; T. T6 c0 E+ K4 `0 `
  3. Return the fractional and integer parts of x.  Both results carry the sign
    9 G' s! E8 ?4 Y1 _8 P% Q
  4. of x and are floats.( P( V* z  E) `% X5 y. `, C
  5. >>> math.modf(math.pi)3 F' S$ ^; }: a
  6. (0.14159265358979312, 3.0)& I/ T4 e6 c1 @4 Z# Z/ a" ~
  7. >>> math.modf(12.34)$ b$ X4 \0 h1 K3 w( t& ~$ D
  8. (0.33999999999999986, 12.0)
复制代码
9 o1 A& a4 ~* p& h( |
math.sqrt(x)  求x的平方根+ b6 ]5 V; ]* n  _: R% z6 E9 N
  1. #求x的平方根
    $ x) W$ F. ?. U( s; s% y, M
  2. sqrt(x)8 ]: D! k3 k7 Y4 i+ H' h" c/ x
  3. Return the square root of x.
    2 }/ k9 l" z" F1 d6 M3 B1 l  \
  4. >>> math.sqrt(100)
    : @( v2 G% h" ^# J" |/ @
  5. 10.06 T7 e7 @& N0 H8 r' g8 X
  6. >>> math.sqrt(16)7 S# y: I$ z7 A/ [$ q( e. A& U
  7. 4.01 X4 u8 ]& D- y7 I1 o
  8. >>> math.sqrt(20)7 d* j# ~. c/ a; r. X% ~2 |5 L
  9. 4.47213595499958
复制代码
, T# E5 J: x2 o
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分+ h  V8 E( a8 e. U: y7 d5 q
  2. trunc(x:Real) -> Integral
    - n, E, Y; _' w) z$ K
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    . G8 i+ Z' e$ H
  4. >>> math.trunc(6.789)
    ; ?$ A* F3 C! H8 u5 A/ o6 s
  5. 6
      I' m" Q8 t0 \
  6. >>> math.trunc(math.pi)) D5 [1 `" w- k
  7. 37 X5 b# ?9 f) {  k# ^
  8. >>> math.trunc(2.567)- x/ ]& [$ h* J' L% z
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-17 22:01 , Processed in 0.077793 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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