新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

( k9 U  n' T" d9 p3 F【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 Z- H4 F. g. v0 m
5 e; W/ W/ r5 T& x
方法1
6 x% W, C, s' R: }% b
  1. >>> import math* [& J9 {6 U+ @0 s; n2 u
  2. >>> math.sqrt(9)0 Q3 r6 n7 P6 ~/ A
  3. 3.0
复制代码
方法26 k5 i) |2 q, S/ i
  1. >>> from math import sqrt8 Y4 P2 H* J4 G; c& c5 ~$ ?
  2. >>> sqrt(9)* u0 W/ A; r( r
  3. 3.0
复制代码
0 F/ U0 S* N- Y2 x6 V9 W

/ u  X/ |4 n- G6 |- Y; k- }
math.e  表示一个常量3 B, R' _" g  d& [5 [$ U$ b  k! ]; h" q4 f
  1. #表示一个常量
    ) y) t: p9 I8 p: Q* C
  2. >>> math.e) m6 z6 p! O' Y1 x
  3. 2.718281828459045
复制代码
  _" R" B7 J3 S6 i) V2 N. Y& c& n0 w
math.pi  
数字常量,圆周率

! m% o0 Z  G# V- W: }* g; U, ]8 U
  1. #数字常量,圆周率% R9 ]2 ]- Q; q8 Z, b( a" ]0 ]1 D
  2. >>> print(math.pi)/ h- v+ N1 I! [2 m4 K
  3. 3.141592653589793
复制代码

8 J& |$ K7 @- f: rmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

6 F' q4 A. `- s$ Y& N& b8 q
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x* b* r7 ]! O) u8 O
  2. ceil(x)
    0 q: t) I' X$ ~; P- T$ i
  3. Return the ceiling of x as an int.
    9 D% A1 c( _/ X$ r& w) W
  4. This is the smallest integral value >= x.
    # a0 C0 Z% C( C2 V  t

  5. 4 v# d0 a9 K0 B/ l, B
  6. >>> math.ceil(4.01)
    2 s& v& r0 D+ J  S
  7. 54 k0 T* v+ [( r; \! ~6 C9 d
  8. >>> math.ceil(4.99)9 y/ J' ~1 d. n
  9. 5* A$ p! v' \7 e. U/ r7 E  }
  10. >>> math.ceil(-3.99)9 `* {& h: Y6 L4 i6 n
  11. -3
    ) X# j& F9 K7 O9 y8 ]# |
  12. >>> math.ceil(-3.01); O3 _% M7 b! d% ]: V# z; B
  13. -3
复制代码
. Y, G9 T; b. q) [3 N+ g& ?! J
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 u* A8 u" R* s+ w
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身% I* j' n/ o- j6 e4 b; z/ o
  2. floor(x)
    & z5 g1 i  o2 ]6 M$ ]/ O. e
  3. Return the floor of x as an int.! W- B. d/ D5 e7 }) B
  4. This is the largest integral value <= x.
    , ^; W% ?' R6 f
  5. >>> math.floor(4.1)
    $ \2 D! i: Q1 D: w
  6. 4
    , i! O' S: w: F! D# ]* Q; ^; x
  7. >>> math.floor(4.999)! V3 _( |3 j( \+ N- ?" U% L
  8. 4! o- s+ i# f* C- l  @) h
  9. >>> math.floor(-4.999)0 c7 ~7 v9 O& O  ?% ~# _; B' W
  10. -56 r! s5 h, g, ~4 X/ F6 E# d% R
  11. >>> math.floor(-4.01)0 n( r$ _, n% ?! B) F8 r
  12. -5
复制代码
. w" @% O& j" A6 S! S
math.pow(x,y)  返回x的y次方,即x**y$ ?$ R+ k. H; U5 L' F
  1. #返回x的y次方,即x**y" B7 G( ^- b8 P# ]
  2. pow(x, y)* t( S9 h2 B, ~( D/ Q
  3. Return x**y (x to the power of y).1 ]- b, B, M; w2 R: [
  4. >>> math.pow(3,4)2 p3 U, {/ R2 [( o: S5 s
  5. 81.09 x6 h  r3 ^' G' O; P9 Y: \
  6. >>> ' i1 J2 W( ]4 Y* r
  7. >>> math.pow(2,7)0 j4 U5 h/ j$ t
  8. 128.0
复制代码
  G4 t1 L$ l+ T( Y
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base): v4 g, v' u6 P8 e* x6 F+ _* w( r
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)  o1 z+ t8 S) z6 H7 }
  2. log(x[, base])
    7 C6 G1 g& l; Z/ q
  3. Return the logarithm of x to the given base.
    ) u6 n0 l4 \* n* u, w/ P* m) ]+ f* _
  4. If the base not specified, returns the natural logarithm (base e) of x.; @! ]6 a, k6 z9 k( p
  5. >>> math.log(10)
    3 H- [$ V3 y# K' w% K' {
  6. 2.302585092994046
    . d8 }: Z  @; N1 X! P  ^# l
  7. >>> math.log(11)  X( g: n4 H5 x2 J* r
  8. 2.39789527279837074 R* b- E5 T9 e/ A& z
  9. >>> math.log(20)
    ) z7 Q! ^. L7 K0 e# f
  10. 2.995732273553991
复制代码
- J. a2 B( @$ E0 _: r) m
math.sin(x)  求x(x为弧度)的正弦值: C4 [  G9 d1 Z+ G& S3 J1 D
  1. #求x(x为弧度)的正弦值
    & P0 I  @" u) v, m; {9 V
  2. sin(x)
    : ?/ y% k7 f: w- y
  3. Return the sine of x (measured in radians)./ i* k& j$ z- l9 V
  4. >>> math.sin(math.pi/4); i3 H7 Q1 I: Z& x9 a) V" M
  5. 0.70710678118654755 {$ R% p- L9 |, b
  6. >>> math.sin(math.pi/2)
    5 `. ^! \% v2 c% `/ N
  7. 1.0/ Z; N$ q0 F* q; [
  8. >>> math.sin(math.pi/3)
    ; I& m1 ]: T  Y3 Y# H" v/ {
  9. 0.8660254037844386
复制代码
9 g, _4 J+ y- X! g/ w2 _% O
math.cos(x)  求x的余弦,x必须是弧度. T) s) D- Z( j1 O
  1. #求x的余弦,x必须是弧度
    1 C! v" [6 g: A" q
  2. cos(x)" ]! O7 A( S7 o7 Z
  3. Return the cosine of x (measured in radians).7 @& t# w5 u0 b
  4. #math.pi/4表示弧度,转换成角度为45度
    ! k. u. Q( I  e
  5. >>> math.cos(math.pi/4)
    - D" O: M0 j7 F7 x4 g7 f: E8 X& l
  6. 0.7071067811865476
    * p* L3 [- j2 T' D' A% w
  7. math.pi/3表示弧度,转换成角度为60度
    5 a* K4 W2 A5 U& K
  8. >>> math.cos(math.pi/3)7 }5 x' X( J' e  }; _, [$ d
  9. 0.5000000000000001% N; w/ U2 ]; ]6 m; P! Q5 B* X5 r
  10. math.pi/6表示弧度,转换成角度为30度
    * K1 ?( m; p& Z* a
  11. >>> math.cos(math.pi/6)8 @9 k# `4 w- @: a+ t# y
  12. 0.8660254037844387
复制代码

3 c, S, ^% k4 N/ i9 g1 y4 K8 Xmath.tan(x)  返回x(x为弧度)的正切值
: y1 h( w+ f  h
  1. #返回x(x为弧度)的正切值% G- V2 k- a# `3 C! U- Y) r2 \8 V
  2. tan(x)
    ! ]: l! |. U0 u0 N
  3. Return the tangent of x (measured in radians).2 [  a: r* B* e; }2 t
  4. >>> math.tan(math.pi/4), t5 l" f' d) R* x+ N: l
  5. 0.9999999999999999+ l& e) H: r3 a7 m" N3 c7 V, R  u
  6. >>> math.tan(math.pi/6)3 f$ P0 S  \& k6 [, _, T9 `! j! e
  7. 0.57735026918962577 n% E% C1 u2 Y& T
  8. >>> math.tan(math.pi/3)
    & I6 C0 ?0 M/ S1 r6 q# g4 _
  9. 1.7320508075688767
复制代码

# n; H& Y' t' fmath.degrees(x)  把x从弧度转换成角度
' x# m) o! |- c, K
  1. #把x从弧度转换成角度
    . O/ j8 \" K3 I  Q2 |+ Q
  2. degrees(x). d# J0 J3 J- T, H' E7 u
  3. Convert angle x from radians to degrees.9 f$ S8 ]- [4 C% [* H" _$ F
  4. 4 X& a1 y( l# }1 x. M
  5. >>> math.degrees(math.pi/4)- j" _8 j$ u& {0 `' q2 W% Y( d0 ^- C
  6. 45.0
    : c- p7 E: |2 k. h
  7. >>> math.degrees(math.pi)
    0 M0 ?9 D2 _5 |0 |
  8. 180.02 C4 M! }1 _+ @' k8 B7 Q7 N
  9. >>> math.degrees(math.pi/6); j" a! J1 p) V
  10. 29.999999999999996
    ; L# M( S$ Q" ?$ n) a
  11. >>> math.degrees(math.pi/3)- W& ^. Z6 e' k  e! ~
  12. 59.99999999999999
复制代码

, P1 a! V9 L. _% s: }math.radians(x)  把角度x转换成弧度2 `9 b& f1 W* L" Z4 @
  1. #把角度x转换成弧度+ ^& T3 N* X5 l
  2. radians(x)5 d$ S2 W+ M' Z. W: T; y1 ]8 n
  3. Convert angle x from degrees to radians.
    1 s" p! e7 H! C4 [
  4. >>> math.radians(45)
    ! |- ]9 M/ ~; g/ f" h  l
  5. 0.7853981633974483
    7 }8 O- u# Q6 ~8 z! d& y
  6. >>> math.radians(60)
    + V3 _! Z" F3 [" \* A- ]$ H
  7. 1.0471975511965976
复制代码

- u+ y6 d! e" Y& _0 O# l) @! n$ m$ pmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
" G* E- E% ~! X& v% ]7 w4 \
  1. #把y的正负号加到x前面,可以使用0
    * h+ A7 A9 W1 N. N
  2. copysign(x, y)/ {7 ~5 |$ E; `
  3. Return a float with the magnitude (absolute value) of x but the sign & F+ x  n& {/ J$ e
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    1 `6 _7 n! S: U
  5. returns -1.0.
    $ G* A0 {. M' p* Q' j

  6. & i& _5 |' I# m0 J$ M
  7. >>> math.copysign(2,3)- c* e- I2 l! V) J+ _5 D
  8. 2.0
    . S! g4 R2 }0 @9 H$ s+ o
  9. >>> math.copysign(2,-3)
    . x- b7 J) v: O8 X( ?
  10. -2.0" C+ T# i  Y5 g! Y' z0 q& \: a
  11. >>> math.copysign(3,8): F+ F7 `0 Z( d5 l
  12. 3.0" _3 c6 ?, Z! W8 i; H
  13. >>> math.copysign(3,-8)" Y' U' J4 ~, v. L" ]
  14. -3.0
复制代码

$ J% |- M; Q* n' `, ?* O  Mmath.exp(x)  返回math.e,也就是2.71828的x次方, w  X0 P; S* U9 n
  1. #返回math.e,也就是2.71828的x次方9 i) c: I, ^- _1 ?
  2. exp(x)
    $ k" O2 o$ z- s3 z  M2 {& ^
  3. Return e raised to the power of x.! y0 f4 u% }  i( M

  4. ( i) Z% N) ?; j, @% q- @  {5 \# H
  5. >>> math.exp(1)
    6 X/ Y% U& X5 H" V/ K5 `
  6. 2.718281828459045  [. r/ ]8 u4 Z5 `: N
  7. >>> math.exp(2)- @% W0 R, K$ a$ e- C
  8. 7.38905609893065
    6 K, m$ x. y. l
  9. >>> math.exp(3)
    & J* e4 i. k$ O
  10. 20.085536923187668
复制代码

  N/ o7 |! Q8 D+ Bmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
3 X# U, H" ?& z1 K
  1. #返回math.e的x(其值为2.71828)次方的值减19 g) |: h8 g& C" l) y' K
  2. expm1(x)
    , F( d' P4 q) J/ x
  3. Return exp(x)-1.
    ) k# l2 i* p) f2 Z$ x4 M3 C2 J  ?# l
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    # \- j( c6 C$ C* G5 m

  5. * O8 D. u% p6 m$ J; [2 i
  6. >>> math.expm1(1)+ T! \4 T& x( U6 M1 m& A2 y0 p
  7. 1.7182818284590459 W, a5 Y! t# V/ x
  8. >>> math.expm1(2)" g, \5 E/ m' u/ w/ H1 F
  9. 6.38905609893065
    & ^  N* l: \# x- Q
  10. >>> math.expm1(3)! H& x# T5 `: o5 }( f; K( g
  11. 19.085536923187668
复制代码

( ]2 _! h9 Z- L# l, A) V! |math.fabs(x)  返回x的绝对值
9 Q6 p1 @3 W, g4 U, ^/ B$ @
  1. #返回x的绝对值
    " f1 q  r* Y. U# F! ?4 F
  2. fabs(x)0 P$ a1 K* \4 O, z' g0 z) {, x
  3. Return the absolute value of the float x.
    ' m- v, `3 f; M1 H, v% H
  4.   K0 x0 u7 P9 w
  5. >>> math.fabs(-0.003)
    4 V2 ^  D1 ]  b9 W5 S0 d& Y
  6. 0.003  i1 E% {' N$ i7 p. K; N" H
  7. >>> math.fabs(-110)8 j+ t9 e, L) X
  8. 110.0
    - i& a1 r7 e! h( D  Y0 x
  9. >>> math.fabs(100)
    . ^1 {  o) u; v$ Y! [5 w
  10. 100.0
复制代码

  N( |1 l& s9 a( d, g# Umath.factorial(x)  取x的阶乘的值: d$ ^+ A# B& K
  1. #取x的阶乘的值2 W9 \  m  u  l) A# U
  2. factorial(x) -> Integral
    ( Y5 e! p, ]0 z& B5 B/ K/ N
  3. Find x!. Raise a ValueError if x is negative or non-integral.# n3 K5 H# g, W! Z6 a% F- M
  4. >>> math.factorial(1)' u( C% V1 K# K2 S1 q$ v
  5. 1* r, g4 s1 A/ p; l9 g  X
  6. >>> math.factorial(2)
    / A% o8 j+ T5 B# x
  7. 2
    6 ]2 d+ p/ {1 ]$ ^& x
  8. >>> math.factorial(3)
    . ^- U3 C( S% ]$ t! e
  9. 67 f4 L7 Q4 f$ x6 F( h
  10. >>> math.factorial(5)" H+ x  F1 `) Y* F7 x
  11. 120
    * L- Y6 t( F  i
  12. >>> math.factorial(10)
    ! M( t7 e1 ]! P- O
  13. 3628800
复制代码
* i: Y0 X% @( N- E% i6 [9 z
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
2 d# ]/ C; X6 a. Z
  1. #得到x/y的余数,其值是一个浮点数& L; ~3 f" Y$ ~  y+ v- H
  2. fmod(x, y)
    , |( T7 O3 |4 X
  3. Return fmod(x, y), according to platform C.  x % y may differ.$ L' L( W9 o. l- M3 P
  4. >>> math.fmod(20,3)
    # `3 P9 a: L+ M# D2 R2 T
  5. 2.0% [) n. U" c/ I" W3 a
  6. >>> math.fmod(20,7)
    * S3 X1 q# s: T6 _1 N
  7. 6.0
复制代码

/ g, n2 w+ s. g7 z$ U- emath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& `! ^& _6 m  R# e3 U/ q0 K
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ( i. G, e0 g; C2 _% B+ w- X1 ?0 M1 T
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    " n/ U4 H9 f2 n9 F
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1( `5 f7 r+ ~! S/ R& l( r! }$ q
  4. frexp(x)* \' M( Z2 ?* R1 M& q
  5. Return the mantissa and exponent of x, as pair (m, e).
    " o& v' q8 U6 y3 _
  6. m is a float and e is an int, such that x = m * 2.**e./ C  R8 p( e1 _, [* s
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    ' M0 }  ^5 v; @' D
  8. >>> math.frexp(10)! h4 H7 c* L; L  a8 J
  9. (0.625, 4)
    / ]# V  o7 ^7 ?( k
  10. >>> math.frexp(75)9 ^9 X$ B& H3 J) @+ S+ u) [
  11. (0.5859375, 7)
    % O9 m4 W' Z" ~3 C# e
  12. >>> math.frexp(-40)
    : ^( [6 J1 N8 |, w' p) x
  13. (-0.625, 6)
    , Q: z# S* r( C2 i! L
  14. >>> math.frexp(-100)* Y+ |) F, P5 V, Y2 j( z
  15. (-0.78125, 7)( {: s& V! Z  Q- \& ]) P6 q. R
  16. >>> math.frexp(100)
    " T9 z7 n0 K2 x- ~3 o- E! v2 w
  17. (0.78125, 7)
复制代码

. u5 P1 C* m4 A2 {math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
: S3 t5 x" J  M( a  u
  1. #对迭代器里的每个元素进行求和操作! N: M2 l& B4 @9 _0 x
  2. fsum(iterable)
    2 d- x* D' ^8 Z( v; Y/ a+ ]
  3. Return an accurate floating point sum of values in the iterable.& J) C2 ~  x' ^6 n1 v6 y) |+ d( E
  4. Assumes IEEE-754 floating point arithmetic.7 f1 t$ Q; v5 G' q
  5. >>> math.fsum([1,2,3,4])
    + E) s  g$ V% T' j! E" `, M
  6. 10.08 Y  X/ i# ]9 f% O
  7. >>> math.fsum((1,2,3,4))7 h) `$ D! z: V) H
  8. 10.0! x+ R4 X; b7 z4 w
  9. >>> math.fsum((-1,-2,-3,-4))
    4 F( D* k1 e2 M9 t  o4 _0 u, Z, {9 G
  10. -10.0& ]( @5 a% {% {) M! b5 [0 T
  11. >>> math.fsum([-1,-2,-3,-4])
    + S" I( J& w: z% s( A2 y
  12. -10.0
复制代码
4 m' ]6 f- f) R: k5 `
math.gcd(x,y)  返回x和y的最大公约数
7 ~# @4 |& J& p; ]
  1. #返回x和y的最大公约数% a, d( p/ d9 `3 u3 X, `  j
  2. gcd(x, y) -> int
    " A; o- b' S2 f" o2 m+ w5 f1 |
  3. greatest common divisor of x and y" X3 T  v! R1 v
  4. >>> math.gcd(8,6)5 _( v# d) W  D. ~) ?' K) d
  5. 2
    . w) X+ n/ L, x- H& Y
  6. >>> math.gcd(40,20)
    5 h- R: C1 w- r9 S# A' j8 u
  7. 201 n5 H) Y1 l/ h
  8. >>> math.gcd(8,12), `, H3 p! n% g+ E" h' R) s: T
  9. 4
复制代码
) I1 F) _) Y8 C* N4 E' I  ?! G/ |
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
* \4 O. `/ }: u- n, f* o$ y
  1. #得到(x**2+y**2),平方的值
    % P, w5 m2 w& i, G' F( Z0 f2 J
  2. hypot(x, y)0 X: i! ~5 ~/ d' ^$ I! X& E
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ) y. C) p& F2 v2 b+ S
  4. >>> math.hypot(3,4)% }+ P" o; q7 Y
  5. 5.0
    % f# ]; C5 N5 T. f
  6. >>> math.hypot(6,8). P( k8 H/ P, `, S+ o( E3 G
  7. 10.0
复制代码
& y. L8 h, l- b0 n
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
6 _5 J8 Q- @1 @. l8 E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False, u' `/ `# `2 K. R- a
  2. isfinite(x) -> bool1 l& Y: x4 R# v0 f
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    2 w: [/ f6 Z; C$ H3 A- T) f1 m8 b
  4. >>> math.isfinite(100)
    ) |' R2 I, c# F5 W9 i, |
  5. True0 N' E2 C# W3 A; n# l- o
  6. >>> math.isfinite(0)$ e# [0 i" m$ }% _" N: V* v! F) J& P
  7. True
    ' ?- d' K+ M. M0 N# b3 c
  8. >>> math.isfinite(0.1)
    % u, `5 u5 L- m" O0 Q8 r. E
  9. True$ e4 z9 J4 ]" T2 @
  10. >>> math.isfinite("a")
    6 r9 E" U, s  R/ c9 E) k
  11. >>> math.isfinite(0.0001)
    3 {* Q; R5 v8 ?6 R' m% v$ a
  12. True
复制代码
% {3 n' S7 b, y) F4 ^" R
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False  {. x. H8 z8 i' n( b1 ~
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ! t) I% j  N% f; L
  2. isinf(x) -> bool
    ; Q2 }. O) }7 K5 y8 {
  3. Return True if x is a positive or negative infinity, and False otherwise.
    % z4 S. U& N' N$ k( ^( o$ i5 i
  4. >>> math.isinf(234)7 u/ O4 W1 N7 m2 [7 T, L
  5. False. k# q+ m* V8 i
  6. >>> math.isinf(0.1)
    # P  P0 ^# w/ F' B0 R
  7. False
复制代码
6 J, h) ^+ F0 o  B* E% x/ t
math.isnan(x)  如果x不是数字True,否则返回False3 q4 F/ L3 x: {
  1. #如果x不是数字True,否则返回False
    0 y, C/ I# Y5 m) C' D2 z
  2. isnan(x) -> bool! q0 }4 }( F0 r2 a$ s2 E
  3. Return True if x is a NaN (not a number), and False otherwise.
    ! T) y4 x2 v3 O1 u$ c
  4. >>> math.isnan(23)
    . p1 k8 V) q4 J: }! [
  5. False% M- \1 V, z; x/ x/ z) _+ b
  6. >>> math.isnan(0.01)5 C: m6 e4 J* E- `/ M
  7. False
复制代码
  _8 C/ S  |: [0 o
math.ldexp(x,i)  返回x*(2**i)的值8 A# n- u0 ?1 K5 w4 }
  1. #返回x*(2**i)的值  v' U# ~- i8 @4 q, n
  2. ldexp(x, i)% R5 S* x& G9 j( ?# A0 o
  3. Return x * (2**i).' \- Y% W: B7 W0 U9 F5 i
  4. >>> math.ldexp(5,5)
    0 K% f0 K/ _0 g4 H* D$ z5 f
  5. 160.04 W( C4 D/ y. T% ?
  6. >>> math.ldexp(3,5), F* a$ D" r6 h: t  ]
  7. 96.0
复制代码
( m1 S4 c/ {4 w. F- I
math.log10(x)  返回x的以10为底的对数
& u& @0 N: j5 k
  1. #返回x的以10为底的对数. ^5 u8 {4 ~" i% {: G
  2. log10(x): U! |" k: }/ S  y& c( b  @
  3. Return the base 10 logarithm of x.
    3 _* s6 f+ Y& w) \: h
  4. >>> math.log10(10)  d( }5 }" H$ W) W. S5 z. z
  5. 1.0
    9 I/ A- b1 f  Y' f; t
  6. >>> math.log10(100)
    6 r& g* Y( i- G7 U
  7. 2.09 X; p# B! W0 o+ J: M0 o7 V4 r
  8. #即10的1.3次方的结果为209 G& ]6 j- [5 G7 }0 j- |7 n9 _
  9. >>> math.log10(20)
    - i" R8 Z# n. P( b
  10. 1.3010299956639813
复制代码

% J' p. |; u' ]- M+ W! x5 R. x% n/ M* Bmath.log1p(x)  返回x+1的自然对数(基数为e)的值
- Y, U2 M* y+ U8 _' {6 T# p
  1. #返回x+1的自然对数(基数为e)的值
    ; P5 y  H! l- c( f) x
  2. log1p(x)
    ; O3 l' f" g0 ~
  3. Return the natural logarithm of 1+x (base e).2 B3 m8 `2 X/ \9 p; ^. Y0 W
  4. The result is computed in a way which is accurate for x near zero.
      |- S' S! p' l0 e  L
  5. >>> math.log(10)7 D/ J, }! m) G0 t# T4 V
  6. 2.3025850929940469 w2 ], }7 _' [/ T& y$ P
  7. >>> math.log1p(10)
    6 u8 D0 H3 k1 M4 F" r
  8. 2.3978952727983707
    / j1 i% b! h0 R% W0 `1 S2 M
  9. >>> math.log(11)" ~! m' g5 [" W$ h( F' f
  10. 2.3978952727983707
复制代码
7 y/ ?& [- ^/ O5 t4 \+ ]( E
math.log2(x)  返回x的基2对数0 J. s3 S; g7 N: t1 s% f$ q# l) |# k
  1. #返回x的基2对数
    : j/ S) F5 w. R& w) N! m+ T
  2. log2(x)# N, i; `: @  ^
  3. Return the base 2 logarithm of x.; u( x0 O) j+ t, q  h
  4. >>> math.log2(32)% X" }" a& @! s4 N
  5. 5.0
    . v- T+ q/ k$ E2 g
  6. >>> math.log2(20)4 |; Y4 w! o3 }4 u% i' Q
  7. 4.321928094887363! `; B9 p# S" q
  8. >>> math.log2(16)
    " j: a2 v6 h8 G$ I; k
  9. 4.0
复制代码
6 K- U! A& u1 v; k. _
math.modf(x)  返回由x的小数部分和整数部分组成的元组4 f. f& J9 S" l) A3 s* B; ~
  1. #返回由x的小数部分和整数部分组成的元组
    ! v' w& K, n/ @
  2. modf(x)6 r' p$ Q1 q5 u" @  W
  3. Return the fractional and integer parts of x.  Both results carry the sign
      Z7 y4 c5 q  Z- ?1 d6 J
  4. of x and are floats.: G) B6 ?7 b# Q. A2 I% g
  5. >>> math.modf(math.pi)
    & A- S# S7 C1 r5 v  E+ G7 \! t% E
  6. (0.14159265358979312, 3.0)
    9 y: c1 I5 P; T0 W; c$ A
  7. >>> math.modf(12.34)
    4 C2 q* A* X' m' l0 f- u: g
  8. (0.33999999999999986, 12.0)
复制代码
5 w) K/ H: k& d; B
math.sqrt(x)  求x的平方根
9 T+ w/ `! u% I) w( L
  1. #求x的平方根" p: _5 f" i# M' K! g, F
  2. sqrt(x)
    / g3 d4 k6 z! K' x% `
  3. Return the square root of x.
    3 [9 m8 Y5 @- ~# ], [# ~
  4. >>> math.sqrt(100)! s9 d) S2 L0 J& P
  5. 10.0. G% o8 r% u! d* S; c. G: `
  6. >>> math.sqrt(16)6 e' w/ |( X0 ~
  7. 4.0
    ; J& U3 @! j6 d) h9 @) |
  8. >>> math.sqrt(20)
    7 M3 W0 `; c; D% J. ]
  9. 4.47213595499958
复制代码

1 W% n) Q6 v. J( gmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    9 [- E4 i- l; S1 M! e) i/ D, u. K
  2. trunc(x:Real) -> Integral
    ' k8 b8 a- Q( x+ i
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method./ d! k# O1 [* `3 U" J' b, D
  4. >>> math.trunc(6.789)5 j% n7 }' s( {0 o$ c' t' ^7 k
  5. 6  F2 R* H- T* o# K5 P1 V
  6. >>> math.trunc(math.pi)6 a8 _0 ~  T* v, `
  7. 3
    3 I- _+ v. d1 r8 \/ J. ^
  8. >>> math.trunc(2.567)
    ( Q0 J1 V1 J& `3 g. W
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-2 09:04 , Processed in 0.095218 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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