新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

# t9 i3 m! X  ^0 C& A( Q【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
) V* q6 A" B6 e  r* c  z
* T4 k3 S% _' O. V5 d/ B# |
方法15 j3 y8 p5 B6 L" {, Y! W2 H
  1. >>> import math
    2 n) w+ x% H$ b! a
  2. >>> math.sqrt(9)
    ) |7 y8 Q. T' _; @$ {* n
  3. 3.0
复制代码
方法2
  N1 V0 o; L' L- Z
  1. >>> from math import sqrt9 F8 Q" f! |# z* u- T5 b
  2. >>> sqrt(9)
    2 Z5 }3 ?* m9 @2 j2 B/ I, j5 T  j
  3. 3.0
复制代码

/ w- W2 T6 U* w3 w+ f' z' W
) H/ E' f1 t, A8 K5 \3 Q: R
math.e  表示一个常量
% [' {; m) V  k
  1. #表示一个常量  c) ^0 ^4 t$ l, ]5 E7 P6 j! ^" u" j
  2. >>> math.e, b& R# H! `( D
  3. 2.718281828459045
复制代码
3 C! {3 {9 Z3 h- q( _
math.pi  
数字常量,圆周率

2 F7 u$ [' k- H
  1. #数字常量,圆周率
    $ ]- @* E0 F0 m3 Y& ]6 ?' G
  2. >>> print(math.pi)
    ' M3 G* V& H2 N& F3 c3 }
  3. 3.141592653589793
复制代码

; @2 Q% X$ S9 R1 z( m6 Nmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

  p7 H9 A* M8 N3 b; c" M3 ], a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 q9 r1 r# d, h5 c7 ^
  2. ceil(x)
    8 T/ ?9 r& f7 |  u$ ]1 C1 \7 H+ X) x, U
  3. Return the ceiling of x as an int.
    % T% a: h  u4 U
  4. This is the smallest integral value >= x.1 H) U8 @) y* m- c
  5. 2 d) `& l, @) d2 J
  6. >>> math.ceil(4.01)
    * V* n5 O& R- i/ U% u
  7. 5
    9 J4 k# k( n* S$ D8 ~
  8. >>> math.ceil(4.99)
    5 |" Y: z' B! k4 v( D/ A
  9. 5
    - O  n3 b& W2 [. t( p
  10. >>> math.ceil(-3.99)
    ; z. }* I9 d9 |4 H. n% b3 I0 a, w
  11. -3
    0 X5 K+ G; p9 @( N- ?0 s
  12. >>> math.ceil(-3.01)
    8 b+ p' `0 y  D; x3 Y3 M' b5 m0 ]
  13. -3
