新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

" {7 Q, o9 ~' D8 G【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。" j  I7 E* s( d' v7 @( H

5 O4 `$ X, q' a3 v. X6 \$ D方法1
5 D) e5 M& l+ u8 \
  1. >>> import math
    0 M. m9 _( H/ L9 z) A
  2. >>> math.sqrt(9)
    - Q2 Y$ U* d6 `4 O4 W: }- [" @
  3. 3.0
复制代码
方法2% \% G. ?. ^' O- y7 Z7 P9 |
  1. >>> from math import sqrt
    : l# L, p, s/ k" _* n# E. D
  2. >>> sqrt(9)' _- @& X0 C( q; R, U0 J
  3. 3.0
复制代码
* _9 h* o0 \( @# {* w- e  [

! z9 n& M6 k) b1 b6 u5 g: a
math.e  表示一个常量4 ?& a6 V8 D2 N$ p( ^3 }" ~
  1. #表示一个常量' f+ A; l; r. L* x
  2. >>> math.e
      }2 ]. d7 A. A3 a0 C2 z- x. Z4 _/ k
  3. 2.718281828459045
复制代码

4 L" F) D3 n( w( zmath.pi  
数字常量,圆周率

; U' G1 c2 j; l3 |% l0 m0 E$ z2 k
  1. #数字常量,圆周率
    # _9 G3 Y) Y5 h# Q* r. s
  2. >>> print(math.pi)
    7 `/ z; r/ a! P. {9 B" W
  3. 3.141592653589793
复制代码

8 a4 A# [7 w7 p: J4 pmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

! Q9 J& c# n# x) K( O
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    , v- j- o0 G4 H: ]7 F' x
  2. ceil(x)- X* f, H7 j0 U  B$ q7 _# x
  3. Return the ceiling of x as an int.0 q% Z* D1 q" v/ U
  4. This is the smallest integral value >= x.
    * i- \6 r/ U2 h+ _% S) U
  5. & j' s, k8 M6 x' ~2 j! W1 a
  6. >>> math.ceil(4.01)" a# p: _' s4 s5 r4 @
  7. 5) \0 `7 m& D9 ~/ o
  8. >>> math.ceil(4.99)3 J) h1 l! v  z! c8 M, C
  9. 51 a# l" n; U6 i+ b% B2 a8 l
  10. >>> math.ceil(-3.99)) N' G7 p$ }' e  h2 u: r
  11. -39 U) D$ c1 H% L4 N
  12. >>> math.ceil(-3.01)' P( G* B- p; a- _
  13. -3
