新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
$ u4 C. v. a2 {. v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
- a" Q. B2 a  d$ V

9 T1 k9 l) ?  Z. [# l- u1 \方法19 J* P7 T" H0 V2 C* ~! `
  1. >>> import math
    , i/ ?2 n  W1 p: d
  2. >>> math.sqrt(9)
    / ?) W! z9 R, i- v& u7 ^
  3. 3.0
复制代码
方法2* I4 B- N. _( c9 W5 ?. Z
  1. >>> from math import sqrt
    % y1 H. L$ t, \0 [5 `: I
  2. >>> sqrt(9)' L; D! C, R' L
  3. 3.0
复制代码

5 n  j, I% }  h* P
. |/ _' C* |& n* f3 C7 R" ]
math.e  表示一个常量, U' G# ~# Z: d9 d
  1. #表示一个常量6 ?$ Z! C/ P, f! \
  2. >>> math.e
    : e' Z) F6 [. U; m$ Q0 N
  3. 2.718281828459045
复制代码

; ^; X5 o4 t9 o$ bmath.pi  
数字常量,圆周率

# A9 L- k' K( `* T; ?) P' B& S
  1. #数字常量,圆周率
    " ~0 V$ R. N2 B
  2. >>> print(math.pi)1 a- f4 w! x) t
  3. 3.141592653589793
复制代码

