新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
6 Y7 Q8 z( e: C6 ~( p0 K" S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。  L$ q7 ]$ h, C; U9 k
$ q* Q- P( N6 Z
方法1
* y, p: J& ~# J
  1. >>> import math
    0 [# {. U( M1 I$ z3 v$ Q) t
  2. >>> math.sqrt(9)
    ; R9 x: u8 V; l; U: N7 L
  3. 3.0
复制代码
方法2
* I# ]4 m7 n' k6 v! `0 s# r
  1. >>> from math import sqrt$ m2 ?6 }- e6 b$ \6 _/ e1 a5 I
  2. >>> sqrt(9)# }1 \$ `0 {# T- s- a
  3. 3.0
复制代码
$ |, m6 S5 ~7 h

: F% t" t# G4 y! t9 w2 H
math.e  表示一个常量$ ]) E6 w2 Y4 l' U/ {3 U; B( p
  1. #表示一个常量3 ^+ U; U5 Y5 Q. x9 ]; H7 _
  2. >>> math.e
    * ^5 r# C- O! D, X: ~1 `' J' [9 r
  3. 2.718281828459045
复制代码
" _+ t# {4 r0 J! Z3 ~7 a
math.pi  
数字常量,圆周率
8 e5 `8 \9 C2 O* c9 O
  1. #数字常量,圆周率
    ( A+ b; U* d" S3 x9 [
  2. >>> print(math.pi)* ~% q! b0 I- ^2 R  [
  3. 3.141592653589793
复制代码

! e" f+ X) X0 w# m( F2 w8 h1 ymath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
! Z% n+ Z& [% _! [' K7 X" {; R; a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    % |! U2 \# x" L+ S1 T9 L
  2. ceil(x)2 Q7 i7 S8 X! W
  3. Return the ceiling of x as an int.- C+ K5 T% ?/ n. t/ I, O- Z+ q
  4. This is the smallest integral value >= x.
    # R2 d1 Y& Z: y) b( r
  5. / i; _- z4 v7 ^, v3 H6 y& d/ y
  6. >>> math.ceil(4.01)
    ; k- `# i: q' e! K8 ~# ?% j0 N+ j
  7. 5: Z0 ~" R+ I* o9 K
  8. >>> math.ceil(4.99)
    % k! a: ~. Q) o3 u+ c' o4 |
  9. 5
    # |8 ^- V$ M7 P  R5 j3 ^
  10. >>> math.ceil(-3.99); m' s6 @9 b% f1 E  C; c
  11. -3
    . k6 T; S; m0 b; D# g0 z
  12. >>> math.ceil(-3.01)/ B- S% Z4 W0 o* Z* \
  13. -3
复制代码

