新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
. ?- V& Z1 x. U4 g, g. Q3 V
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
* B& X  h# P/ {5 \

- H# k; s" N. [  ^4 @' h  U方法1* Y! F/ N; ^; }3 e* _1 j
  1. >>> import math
    ' K; I" K5 E: Y+ }( b' S2 i. F$ b
  2. >>> math.sqrt(9)
    & N2 l' q0 o4 @) z+ I1 S% `3 O
  3. 3.0
复制代码
方法2
6 s+ q; p7 |1 d! @) h( J
  1. >>> from math import sqrt: N9 |/ m0 a* h8 _
  2. >>> sqrt(9)
    ' ~& {7 M" }* f& F  t8 l1 j( c
  3. 3.0
复制代码
7 t9 z4 H3 d6 f! V  @% f1 q


6 A5 q4 q# {+ f' r; O7 ?3 ^
math.e  表示一个常量) X3 E  I! |' Z* u
  1. #表示一个常量  l. _6 ^- E" V
  2. >>> math.e
    3 I) k2 F( U9 w# r4 E9 c
  3. 2.718281828459045
复制代码
' e3 l+ E4 m) h8 Y, r
math.pi  
数字常量,圆周率
/ N# }" a8 t/ u8 q% J% f( Z9 w
  1. #数字常量,圆周率
    * ?4 F7 E1 @8 ^' F0 D
  2. >>> print(math.pi): P7 x/ N& e  v; U  X! c: i8 c
  3. 3.141592653589793
复制代码
, b5 [/ f  f% }6 j5 O
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
# n* A' B) Z: ]
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ! b, l0 z: m" H( V& d9 G7 b. O9 o% \
  2. ceil(x): \% Y7 H% f- Y
  3. Return the ceiling of x as an int.& A$ _& h1 V& l8 v
  4. This is the smallest integral value >= x.2 k3 [$ x  i6 M3 Z: h  q" H2 u

  5. 9 X# K6 h" ]4 X! L; h4 A1 F% {
  6. >>> math.ceil(4.01), M8 C0 A+ K! d* o- A4 k2 K
  7. 5
    , R5 g) b1 g' y
  8. >>> math.ceil(4.99)+ H% F! P- J# A& `- E  D; D1 y
  9. 5  m2 N; K8 {/ \4 z
  10. >>> math.ceil(-3.99)$ {: }2 N, a6 s& B, D. Q% j. j0 a
  11. -3+ k' a  x& G; Y* z* B
  12. >>> math.ceil(-3.01)# u$ {! a: P, n8 Y9 |
  13. -3
复制代码

( q$ z3 s. S' f7 g9 V& {math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
  c9 i- k" T9 i# M8 i
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    1 d9 f2 N# E5 V! u* W
  2. floor(x)! K, I- @% w$ M* w9 x% R% J1 F- N
  3. Return the floor of x as an int.
    4 H) T" o, g# ^
  4. This is the largest integral value <= x.: Z: k3 d  z1 d5 \* D" M
  5. >>> math.floor(4.1)
    & s- @' Z- T- p1 O
  6. 4
    ; D7 ]# C6 {0 S% V2 b1 H
  7. >>> math.floor(4.999)6 Y5 b" I7 z1 q' {  [  O" X1 v
  8. 4
      |( h8 a/ y. k' T) L. d/ P' S/ w/ [
  9. >>> math.floor(-4.999)
    9 p9 l# U, `& R/ G
  10. -5/ |) a, E! B: B3 q% E
  11. >>> math.floor(-4.01)
    ' g. Q0 r+ S5 A  W3 g" B
  12. -5
复制代码
: s0 t/ m2 q1 g0 W
math.pow(x,y)  返回x的y次方,即x**y9 B2 h) @1 O, u& K
  1. #返回x的y次方,即x**y# U6 X1 g1 a  m  Z* {; z2 n
  2. pow(x, y)
    ) S: C" j7 k0 _3 D
  3. Return x**y (x to the power of y).
    1 R# b% r* A  Y
  4. >>> math.pow(3,4)
    , \* Q; ]4 d3 n# ]0 X2 c% K
  5. 81.0
    # D4 R) A  S& b7 c3 ~: r  h
  6. >>>
    ) L; I. T4 p9 U, d
  7. >>> math.pow(2,7)  {+ c% n  F, O3 z
  8. 128.0
复制代码

8 w. `" X$ q* G# N; J) M+ Amath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
+ C$ }! m1 m1 U) r" b. y$ W! a
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    , @- w' R4 J! d" P+ e" V$ ?
  2. log(x[, base])
    + {9 O5 m8 H; G* w( N
  3. Return the logarithm of x to the given base.
      K+ h$ @! n1 @# T' D
  4. If the base not specified, returns the natural logarithm (base e) of x.
    % N1 H' b7 G5 T: x* \3 g8 r
  5. >>> math.log(10)
    , K0 J' f# z! Q- E2 g6 G
  6. 2.302585092994046% c, s6 e& V+ b! N8 R
  7. >>> math.log(11)
    - q, g& ^& J9 s8 N, q! J" \1 s
  8. 2.3978952727983707
    7 Y/ Y$ H1 Y. s! Q0 g4 i
  9. >>> math.log(20)
    / C$ ]9 z: u% r/ F9 j2 U% [
  10. 2.995732273553991
复制代码
9 Y3 l" }( V+ H4 y
math.sin(x)  求x(x为弧度)的正弦值
2 I  l+ Y4 W5 W' F1 U+ p: `( i) X
  1. #求x(x为弧度)的正弦值1 u! F) @. [  d/ B
  2. sin(x)- E% l* G' w# k. S: Y8 x2 Z
  3. Return the sine of x (measured in radians).
    ' ]/ a( m& Z7 B9 [1 ~0 }, t3 O3 k
  4. >>> math.sin(math.pi/4)
    1 ]! l6 ]" L  @- {9 }6 y9 J  z+ K9 o
  5. 0.7071067811865475- l1 @! {/ l9 r1 N+ A3 _4 {; f
  6. >>> math.sin(math.pi/2)
    ' q) [3 _2 G! m# F  i2 l0 O
  7. 1.0
    ; k, q: O" s  p1 i- U! `
  8. >>> math.sin(math.pi/3)/ `( V* |' A: e3 H0 G
  9. 0.8660254037844386
复制代码
0 B& `' [, o& y8 q3 S
math.cos(x)  求x的余弦,x必须是弧度( Z' p/ E* [) s( {0 q+ X' V. A
  1. #求x的余弦,x必须是弧度. R. O& Q5 m# c" D' J) ]" T+ v
  2. cos(x)
    $ j5 |) E5 d, o
  3. Return the cosine of x (measured in radians).4 e: M, @8 g9 v) E
  4. #math.pi/4表示弧度,转换成角度为45度
    - |9 D5 X7 z- r# T4 `- Z( z* Y
  5. >>> math.cos(math.pi/4)
    5 S9 r1 @) M# j! K
  6. 0.7071067811865476
    / ?5 q" v9 `& z8 o
  7. math.pi/3表示弧度,转换成角度为60度
    $ V. f: T# o" P  [5 N% ^2 m
  8. >>> math.cos(math.pi/3)! `0 b9 L+ G& H/ x! j
  9. 0.5000000000000001/ _& g1 w7 s* C4 t/ x5 B
  10. math.pi/6表示弧度,转换成角度为30度
    . w8 W, A0 `7 D0 b1 z3 ]( N' ~
  11. >>> math.cos(math.pi/6): u2 b: s3 R# X" l+ M
  12. 0.8660254037844387
复制代码

! a7 {# S  ]$ B3 b% m2 x) qmath.tan(x)  返回x(x为弧度)的正切值
9 f- a. g0 J1 [3 g; L9 x* d
  1. #返回x(x为弧度)的正切值0 z3 C- g  t. v( M* _7 o* y' o1 r
  2. tan(x)
    2 C0 @* ^. `( G& V+ J+ k
  3. Return the tangent of x (measured in radians).1 ^& W- X: t* \  Z* `' `
  4. >>> math.tan(math.pi/4)! u  L: E( n: j* B0 Z& {
  5. 0.9999999999999999& o0 C; y! i  O. x
  6. >>> math.tan(math.pi/6)
    ; G3 I. N$ R0 g+ j4 ]" t
  7. 0.5773502691896257
    . r2 x) L9 o4 e9 a4 H8 H; b
  8. >>> math.tan(math.pi/3)1 m1 R! x. @& h! c' v
  9. 1.7320508075688767
复制代码
/ c/ l* }7 q$ z5 Q6 t( X
math.degrees(x)  把x从弧度转换成角度9 c0 ^) M3 E+ @# {3 B# X
  1. #把x从弧度转换成角度
    8 {+ Z# s0 G8 r. w
  2. degrees(x)+ L  t( }0 @2 P; A9 B! E" A' W
  3. Convert angle x from radians to degrees.
    + d8 U0 y# m$ t8 d( H" k6 j7 q

  4. / J$ p1 ~, ]7 M. p
  5. >>> math.degrees(math.pi/4)" M- l) r) Q8 ^0 B9 y
  6. 45.0
    8 q/ |% U/ ~1 F2 b* ~2 |
  7. >>> math.degrees(math.pi)+ L* P" o+ ^1 G
  8. 180.0
    " A: e% J& h* r- [* F
  9. >>> math.degrees(math.pi/6)3 Q* l. k1 R0 ]
  10. 29.999999999999996$ r! x3 T; J# q% Z% `6 L! ~  C/ t
  11. >>> math.degrees(math.pi/3)
    2 V' Y9 H! f/ i! A& ^
  12. 59.99999999999999
复制代码
; L1 H2 p( t. ^$ `0 L) e; N: {
math.radians(x)  把角度x转换成弧度
6 p7 ~7 A/ g2 J( k9 v4 A0 D. x
  1. #把角度x转换成弧度8 `2 x# L, R+ x8 V, L$ n' S# q( L
  2. radians(x)
    " j* N  j3 e# H8 i  P# O! P  h
  3. Convert angle x from degrees to radians.( M0 w3 @+ o. M8 D
  4. >>> math.radians(45)
    ! ^6 O! O/ P% A& p6 ^0 c$ _
  5. 0.7853981633974483
    0 t. q8 b& k6 |& H' W- f3 F
  6. >>> math.radians(60)* q( A  ?, R8 r+ B
  7. 1.0471975511965976
复制代码

6 S# i, K& F5 e+ u' t$ o1 V& Dmath.copysign(x,y)  把y的正负号加到x前面,可以使用0( x1 V3 Z9 F9 O( C$ ]
  1. #把y的正负号加到x前面,可以使用0
    6 }: @$ ~' O- I* h$ c
  2. copysign(x, y)
    , @/ l- Q* P! ^# [" {5 I( o
  3. Return a float with the magnitude (absolute value) of x but the sign
    4 c6 T8 ], s2 `* P) B! d% c/ |( h; v1 n
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) * d  W- ]9 w/ B; F
  5. returns -1.0.* Q& d; ]2 b% m5 j/ _

  6. : L1 V4 G! F+ Z0 t! d
  7. >>> math.copysign(2,3)
    / K9 q% R; ^! A( ^/ q" u/ ~5 R) X
  8. 2.0- _) h/ j  h0 ?( ~' K5 S
  9. >>> math.copysign(2,-3); S  F  D& q9 s7 n4 b# v
  10. -2.0
    " S/ e* F2 G) l' j2 |7 I
  11. >>> math.copysign(3,8)
    9 e, S( W- R, p" e) y- x) {) _
  12. 3.09 }4 n* M9 F6 C( a
  13. >>> math.copysign(3,-8)9 J/ W& q) x+ M  J  Y
  14. -3.0
复制代码
$ A, Y- d- z5 f" G
math.exp(x)  返回math.e,也就是2.71828的x次方
) b" P1 j" R2 S8 L, U- x
  1. #返回math.e,也就是2.71828的x次方
    # s! M1 Z" x/ M( w
  2. exp(x)1 V$ E7 u! N* n" P2 V4 _# a
  3. Return e raised to the power of x.6 @7 ^1 Z9 A4 c$ {) \8 L

  4. . t+ ^. P6 d; M4 B# \6 m
  5. >>> math.exp(1)- h1 ~$ \" P3 I  w4 k# K: U0 E7 E* G
  6. 2.718281828459045- Z! z. z) _' z! b. i9 v2 W- F# ~
  7. >>> math.exp(2)+ S, @) a5 E2 [  y5 D" S
  8. 7.38905609893065
    * L5 i2 N1 z7 {+ l
  9. >>> math.exp(3)3 X3 `5 ^: B7 Y' N/ R' |
  10. 20.085536923187668
复制代码

, G7 d+ s; `& p% `, w2 o- omath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减11 `5 \; i" b9 M
  1. #返回math.e的x(其值为2.71828)次方的值减1- ]( z5 |3 k5 ]( d
  2. expm1(x)
    ( R: q1 o3 G1 k: N% B- g
  3. Return exp(x)-1.
    7 ]! T) v3 [8 {+ t+ M& ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    : m1 [' q  g: @  j

  5. - |3 B( G4 J4 |) }
  6. >>> math.expm1(1)
    6 Z" W: u3 _% h1 o" U( o8 Z
  7. 1.7182818284590455 u: w% I* Y1 v" ]) D6 {0 T9 b! Y
  8. >>> math.expm1(2)
    1 U  a0 [- y0 D  Y) T
  9. 6.38905609893065
    / o' K* g5 ]$ N3 p
  10. >>> math.expm1(3)" h& N6 d) P# F, G$ x9 d4 x" H, N
  11. 19.085536923187668
复制代码

3 y$ a# n! v7 g: @math.fabs(x)  返回x的绝对值
, d6 @6 _; U; i; i
  1. #返回x的绝对值
    6 O+ P7 T6 d+ |8 a
  2. fabs(x)
    , G$ |4 r) @6 \$ |+ X' X
  3. Return the absolute value of the float x.# }: V: ^: d3 ]9 g
  4. ( ]8 p  s. B/ g$ X* N
  5. >>> math.fabs(-0.003)
    $ l  X( e1 L& {* R, r1 W$ U
  6. 0.0033 A: `$ h* {7 w" i4 W
  7. >>> math.fabs(-110)2 W5 a4 X5 t5 b* O
  8. 110.0) G$ h1 M2 I6 z. X, `2 _- z
  9. >>> math.fabs(100)
    $ O8 q. q$ u1 {1 `/ V* j3 S- c6 ^
  10. 100.0
复制代码

( a! r  U3 Y3 Tmath.factorial(x)  取x的阶乘的值+ l( \  N9 z" d8 h3 a* H
  1. #取x的阶乘的值6 g! S9 l& w9 Z
  2. factorial(x) -> Integral
      Q: ^# {" u5 i9 p, y& S/ L' }
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    " i* s5 ]; |  O+ P8 f
  4. >>> math.factorial(1)
    / p& S/ \9 U; U% e: ^
  5. 1, }$ A/ l# y; D5 z: Q' B6 m
  6. >>> math.factorial(2). g: W2 \7 R; ^) b- u
  7. 2& G  @4 o6 [3 G& M0 m5 ^
  8. >>> math.factorial(3)& G. t% u  M' h
  9. 61 i$ C) s2 Y  T- v1 u4 D
  10. >>> math.factorial(5)
      ]  z  b+ t, `% `6 [# H7 ~& }
  11. 1205 m: J0 M! i9 ~+ H; e3 Q
  12. >>> math.factorial(10)
    0 `+ ^, Z3 }- Q: E
  13. 3628800
复制代码

* S% S: e! Q+ U% T/ nmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
( C8 |8 a: J2 X6 T; I5 o- w) T
  1. #得到x/y的余数,其值是一个浮点数; v7 W8 F. O% t# A& X! {) m7 f# S
  2. fmod(x, y)* Y8 I2 {, V- V7 T. D" o1 _/ _
  3. Return fmod(x, y), according to platform C.  x % y may differ.. N% k) V( _: X8 r5 h
  4. >>> math.fmod(20,3)
    : j0 ^7 B: y" ~' `; }9 m) P2 F" w
  5. 2.0) B. K  W2 k7 U6 W) [! b2 V+ _
  6. >>> math.fmod(20,7)
    9 a8 J8 @" \2 _4 B; S: l3 s" H) i
  7. 6.0
