新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

  a& k# Y% D# t! P【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。; y( l$ x3 i! c

) Q" ]/ p) m7 m6 I' M方法11 S2 D0 H) D5 ?! e
  1. >>> import math
    ) M& Z' `2 {$ k
  2. >>> math.sqrt(9)
    / @( r6 M) }$ W
  3. 3.0
复制代码
方法23 r5 }1 @8 B: E. h
  1. >>> from math import sqrt3 ]7 L( o/ {" t' S, p
  2. >>> sqrt(9)6 W. X- V  R; u& ~1 o' P
  3. 3.0
复制代码

; c  q. Y: f: L; c
+ s6 c  K* _- e* l& ~9 w! {
math.e  表示一个常量
& L5 s0 `. s5 U" ]5 U' j
  1. #表示一个常量' I7 g9 e1 B4 N9 A; p
  2. >>> math.e
      R( J0 i' e7 ?* B2 E# ], h4 T
  3. 2.718281828459045
复制代码
2 z. q7 E. E& _1 r' `- J+ ~
math.pi  
数字常量,圆周率
6 @- g3 C2 H) T$ A- H9 U2 _
  1. #数字常量,圆周率
    6 Y/ M: e; _$ p7 }* ]* Q# Y
  2. >>> print(math.pi)
    0 C3 X" `- J7 a4 H' h
  3. 3.141592653589793
复制代码
+ P8 M' [4 I* b2 L7 ~
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
& }/ L; a% x& g/ n1 ^& ~7 q6 a
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x+ R% \* j0 P% p) Z. u
  2. ceil(x)9 e/ v* o" v5 F# V1 |( q/ B- ~
  3. Return the ceiling of x as an int.; ?, ^) J; F2 f6 D2 w
  4. This is the smallest integral value >= x.
    6 j, I, @* \; A6 b% `4 j

  5. 9 ]' Q  o* b8 t" a
  6. >>> math.ceil(4.01)3 Q1 p) T5 l1 j
  7. 59 `3 F% f4 `: v+ X6 G! d
  8. >>> math.ceil(4.99)* r& h8 y( ^1 ~$ A7 O
  9. 5
    & v; L' E$ t5 D/ I0 d* o! w
  10. >>> math.ceil(-3.99)
    3 D0 }2 \) ~% p3 \/ i" ?2 p# _
  11. -3
    ; y, c4 ]9 v. l7 h
  12. >>> math.ceil(-3.01)4 d" A) h$ M7 f2 Y$ W
  13. -3
复制代码
$ y- h) Z+ I+ T$ Q
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
5 X4 [0 H9 Q# u; X
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    3 M  `: b: Q. W0 U; _  o
  2. floor(x)
    ' ~9 o; s" k  I7 P& M$ L: K
  3. Return the floor of x as an int.$ h; F9 ]2 ^4 n+ C$ r) x+ t
  4. This is the largest integral value <= x., z* u- S7 k. D! `. y. S1 w! l
  5. >>> math.floor(4.1); U8 X5 x( K$ r9 N* w) x
  6. 4
    ' _/ {# }. r( k' j  L* |5 S
  7. >>> math.floor(4.999)2 Y( Z0 s6 h' g2 F2 W
  8. 4
    " \- ]9 R: M* Y& v* J( A
  9. >>> math.floor(-4.999)1 G$ {) j1 ~) R. t' }5 q& p5 x
  10. -5
    1 l$ p8 _/ h) Q" j
  11. >>> math.floor(-4.01)! J9 H; `! r0 t' E( `8 T, Y
  12. -5
复制代码

