新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
* `( w) ]4 x& _, D& d
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。& Q5 N) T9 \. S/ s- R$ Q6 z
; v( x" {4 Z' P* y$ b2 m0 P
方法1: L& T2 S- @' D' W4 Z
  1. >>> import math
    1 F& k6 {  D- ?4 G; |3 S
  2. >>> math.sqrt(9)
    7 k2 ?3 n! n8 t. Z3 G! K/ _
  3. 3.0
复制代码
方法2
8 p* D' j3 `" \6 T0 E* j  d$ l$ f
  1. >>> from math import sqrt
    - I/ K3 y9 Q+ ~9 p6 ]0 C2 q' i
  2. >>> sqrt(9)
    ; H4 D/ Z. s/ A  T) g, V! N: K- W& f
  3. 3.0
复制代码

( i4 j. w7 P3 F
1 O7 k9 [( W9 g2 J
math.e  表示一个常量9 f3 S. h+ j' P+ {3 R
  1. #表示一个常量: u% I  s- t9 \! h; U
  2. >>> math.e, B% w7 ~* e( Q0 z' `! ]: E
  3. 2.718281828459045
复制代码

: u% F7 W! R" N& I8 N: i# H7 H: I8 ^math.pi  
数字常量,圆周率

$ J9 D$ i" R0 |% Y
  1. #数字常量,圆周率
    - d9 m+ W* `2 e  n: n
  2. >>> print(math.pi)8 P. B0 _* h8 i# \' h  {7 j3 X0 P
  3. 3.141592653589793
复制代码
. H5 d; O. Z$ g9 A2 k7 X
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 B; i+ m) `% Q, `8 z$ Z5 G
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 V1 V2 u3 W. U: r3 E% u' K' B# g
  2. ceil(x)
    ' _* D1 [" W! `0 ]! U
  3. Return the ceiling of x as an int.; N8 A, Z+ f) h, W) c
  4. This is the smallest integral value >= x.
    " J- X. E& Y& J% j

  5. & M" i7 {8 d- v8 s
  6. >>> math.ceil(4.01)
    * B( M, R  a0 p: D5 S% T
  7. 55 i  U+ |- Z; z! i1 ^; T2 D- S
  8. >>> math.ceil(4.99)
    3 x0 F" I8 i9 t. H. \
  9. 5# X- x9 b! _: M) P1 v% X6 Q
  10. >>> math.ceil(-3.99)
    & }0 ]. U4 C6 N3 ]1 ~" |" F
  11. -36 E  [+ W5 t4 q% E& M
  12. >>> math.ceil(-3.01)
    3 {: E& N5 [$ \: e! m: s# \( U+ S
  13. -3
复制代码

* Q! U3 k, }& n; l: J) e8 j" c( wmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身4 i) t% k: o$ @
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% M/ Q2 o( F' e# ~% h- x& @) L
  2. floor(x)
    , I2 a5 x. H3 ^$ C2 {
  3. Return the floor of x as an int.% n# l& X* T/ H5 E4 k$ k
  4. This is the largest integral value <= x.2 Y; _! C+ D: Q
  5. >>> math.floor(4.1)
    & q& P& w8 w3 ^: {
  6. 4
    4 V% v  i+ z5 V3 j
  7. >>> math.floor(4.999)
    : Y; T0 c" ?6 [5 U* [
  8. 4
    & X+ W" Q0 k! h# p4 q+ K) D3 U# Y( Y
  9. >>> math.floor(-4.999)3 b. J/ g" U% E$ o
  10. -5. a. \: @/ v. j. |. M1 {4 ?
  11. >>> math.floor(-4.01)
    4 |' j4 i& H% C4 a) ?6 c9 M+ X& ~
  12. -5
复制代码

7 G0 w; o) j; J, Rmath.pow(x,y)  返回x的y次方,即x**y
* j4 p& v& }6 h+ T# |% x
  1. #返回x的y次方,即x**y% I8 V5 b, F) _. ^: }3 Y
  2. pow(x, y)" O( m  x5 ~! _3 |; n  ]& [& K& z
  3. Return x**y (x to the power of y).$ q* @- L2 i1 B% h
  4. >>> math.pow(3,4)
    ) N0 O; W  V$ r' k0 n8 m
  5. 81.0
    4 }& j# ^8 ?! g
  6. >>>
    1 Q- R: g$ a# K0 X+ P" u
  7. >>> math.pow(2,7)
    ! m! X% H, t% @, W  p3 a, D
  8. 128.0
复制代码

, @* K7 d, o" L" b; Qmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 a' ?1 O* e& d/ p# e2 V& S
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' u0 M: w2 q. o
  2. log(x[, base])* }% K7 O1 E0 m$ k9 U0 c
  3. Return the logarithm of x to the given base.& U8 N5 P* |7 U; t
  4. If the base not specified, returns the natural logarithm (base e) of x.5 {* T' [# l! O8 R. K
  5. >>> math.log(10)
    ( x1 v* L% {5 @  u# V- Q5 [
  6. 2.302585092994046- s( Y# q0 X: e1 X) O
  7. >>> math.log(11)
    & m$ A, j6 v, m* a( [
  8. 2.3978952727983707
    ; L" T9 D7 o$ w* `. L
  9. >>> math.log(20)
    & ~, o+ l8 `+ X8 N+ w# T
  10. 2.995732273553991
复制代码

$ F& }+ Y% q4 l* \/ jmath.sin(x)  求x(x为弧度)的正弦值5 L! h# q) d# \- R
  1. #求x(x为弧度)的正弦值
    4 O/ E% i) ]0 S9 U- d. u
  2. sin(x)  {  b! L4 u$ D" H, C- r' ]
  3. Return the sine of x (measured in radians).
    & H# g6 V2 [* ?* u. q. P
  4. >>> math.sin(math.pi/4)$ c! M9 y% L$ J; U% Y9 n
  5. 0.7071067811865475
      _: b" L# K# \; ^
  6. >>> math.sin(math.pi/2). b9 i- P1 e4 x# h( T. b$ P; U& j
  7. 1.0
    . ?4 ]# H( R" u0 Z- D2 z
  8. >>> math.sin(math.pi/3); F' R1 d! Q/ J2 x0 J3 ~
  9. 0.8660254037844386
复制代码

$ ]3 |8 D7 I  C" D+ mmath.cos(x)  求x的余弦,x必须是弧度. r2 h; e3 X, m" c- n0 r6 F  ]
  1. #求x的余弦,x必须是弧度: B$ @8 W! L/ I+ {- L) M. P
  2. cos(x)
    . Z% C; l( W6 A7 l  E
  3. Return the cosine of x (measured in radians).$ k* {" B. M; m& U5 f& ^. @
  4. #math.pi/4表示弧度,转换成角度为45度
    - }3 v7 g% E# Y. t4 ?: ~
  5. >>> math.cos(math.pi/4)4 m! F2 d) B& H3 J/ v3 g. V
  6. 0.70710678118654769 l# i2 O/ r# _" t
  7. math.pi/3表示弧度,转换成角度为60度
    " R$ g8 C+ e  J; y) A: ~7 ]
  8. >>> math.cos(math.pi/3)) N. K* j3 c  z3 G
  9. 0.50000000000000018 {4 J) N4 e  B
  10. math.pi/6表示弧度,转换成角度为30度" T) ]- l5 Q6 C$ ^/ z" o
  11. >>> math.cos(math.pi/6)
    + f" k* H( x8 `% b' |/ k6 q
  12. 0.8660254037844387
复制代码

; n0 X* r9 \) L1 q, {6 E& L( n5 V, lmath.tan(x)  返回x(x为弧度)的正切值
( v; ~& b+ }' `( D
  1. #返回x(x为弧度)的正切值
    ' S6 f( ?7 x+ ^9 |3 a/ ~
  2. tan(x)% m5 j% G! f) J8 v2 ^
  3. Return the tangent of x (measured in radians)., S0 t0 O# A3 Q, C$ ^
  4. >>> math.tan(math.pi/4)% `' v; \" S1 T) o9 r
  5. 0.9999999999999999
    4 G- l9 s) `& f8 b/ k' d
  6. >>> math.tan(math.pi/6): e+ x% l3 h* N4 f
  7. 0.5773502691896257
    . `, z% _, }9 O6 |( O
  8. >>> math.tan(math.pi/3)& A5 v1 y) U' v# Y; Y, @
  9. 1.7320508075688767
复制代码
: w+ @" ]! u6 k: _6 ?1 n% W& c  g
math.degrees(x)  把x从弧度转换成角度0 ^& Q9 R; {6 ?& U- Q# g: }, \
  1. #把x从弧度转换成角度! @1 }% w8 F* _; u" v0 Y: X
  2. degrees(x)
    6 C/ R: v0 Q5 a, v. R
  3. Convert angle x from radians to degrees.( e5 Q, r. j# T7 p) y+ M) X

  4. - x/ r, a8 e; P& J
  5. >>> math.degrees(math.pi/4)
    * b1 A, F5 i9 c0 |9 s
  6. 45.0
    & Q. R6 `! D/ y  Q( \
  7. >>> math.degrees(math.pi), m3 n' B6 g  S/ C- F% ]: L
  8. 180.0
    + G0 Q6 p: q* ~" n
  9. >>> math.degrees(math.pi/6)0 g" g: I& R' V( d. s
  10. 29.9999999999999964 Q- J, [& R) U) H+ U$ M0 q0 d. s
  11. >>> math.degrees(math.pi/3)- F) A. X* B0 i  c% N
  12. 59.99999999999999
复制代码

8 Q' C5 _% K" Z+ Vmath.radians(x)  把角度x转换成弧度/ E# {% e1 i, P0 q
  1. #把角度x转换成弧度/ G; T6 p8 q7 r0 {8 a  u
  2. radians(x)+ G4 _# y  ~0 F
  3. Convert angle x from degrees to radians.
    ( ?3 p( H$ u- t8 b3 h* g
  4. >>> math.radians(45)) o  U5 y: }% s6 W- y, h/ j
  5. 0.7853981633974483* ~* J6 }4 ~/ v2 d
  6. >>> math.radians(60)
    3 M5 X: x1 n6 P
  7. 1.0471975511965976
复制代码
+ }( }. D: o& n7 b8 X
math.copysign(x,y)  把y的正负号加到x前面,可以使用0- e/ K. d6 I/ p
  1. #把y的正负号加到x前面,可以使用0
    : q  K2 c# |8 }# @
  2. copysign(x, y)
    : y+ N, ?6 E. n) x( B
  3. Return a float with the magnitude (absolute value) of x but the sign
      ]& J  u/ e3 j4 ]. {* T
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 j6 U2 s: T& c8 ~& l
  5. returns -1.0.! L$ B7 Z$ `8 Z1 q6 ^' h
  6. 1 [1 b8 j& E* O; W( z! C) r; u
  7. >>> math.copysign(2,3)
    0 q4 C) w$ H  `* t! H
  8. 2.0' @: C  s5 P3 u5 S- Q% x0 G
  9. >>> math.copysign(2,-3)
    ; f; n0 X" ?( V. S4 T& R
  10. -2.02 T! m& J8 L) H
  11. >>> math.copysign(3,8): m2 X4 A( ?, n3 {
  12. 3.0+ B7 F; A6 d8 U: a
  13. >>> math.copysign(3,-8)( O- z- q- I6 i+ c% \
  14. -3.0
复制代码

: P% f2 y# `1 Bmath.exp(x)  返回math.e,也就是2.71828的x次方
3 L/ x7 c8 E% X) J# V
  1. #返回math.e,也就是2.71828的x次方% p  ^2 Y& g6 ^8 `/ c5 ~: E
  2. exp(x)
    & D9 Q# ^" h& E/ }! G
  3. Return e raised to the power of x.
    $ g+ G, F  V( f4 O+ V- k3 V% _# O) A2 K7 ^
  4. 4 g1 ?/ b/ M5 |# l: o1 G1 h
  5. >>> math.exp(1)
    6 s: [7 w5 b# l" R
  6. 2.7182818284590457 P9 m5 Z  C+ s+ P; b
  7. >>> math.exp(2)
    0 f, F: A) y% P2 o
  8. 7.38905609893065: e+ @  y/ `  p2 R4 b/ y3 y/ H% h/ ~! J
  9. >>> math.exp(3)  c2 S6 y! [2 s2 |+ s
  10. 20.085536923187668
复制代码
( e" ?- {9 Z& u
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# D* R6 F- k2 v2 c5 q2 l: T0 f6 f
  1. #返回math.e的x(其值为2.71828)次方的值减1
    6 Z  }& O! U4 j" ?# Q7 N' |
  2. expm1(x)4 l2 f3 R' n/ F  @& p" }
  3. Return exp(x)-1.) e5 i6 D  D9 M' C- P; ]
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    " A6 i( k2 x3 o% L" j0 x

  5. & i+ K5 s9 E0 \' {1 i0 t6 e
  6. >>> math.expm1(1)
    3 i7 T$ K  w3 y5 B! r) U
  7. 1.718281828459045
    : l" y0 K/ W" W
  8. >>> math.expm1(2)
    & e5 P, k  W+ K/ z( `- d
  9. 6.38905609893065
    - D- }$ n$ {  A0 V) Y
  10. >>> math.expm1(3)
    $ Y* ~$ ?$ y4 k9 A  ]5 P
  11. 19.085536923187668
复制代码

" Z' R& i9 j1 O4 ~! Qmath.fabs(x)  返回x的绝对值
4 d) @& e0 I& d, p3 V, x7 ?/ b) @0 o
  1. #返回x的绝对值8 A, _- f) V% {! q* m6 |) y! f
  2. fabs(x)
    ) k- a. Y8 ?: }2 V; X; {% D" B
  3. Return the absolute value of the float x.
    + I% j. i$ J$ }4 u) e: n
  4. - F; E# X: ~8 C5 `
  5. >>> math.fabs(-0.003)
    1 L  v6 H/ q3 ^& O' x" i8 I! f
  6. 0.003
      u8 v( ^* p) p8 |( B- p* c& S+ R
  7. >>> math.fabs(-110)
    $ Z5 K( D) R( k* u% l' r
  8. 110.0
    1 F- k7 P1 W& ?5 r+ d
  9. >>> math.fabs(100)
    2 r- v9 S# d" O' F8 Q) E
  10. 100.0
复制代码
8 \8 m8 w) p8 N; D
math.factorial(x)  取x的阶乘的值5 N* O, D) z. @. m& q" i. M4 z7 S
  1. #取x的阶乘的值% g7 [# ?6 i' x) ^! R
  2. factorial(x) -> Integral- R1 c7 E$ d8 B& {- H
  3. Find x!. Raise a ValueError if x is negative or non-integral./ o, w+ n5 \) `( p: s+ l8 C
  4. >>> math.factorial(1)
    $ M- ^5 C' E* l+ A& F( K
  5. 1
    4 z0 t( D# M1 }  Z; W* `
  6. >>> math.factorial(2)
    4 `$ P! b2 h) ^5 ~" Q1 s( A$ e
  7. 21 j2 {# d! V- r0 j
  8. >>> math.factorial(3)
    6 _* k8 M( I$ T
  9. 6
    , _/ n; ?- ]. d4 {( l
  10. >>> math.factorial(5), j+ s/ u% p$ k0 N" o
  11. 120
    ! p- n: K8 j/ z( P* b* f8 ?
  12. >>> math.factorial(10)
      b" q7 y; u5 A, W% B6 s
  13. 3628800
复制代码
' Y; o) A/ g: {
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
* I& W. Z' x, x
  1. #得到x/y的余数,其值是一个浮点数
    4 `* F7 C' b' D" \0 \2 ?
  2. fmod(x, y)+ K9 ~1 I( w* w- v7 K( I
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    1 \  c" e, T; b0 @( e& `" _
  4. >>> math.fmod(20,3)
    6 |1 g/ |$ }7 f5 u
  5. 2.0
    " n0 |5 n/ W3 U7 G  ]9 U
  6. >>> math.fmod(20,7)7 }. x7 E6 w. P, L4 [) a
  7. 6.0
