新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

% S1 @' o1 i/ Z' ~. A【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
$ _/ h1 ^9 [+ B9 l3 M
  i- P( z* u# r; `, G
方法1
/ ?. f: m9 E. o. b
  1. >>> import math! [' `! k& p! W' G- D* `9 S1 W* g
  2. >>> math.sqrt(9)
    9 x. z: S! ]. S4 \7 l
  3. 3.0
复制代码
方法2
8 b+ P9 R5 s+ N8 C) B
  1. >>> from math import sqrt4 s2 ?; r% ]6 {& ^+ S4 v3 ~
  2. >>> sqrt(9)
    0 w! p2 Z  Y" F
  3. 3.0
复制代码
- I) G/ \5 N$ Z8 o7 l1 @


) @) Z, \  {$ @% L: \: n
math.e  表示一个常量
4 ^+ U! G; t  ~  r
  1. #表示一个常量$ }; }- Y5 w' |  E* n; M& j
  2. >>> math.e* W9 f! `" ?8 x5 {
  3. 2.718281828459045
复制代码
9 a$ D1 v/ I% f
math.pi  
数字常量,圆周率

9 v2 f( X. w" C4 O
  1. #数字常量,圆周率
    7 v9 ]& K# f+ Z* k
  2. >>> print(math.pi)9 D% K/ Z9 X& _2 O7 z, i
  3. 3.141592653589793
复制代码
- V3 T0 k: h. r7 _4 y
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
* e3 z8 D2 F, R& `% o
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    , L( ^$ H2 L& Y/ }  p
  2. ceil(x)' o' J7 m4 c' d5 S  Z6 b% I
  3. Return the ceiling of x as an int.$ J9 ~) U9 ?3 k* y- q8 w
  4. This is the smallest integral value >= x.( a9 m- N7 t! f" i9 s
  5. # s" W( F, H; `# ^' U6 v" t/ o7 r9 G8 R
  6. >>> math.ceil(4.01)7 p) {- F  C0 n. ~" `
  7. 5. s' [7 I# V% F: R8 ?2 F; z. t
  8. >>> math.ceil(4.99)* y0 a! r3 w7 L* k
  9. 5
    & r% J8 E' x5 H; w( \
  10. >>> math.ceil(-3.99)! l' r' o& E6 o. W
  11. -3( z/ d  T# e) @# S+ @* n
  12. >>> math.ceil(-3.01)* d3 S1 N9 w' h0 R1 T
  13. -3
复制代码

0 j  f( V( ~) ymath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
9 @+ f0 A" N, t/ Z; p1 q" ~5 w
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 l! l2 l5 [( o
  2. floor(x)8 I. j3 W- C) a3 x
  3. Return the floor of x as an int.
    ! h2 p, O  [# E$ U. t( X& `
  4. This is the largest integral value <= x.
      V" @3 E& L. H# d/ y: A! h
  5. >>> math.floor(4.1)
    ! C: J( \* P2 _4 C; S4 h
  6. 4, _$ S1 J, F# L! w  [
  7. >>> math.floor(4.999)
    : M4 C" D% p' K: Z7 F8 F5 n
  8. 4
    * i0 G: W1 |6 y. N2 l7 D
  9. >>> math.floor(-4.999)
    0 v; G) X6 }7 [- ~
  10. -5
    + J/ v% u5 X: n% b! n: W; g
  11. >>> math.floor(-4.01)
    / t& V( n$ o0 g1 r$ u7 ~
  12. -5
复制代码
) t. A# s' ?) I' B0 n$ c+ G  i: x+ v
math.pow(x,y)  返回x的y次方,即x**y
/ t, w1 ]" r" t& o- a
  1. #返回x的y次方,即x**y5 x* L+ C8 ~# M" H
  2. pow(x, y)# C2 S$ Q5 l" k$ p* D: H
  3. Return x**y (x to the power of y).
    & ?) n  M% V$ A% N/ n7 @
  4. >>> math.pow(3,4)
    ( l+ N5 h6 z/ G6 |
  5. 81.0
    ! W2 n4 d" B* {% E" ?% [
  6. >>>
    1 ^  b1 }( p8 y2 Z* @: @
  7. >>> math.pow(2,7)  Z! T( X( t1 x: ^7 ]+ Y  l0 r% g
  8. 128.0
复制代码

* d1 F6 `+ ^1 Mmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 ]+ K( w; o; D& J
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ p8 B3 V8 w3 r$ X% _6 Y
  2. log(x[, base])9 l  k" @  W, _# V: A
  3. Return the logarithm of x to the given base.. |6 F+ u" I% T+ f: c' a
  4. If the base not specified, returns the natural logarithm (base e) of x.
    1 D( o  W  m& F9 E) S, M4 }
  5. >>> math.log(10)+ |# f4 m( p1 c3 s6 \
  6. 2.302585092994046! S1 v, ?! G! @
  7. >>> math.log(11)
    ( ^4 Y$ n# A; H9 l4 C: I' U
  8. 2.3978952727983707
    - B/ [( q: L$ f# W
  9. >>> math.log(20)
    5 x1 ~9 P4 R7 h
  10. 2.995732273553991
复制代码
- Z9 W8 J( ]5 u9 q* {7 \
math.sin(x)  求x(x为弧度)的正弦值
$ u1 ]/ y9 A# C2 n* {+ o3 Z
  1. #求x(x为弧度)的正弦值4 B# k* u) J3 R, y; C( W4 K
  2. sin(x)
    5 L% ~4 S6 T% E% K# q
  3. Return the sine of x (measured in radians).
    ( Y4 b, S) q5 F$ k& g; e
  4. >>> math.sin(math.pi/4)
    . e4 v: p+ |( i0 y; L( k9 M5 A) \
  5. 0.7071067811865475- ]9 G6 T$ b# F  L' h) ^+ s
  6. >>> math.sin(math.pi/2)
    / I# c/ Z. d+ S3 C* P
  7. 1.0
    0 u/ w. _# }# ^+ Z1 ~  k; L
  8. >>> math.sin(math.pi/3)
    # r$ {2 x& B$ ]! ]; d
  9. 0.8660254037844386
复制代码
8 C2 u+ J, f( ^3 C1 s7 w
math.cos(x)  求x的余弦,x必须是弧度, }( ^. l# e+ a2 C5 b$ b2 A% S
  1. #求x的余弦,x必须是弧度5 P, M! u; R) u  L$ N
  2. cos(x)4 A  j: E/ f6 t8 p
  3. Return the cosine of x (measured in radians).
    - C' R/ b9 M" m* I3 W3 d
  4. #math.pi/4表示弧度,转换成角度为45度
    1 p2 y& w, V; q$ K2 v: h, Z
  5. >>> math.cos(math.pi/4)
    # p) o: Z5 e) @7 p7 T9 }! }
  6. 0.70710678118654760 N% C' @- {4 _  w+ X6 W3 o
  7. math.pi/3表示弧度,转换成角度为60度8 R% Z* i! J" m
  8. >>> math.cos(math.pi/3)
    : {0 y# T% C+ G- @: K
  9. 0.5000000000000001
    5 y6 p/ H" D, H  l1 O
  10. math.pi/6表示弧度,转换成角度为30度/ z# r% n: t6 N* g1 h
  11. >>> math.cos(math.pi/6)& ~5 {( e/ l! e2 p* Z* H/ C, R
  12. 0.8660254037844387
复制代码
! P! E2 x: y( ?% N* t8 ^
math.tan(x)  返回x(x为弧度)的正切值, y# L6 \9 s0 a- `# ]" J8 F' F# l
  1. #返回x(x为弧度)的正切值
    0 g2 P( _. L7 D4 l# a7 j2 X
  2. tan(x)2 Z$ c7 ^3 P2 G0 e) @7 v' }
  3. Return the tangent of x (measured in radians).
    5 |4 L: b  M1 j; U4 {
  4. >>> math.tan(math.pi/4)2 I  ?6 f5 F  r, q% u! {1 ~
  5. 0.9999999999999999: `) v8 e, W/ e9 g6 d: @1 k
  6. >>> math.tan(math.pi/6)% {# I+ f7 {9 t; G9 w
  7. 0.5773502691896257
    ' o9 f/ v: E1 L" y8 X8 L
  8. >>> math.tan(math.pi/3)& [' c- @' U. z* S! Z/ q
  9. 1.7320508075688767
复制代码
  A/ \% }; U" v3 C; n
math.degrees(x)  把x从弧度转换成角度
$ F, N2 L! }8 `8 X, K! W$ r, e
  1. #把x从弧度转换成角度
    + E& ?8 l$ V, s# m; R* v
  2. degrees(x)* Z7 j$ E+ Z: |, _
  3. Convert angle x from radians to degrees.
    " i' o# p7 O* `+ S( R

  4. ) o0 [' o3 q" F1 r, q( ~1 y
  5. >>> math.degrees(math.pi/4)
    $ }) _- Q3 p3 V, D
  6. 45.0
    ) \  t2 b: `& I2 {$ b7 C
  7. >>> math.degrees(math.pi)2 P! K) M8 ]4 y1 B
  8. 180.0
    : N2 C, `6 G# a/ q! H
  9. >>> math.degrees(math.pi/6)
    + h; M5 |% C8 ~) Y
  10. 29.999999999999996
    # S9 ]; H( ?& r# Q! Z
  11. >>> math.degrees(math.pi/3)
    & h# ~3 i, ^* d$ k
  12. 59.99999999999999
复制代码
! W* S7 ]0 @1 N0 K) p
math.radians(x)  把角度x转换成弧度
$ }  P3 Y' s( C; {/ N% A4 R& Z
  1. #把角度x转换成弧度0 e  q: w* _" G; O8 t8 T
  2. radians(x)% {9 p8 `+ M2 ]6 @( G* |
  3. Convert angle x from degrees to radians.
    5 c) \6 s6 j& a( y/ V
  4. >>> math.radians(45)
    ) P7 M# _! a4 _
  5. 0.7853981633974483
    ( z$ x+ g6 P- }
  6. >>> math.radians(60)
    5 N" k2 ?  }% r1 p7 S+ N0 z* z
  7. 1.0471975511965976
复制代码
; F  u) `1 ?9 Y; X: Q' P
math.copysign(x,y)  把y的正负号加到x前面,可以使用05 X  E+ }' B8 Y' F
  1. #把y的正负号加到x前面,可以使用02 k6 l+ a1 Z0 G: E5 s! `6 |
  2. copysign(x, y)1 c, ?7 `- }8 t7 Z
  3. Return a float with the magnitude (absolute value) of x but the sign
    5 u, \6 _  E1 v/ d# `* ^
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    2 B8 e! F- D5 a+ D$ K5 L" V) t
  5. returns -1.0.
    / o; E0 D* B0 g/ c5 f1 I1 b
  6. 2 y& P: ?( o. R
  7. >>> math.copysign(2,3). s5 v' F* v5 d9 e; S- P4 d
  8. 2.0
    . X4 w# B1 a7 X! s9 `2 V
  9. >>> math.copysign(2,-3)
    ( s7 [" v; k2 Q2 K& n
  10. -2.02 K* P; O  H. g0 G
  11. >>> math.copysign(3,8)# ]$ |" z& E; n
  12. 3.01 @  o! [: H- D3 B
  13. >>> math.copysign(3,-8)
    # @$ U, E# L; L
  14. -3.0
复制代码

" t: T, u# }& I2 @$ gmath.exp(x)  返回math.e,也就是2.71828的x次方
6 n5 j# h8 B0 R# K# v3 P/ J! }( f6 Q" L2 c
  1. #返回math.e,也就是2.71828的x次方1 d/ `- [( l- g" V4 Q/ Z+ y
  2. exp(x); l" k; Q/ p" q9 L5 ?
  3. Return e raised to the power of x.
    8 ^$ @- \& i' }' t# ]" N7 Y
  4. 1 {+ \9 e: R' _0 y4 u/ {9 o# m
  5. >>> math.exp(1)1 d4 \' H; f. q& E: Z( n5 z+ c
  6. 2.7182818284590458 R5 a3 Y; x  S6 a" q
  7. >>> math.exp(2)
    # N* K! ?3 e% |- O% q2 G
  8. 7.38905609893065
    5 p& B, ~3 h+ ^- Q; x
  9. >>> math.exp(3)9 D: K' C8 b; c
  10. 20.085536923187668
复制代码

4 e- }  L5 Y9 u1 P- t9 w  zmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1' T1 u. A$ H% x5 `7 c
  1. #返回math.e的x(其值为2.71828)次方的值减1& @6 u2 Q; d1 P9 h4 y* N  T
  2. expm1(x)
    , X& l- ]4 g3 a4 ^, Y- q
  3. Return exp(x)-1.5 u* e$ b. S. J; f
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ; c- ^: n7 [+ ~

  5. , `- g+ k( {4 m  ^
  6. >>> math.expm1(1)
    ( F) j" d( K" G6 P+ ?) }5 b! c. j7 y5 u, b
  7. 1.718281828459045
    6 g9 j% X+ s: j) \/ P! @- {" N
  8. >>> math.expm1(2)% S! V) z+ f6 L4 N/ Z/ O( m/ f
  9. 6.38905609893065% L5 L5 @5 J3 M. G; w
  10. >>> math.expm1(3)9 \$ _7 B! G; ?$ V+ x
  11. 19.085536923187668
复制代码
2 L1 p/ }$ O9 r3 {, v9 p( f! d
math.fabs(x)  返回x的绝对值' H; {! j. P1 Z3 f7 Q
  1. #返回x的绝对值
    4 B. Y; Q- L) A2 K: V2 d
  2. fabs(x)5 r) L7 R, Q4 l" f0 o  b3 o, a6 z
  3. Return the absolute value of the float x.
    2 Y9 }+ t1 r2 V* k0 [
  4. ' P# p$ J& R7 ?! r
  5. >>> math.fabs(-0.003)2 I  F- ?2 F0 i6 w9 A2 n* E
  6. 0.003' _- e6 f8 l+ M9 c$ y9 v
  7. >>> math.fabs(-110)- J5 {1 _. @1 H4 G9 I6 c# a
  8. 110.0
    ! c9 z; ?; I4 [7 r2 n3 S6 P
  9. >>> math.fabs(100)) W6 W/ @- T  m& d
  10. 100.0
复制代码

1 y( e: z2 Z- amath.factorial(x)  取x的阶乘的值/ B% Z2 Q% p9 z1 B) I/ q9 X4 ]
  1. #取x的阶乘的值
    / H$ Y8 O, D$ x4 I0 m: x
  2. factorial(x) -> Integral6 s7 n) p6 u6 ]) D7 n
  3. Find x!. Raise a ValueError if x is negative or non-integral.6 t- P/ {4 Z& j* i: _6 w, w
  4. >>> math.factorial(1)/ I& f; H$ E" a% W5 \# @. J
  5. 1, j, l" ?' P7 c6 Z; K
  6. >>> math.factorial(2)9 `' ?2 u6 j- j' y5 j' L
  7. 2) x. A% z% n# y( d$ h
  8. >>> math.factorial(3)0 E" b5 E. a2 k* o
  9. 64 f/ j* Z, w7 N) y3 W: k" ^6 G
  10. >>> math.factorial(5)
    2 I# Q. C6 g$ U; k3 {
  11. 120! s! C9 ]' l( a/ Q& x1 q8 x
  12. >>> math.factorial(10)
    3 R) ]' F8 W! V. B( M0 J
  13. 3628800
复制代码

( X2 I3 o, J6 c% r# S5 Qmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数/ g* w& _9 ?; I& g" a
  1. #得到x/y的余数,其值是一个浮点数, ~3 x' V, i( g, X
  2. fmod(x, y)
    $ S3 Z5 T% I7 S1 s- j0 n& K
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    $ |0 G% K4 o1 L
  4. >>> math.fmod(20,3)
    : b, q$ \9 U" v4 A9 H
  5. 2.0
    8 y4 L% b) p5 n  P$ ]6 }
  6. >>> math.fmod(20,7)+ Q; j! ]. H0 Q' \* B! {
  7. 6.0