( {7 t, N! Q+ [$ Umath.pow(x,y)  返回x的y次方,即x**y* T9 ~+ m$ R. M' i) Y8 v& L
  1. #返回x的y次方,即x**y+ E7 H; p0 z! W0 q/ a
  2. pow(x, y)4 v* N  h; G6 t
  3. Return x**y (x to the power of y).0 K; H6 d$ ]7 M3 m/ N
  4. >>> math.pow(3,4)
    ; M& u9 J9 b! f
  5. 81.0/ [0 J9 W6 u0 t; P9 x  N
  6. >>>
    2 t' l4 y8 T% t) x* D3 z; I
  7. >>> math.pow(2,7)
    . ]. R4 O+ V8 {: ^, s; ?' Z
  8. 128.0
复制代码

$ ?: g+ T$ d' E3 t; |math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)" F: T. P; h/ F/ c
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ( `! {! D$ ?7 o, I
  2. log(x[, base])) d6 o5 D+ I1 r$ G; ?+ l  D
  3. Return the logarithm of x to the given base.8 h5 x6 l$ O+ O& C
  4. If the base not specified, returns the natural logarithm (base e) of x.: W4 R( y$ n( Z
  5. >>> math.log(10)0 K; a9 c9 L: B7 e+ G" h
  6. 2.3025850929940467 p7 ]) y9 \; l2 J
  7. >>> math.log(11)2 \' N5 _" X# z' U" i
  8. 2.3978952727983707/ ?) l5 `7 s6 F: X8 g+ t) H
  9. >>> math.log(20)
    - g( m  r5 u) ~( J
  10. 2.995732273553991
复制代码

" E4 [% d& m& y# R5 S/ m3 k7 rmath.sin(x)  求x(x为弧度)的正弦值+ s( {' ~. z( g  `* D
  1. #求x(x为弧度)的正弦值
    % W- I6 w" W( |9 d) q
  2. sin(x)4 c0 R; r) a. @# U' r/ t% a# P5 g! e
  3. Return the sine of x (measured in radians).' R4 e* ^! y; g7 ^9 V3 V4 v! Y- n
  4. >>> math.sin(math.pi/4); B" d: t6 Z1 H9 j% h. g8 K* ]) E
  5. 0.7071067811865475
    + ^" _' B' t" f
  6. >>> math.sin(math.pi/2)
    9 d4 l7 @- C& M; |# K: Z
  7. 1.0
    0 S4 T; w0 L* s; }0 y
  8. >>> math.sin(math.pi/3)7 b# C+ Z; s  `2 t: p
  9. 0.8660254037844386
复制代码
' m; W$ K+ E2 j; J6 H
math.cos(x)  求x的余弦,x必须是弧度
+ Z6 d5 j% ^! n+ g; `
  1. #求x的余弦,x必须是弧度
    0 C, E% c2 x8 u
  2. cos(x)6 E" z& E8 `$ ^& I+ r$ x
  3. Return the cosine of x (measured in radians).7 H9 V9 B2 \# X2 S9 b  X4 }8 D5 ?! h
  4. #math.pi/4表示弧度,转换成角度为45度
      s* o, A3 D' E
  5. >>> math.cos(math.pi/4)
    0 J' s4 y  X* ]  Y+ P
  6. 0.7071067811865476
    2 A8 ]* B0 d5 @) ?
  7. math.pi/3表示弧度,转换成角度为60度
    7 o0 a: X5 _+ ^, |1 d) [
  8. >>> math.cos(math.pi/3)
      O3 \6 ^2 x! L3 E* t
  9. 0.5000000000000001& D' x! e2 O" m
  10. math.pi/6表示弧度,转换成角度为30度5 P" I+ Y8 M: \7 v9 J/ I9 f
  11. >>> math.cos(math.pi/6)
    " @, s" p* o; I3 l: T4 j: Y8 \
  12. 0.8660254037844387
复制代码

, r' q8 \' s+ s, R7 Jmath.tan(x)  返回x(x为弧度)的正切值
$ Q! f& y( y0 q: ]" t8 e2 F/ X
  1. #返回x(x为弧度)的正切值- r! e9 q% \  c+ X
  2. tan(x)
    ) ^1 {# k* P! g6 v: d, |3 _1 `
  3. Return the tangent of x (measured in radians).
    ! u' t1 c# `, z
  4. >>> math.tan(math.pi/4)
      k6 P2 ?2 T6 h* b
  5. 0.9999999999999999
    2 D% R0 b. J* T) X
  6. >>> math.tan(math.pi/6)
    % m1 q' S& ^3 c. t3 x8 z; t
  7. 0.5773502691896257' g8 I# a3 F' `, i. h- N; `
  8. >>> math.tan(math.pi/3), c# d& x$ U+ @- \2 i( V
  9. 1.7320508075688767