复制代码

, {. E& ]) `& j. B7 B. x# Wmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 F% ]' U0 V# I" u
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,6 \* @- A4 s) i, z6 I1 Z, i
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值% I+ W( L6 U7 K' r
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    : {. k' l# U6 Q1 K1 a
  4. frexp(x)
    ' e3 S0 y* }% H8 Z4 H7 [9 U" X6 `
  5. Return the mantissa and exponent of x, as pair (m, e).
    " }& R; W* O+ i  c( |! K, t
  6. m is a float and e is an int, such that x = m * 2.**e.
    5 w1 P4 k$ `8 O9 o0 X
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    1 J0 X& w1 K9 G/ o- p
  8. >>> math.frexp(10)1 C) |! Q) ]* W. P$ C5 E# Q; K
  9. (0.625, 4)+ r" b1 M2 v; k
  10. >>> math.frexp(75)! r+ w+ e5 Q$ @- O
  11. (0.5859375, 7)2 {1 G  p5 I4 s/ y" J4 X) ]
  12. >>> math.frexp(-40)
    ; h& L& N* }- B( W6 e! b) h5 ^/ {
  13. (-0.625, 6)2 ^3 `$ |3 n* U
  14. >>> math.frexp(-100)" n! X, g1 e' x; ^' j$ T  s1 e
  15. (-0.78125, 7)
    ( c5 f/ Q1 |% l- V2 Q  ~, T
  16. >>> math.frexp(100)- Z$ x2 S% B* b7 K2 v9 l4 i
  17. (0.78125, 7)
复制代码

8 W5 K  R$ G( a) U, Y7 rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列8 ]7 O7 B3 u, q0 J1 {/ Y* U" T
  1. #对迭代器里的每个元素进行求和操作' t( N( B4 V! ~, f
  2. fsum(iterable)
    ( h/ u+ m: J$ V# u
  3. Return an accurate floating point sum of values in the iterable.
    " w3 r6 `# W+ w6 U
  4. Assumes IEEE-754 floating point arithmetic.! h" S* N3 z& v3 g7 ]1 d5 N
  5. >>> math.fsum([1,2,3,4])
    % ^; h7 a: [- F- q
  6. 10.0
    1 i- O3 @, j. z0 c+ X! s; m8 u
  7. >>> math.fsum((1,2,3,4))3 n# j% R# L$ J" N
  8. 10.0
    % s. ?/ S4 {: c/ `
  9. >>> math.fsum((-1,-2,-3,-4))9 _+ _& t9 M. \% f
  10. -10.0
    # y: X% p1 h4 ^/ ]) D
  11. >>> math.fsum([-1,-2,-3,-4])
    7 B7 ?. K8 R# e; X8 d" L
  12. -10.0
复制代码

; h1 A' h) T9 E: r" Dmath.gcd(x,y)  返回x和y的最大公约数
7 j  h7 x7 }+ X+ H
  1. #返回x和y的最大公约数" X! X/ [( u% [. A/ q+ D) j  X
  2. gcd(x, y) -> int
    9 v7 ]& t: l! ~& M2 T
  3. greatest common divisor of x and y
    ; `( J1 _1 N5 F$ d+ _) w
  4. >>> math.gcd(8,6)
    2 Z7 c) r! q4 `5 `- }8 U
  5. 2, |& [, s# ?$ ]( ~0 [
  6. >>> math.gcd(40,20): d+ e4 p; z' _" U( G' k3 E
  7. 20
    8 m& \  d5 ]  n
  8. >>> math.gcd(8,12)" e3 P8 c. H7 f+ z
  9. 4
复制代码
7 q. R* W6 m* [$ Z, S
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
. E9 P9 |# H+ c
  1. #得到(x**2+y**2),平方的值, o: u3 s# B- d) ^
  2. hypot(x, y). R8 h$ w, C& g" C" {
  3. Return the Euclidean distance, sqrt(x*x + y*y)." v, {1 O" S6 `0 _  ]
  4. >>> math.hypot(3,4)
    6 Z8 v2 D( D/ X# H
  5. 5.0
    % G% j0 T! n. O% R$ v# W* y
  6. >>> math.hypot(6,8)
    2 H5 s4 A: o. K* n
  7. 10.0
复制代码
. v/ u/ d) H) Z, y+ w
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
- c- V. n% _7 H5 S# q- Z( t
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    . W4 M+ `# W4 B* i
  2. isfinite(x) -> bool
    3 g: X' j, h8 h0 }, i$ h
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    ' k" C, n1 G  F) Y( C$ N! k$ U
  4. >>> math.isfinite(100)
    % t' ?( Z- K+ y2 P9 V# j
  5. True
    . F+ W" F) A" O5 n$ l3 O: n
  6. >>> math.isfinite(0)
    * |. {5 V. [1 v" Y- ~( v
  7. True
    : C3 {4 P# H4 j0 x+ o
  8. >>> math.isfinite(0.1)! w/ e* n& D; _* I8 [2 \, b
  9. True
    4 a9 n4 ]( ]" u* A  z; a
  10. >>> math.isfinite("a")
    7 p$ P- u4 M7 G' @9 N
  11. >>> math.isfinite(0.0001)
    ; F) n" f( _) I3 a# [, _" E
  12. True
复制代码
! f& @. W* I7 [  I1 |% A: Y
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False- B+ ?. k. i$ D) g, f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False6 x; g5 ?5 F) \# }: G7 O
  2. isinf(x) -> bool4 e& q$ l9 W" S7 N; {( o4 x
  3. Return True if x is a positive or negative infinity, and False otherwise.* b0 z2 N$ m9 |6 n+ _
  4. >>> math.isinf(234)
    ) g2 @8 r8 J9 A9 ~3 i
  5. False7 \0 C) a, E% w) s$ E4 h, ~% r
  6. >>> math.isinf(0.1)* Q1 R/ z% F( Y  F; W* b# K
  7. False
复制代码
. s9 R7 E4 W1 t7 ^4 C' Z+ R  r+ [
math.isnan(x)  如果x不是数字True,否则返回False
, A: Z* S! N' U1 O' q9 j5 [' p
  1. #如果x不是数字True,否则返回False
    ' m* a& x' T' C7 [
  2. isnan(x) -> bool
    1 I' @- K# a, T5 E7 y) t
  3. Return True if x is a NaN (not a number), and False otherwise.3 U1 p% A. @# f& S
  4. >>> math.isnan(23)
    1 A0 A7 B! [+ d6 {. |/ G+ {7 M. k
  5. False4 B. ^7 R% @/ t% s4 c$ s  R+ J
  6. >>> math.isnan(0.01)
    4 t4 U! R5 [; Q; R9 H
  7. False
复制代码

6 _0 L: w+ A2 T$ ^& @math.ldexp(x,i)  返回x*(2**i)的值5 |+ L2 }, _$ A: {  }
  1. #返回x*(2**i)的值
      B/ Y# g7 p( m
  2. ldexp(x, i)  a, Y- Z8 Y/ |& p& x
  3. Return x * (2**i).
    ) H0 b$ `( W' {5 N4 v
  4. >>> math.ldexp(5,5)
    ! p5 W# L2 x! o+ ?% V" h
  5. 160.0
    ! ?) i  n+ j0 C* j5 J
  6. >>> math.ldexp(3,5)
    " M6 r: X" W/ Z9 |7 o% L
  7. 96.0
复制代码

; b4 [; p; K0 F9 [, u, e5 M& S( Kmath.log10(x)  返回x的以10为底的对数" t: V4 e4 U/ t8 e
  1. #返回x的以10为底的对数' @5 x# ?0 P* s
  2. log10(x)+ ]  B# B6 `6 i5 w8 Y  e/ L
  3. Return the base 10 logarithm of x.
    ' v" X. p* @. c( V( j
  4. >>> math.log10(10)* N3 U0 N1 |+ i& }
  5. 1.0$ U: \5 R$ Z( B
  6. >>> math.log10(100)
    : M9 q8 |; x( ?$ ~/ [- p
  7. 2.08 y( p( s! U3 i# n4 X) i
  8. #即10的1.3次方的结果为208 d  z% S! |' n
  9. >>> math.log10(20)
    . N; I- p# L- h3 H4 Q
  10. 1.3010299956639813
复制代码
( F- ^7 N6 R' d0 }5 i) E
math.log1p(x)  返回x+1的自然对数(基数为e)的值
" m) \7 E9 q2 g) Z" R0 o- b/ H
  1. #返回x+1的自然对数(基数为e)的值# v# Z/ ?' Z% @. _* Q, k, N
  2. log1p(x)
    9 e/ L" U& |+ F7 D9 B/ A& ]
  3. Return the natural logarithm of 1+x (base e).1 K  B$ e" @3 x2 `8 c
  4. The result is computed in a way which is accurate for x near zero.( t3 C' k, U/ k' l# ^6 y- L
  5. >>> math.log(10)
    $ p0 S6 A/ @+ D' W; b5 X
  6. 2.302585092994046
    1 k, i& i6 _9 c- y7 m8 B5 g
  7. >>> math.log1p(10)
    & T7 d9 f! `6 j( s' `9 T
  8. 2.3978952727983707
    . d! @; ^0 C1 ^4 J; m
  9. >>> math.log(11)1 n8 u8 k5 l. ]2 H8 n
  10. 2.3978952727983707
复制代码

0 t/ \& t! i  c  l" [4 m+ U% G- A2 ]math.log2(x)  返回x的基2对数4 t0 @7 z+ `# h3 w* u
  1. #返回x的基2对数
    8 B- z0 W1 A% _
  2. log2(x)
    * }8 P$ W$ R. W! z$ B' ]  O# I; A) L4 T
  3. Return the base 2 logarithm of x.
    - h  Z5 E9 q1 Q. N$ {
  4. >>> math.log2(32); i) Q) v' v7 b; @3 G( ^5 H
  5. 5.0
    ! o% Y' u. k* p+ R! E5 W: h/ k7 @
  6. >>> math.log2(20)9 t6 |  F' L# e2 I, s
  7. 4.321928094887363# T0 A! j! R- \% d- M! R
  8. >>> math.log2(16)# b- o, o( |& n
  9. 4.0
复制代码

2 H2 t/ ^' G+ Q( \* |6 G4 X$ Vmath.modf(x)  返回由x的小数部分和整数部分组成的元组
0 [3 `, l  ~, [, t( R/ @/ f. C
  1. #返回由x的小数部分和整数部分组成的元组# h  [( e' b& V4 S: f8 l  z
  2. modf(x)/ @6 D: ]7 n+ m
  3. Return the fractional and integer parts of x.  Both results carry the sign
    2 I: |; y8 ~5 S
  4. of x and are floats.6 ]! {# T4 ~- W3 j/ F& b; |
  5. >>> math.modf(math.pi)6 L3 {& R8 J& D) H
  6. (0.14159265358979312, 3.0)
    9 S5 T$ q( @; k# T/ L! D
  7. >>> math.modf(12.34)! S* l% K0 J: A  D
  8. (0.33999999999999986, 12.0)
复制代码

. W$ a% E9 @9 |( H7 K  f% Kmath.sqrt(x)  求x的平方根
; U2 J- S/ T, w
  1. #求x的平方根
    / Z8 O2 Q' ^4 N1 M
  2. sqrt(x)9 q3 G& j0 t) y$ E
  3. Return the square root of x.% G7 H# i0 O' N5 O8 l
  4. >>> math.sqrt(100)& d9 t+ L& v0 P, Z2 Y: l- w0 Y
  5. 10.0
    3 k5 ^8 P8 Z+ \$ S
  6. >>> math.sqrt(16)
    3 n) K4 [+ d6 `7 j4 {4 C8 D- r# L
  7. 4.0. _* k6 U! N" I) r; j/ j' r) R
  8. >>> math.sqrt(20)
    ; S0 Q3 g2 H. B
  9. 4.47213595499958
复制代码

+ A6 t5 R$ l& U- d1 Q( ]" q3 Qmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分9 _; C$ c: \# N. h, s
  2. trunc(x:Real) -> Integral( x( l$ q/ n( O, d) h
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.1 u+ w1 {% B- B# v
  4. >>> math.trunc(6.789)
    : h% w- F" ^) ]7 F, l+ z& g+ T
  5. 6- A3 U) a; {4 S) h  I- ?
  6. >>> math.trunc(math.pi)
    2 h5 @1 x+ j" F  I& z* T3 d
  7. 3" y4 R9 b5 T  C0 }! }( H. c
  8. >>> math.trunc(2.567)2 M3 m7 N) c  Z) {. r2 _/ T0 b
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-31 16:02 , Processed in 0.081676 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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