复制代码
1 ^3 x$ P. U- G" A; i1 m8 z3 H
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: `; t) S# v$ _: ], a! s
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ L+ U! I5 W. R; a; H& T/ j
  2. floor(x)$ M: l; x9 o( r$ o9 d+ o
  3. Return the floor of x as an int.
    0 ~/ V; [  r9 b* ?+ c3 {0 Y- ~& X/ g
  4. This is the largest integral value <= x.0 C, N% g0 d: l' s$ b0 R% D
  5. >>> math.floor(4.1)& m) d  c) {- h( d* i5 U
  6. 4
    4 C" Q7 B. J! ?/ L! m& d- x
  7. >>> math.floor(4.999)
    & X' ^! `3 k* S
  8. 44 i: J2 {. c2 N* A* F5 U
  9. >>> math.floor(-4.999)' H5 B) x" q8 B! X. y
  10. -57 a  z) G/ b3 E  u
  11. >>> math.floor(-4.01)
    7 B. |* B) n: p/ }9 p9 `
  12. -5
复制代码
4 O$ [( p; w6 P% W$ p
math.pow(x,y)  返回x的y次方,即x**y
9 L1 P, t7 {8 T, X2 t3 s
  1. #返回x的y次方,即x**y
    ( b. }* R7 @" L$ c* Q
  2. pow(x, y)- _/ l- G( D$ h; v
  3. Return x**y (x to the power of y).$ f; _& C; H, j
  4. >>> math.pow(3,4)
    1 B" E0 T/ s/ I/ L
  5. 81.0
    8 f$ k0 F3 e8 \: P
  6. >>> ! F3 y, b- c6 c! V0 |+ E
  7. >>> math.pow(2,7)
    2 k& P5 u& ~1 w/ A
  8. 128.0
复制代码

1 j) U* z3 R  f/ y! t9 @math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
& D. v  A; X7 h6 W8 y; H
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    7 C7 h5 G0 O8 J  T8 e; t
  2. log(x[, base])
    1 L  s% y  r. a8 g$ _
  3. Return the logarithm of x to the given base.
    6 G5 c% F: _5 i
  4. If the base not specified, returns the natural logarithm (base e) of x.
    $ H( k' z8 J$ s  F9 C" I$ {- p$ F
  5. >>> math.log(10)
    1 x: X4 V2 P& Q3 \
  6. 2.302585092994046- u' Q# q$ w) i+ C1 n7 ~1 X7 Z
  7. >>> math.log(11)
    & s0 E: ]( Q. G
  8. 2.3978952727983707
    ' K7 {- R: c/ J8 y: I- S8 H& d
  9. >>> math.log(20)$ j( }, n% d) r6 r2 F* v' X; G
  10. 2.995732273553991
复制代码
+ J) q5 T( }! E4 G; j8 N
math.sin(x)  求x(x为弧度)的正弦值  s% H9 t3 w% R
  1. #求x(x为弧度)的正弦值
    " H- R& f% X7 [0 G! g$ V
  2. sin(x)
    ( D4 w+ @& k4 j5 d
  3. Return the sine of x (measured in radians).
    3 d! z" Q5 e, F: P. s2 N
  4. >>> math.sin(math.pi/4)
    " D5 ~  a1 ]# n2 V& t7 }4 l& L
  5. 0.7071067811865475
    ; U( {/ U8 g' J9 C; O8 {; r
  6. >>> math.sin(math.pi/2)+ O7 ^! Q$ d' _  E( Y
  7. 1.0, i! G7 T" x: t
  8. >>> math.sin(math.pi/3)/ j/ A  c5 ~' J" O7 y
  9. 0.8660254037844386
复制代码

3 e; C" d# m9 m. p: S! Z. ~math.cos(x)  求x的余弦,x必须是弧度
8 X  P7 J! [/ d& Q/ W
  1. #求x的余弦,x必须是弧度: z9 P9 Y& D: C5 r+ _4 c7 Y
  2. cos(x)9 O5 s4 z" W+ N7 W+ K; b, X
  3. Return the cosine of x (measured in radians).
    1 J) O1 n! `! Y
  4. #math.pi/4表示弧度,转换成角度为45度
    2 x- j8 V; J; m) v' _
  5. >>> math.cos(math.pi/4)2 J* P, ]2 V1 N6 n0 Z
  6. 0.7071067811865476
    ( E& W, p7 b% ?+ V( C( z
  7. math.pi/3表示弧度,转换成角度为60度) u0 V. X( C# [8 [- A
  8. >>> math.cos(math.pi/3)
    5 g6 E7 a% K9 @4 x. q) I
  9. 0.5000000000000001
    . |+ q' N: e4 I) M  o( q0 ~6 P
  10. math.pi/6表示弧度,转换成角度为30度
    " c" N) |' t/ G9 {$ V: B) ^  I: G
  11. >>> math.cos(math.pi/6)) l+ W4 V5 S, Y
  12. 0.8660254037844387
复制代码