, J2 e% z" I- }5 ?1 U1 j+ |math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 |7 @9 E+ |& F7 ^/ g+ M1 Z) i
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
      i7 l  y3 u* h8 }: s1 ?' a' l
  2. floor(x)2 U; f9 J! Q6 |3 A9 u" @/ n
  3. Return the floor of x as an int.
    , Z( U- i, |& P8 X6 e* B( t3 t
  4. This is the largest integral value <= x.
    # t' c7 M* M& [8 L/ ]( {
  5. >>> math.floor(4.1)
    * I+ v' a- n. O. w5 ]
  6. 42 `1 G3 R2 v# d# G5 o$ _9 T& e, P
  7. >>> math.floor(4.999)' P: z: z# }- K- d" p
  8. 4
    / Z  E. ]/ d  `& {& Y
  9. >>> math.floor(-4.999)
    ( P! W6 Q# g0 M0 |! R/ y* d, t/ b
  10. -5
    ' `2 a# P; r7 s4 U/ m+ R
  11. >>> math.floor(-4.01); u. R! J' W- y% b0 ~
  12. -5
复制代码
- b" X( }" F5 j+ e, i8 R/ O
math.pow(x,y)  返回x的y次方,即x**y4 n, |# [: C& H1 M! `6 m
  1. #返回x的y次方,即x**y
    - C8 A) D/ ~: P% f, z
  2. pow(x, y). p  `- w2 {; r
  3. Return x**y (x to the power of y).7 u& J. `) t. H! M/ g
  4. >>> math.pow(3,4)! d0 a3 @# v" D4 b9 u% i
  5. 81.0$ G& K3 p3 x: M. o
  6. >>>
    $ k/ I0 w& K  S- T* R
  7. >>> math.pow(2,7)
    3 }" w' u& \# F- r" ?; v
  8. 128.0
复制代码

+ |5 g. o7 s. _& P. mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
6 |/ ]# U, ~8 J) t& g
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 m2 u( j4 n# {5 ^
  2. log(x[, base])& d) x+ U$ V, M8 R: x
  3. Return the logarithm of x to the given base.0 s4 W1 \3 I) I1 B' V4 E6 W
  4. If the base not specified, returns the natural logarithm (base e) of x.
    % n' v  V$ q2 a" Y( ^7 A
  5. >>> math.log(10)
    1 R9 ^  W; n7 T0 l& o; f6 }& u* Z
  6. 2.302585092994046
    - b6 B* W0 p+ E$ \
  7. >>> math.log(11)) m5 k: E4 {- h  Q' H, p, F) T  J) C
  8. 2.39789527279837072 y$ S5 S. w2 O7 U$ Z; G* S; _! C3 k
  9. >>> math.log(20); y! L* H. K; n7 U
  10. 2.995732273553991
复制代码
2 J2 e2 O% u- `' d3 U8 I
math.sin(x)  求x(x为弧度)的正弦值
6 M* ?3 h+ ?% W7 J- k9 B
  1. #求x(x为弧度)的正弦值
    0 Z! ~4 J/ p0 }
  2. sin(x)
    1 M0 K) `& O+ F  ~/ l. r" x4 j
  3. Return the sine of x (measured in radians).
    + l" V6 i: K! e6 A2 I9 i/ F& P
  4. >>> math.sin(math.pi/4)6 r; q" ^5 Z# R
  5. 0.70710678118654756 v# ~9 h( _7 ]$ k# s
  6. >>> math.sin(math.pi/2)8 l. Q5 f  }3 |  b$ G; M/ L
  7. 1.0! W: X6 \) B4 b
  8. >>> math.sin(math.pi/3)
    7 N8 l0 Z- d* r; D; C' t" P
  9. 0.8660254037844386
复制代码

3 I( N; A! n% X" p) G' z& W, k& Tmath.cos(x)  求x的余弦,x必须是弧度" N( I# r8 S0 ]' y2 m
  1. #求x的余弦,x必须是弧度
    : ?9 I+ o. _9 B* |! a
  2. cos(x)
    ; @, G, v+ |1 \& t/ s' H# f( f2 U6 R
  3. Return the cosine of x (measured in radians).
    ( X+ i: f2 }5 l: M
  4. #math.pi/4表示弧度,转换成角度为45度
    + E6 c3 V  {$ r
  5. >>> math.cos(math.pi/4), ~! a" b4 O6 L6 R2 c5 [+ ~
  6. 0.7071067811865476
    2 V3 P: o! P, R2 ^9 }3 B! H) _
  7. math.pi/3表示弧度,转换成角度为60度
    6 o; x% `' K) b* `" s2 F
  8. >>> math.cos(math.pi/3)5 u1 f1 S0 u6 [
  9. 0.5000000000000001
    3 T0 x7 Y3 Q: F) @% p
  10. math.pi/6表示弧度,转换成角度为30度6 d3 N$ b) H; T( E5 ?) E4 R" s
  11. >>> math.cos(math.pi/6)* a; x6 N+ i. l& v6 C0 T
  12. 0.8660254037844387
复制代码