, E! E7 @2 D1 I/ O  gmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
7 o2 e/ \& {+ o  \
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    $ L7 ]' I) p2 a6 e6 o
  2. ceil(x)
    6 X3 E5 S. `) U8 {4 R3 W
  3. Return the ceiling of x as an int.' j# _1 ]3 ]7 ^$ j6 q( r6 g1 ^
  4. This is the smallest integral value >= x.* k; E+ @# }' {/ y# H- u- z
  5. 1 f/ G4 [' @/ V) v, c
  6. >>> math.ceil(4.01)
    7 o4 m' Q6 u& O* o) e  B
  7. 5
    , D1 r. H0 D2 E( O; M: b
  8. >>> math.ceil(4.99)+ v; u5 P3 C$ g$ a6 P) C% c3 I
  9. 5. m: p3 n0 N7 v
  10. >>> math.ceil(-3.99)7 A  ]! e6 X( r  e
  11. -30 R9 d7 d7 W0 j8 j# ~2 r
  12. >>> math.ceil(-3.01)# ^" ?1 x  u. [1 w* r
  13. -3
复制代码
1 V" Q6 R- ^2 I3 K; f' K
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身3 r# x- n2 g* k" g( j- d
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 W0 m- ^5 I: a
  2. floor(x)( s! \9 R* O& u+ {
  3. Return the floor of x as an int.
    1 h3 ^: K" F: y! a) ~
  4. This is the largest integral value <= x.1 ~! s5 V: z$ N0 |, B8 j7 O
  5. >>> math.floor(4.1), b# d, B, C! j" E& I+ {  _0 t, k
  6. 47 e: h, ^& {' V- {9 M
  7. >>> math.floor(4.999)
    8 A. Y+ G$ d1 [' X: ~# I1 T
  8. 4
    3 S' c' x: s1 n7 g: }
  9. >>> math.floor(-4.999)
    ; z% F: `8 y* b6 y) i" H: r
  10. -5) b( M8 l" t4 J6 Q9 t, w" g( t/ s
  11. >>> math.floor(-4.01)
    , W5 \: a. n$ R2 z; {# A  g
  12. -5
复制代码

9 m3 o- t& ^: |, ?! A! i( h& D$ jmath.pow(x,y)  返回x的y次方,即x**y$ a- D4 U5 A& J" n/ H1 y; Z
  1. #返回x的y次方,即x**y
    6 D$ A' u. C$ G+ `' N
  2. pow(x, y)# \8 ]4 c6 A7 o) o3 R8 e
  3. Return x**y (x to the power of y).
    4 Y7 J) x7 _' C; S0 l. _
  4. >>> math.pow(3,4)) l; B$ o( ?) x3 u* E  e( c0 V7 Q
  5. 81.0
    * j" x' |. D4 I8 m7 P7 t; i
  6. >>>
    , c  x- i+ q3 F/ b" H1 c8 }8 Q' y
  7. >>> math.pow(2,7)- D, V/ y+ ?5 J& e3 g& N
  8. 128.0
复制代码

% Z6 ?2 H7 j; [* emath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)$ @0 k8 `8 C( S! ]
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    # ]! l3 H8 ]# n3 M% j
  2. log(x[, base]); G  `6 D9 x% t
  3. Return the logarithm of x to the given base.3 D$ P8 T! k" Z) {' n0 T
  4. If the base not specified, returns the natural logarithm (base e) of x.$ s% ?8 b: _7 A
  5. >>> math.log(10)7 T4 E9 q: Q4 y( Y: i& |- _
  6. 2.302585092994046
    3 I2 X. ^' W  x7 h9 J
  7. >>> math.log(11): v8 m& v: I. K- m: `
  8. 2.39789527279837075 y  U6 _9 h; A" e! V
  9. >>> math.log(20)
    , ~; w+ o* p3 b8 h  r* g6 f  p
  10. 2.995732273553991
复制代码
) b# H9 E( b+ G. m
math.sin(x)  求x(x为弧度)的正弦值: d5 j. L2 {: K# X2 s
  1. #求x(x为弧度)的正弦值
    ' C* k" O* y  k$ p# U: e
  2. sin(x)
    - }2 M; K) U$ |& x6 i1 B. G
  3. Return the sine of x (measured in radians).
    " Z! n% d# J0 l( f
  4. >>> math.sin(math.pi/4)
    / a' T8 j- U; L# P. ~+ T4 {/ ~
  5. 0.7071067811865475
    / x: L* q0 O" ]; \$ b' r
  6. >>> math.sin(math.pi/2)# Z& M0 J. r) N; h3 S  @2 G
  7. 1.0
    9 Q8 e7 p% G- ?- B# b0 R
  8. >>> math.sin(math.pi/3)
    % R" ?( c9 t& @0 B
  9. 0.8660254037844386
复制代码

$ p$ r( _7 N  l! r9 jmath.cos(x)  求x的余弦,x必须是弧度( @% q8 ^  F) c: }' }- L' W
  1. #求x的余弦,x必须是弧度0 k2 s/ {4 D8 C# K
  2. cos(x): i; s4 H( a- U. {! I/ Z2 `$ p
  3. Return the cosine of x (measured in radians).
    ' {% E# g: y( ~( `) g4 R
  4. #math.pi/4表示弧度,转换成角度为45度
    1 w% @0 z. }: O* z% C$ w
  5. >>> math.cos(math.pi/4)2 I0 K, M3 ~8 B6 Q& I; t
  6. 0.70710678118654761 ]2 e" Y. n. R. ^$ P: B
  7. math.pi/3表示弧度,转换成角度为60度! _" c( l7 |9 G# b" w0 h" p0 a7 p
  8. >>> math.cos(math.pi/3)5 V0 n; D* M" |1 J7 r) L7 z7 v2 F
  9. 0.50000000000000015 O- {' f: U2 d8 u
  10. math.pi/6表示弧度,转换成角度为30度' g# {: _5 @% o  ?1 X
  11. >>> math.cos(math.pi/6)$ L: Y. [% h" J9 A# m* P* Q
  12. 0.8660254037844387
复制代码

; a7 r% _& P5 X' X+ S8 h# p9 Imath.tan(x)  返回x(x为弧度)的正切值
1 D3 p& M/ h  x9 P" \
  1. #返回x(x为弧度)的正切值0 d7 Q1 Z' ~+ j% w8 E& D
  2. tan(x)
    7 T' G2 j$ {# e" Z" [
  3. Return the tangent of x (measured in radians).
    8 G$ @: K* N2 i8 w7 M+ S: A
  4. >>> math.tan(math.pi/4)/ m5 {# {2 Z, Y  A$ g6 S) e* l+ n
  5. 0.99999999999999992 s+ T0 w* |+ B  {# J
  6. >>> math.tan(math.pi/6)( }2 a/ n! g/ @4 V, z& X
  7. 0.5773502691896257+ B  U8 Q  l- F5 e7 _" N7 I& A
  8. >>> math.tan(math.pi/3)" l' x- ]& ?) M2 a9 z3 Q
  9. 1.7320508075688767
复制代码

" a" ]" W, x% [5 f8 \8 r" |7 z7 }math.degrees(x)  把x从弧度转换成角度
6 @' a$ |; m; ]
  1. #把x从弧度转换成角度
    / H. g, y# y3 j6 u9 W% d
  2. degrees(x)
    ' n% @4 d- d: x
  3. Convert angle x from radians to degrees.( N4 C' p; D6 p/ z# b+ R
  4. + u( `. ?/ Y) {( M) l- F
  5. >>> math.degrees(math.pi/4)
    + U! b- X% x  @8 L+ M
  6. 45.04 s% s6 [& }) {5 t8 X! x0 e
  7. >>> math.degrees(math.pi)
    & T" m' N6 G# N- K% s$ X
  8. 180.0
    0 R( u( B- K9 u/ }7 ]' P5 V
  9. >>> math.degrees(math.pi/6)' J' c, p9 h1 {  y! ^7 L' ^  ^
  10. 29.9999999999999962 ]3 _! F, T7 }! e2 H; c, L
  11. >>> math.degrees(math.pi/3)$ P% C4 G% A0 }' J4 t
  12. 59.99999999999999
复制代码
0 l) B( s8 e  p% e* V. N
math.radians(x)  把角度x转换成弧度0 r' W5 X8 `, ]
  1. #把角度x转换成弧度' `3 m" ~! V# Y$ J
  2. radians(x)
    " ]+ f" b" k6 i( b# |! f1 f, V# s
  3. Convert angle x from degrees to radians.& Z6 l' d: j, I8 w
  4. >>> math.radians(45), y; W- G- h* i+ ?) ]2 e" @. B
  5. 0.78539816339744833 k& W! A% H# p! n& k' i9 T
  6. >>> math.radians(60)7 q2 A; h  y: O. J1 N! b
  7. 1.0471975511965976
复制代码
6 i0 ~# C9 L# H8 Q
math.copysign(x,y)  把y的正负号加到x前面,可以使用0: Y) G& z( c0 n+ K
  1. #把y的正负号加到x前面,可以使用0! Y; E( I1 ]% ?% L; p
  2. copysign(x, y)
    : c9 @# _1 m% t* F+ m1 b0 }0 x  i
  3. Return a float with the magnitude (absolute value) of x but the sign ( Q7 P, Q' l" D  T& \
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 7 g9 M5 B4 C% A' y; r
  5. returns -1.0.
    & v7 p3 b: {3 Z% y/ m
  6. 8 W$ u" |# ]' K; _# U$ o
  7. >>> math.copysign(2,3)7 h3 _3 @! ]" X" ^
  8. 2.0
    8 U$ L1 _6 t3 C8 @9 N
  9. >>> math.copysign(2,-3). ?4 C) i; Q( v
  10. -2.0
      U$ n2 q- B+ G8 ^
  11. >>> math.copysign(3,8)8 }5 _8 m$ O! o& n' y
  12. 3.0
    1 A! P; ^) _1 @% Q
  13. >>> math.copysign(3,-8)- ?( B. `8 u) [7 t) m* F& D
  14. -3.0
复制代码
6 A; o( s% k! q
math.exp(x)  返回math.e,也就是2.71828的x次方" j8 _9 |3 l  q. I! c" U/ l4 R
  1. #返回math.e,也就是2.71828的x次方" f6 w6 u, q' Q8 r
  2. exp(x)
    / v! E- V0 V0 P
  3. Return e raised to the power of x.
    ! U' r/ a1 U6 O# C7 |$ K
  4. 7 G1 w1 A9 e( X3 B; j4 I& x
  5. >>> math.exp(1)
    2 {- g7 N: S; [  u
  6. 2.718281828459045. w7 V" \. L- K
  7. >>> math.exp(2)% c: ~/ P8 M! I. {
  8. 7.38905609893065/ I" U9 w; p4 }/ t0 _$ L# B0 O
  9. >>> math.exp(3)7 j8 q, d' p3 b4 u- q
  10. 20.085536923187668
复制代码

( V; [( i; j6 M  E8 @4 ]" amath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
+ y5 ]( f! ~* R( P. z. s
  1. #返回math.e的x(其值为2.71828)次方的值减19 R3 p6 m( W1 w# \
  2. expm1(x)
    2 z* X: B" Z- |  B' v6 H) P
  3. Return exp(x)-1.
    % C* g5 V/ _! e( ?$ {
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    $ m& e! r- `6 M* j8 l4 K$ X5 h( a

  5. * k: e7 Y, k+ v- U+ E, S7 t( P
  6. >>> math.expm1(1)
    ! Z! ~; E/ u. z. u4 i' ?9 i1 r! P
  7. 1.718281828459045
    0 f2 b' N% l; e+ b% g, z5 k6 f3 a
  8. >>> math.expm1(2)) t9 k0 D6 j+ J' ]* c2 ]
  9. 6.38905609893065+ X/ y8 W& U( w
  10. >>> math.expm1(3)
    5 \7 i8 I: r0 _' A  b, X
  11. 19.085536923187668
复制代码

9 B* w9 o6 v- @* n7 O# M; |9 Ymath.fabs(x)  返回x的绝对值( E" }: a  Q1 K) o/ f
  1. #返回x的绝对值
    " \, J! S/ T$ ^% f+ X* M
  2. fabs(x)+ t5 T& g0 Y3 m8 f) }  O
  3. Return the absolute value of the float x.
    7 N  J+ J2 u( v/ a# d9 z( I* e$ b( f2 e

  4. 7 H) N5 C, ^9 }; L! M5 |
  5. >>> math.fabs(-0.003)  b& ?& B1 M9 r: V+ e1 V) Z
  6. 0.0033 q8 A% B( x% }+ w* f* K: a
  7. >>> math.fabs(-110)7 _' y$ f9 ~* q; k/ [1 }6 u5 {
  8. 110.0
    6 h8 N2 s- X: k
  9. >>> math.fabs(100)
    9 l8 G. t2 l3 G: b6 Q
  10. 100.0
复制代码
& |% v8 K: X: G) x0 b8 ]
math.factorial(x)  取x的阶乘的值
/ K9 ]5 l/ S' f3 K$ n+ ^
  1. #取x的阶乘的值# |  j' b* {+ o8 ]& O
  2. factorial(x) -> Integral4 g9 y/ n: u* V( j: d9 E6 i% L5 a: m
  3. Find x!. Raise a ValueError if x is negative or non-integral.  E8 N0 c( z$ Z. L: c
  4. >>> math.factorial(1)
    2 Y$ |% }( q0 Q
  5. 1+ ]0 n, n: O/ B& b# ?4 x( @
  6. >>> math.factorial(2)" V1 @8 a: f7 W
  7. 2
    ( t: c( k: H- q) k# |7 l
  8. >>> math.factorial(3)) V2 h  [* r3 v# v+ D* R9 d2 L
  9. 6) V5 ~, x. k# D0 W. q; c
  10. >>> math.factorial(5)
    & G7 X- B4 a( G
  11. 120
    ( }# {/ g$ R; r8 ]" B9 ~5 H
  12. >>> math.factorial(10)
    . [/ w% A& {8 @: m1 o9 S
  13. 3628800
复制代码

2 K$ S: u1 e& Gmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数' Q" m/ [* t6 D' k1 |
  1. #得到x/y的余数,其值是一个浮点数
    8 p2 n) ?) N& h0 B, N1 ?4 K9 H
  2. fmod(x, y)! Y7 o) O' `$ ?$ M
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    % P2 o! D" o$ @. V% A/ t
  4. >>> math.fmod(20,3)
    ; |  {% e1 S3 N; P
  5. 2.0
    * z5 C6 q' X6 e1 w$ U7 A- f
  6. >>> math.fmod(20,7)
    " q8 V% j/ Z: ]0 ^1 R. j) r- E
  7. 6.0
复制代码
5 Y5 a& ^: z# |4 X& ?
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
5 h) M- V* P* H# {9 \" X! K$ L
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    1 Q; U' K, J6 ~, ]2 }8 C: c# m
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 V$ H& c) W& D, m5 G/ x( w
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    9 C- J# q! x6 ^. @5 H( T5 R! C
  4. frexp(x)2 I6 M+ D  C4 A4 D) h6 [5 k
  5. Return the mantissa and exponent of x, as pair (m, e)." Q& n. v: |2 Y5 E% I6 H5 i
  6. m is a float and e is an int, such that x = m * 2.**e.% U9 f2 Y. E/ P" q/ |  D
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.) W" f8 q- a# v: n* M' V. D5 H- d4 `
  8. >>> math.frexp(10)8 L5 e' d+ z+ ?
  9. (0.625, 4)* o  ~1 d- T9 ?, H& Z
  10. >>> math.frexp(75)# h% e/ s+ y, k! L* J
  11. (0.5859375, 7)' W2 c& ?8 \& o  e6 @1 o6 ]
  12. >>> math.frexp(-40)0 R8 T# k) \, J! N% L+ p" |
  13. (-0.625, 6)
    , j5 a4 ^& m5 ?  ?4 H0 ?$ g" l% |
  14. >>> math.frexp(-100)9 P; y8 M& j% H. U4 K  z4 c
  15. (-0.78125, 7)
    ( x8 d" N( n0 |6 z
  16. >>> math.frexp(100)
    8 B: @4 R- M+ x5 z8 c
  17. (0.78125, 7)
复制代码
# j) G: h# h; D( K2 y7 j6 Y+ F
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
  J. v3 W# i# Y% L# M
  1. #对迭代器里的每个元素进行求和操作
    8 L) o' W$ `$ |! }
  2. fsum(iterable)
    + G. ?2 Z2 c8 N
  3. Return an accurate floating point sum of values in the iterable.
    " q0 _* s  q( \" ~
  4. Assumes IEEE-754 floating point arithmetic.
    # R' [7 ?8 ^3 N. K# V/ e3 {* O
  5. >>> math.fsum([1,2,3,4]). c6 X. C1 N$ q* i" f' f$ Z6 k
  6. 10.0
    ! [/ r0 B2 i; A) P: J
  7. >>> math.fsum((1,2,3,4))
    2 [3 p1 @: {+ j9 ?! n
  8. 10.06 Z# N) D+ w. I
  9. >>> math.fsum((-1,-2,-3,-4))
    + ^/ Q0 g- g" p
  10. -10.06 @0 W7 _  A/ i
  11. >>> math.fsum([-1,-2,-3,-4])( \/ _, C5 s& Q# E& O- L. V
  12. -10.0
复制代码
) b! q% S1 m  _: _, w& U2 r: s
math.gcd(x,y)  返回x和y的最大公约数" h7 H/ _' d% r
  1. #返回x和y的最大公约数6 C! N3 Y: d8 h( F/ y- T  |4 W
  2. gcd(x, y) -> int5 Y  Q; L% t2 \
  3. greatest common divisor of x and y
    : k6 F/ A& o2 D- W, Y! x
  4. >>> math.gcd(8,6)
    / x! W) I2 a1 I* q4 o
  5. 2
    9 O3 }4 `6 u7 u& n. v
  6. >>> math.gcd(40,20)5 w+ K' h6 T4 c7 Z
  7. 20
    : I' w% z0 l: E6 }0 g1 `$ f
  8. >>> math.gcd(8,12)8 s! p4 @4 @8 I
  9. 4