' Q* l& N# b* {) p6 r' |math.tan(x)  返回x(x为弧度)的正切值. t% j9 b( n; j& u# c& P- @; l
  1. #返回x(x为弧度)的正切值% T, G# \; k, d2 V3 W# V; X, O9 i
  2. tan(x); d- I, p6 Q2 O: F$ K
  3. Return the tangent of x (measured in radians).3 L+ P* s8 O; H, e
  4. >>> math.tan(math.pi/4)8 O8 j# Z# o! t' V6 T
  5. 0.9999999999999999
    ) z" c5 ^1 Q% V4 E& x
  6. >>> math.tan(math.pi/6)
    & X0 [& M' Z- Z3 R/ e$ [3 ~
  7. 0.5773502691896257  z* `& @7 f. y
  8. >>> math.tan(math.pi/3)
    9 Y) L- L, A. _' [! {+ x* S# h
  9. 1.7320508075688767
复制代码

' `- l$ v3 D- A1 u0 Rmath.degrees(x)  把x从弧度转换成角度
5 s7 N' ~/ w. @, D3 R8 q! |* K2 G
  1. #把x从弧度转换成角度
    2 V' ~4 |* n+ h
  2. degrees(x)  `! e* Y$ J4 H3 K% K
  3. Convert angle x from radians to degrees.2 R! ]6 h8 _& _+ ^) l
  4. 3 |+ r/ @: q! }  a
  5. >>> math.degrees(math.pi/4)
    + c$ V$ w9 [9 l& `: H
  6. 45.0. K$ I; U- A9 t  O1 F" R2 o
  7. >>> math.degrees(math.pi)
    ! `+ y! p; S; |: e
  8. 180.09 [! L: o5 ^6 y$ U
  9. >>> math.degrees(math.pi/6)
    + F/ J' F) y3 {# j6 T
  10. 29.999999999999996
    - C4 |( [2 w: y
  11. >>> math.degrees(math.pi/3)
    3 m4 s" X& \# }5 x2 k- d. `
  12. 59.99999999999999
复制代码

5 U. r! D( M7 emath.radians(x)  把角度x转换成弧度
; g) U3 P/ y3 H0 x6 C
  1. #把角度x转换成弧度
    9 s, l8 I5 R7 S! L! O! q
  2. radians(x)
    # p5 b6 @2 m! z, p# o7 @- C
  3. Convert angle x from degrees to radians., x- r5 z" @4 l3 c1 p  @& @8 s' u
  4. >>> math.radians(45)
    7 [. I5 G9 w/ f+ L- P
  5. 0.7853981633974483
    3 k" ?- W; p# m: ?
  6. >>> math.radians(60)
    ! T3 d7 E- i- W5 B$ j( T  M; ^  |
  7. 1.0471975511965976
复制代码
& S% ~. J* G3 E; s# [; w
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
$ Q+ N+ o: I$ f" D9 D6 F9 q
  1. #把y的正负号加到x前面,可以使用0
    . f9 y( Q; W) t( j4 k8 l
  2. copysign(x, y)6 |, P  a9 e) u* _
  3. Return a float with the magnitude (absolute value) of x but the sign
    : F8 e  k$ r2 P( Z
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) / D: ~! @9 F( g8 x9 j# x
  5. returns -1.0.) U2 l* u/ X- K8 _

  6. " z, U& H: D' i8 p; m! _6 V4 o
  7. >>> math.copysign(2,3)
    ( X  A! P$ @* c  y$ }
  8. 2.0) X3 b2 t& @: `, e' d
  9. >>> math.copysign(2,-3)
    / Y1 m# m& Y+ e) e* Q6 i
  10. -2.0
    % |4 [( ?7 W" b( i5 n+ b
  11. >>> math.copysign(3,8)
    1 _: |4 }" J. i. C% U0 v& l
  12. 3.0
    3 e* b6 F, J3 n2 j
  13. >>> math.copysign(3,-8)/ Y' q) m; a7 J+ U1 G# `7 e
  14. -3.0
复制代码

9 a- A# x) K; ?( amath.exp(x)  返回math.e,也就是2.71828的x次方9 J4 @1 i' g& S3 Q  b# {& @
  1. #返回math.e,也就是2.71828的x次方
    + i% `1 m, |  a; c6 }
  2. exp(x): ~. Z; G$ x7 R
  3. Return e raised to the power of x.
    ' D1 ~) Y* Y4 j

  4. / ?' I& ^' @$ x: r: L7 ^
  5. >>> math.exp(1)+ E" R+ S* H6 V# W" g6 `
  6. 2.718281828459045
      O# \; W+ o! _! E1 p' z
  7. >>> math.exp(2)
    0 q$ d; h  H8 x
  8. 7.38905609893065
    + P! r% D, r! Y$ D
  9. >>> math.exp(3)
    * T# @% K4 p1 l6 H
  10. 20.085536923187668
复制代码
/ O% s5 J! ]5 f( [
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1# g* s5 r( T! {2 {
  1. #返回math.e的x(其值为2.71828)次方的值减1+ w, F! ^( `1 ^5 P3 X
  2. expm1(x)
    ) l5 a6 O  s7 W# M, \- X( g
  3. Return exp(x)-1.
    ; ~$ |) |8 ^) g/ l( e
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    3 g2 c0 q# q! q9 S. @- S
  5. 2 b- n* l5 S; [% g
  6. >>> math.expm1(1)
    7 Z+ z6 a) u# g; V& _) z& ^# c9 @8 L
  7. 1.718281828459045* B4 g; g* e6 F2 J+ x4 ~7 O
  8. >>> math.expm1(2)
    * _! I/ p8 ^* d
  9. 6.38905609893065
    * A0 K5 T) y5 T2 K
  10. >>> math.expm1(3): t5 I5 ?) E$ `: _
  11. 19.085536923187668
复制代码
6 ~$ `) y1 |, z
math.fabs(x)  返回x的绝对值
2 k5 E4 s2 L3 S7 W, ]6 x
  1. #返回x的绝对值( K) ]( Z. n8 |
  2. fabs(x)8 V; Q0 ?- _2 S4 T! g
  3. Return the absolute value of the float x.
    7 u% M, d: F% Q! L0 U$ a' {

  4. " R% e' D: J1 Y9 h& c
  5. >>> math.fabs(-0.003)6 B: d% Z% p% s9 j  d; w
  6. 0.003
    ! l2 c, k6 ?' A& ]9 c
  7. >>> math.fabs(-110)3 M1 d2 G1 v' U0 O/ o3 ]* |
  8. 110.0. E2 z- Q# J* |# l: ^
  9. >>> math.fabs(100)
    1 d, v# ?' r$ K; {
  10. 100.0
