新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

3 X! N1 O* [% R1 e【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
/ e; K4 b; z0 M2 X2 u  E0 h7 K9 H

: ^/ q/ z# O; M5 ?4 S! B3 \1 d方法1
7 Y8 B8 d) g9 M$ P; S  [
  1. >>> import math- W7 X, v0 a( Q
  2. >>> math.sqrt(9)
    + ^% X( {- T3 p  s
  3. 3.0
复制代码
方法2
, X, G* |  N& B- \& `/ H7 Z* }
  1. >>> from math import sqrt$ A* P+ r: S2 e, p
  2. >>> sqrt(9)
    : m% u( Q4 S1 O- f1 W7 @! M* A( ^
  3. 3.0
复制代码

$ h! k! }# l$ S0 l! |
! T! A# N% @0 M9 \5 Z5 T
math.e  表示一个常量
# G5 ~7 j; J0 s% `6 O& K  ?
  1. #表示一个常量
    4 z+ z" [9 Q, X% p1 E9 Z. V& n; H
  2. >>> math.e& C/ w: B- v4 Y8 c& O
  3. 2.718281828459045
复制代码
. t) V+ L' m' B. M/ o
math.pi  
数字常量,圆周率

) B9 g6 E2 D+ l9 E
  1. #数字常量,圆周率* K$ a3 l3 U6 C+ H; a
  2. >>> print(math.pi)# b0 k, D. G# m8 h# E2 }7 d8 c' X
  3. 3.141592653589793
复制代码
, n5 R2 g) ]' F
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

  Y6 y& k4 P3 A! `
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x* m0 I' s- Y2 |6 l2 [
  2. ceil(x)
    5 U6 [0 k$ r& }
  3. Return the ceiling of x as an int.! x, \8 O0 A, Z
  4. This is the smallest integral value >= x.
    , M9 {1 K0 w7 D! S( g$ a1 \
  5. ( S8 x. T& o* g: L: v5 n) ?
  6. >>> math.ceil(4.01)6 X# P! u2 M& N/ ?3 T& q/ C
  7. 53 t: D4 c# e3 d
  8. >>> math.ceil(4.99)) X5 W/ I' E6 H' c/ \# F2 ~* i3 s
  9. 5
    8 w" Z3 [0 s- u) l* q  b$ r+ I
  10. >>> math.ceil(-3.99)
    : Y6 J9 i/ L& V* W) Y
  11. -3
    ) l5 }9 J: E$ _' k
  12. >>> math.ceil(-3.01)9 G7 \+ e  Z* n" F* Q2 K2 `
  13. -3