复制代码
3 v: y: |8 _# Y, Z* j5 [. v
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False4 f9 h, E, r. R/ F1 ?+ x" }0 ]
  1. #得到(x**2+y**2),平方的值7 v- x% v7 i4 C% a* S; X
  2. hypot(x, y)
    - D0 O; g, X& z' Y- Y
  3. Return the Euclidean distance, sqrt(x*x + y*y)./ v' F% y9 T) R2 ?( F# y
  4. >>> math.hypot(3,4)3 G/ U* V; z- M$ L( z) v8 f
  5. 5.0/ h* w$ P) ?" ?7 }
  6. >>> math.hypot(6,8)
    ! S& R& p8 S1 e7 z
  7. 10.0
复制代码
1 n( w- N) g1 c5 l$ `
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
# s* c% w( S3 y1 ~* V0 S" O7 Y
  1. #如果x是不是无穷大的数字,则返回True,否则返回False+ ]0 i/ b/ h$ h6 I% }) B
  2. isfinite(x) -> bool
    ( p" m) F: {2 t( Y* m
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.4 o! |7 ]" H1 L% `* o1 \
  4. >>> math.isfinite(100)! [4 A3 i2 t3 Y! n
  5. True
    5 X- L% H1 M) G: `0 O: y
  6. >>> math.isfinite(0); i+ D, Z/ e$ @
  7. True& w+ Y3 g3 o& h1 s
  8. >>> math.isfinite(0.1)
    1 I' F# u7 |, S% D; }, c1 n7 M
  9. True
    & f0 o: d2 {( q
  10. >>> math.isfinite("a")$ j# |; |: b. T" S9 m6 u1 o5 `- ^
  11. >>> math.isfinite(0.0001)
    7 t' J: o. ?- }* y4 o
  12. True
