新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
- @. q0 K1 N) o/ z( H- @
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
4 X! k7 x9 |+ C6 D  B
$ |9 _* G& h* Q9 U3 i5 V2 e$ i0 A
方法1
- R' w, C- T+ \3 I7 l% ]6 h
  1. >>> import math
    & n1 b- s( o, v+ A$ _# g+ ^9 T. U/ ^
  2. >>> math.sqrt(9)
    . f1 n& J$ j( ]; h& a& x* @5 @
  3. 3.0
复制代码
方法2
  \8 L/ c6 i) V" Q. ~' a3 x
  1. >>> from math import sqrt# _/ c; C4 y3 j
  2. >>> sqrt(9)
    2 W9 ?* h9 \6 u0 \4 S) N0 C* e' o: Z
  3. 3.0
复制代码
. u; c: a; ]: A3 z2 C* s+ D/ z

7 z  a4 ^. f0 v- g
math.e  表示一个常量7 h1 }8 L8 [% a9 Q/ _) J
  1. #表示一个常量
    ( [" s3 r1 ]9 h$ o- F5 a* ?- y1 }( k
  2. >>> math.e
    . C& Z  O3 O& I
  3. 2.718281828459045
复制代码

% F' M6 F9 A0 _7 t3 O; Emath.pi  
数字常量,圆周率

$ R# t: [" i  J0 g1 g" V* ]
  1. #数字常量,圆周率
    ; o7 z9 p5 e/ C
  2. >>> print(math.pi)' h2 k! \8 x( @# X
  3. 3.141592653589793
复制代码
! _$ U% S" w* ~" t
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

, {% U0 i% h$ w8 c' @" o
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    * @0 w  G/ x8 J, @- r* J" I' ~4 _
  2. ceil(x)
    7 }0 A8 u+ [9 o8 _  O; T0 z( p$ g
  3. Return the ceiling of x as an int.& Y8 Y, ]7 j: l& x. H
  4. This is the smallest integral value >= x.) \0 _# l6 S5 ~  d: ?8 \

  5. 2 {1 ]: Y6 t% a9 X, I/ ^
  6. >>> math.ceil(4.01)
    . V/ h" a! ?. Q
  7. 5
    & X4 z+ V9 I) `/ c; N4 V. C) F
  8. >>> math.ceil(4.99)
    2 |1 {9 X* G( s
  9. 5+ n* |6 `( ]4 s3 w/ m3 c1 z
  10. >>> math.ceil(-3.99)
    , m1 h( p  I9 `9 U- i- t/ K
  11. -3
    6 M4 f9 m: b4 L. C; r
  12. >>> math.ceil(-3.01)% c, f8 H# n/ t6 ]
  13. -3
复制代码
1 z( G/ c/ X! F1 f$ y
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) H! K/ i6 [' I8 H1 a* D
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      y0 e  {+ i$ e& Y0 E
  2. floor(x)/ X9 F/ r( ~0 M
  3. Return the floor of x as an int.
    3 F5 P& K, G( i. Y* d
  4. This is the largest integral value <= x.4 J: {! k; ~2 b) c9 B* D0 ~1 @
  5. >>> math.floor(4.1)
    7 d" y' j. b/ |, R, Y
  6. 42 H& p8 E( t2 w8 \! L: z
  7. >>> math.floor(4.999)/ d  S7 i/ j) x' ?
  8. 45 w: B0 K; C, I+ R  J# W9 ?% \
  9. >>> math.floor(-4.999): b7 P+ f# m  D: K! _; g$ B
  10. -5$ G2 R, _% B) J4 R
  11. >>> math.floor(-4.01)3 ]& m: G( x, y! H8 ^8 ^: f, E
  12. -5
复制代码

# F! @5 ^0 C7 h  F8 M/ y$ [math.pow(x,y)  返回x的y次方,即x**y. @$ U2 u: E+ O8 w  @! s# Y* l
  1. #返回x的y次方,即x**y! Y% ?4 ~* g/ V8 R! l
  2. pow(x, y)
    0 M7 o- t, R7 A4 ~3 b& p8 V8 Q
  3. Return x**y (x to the power of y).. @+ T. Z1 s4 h- q* O/ T
  4. >>> math.pow(3,4); f4 q4 D5 q% A4 P' J
  5. 81.04 E% r: k1 Z" [7 K( b- I* y
  6. >>> 5 S% n5 E" S/ q- R# x
  7. >>> math.pow(2,7)6 a# e" m  e' y1 v
  8. 128.0
复制代码
2 ^0 S# }, d* Q' m$ v" Y$ N
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
* \/ P6 y; N0 h6 @
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    : B* @+ j7 x* n& q. m
  2. log(x[, base])
    . K' s: ^. h$ f# U/ C2 J
  3. Return the logarithm of x to the given base.
    0 u; Y1 \- P9 S' ]( F
  4. If the base not specified, returns the natural logarithm (base e) of x./ y% f+ f% H/ I: o6 O" |4 @. v
  5. >>> math.log(10)3 G$ d' E; p9 p0 H
  6. 2.302585092994046
    9 Y1 F  ?7 k/ k/ r$ p6 H4 |9 F; S
  7. >>> math.log(11)4 B% s) w- T0 C4 H  s& Y
  8. 2.3978952727983707" r$ M5 A" k: z1 T2 c1 f5 D
  9. >>> math.log(20); o5 e' ]' p& ]9 W, X* T; O
  10. 2.995732273553991
复制代码
5 E* v. N, l! o: k
math.sin(x)  求x(x为弧度)的正弦值7 o5 Z9 R: n# G
  1. #求x(x为弧度)的正弦值8 K4 m& |, H- ~+ f. [
  2. sin(x)0 r0 J  F; t, l% t
  3. Return the sine of x (measured in radians).
    ; k$ Z. W) r0 w0 R+ o! U0 `. @. p7 o$ F
  4. >>> math.sin(math.pi/4)
    # s+ A" i/ r6 a" y$ q7 @
  5. 0.7071067811865475, ^. _9 M' V; ?: E/ Y1 b
  6. >>> math.sin(math.pi/2)% p8 q0 H0 n1 {7 z6 }
  7. 1.0
    ! N0 ]  f( o! ~4 Z. K& |) Y
  8. >>> math.sin(math.pi/3)9 c+ w; L9 a& _1 [7 a8 J( ?
  9. 0.8660254037844386
复制代码

% ~% |& ~. ]! fmath.cos(x)  求x的余弦,x必须是弧度
- c; {. F! `' j( J) _
  1. #求x的余弦,x必须是弧度9 a/ J3 L. ], _  g6 }9 v! ~. ]$ l
  2. cos(x)
    0 i/ u6 k2 \3 }7 |) `2 ]5 q! Z
  3. Return the cosine of x (measured in radians).4 K5 [, @* E0 X4 l0 L) M, J
  4. #math.pi/4表示弧度,转换成角度为45度
    0 D" P: s7 F  |5 t$ P
  5. >>> math.cos(math.pi/4)
    1 h. m3 W: m" ~' y- k
  6. 0.7071067811865476
    6 `! v+ z+ `, D  O
  7. math.pi/3表示弧度,转换成角度为60度
    5 K( a9 k% a. V
  8. >>> math.cos(math.pi/3)
    $ l% _; G, c: ^; e1 o9 E
  9. 0.5000000000000001
    % l0 u' e6 z/ K( H, j* d
  10. math.pi/6表示弧度,转换成角度为30度) A, A  S0 d, o
  11. >>> math.cos(math.pi/6)7 f% P% a" ]; M* C' p8 p
  12. 0.8660254037844387
复制代码

: }2 |( r1 m% w3 L" l: {& n! {! Mmath.tan(x)  返回x(x为弧度)的正切值
/ X1 J8 {1 H5 b* c% ^* V9 E
  1. #返回x(x为弧度)的正切值
    : U9 O. |  H" L& T  \
  2. tan(x)
    1 |/ K( M$ v! V. u3 C
  3. Return the tangent of x (measured in radians).
    # I7 n  P: o. E# G
  4. >>> math.tan(math.pi/4)
    , v! X: ?- g& N8 s  A
  5. 0.99999999999999998 {0 i, E5 [1 P/ r9 _, ^2 E
  6. >>> math.tan(math.pi/6)( Y) t# x1 p1 |5 E
  7. 0.5773502691896257+ X3 q! }; H; D+ f
  8. >>> math.tan(math.pi/3)
    ' Y: c+ o' E+ a  D- T, G2 q
  9. 1.7320508075688767
复制代码
+ _- g* P& P: z' \/ N% h
math.degrees(x)  把x从弧度转换成角度1 {/ B, u# V% X
  1. #把x从弧度转换成角度
    ' T* `  |# }$ o+ O( T' d
  2. degrees(x)
    ) ?9 }& A0 }5 ~
  3. Convert angle x from radians to degrees.5 t7 R, z; [' s, q  P! D
  4. $ @2 |$ x  T0 A8 f: z
  5. >>> math.degrees(math.pi/4)' v/ \# h: i6 r3 e
  6. 45.02 q/ j) _0 G( h! k: g( \
  7. >>> math.degrees(math.pi); t+ v* M4 c: n: v9 e5 B0 u
  8. 180.0
      ~/ \  ~9 o, J/ B
  9. >>> math.degrees(math.pi/6)/ }) G- s; U$ z# c( A
  10. 29.999999999999996+ Y0 k7 C; k' C
  11. >>> math.degrees(math.pi/3)
    ; y5 W8 L7 L+ G) y
  12. 59.99999999999999
复制代码

/ Y4 C7 t) k: g" umath.radians(x)  把角度x转换成弧度
! r2 z! ]# Z  G$ r
  1. #把角度x转换成弧度5 \) G3 M7 E* B, r4 r
  2. radians(x)! n/ n5 J/ e( h+ q' `
  3. Convert angle x from degrees to radians.* n) B- A: y( j) d/ B0 S
  4. >>> math.radians(45)
    ' L6 V  ^# G/ H: V$ h3 [
  5. 0.7853981633974483
    + S- r2 J: i! c! B
  6. >>> math.radians(60)
    : |8 R% {7 t: E, P3 b" V) B
  7. 1.0471975511965976
复制代码

7 X9 z' ?2 m7 H& D1 F& l9 bmath.copysign(x,y)  把y的正负号加到x前面,可以使用0/ G0 D* [6 d7 R9 Q+ t
  1. #把y的正负号加到x前面,可以使用0
    ! K# r) v% J0 g2 \* R  M9 p" j: L
  2. copysign(x, y)
    9 x- H: [. s  g- n! J/ E
  3. Return a float with the magnitude (absolute value) of x but the sign $ G- g3 \8 D! L8 d+ f! n' w+ a
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & y8 V( j2 V7 \/ b+ F/ {
  5. returns -1.0.; l; `& I6 D0 }# R+ i

  6. # V7 J; c' M, F2 d
  7. >>> math.copysign(2,3)
    ( w& e. w8 l% k; r% N
  8. 2.0- y& }0 ?- ]6 V1 e3 v5 u
  9. >>> math.copysign(2,-3)) p7 X& L% w  @  e6 H  @
  10. -2.0! Z. ~0 [; I( J* U
  11. >>> math.copysign(3,8)
    # k* k8 @# \/ e7 y( G
  12. 3.05 |! ]) i. L1 X
  13. >>> math.copysign(3,-8)
    - x+ o. i5 w  @7 k
  14. -3.0
复制代码

' A8 F5 U- B" v: ^' C5 ~math.exp(x)  返回math.e,也就是2.71828的x次方
- D4 R1 s, D4 f: u# P+ H& L) x
  1. #返回math.e,也就是2.71828的x次方
    ( C. I: x: M; R* X
  2. exp(x)  @8 j* B/ ^( z
  3. Return e raised to the power of x." B0 g# J! M8 _9 t3 l& W; {
  4. % h1 U* e8 c$ r4 P3 R9 x! @
  5. >>> math.exp(1)
    & Z8 b) d  y1 J8 D$ e- W" Y
  6. 2.718281828459045
    $ ]3 Y0 Z1 N) a& u6 L
  7. >>> math.exp(2)0 ~% G5 i3 o: i% x8 w) {
  8. 7.38905609893065
    , ~0 m0 a, ?. s, X- M& S* g
  9. >>> math.exp(3)/ I5 J. W5 o7 |$ j# |
  10. 20.085536923187668
复制代码

& k0 Q7 Q; O) m: Dmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1( w' A0 f! E% M7 k* W! n- [
  1. #返回math.e的x(其值为2.71828)次方的值减1, l( V& l* n( J* e& b) J
  2. expm1(x)
    : e2 L: y! i* {: `
  3. Return exp(x)-1." n! w' @: f7 X
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    7 C9 \, z, ?' ~

  5. 9 \2 m. Z' f& q! u
  6. >>> math.expm1(1)# M' |* g0 G7 L# N5 Y0 k6 {4 r
  7. 1.718281828459045
    / s1 e6 s% Y8 ]+ l# I
  8. >>> math.expm1(2)2 V% x/ c2 O. C$ z7 Y1 d
  9. 6.389056098930652 r( Q: v! Z8 S  x+ ?, D
  10. >>> math.expm1(3)
    8 }8 ?1 B7 _: i# W+ V+ K5 p, R
  11. 19.085536923187668
复制代码
" G# T/ q% R* a
math.fabs(x)  返回x的绝对值
0 h1 g0 H( a" g) A- S- o
  1. #返回x的绝对值7 y% N0 {- ], N
  2. fabs(x)8 G+ y" q% J. ~- |3 Q' i3 S. P
  3. Return the absolute value of the float x.
    # \. C- U4 W/ n5 s* ^2 p

  4. ! f' B% h9 ]7 @! Z4 Y
  5. >>> math.fabs(-0.003)
    " J3 j+ J2 s9 X+ I
  6. 0.003
    * F% r" D4 W) Z. u/ `9 `
  7. >>> math.fabs(-110)
    5 [: X+ `( Y) T+ s5 F% y$ v
  8. 110.01 G; @" P9 U% {- Z1 c) b
  9. >>> math.fabs(100)
    3 S: ?9 B% U% @$ J
  10. 100.0
复制代码
7 Y% e! k( i: f6 L% c
math.factorial(x)  取x的阶乘的值
& Q1 }2 d5 s$ O# T7 ?# k, ~2 f% ~
  1. #取x的阶乘的值
    $ Z$ \+ J3 X1 z; W3 L1 E3 C
  2. factorial(x) -> Integral
    ! a) x" D- F  Z' \
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    6 {/ B! G7 K9 g9 d% r
  4. >>> math.factorial(1)4 }5 V* v5 u: r
  5. 17 k8 @  R, f: ]2 @2 ~9 ?
  6. >>> math.factorial(2)& O& p3 T: P3 |% `& x
  7. 2
    , n9 D  C& G% B5 Q! \' d
  8. >>> math.factorial(3)
    " `: e! H8 C. r! t" w: x
  9. 6. o# U8 w, c# @- x
  10. >>> math.factorial(5)
    * T: S2 m& x! g+ W5 O0 n% l+ y
  11. 120
    4 H/ I) r# \# L4 E( e
  12. >>> math.factorial(10)
    # |7 ~6 `, R" Y  E. x8 _
  13. 3628800
复制代码

* D. H2 r5 l) |0 D& k2 N1 xmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数+ p$ i% c) Q! d# {% v1 z: k% \
  1. #得到x/y的余数,其值是一个浮点数
    * }9 V2 c; }3 N
  2. fmod(x, y)0 ^7 v1 v2 ]/ V
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    ) }& Z6 M/ A" E2 q" u3 N8 n, F$ C
  4. >>> math.fmod(20,3)
    0 f: o4 d) T$ r/ y( `8 K- m
  5. 2.0
    8 ?9 ]# V7 @1 {2 L9 V- q
  6. >>> math.fmod(20,7)
    7 H& q( D5 J: \! W0 {
  7. 6.0
复制代码
9 D$ L) o9 U) n# L# h8 x: \% Y2 T* {
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
2 C: \1 G' @  A9 N0 z* K! _2 P
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,  v) \, i  m7 Q
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    6 y6 C1 \, m9 A6 r8 g
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    / U2 B: Q/ W# M/ h  [
  4. frexp(x)# _# b0 p. d. ?' C! A
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 ], u3 k% j; F* [
  6. m is a float and e is an int, such that x = m * 2.**e.
    + y; _8 w7 b5 c) X- L3 _9 b" f
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 Q% e4 s( c/ `9 l
  8. >>> math.frexp(10)
    , h! ?" |3 i% J$ z# m! [# M; C0 R- F5 P
  9. (0.625, 4)/ M+ }& m" `+ n" p! A/ ~
  10. >>> math.frexp(75)3 I2 B4 z5 O3 @5 a
  11. (0.5859375, 7)5 l' \# X6 D% L
  12. >>> math.frexp(-40)
    # x1 F& o! `% y$ w1 ?
  13. (-0.625, 6)
    . o" @' ]8 w, E- p1 Z
  14. >>> math.frexp(-100)' T$ I% m) V) t
  15. (-0.78125, 7)
    . _+ D4 y) G* N) ~' _
  16. >>> math.frexp(100)
      ~" s/ |; E' n( a- o/ k( H4 [
  17. (0.78125, 7)