. m" T  {8 }# J2 x& H# y+ S5 wmath.tan(x)  返回x(x为弧度)的正切值3 t$ n3 x0 ]4 Z( C
  1. #返回x(x为弧度)的正切值5 m$ {: r+ u' f, J
  2. tan(x)4 G4 j  n( @" Y$ h
  3. Return the tangent of x (measured in radians).5 k+ J+ Q0 u! M+ [8 y- u! D# p. F
  4. >>> math.tan(math.pi/4)
    5 z; v3 T1 o: g0 E7 }# ^' C
  5. 0.9999999999999999
    ' M  q  ?3 ?! C  F( N7 A
  6. >>> math.tan(math.pi/6)% Q4 L4 l" V/ D0 j( X4 P3 x
  7. 0.5773502691896257
    " }7 A, S) k  W# Q3 e4 b. o8 D
  8. >>> math.tan(math.pi/3)
    % F+ l  f' F" K: M2 J5 K
  9. 1.7320508075688767
复制代码

( E, A7 d+ F/ R& J  u7 O' Nmath.degrees(x)  把x从弧度转换成角度
+ W9 F1 v; f" A1 w
  1. #把x从弧度转换成角度
    ; h7 O  D) N. u- y
  2. degrees(x)
    8 a- m" m3 m. E* s' T8 G+ W" c
  3. Convert angle x from radians to degrees.4 N) x5 W  A0 ?3 o/ N

  4. & y6 f' x5 X' S
  5. >>> math.degrees(math.pi/4)
    7 Y0 W  L4 `  ~. j+ O. {% c+ S: P& E
  6. 45.0
    # {& Q: [$ d9 I% ~7 a9 Y
  7. >>> math.degrees(math.pi)9 _  d5 }9 X. Z: y  H# q5 B) m
  8. 180.0
    3 J; Y+ z! H2 m8 k$ B: L
  9. >>> math.degrees(math.pi/6)* [3 q, d; v6 i' P. I/ I, L1 r
  10. 29.999999999999996. @( j; d4 _4 C
  11. >>> math.degrees(math.pi/3)
    0 |/ T6 _! d" f1 G
  12. 59.99999999999999
复制代码
. O5 i; Y9 Y. R6 \  k3 A4 y
math.radians(x)  把角度x转换成弧度1 Z6 U" p' Q7 z
  1. #把角度x转换成弧度+ Z& W% `- _1 C8 q1 @
  2. radians(x)3 p' \$ R, [4 C* y% q+ }2 j
  3. Convert angle x from degrees to radians.
    ' Z9 I/ g1 _1 K& e+ P7 d; y
  4. >>> math.radians(45)9 N# ?; G6 G# E/ J2 n1 c
  5. 0.7853981633974483
    + y! }6 Z2 U0 o
  6. >>> math.radians(60)
    $ C8 w7 Y7 T5 |0 w6 s5 [& [
  7. 1.0471975511965976
复制代码

+ L9 G1 L4 H- W/ h; s& W0 Cmath.copysign(x,y)  把y的正负号加到x前面,可以使用0: S5 T- [$ D5 c+ b
  1. #把y的正负号加到x前面,可以使用0/ ?. X# Q( y1 ~' J
  2. copysign(x, y)
    ) \3 `3 K9 c1 q  N, v- y8 {
  3. Return a float with the magnitude (absolute value) of x but the sign 1 w! q0 Y7 p* L& `8 f
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ( ?: i9 ]# C9 D6 P' D4 s3 T
  5. returns -1.0.
    2 E0 o, l7 u* _$ ^

  6. ! y5 K! @+ R3 y% Y1 T1 z* o  t
  7. >>> math.copysign(2,3)
    1 h2 @' L  f3 x. F, x
  8. 2.0; E* [5 o2 W' A- \
  9. >>> math.copysign(2,-3); E) c" R- G, _+ H  j
  10. -2.0# l4 ?: A* q/ v3 Z
  11. >>> math.copysign(3,8)6 Q/ s! g4 s% w) j
  12. 3.07 K* w" e/ A, e1 g+ ^
  13. >>> math.copysign(3,-8)
    - l$ u. v: k$ ]; j7 X
  14. -3.0
复制代码

* C& _1 G7 ]' b5 o" M8 K0 t7 ]5 Emath.exp(x)  返回math.e,也就是2.71828的x次方7 t+ @8 `/ Z' `% J8 h) x
  1. #返回math.e,也就是2.71828的x次方
    - e9 A& l  ~6 f4 o: q
  2. exp(x)- b( x: t0 }, t  v
  3. Return e raised to the power of x.
    : u4 o' T$ ?9 p. ~( e% j

  4. $ {+ }0 |2 g/ x6 l+ U" `. _
  5. >>> math.exp(1)
    5 T& D9 R5 l$ K$ V3 `! b* u- z/ y
  6. 2.718281828459045
    8 T! S# o/ N; s- I# {
  7. >>> math.exp(2)
      A) R( I1 C. k& n  J: p9 P
  8. 7.389056098930652 a  d) e. m) h. u3 q+ f5 F
  9. >>> math.exp(3)" R2 O, g% }, F5 r7 _
  10. 20.085536923187668
复制代码

. Q" o+ x, M) M) Rmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
4 i) h: Q* s# A
  1. #返回math.e的x(其值为2.71828)次方的值减1- o) v( q) o7 x+ E7 T# o
  2. expm1(x)
    ( p9 i+ U+ r' U- e, p" K0 l
  3. Return exp(x)-1.
    + n' _% {' {, R+ \- c" \; Y
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ D' z! `8 C3 |
  5. 9 z7 w  D- F: H! t8 T# c
  6. >>> math.expm1(1)$ ~! z+ N% u2 I5 f7 n0 U
  7. 1.718281828459045# v( G+ k1 _/ @4 `& v0 U" W9 H
  8. >>> math.expm1(2)5 o8 a- }- f) y; _/ {- n
  9. 6.38905609893065
    ) K& l6 G* W: J
  10. >>> math.expm1(3)
    8 s1 C! i# I5 r4 z5 g2 ~
  11. 19.085536923187668