复制代码
3 w9 y  `. s# ]1 _9 P& q5 w% s7 |
math.factorial(x)  取x的阶乘的值& R# p: h* |* J$ _' Y
  1. #取x的阶乘的值
    ) Y3 N2 r* n" z
  2. factorial(x) -> Integral+ Y) {" i) r* E) ?7 ]" m6 D% U
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ( P3 e- B! ?3 d1 |( G* U5 s
  4. >>> math.factorial(1)
    + ]0 C3 a) ?( q+ p+ S8 V
  5. 1
    / J8 ~$ r3 F% k5 u
  6. >>> math.factorial(2)
    - X, P+ S  n; N6 K
  7. 2' s) [6 D7 r2 U& p
  8. >>> math.factorial(3)! k0 g. b# h4 V$ `. @! L% N
  9. 62 o/ o5 z  z0 A) c) A( H
  10. >>> math.factorial(5)# v8 |' I9 d; L2 K7 ^6 P4 L
  11. 120' u8 e# T% q7 p1 K0 f0 |. }* `
  12. >>> math.factorial(10)
    2 b; T: C9 N' ^3 e/ t  J5 ?
  13. 3628800
复制代码
; |. `. Z. d  d% c
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
8 L% q0 e- h0 @/ g
  1. #得到x/y的余数,其值是一个浮点数
    5 C# b, L- t# L. P
  2. fmod(x, y)( P! a( M1 z5 b. a# e+ M$ }' ~0 K
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    4 F  |4 Y) J+ {
  4. >>> math.fmod(20,3)" s5 l1 ?) s1 N
  5. 2.0( B% b, z9 Q) h6 P; |" V7 d
  6. >>> math.fmod(20,7)
    0 ^4 g% F, B1 S( {4 C4 N
  7. 6.0
复制代码
9 q5 p& \0 {$ _. e! t- c. p
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: o) X+ L7 X3 P0 A. L# U, Z# I
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,7 x& P# ^- w: S2 L
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值' \$ ?8 E$ u* t0 I' \" F7 w
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ' }1 [. K+ u$ S. M: u
  4. frexp(x)
    % r- w- \- _# ]: q& i
  5. Return the mantissa and exponent of x, as pair (m, e).
    - x+ c" z! @, B: w' D4 }# _
  6. m is a float and e is an int, such that x = m * 2.**e.
    / C% j9 R) n# l/ x- ~* R
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    ) Q3 M8 R; p: `  F! k" m
  8. >>> math.frexp(10)
    ' L6 [+ Q0 b( i1 i$ T$ J' j/ t+ k5 K
  9. (0.625, 4)
    - |3 F  J5 B: ~# j
  10. >>> math.frexp(75)
    - P/ B. B% p* U+ N
  11. (0.5859375, 7)+ r+ b: |1 E7 N8 T
  12. >>> math.frexp(-40)
    / n( \8 S& ]/ H, W
  13. (-0.625, 6)3 Z2 |4 b) o- L& q% X" x9 O! E: s
  14. >>> math.frexp(-100)
    0 g9 i' o" a( ?6 X/ s; m8 A
  15. (-0.78125, 7)
    5 f$ Q* b* ^, d! a
  16. >>> math.frexp(100)2 y- E$ v9 J1 I& _  j# s! _) E3 b: V
  17. (0.78125, 7)