复制代码

0 O7 a' g' D8 g2 H. z: y  _0 Y# Emath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
; |# C1 @* z" i( V: r! {
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,. v) I8 j; [0 d  _
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值4 d" D/ n  w7 \0 r
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    % i/ e, U9 p4 G+ q, m, i/ k% Z7 O
  4. frexp(x)9 J6 {& J$ O! }3 ?+ `
  5. Return the mantissa and exponent of x, as pair (m, e).
    + ~" w* l# n5 k: I
  6. m is a float and e is an int, such that x = m * 2.**e.6 f9 V  }: D7 U+ X' G7 M& e9 B$ J  }
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.! D4 F/ r3 [8 N0 b
  8. >>> math.frexp(10)
    : `: x4 C# ]2 w6 e3 X2 S% G  f
  9. (0.625, 4)0 y7 Q3 C! ]  s
  10. >>> math.frexp(75). Y" c, l3 v7 a2 D; {  j
  11. (0.5859375, 7). r7 w$ L& C( a
  12. >>> math.frexp(-40)
    1 B0 \% y' q! S( K; q8 }+ I
  13. (-0.625, 6). V9 b6 R) u  }. J! Q  T. N
  14. >>> math.frexp(-100)
    ; }  W" Q; g3 i, }! y: ?
  15. (-0.78125, 7), A- f1 Q  _, z, e$ E
  16. >>> math.frexp(100)
    * @/ T9 N$ u" l
  17. (0.78125, 7)