复制代码

# V1 S' u9 M' f7 t' cmath.degrees(x)  把x从弧度转换成角度# i( R1 o+ K3 M/ a
  1. #把x从弧度转换成角度
    * P. c5 ^2 c1 u" S/ @! z
  2. degrees(x)7 @3 P! z5 |% l$ ?
  3. Convert angle x from radians to degrees.
    : }- R) f; h5 S
  4. 8 D5 C$ z# c5 W  ^) J
  5. >>> math.degrees(math.pi/4)' R; Y0 u% b4 g& c
  6. 45.0
    + V' L$ H) A% O( ~
  7. >>> math.degrees(math.pi)- q; D: d! G9 ^
  8. 180.0* M- w+ j- x/ \2 z0 \
  9. >>> math.degrees(math.pi/6)2 P/ W# z5 ?, f8 |
  10. 29.999999999999996! J. m$ o9 q9 u; J' s3 ~/ f" J  `" b
  11. >>> math.degrees(math.pi/3)4 Z5 [9 ?' T. j1 V, O
  12. 59.99999999999999
复制代码

/ V! M- m* k: h9 e! Jmath.radians(x)  把角度x转换成弧度
3 b( v" Z: X8 q- ]% b. K
  1. #把角度x转换成弧度7 ~9 G: s7 X% N# o- ], S
  2. radians(x); M$ \# L: t! }4 H
  3. Convert angle x from degrees to radians.
    % S! F" c/ b4 ^: x; }4 I% I
  4. >>> math.radians(45)
    7 R. b7 G4 ~1 l. [( N
  5. 0.7853981633974483) ?' W0 I; U( a4 t
  6. >>> math.radians(60)
    ; A) m( I# r/ X% p1 |( K7 T& Z
  7. 1.0471975511965976
复制代码

) D" T# Z" g9 y( p$ @! o! Cmath.copysign(x,y)  把y的正负号加到x前面,可以使用0/ ]: b% C( }* M
  1. #把y的正负号加到x前面,可以使用0
    # S0 I0 |+ A& q, Q2 ]5 c
  2. copysign(x, y)
    $ x% J" z8 C5 K+ R9 h
  3. Return a float with the magnitude (absolute value) of x but the sign ; J( ~# v9 z7 e$ V
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ! u, F$ o# b  y( y; O& H6 s# s. ]$ E
  5. returns -1.0.
    1 U4 N1 [" T( H
  6. 4 Z; v/ R2 ^5 z- K
  7. >>> math.copysign(2,3)9 _. ~" f) Q& H
  8. 2.0
    $ `, P- e" L9 }# ^$ M4 i) p! t: Z
  9. >>> math.copysign(2,-3)
    4 L* X0 w" y% A2 d" Y9 l2 u9 j( ?
  10. -2.06 O+ X4 N" |; ^
  11. >>> math.copysign(3,8)
    8 i0 w+ I) A! m+ m4 b7 U  Q
  12. 3.0: c9 i; K& i3 X1 @
  13. >>> math.copysign(3,-8)
    5 N  f1 [# A; M& F- H7 X
  14. -3.0
复制代码
% h7 [5 \7 H! z: Y9 {
math.exp(x)  返回math.e,也就是2.71828的x次方
+ |) g' K4 y9 l1 J; T; i
  1. #返回math.e,也就是2.71828的x次方
    - g$ g. a, I' A4 G: L! o' \1 c2 d
  2. exp(x)
    % K  \8 Z) J# }2 g5 E
  3. Return e raised to the power of x.; K  j% b* \8 O/ x0 g% E) i
  4. ) B7 y  {: [; k
  5. >>> math.exp(1)
    3 r2 {9 k6 E- ?- i
  6. 2.718281828459045
    8 Z: `) c% g% [# T0 P8 w
  7. >>> math.exp(2)- e. }! m1 |- T6 |
  8. 7.389056098930656 d4 W0 ]1 k8 S9 w) L6 x4 t; H
  9. >>> math.exp(3)5 h3 y$ m; t0 B# P! A  V3 U9 w
  10. 20.085536923187668
复制代码

1 M5 i3 v4 b6 q6 Y: o1 X' Gmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1: F+ @. `9 {* J6 a! a
  1. #返回math.e的x(其值为2.71828)次方的值减14 y5 t0 h! _  [0 e9 r
  2. expm1(x). O2 b3 ~) t% ?( {
  3. Return exp(x)-1.
    $ e0 }* K/ F# ]8 g
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.$ a' E1 P  R0 i. d) T+ r: y" c

  5. , Q& B2 u1 v" s, ^: T% h  n
  6. >>> math.expm1(1)* w- L* m# N% ~$ `  A8 |. ]' j
  7. 1.718281828459045) D1 `( z, ?8 n' M. }
  8. >>> math.expm1(2)8 U7 q3 l. d+ ~! i2 o
  9. 6.38905609893065
    6 C5 l! {* }/ |) J
  10. >>> math.expm1(3)# _" x! m& E: }8 k/ f7 a
  11. 19.085536923187668
复制代码
. N# d+ |. o. ?0 x
math.fabs(x)  返回x的绝对值: d/ }1 j: P+ Z2 K8 T  X& \
  1. #返回x的绝对值! x  E' [1 J0 a4 m! u
  2. fabs(x)
    - |. U) Y. s" ]1 z: A1 i! B
  3. Return the absolute value of the float x.+ b5 [9 H8 f+ f) l. T* f) O: k

  4. 4 c, B) i% B( g( g: X8 j( t& s
  5. >>> math.fabs(-0.003)/ a% _* b# r: L# }4 ?, O4 q
  6. 0.003, f0 N; `1 f2 {8 M8 V7 y! |
  7. >>> math.fabs(-110)
    9 v( h% x1 ~- F
  8. 110.0" w" R2 K1 p0 F/ Q4 W# L8 k  U4 o
  9. >>> math.fabs(100)
    9 y& G9 b, s) ]
  10. 100.0