复制代码
& C4 u( {% s6 `) o
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
& Z9 }: l/ `' F4 N5 I2 E4 y
  1. #对迭代器里的每个元素进行求和操作9 m; \$ ?5 o% i" X) B8 e. S
  2. fsum(iterable)3 Q0 E( J  V" F# @) w
  3. Return an accurate floating point sum of values in the iterable.
    * s: b/ l4 n' M: c: x, ^/ P1 j
  4. Assumes IEEE-754 floating point arithmetic.# j# s( {  U) Z2 Y* J
  5. >>> math.fsum([1,2,3,4])
    % w2 n/ p5 h9 z) M* s
  6. 10.0
    & ?& _' H7 \. [3 X  K
  7. >>> math.fsum((1,2,3,4))0 ~9 S2 |/ b4 w
  8. 10.0! h, F' [7 Z1 M
  9. >>> math.fsum((-1,-2,-3,-4))
    ) P0 [- v6 Y; }5 |8 k/ ]
  10. -10.0% X) H& L' T$ d) [3 V
  11. >>> math.fsum([-1,-2,-3,-4])
    ! I; {7 H& p3 m- B
  12. -10.0
复制代码
; ?! L. N8 W( w( i) t
math.gcd(x,y)  返回x和y的最大公约数4 Y  `4 ]  Y7 }& h' J9 P
  1. #返回x和y的最大公约数& \; e& ~$ _2 |+ [
  2. gcd(x, y) -> int8 P( @2 J) l& B# i0 T. y8 D
  3. greatest common divisor of x and y
    ; a( v# N7 p" {; o2 l1 Q: U  M( M1 W
  4. >>> math.gcd(8,6)$ N# R- Z( _  C, u& O( l
  5. 2; C/ @& v, ?5 }
  6. >>> math.gcd(40,20)  K" N# M, G4 g. T3 {* A) }
  7. 20
    . D- I# R, d* x- ]/ p4 ^
  8. >>> math.gcd(8,12)
    : e& [& ^3 p8 n$ Y9 e/ Q  ~
  9. 4
复制代码
& D# L8 E# g. r  Y: y5 y
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False  b* j. M6 p% J
  1. #得到(x**2+y**2),平方的值
    / L- e" e+ t1 f% L7 `
  2. hypot(x, y)) n$ }; y  u/ F/ a  V! i8 T
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    5 S2 ?5 O9 Z6 ?$ ?% J8 S) g
  4. >>> math.hypot(3,4)5 ?0 ?* @+ V1 i7 O- I" n
  5. 5.0
    ( C4 ]! ^- s: v
  6. >>> math.hypot(6,8)
    . s; Y! I  _  C+ T  ]  f
  7. 10.0
复制代码