复制代码
2 v  {  P' ]2 \( a
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身- v, J, X: O5 h: u
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 \3 x9 U; `$ _- j
  2. floor(x)
    & a  D0 M! @. Q( R2 n0 u
  3. Return the floor of x as an int.7 B2 k$ t' A3 ^  N( i
  4. This is the largest integral value <= x.
    7 h8 k3 h& X8 \2 S# {
  5. >>> math.floor(4.1)
    . ?/ _5 ]' b  J1 b
  6. 4* K6 X3 H5 [/ K: f! O
  7. >>> math.floor(4.999)
    8 V0 o' V3 f% }8 m& n; ]
  8. 4+ Q" X, A" n' ^9 E# ^$ O/ |0 Y
  9. >>> math.floor(-4.999)) T" ]5 L" H  t0 E7 U* [
  10. -5
    $ \, S& x$ P( a# ]
  11. >>> math.floor(-4.01)  S5 i$ H& _( P  q
  12. -5
复制代码

# H% B9 \. i* K3 kmath.pow(x,y)  返回x的y次方,即x**y  B* }! R/ R9 c& i. \2 R; b
  1. #返回x的y次方,即x**y& x" V; ?1 z( l5 T& K$ R! u
  2. pow(x, y)
    9 I6 `- O7 }. t  |7 p) Y
  3. Return x**y (x to the power of y).
    ( ]6 T7 u( G+ z. W
  4. >>> math.pow(3,4), c9 D7 Y# `1 f! p- P8 G' v5 N. U
  5. 81.0
    - b& {& v4 ]2 P# U  j" v; V
  6. >>>
    - T4 V" y: _/ m  A: |( a& Y
  7. >>> math.pow(2,7)) a( C/ W( B& O' V
  8. 128.0
复制代码

' f9 Z2 ^: L7 `& }math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ b9 ~8 V" @, d+ C" n9 i. p) O
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* l8 X  ?5 h! x% s1 N% ]
  2. log(x[, base]). ]: q# Q+ M7 O5 S' r# n; }0 E' H' r
  3. Return the logarithm of x to the given base./ r" U9 ^9 I& J1 z* a8 |
  4. If the base not specified, returns the natural logarithm (base e) of x.% q5 b4 X7 z8 R
  5. >>> math.log(10); S, C6 X4 d0 _* y9 C' p& t
  6. 2.302585092994046
    ) T; \$ \  \1 j& a' g
  7. >>> math.log(11)2 K2 U  y6 {2 ^% l, M
  8. 2.3978952727983707
    # P3 a: R6 r: M% Z) {& p# f: t1 l
  9. >>> math.log(20)
    0 U1 w' V2 ]- R
  10. 2.995732273553991
复制代码

$ P* g8 U1 [6 U+ n! P7 j$ b' ?math.sin(x)  求x(x为弧度)的正弦值
  j, w1 j/ G- L
  1. #求x(x为弧度)的正弦值5 [  s. ?# A8 |4 p3 C  o0 W
  2. sin(x)
    6 b, F( u. D7 l0 g; A* ?( f
  3. Return the sine of x (measured in radians).
    0 Q6 c& V+ K2 M
  4. >>> math.sin(math.pi/4)/ f  l+ p0 m; v8 h/ F: m- D5 i
  5. 0.7071067811865475
    ' I( K, a# s7 [/ R; n$ u  q
  6. >>> math.sin(math.pi/2)
    ( [; U5 N5 V2 q
  7. 1.0! ]2 ?6 w% _7 l  i& [# q# W
  8. >>> math.sin(math.pi/3). B- J) n& C5 X, k: l
  9. 0.8660254037844386
复制代码
, z& a& l, e( b: g0 |! J/ W
math.cos(x)  求x的余弦,x必须是弧度
4 `' f& _3 i4 W
  1. #求x的余弦,x必须是弧度
    + U/ A5 P8 M6 Q" Y/ e' n
  2. cos(x)
    9 e+ ], p2 @6 A/ c  j, k
  3. Return the cosine of x (measured in radians).1 l$ t9 p0 }* [5 {
  4. #math.pi/4表示弧度,转换成角度为45度  b4 O8 V1 Q* K. Y% B
  5. >>> math.cos(math.pi/4)
    ; h$ Z& g0 k0 m% Q9 P6 \
  6. 0.7071067811865476
    ' V2 c% J' R- K+ F! Y6 i
  7. math.pi/3表示弧度,转换成角度为60度  M& `3 G0 K( i8 }& _0 ?1 u
  8. >>> math.cos(math.pi/3)9 k  G3 Q6 ~9 q6 P, j; Q9 F
  9. 0.50000000000000012 F, @  Q2 S9 {) n
  10. math.pi/6表示弧度,转换成角度为30度
    8 N6 u! Z9 ]$ X
  11. >>> math.cos(math.pi/6)$ p# d( h  M  f5 q
  12. 0.8660254037844387
复制代码
. _! V2 }# S1 O: _. m% w' b
math.tan(x)  返回x(x为弧度)的正切值
; N- J; g2 b5 U, `6 E4 O5 m$ P) N$ \+ j
  1. #返回x(x为弧度)的正切值
    ( ~9 N" g8 m! ?; W/ u
  2. tan(x)% r7 G2 K9 m- P; G/ G1 k
  3. Return the tangent of x (measured in radians).0 H0 S& V5 A5 s+ _/ I: E  W
  4. >>> math.tan(math.pi/4)5 H% x' Y+ J* j* L7 \- z6 p% K
  5. 0.9999999999999999; a4 k# {8 I; s7 _2 c* T3 w- P6 f
  6. >>> math.tan(math.pi/6)3 z1 J- A& A+ G" u1 j
  7. 0.5773502691896257
    2 y- y' f- e5 p* c, H& h
  8. >>> math.tan(math.pi/3)/ G% j" }& F) L7 p; D; I" b
  9. 1.7320508075688767
复制代码

! f1 Y" s1 W# B" G) gmath.degrees(x)  把x从弧度转换成角度
7 R7 ]; o  J, C1 T" }
  1. #把x从弧度转换成角度% z' k( V+ b7 E$ V# r# r  b
  2. degrees(x)% K. J; e  Y1 F8 P/ H9 c
  3. Convert angle x from radians to degrees.
    * D4 z8 w: Z2 Q3 H% v, K

  4. : H- K4 S# v) s, `; H; m' ^
  5. >>> math.degrees(math.pi/4)$ d2 d6 A% D9 O5 O5 Z
  6. 45.0
    9 L* @. |- b# U# I& |/ L3 W  R
  7. >>> math.degrees(math.pi)
    ( s# S: s- T3 m8 a4 _
  8. 180.0) [5 y( Z. b/ n# |
  9. >>> math.degrees(math.pi/6)# H' j& m8 O/ S0 |+ ^
  10. 29.9999999999999964 k" B5 I) P$ [! C
  11. >>> math.degrees(math.pi/3)
    $ g+ C: \& N* i! Q/ ?8 T
  12. 59.99999999999999
复制代码
% L! ]) b3 [7 T6 ?
math.radians(x)  把角度x转换成弧度
2 A. }' n8 S$ B4 L' [' {0 a5 I! p
  1. #把角度x转换成弧度9 ?+ A: f: E* O0 a& A0 T- X
  2. radians(x). V4 T) Z& e' C+ i8 L/ Q1 B
  3. Convert angle x from degrees to radians.- o* i  H' v4 u7 @6 e
  4. >>> math.radians(45)
    ' G) i' Y0 p' [
  5. 0.7853981633974483
    5 x3 h" l# ~; r; N* V1 S5 Q
  6. >>> math.radians(60)% _5 J" s5 a( W
  7. 1.0471975511965976
复制代码

; j; t4 D2 g5 r$ z: Jmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
" ?* S. P4 {0 A% ~: W$ E; ~
  1. #把y的正负号加到x前面,可以使用0
    : E- k. w# }4 B3 D& ?
  2. copysign(x, y)  ]  K: m( ^2 B" c! q6 o" r; o
  3. Return a float with the magnitude (absolute value) of x but the sign 5 K) v. i8 \) m; q
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , z6 v3 }% m1 ]" F6 F5 Q' P' f
  5. returns -1.0.
    ! H& D) E3 K' K/ G  K$ v

  6. 8 v( F  ]- k7 ~7 \- I9 {
  7. >>> math.copysign(2,3)
    2 E% P" c2 X# t$ v8 R6 s3 n
  8. 2.0
    % X" O& p2 _) C1 q) B! |+ I
  9. >>> math.copysign(2,-3)
    * }) F+ ]& F9 ~+ Z7 K1 N8 C9 C% z2 B
  10. -2.0
      T* _: g  W( J! L4 x
  11. >>> math.copysign(3,8)
    1 q% X- Y  [! e! x7 f8 H
  12. 3.0
    % l, ]/ b- j6 [1 t4 p2 i0 g$ Z
  13. >>> math.copysign(3,-8)( W' R8 {: A5 p$ N- p! e
  14. -3.0
复制代码

- M2 ~; F* v3 D/ X5 E! qmath.exp(x)  返回math.e,也就是2.71828的x次方7 R' [& f7 W, T5 `
  1. #返回math.e,也就是2.71828的x次方/ w. O# }" ]; t0 j
  2. exp(x)
    2 e0 I# o' _" U
  3. Return e raised to the power of x.
    . V/ m0 ]5 d3 h8 D; b# j+ z
  4. 8 F8 B: H. |% [/ @0 f2 U- i
  5. >>> math.exp(1)
    ' ?% e2 f2 m7 A
  6. 2.718281828459045
    5 z) M) l2 y2 v" m4 j$ f7 l
  7. >>> math.exp(2)8 Q  e, F+ y& h. D- ?( s
  8. 7.38905609893065
    8 f$ a. t: G) b/ z" m
  9. >>> math.exp(3)4 _- }1 a/ o8 ^; h6 Y& E4 ]
  10. 20.085536923187668
