新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
- F- \# Y" Y5 @: F5 h. H! c- k
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" }5 m' L$ d  K% h
5 n' u! N! g7 ?5 O8 N; d
方法1% r5 x$ D6 N; n' j! e2 a! j3 C, ^1 I
  1. >>> import math0 n2 r! ~6 ~2 y: i: A0 L5 A+ Y: {
  2. >>> math.sqrt(9)
    6 t0 K/ N( _1 Y
  3. 3.0
复制代码
方法2
& H. }# g9 v" r& k8 l8 a) B% v
  1. >>> from math import sqrt8 o+ h  [; l+ h0 Y
  2. >>> sqrt(9)+ d5 L* Q# R; X( n5 b/ H
  3. 3.0
复制代码

. |3 o+ B1 J9 R( O# Q9 [* y& g2 H7 j

2 n5 F" r# H3 p& b" G7 _
math.e  表示一个常量0 k" j" e/ w$ u0 B; M
  1. #表示一个常量
    4 o7 C& n$ V! I# f; Z1 \1 W
  2. >>> math.e* c  {9 p3 F8 ?' d# M1 ]  Z
  3. 2.718281828459045
复制代码
8 p, X1 `' O8 e/ `7 G
math.pi  
数字常量,圆周率
8 g: n! U& B) v+ p, Z: G
  1. #数字常量,圆周率
    + h1 d- L* h0 _
  2. >>> print(math.pi)* ~( i4 |; }& ~  N
  3. 3.141592653589793
复制代码

: D. p3 s' W0 Imath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

0 b% ?  c) J+ K* S! e
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    6 T5 t2 X% \; J6 `; m
  2. ceil(x)
    ! L! }" \  G; i" s" g+ ?
  3. Return the ceiling of x as an int.2 y' n$ }9 F7 j) ?
  4. This is the smallest integral value >= x.+ R  i' Y( G0 ?
  5. 9 Z5 f/ O! l) V7 a9 X
  6. >>> math.ceil(4.01)$ R! ^* x- b: f5 U+ ~
  7. 5
    + S% D* R% J9 U5 E+ k
  8. >>> math.ceil(4.99)
    : o; A! B1 z$ B: w. K: L& A; c$ ~
  9. 54 n. L# j, ?) b/ {
  10. >>> math.ceil(-3.99)
    , }: T& A' I' ?; H: u/ V6 p( d
  11. -3
    ! h8 R0 c0 O9 G$ n' C) J% F
  12. >>> math.ceil(-3.01)
    7 ^2 G9 P0 n7 p: k
  13. -3
复制代码
0 F- f' A4 X7 z$ j* D# \
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
; H2 \1 [! O3 T* J
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    ( f& \. c' b* A2 |: g
  2. floor(x). h' i# s" f, @; q/ [/ q5 l0 e* G! s
  3. Return the floor of x as an int.! J6 {1 N9 X2 i2 ]- l- b
  4. This is the largest integral value <= x.
    0 [! Q, M% T1 ?6 |4 e# R5 Y) J
  5. >>> math.floor(4.1)
    : E( H& B! D3 R- r" n
  6. 42 C0 [) m5 V4 p2 H
  7. >>> math.floor(4.999)* U2 e. i3 H: v: d: u
  8. 4  A3 ~' M+ X; s9 y/ z9 c) b$ J
  9. >>> math.floor(-4.999)
    + q! U5 [" q0 j
  10. -5. @9 U/ E+ J& \# b, a
  11. >>> math.floor(-4.01)0 R: Y& `* A- _% S. m
  12. -5
复制代码
# \* @9 ^" [/ {  H# O! q- u7 o
math.pow(x,y)  返回x的y次方,即x**y! L) y  u, t; K  X& Y
  1. #返回x的y次方,即x**y" Q. T9 ~5 Z& h% H* N1 T% T" T! e# f
  2. pow(x, y)3 E4 x6 d7 u8 X& |2 l
  3. Return x**y (x to the power of y).8 _. y& U1 r: T6 T( t4 Q3 @7 q
  4. >>> math.pow(3,4)
    4 m8 E& s; ]& E% B, h
  5. 81.0# K( x4 t% _$ q; K& D7 z
  6. >>>
    3 I2 d" u, |' L' `3 @$ w
  7. >>> math.pow(2,7), r) P( j* n' f* r- T4 W! G) R
  8. 128.0
复制代码

- ^* Q/ `& R% amath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 r9 u3 j% p* Z) G6 C
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ' o. ^4 N' B. P+ E$ M$ m# `
  2. log(x[, base])
    ; h/ j8 h9 B" E, ^: M
  3. Return the logarithm of x to the given base.; j2 ^1 E( {: Y3 a% W
  4. If the base not specified, returns the natural logarithm (base e) of x.
    / E) x% ?0 M2 g, p( z  D8 m  O3 D
  5. >>> math.log(10)" p1 O4 w4 J) Q* K3 q/ l3 P6 ~
  6. 2.302585092994046
    2 S1 n( P, s6 w; H# l% ^
  7. >>> math.log(11)% A; \% z: z" s- N; C% s
  8. 2.39789527279837070 L* `9 v' w9 {' x
  9. >>> math.log(20)% k8 @" a+ M2 L  \
  10. 2.995732273553991
复制代码

$ S5 ]& [8 A$ l& ?0 Kmath.sin(x)  求x(x为弧度)的正弦值
3 s! q+ K5 Y7 h3 W; h
  1. #求x(x为弧度)的正弦值
    & i) ]7 w2 C& v
  2. sin(x)! V0 v+ x1 @; N
  3. Return the sine of x (measured in radians).
    + o! o$ ^2 J9 i5 w" c! _
  4. >>> math.sin(math.pi/4)  j9 U; K) C! y& {" N2 `' v
  5. 0.7071067811865475
    # F; m9 B0 W3 q* j+ l- G
  6. >>> math.sin(math.pi/2)
    / `# R# Z2 a: ]
  7. 1.0
    : o" z! J1 c: G( s  ]0 `8 q) y5 c
  8. >>> math.sin(math.pi/3)
    ( j$ t& S, X  d, h, K( b1 R
  9. 0.8660254037844386
复制代码
& E& M7 O: T8 Y; A* C; l1 w2 v$ H1 i: A
math.cos(x)  求x的余弦,x必须是弧度
3 P2 ~& Q; U$ d. O9 e
  1. #求x的余弦,x必须是弧度+ F2 f7 {; |( A3 S
  2. cos(x)
    / v. B; |( A* K
  3. Return the cosine of x (measured in radians).7 V1 K0 n. N0 H, p! }1 u
  4. #math.pi/4表示弧度,转换成角度为45度
    . B  \. B0 @! o2 g
  5. >>> math.cos(math.pi/4)
    ' p" {# G: s) b6 H8 x7 ~3 d8 t
  6. 0.7071067811865476  G5 U$ K: c1 S9 x+ N& g
  7. math.pi/3表示弧度,转换成角度为60度
    ' \/ Y7 O, A$ j4 B- ?# Z
  8. >>> math.cos(math.pi/3)+ t( `; C( o% i
  9. 0.5000000000000001
    ) Z; s3 \7 V! ?+ _- s
  10. math.pi/6表示弧度,转换成角度为30度$ ^6 a! J7 m7 T
  11. >>> math.cos(math.pi/6)
    ' @  S1 \% X: ^5 B* R
  12. 0.8660254037844387
复制代码

3 @% v7 D( |  r' Z5 C& ~5 zmath.tan(x)  返回x(x为弧度)的正切值
" L- s5 E* e- z& x, V) H
  1. #返回x(x为弧度)的正切值
    ) n8 N/ B- g1 @5 C( ~* ^: `5 v+ ?
  2. tan(x)
    7 P: Q6 T2 A( Z$ q+ Y8 f! T
  3. Return the tangent of x (measured in radians).' h2 m- ]- K& b# o- q
  4. >>> math.tan(math.pi/4)
    2 c0 H3 v) I' x3 N# K
  5. 0.9999999999999999
    8 F) x' x. q; r3 b* {7 F2 N
  6. >>> math.tan(math.pi/6)6 i0 C- z2 H! X/ @7 q; B" k" m
  7. 0.57735026918962576 c! G. Y) x) i0 ]/ x
  8. >>> math.tan(math.pi/3)% J5 j; H+ \8 r6 h! j: u! ?
  9. 1.7320508075688767
复制代码
! j4 w; r6 U; ~
math.degrees(x)  把x从弧度转换成角度6 {0 v. E) N, e4 D
  1. #把x从弧度转换成角度4 \& V, V! t6 y
  2. degrees(x)
    6 g/ e) L" m9 z
  3. Convert angle x from radians to degrees.2 M! _, A7 [) y0 c

  4. , O  g; a$ q. B
  5. >>> math.degrees(math.pi/4)
    2 g* N# R( ?8 @2 n7 b% ?; M
  6. 45.0* R: U' }5 v! h+ k6 U& I( ]
  7. >>> math.degrees(math.pi)
    & ^$ D- I" B# c* {) L& l1 m( \0 p
  8. 180.0
    ; W" b# z4 q7 F9 I; ^3 }
  9. >>> math.degrees(math.pi/6)7 @# Q: u; K0 N$ p
  10. 29.9999999999999964 ~$ h6 P% O. ^/ E$ {
  11. >>> math.degrees(math.pi/3)
      [7 H! a+ Q- i) z( s5 `
  12. 59.99999999999999
复制代码

; S( b8 J/ P8 r2 vmath.radians(x)  把角度x转换成弧度$ o" G8 h- _$ w
  1. #把角度x转换成弧度
    ' Y! S! w/ w2 e0 B0 U
  2. radians(x)
    3 H! K6 B& ]' |0 \+ I# A' L
  3. Convert angle x from degrees to radians.9 Q3 o6 d# L! m; F: `* f
  4. >>> math.radians(45)
    3 w9 C1 r! h" Q  ~2 M/ e
  5. 0.7853981633974483
    % k& a4 Q' G; O" C1 L. l) n
  6. >>> math.radians(60)( a5 p& ~6 @9 ?8 i1 u) K
  7. 1.0471975511965976
复制代码
7 C5 l# N! d* I+ K# z
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
8 x* o1 N+ ^; C" a
  1. #把y的正负号加到x前面,可以使用0
    ( x  W9 C$ ^$ i3 ~  Z- e, G7 ~
  2. copysign(x, y); k( Z8 W# Z5 G0 O4 {) B/ U
  3. Return a float with the magnitude (absolute value) of x but the sign
      o* @6 c/ X. F
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    4 n1 z: J5 ?& N
  5. returns -1.0.
    % U0 a" W5 b) N
  6. ' O0 T- `+ o# q. }& J9 \3 U2 Q0 ]
  7. >>> math.copysign(2,3)( [. x5 O% |- Y: Z$ s- n
  8. 2.0' ~9 {" G8 O- r! N* O1 Z
  9. >>> math.copysign(2,-3)
    ! F" A- k! ~+ y7 ?+ ?
  10. -2.0* y" P, E+ L# k$ c
  11. >>> math.copysign(3,8)
    6 d$ J: ~2 L; l0 D5 a, \+ n2 G. p4 M- Z
  12. 3.0
    . d4 b" ]. U! h0 q+ L. X
  13. >>> math.copysign(3,-8)( q" J: q0 R( n# F/ d+ ^
  14. -3.0
复制代码
- c, t/ o! d3 ?
math.exp(x)  返回math.e,也就是2.71828的x次方9 r% N6 Y- `6 F9 F
  1. #返回math.e,也就是2.71828的x次方
    ; I1 Y3 k$ n5 p3 F
  2. exp(x)
    ( ^1 b3 E7 i2 X+ j' \' k
  3. Return e raised to the power of x.: v( j2 m9 R+ u

  4. , p* x5 e  o& e7 b4 R# u! @+ W
  5. >>> math.exp(1)
    " ^- P4 \/ Y0 g: L# [9 ~
  6. 2.7182818284590451 G% j8 s6 D5 `! o( z
  7. >>> math.exp(2)- ~1 o' W+ [) y  Q9 v5 {/ W' o. T
  8. 7.389056098930651 u. \7 q; a6 Y" K8 I) A4 l
  9. >>> math.exp(3); \1 f8 a$ h* M$ Z* R0 k
  10. 20.085536923187668
复制代码

/ [* Q+ K0 n% W/ Emath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减16 h+ _% Y& K+ S
  1. #返回math.e的x(其值为2.71828)次方的值减16 h2 v6 L# _8 L
  2. expm1(x)( n% V0 B3 M! k# x) B
  3. Return exp(x)-1.
    # ]- y3 x* n8 I6 z' O  ~* r
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    1 }' W6 |* f2 E2 F5 N! {
  5. 6 }5 n, `, U1 r/ L& ~0 a4 F, r
  6. >>> math.expm1(1)5 o; T3 h) u1 L  j( I
  7. 1.718281828459045! H9 P1 b6 {9 w1 P: m, M& {
  8. >>> math.expm1(2)
    4 A1 J( n) `( \- ]- K! L- m. Y
  9. 6.38905609893065
    ; \8 I6 F5 N' w; m& ^1 C/ }' M  ]4 A
  10. >>> math.expm1(3)
    : ~  [4 U3 U3 G
  11. 19.085536923187668
复制代码

: x& C  R; ~+ P) hmath.fabs(x)  返回x的绝对值5 f6 ^6 \6 s$ z% v2 K: p. H- t
  1. #返回x的绝对值- P' ~8 D+ z: W2 r6 V/ P
  2. fabs(x)
    " K! b* o, N5 T
  3. Return the absolute value of the float x.3 E* s1 y6 [& N. `( H; p
  4. ; O7 b& R# R- ~- a  p$ Z/ [
  5. >>> math.fabs(-0.003)
    % _/ U: Q* S$ b6 a9 M# T* R1 B) ?+ `& j3 I
  6. 0.003$ t6 r: P1 ~/ C" y7 f) P
  7. >>> math.fabs(-110)
    " L% z; v1 @2 V) E6 I' R
  8. 110.0
    2 d' e3 U* v6 o2 Q
  9. >>> math.fabs(100)0 A5 ^4 {  L2 r1 b
  10. 100.0
复制代码
$ n# J2 z# R: |9 Z
math.factorial(x)  取x的阶乘的值
* |5 m: S. X0 J, o/ P/ i8 P5 y
  1. #取x的阶乘的值  P! p- s; S' O  d4 w
  2. factorial(x) -> Integral
    6 n, o0 h( q8 V' c
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    * F- N8 w" i$ L! c; d! C4 }
  4. >>> math.factorial(1)2 v1 {+ w( Z+ R
  5. 1
    ; e" T( x# v" H
  6. >>> math.factorial(2)
    + i+ l! F% g* `
  7. 20 y( _3 B' C$ x4 Z" v! Y; B
  8. >>> math.factorial(3)
    ) `/ R( Q. n, `; ]) _/ r, w3 }
  9. 68 G1 p& k4 G! H! B7 E( g' @
  10. >>> math.factorial(5)
    8 M6 m1 F0 `% E/ m2 t- w1 y( z( n
  11. 120' s1 j1 h! B  a$ I* r. j
  12. >>> math.factorial(10)" c: j$ x& L8 n/ m3 W% x& a
  13. 3628800
复制代码

' z1 x" S+ s0 ^/ s/ L( ]# K( P# O  `math.fmod(x,y)  得到x/y的余数,其值是一个浮点数0 M( r8 H1 V) }. Q7 V+ d
  1. #得到x/y的余数,其值是一个浮点数
    / o1 H/ S: e5 V
  2. fmod(x, y)# w* }0 h* `  A  I* {4 U& H& N* `* P1 o
  3. Return fmod(x, y), according to platform C.  x % y may differ.3 M) d( }# z8 V
  4. >>> math.fmod(20,3): U! M: f! r* o) o7 M2 ?1 {& i
  5. 2.0( C$ S8 ^* J8 [: T( f( Y* O7 u
  6. >>> math.fmod(20,7)$ r- s4 ~9 e7 u9 _7 [; V9 B/ _
  7. 6.0
复制代码
$ g4 _7 N" Z2 G+ J5 Y9 h
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
" h" Z0 c" W. [3 V: h4 U
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 _% u5 o' ~, u5 v6 u$ T
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    , T# ?, E( q2 S5 w. h/ m5 f- U
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ( t% o1 i6 X. {( u& N( O
  4. frexp(x); L4 x8 M2 d- q9 u7 R/ D% e
  5. Return the mantissa and exponent of x, as pair (m, e).# ?9 q! `3 y" W: g
  6. m is a float and e is an int, such that x = m * 2.**e.
    9 \2 Y( S' d5 `6 W" s8 e# a6 R6 w- X
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    $ N7 @% w# P0 E* k5 U" {- Y4 Z
  8. >>> math.frexp(10)
    , f7 ~* p. `; ]' A# L/ X
  9. (0.625, 4)
    . L7 w) }# f) x& k
  10. >>> math.frexp(75)
    : C/ m$ k! m$ a1 F
  11. (0.5859375, 7)
    3 m& s7 W: T+ H- K
  12. >>> math.frexp(-40); W( q' }  G# ]4 H  _% T
  13. (-0.625, 6)6 q! u* ^* @; ^* L4 V8 }5 Q
  14. >>> math.frexp(-100)- ^# m+ Y) O8 R( }5 F  a
  15. (-0.78125, 7). ~* u/ |& q4 s* P
  16. >>> math.frexp(100)
    " B* S2 ?0 g9 z) T
  17. (0.78125, 7)
复制代码

2 Z, j: Q  ?: t  r% n8 a5 q1 bmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
0 b6 ^/ E5 k- U0 q
  1. #对迭代器里的每个元素进行求和操作! w. {+ |6 T6 N/ z" c
  2. fsum(iterable)" q  D( s) Z& [8 H% H# t
  3. Return an accurate floating point sum of values in the iterable.( B, \4 e+ l% a/ o' B
  4. Assumes IEEE-754 floating point arithmetic.
    # E  m7 t: y2 @  m
  5. >>> math.fsum([1,2,3,4])6 s7 u9 ]9 E* z' f
  6. 10.0
    # O/ p  p2 c" v! i. f
  7. >>> math.fsum((1,2,3,4))
    2 I6 D9 F3 C$ j8 B, k2 O2 N) }3 T! ^
  8. 10.0
    * e# A) z2 P: _
  9. >>> math.fsum((-1,-2,-3,-4))
    / p0 }. X* z/ ^/ d$ G
  10. -10.0* i0 m  |7 s8 {
  11. >>> math.fsum([-1,-2,-3,-4])6 g/ w5 w- A# g/ U
  12. -10.0
复制代码

! ^2 o5 }4 u' f* ]8 mmath.gcd(x,y)  返回x和y的最大公约数
& ^. H$ t% C9 T$ f
  1. #返回x和y的最大公约数- |* U8 v( W2 K
  2. gcd(x, y) -> int
    , V% @  `! V  Q! n0 ?5 n
  3. greatest common divisor of x and y
    8 m0 O7 J7 a+ X
  4. >>> math.gcd(8,6)
    % Q' Q( G0 F' J7 M& [' B) P
  5. 2
    7 @& U& `' t5 ~7 T! f
  6. >>> math.gcd(40,20)
    ; H* s# |/ N+ I, `) T! q
  7. 20
    " X; @+ q' H' {% \( A. L' {( P& y
  8. >>> math.gcd(8,12)
    * Y. v- K. V" f: v% g/ r$ n
  9. 4
复制代码
- y' p: U2 R1 }$ r
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False: d/ O, ?5 M& g% O) l# ]
  1. #得到(x**2+y**2),平方的值
    5 _" j0 E: W9 H! \# v  B7 H
  2. hypot(x, y)
    ; |* p' h! ^# S& h
  3. Return the Euclidean distance, sqrt(x*x + y*y).! _; ~3 O# G2 {) L
  4. >>> math.hypot(3,4)" z+ m' G' ^: g9 u
  5. 5.0: o/ H  `" a0 w( Q8 N
  6. >>> math.hypot(6,8)
    4 X2 u" _; w! a! \4 K
  7. 10.0
复制代码
: G4 P! T; F% A6 P7 M4 d& a
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
4 f1 x* v  n3 c+ j; R
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    8 |7 t6 z( z" Y8 }3 C% Y. b: ^3 Y, z3 M
  2. isfinite(x) -> bool
    , o+ V$ o( [9 q) N( K
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.( R, Z* w1 q7 ?* S2 z' e8 F
  4. >>> math.isfinite(100)
    2 J/ ^0 u, B3 w
  5. True
    / R) R1 b8 R0 R1 T# S! ?! F
  6. >>> math.isfinite(0)
    4 w+ d" ~3 H) _# \4 ~6 c
  7. True
    % A5 L; Y& p' o: |$ A% s
  8. >>> math.isfinite(0.1)1 L9 B+ \$ v6 J1 U) L5 Y* s
  9. True
    + F2 E' I( ]6 B5 t
  10. >>> math.isfinite("a")* V; y$ B% i/ U* e
  11. >>> math.isfinite(0.0001)
    5 G; o  e" n6 D, {5 G4 |- `* O8 H$ J( ?
  12. True
复制代码

2 }) N" {& B6 ^( U/ g: X3 e3 pmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
. k, f+ P- j: k/ |- x3 z9 p
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    4 q/ P. N8 H: N7 y/ b
  2. isinf(x) -> bool
    / x$ a0 a# ?) C2 `$ J
  3. Return True if x is a positive or negative infinity, and False otherwise.5 s, r# m3 N+ f/ t
  4. >>> math.isinf(234)
    - K2 a  \. D: H8 P6 f  [& k
  5. False
    " V8 E/ l) U# k. F2 C. }, X, P
  6. >>> math.isinf(0.1)! r" I; N' z- U0 d
  7. False
复制代码
, V& s+ v$ S8 {- w
math.isnan(x)  如果x不是数字True,否则返回False
  r' P0 j3 `& c4 z0 z
  1. #如果x不是数字True,否则返回False7 `: |# |3 W2 X/ u
  2. isnan(x) -> bool
    9 P$ W5 [! F  V
  3. Return True if x is a NaN (not a number), and False otherwise.
    % e6 E9 _& \( k
  4. >>> math.isnan(23)
    3 Q" e) v5 o0 s  S
  5. False& {- D8 ?" K2 S* Y0 y0 L9 F7 _& R
  6. >>> math.isnan(0.01)
    0 j) Z; z, v# N( P
  7. False
复制代码

8 J7 P, u8 Y* S3 W, r- C4 Wmath.ldexp(x,i)  返回x*(2**i)的值4 V$ r3 h8 Z* P- W/ A$ R% ~, F
  1. #返回x*(2**i)的值
    : U, V& e0 c" ~) _9 A
  2. ldexp(x, i)
    / G# O7 t. j& j5 I8 \& k' |
  3. Return x * (2**i).: ]( J% i! s" C* n# Y3 g( h. d
  4. >>> math.ldexp(5,5)& ~8 |7 F3 Q5 R9 z2 D1 S7 k
  5. 160.0
    " |( N) c) p4 n) k) E4 ^
  6. >>> math.ldexp(3,5). o# T; R& V0 w4 x
  7. 96.0
复制代码

4 r5 O: d# C0 [7 Lmath.log10(x)  返回x的以10为底的对数" s& i3 S* E! ?6 G* q. `" S
  1. #返回x的以10为底的对数
    ! c+ \- P) W' k1 w  a
  2. log10(x)" i1 q4 B" T, F# j
  3. Return the base 10 logarithm of x.
    & r6 C* @3 d; q6 q
  4. >>> math.log10(10)
    / d& e# Z9 i+ S
  5. 1.0
    9 d4 L9 f) C5 q; c9 ?2 |
  6. >>> math.log10(100)1 c6 g! S8 O" ~- l; U, Q# w( ?/ q: H* j' J
  7. 2.0) q$ e. X3 A- I
  8. #即10的1.3次方的结果为20( \7 E% U/ K' z1 w& O
  9. >>> math.log10(20)4 l- _5 ^- d$ d9 n& U* c4 [# Q
  10. 1.3010299956639813
复制代码
- I# [% t) s, u% H
math.log1p(x)  返回x+1的自然对数(基数为e)的值  J" d( e" L9 ^8 R) L$ {
  1. #返回x+1的自然对数(基数为e)的值. S6 A( w0 Z1 Z5 M4 q
  2. log1p(x)
    $ p7 @! W8 R7 E" m0 _
  3. Return the natural logarithm of 1+x (base e).  }! }2 S3 I3 z
  4. The result is computed in a way which is accurate for x near zero.7 g+ \/ E( l" @/ x/ ~
  5. >>> math.log(10)+ S" m2 ?7 b% R$ G1 V
  6. 2.302585092994046
    : s4 s5 a) n- G1 X" w
  7. >>> math.log1p(10)( j/ f0 P( N8 n: I9 Y7 N
  8. 2.3978952727983707
    ! b' Q; Z# ~! W; l9 C
  9. >>> math.log(11)* P' ]. e; d: l( t
  10. 2.3978952727983707
复制代码
# D( k" N6 v4 u$ O3 n' f* R# z
math.log2(x)  返回x的基2对数$ @/ ?. e0 _% E
  1. #返回x的基2对数
    6 K; P1 S3 o- U  W$ n
  2. log2(x)4 o/ V3 R# F% j; _
  3. Return the base 2 logarithm of x.
    % A5 h, ]$ f4 d. n! N/ f: k9 G7 t
  4. >>> math.log2(32)
    4 N" }) y1 D, r7 c$ X
  5. 5.0) {) g: O" ~. B. i: `9 \: K
  6. >>> math.log2(20)
    6 T1 @8 E8 k* Y
  7. 4.3219280948873639 K4 I" o, k1 Z, f" m
  8. >>> math.log2(16)
    ' k6 f1 {& }5 D3 q6 K
  9. 4.0
复制代码
! w" A6 N! D% l: ]& q
math.modf(x)  返回由x的小数部分和整数部分组成的元组6 A0 U4 c/ V  j, Z* C7 y6 O
  1. #返回由x的小数部分和整数部分组成的元组
    1 V6 V2 o1 K/ E/ Y
  2. modf(x)
    8 s. O9 S" J6 Q" R$ y! o8 H
  3. Return the fractional and integer parts of x.  Both results carry the sign$ U2 ~# c2 e9 _+ Q4 K! ~) `+ `
  4. of x and are floats.
    , ]0 G+ m+ \) G0 |9 r/ _
  5. >>> math.modf(math.pi); A/ u9 ]; W& D
  6. (0.14159265358979312, 3.0)2 b9 U5 K2 m  k& L. S
  7. >>> math.modf(12.34)* r5 E0 s9 w7 s) G( N: y
  8. (0.33999999999999986, 12.0)
复制代码

- Y2 D: ?$ {( J/ [math.sqrt(x)  求x的平方根
, w! \' ?1 S; ?( z. N9 a/ w  k
  1. #求x的平方根! F5 o4 O; j! c' Q" B
  2. sqrt(x)
    % H) e$ ]& J3 \: R3 r& h# q
  3. Return the square root of x.
    ( @' v0 f$ x" x$ Y9 f$ ?  O7 X6 I2 d
  4. >>> math.sqrt(100)
    : w5 ~$ x  S- @  }
  5. 10.0
    ; T- `9 E2 a5 O9 X0 M4 g0 V7 f9 ^
  6. >>> math.sqrt(16)
    % g' }7 V9 o) w# D3 A5 O* i9 x
  7. 4.0
    & |+ D! P' z, Z+ m/ B* Y8 X
  8. >>> math.sqrt(20)
    - a0 ^3 g$ E0 C  X0 i5 o. r
  9. 4.47213595499958
复制代码
2 `% Q* D' Q6 T5 R) a% G& S
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分9 s9 z. i& _" Y. ^7 R  }2 J1 y
  2. trunc(x:Real) -> Integral
    6 b  b5 `4 @4 F$ P
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    - c2 g1 A2 T9 N: j7 @. Z- O
  4. >>> math.trunc(6.789)
    9 A; P5 C  I* e( V# |% b
  5. 67 }4 Y+ v2 u! ?! f' S# R1 p" ~- @5 v
  6. >>> math.trunc(math.pi)
    $ ]+ J9 a* z- c% z& X& W0 L5 h
  7. 39 B& c) P! E, `! G  e8 N" U, m
  8. >>> math.trunc(2.567)
    ) G' i# q% t) d3 G. E2 \2 s. m* C
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-13 22:21 , Processed in 0.076783 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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