复制代码

3 h4 J4 K7 v& t  P$ k! _8 jmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围% s8 o& {5 K: ?$ h+ Y* p+ I
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,& ], t0 R) E$ _2 m7 a3 G
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    & t9 B- a$ J8 b; J
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    4 t0 B& j# K% t; O9 U5 C
  4. frexp(x)
    9 S( F7 a- e: v
  5. Return the mantissa and exponent of x, as pair (m, e).) `# [: [, X, u* Q8 _6 w
  6. m is a float and e is an int, such that x = m * 2.**e.
    4 J# ~8 P4 O7 v* k6 u( r
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    ; I( b6 ~: k: M. q: j1 n) ?1 B
  8. >>> math.frexp(10)8 v, z( k: g% E+ k
  9. (0.625, 4)
    ( c0 i# Z5 N+ L/ O- b% M
  10. >>> math.frexp(75)# ?9 q4 ~  I/ Z" F. X, y
  11. (0.5859375, 7)
    * z3 v6 }3 D! F* d
  12. >>> math.frexp(-40)
    ( d' M/ Y. L1 _. [/ z/ h. Y" Q
  13. (-0.625, 6)' a* J8 H% d( ^- ~0 n5 q3 A$ W6 U2 F
  14. >>> math.frexp(-100)
    # p# Q3 D# a; L8 d! E
  15. (-0.78125, 7)9 ]8 v2 q' F* m' `+ L2 {
  16. >>> math.frexp(100)
    : `8 ~+ l; @7 E3 v' ^; b
  17. (0.78125, 7)
复制代码
1 D9 d5 R" W2 X3 [5 h5 D
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列: [2 `6 C5 Q5 g  R
  1. #对迭代器里的每个元素进行求和操作
    ! N- d& O$ y/ I  A) ^5 O
  2. fsum(iterable)# W/ x) @% Q4 S4 e
  3. Return an accurate floating point sum of values in the iterable.4 y6 C# b9 K8 l  i9 }
  4. Assumes IEEE-754 floating point arithmetic.2 o2 L3 p& X: I. Y
  5. >>> math.fsum([1,2,3,4])# S* }* _9 p7 W. n% Y8 s- d
  6. 10.0
    5 w! v( r0 d5 {) b' D
  7. >>> math.fsum((1,2,3,4))
    + B0 l" {! z4 Q
  8. 10.07 ]" o7 ?. R( \, E7 s( r
  9. >>> math.fsum((-1,-2,-3,-4))) l3 T! u$ [3 |% R4 ~+ P, a3 t* I
  10. -10.0
    ( s6 X  X/ l: I3 h4 X7 I7 S! i
  11. >>> math.fsum([-1,-2,-3,-4])
    + q2 }5 [8 I" M2 T: i
  12. -10.0
复制代码
' S! B" _7 Z& Y' X" U) y
math.gcd(x,y)  返回x和y的最大公约数$ V5 v* g5 L, v2 r+ D" R! y7 S
  1. #返回x和y的最大公约数" Q: f4 J  y2 [1 R
  2. gcd(x, y) -> int- b- m+ M( W1 Y3 D9 M- K
  3. greatest common divisor of x and y
    ! q, o" l8 c+ N  W6 C3 X
  4. >>> math.gcd(8,6)+ }! z2 T) h6 e3 [4 f: R
  5. 2
    & o1 `  s5 I# j7 J0 N$ ^+ s1 r
  6. >>> math.gcd(40,20)0 |5 a$ t$ D0 Z* A
  7. 200 `  c! s+ {7 C3 J0 r% L) F
  8. >>> math.gcd(8,12)8 z' E9 `4 m) Y9 B$ d% Q9 c( b" C0 m
  9. 4
复制代码
& d! ?; C" e, F, C( G. i
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 I' g6 [* W6 d( ?
  1. #得到(x**2+y**2),平方的值
    & }5 N* M1 e3 G, [+ a. T  P
  2. hypot(x, y)8 `* n+ k6 e& }4 _% I9 B8 q
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    . i# M' [, W( W* J. B8 D* m* d
  4. >>> math.hypot(3,4)
    4 |3 I7 D5 h; ^/ w( _$ [7 {
  5. 5.0. ~7 i* Q$ `" k
  6. >>> math.hypot(6,8)
    ' r& p' |& ~2 I2 R
  7. 10.0
复制代码

- q* Y. a3 B. T. i$ imath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
; t: f% x8 n8 |  ?* f) w
  1. #如果x是不是无穷大的数字,则返回True,否则返回False" Q! K% A3 P( o' C4 m5 S/ M: H
  2. isfinite(x) -> bool
    3 ]2 y- ]/ |% C0 S* |( q
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    7 w6 r8 g# z: _
  4. >>> math.isfinite(100), Z  |8 W) X: M2 b' U1 i
  5. True
    3 Z& A. d% U6 S: Z
  6. >>> math.isfinite(0)
    3 L! O- i3 K9 o6 g  i
  7. True$ y) f# O* }- Z6 b! B1 y7 f
  8. >>> math.isfinite(0.1)
    * H" g  n# G' I9 V0 @% p
  9. True
    ! ~8 X  V! O/ W5 j+ b
  10. >>> math.isfinite("a")# }# u5 R. \0 N2 x
  11. >>> math.isfinite(0.0001)
    # x: g# W2 c  z
  12. True