复制代码
. M" ], E( E3 q/ C: Y
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
# M" g/ l$ S% b: P. |0 g8 t7 ]
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ; ^8 K+ g% `. ?% n+ v
  2. expm1(x); {& O) z3 ~0 Q
  3. Return exp(x)-1.
      E! `0 l# J( I  w* M0 X
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    " o# m/ C+ y1 e8 T
  5. " W% {3 \$ ]1 O
  6. >>> math.expm1(1)
    , X& z/ X$ p& |8 w
  7. 1.718281828459045
    : Q, W( Z; B* o9 V2 Z& w
  8. >>> math.expm1(2)! ^8 \6 l9 F" P8 C7 C2 X( F
  9. 6.38905609893065
    % d8 W9 J, X" P5 O* N, [* H% x. K; u
  10. >>> math.expm1(3)
    . X  f! u: @0 R$ [) _
  11. 19.085536923187668
复制代码
. ]& t: u! T4 r& o
math.fabs(x)  返回x的绝对值2 I: s4 W5 a5 V7 Z
  1. #返回x的绝对值2 s. J& q! W4 Z
  2. fabs(x)
    7 Q& O4 d: ~9 a
  3. Return the absolute value of the float x.
    2 [: ]4 a9 j) C7 W

  4. ) v- l) W- t  s, z% P
  5. >>> math.fabs(-0.003)
      ]( V" Y9 ?- ?9 v3 [9 f& d' r
  6. 0.003$ O6 ], D0 f% a  A" A3 A
  7. >>> math.fabs(-110)
    # C) y* ^# [  g/ Z: q+ D6 ]; B
  8. 110.0! s' P; T* A- [3 E3 H, t
  9. >>> math.fabs(100)
    5 F6 u6 n" v/ b/ ]
  10. 100.0