复制代码
( d' F5 S9 E: [2 o0 a) R
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
5 D& y& H) `# r6 t
  1. #对迭代器里的每个元素进行求和操作
      \) A1 l" B. j; A6 g: P
  2. fsum(iterable). @; C# K" b, M7 @' X
  3. Return an accurate floating point sum of values in the iterable.: U/ y& o# N& L; r+ @' V
  4. Assumes IEEE-754 floating point arithmetic.
    ( ?! Q: G0 |/ a5 ]
  5. >>> math.fsum([1,2,3,4])$ r" J7 \6 @1 D
  6. 10.0  o1 j8 D8 R% R4 z
  7. >>> math.fsum((1,2,3,4))
    " z$ H: q& a0 }, a4 E) Z3 ?* h
  8. 10.0( z! P+ G$ e- P
  9. >>> math.fsum((-1,-2,-3,-4))
    * E* w, M: Z  e
  10. -10.0
    : n- j$ H. t# b8 v4 o$ L
  11. >>> math.fsum([-1,-2,-3,-4])7 j/ r/ Z! [8 W" `3 K
  12. -10.0
复制代码
' }, v% u: \) q8 Q: |+ d* Y
math.gcd(x,y)  返回x和y的最大公约数
, n: o$ v0 r+ y+ Y# ]! v' p6 C8 I0 p: R
  1. #返回x和y的最大公约数
    6 M3 `9 @5 a( ^- e- u
  2. gcd(x, y) -> int
    3 q2 ?* a, M* v3 W" r
  3. greatest common divisor of x and y! B: N8 T4 k3 t( G3 T' S- v/ f
  4. >>> math.gcd(8,6): F8 j) a  o( M* H/ d3 X# B
  5. 2
    ! D9 k; u; ?& `7 P5 Q' S3 p% k) b
  6. >>> math.gcd(40,20)
    1 ?7 B: k& ~( _  Q5 X4 o& u
  7. 20
    : V" r  S. O: x; p, b. v4 O
  8. >>> math.gcd(8,12). @2 j- N- Q3 l" l6 e( R) L8 ]9 u
  9. 4
复制代码
# {; b+ C; k- v9 u% |2 C
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
( a: i! n+ u* b6 b
  1. #得到(x**2+y**2),平方的值
    # g2 ]; L6 u  r+ `
  2. hypot(x, y)+ V* n, ^) ?& F
  3. Return the Euclidean distance, sqrt(x*x + y*y).6 ~- e5 u1 _& _* y
  4. >>> math.hypot(3,4)* H3 K7 c$ ^6 b: K$ j7 ]
  5. 5.03 ?1 c. e* F4 T! ]3 p$ U5 f* E
  6. >>> math.hypot(6,8)
    * O7 g6 i4 O5 Y- G3 j! K) z
  7. 10.0