复制代码
4 Y0 n% h" g' M
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ w' A% |- Z) ^' ?& @
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- w4 X' i2 w7 N2 i3 u
  2. floor(x)2 C% a. G: M2 c
  3. Return the floor of x as an int.  M9 I4 \6 e+ Z9 z' p, P. _
  4. This is the largest integral value <= x.6 @3 V" m3 Y# w" q/ {, h% [) ?% k9 A
  5. >>> math.floor(4.1)3 i) R7 m3 ?$ h% z0 A' b
  6. 4' p7 D  _4 L% N8 |1 k5 q9 U6 i
  7. >>> math.floor(4.999)
    : p* B/ k+ C  W
  8. 43 k# D; Z" F( ~
  9. >>> math.floor(-4.999): q1 x( E% J% F; r6 K
  10. -5
    2 q8 A7 v, ^" q( }1 y) x( J) L: \. O
  11. >>> math.floor(-4.01)
    % C3 t! l+ W' m2 z
  12. -5
复制代码
* V1 [0 ~4 L0 I" z- c
math.pow(x,y)  返回x的y次方,即x**y7 z# T! I- V& S# O& [' ~) H
  1. #返回x的y次方,即x**y
    8 R# |7 i/ \2 e3 w3 Q
  2. pow(x, y)0 e) D: ?- O) n; z
  3. Return x**y (x to the power of y).& z3 F7 H7 q% g3 Z
  4. >>> math.pow(3,4)
    ' [9 [8 u7 p  r# p' Z7 b" A
  5. 81.02 ^" a* k* k% ~( c; p
  6. >>> ! S& r( {# o8 q" ?
  7. >>> math.pow(2,7); w! ?4 J+ `; G$ P2 I+ g
  8. 128.0
复制代码

% R% ~+ Y4 B6 F- F& {math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base); f; M  V0 M- I/ R( l& {/ k
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 I) L+ H$ d0 b' i" b
  2. log(x[, base])# J! a$ W4 f7 `9 p& _( i7 u  x
  3. Return the logarithm of x to the given base.+ [: e, Z) H# m3 F) W9 J
  4. If the base not specified, returns the natural logarithm (base e) of x.
    , k- q1 _: C8 M% U4 o. V
  5. >>> math.log(10)6 Q; h8 k! G- [+ M/ ?
  6. 2.302585092994046% F5 t/ G2 t5 b  _! j
  7. >>> math.log(11)( w# J# \8 J3 Y6 D
  8. 2.3978952727983707
    % a7 d# X  Y# r
  9. >>> math.log(20)/ C% U% Q' Z1 @. ~! Y
  10. 2.995732273553991
复制代码
5 V$ b- L6 b1 s, R
math.sin(x)  求x(x为弧度)的正弦值
& h! \+ O* k; A" B" V, J. R
  1. #求x(x为弧度)的正弦值
    ; Q; u5 z; o9 \
  2. sin(x)) h8 j2 o. d* x# ]
  3. Return the sine of x (measured in radians).
    , G2 i/ j/ d" t+ _3 J& ?4 N6 o- ?
  4. >>> math.sin(math.pi/4)
    ' z. b+ l  b: N" R: ~- D# T
  5. 0.7071067811865475
    0 l- P0 O& \7 h1 H/ S2 }' R
  6. >>> math.sin(math.pi/2)
    % a) d$ r8 N0 Q# {: u
  7. 1.0
    $ j: E- v  k' x( U! O! s( [( K0 S
  8. >>> math.sin(math.pi/3)8 F. B& a# L7 `8 z& ^# R
  9. 0.8660254037844386
复制代码
' X4 I3 D/ z" D2 X# ]  a
math.cos(x)  求x的余弦,x必须是弧度
8 H2 e# G( j1 i- l, t0 Y4 i/ d
  1. #求x的余弦,x必须是弧度5 U+ b, j5 s) g' F) u6 G- g
  2. cos(x)6 X7 P! p. l% [8 F$ m& M' S/ T
  3. Return the cosine of x (measured in radians)., a& I6 S1 ]8 R2 z
  4. #math.pi/4表示弧度,转换成角度为45度
    , r4 ?$ k6 N8 l# ^  w+ p+ `( }
  5. >>> math.cos(math.pi/4)3 n  z+ \. n1 h% o  O% S6 @" L
  6. 0.7071067811865476
    $ f1 J4 O9 c3 N5 s! f  P7 n" b
  7. math.pi/3表示弧度,转换成角度为60度( K( Z3 J7 j7 n. G3 |4 ?
  8. >>> math.cos(math.pi/3)
    9 _" W+ E2 J0 L4 v2 L( Q4 H! L
  9. 0.5000000000000001
    . r8 a2 }! ~5 W2 H! B$ b& ^3 P
  10. math.pi/6表示弧度,转换成角度为30度
    3 h) D' w7 I8 t% F  I; D6 V( S
  11. >>> math.cos(math.pi/6)
    - Y& i+ b/ D8 B: ?+ D5 n: v( i1 t
  12. 0.8660254037844387
复制代码

& m  j: H/ I& T& c: ?, Emath.tan(x)  返回x(x为弧度)的正切值
0 c( j& k8 e6 L& F6 I5 I0 x' |( T$ c
  1. #返回x(x为弧度)的正切值
    $ y+ L" ]9 Z1 r1 O
  2. tan(x)0 m( @% E% R  ~5 R
  3. Return the tangent of x (measured in radians).
    ( e( V  C2 d  i) x- t& s
  4. >>> math.tan(math.pi/4)
    $ t9 O- V; k: B2 J1 ^" H
  5. 0.9999999999999999
    $ j1 h; {; W* x5 V) B
  6. >>> math.tan(math.pi/6)9 W( N# @! t9 l& w8 j  H7 ^
  7. 0.57735026918962575 m" ~5 I- X4 G
  8. >>> math.tan(math.pi/3)
    $ x' t  H2 G$ Q; O
  9. 1.7320508075688767
复制代码
1 P* R9 S6 d2 u
math.degrees(x)  把x从弧度转换成角度) K+ F$ P2 }$ b3 ~! r5 z1 r
  1. #把x从弧度转换成角度
    $ ]% g( R  C0 z$ n
  2. degrees(x)
    " ^8 G& ~& P& z
  3. Convert angle x from radians to degrees.- l' @) |. J( v+ q- `6 z

  4. % s/ n# h9 c5 q+ a5 J4 ?
  5. >>> math.degrees(math.pi/4)
    / T0 L) {$ V0 Z' w! s! J
  6. 45.05 ~. Z4 @3 k; d+ P, E0 X
  7. >>> math.degrees(math.pi)2 _5 o& L$ o1 r0 o0 {) n% k; _
  8. 180.0
    ; @/ N: [# z3 v" M3 V! X, S8 F- x
  9. >>> math.degrees(math.pi/6)
    , b0 b* u4 K6 X7 z) _8 C( H" U
  10. 29.999999999999996
    + y6 T+ F8 Y3 J1 K3 ?
  11. >>> math.degrees(math.pi/3)
    ' r- P! F; t/ [) l: N: w
  12. 59.99999999999999
复制代码
1 ^! a1 V- b6 }4 l6 x  ]
math.radians(x)  把角度x转换成弧度
9 K; S; k5 L3 B+ j' v
  1. #把角度x转换成弧度
    3 P1 z6 P- q5 j% Y& m* x
  2. radians(x)% d: a2 Q; I6 X4 \$ `0 m
  3. Convert angle x from degrees to radians.5 `6 l* g+ w6 h* s1 ^9 S
  4. >>> math.radians(45)
    & q# d1 s9 R5 q9 e4 M7 U
  5. 0.7853981633974483
    9 i6 Z+ K* a$ c7 P/ G
  6. >>> math.radians(60)  I4 Y* J" H0 R  W  g4 L
  7. 1.0471975511965976
复制代码

; s0 x1 `. d- E  `( ]; cmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
$ G/ `0 |+ {: @) Q
  1. #把y的正负号加到x前面,可以使用0* T- E; _9 F) F- ], u/ V, g
  2. copysign(x, y), ~7 Q! x& `1 k) y4 ?
  3. Return a float with the magnitude (absolute value) of x but the sign % {* R9 q4 H( M2 X
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 W- ^% L" E' @$ v/ ~
  5. returns -1.0.! j  l; N$ Z, \7 [% Z0 ?0 j

  6. ; C2 x- q; K1 b& s3 K/ H1 z! d
  7. >>> math.copysign(2,3)
    . R+ n  ^" ]: x( j
  8. 2.0. F/ J0 q, T0 d7 n
  9. >>> math.copysign(2,-3)
    4 R% D5 N& j; W/ V4 V6 f
  10. -2.0
    2 k9 F% t# b5 O+ [+ ^6 o
  11. >>> math.copysign(3,8)" j! a5 `+ o' ]
  12. 3.06 |! j" b2 \1 s8 f1 i- r* t
  13. >>> math.copysign(3,-8)
    : J$ S1 e; ^# j" w
  14. -3.0
复制代码
2 W( B- P1 I5 w
math.exp(x)  返回math.e,也就是2.71828的x次方/ D& B) O) x' W
  1. #返回math.e,也就是2.71828的x次方
    . I3 m- ~1 `# W( |0 L
  2. exp(x)
    ! i' v5 O: I9 q: g
  3. Return e raised to the power of x.4 ]! I1 B4 a1 q1 I& K. k0 ~! E

  4. - c5 w' p; ?0 E0 |: E8 k
  5. >>> math.exp(1)
    - v0 }" y5 H/ v0 ]' z# _; ^* P
  6. 2.718281828459045) w7 X1 e: B% D2 W2 S
  7. >>> math.exp(2)
    ' V; O7 B  W* i% R
  8. 7.389056098930658 P+ m( X: `% F/ L0 |; a% e1 D* v! r4 y
  9. >>> math.exp(3)
    8 S7 a; p1 |6 ^! r2 P  _5 l
  10. 20.085536923187668
复制代码

* V; V- F  A: r# d/ a- @math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1* s& M0 l, h! c: x) j6 W) A
  1. #返回math.e的x(其值为2.71828)次方的值减1
    / {) b& u- X, `
  2. expm1(x)) w2 B3 T+ U. j# K3 R8 Q2 o- R
  3. Return exp(x)-1.2 T1 g$ P- R2 }4 s
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.* f$ y) W: y) Y! ?: `/ N3 X3 u

  5. , Y0 n1 @& s, Y
  6. >>> math.expm1(1)/ Q% @' G5 {/ A. h$ _: A
  7. 1.718281828459045
    4 e$ B+ b% x- J1 W7 F" |
  8. >>> math.expm1(2)
    " M  m/ _9 M" H/ j+ U& c8 H. @. W  i
  9. 6.389056098930658 B, m+ n% i: f' F4 Y, g
  10. >>> math.expm1(3)
    7 ^7 n5 r" U" w- \3 p9 p
  11. 19.085536923187668
复制代码

8 B( l) {; S0 J3 m+ {/ y: Pmath.fabs(x)  返回x的绝对值
, X8 @3 p0 X0 H7 z6 F! E
  1. #返回x的绝对值0 k, M, a: k4 u
  2. fabs(x): w. H0 v8 n2 X& K. A
  3. Return the absolute value of the float x.+ ]- K" m& e3 M2 n, Z) V! t

  4. 5 i) ~  H# w6 p& j9 @8 f
  5. >>> math.fabs(-0.003)
    $ `  y1 w% @1 o7 q- `, T( o
  6. 0.003- N  A3 r$ }" R# \  F* H
  7. >>> math.fabs(-110)  L- S* w# q+ p* s( A3 x! c
  8. 110.0
    , c0 |+ E, E. ^, }& _
  9. >>> math.fabs(100)
    - N8 t' l1 W+ Y4 x
  10. 100.0
复制代码
7 i' S- W, {& |
math.factorial(x)  取x的阶乘的值
( P  A9 t# \- Q8 b# |( R6 \5 l
  1. #取x的阶乘的值+ o+ y( {: G) i5 |: M
  2. factorial(x) -> Integral* ~: @$ ?( L8 {# h
  3. Find x!. Raise a ValueError if x is negative or non-integral.# V' m5 T0 \$ i5 `( \
  4. >>> math.factorial(1)# f5 D# n+ L5 _2 X; L% h/ x0 z# O
  5. 1
    / g9 V! t) ?( U8 q" _
  6. >>> math.factorial(2)
    6 S  g4 Y* }- V
  7. 29 ]0 b2 n( Y5 Q. |  {
  8. >>> math.factorial(3)
    " {# [8 k4 F, b8 O* k4 s! o
  9. 68 n6 d1 v4 E( E3 X0 W8 i2 C
  10. >>> math.factorial(5)
    8 V7 n: h9 [( ^& ?2 ]
  11. 120
    ( J" t1 l+ \) ~6 s4 H+ B+ Z" N: A) l
  12. >>> math.factorial(10)
    8 t# N& }& T: R
  13. 3628800
复制代码
0 m1 K* A/ b$ g" Z+ }
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
0 t) V5 j8 h) q0 _9 e
  1. #得到x/y的余数,其值是一个浮点数; Q1 A; m: i4 [4 t4 Y
  2. fmod(x, y)
    3 z; R' A( U8 X9 L
  3. Return fmod(x, y), according to platform C.  x % y may differ.9 P  e5 f" c$ }" P5 k$ V& e& y
  4. >>> math.fmod(20,3)
    $ r  Z, Q! a( b/ N% [( Y
  5. 2.0
    4 T1 [  L) t! N+ a; Q
  6. >>> math.fmod(20,7)
    , o- C- B1 m; s: T  j) m, D1 Z" X
  7. 6.0
复制代码

: s" Z0 n6 {9 ?) {' jmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 O/ a' O# d# {5 R4 w' _7 [
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ; p0 s) V' s( M
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 `# B- [* H9 B8 C6 J  l7 }
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1- }/ M8 W. J! R0 c! I
  4. frexp(x)
    + d& C5 u3 p, g5 J) u- X4 q
  5. Return the mantissa and exponent of x, as pair (m, e).
    " K& w6 `7 i3 u; c+ Q! F+ O
  6. m is a float and e is an int, such that x = m * 2.**e.# ~- o9 x! ?4 B  U1 R
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.  t% y0 z$ E1 S0 ]# ~% F" x
  8. >>> math.frexp(10)9 l# {) r) n, w; i2 G, d# U
  9. (0.625, 4)2 |) T0 t! B( k) t. J. C
  10. >>> math.frexp(75)
    ; H0 {2 @( I: c( R( t
  11. (0.5859375, 7)
    & w7 H  d  |9 \. D
  12. >>> math.frexp(-40)
    1 I& J2 y4 {+ c  j3 }  P
  13. (-0.625, 6)
    3 `9 X; c6 ?1 t4 t* ?
  14. >>> math.frexp(-100)3 `; m% W. N( }: _
  15. (-0.78125, 7)
    5 [' a5 {6 e- v$ H* q
  16. >>> math.frexp(100)
    * ^- @6 n( B6 |# u
  17. (0.78125, 7)
复制代码

, a7 j" U' \7 t$ rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
# ]; b- \' H! ]" U3 Y" [
  1. #对迭代器里的每个元素进行求和操作
    ) O' d/ U$ a6 f0 e! j
  2. fsum(iterable)( _% B8 s, u5 o9 S1 ^
  3. Return an accurate floating point sum of values in the iterable.
    + c; l0 |; v$ \9 Q( x+ d
  4. Assumes IEEE-754 floating point arithmetic.
    % i2 C! Y3 e; h) O6 c
  5. >>> math.fsum([1,2,3,4])
    1 R3 ^/ a+ s0 Q/ i7 ~/ T0 S
  6. 10.0
    0 @( N+ h% M1 S3 N/ Y! U( ~
  7. >>> math.fsum((1,2,3,4))
    3 \* f* M+ _0 a3 O: F
  8. 10.0
    # e! T' @/ m! ]. a4 @5 s6 V
  9. >>> math.fsum((-1,-2,-3,-4)), w/ k! j- V- B- U) |  _
  10. -10.0: a: e$ i$ ]: d: Z! U
  11. >>> math.fsum([-1,-2,-3,-4]): @7 A+ }, J2 ]- y5 ~
  12. -10.0
复制代码

. Q8 e/ O" P; s2 Y* T3 Y5 lmath.gcd(x,y)  返回x和y的最大公约数
) l- N3 D$ [2 c; T8 R
  1. #返回x和y的最大公约数* ?3 t' O6 I9 @% c( w% i0 b& n: }
  2. gcd(x, y) -> int* X1 ]( R  f, s6 X
  3. greatest common divisor of x and y
    1 o# D: i* d+ l5 `
  4. >>> math.gcd(8,6)  e" ?1 A( n4 j! D
  5. 28 v7 y# r2 x3 d' C: Q' ?; i+ C4 d
  6. >>> math.gcd(40,20)
    7 K: R. a9 h6 p9 P0 S; t0 _
  7. 20
    5 f1 ^3 c8 D( n0 K! H: F9 M
  8. >>> math.gcd(8,12); f4 U, I$ a. i, |
  9. 4
复制代码
! I7 v% k) t! @/ l" W& e
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False$ A4 i3 j5 E; H. A) o
  1. #得到(x**2+y**2),平方的值8 N, W+ a8 J( ^6 S; ^
  2. hypot(x, y)9 E. Z; s2 k4 g8 E, {
  3. Return the Euclidean distance, sqrt(x*x + y*y).5 z0 z" f5 U2 y5 v% N
  4. >>> math.hypot(3,4)
    3 @! z8 @& m0 ?) S* c
  5. 5.0
    ' }) L  `3 R* \5 {
  6. >>> math.hypot(6,8)
    3 r2 b; q: u: _' N8 q
  7. 10.0
复制代码

# v4 h% y1 d" V6 P, P+ }! h. e& Vmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
! h9 r$ m6 W  ]9 p1 V
  1. #如果x是不是无穷大的数字,则返回True,否则返回False4 U: H" K+ \; o; o4 \% x4 @3 g
  2. isfinite(x) -> bool
    7 [5 C+ g' H8 L2 q4 t; p
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    1 v" g/ O! v" t! Q& |
  4. >>> math.isfinite(100)
    ' D9 u$ ~9 H" S1 e
  5. True
    ' C8 r+ M2 j0 c& R
  6. >>> math.isfinite(0)
    / U  _9 N6 I, A* r% f
  7. True
    ' f9 o, J6 [0 E
  8. >>> math.isfinite(0.1)
    . h3 K3 A# x1 {" x1 w3 i1 e
  9. True4 e$ _8 D5 ?7 `* v- P
  10. >>> math.isfinite("a"): k3 t  \9 E: }) Y, T8 a% {
  11. >>> math.isfinite(0.0001)
    & O: u9 d  b# |2 J0 U1 d' j
  12. True
复制代码

# ~/ u3 ^: h5 G1 \/ {# ?1 {math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False2 w$ S- y" _) @! u. y
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ( A' I9 q- w# A& C2 `- G9 \; S
  2. isinf(x) -> bool) t* a2 W( S* S. L
  3. Return True if x is a positive or negative infinity, and False otherwise., O9 B/ M# H7 d/ i) ]1 R
  4. >>> math.isinf(234)4 {' ^! @6 o1 q& k! c( L
  5. False
      l2 j1 B2 m% o! n2 g# d
  6. >>> math.isinf(0.1)
    - Y0 g5 T0 p$ K) R  z6 o4 p% C" h
  7. False
复制代码
7 U: z( Y, a! Z
math.isnan(x)  如果x不是数字True,否则返回False% V) F4 c2 H7 q9 X$ a9 X
  1. #如果x不是数字True,否则返回False+ _9 J( _7 p# e# [  _8 j4 c
  2. isnan(x) -> bool
    + f3 \" z' X2 d" J, l
  3. Return True if x is a NaN (not a number), and False otherwise.
    ( X0 u$ J& w# V
  4. >>> math.isnan(23)
    - O: T/ k4 M1 C: V
  5. False
    * \9 V+ v$ e2 E  ^% ^: ?- A
  6. >>> math.isnan(0.01)
    3 K0 I: {8 e% v
  7. False
复制代码

/ Y  i2 m5 E& \* [* qmath.ldexp(x,i)  返回x*(2**i)的值* L: M" u* ~/ R0 k4 V- r1 |
  1. #返回x*(2**i)的值
    " `- C3 Q6 }! Q& u
  2. ldexp(x, i)
    8 ?  ?$ J1 e6 d; C- [0 L
  3. Return x * (2**i).8 x+ o9 V9 E  `2 ]" B
  4. >>> math.ldexp(5,5)1 @/ V6 s* j' f% ]& I! f
  5. 160.0* R8 S5 L# C) x  R
  6. >>> math.ldexp(3,5)
    6 P# A* S6 h+ j" ^6 t' w& T
  7. 96.0
复制代码
8 a3 m+ r: }7 w# V
math.log10(x)  返回x的以10为底的对数
7 t& E) K) \8 a0 {: F
  1. #返回x的以10为底的对数( R, g. r. G2 ~% X" n
  2. log10(x)! J, j) M, \. @& i" Z3 f0 g2 h
  3. Return the base 10 logarithm of x.
    6 ?2 r% [) {/ a
  4. >>> math.log10(10)9 u: L9 K6 m2 x6 i$ u0 Z- i2 V
  5. 1.0& [" E5 s& @6 O# n
  6. >>> math.log10(100)
    5 D  c; X- c1 i$ j) C
  7. 2.0
    + e6 F$ W3 c, t
  8. #即10的1.3次方的结果为203 y  N/ w* b6 m" ]. Z
  9. >>> math.log10(20)2 A( j1 J( s( t8 j- s7 H7 R
  10. 1.3010299956639813
复制代码

: x/ Z4 Y  ]; y3 Hmath.log1p(x)  返回x+1的自然对数(基数为e)的值' ]6 C" d' e4 y: v: R# g' w
  1. #返回x+1的自然对数(基数为e)的值. A1 P/ I0 X" W- [, Z. q
  2. log1p(x). I: n0 c+ @' B6 B; d8 @" [+ [
  3. Return the natural logarithm of 1+x (base e).
    " E  [/ B2 U9 q
  4. The result is computed in a way which is accurate for x near zero.+ ~6 n/ B; f5 l1 g$ B5 d: N
  5. >>> math.log(10)2 ]* ]3 m: u$ j$ _; r2 E
  6. 2.302585092994046
    ) W' f- O, Z% T# [4 n4 l
  7. >>> math.log1p(10)
    ( N* t% q2 ^: f3 O- l+ z. x% G: ~4 T
  8. 2.39789527279837071 f9 n5 _; O3 c" @9 ]: W9 D
  9. >>> math.log(11)" ]( |, z7 s9 f; U; V/ l8 v5 k% y' i
  10. 2.3978952727983707
复制代码

+ d1 \" u2 _  ]0 a' @5 L/ Smath.log2(x)  返回x的基2对数' B6 o/ \- O2 q, \3 O+ z7 b; w# f9 T
  1. #返回x的基2对数
    / ~# _  r8 T4 m, H  l$ A7 R
  2. log2(x)
      C! d# b8 |& @4 P; t
  3. Return the base 2 logarithm of x.
    % e+ y8 P* q' t1 b1 ]' t  }
  4. >>> math.log2(32)! {) x7 J& P; F1 C9 u
  5. 5.0
    4 ]; k' O. d9 I, [. u6 l
  6. >>> math.log2(20)6 s3 Y8 F. ~0 w0 F: R9 A& F, A
  7. 4.321928094887363& q7 Y, B* r5 U" _* O
  8. >>> math.log2(16)4 n4 j9 q5 K% }: Y
  9. 4.0
复制代码
4 c$ S! _% u' a& D0 e8 L3 s% `
math.modf(x)  返回由x的小数部分和整数部分组成的元组
2 R# g/ `6 S0 J3 v1 t
  1. #返回由x的小数部分和整数部分组成的元组" G: Z1 x/ P) Y& E
  2. modf(x)
    4 @7 U4 h' P( \. _1 U9 l
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ' d0 U  y5 f1 ?+ b/ X# q; N
  4. of x and are floats.
    & D  P2 S1 \5 q4 q" x& A
  5. >>> math.modf(math.pi)4 z2 X, f# n5 H3 O/ p# e% F
  6. (0.14159265358979312, 3.0)5 b6 |# t& {3 ^8 f% Q3 @
  7. >>> math.modf(12.34)4 x" x7 o' A; O( i
  8. (0.33999999999999986, 12.0)
复制代码
/ r% P# ?6 J7 ]& R/ g5 J
math.sqrt(x)  求x的平方根3 d1 ?. o8 f5 x1 q& \+ e" J- b
  1. #求x的平方根4 F& ?  Y; P- k+ F& {4 d4 U
  2. sqrt(x)
    ; C) m4 Q7 [3 [7 Q0 u
  3. Return the square root of x." u  D! s* `& n
  4. >>> math.sqrt(100)
    3 f6 \; `* B2 x8 o
  5. 10.0' j8 u6 G/ S6 ~. C
  6. >>> math.sqrt(16)! t8 k2 v7 ^' w
  7. 4.0
    ! o5 c2 o/ ]7 z- O' w9 Y
  8. >>> math.sqrt(20)
    ! h  A0 b8 R9 o- I2 c. u; [7 C
  9. 4.47213595499958
复制代码
4 u; w! h8 _% G$ d; K
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    5 h- H4 c2 N+ z$ w* K2 [. F* z
  2. trunc(x:Real) -> Integral: ]! ~1 {! n; i% n, n
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.9 n$ M1 u- n' C4 [' j; x; E
  4. >>> math.trunc(6.789)
    " Q: Y4 X1 r. E9 m( D
  5. 6' f1 y8 D/ ^+ ?. W
  6. >>> math.trunc(math.pi)# x3 H. w% R' T
  7. 3
    0 g7 R/ J5 u  r. k# m
  8. >>> math.trunc(2.567)
    $ j, U" M# K' f6 {- x; [
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-20 21:06 , Processed in 0.081923 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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