复制代码

+ R& x  ~9 d5 s1 d; bmath.factorial(x)  取x的阶乘的值5 w8 @" v5 r& m( |" Q  i- h
  1. #取x的阶乘的值2 t8 v  A# |1 k6 b; M4 {
  2. factorial(x) -> Integral
    ! v& v" H4 Z2 u
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    : m3 e1 _: [/ l3 ^# i# E1 B
  4. >>> math.factorial(1)+ u0 ~. m+ p) i  e4 M6 Q
  5. 1
    : }* {; Q; F* O
  6. >>> math.factorial(2)6 S" ?9 M5 _: O+ \8 N7 x
  7. 27 n: L- a0 G* B: G3 U
  8. >>> math.factorial(3)
    ) G* M2 G* w) b
  9. 6
    - J. y* F$ Y2 Z- f  L; p
  10. >>> math.factorial(5)
    2 V$ W5 ?8 D/ c& x: t
  11. 120
    0 I4 G$ m, M) A$ |, u
  12. >>> math.factorial(10)
    : i0 w8 D$ K8 ?: E' N
  13. 3628800
复制代码
, ~9 _9 G7 M) k' y) w: l
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
( K( U. b5 E  ~1 A' {
  1. #得到x/y的余数,其值是一个浮点数8 E' t$ l. `& w! x3 K  M
  2. fmod(x, y)
    7 _8 H+ z, E; P, N: \
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    & g+ c" j; Z" z3 c$ H% {1 X
  4. >>> math.fmod(20,3)
    6 M. d  B4 |6 h4 I3 x
  5. 2.0; z: G; a( J$ d& H) ^
  6. >>> math.fmod(20,7)5 ~6 J- m: ]- S1 y- `3 `
  7. 6.0
复制代码

0 K5 i' r& u, _6 P! a$ Umath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围, f* w1 r$ y4 @* D4 U
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    9 G% @0 R# r  x$ F( z
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ; h! Z, U  f  f# ~1 B1 R& i, E
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" [& p6 T9 }) M& H
  4. frexp(x)
    & m2 a$ F* O' m
  5. Return the mantissa and exponent of x, as pair (m, e).! \9 M) s$ E. V6 L
  6. m is a float and e is an int, such that x = m * 2.**e.6 w+ U) x: ~; M/ G! G
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.2 J* H% |/ P& h* p( B8 B; c
  8. >>> math.frexp(10)
    ) A' P  y2 h8 {; l2 b: S; w
  9. (0.625, 4)2 v/ _6 C4 z) x& ]
  10. >>> math.frexp(75)
    + S4 y, z/ p  ]7 ^* Y5 o" u
  11. (0.5859375, 7)# h) F- ]3 D, l- P* `" U
  12. >>> math.frexp(-40)
    0 U% Y9 _  d1 N8 f" Y7 R
  13. (-0.625, 6)
    * \& J5 w% |1 L/ s! ?! |* r
  14. >>> math.frexp(-100)
    & ~& S" F8 i' C+ k+ u: \
  15. (-0.78125, 7)
    7 {1 C1 t. S% _/ t- V
  16. >>> math.frexp(100)) t  ^% T: j+ s4 J; b* ^* L
  17. (0.78125, 7)