复制代码

' @$ U. y& F; u: ]math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
2 e( z" j  ?/ b% Y, j& w; t
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    $ ~0 Y$ ]0 z- C) D0 s2 _
  2. isinf(x) -> bool( {! u# x5 Q7 U5 y* d
  3. Return True if x is a positive or negative infinity, and False otherwise.; h1 k( i3 y- b  p1 e3 T# t: m- C% R
  4. >>> math.isinf(234)' C+ ^6 q  J% M% S+ n+ [8 b
  5. False
    9 C2 P( i; ~- ^1 [0 B+ {. A
  6. >>> math.isinf(0.1)' j1 U1 y7 ]- O0 n3 L
  7. False
复制代码

  _: f1 M! z; V1 W; Zmath.isnan(x)  如果x不是数字True,否则返回False
& e: Q, T; u3 A; [2 b' Z# H
  1. #如果x不是数字True,否则返回False
    ) X$ t4 Y9 e* @/ X& X6 E8 K* }5 S
  2. isnan(x) -> bool
    - n( @* i& t. l$ V
  3. Return True if x is a NaN (not a number), and False otherwise.2 b' Z: \- |# g1 w
  4. >>> math.isnan(23)
    1 A7 e" q' {7 I8 p. a3 H8 }5 A
  5. False
    $ c& v" B! ~( J: ?( h
  6. >>> math.isnan(0.01)
    1 d5 ]8 y; |" o6 V
  7. False
复制代码
# {" t( r$ f  C, A1 K: h3 @
math.ldexp(x,i)  返回x*(2**i)的值
! b: O) R- a+ P: K- e- s! f
  1. #返回x*(2**i)的值* m! h/ U  F9 s' W* y! C
  2. ldexp(x, i)' E3 o+ j- J# S4 g5 d& B
  3. Return x * (2**i).
    1 ]& H" Q) Z) n* u6 H6 T5 H% N, T
  4. >>> math.ldexp(5,5)3 L+ `$ B9 n& o$ G+ U: M
  5. 160.0
    & j* }( {1 O2 [8 c  Y
  6. >>> math.ldexp(3,5)
    - e7 [2 ?) j! V+ j- `4 U; L
  7. 96.0
复制代码
* m" m+ b: G' D! k, m5 z9 r
math.log10(x)  返回x的以10为底的对数
! x0 O. X% A: ^1 w2 S
  1. #返回x的以10为底的对数
    ' {# l0 L: t" K6 C5 A
  2. log10(x)
    - M4 c8 t3 p/ }3 f% G. \/ d
  3. Return the base 10 logarithm of x.
    : x* z9 {; ]& k) z/ s
  4. >>> math.log10(10)$ A/ ^( I4 @6 }4 o* s6 s
  5. 1.0
    ; ?: N$ |/ m( ]2 Y
  6. >>> math.log10(100)/ ~4 x; Z0 r7 ^8 p! ~" N/ I% y
  7. 2.0
    1 x9 i/ P. @) O' @! ^4 z( l
  8. #即10的1.3次方的结果为20
    & M& \" v: V, B1 {, s
  9. >>> math.log10(20)  Z3 ^0 e/ t6 P8 }% B) F+ l" T
  10. 1.3010299956639813