* W% O  k. |% B7 u& N4 a% rmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
+ z+ Z* N6 u# @& V3 }4 t
  1. #如果x是不是无穷大的数字,则返回True,否则返回False' n1 }# Y4 ]6 W' D7 ?
  2. isfinite(x) -> bool
    2 U8 S4 s: N- N! F) w9 T. O
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    # ?3 L7 Z$ T2 G: N  C
  4. >>> math.isfinite(100)6 E" \* w8 ~: ?3 c
  5. True
    & K8 j; s& f* h% _  G& R+ W1 `' D! I
  6. >>> math.isfinite(0)! ^. I- o; g0 {* }+ }- y$ i8 k
  7. True% u4 _+ e# I% g( \1 T! ~
  8. >>> math.isfinite(0.1)
    8 e' L/ `& I4 ?* \
  9. True
    # D$ q. Q" a0 B; _8 X) T+ S
  10. >>> math.isfinite("a")
    1 R' k+ v" i& j' U0 m
  11. >>> math.isfinite(0.0001)# `& A3 [8 N+ {5 g
  12. True
复制代码
) q8 X/ o" C& D8 x  U. y7 r
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
# l9 p' ^0 W4 g) H
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ; G- F9 O" s) l; ?+ W- T% |9 K0 ?
  2. isinf(x) -> bool
    & ]; |9 x. v1 X) a9 T( w% L
  3. Return True if x is a positive or negative infinity, and False otherwise./ A( [) q: W8 U4 O/ p
  4. >>> math.isinf(234)8 j- V- c+ i! m! \# O8 o0 E3 q
  5. False
    7 V- F6 \2 R# y
  6. >>> math.isinf(0.1)
    . k' @+ R" n; d9 @
  7. False
复制代码
+ N3 V4 k1 ~' D8 P+ Q+ Y: ?
math.isnan(x)  如果x不是数字True,否则返回False6 q. z0 }. I# v
  1. #如果x不是数字True,否则返回False6 S1 D& l' P- [; g4 R
  2. isnan(x) -> bool- A, i3 r/ r! D( |' G7 y
  3. Return True if x is a NaN (not a number), and False otherwise., i7 v$ ]; ?, o% l
  4. >>> math.isnan(23)
    3 Q+ ^2 }" o( j6 u/ O! y
  5. False, d: O9 c0 O- h# V
  6. >>> math.isnan(0.01)/ f% l: Y/ R. j
  7. False