复制代码

: J9 m/ `0 D8 r3 w3 Z9 vmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列  z$ L4 K7 F  F$ y5 F
  1. #对迭代器里的每个元素进行求和操作
    % w+ a3 Y: @' p$ V7 \  D& M, @1 ~
  2. fsum(iterable)
    - K  L" ^" S+ Q' |9 V- u
  3. Return an accurate floating point sum of values in the iterable.4 H. t$ j* @, o8 a
  4. Assumes IEEE-754 floating point arithmetic.) H# q% u# Y, t' J( I
  5. >>> math.fsum([1,2,3,4])
    " I: J* q; I+ {, i/ s# M: s5 X
  6. 10.0
    ) w2 g9 ?7 r3 h3 I' O
  7. >>> math.fsum((1,2,3,4))
    5 b3 b& f7 }' _( f
  8. 10.0; Z" ^: t, f6 k5 |. I) k! Q+ {
  9. >>> math.fsum((-1,-2,-3,-4))
    , `3 U( G" O0 W  i2 U
  10. -10.0
    % z  p0 z' J1 ^7 g1 B  O2 C  a
  11. >>> math.fsum([-1,-2,-3,-4])  `! f6 O% b9 i5 G$ m, Q. [, N2 |
  12. -10.0
复制代码

" M% F" i7 [) T$ O: X1 I8 R2 K- }math.gcd(x,y)  返回x和y的最大公约数! V0 X) v4 C$ I0 q& ]
  1. #返回x和y的最大公约数
    1 t, ~8 x9 B2 w- Q% A$ ^/ ~
  2. gcd(x, y) -> int6 ~; o# v( z' I: S4 \6 e
  3. greatest common divisor of x and y
    7 {- y+ _8 f0 d" d9 q
  4. >>> math.gcd(8,6)6 }% z7 \5 n/ i' P. }) a! \* B
  5. 2$ I; S& a3 Y9 z/ G: H
  6. >>> math.gcd(40,20)
      B- p7 [: _0 `( z& R
  7. 20
    . j. x+ y' Z. }& E" G; |  P- `
  8. >>> math.gcd(8,12)
    7 Z  J* n7 M* ]: k' S0 A$ j0 P
  9. 4
复制代码

; H. D6 O( i' f* q- B' Wmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* N( b( j6 x$ T2 o
  1. #得到(x**2+y**2),平方的值
    2 P8 U9 \, E2 g" N
  2. hypot(x, y)% I: E% s! N6 X4 }; c. k
  3. Return the Euclidean distance, sqrt(x*x + y*y).' p7 u6 Y/ p6 c5 E9 y
  4. >>> math.hypot(3,4)
    % T' g0 y" }; s
  5. 5.0* W6 G) M- d. r
  6. >>> math.hypot(6,8)1 @. {7 ^4 v" G# C" m
  7. 10.0
复制代码
7 b0 ~0 A  M% Q! Z9 _7 r! A
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False. H. d2 e" D/ E0 l; N: N! |
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ' f2 K2 V1 U7 k
  2. isfinite(x) -> bool
    / E7 L: U6 O4 S- A
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.' C4 v7 o1 s2 i
  4. >>> math.isfinite(100)+ ~- s( ?; m+ q6 B5 Q
  5. True5 k/ ]- X$ d5 U
  6. >>> math.isfinite(0)! l) M" T" t' U! k6 [
  7. True; Y& K/ L1 b2 E9 u, D9 z1 ~
  8. >>> math.isfinite(0.1)
    / \5 C4 r0 l4 l5 X# S
  9. True
    * I" }2 I7 g3 Y+ H$ P0 _
  10. >>> math.isfinite("a")
    ( L- L9 o7 t  r/ p
  11. >>> math.isfinite(0.0001)
    # C! t8 ?4 s3 m
  12. True
复制代码
. o# z) S$ C* l- O7 I. b  R
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
  S" P3 D+ O' J( Z* L
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ; S: S3 Q5 E! i- V
  2. isinf(x) -> bool
    + G4 J: b! x8 {1 m5 n/ k1 |. `
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ) ^" W0 M0 `' r- D
  4. >>> math.isinf(234)% Q$ Z* c3 ~9 h+ H: r
  5. False6 U5 U6 g3 {1 @# Z6 r
  6. >>> math.isinf(0.1)
    7 d- l, a4 P- `  |5 N( R
  7. False
复制代码

) ]5 N" ?' l- I- rmath.isnan(x)  如果x不是数字True,否则返回False/ m1 f) U) a6 s
  1. #如果x不是数字True,否则返回False
    9 O$ m# Y+ t: g) z* F
  2. isnan(x) -> bool- F2 G/ Q( I0 [4 B: o" u0 N
  3. Return True if x is a NaN (not a number), and False otherwise.
    # H4 ^9 d" s9 [; t( ~
  4. >>> math.isnan(23)
    6 n8 F) l1 i6 k: W3 P9 y
  5. False
    2 n  o% a9 l; A" W$ P( B" E! B) f
  6. >>> math.isnan(0.01)
    ( t5 R* T1 E7 l8 q( a/ j' q4 D
  7. False
复制代码

0 C) g* u* y, C( ]: \math.ldexp(x,i)  返回x*(2**i)的值8 F% h, B: A- t
  1. #返回x*(2**i)的值
    3 C# d6 B2 x/ Y$ m
  2. ldexp(x, i)
    : G! G+ Y  i3 `" V. E
  3. Return x * (2**i).6 }/ R1 x& w" H( Y
  4. >>> math.ldexp(5,5)  I3 [# `9 ?: q: f7 }
  5. 160.0/ _: y" B$ M+ r) u' q; J/ e
  6. >>> math.ldexp(3,5)
    " f6 h6 r6 y: w( E8 s
  7. 96.0
复制代码
3 F5 L+ L" p- M# r, ^
math.log10(x)  返回x的以10为底的对数
4 {5 S! g& X% N3 A
  1. #返回x的以10为底的对数) o* a+ n' [. T. `0 V
  2. log10(x)
    1 F- D4 B; ?! e$ T9 c& ^
  3. Return the base 10 logarithm of x.
    ; S, X3 n# b+ [- C* ^& J! [
  4. >>> math.log10(10)
    : T# H! z$ T" E7 B  u4 L
  5. 1.0
    7 r0 p1 ]& Q* I1 y/ l# i8 s. W3 Y! c
  6. >>> math.log10(100)( I* Y8 e. S  o. d4 n- |* d$ D
  7. 2.0; P, B$ q9 ]9 P1 D: n5 Z! m# G
  8. #即10的1.3次方的结果为20  N) q- j- u2 `- v# r
  9. >>> math.log10(20)
    : s& R" Y6 w! v/ _1 J% p
  10. 1.3010299956639813
复制代码
) {5 _) o4 Y. g" I/ r( E
math.log1p(x)  返回x+1的自然对数(基数为e)的值
1 @* H) a5 h9 d2 M" i/ W
  1. #返回x+1的自然对数(基数为e)的值3 N9 @0 l0 w) `- ]) N5 d
  2. log1p(x)
    : A7 Q! p0 M6 s
  3. Return the natural logarithm of 1+x (base e).% Q5 ?* Q* ^; A* X
  4. The result is computed in a way which is accurate for x near zero.
    - B1 ~. O! [% {+ f, N* x5 f3 `7 o6 E
  5. >>> math.log(10)
    # U5 @8 T* |# n$ |- D" }, Z
  6. 2.302585092994046
    ( F( Y& ^' E. t: s7 k; v
  7. >>> math.log1p(10)0 i8 }& R3 Q3 W: T+ r, x
  8. 2.39789527279837079 E; D; }9 p) p8 g
  9. >>> math.log(11)/ ~4 x, L0 w" @; r
  10. 2.3978952727983707
复制代码

2 T9 y" j1 S- o9 j6 |% b' ?! Imath.log2(x)  返回x的基2对数
. ^9 k( k* V4 E! t7 h8 D0 |. s1 U
  1. #返回x的基2对数
    9 l' ^* Y& \+ e4 o7 l
  2. log2(x)
    , R, l% ?# _3 {6 M3 i
  3. Return the base 2 logarithm of x.
    $ H* M5 v# M( }1 j; C7 \. c7 v# P
  4. >>> math.log2(32)
    . V: g& L5 z% d: v9 K$ i( O. i( R
  5. 5.0
    # u. ^* \4 k3 b  t" O1 S
  6. >>> math.log2(20)8 L5 N( V) w: @9 {- i3 M, @+ v. k1 j
  7. 4.321928094887363. S. n9 f- K- }! ]8 z7 ~( L
  8. >>> math.log2(16)/ Q6 ?, h% O9 l9 M, x
  9. 4.0
复制代码

0 i- g+ ?, \3 A' v* E( K8 \3 ]math.modf(x)  返回由x的小数部分和整数部分组成的元组
2 T7 D4 J7 Y$ W
  1. #返回由x的小数部分和整数部分组成的元组8 _! n$ m* N4 _( ]& d
  2. modf(x)
    - f) l4 T8 S0 S  ]5 e5 T
  3. Return the fractional and integer parts of x.  Both results carry the sign! T. x$ u  ]& \! R) o" K) x
  4. of x and are floats.
    ' h7 t4 l5 i: m( t6 ^5 @% ~
  5. >>> math.modf(math.pi)5 K& Y" L3 K8 ~% }7 ?& Q" B
  6. (0.14159265358979312, 3.0)2 V9 ~7 \5 }" [$ U5 E, K8 y
  7. >>> math.modf(12.34), z" s/ B$ i9 G! P/ I
  8. (0.33999999999999986, 12.0)
复制代码

+ F9 p' ^5 n+ P1 t# L* |math.sqrt(x)  求x的平方根
" W+ E! W5 C6 u
  1. #求x的平方根
    5 k; C0 T; Y+ {1 F3 t. C  g" A
  2. sqrt(x)7 l* O& e3 j3 x" E$ `
  3. Return the square root of x.
    / p0 |) y/ q( R* {, S
  4. >>> math.sqrt(100)3 _# p. g! ~% E9 @8 ^
  5. 10.0
    4 P7 d4 r' p* [: W- r
  6. >>> math.sqrt(16)
    & t) H: @# B/ i; ~0 U3 n
  7. 4.0/ \% [7 @  C. y
  8. >>> math.sqrt(20)
    6 I* h- ^4 s( P3 n3 B4 w7 |
  9. 4.47213595499958
复制代码

7 [: J, x: X$ Q8 D. J, Imath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    4 G! c- h2 J, f" d( V$ ~1 C
  2. trunc(x:Real) -> Integral
    * x9 f5 r9 A  j. Q9 R
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! B! i1 p/ e# B$ r4 Q
  4. >>> math.trunc(6.789)1 T0 Q& |0 j, _  ?' g
  5. 6
    0 F( Q; v+ C- \0 O
  6. >>> math.trunc(math.pi)
    ( L! u3 p9 I) G0 {$ |" {2 d
  7. 3
    ( l" V/ O+ ^' D; i7 d# m+ m6 P
  8. >>> math.trunc(2.567)0 k( Z3 f, r2 z* `, i7 t4 v3 w
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2025-12-5 00:17 , Processed in 0.093962 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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