复制代码
* b1 _" [/ u" s
math.factorial(x)  取x的阶乘的值4 \2 Z+ g7 O7 P* i5 o( |7 b
  1. #取x的阶乘的值
    8 {+ K% g5 F5 a  W) L; y
  2. factorial(x) -> Integral
    - J2 U+ W9 g. ?3 w# Y9 [
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ; D: Y$ A6 B4 K
  4. >>> math.factorial(1)$ z! t- X5 [+ H: w
  5. 14 \9 K! |2 f9 h0 j/ U; {
  6. >>> math.factorial(2)+ e/ l+ b1 [1 ^: i; i9 D
  7. 2% ^9 B) a1 A3 J. @& r
  8. >>> math.factorial(3)0 Z, `: r1 X6 \: M( G+ k8 Y% K
  9. 6
    $ z2 R5 L& H+ b2 E" J
  10. >>> math.factorial(5)
    4 M2 r6 _$ t- l" g5 R) \* t8 |
  11. 1205 y! n6 L2 \% e
  12. >>> math.factorial(10)+ `$ k& }. `1 L# G  b
  13. 3628800
复制代码

4 @+ ?7 y% ]! c9 n' [8 i5 Tmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数# X7 C1 e' Q4 B1 l+ B' F' ^
  1. #得到x/y的余数,其值是一个浮点数1 w! G5 ~  W% }* h8 W* f
  2. fmod(x, y)
    " j' ?1 a5 O+ H# t) ~* O" g! `
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    4 K0 s$ V2 O2 F2 k/ P- A. p+ S6 f$ b
  4. >>> math.fmod(20,3)! [: b+ g) z& S+ l
  5. 2.0
    # U: b( }4 d/ A
  6. >>> math.fmod(20,7)
      q% N6 ^- {: D4 b' @5 x
  7. 6.0
复制代码

8 M9 x- e; C+ E' J' Z* g! }math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围/ U/ J; A4 P1 ]! ?! }$ l$ v# }
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 v( y' V! ?  _) ~" g7 ~3 b/ d- n* B
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值: u0 w4 ?; b& D; a% L; H1 W( y
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    7 Z; ]# v# [9 H4 g6 ^
  4. frexp(x)
    ' x6 C  u& b  G  u  `
  5. Return the mantissa and exponent of x, as pair (m, e).0 s4 V& k" Y5 d/ U/ q
  6. m is a float and e is an int, such that x = m * 2.**e.( w7 [2 q! _7 V4 x# D9 V
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.  `+ ]: ]' f+ w
  8. >>> math.frexp(10); M6 R- x( H- f0 H
  9. (0.625, 4)
    2 q/ ^; b8 X+ t' b7 e, ^
  10. >>> math.frexp(75)4 Q( l$ M. P! W8 s4 y6 a  v
  11. (0.5859375, 7)
    ' Y' Y# B- \4 X. u' Q
  12. >>> math.frexp(-40)  E9 Y% Y2 z( v
  13. (-0.625, 6)( F1 e1 m  {& r
  14. >>> math.frexp(-100)
    $ U+ t9 \8 a& S$ e4 T! g, b
  15. (-0.78125, 7)
    7 f0 M$ P$ f8 I3 N6 N
  16. >>> math.frexp(100)! x* m/ e; m& V
  17. (0.78125, 7)
复制代码

( b' H: M% N/ e/ Rmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
& v) u4 N! W; s% H0 R% m5 k6 l
  1. #对迭代器里的每个元素进行求和操作( j% Z  R! Q1 w' G8 g( P/ |! {
  2. fsum(iterable)
    3 t1 o9 e% S- {! N8 d# m, O
  3. Return an accurate floating point sum of values in the iterable.
    % t' ^, y- p5 e+ {* L
  4. Assumes IEEE-754 floating point arithmetic.
    ' L+ g" A' _$ }/ d* U9 P! U& [1 p
  5. >>> math.fsum([1,2,3,4])
    * m- d& p6 w4 u! G
  6. 10.0
    2 c: J, t& o* q  B5 Z* m
  7. >>> math.fsum((1,2,3,4)); n7 k: c4 r6 Y% m3 `" z, v' t9 T
  8. 10.0; w/ ?. N5 ~. D. X! S+ U. S( n0 [
  9. >>> math.fsum((-1,-2,-3,-4))  P% D& v- u0 a+ z
  10. -10.0# ]$ w) j  ~# m0 T* ^9 y! k
  11. >>> math.fsum([-1,-2,-3,-4])' F1 O1 `& \; D  v0 J
  12. -10.0
复制代码
6 x, n4 [$ W7 H+ i# v; _' w$ E
math.gcd(x,y)  返回x和y的最大公约数0 a' M3 d8 w2 b% m& z
  1. #返回x和y的最大公约数4 s, e, T% Q9 E  N6 L1 m
  2. gcd(x, y) -> int
    ' t; T0 S* s" z7 K% M4 h( n; i
  3. greatest common divisor of x and y
    5 x) k# \. B6 e( I. Y
  4. >>> math.gcd(8,6)
    4 K+ V9 ^; }! D0 P
  5. 2
    5 T2 a, q/ Z$ w- Q, L" r
  6. >>> math.gcd(40,20)8 }" D7 W0 K0 r& Q  M$ X/ R
  7. 20
    4 m) o0 K6 ~0 m6 D: s4 x( Y
  8. >>> math.gcd(8,12)  ^/ Q% P' g. K( V# u5 s( ~
  9. 4
复制代码
2 u: D$ ?8 ?& g' z5 w4 `
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
- l# {9 M; j; A/ Y
  1. #得到(x**2+y**2),平方的值, T$ G' R: g/ i+ \0 Q7 c
  2. hypot(x, y)
    . |0 W: Y% D  T( R# e- l" r
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    1 |8 c8 I1 e5 Z# F* t& m
  4. >>> math.hypot(3,4)0 N' m# V% B. H- d6 f  w
  5. 5.0+ b; b$ _; s. x2 c" f& Q' ?6 P. ^
  6. >>> math.hypot(6,8)
    ' v( p& y7 S0 X& K( O( @& |
  7. 10.0
复制代码
3 {* Z% D% k$ `6 {2 X. M. Z
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False4 H4 m. s* @' R. B0 l+ }6 _5 r0 f; |
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    * K% i5 l. M- T5 L4 j0 \
  2. isfinite(x) -> bool$ {- B( N8 i, ~7 b( P
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.- b: W9 e. A! |! S) k, x3 Q9 w
  4. >>> math.isfinite(100)) k9 y, q1 V) I5 L' D  R2 [# _
  5. True9 Y3 ^0 l) d5 {- N  O+ N: }
  6. >>> math.isfinite(0)  P. S! e9 G2 ?, g
  7. True- n; p3 t7 t1 R' p  r
  8. >>> math.isfinite(0.1)
    ' C6 X0 N3 M; C! J9 u, O4 q! X3 ~  ^
  9. True
    / U( j9 j' h1 S# E
  10. >>> math.isfinite("a")
    : }- V- n# v) |6 K" v( S$ u
  11. >>> math.isfinite(0.0001)
    & P  E7 S/ w0 z6 J
  12. True
复制代码
- u2 ^1 m8 S4 \; ~
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False) p2 L" k; e5 ?( o: O/ c" B
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ) S$ G+ s  `& E* K
  2. isinf(x) -> bool. R2 c' D8 x, v
  3. Return True if x is a positive or negative infinity, and False otherwise.
    5 k. K# b% O1 w" e6 `7 S* X5 a3 ?
  4. >>> math.isinf(234); K0 B4 j& w) w7 _3 I6 {
  5. False
    2 i- y! X2 v9 {1 K8 i, Q
  6. >>> math.isinf(0.1)
    7 ~% H$ ^6 G- v
  7. False
复制代码
  j# ?: N+ N) v! ^, }0 W
math.isnan(x)  如果x不是数字True,否则返回False
: }- D! |7 s" ~! g  c" H
  1. #如果x不是数字True,否则返回False
    ! F; @, a  E7 }$ [* @: ]# z8 j
  2. isnan(x) -> bool" A! V) K, C5 @9 |/ V' W' L5 j
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 x* L& ~! {% y; N5 z3 |
  4. >>> math.isnan(23)& z( ]( C( {8 Y4 X# k: D& K
  5. False7 e' n" j7 A' u- y) c
  6. >>> math.isnan(0.01)
    . F! t" o9 K5 a6 M. r2 d
  7. False
复制代码
5 d3 z6 J5 c, A( ?
math.ldexp(x,i)  返回x*(2**i)的值
5 ^# t( t  S  H+ f
  1. #返回x*(2**i)的值5 n; X: B8 O9 c; [1 i9 N# Y8 Y
  2. ldexp(x, i)
    * @" f, O9 o. I* w$ N; D
  3. Return x * (2**i).- D: }4 V! A% V
  4. >>> math.ldexp(5,5)$ z4 u# X' I0 b- ?! v
  5. 160.0: d+ c1 l% k1 ]% P; D0 h
  6. >>> math.ldexp(3,5)
    - R: E8 S% L" o9 i8 ~; G
  7. 96.0
复制代码

& _$ b9 _9 M* r6 h7 x( e4 X- Y/ Bmath.log10(x)  返回x的以10为底的对数
, g  T! F3 z" n
  1. #返回x的以10为底的对数
    5 R9 i% Z5 S/ K- S/ H
  2. log10(x)1 r$ O" A% M6 I  m
  3. Return the base 10 logarithm of x.
    ( }( ^% C' Y! ^* k$ X/ ?( l/ T
  4. >>> math.log10(10)" a' J# e3 j, [' p
  5. 1.0
    5 J3 F2 Z7 s9 I
  6. >>> math.log10(100)! R8 }# [+ S9 J
  7. 2.0% J) W; v6 o2 {9 Y0 ]
  8. #即10的1.3次方的结果为20
    . W- U% Y$ Y: M! b: p( f2 }
  9. >>> math.log10(20)
    8 G" c: F$ B/ ]0 A
  10. 1.3010299956639813
复制代码

) m5 g3 u$ |8 ~% r$ {$ @7 U4 Gmath.log1p(x)  返回x+1的自然对数(基数为e)的值
7 ~( D; F- n, q2 V! S
  1. #返回x+1的自然对数(基数为e)的值
    " Y/ y6 j0 Q$ Q) N. b
  2. log1p(x)
    2 @9 F! \+ U, q
  3. Return the natural logarithm of 1+x (base e).
    ; H4 H- {, Z' `5 ^' |4 V. A
  4. The result is computed in a way which is accurate for x near zero.
    # B% ?- N# x8 c1 n9 k! A# h
  5. >>> math.log(10)+ K5 {) n' L7 v+ T5 Q. z% R
  6. 2.302585092994046
    2 p6 s2 F  M# N6 o
  7. >>> math.log1p(10)
    $ F4 l# T/ b% Z3 c) Q' U1 o
  8. 2.3978952727983707
    # b0 W& S& t$ a2 l
  9. >>> math.log(11)4 ]& \6 |, n& o. N; \
  10. 2.3978952727983707
复制代码
! ~( z' E; j) F" H
math.log2(x)  返回x的基2对数
- v5 I7 E, d6 r: }+ ?, s0 e
  1. #返回x的基2对数) Z7 f$ X& O% U+ ^! q, B% g" M; A3 m, r
  2. log2(x), _* x6 x" }+ e5 O
  3. Return the base 2 logarithm of x.6 I/ o( F' R* d9 D5 W0 }. p2 Y
  4. >>> math.log2(32)
    ( f7 Z4 @3 i& V0 O+ n  c
  5. 5.0; Z& ]) H+ r' H; ]0 x0 n4 e
  6. >>> math.log2(20)
    ; b* \3 u% C7 i
  7. 4.321928094887363" o1 Y% [& @3 |4 J
  8. >>> math.log2(16)
    , e* K" P' c; M5 }  L! s& Y
  9. 4.0
复制代码

+ E2 f8 z0 G5 w+ m9 Mmath.modf(x)  返回由x的小数部分和整数部分组成的元组# l! W2 X2 S5 T% [
  1. #返回由x的小数部分和整数部分组成的元组
    ( T2 v4 Z& D, o4 f7 f) g3 M1 n7 |
  2. modf(x)
    / D4 h( G$ \1 o
  3. Return the fractional and integer parts of x.  Both results carry the sign
    # m0 Y+ N1 H& |8 U
  4. of x and are floats.4 V" T; ~  V# ~/ r2 t
  5. >>> math.modf(math.pi)( a. _/ P2 }3 }- ?" ^0 E& W
  6. (0.14159265358979312, 3.0)" n4 Y. o& O) Y  z2 _
  7. >>> math.modf(12.34). @6 b) h/ F& g+ G9 H
  8. (0.33999999999999986, 12.0)
复制代码
4 r/ X7 C3 C/ E: u  A4 z# M
math.sqrt(x)  求x的平方根
& h0 m5 r' ~* X8 }8 o( `6 q
  1. #求x的平方根
    $ q5 i! O, S' n5 k$ p1 w9 @
  2. sqrt(x)5 R) \3 o' C* Z. ^& Q" N
  3. Return the square root of x.3 @; s, |8 @3 B% x2 S9 J
  4. >>> math.sqrt(100)' t" y1 Y- g8 _  H' T3 B" ~% Z
  5. 10.0
    # {" t9 O$ w1 P. n, S
  6. >>> math.sqrt(16)
    8 X$ V7 ~% v3 p) R. P- y
  7. 4.0
    0 G8 r) ^4 I- W& H. x( j
  8. >>> math.sqrt(20). E2 y3 D. `5 D/ M9 f0 M& R
  9. 4.47213595499958
复制代码
/ Y( B- ^: J5 d: K
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    - ]# d; Q- B* s( ?; R8 a. o
  2. trunc(x:Real) -> Integral) t+ e1 @1 a0 m& b% k$ O) x
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 L- F; w4 C, A1 F
  4. >>> math.trunc(6.789)
    1 H8 W5 B9 ?( r( p; o- c
  5. 6
    + Q: C" |9 k9 b
  6. >>> math.trunc(math.pi)& W$ c! `+ J) g9 c
  7. 3
    2 l8 @- K, q" T7 t  Y, j
  8. >>> math.trunc(2.567)- z  r' g5 _! [5 w% b
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-22 13:50 , Processed in 0.094222 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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