复制代码
9 B0 ], ^! V6 x, ^/ e0 W; k/ B6 L
math.log1p(x)  返回x+1的自然对数(基数为e)的值
: C) o- @- H( u; Z0 u" r. @
  1. #返回x+1的自然对数(基数为e)的值
    " ?* e& U# f, u1 \
  2. log1p(x): z" H$ H1 l; v/ Z" e
  3. Return the natural logarithm of 1+x (base e).
    0 F& H1 j$ t* A& Z
  4. The result is computed in a way which is accurate for x near zero.
    ' ], q4 K0 Y6 P3 W5 f, a9 ]
  5. >>> math.log(10)/ _! C: [! \( @3 e% X) F! X6 m
  6. 2.302585092994046
    7 u* v+ d) z5 Z
  7. >>> math.log1p(10)7 s% A* L" B; m& d3 n; p
  8. 2.3978952727983707% ~( X* a: F' l* k$ T
  9. >>> math.log(11)7 S$ B! U2 `5 W# ~
  10. 2.3978952727983707
复制代码

& T1 V: o0 t1 Omath.log2(x)  返回x的基2对数
- I+ U1 U% X3 W+ s9 Q# \1 Z
  1. #返回x的基2对数
    1 ^: e, v0 `* z& q! x$ S+ S/ t
  2. log2(x)* C' V6 a9 h/ E6 y$ N% M
  3. Return the base 2 logarithm of x.3 h  w8 r6 D7 O0 e3 d6 K9 A
  4. >>> math.log2(32)
    " T0 s' B! [# g) j' O5 H# r$ h
  5. 5.0/ O- H- t6 S+ t4 I. ^) B
  6. >>> math.log2(20)/ F6 r; w) |' Z+ _- G
  7. 4.321928094887363$ b# p' P. l" w; l! h% @7 F
  8. >>> math.log2(16)
    6 P* r* }5 S$ l# i5 \6 F
  9. 4.0