复制代码

% h3 R/ r  ^1 o. ]math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False* U) O' H/ t9 N5 [# n
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ) T; s, C9 o* p/ J% w
  2. isfinite(x) -> bool$ w" T) }+ Q- ^* o1 t
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.* m( G/ F/ e: _( T- U/ o# C/ v4 A
  4. >>> math.isfinite(100)
    + a5 I) G4 L; q5 h* E
  5. True9 c& s+ {, w! h. w( ?8 E$ E
  6. >>> math.isfinite(0)
    $ c7 F1 k9 ]6 {! ?2 D
  7. True
    ) t7 Z8 I% g7 X) P& P5 X2 {
  8. >>> math.isfinite(0.1)1 Z% f; X% R. x" \3 g8 J: X/ |
  9. True
    ) C3 U0 \: F, q4 Y# F
  10. >>> math.isfinite("a")
    ( ~1 D7 O9 h' M+ k5 \( }
  11. >>> math.isfinite(0.0001)
    4 Y( a! j+ A  T8 w0 Q" V# m0 w
  12. True
复制代码

# E! l7 @9 P( H9 N5 Z1 z7 n5 b6 umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
" h: X( G/ i% @3 R6 ~
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ' @/ S6 S" X3 e# k) p
  2. isinf(x) -> bool6 i4 ^" |: a- }9 P6 H
  3. Return True if x is a positive or negative infinity, and False otherwise.. [% O0 ]/ H, [( j9 c6 u& M8 N+ C" u
  4. >>> math.isinf(234)
    % [/ s9 u+ W% G  H
  5. False
      o* I* s" x4 g4 J
  6. >>> math.isinf(0.1)5 h+ Y: `; R* `. z, q. G# m/ H
  7. False
复制代码

3 L5 Q$ S" l% l) g( X; \( Jmath.isnan(x)  如果x不是数字True,否则返回False/ f1 e) s+ l" g* }" S3 S# p
  1. #如果x不是数字True,否则返回False
    ! w; f8 }1 d. e+ z- c- o( w+ U. I
  2. isnan(x) -> bool( H+ Z- {# c2 e* |% B# [  \8 b
  3. Return True if x is a NaN (not a number), and False otherwise.
    / e+ y+ I3 {4 {2 r  c7 U
  4. >>> math.isnan(23)
    : ]3 [; q9 _1 \6 Y: Y
  5. False
    0 ~+ B7 e4 X. ~( `2 x& r; C6 E+ o
  6. >>> math.isnan(0.01)$ l4 Y( O: Z+ H/ M0 o9 o/ f
  7. False
复制代码
" a" K! r" c$ N" ]
math.ldexp(x,i)  返回x*(2**i)的值
  v$ @, z% f5 b* @3 i6 s' d
  1. #返回x*(2**i)的值
    7 g9 L' ?% d* s! r7 v7 X  U5 _; v
  2. ldexp(x, i)( G+ g; \! _& ]& Y& K/ \
  3. Return x * (2**i).- i& P9 u& n) ~. l3 c" b, Z
  4. >>> math.ldexp(5,5)& p- b* W" `' ?! m# a$ L4 W. H
  5. 160.01 ]  d: f$ m  Z1 `8 V, [- A, K
  6. >>> math.ldexp(3,5)9 H, s% c& J2 `5 a: ^
  7. 96.0
复制代码
5 A( [' e, k8 r1 v/ O
math.log10(x)  返回x的以10为底的对数0 C) u1 m. Y' H* H: O
  1. #返回x的以10为底的对数
      b4 \9 U& A# U" U
  2. log10(x)
    9 Y5 f1 X; A/ P& |. W8 q2 }
  3. Return the base 10 logarithm of x.4 |. C; a2 v# \/ \' P
  4. >>> math.log10(10)* N+ K0 o/ t1 c; n% B
  5. 1.00 `' c2 K! m5 \# E
  6. >>> math.log10(100)
    , [$ _8 ~2 R: C% h/ N
  7. 2.0  I8 r' `1 Z! W) R4 y
  8. #即10的1.3次方的结果为20
    6 F1 @. H: I. W) J' ]2 [+ h
  9. >>> math.log10(20)6 D; d4 W; `! m8 j2 {/ t; @( x  ?/ v
  10. 1.3010299956639813
复制代码

: u6 s1 {/ N1 e& F, G" q* `8 zmath.log1p(x)  返回x+1的自然对数(基数为e)的值
& Z. J; p+ Q1 L# g& K/ j' K3 j
  1. #返回x+1的自然对数(基数为e)的值
    , U3 `, D, `" @9 W% w
  2. log1p(x)" Z3 q. K! g: a. o' w' Z$ p
  3. Return the natural logarithm of 1+x (base e).
    8 q( }9 }6 X6 c* \* b, f( E
  4. The result is computed in a way which is accurate for x near zero.
    2 B  h& H. @; U! g$ G0 J
  5. >>> math.log(10)) @+ Q5 x2 c* K
  6. 2.302585092994046
    ' L& ^; ]" f2 }* ?
  7. >>> math.log1p(10)
    9 P' `8 ]3 N3 }' P/ l% ~% Z
  8. 2.3978952727983707( j. j* n% G- p- ]. ~7 L
  9. >>> math.log(11)
    : B7 |; _) V& @
  10. 2.3978952727983707
复制代码
% V( X8 R0 A/ v2 w
math.log2(x)  返回x的基2对数
  x" p, D/ m2 |9 R) b* k
  1. #返回x的基2对数
    $ S, s0 n; Q/ M# v' F! p
  2. log2(x)6 o7 [; [) @: n; f  J7 Y/ L0 C
  3. Return the base 2 logarithm of x.$ |0 M( F8 {& u, ?4 }( \0 g
  4. >>> math.log2(32)2 j9 u! A' t; h
  5. 5.0  n3 h. J& [- P2 f* |
  6. >>> math.log2(20)
    * k* C, t+ @" E# g
  7. 4.321928094887363
    1 |" V  h$ _, [8 b' I9 d1 T( Q5 w
  8. >>> math.log2(16)
    8 F, r7 F3 i+ @6 u& t7 ]* w
  9. 4.0
复制代码

; O, w0 A& _1 m5 e+ Zmath.modf(x)  返回由x的小数部分和整数部分组成的元组3 }6 Z- @8 n+ @3 m
  1. #返回由x的小数部分和整数部分组成的元组. X8 Y( `0 E. N7 ^: F% K( x
  2. modf(x)
    + {3 ]; G& r/ o& j: ?& b7 e
  3. Return the fractional and integer parts of x.  Both results carry the sign
    , I3 m% z' z0 R
  4. of x and are floats.5 n1 a, w- D4 U# _
  5. >>> math.modf(math.pi)
    0 t( T6 O, r' E% j$ r: |( _; a# w
  6. (0.14159265358979312, 3.0): h; D& l+ i9 p1 H, ~# F; F
  7. >>> math.modf(12.34)
    ) `/ E0 K3 d2 T1 }2 L4 f
  8. (0.33999999999999986, 12.0)
复制代码

! B! x( z6 y9 e! a! e. B! kmath.sqrt(x)  求x的平方根+ @! Y5 ?4 R1 T! \/ X
  1. #求x的平方根
    ; @& w8 c+ G5 [
  2. sqrt(x)) f3 T6 ?) p% W0 H) L  h) L
  3. Return the square root of x.% _- @3 r' W. c5 ^% ~
  4. >>> math.sqrt(100)6 e2 P$ z  M. ]+ f+ u. r$ r
  5. 10.0
    % k+ |8 }4 B, `) A9 X% s( r
  6. >>> math.sqrt(16)
    $ s" C/ e5 C# F
  7. 4.0
    $ L. w" u; t2 n" d/ ?
  8. >>> math.sqrt(20)" N1 j( O& ]- q
  9. 4.47213595499958
复制代码

/ t# P/ n& i. S1 B1 g0 J' kmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 J$ i* k; @$ \+ Z8 ^$ }/ }
  2. trunc(x:Real) -> Integral
    . T5 J3 ]+ y0 t6 b; j
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ x. n2 C$ U+ l9 u5 c1 D' p
  4. >>> math.trunc(6.789)+ z- v6 p1 O7 S9 n$ Z1 P; b7 X( J6 k
  5. 63 H9 L- l4 L6 M; o/ L
  6. >>> math.trunc(math.pi)% \- }- k1 U8 @( q
  7. 38 |+ E7 C3 ]" Z8 ~6 A: D! U
  8. >>> math.trunc(2.567)
    * G6 C& m/ \) ?
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-25 21:11 , Processed in 0.083069 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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