复制代码

9 C' ?: d- o$ \; u2 {math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列0 q: R+ [+ k9 ?( v; S+ E% F
  1. #对迭代器里的每个元素进行求和操作( a- i  n. q( I/ l. G4 C1 T
  2. fsum(iterable)# B: q" C( D1 X
  3. Return an accurate floating point sum of values in the iterable.% k" M4 c. a3 `0 ]* z* D2 u
  4. Assumes IEEE-754 floating point arithmetic.4 ^2 s3 E  _; Q
  5. >>> math.fsum([1,2,3,4])
    * w; C) G1 R, e* ^
  6. 10.0+ p) e6 e6 S: a3 f+ R4 n
  7. >>> math.fsum((1,2,3,4))
    9 L; S+ [6 x2 D; q
  8. 10.0# R) w9 l2 n+ i% n  v" h4 V% T
  9. >>> math.fsum((-1,-2,-3,-4))
    ' b6 T8 @1 D! ?
  10. -10.08 I3 ^$ y7 U2 w; ?( X
  11. >>> math.fsum([-1,-2,-3,-4])+ @1 b- d) F( D' X% y
  12. -10.0
复制代码
& I: t# O7 X, D% t* ^1 [! q
math.gcd(x,y)  返回x和y的最大公约数
$ g5 ^: Y) Z2 n" r7 I
  1. #返回x和y的最大公约数
    0 m! ^  k- L( Y3 U  h
  2. gcd(x, y) -> int
    1 Z, z2 F$ h5 a# V! d: |9 x
  3. greatest common divisor of x and y( m! _" n! S( Z
  4. >>> math.gcd(8,6)
    4 z9 B. L& i, b- Y- Z7 X
  5. 2' h  O, S8 G4 S. C6 V
  6. >>> math.gcd(40,20)
    ( J0 G" A" t3 }" r
  7. 20
    $ p8 x% L& z: [! @: i6 _3 g) d
  8. >>> math.gcd(8,12)& d, J% U9 ~  F, V1 [* z$ a
  9. 4
复制代码

# ]  S' t, _4 D$ z3 v" m7 F& rmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
8 ?4 t) j7 m1 I$ ~2 S9 }/ ~
  1. #得到(x**2+y**2),平方的值
    6 D6 D+ y5 f- E* k! J! D
  2. hypot(x, y)
    & x, i" B# ]6 q4 A2 C$ f. X. A0 G
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    0 x5 X: r. Z6 i% _5 ?
  4. >>> math.hypot(3,4)
    6 L2 z, w) B0 L$ W
  5. 5.09 P$ x2 r  l2 m/ n" d9 Q1 u! Y4 ?
  6. >>> math.hypot(6,8)
    - t/ y" m. k1 j
  7. 10.0
复制代码

; t1 _+ J" l3 G  G  Tmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
8 y+ g" c4 k3 K, W& ~
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    3 A6 I4 _4 q; x  P4 y
  2. isfinite(x) -> bool
    * z2 e- N. [7 U& S
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.1 _  u4 `4 ^. ]5 V. a
  4. >>> math.isfinite(100)
    + A- w1 t. p  o& b% Z8 P
  5. True
    . _5 l7 m, H4 O" D
  6. >>> math.isfinite(0), z$ s2 h7 y+ t
  7. True$ v  z3 K/ s* l( ]' ~( k
  8. >>> math.isfinite(0.1); \. ^7 d: m0 ~$ q! }
  9. True
      d" O: T0 b2 G  ?  B3 k
  10. >>> math.isfinite("a")# ~6 {  U: ~" ~( }5 w% Z
  11. >>> math.isfinite(0.0001)
    # ]- U( r' ]  V" U8 {
  12. True
复制代码

5 V8 @" y/ F/ G+ R5 }math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
; T' _$ \  P, r" J: m) Z  g: V
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ! O7 h; o( t) Y. M! k! S" C
  2. isinf(x) -> bool# @2 t, R0 }+ n! p8 s
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ' o# ^/ H- n) n* ^: c8 ~, b: S
  4. >>> math.isinf(234)
    ( `5 J7 i; L  @, s$ q* q
  5. False4 n0 U, i/ `- b6 I" g
  6. >>> math.isinf(0.1)
    # U5 @1 T- i1 r) `
  7. False
复制代码
8 m/ {. V5 e" p/ g( X5 e
math.isnan(x)  如果x不是数字True,否则返回False
& ]3 S3 v3 @! `6 g* U3 |+ c/ x' {
  1. #如果x不是数字True,否则返回False
    ' l, L; R8 @+ }) n3 z0 z- i% I
  2. isnan(x) -> bool
    , K' h# Z, B2 d/ B* }
  3. Return True if x is a NaN (not a number), and False otherwise.
    2 D6 H2 A1 R- q; q/ b
  4. >>> math.isnan(23)
    ' E- u" t: m; j( g
  5. False- J9 G- w% h( ?, ]) f% P
  6. >>> math.isnan(0.01)
    ! @. ]* O+ o: {
  7. False
复制代码

! k2 z% ]7 f% u* {) k( P' q0 l* F$ Nmath.ldexp(x,i)  返回x*(2**i)的值7 w1 i+ U' C3 V: Q
  1. #返回x*(2**i)的值
      @3 Q' e7 G2 T
  2. ldexp(x, i)
    : j* e+ K9 g2 h* g( k
  3. Return x * (2**i).9 [7 e  y/ q! r4 [. R
  4. >>> math.ldexp(5,5), @$ X6 ]) z9 H% L) U* V
  5. 160.0( ?) R4 a9 m0 |5 ?+ k
  6. >>> math.ldexp(3,5)
    6 g; {) q3 F) _, r0 {7 B
  7. 96.0
复制代码
  c* Q0 y0 Y6 O
math.log10(x)  返回x的以10为底的对数+ e$ @1 v8 ?) g7 D0 ]! a! r
  1. #返回x的以10为底的对数
    7 a! K$ ]+ a! W1 _/ s
  2. log10(x)8 h: I  L  F  o/ \. A' K. L. V8 a
  3. Return the base 10 logarithm of x.& o* h; ~0 Y# q  T4 b) }; V/ S
  4. >>> math.log10(10)( E; y, N& W1 h* ^, ^3 V$ f8 z
  5. 1.0- Z0 O2 y" T6 G$ H
  6. >>> math.log10(100)
    * Z  Q9 t  }, C' [+ E
  7. 2.08 p0 L2 D) a! |3 q# M5 p; E/ m
  8. #即10的1.3次方的结果为20$ u5 z; K% s% n7 W5 B: G2 w
  9. >>> math.log10(20)+ b, s# U3 ~! s3 v% A
  10. 1.3010299956639813
复制代码
8 I1 k" K0 E% b; n" }& F
math.log1p(x)  返回x+1的自然对数(基数为e)的值7 I0 Y) r, S7 w( o& f! P
  1. #返回x+1的自然对数(基数为e)的值7 f! N& K8 h% a' [0 D" ?' }% g
  2. log1p(x)
    ; P5 Q/ h/ I, _7 I8 C2 u
  3. Return the natural logarithm of 1+x (base e).
    3 c2 z+ z, ?, }6 w* `
  4. The result is computed in a way which is accurate for x near zero.
    0 ^3 [# p" u+ \9 y) F
  5. >>> math.log(10)
    9 E# m9 v; l6 n6 o
  6. 2.3025850929940464 |0 G" @/ c* @  l; `
  7. >>> math.log1p(10): F% m, T0 G* P' z" P% Z, A
  8. 2.39789527279837075 G' Z; Q- t3 G: s& j$ @
  9. >>> math.log(11)
    6 a% j4 m% D, u. t
  10. 2.3978952727983707
复制代码
+ R0 V) ~  T7 f: G- A6 s
math.log2(x)  返回x的基2对数$ X# C' k' ]# |( ^* b9 d& v
  1. #返回x的基2对数
    ) P/ ]- k! f/ m* v+ h
  2. log2(x)
    , r( O* I" G5 R/ R
  3. Return the base 2 logarithm of x.; G) [/ K2 R. \9 o: e7 ?
  4. >>> math.log2(32)
    7 y; {7 U- C6 N- c$ [& D
  5. 5.0' Y* d& X! t% E  Q" c4 Z( a7 h
  6. >>> math.log2(20)
    4 ^& w+ A; S7 `& t
  7. 4.3219280948873637 U' _- p/ [/ `) [) r
  8. >>> math.log2(16): g$ M) z' q% Y6 z6 u% i  e
  9. 4.0
复制代码

9 o3 {" R# y! F& G1 h6 Omath.modf(x)  返回由x的小数部分和整数部分组成的元组) h' x8 A0 L+ O2 q1 d4 [
  1. #返回由x的小数部分和整数部分组成的元组; u: j, C2 N' n) _2 g
  2. modf(x)+ ~+ p- n" z. E4 Y' l- Z
  3. Return the fractional and integer parts of x.  Both results carry the sign4 s1 q' F2 ~4 l" _8 S2 P* l
  4. of x and are floats.8 h; q% v; l6 P4 l# A# k
  5. >>> math.modf(math.pi)
    " `  Y( U; x: {) E9 _
  6. (0.14159265358979312, 3.0)1 g4 m1 H7 e/ z$ L+ Z3 S
  7. >>> math.modf(12.34)
    $ y* V8 s4 ^: k6 J+ w
  8. (0.33999999999999986, 12.0)
复制代码
& z+ h$ A* [# R9 y
math.sqrt(x)  求x的平方根
8 G# K# E" C0 @5 T8 b
  1. #求x的平方根# m& q" R; P8 C, @
  2. sqrt(x)
    8 d: n! Q5 A1 x+ l2 ^, O
  3. Return the square root of x.
    5 E+ {, v6 b7 p2 i" W
  4. >>> math.sqrt(100)# S: k7 i- l/ v" N8 _4 e
  5. 10.03 T% @6 _1 H# u6 l5 q( X4 |
  6. >>> math.sqrt(16)4 ]: J! G: N0 @
  7. 4.0
    2 {, z" t3 R3 y6 n) p  F* g  C" O1 \
  8. >>> math.sqrt(20)" e8 q2 l! b" P3 a
  9. 4.47213595499958
复制代码
/ u' Q1 P6 m9 G! a" H9 K! u
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分3 @, n+ l$ g+ R9 F$ c$ K! E- \6 G
  2. trunc(x:Real) -> Integral
    ) L7 d. g! \! u1 e  C& x
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    & o; _. d. t# u: b% m7 @
  4. >>> math.trunc(6.789)
    ' j% z0 I* w( u) i# Z
  5. 6$ F$ S9 s6 m; a, B" ]5 p* t
  6. >>> math.trunc(math.pi)3 X8 d2 B# f* ?& K
  7. 3& a2 I3 P9 C$ y' p2 T
  8. >>> math.trunc(2.567)  k2 L( h" x" y* [
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-9 23:57 , Processed in 0.078614 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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