复制代码
( h# r7 u/ O0 |8 k
math.fabs(x)  返回x的绝对值& K$ ?7 ~3 |6 R$ H, x
  1. #返回x的绝对值
    : d0 e/ t0 c9 h+ G- _; Q
  2. fabs(x)
    - c3 P( |' [; b( }8 F5 W- c
  3. Return the absolute value of the float x.
    % p* r, z* N6 e: O) A1 r

  4. ; p$ m* W) j! o, O
  5. >>> math.fabs(-0.003)
    + r! J8 q- R! @' P3 t
  6. 0.003
    ! _" Z2 [  A" H) u
  7. >>> math.fabs(-110)) w8 i  V3 p$ Z/ W# y9 K$ s$ [8 ?7 N
  8. 110.0
    $ O* v8 o3 R& x  u$ n( c6 z
  9. >>> math.fabs(100)
    6 g- H+ S" w" \) i/ g5 a+ \
  10. 100.0
复制代码
* n: e, q' v+ s
math.factorial(x)  取x的阶乘的值
( m- ^# `( S/ y/ c$ k4 T
  1. #取x的阶乘的值! J3 R8 f' d2 c$ P  R8 I- a  W
  2. factorial(x) -> Integral
    8 M* ]7 _) i" q9 J" J) D: C4 q
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    3 D' E! x8 I' o, X0 {( O
  4. >>> math.factorial(1)6 U* u- N. W, k
  5. 1- a+ n/ {6 ]: |8 Q. p) n0 X5 G
  6. >>> math.factorial(2)
    : c7 J- x5 l8 F# M5 P! C
  7. 2# s: q  O2 {; B# M# n
  8. >>> math.factorial(3)) a. x5 _% ]2 h6 |% ?& f  I
  9. 6  u3 t/ w1 n* k: D4 X
  10. >>> math.factorial(5)2 H' d6 \. z1 x
  11. 120
      u' o( n' z% c: e
  12. >>> math.factorial(10)
    + R5 q1 T9 M  `* p+ t
  13. 3628800
复制代码

6 D" u( J5 Q! emath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
0 y. _, _0 |8 D! q! k) [
  1. #得到x/y的余数,其值是一个浮点数* f( B; p% Z+ ~6 U$ ]# x0 A
  2. fmod(x, y)
    + y6 C' H4 O1 k
  3. Return fmod(x, y), according to platform C.  x % y may differ.: A7 m$ m) v- K* t) U
  4. >>> math.fmod(20,3)  ]0 X: N2 c% S- C" k! w9 R
  5. 2.09 _2 [. |7 p! _; U1 f+ v
  6. >>> math.fmod(20,7)
    # c9 J# O2 @9 i3 t+ m* Q" }7 ?
  7. 6.0
复制代码

) B. p' s% @* @2 Emath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
& g  K$ F1 c/ s
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,- A5 H% |" M  B' q' E
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ) `" W9 M0 x. B' o+ ~
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
      c5 m) a- M0 ]: F. i  g0 L; J4 E8 K
  4. frexp(x)
    : x' \9 }2 k* ]" G4 ]
  5. Return the mantissa and exponent of x, as pair (m, e).
    " V1 H9 B, u% k6 Y
  6. m is a float and e is an int, such that x = m * 2.**e.2 n# Q4 l+ C, g) Y
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.# w* W/ p$ L' G
  8. >>> math.frexp(10)
    / _& f/ j* Z- u
  9. (0.625, 4)$ Q" S: B, Q. _( W7 _( z7 @1 {
  10. >>> math.frexp(75)- ]. i  C+ J  T: j/ g
  11. (0.5859375, 7), _) y3 _" J1 s! ]2 b
  12. >>> math.frexp(-40)' X/ \3 [6 n+ O0 e, F2 k
  13. (-0.625, 6)0 N) e' c% h5 h+ g' x2 ]7 I/ B
  14. >>> math.frexp(-100)" v  k. z( S, A! j6 M& q
  15. (-0.78125, 7)+ S$ G  X' t; b
  16. >>> math.frexp(100)4 p) }1 e# H0 t7 ]
  17. (0.78125, 7)