复制代码

  a4 [, t( {% r0 p% k5 u) W( L' ymath.ldexp(x,i)  返回x*(2**i)的值. }8 X! h9 R# S8 g# {
  1. #返回x*(2**i)的值5 g& Y. Q0 R0 @$ J3 X
  2. ldexp(x, i)3 |$ y3 \- J+ N3 z) N8 h6 t! D
  3. Return x * (2**i).
    + C. ~  R$ t0 Z
  4. >>> math.ldexp(5,5)
    7 `, a* P6 L$ r9 v* p
  5. 160.02 d8 N  o7 P. d  |) ^
  6. >>> math.ldexp(3,5)+ w# S9 c* y! Z: M* z+ b
  7. 96.0
复制代码
& y  q- O5 G. m/ q# @
math.log10(x)  返回x的以10为底的对数. q5 a4 Z! h; d
  1. #返回x的以10为底的对数- G$ @: _$ `4 L4 M
  2. log10(x)
    3 ]. \0 o4 }0 v
  3. Return the base 10 logarithm of x.% y( k& G  i- G1 f
  4. >>> math.log10(10)
    ( r7 t! D$ l# \1 ^/ k- @5 b0 g
  5. 1.0+ ~  I3 I: A& `5 d6 A% I
  6. >>> math.log10(100)
    % ]5 h" W! Q4 h' H2 E
  7. 2.04 o9 o& i" D3 u; }9 p2 Q
  8. #即10的1.3次方的结果为20* ]4 Z7 T1 Q4 P
  9. >>> math.log10(20)
    3 [- X. m3 N. P3 Q! B9 m
  10. 1.3010299956639813
复制代码
, F" @; d% H7 U5 v, E( [: C& Q
math.log1p(x)  返回x+1的自然对数(基数为e)的值
" V3 a$ d  G9 i, g* W! p6 I
  1. #返回x+1的自然对数(基数为e)的值
    0 Y0 Z3 F( v" @/ ~- T" X. w
  2. log1p(x)
    + t( ?- v  U9 i; l5 V
  3. Return the natural logarithm of 1+x (base e).
    % Q( v( s; F) U! y+ C3 z" v
  4. The result is computed in a way which is accurate for x near zero.% N" t/ ^$ U! }* ?( e# W$ h0 g
  5. >>> math.log(10)$ B) h7 J' d- a$ N- x0 I' P3 }
  6. 2.302585092994046
    0 ~/ r) _# Y5 j9 [& G7 }
  7. >>> math.log1p(10)
    % s4 y/ n) Y4 p7 F1 J
  8. 2.3978952727983707; y7 `5 E! n, @. \4 L8 U
  9. >>> math.log(11)
    - O( }' C# S' ^$ W
  10. 2.3978952727983707
复制代码

- f) @+ _, t# K$ M6 z) umath.log2(x)  返回x的基2对数( B' P% ?6 @" S# _
  1. #返回x的基2对数& y3 J0 X8 a+ t' J
  2. log2(x)
    ( p3 L- m; W: Z
  3. Return the base 2 logarithm of x.
      }" y" t+ H4 s) t
  4. >>> math.log2(32); C$ v( L  z" o$ d" ]/ a3 G
  5. 5.0. J4 k0 j, _* g- x/ }! @* C
  6. >>> math.log2(20)' H. z" c8 |' ?
  7. 4.321928094887363' n# X: A" a9 |( |' T
  8. >>> math.log2(16)
    9 b, W! J; {1 y  r4 j- r* F
  9. 4.0
复制代码
' a0 Q  U! i) e; ]  U) @: G- W
math.modf(x)  返回由x的小数部分和整数部分组成的元组
# s. d0 {( U! z( S1 o
  1. #返回由x的小数部分和整数部分组成的元组
    1 [9 X" H' f4 b0 u. k7 k' l
  2. modf(x)
    : `9 @' R3 U  |" N/ h$ b
  3. Return the fractional and integer parts of x.  Both results carry the sign% i- z; D% X0 ^8 C. y
  4. of x and are floats.
    ) {0 H# B% S# h$ \
  5. >>> math.modf(math.pi)
    # u+ C( R0 e! B. u, l0 R' i' a, ?, ~
  6. (0.14159265358979312, 3.0)
    ( o0 S7 i* {. ~2 Y
  7. >>> math.modf(12.34): h0 G3 K, x6 v& Y' ^: t
  8. (0.33999999999999986, 12.0)
复制代码
9 }$ b& j* `" Y+ m
math.sqrt(x)  求x的平方根# l; j2 a8 c& S% {  Q/ C
  1. #求x的平方根; ?) B. c* T0 j1 D
  2. sqrt(x): X: r' C; U3 A* I
  3. Return the square root of x.
    $ I9 ?# R1 h" ~4 m
  4. >>> math.sqrt(100)
    . `+ k# ?8 \' [) d
  5. 10.06 F. s- Q4 _, w6 S3 ~; x0 t5 c# N# c
  6. >>> math.sqrt(16)
    # m4 d6 u1 J) K
  7. 4.0
    5 Z1 _' N6 t$ @* L* C9 C
  8. >>> math.sqrt(20)
    7 ]7 X$ c: Z" G4 g9 {
  9. 4.47213595499958
复制代码
) A1 i& @1 N0 `1 m# w9 `
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 I& d. [# C: J0 H0 \. n& j  I4 I* `
  2. trunc(x:Real) -> Integral# V- V/ c, E5 i+ V
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.% N! j" @" u5 |7 a% P2 W
  4. >>> math.trunc(6.789)+ l# m& |! L  x1 j4 ?$ K, N4 s/ U
  5. 6' M9 Y- V$ M1 x6 r$ M2 o5 m: h9 x4 z
  6. >>> math.trunc(math.pi)7 Y/ d" I5 [! h; U  y
  7. 35 o) a( r" P' E5 {9 G, b& X7 m
  8. >>> math.trunc(2.567), V, F( i7 U) O% L- _/ Y( R, x
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-19 10:54 , Processed in 0.092240 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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