复制代码

* y4 T+ \  _1 g, g4 dmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
  t  w1 `& _. Z1 Z$ L
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 ]  Z  }6 I1 p9 @
  2. isinf(x) -> bool0 I1 D# |; L3 A9 O
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ; y# A; X. G( c) z
  4. >>> math.isinf(234)
    # _7 r# R3 c5 {; O' I
  5. False
    $ I. ]9 g7 T4 y6 q
  6. >>> math.isinf(0.1)0 [6 Y/ o! E2 `8 V# n5 C
  7. False
复制代码
- ~" e! _1 {8 U- U5 W* s
math.isnan(x)  如果x不是数字True,否则返回False
2 ^/ u' f" |: u7 z$ g/ S) x
  1. #如果x不是数字True,否则返回False8 Q9 U5 d: }3 |; H! P/ |% ~4 Z
  2. isnan(x) -> bool
    4 [. B; f) x) c
  3. Return True if x is a NaN (not a number), and False otherwise.
    ) J# ~- d: ^# r4 U) j
  4. >>> math.isnan(23)
    6 K. H, m/ \0 T: E) t
  5. False
    * h# o; V% _: H. K& P/ @
  6. >>> math.isnan(0.01)
    8 ?8 k  k" n3 P9 b, q) R5 b
  7. False