复制代码

9 Z; Q. n# u& ^* _math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列  j  D3 {+ ]$ N* j3 Q/ L. L
  1. #对迭代器里的每个元素进行求和操作
    1 G( r0 J% p5 Q, W7 t- x) w
  2. fsum(iterable)
    - }% l) S; I$ l) U
  3. Return an accurate floating point sum of values in the iterable.
    : Y2 Z) s8 d" v- v
  4. Assumes IEEE-754 floating point arithmetic.
    % D* z& D% I- S, u1 W
  5. >>> math.fsum([1,2,3,4])
    * t. O3 B! a8 J% J. _, L- Q6 l+ r
  6. 10.0; p' b% h. q. E
  7. >>> math.fsum((1,2,3,4))
    / |/ j/ I$ d& o& C# D
  8. 10.0! z8 u+ T: F2 `. r
  9. >>> math.fsum((-1,-2,-3,-4))- E7 \7 d- ~# Y2 P
  10. -10.0
    9 g3 p) d  Q% A1 d1 l4 I0 @
  11. >>> math.fsum([-1,-2,-3,-4]): l. ]1 L' t% H4 ^
  12. -10.0
复制代码
$ G3 m3 U) N2 o3 B
math.gcd(x,y)  返回x和y的最大公约数
  c5 |% G( }' H& U' }1 X
  1. #返回x和y的最大公约数8 V2 U3 v! {; ~2 R0 Q8 j! ^. K! q0 }
  2. gcd(x, y) -> int& O& M& a9 s+ k; }
  3. greatest common divisor of x and y  m3 p* H/ m( `
  4. >>> math.gcd(8,6)
    , L( [. @3 e% F
  5. 2' g: Z. h4 C. @! L# S& r5 o  L
  6. >>> math.gcd(40,20)
    6 c6 z& ~& o6 ~& U
  7. 20
    * `5 D) L1 p) \' J! {3 p+ I; S) o
  8. >>> math.gcd(8,12)
    4 u! R$ c, j' j6 b
  9. 4
复制代码

3 j% w/ A5 N* d% `/ hmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
' S, w+ D" N' r( S' N
  1. #得到(x**2+y**2),平方的值
    7 X% l" d. I( |, s  h1 R9 i
  2. hypot(x, y)
    ( q9 V" C/ `- y' z5 j1 _5 v" Q
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ' \! C6 ?; i/ G$ h- H% b' f) T1 r
  4. >>> math.hypot(3,4)
    % E  u9 Q# K' `$ t5 k  A4 X
  5. 5.0
    . K  C0 v3 a0 L( j3 \3 G
  6. >>> math.hypot(6,8)% A& V" W! u8 M1 I" T2 v
  7. 10.0
复制代码

: J. ?* B- @+ }1 u, Q, B% W8 D- q  imath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
' H: b0 ~9 S: i
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    2 X4 C; s* n  N$ B
  2. isfinite(x) -> bool" E: f0 O4 @% K1 v* u
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    : S2 x) n# `9 T4 u9 g8 Z# v/ H) M
  4. >>> math.isfinite(100)" q7 ?  f" E0 h0 _/ a. Y' w& k
  5. True5 x- D, v4 [3 n* X
  6. >>> math.isfinite(0)
    ! l. K) n0 H/ ~
  7. True& W$ ~' N% \7 q+ n  a6 R8 p
  8. >>> math.isfinite(0.1)7 `2 m+ y" K* m' S6 q7 m
  9. True
    " O  G/ G) Q1 l% \
  10. >>> math.isfinite("a")
    * y( k$ ?& `' E
  11. >>> math.isfinite(0.0001)
    ( b+ D4 ~' R9 z, v! _1 Y
  12. True
复制代码
, z+ d( O5 ?5 I1 l, E
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
* o; }1 \9 Y/ Y( L2 j( w& I1 y& {
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False0 M8 m9 W# J. ?1 X8 y
  2. isinf(x) -> bool' J# B+ o( ~% _! w- x  H
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ' U& u8 p* @4 _( ^
  4. >>> math.isinf(234)
    * n$ _6 o( z+ }$ V& b
  5. False
    ) F  o/ B/ S: z# p6 {8 L2 ]* c- X
  6. >>> math.isinf(0.1)
    * u9 _% p# t/ J. _& I& ?7 V+ P
  7. False
复制代码

. _2 W: x  O$ ~5 A6 Z( ~math.isnan(x)  如果x不是数字True,否则返回False! y$ X4 a/ o3 ~9 o! q0 N% W
  1. #如果x不是数字True,否则返回False$ G1 H# Y5 ^! G) |
  2. isnan(x) -> bool
    5 x4 O/ I- O+ |* T; y
  3. Return True if x is a NaN (not a number), and False otherwise.
    $ J( s: y1 t# }% i" }* T% j
  4. >>> math.isnan(23)9 D* M% y. u/ x. @( k1 Q2 L3 U
  5. False) {" u1 ]& S  D6 U5 y& k
  6. >>> math.isnan(0.01)' K/ t( v  V) I. P
  7. False
复制代码
0 \" N0 t3 p# [5 t) d" O2 a" S
math.ldexp(x,i)  返回x*(2**i)的值1 H7 |" L* q0 t/ J
  1. #返回x*(2**i)的值  d0 e4 p0 _6 X- U
  2. ldexp(x, i)
    7 K8 J( V$ k3 x/ J$ V5 r
  3. Return x * (2**i).
    7 j& f6 h$ Y, J( _
  4. >>> math.ldexp(5,5)( P0 t% i" T/ n  K& j& J
  5. 160.01 t# m2 L8 {+ n' F& A
  6. >>> math.ldexp(3,5)
    1 d6 j' S8 s8 N2 I0 g0 c
  7. 96.0
复制代码

* U3 I4 {- ]  {7 w2 Fmath.log10(x)  返回x的以10为底的对数
% o, }& j! O' b3 a, C' }
  1. #返回x的以10为底的对数: [" ?/ N  o. J) \
  2. log10(x)
    ) B0 J& f3 V4 n! s
  3. Return the base 10 logarithm of x.
    0 ^: Z8 Z* u  I+ y& u6 h# h
  4. >>> math.log10(10)
    " ?6 j. |& h; o! [
  5. 1.08 s% C- ^) F6 L  r: D4 z# Y1 I
  6. >>> math.log10(100)
    " R: `- d8 Q# ]4 F; k
  7. 2.0
    . }: `- l6 f* s* V( {
  8. #即10的1.3次方的结果为200 w4 r/ O4 X# j* E) o
  9. >>> math.log10(20)
    2 m  ~4 e# N% f; p* x  c8 A
  10. 1.3010299956639813
复制代码

8 B2 e, F3 _, J9 A% d) u1 d6 r0 I( I7 umath.log1p(x)  返回x+1的自然对数(基数为e)的值
4 ~) M) x6 }7 z1 `& J( h
  1. #返回x+1的自然对数(基数为e)的值
    9 I5 r; K1 g0 M
  2. log1p(x)6 f2 D* [9 s1 Q5 p* W: ^$ w
  3. Return the natural logarithm of 1+x (base e).
    5 m, ?- T) b. O7 M9 w) l1 @: \
  4. The result is computed in a way which is accurate for x near zero., y3 j4 S+ d& ^0 m- T) U
  5. >>> math.log(10)  Y- M( x1 u. Q: }+ a' u
  6. 2.302585092994046& l, {. ]2 @0 z" O
  7. >>> math.log1p(10)) O& l5 L* Y7 }4 i
  8. 2.39789527279837073 e! i. ?4 w( \
  9. >>> math.log(11)
      m! G: i- V' [  k2 v( z! G, T
  10. 2.3978952727983707
复制代码

) r3 i; k' P1 v( F% B4 Pmath.log2(x)  返回x的基2对数
6 K/ r: u) ?" `
  1. #返回x的基2对数2 K  R1 x( |$ r6 i# n: S! O
  2. log2(x)
    & r3 ?. V4 m: x* {% n9 N
  3. Return the base 2 logarithm of x.
    8 O& u: U! P+ ~4 d, w" o; I
  4. >>> math.log2(32)
    ' f, Y8 L) K* h; z7 \
  5. 5.0
    & |0 ?0 ^: f5 v. S- }' w$ ?5 M
  6. >>> math.log2(20)" S6 Q; K/ i/ i0 t$ @& V
  7. 4.321928094887363
    . K9 f& D# c% i7 R0 O; q( D
  8. >>> math.log2(16)
    & F' p5 A. I1 Z5 p" A0 _' A' S
  9. 4.0
复制代码

9 L/ e4 w) \$ M. `# a% P6 x6 Vmath.modf(x)  返回由x的小数部分和整数部分组成的元组
* i* i7 ?9 q) C5 q) n
  1. #返回由x的小数部分和整数部分组成的元组
    " X7 s' r% F1 V, X6 Z" u, x
  2. modf(x): ?- j. g7 H3 P/ Q
  3. Return the fractional and integer parts of x.  Both results carry the sign3 J' z8 g+ \% J) R, m1 t
  4. of x and are floats.9 O9 I2 j+ N) G' u
  5. >>> math.modf(math.pi)& K3 f9 J4 f; h9 _2 M$ `( e+ }
  6. (0.14159265358979312, 3.0)
    1 b: _' N1 s3 h: t- X+ F
  7. >>> math.modf(12.34)8 v. {8 _3 u7 n& {: @+ F; S
  8. (0.33999999999999986, 12.0)
复制代码
3 W2 Q5 J" [9 Q# Q6 L/ {/ H
math.sqrt(x)  求x的平方根
# I. V: h; l0 l  b; U9 f
  1. #求x的平方根; E+ H  L9 h& Y7 C6 i1 J
  2. sqrt(x)
    / J' m8 D- X- n
  3. Return the square root of x.
    ; b9 m" m% J$ c* a* V* o' j
  4. >>> math.sqrt(100)+ A! k2 I) R1 m' |5 _/ K
  5. 10.0' @( Q. @5 ^7 q2 }% x  p
  6. >>> math.sqrt(16)
    ! e/ |2 o: M) R" K  y, ^
  7. 4.0
    * d, V. H+ h  c9 N3 {' I9 J8 r. r* ?
  8. >>> math.sqrt(20)+ w# d1 A0 N- G0 \
  9. 4.47213595499958
复制代码

! f7 Y3 q, l1 O+ f6 S* y* V6 Mmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; ^2 R2 k# F6 I
  2. trunc(x:Real) -> Integral4 A8 `6 E8 x' C" ^2 X# \
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.8 E7 F$ _! e% |( S5 \5 @
  4. >>> math.trunc(6.789). ]2 k0 }  r$ S  \* Y6 @
  5. 6- b  a9 G2 y6 a0 k# ?/ c7 }# B/ H- K
  6. >>> math.trunc(math.pi)
    / X8 T- E+ F2 p, C% t
  7. 3& c3 @1 q: R! Y+ n
  8. >>> math.trunc(2.567)
    , z7 v( M5 }) a
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-26 11:24 , Processed in 0.091017 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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