复制代码

* `! a$ I5 `$ fmath.modf(x)  返回由x的小数部分和整数部分组成的元组
+ o2 o, n" S* d2 u, A& t; b1 h
  1. #返回由x的小数部分和整数部分组成的元组
    + N' k- `; G$ |9 _. o
  2. modf(x)
    " Q9 H3 d' H3 e- C8 F
  3. Return the fractional and integer parts of x.  Both results carry the sign
    , U4 W- ^1 H( w: V/ V) g) ]
  4. of x and are floats.
    & x& J) }+ s$ @+ F. h/ g/ Y& ?9 ]
  5. >>> math.modf(math.pi)0 R7 q# H% }5 G0 ^+ B3 @
  6. (0.14159265358979312, 3.0)
    * C* w: l2 `8 V2 C
  7. >>> math.modf(12.34), {* j. r5 V" }
  8. (0.33999999999999986, 12.0)
复制代码
' @$ l) z: C3 ~6 Z5 R% P5 u/ a
math.sqrt(x)  求x的平方根# p! f' e8 K' }" d$ ?% B
  1. #求x的平方根) o# f8 o* w& H2 V* J
  2. sqrt(x)
    & q+ U" [( L) d# u5 g7 ?* R
  3. Return the square root of x.
    % W4 X4 D7 ]1 ]7 w  h- @0 r
  4. >>> math.sqrt(100)3 Z; K$ g1 K" i' C9 w6 @1 I
  5. 10.0$ L9 l; V# h0 g2 ]; @' B. ~
  6. >>> math.sqrt(16)1 Y* @1 Z, G- R" W3 Q& h% b
  7. 4.0
    + [, [2 m2 f9 B. T7 R2 R
  8. >>> math.sqrt(20)
    / }  l" E: N! G# u# }: M. Q/ c
  9. 4.47213595499958
复制代码
  ]# I; M* z0 A, m1 x
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分* P. g. C3 k, }$ G/ s
  2. trunc(x:Real) -> Integral& r, |5 i7 r. N- y- C1 m8 \0 w
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    + i8 [- u. {+ k7 T4 w' E
  4. >>> math.trunc(6.789)
    : Y3 t% F0 y5 g! C5 S
  5. 65 \4 i7 y; `  M$ w/ `% S
  6. >>> math.trunc(math.pi)' D3 U6 s% I6 l/ o. a2 j
  7. 3) E0 t" C) y$ j" t9 F
  8. >>> math.trunc(2.567)3 [. Y1 k8 n; H& e: v+ ~9 N; [
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-11 21:32 , Processed in 0.082005 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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