复制代码

. L& d: R" B% |. ~2 J% x7 B5 {' e# [3 Y" gmath.ldexp(x,i)  返回x*(2**i)的值
  u& u# p7 P0 x
  1. #返回x*(2**i)的值0 q7 r) @+ Q8 h$ ]0 {1 U* g# ]
  2. ldexp(x, i); k5 I* Y9 {# @3 i6 o" x
  3. Return x * (2**i).
    9 F4 h1 u9 o  }
  4. >>> math.ldexp(5,5)
    0 D; {! X8 ?2 N8 m- O6 v# A6 _
  5. 160.0
      g8 Q/ [  O+ I: s
  6. >>> math.ldexp(3,5)
    * k, d: E7 K% S
  7. 96.0
复制代码
7 x# ?9 H; m9 e+ d
math.log10(x)  返回x的以10为底的对数& t: t  |4 D( e( |
  1. #返回x的以10为底的对数
      E( g! O4 ~7 ^" `4 Q* J8 R
  2. log10(x)
    3 S# u- K, z. v' C( n
  3. Return the base 10 logarithm of x.
    7 h: K1 y6 J4 A" P0 J. P/ b6 X
  4. >>> math.log10(10)
    . f# O5 d) F& ~: ^
  5. 1.0
    $ I, s4 m/ X, a. \' ~! j
  6. >>> math.log10(100). P: ~, O: W+ Y4 `( H2 }% H- w
  7. 2.0
    $ _  y% ]+ R1 h' n( _7 n, _
  8. #即10的1.3次方的结果为20/ l6 |6 B) {1 [% u
  9. >>> math.log10(20). K6 s: H7 N8 S# p5 Y6 s
  10. 1.3010299956639813
复制代码
1 l+ J( G  Z# F& \8 Y9 D
math.log1p(x)  返回x+1的自然对数(基数为e)的值
9 E3 E! s; G  C  U; J' D
  1. #返回x+1的自然对数(基数为e)的值
    9 Z! F* X( Y% T4 j
  2. log1p(x)+ y: k/ O2 e" {9 c# d4 _& R
  3. Return the natural logarithm of 1+x (base e).1 U& Y3 i& T; F6 Q( X" \' y
  4. The result is computed in a way which is accurate for x near zero.$ U4 Z. X9 q, j$ X- h& W: q
  5. >>> math.log(10)" O4 w0 |, V9 {' ^' v$ P
  6. 2.302585092994046! B  k7 C- i: o+ }7 W
  7. >>> math.log1p(10)3 N: S8 o- `7 j) I! k6 Y- F
  8. 2.3978952727983707% _0 F, _' Y6 ?* z
  9. >>> math.log(11)2 a8 O7 A0 @' [" @4 u9 |0 @
  10. 2.3978952727983707
复制代码

. a; l) H  s; |& t2 z4 ]math.log2(x)  返回x的基2对数
6 t8 Q0 U  ?# G" G" j, M
  1. #返回x的基2对数  A) y; `; _( }% v- C1 t+ r, Q" X
  2. log2(x)+ M3 ]7 {9 V; P8 ^/ A- A( I
  3. Return the base 2 logarithm of x.  ^" Z, C' `; B6 U, `
  4. >>> math.log2(32)
    8 _+ Y  S* m% l: N) W0 f+ r
  5. 5.01 ~- B6 e8 j, ~* x4 W, @1 j
  6. >>> math.log2(20)
    - L4 b( ~/ q- q& v
  7. 4.321928094887363' T# o" q" @2 F' L3 J0 G4 i
  8. >>> math.log2(16)
    ; {9 a/ E. n/ j# ^0 T/ ~
  9. 4.0
复制代码

  D% \3 c- l3 |4 m: I$ qmath.modf(x)  返回由x的小数部分和整数部分组成的元组
: e& [( E( b0 b
  1. #返回由x的小数部分和整数部分组成的元组
    - u7 j2 g' V+ w& N
  2. modf(x)
    . A' o9 L: ^1 e# P% c0 A5 R
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ; B2 p% c$ W- o& c
  4. of x and are floats.
    " g0 ?6 H2 R: S' i7 \9 D
  5. >>> math.modf(math.pi)2 @# w( S! p# o: @
  6. (0.14159265358979312, 3.0)
    1 O% g! Y9 ^0 ?2 b# D9 O6 t, I: o5 b
  7. >>> math.modf(12.34)8 W2 i% w4 T  B9 l7 L5 I
  8. (0.33999999999999986, 12.0)
复制代码

- ?, P* b$ F& [/ a) E" Kmath.sqrt(x)  求x的平方根3 c. R6 v, Q5 L' z3 |8 i" S
  1. #求x的平方根
    0 D7 p4 t" x0 O9 K
  2. sqrt(x)9 B: e+ B% e6 y, p1 Y
  3. Return the square root of x.
    ! d( l; e  x7 ]7 V, M
  4. >>> math.sqrt(100)
    " ~; ?/ l/ h+ z: C1 K
  5. 10.0
    / V& l. W& t* A7 ~2 I+ l7 ]
  6. >>> math.sqrt(16)8 W+ L3 R' X7 p1 T6 I: A/ \, ^
  7. 4.0  }9 r# \7 g) W
  8. >>> math.sqrt(20)
    3 P- B' m0 M0 m8 {
  9. 4.47213595499958
复制代码
- _4 b, C( V' P8 h% u
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    & t5 i# r% r: h
  2. trunc(x:Real) -> Integral6 g8 G- [1 j+ X6 ^
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.; P2 A' O( {" u! l
  4. >>> math.trunc(6.789)1 O7 W9 B4 s. s$ j2 U
  5. 6& V5 T, i3 B9 n$ ?  j* t
  6. >>> math.trunc(math.pi)
    2 i, \- h0 f, C% i
  7. 3& W( U9 d, [. ?, K
  8. >>> math.trunc(2.567)
    8 ]2 V& U- }' R$ e, W
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-18 14:47 , Processed in 0.098970 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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