新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

2 I# N% |. R# Q  R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 Q" e  n6 i) R: f! ?% {- m: H) o1 f
( A7 t9 `: H. M4 [8 m
方法1- F! L$ ?! F0 f9 h5 ^& j/ C
  1. >>> import math  V0 Z/ Z$ T. Y. ]9 j. {& Y
  2. >>> math.sqrt(9): F& y, q* H7 i5 a5 ?1 Q
  3. 3.0
复制代码
方法2
' h$ X$ K- S7 H  b  e$ \
  1. >>> from math import sqrt- T9 h" T! F/ [
  2. >>> sqrt(9)0 Y: B- ~7 K) D. Z) Q  r# j+ P, L
  3. 3.0
复制代码

3 r- Z8 ]* O/ N0 R! x8 n% O  Q( m

/ t4 N: C" b. C" ^
math.e  表示一个常量2 Y* ?6 U' U/ K3 B5 Q4 [. b" q, D
  1. #表示一个常量
    8 h% K2 R; W& ]
  2. >>> math.e
    1 t3 l% p2 Z3 R
  3. 2.718281828459045
复制代码
. ^/ |8 ?5 {; f5 A- y. T5 @
math.pi  
数字常量,圆周率
+ ]6 {* B; C  K# o3 T/ k- [* [! @1 s
  1. #数字常量,圆周率
    6 n, f! R, H0 z. I4 X9 H
  2. >>> print(math.pi)
      a3 |3 J9 x' m& ]8 Q
  3. 3.141592653589793
复制代码

  B" q- e7 n8 ^math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
: X; U; W" U! }& Y9 M3 X
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    3 ?! b; N# `& R: C9 u3 [
  2. ceil(x)3 [/ x- y/ F% D) s
  3. Return the ceiling of x as an int.
    % ]' h4 ?9 l6 f, L/ m! `, {$ j
  4. This is the smallest integral value >= x.
    4 A- ^6 u' J# f$ A2 s4 o

  5. 0 V9 Y: F/ Z. Q, h# p- N8 @
  6. >>> math.ceil(4.01): h( i. r; P" T3 x0 c
  7. 5/ `8 Z2 Z" \% b
  8. >>> math.ceil(4.99)) X2 O& C( Y0 p: L9 {
  9. 5
    4 ^3 O# |  `1 B: L
  10. >>> math.ceil(-3.99); {7 W' t5 ^6 t+ o$ D8 h2 U0 t
  11. -3, F1 ?, S+ n' V+ c- l
  12. >>> math.ceil(-3.01)
    9 `3 P  i: _" d' \, w) Y
  13. -3
复制代码

& C* q* E9 f$ ]/ omath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 w+ _! E1 P% U4 c' P* l3 ?
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    3 a6 a, d' p: O& L* n9 l4 n! P
  2. floor(x)! o9 y& A( v! f2 s9 p
  3. Return the floor of x as an int.
    9 d! n% \; ?8 K' B! t$ G
  4. This is the largest integral value <= x.4 P0 M) O/ a, o# E8 ~7 \
  5. >>> math.floor(4.1)5 o) }- ^+ q, D6 V
  6. 4
    + B; W! r6 A3 t) m+ }
  7. >>> math.floor(4.999)8 o; z- A" W9 ?" Z% ?1 H
  8. 42 J) r: o' u* o8 i5 i3 W3 f
  9. >>> math.floor(-4.999)
    6 b% q9 F' ?6 r( s, H
  10. -50 p  x" N: k% A2 l" Z2 {  q
  11. >>> math.floor(-4.01)4 c7 K! S; v3 W' w6 Z% z: s/ |
  12. -5
复制代码
9 M  |/ X  X. C# o
math.pow(x,y)  返回x的y次方,即x**y/ \$ Z9 j8 }1 v# @% U
  1. #返回x的y次方,即x**y6 K/ M+ T% \/ M% v4 s' B9 Q& }: ]" L
  2. pow(x, y)
    7 e: R' Q! `% S. x* R! _! [
  3. Return x**y (x to the power of y).. d$ `$ d2 W8 |# O. B
  4. >>> math.pow(3,4)9 O* T* J0 @( ]( o' m) g0 j3 L2 p' l
  5. 81.07 T: Q. x2 e5 \) L* a2 o! k
  6. >>> 3 x  D: g# k2 ~" X4 }
  7. >>> math.pow(2,7)' }6 E, [) H+ n
  8. 128.0
复制代码

6 N* N7 Q# ?$ a+ m/ L3 q# C, ^math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 `( Z$ @% h4 y& `) g9 c! e
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), P0 m3 F8 _+ w- c$ _
  2. log(x[, base])5 i4 x5 _6 ^2 N. k+ F
  3. Return the logarithm of x to the given base.
    5 _( b% i% Q3 A- F- N7 G
  4. If the base not specified, returns the natural logarithm (base e) of x.
    0 G, [# o: D0 x; ^% v5 ^# C
  5. >>> math.log(10)
    ( I/ g* [- R! F% F  y. ^
  6. 2.302585092994046
    7 C9 p) f$ `+ a$ H4 j) d
  7. >>> math.log(11)
    - W! A/ X, o5 Y. k0 m$ ^! n
  8. 2.3978952727983707* t" o5 G1 B  ?8 `8 a1 [
  9. >>> math.log(20)% D" w4 ^  s3 W% o) o
  10. 2.995732273553991
复制代码

9 P* G! O; g' J. r" z+ kmath.sin(x)  求x(x为弧度)的正弦值$ N' \( I: _- }# P2 Y  v$ f
  1. #求x(x为弧度)的正弦值
    ! f: t/ V2 T" F: a& C- ^
  2. sin(x)" N! I9 T5 K: d
  3. Return the sine of x (measured in radians).+ f7 x) v( ]: c9 O- ]1 x4 T- i
  4. >>> math.sin(math.pi/4)* u7 ^' C6 X8 w. n
  5. 0.7071067811865475
      k% q& @' Q; C9 R) D7 ~1 H2 }' e
  6. >>> math.sin(math.pi/2)
    - H5 N) R1 p% k. ~& A/ ^2 g3 j
  7. 1.0
    . J/ q9 [9 X$ l) M3 W7 x
  8. >>> math.sin(math.pi/3): }( E" `/ O% w9 j) U5 M
  9. 0.8660254037844386
复制代码

9 B) Y- e) ^; A! N; Y3 N  {5 D% v/ Xmath.cos(x)  求x的余弦,x必须是弧度3 L: k+ E( H+ H* h
  1. #求x的余弦,x必须是弧度
    * L  {) F- L% d" x8 L
  2. cos(x)$ g3 F; b9 \% x2 P) w& o
  3. Return the cosine of x (measured in radians).: \7 O9 |. w5 M+ Z
  4. #math.pi/4表示弧度,转换成角度为45度
    " K% A$ r3 U# N4 k% j
  5. >>> math.cos(math.pi/4)% I9 E, e! e6 c
  6. 0.7071067811865476" N! A; N8 G( T
  7. math.pi/3表示弧度,转换成角度为60度% u" W! t2 e, ^7 l4 {9 f8 J+ ~. |
  8. >>> math.cos(math.pi/3)
    " R0 A4 {; X6 Q1 [
  9. 0.5000000000000001; V0 P& W( V  i) Z2 X; R1 j% r8 m
  10. math.pi/6表示弧度,转换成角度为30度
    % t6 X8 p) |' i" ^  D
  11. >>> math.cos(math.pi/6)4 T' n- J& D) v0 v
  12. 0.8660254037844387
复制代码
/ v( }, [  m- c1 {9 b$ P4 p
math.tan(x)  返回x(x为弧度)的正切值/ ?( u  V! ?" h. {& ^4 C
  1. #返回x(x为弧度)的正切值
    " x8 |( A3 g3 E' I9 ]/ \
  2. tan(x)
    1 \3 p4 r2 X  y( S2 v
  3. Return the tangent of x (measured in radians).
    * L% U# z; t! b( A
  4. >>> math.tan(math.pi/4)( d3 i- |6 Y4 R" g7 S
  5. 0.99999999999999993 o( c' B+ l" v) h/ x, B
  6. >>> math.tan(math.pi/6)- m, C5 Z" o( s
  7. 0.5773502691896257
    - W( ?# H5 \7 g9 H: J: E' x1 t, I
  8. >>> math.tan(math.pi/3)% h( J% n0 f5 B( x
  9. 1.7320508075688767
复制代码
4 x' A6 S6 h9 x; O
math.degrees(x)  把x从弧度转换成角度& Z$ l2 a; l, s8 b! Y
  1. #把x从弧度转换成角度2 r- l' n+ \) t& Y: `% n
  2. degrees(x)
    4 a( ^: @- |3 d+ o4 x
  3. Convert angle x from radians to degrees.
    , Y! V6 Z2 e  e7 @- ], s
  4. * g: c1 D2 u8 v
  5. >>> math.degrees(math.pi/4)
    - T2 I0 A  h  a( ~" R( H0 A
  6. 45.0
    1 I: R6 @, B5 k; h8 B
  7. >>> math.degrees(math.pi)1 M9 l5 l6 J+ ^- a$ J5 q
  8. 180.0/ z. C; f3 F/ N: v& I# w
  9. >>> math.degrees(math.pi/6)
    8 ?  J" J: L4 E
  10. 29.999999999999996
    # C" e% g$ Y5 E6 }- X
  11. >>> math.degrees(math.pi/3); H& K1 q$ e  v3 F( W3 k: z6 n
  12. 59.99999999999999
复制代码
; S5 N  A) S0 Q$ U/ U8 Z
math.radians(x)  把角度x转换成弧度
) A0 ~$ @+ d! p0 o! N% s
  1. #把角度x转换成弧度
    ( @& Q+ L4 Z1 ^0 \
  2. radians(x)
    6 k0 w4 D' u2 S9 M- `" ]1 m& C
  3. Convert angle x from degrees to radians.
    - i3 T$ L" V3 @3 C( v4 ^! g
  4. >>> math.radians(45)+ A' z  o( E$ k  \$ Q4 X+ c
  5. 0.7853981633974483
    1 A; q& o6 f7 N! F9 Z( |
  6. >>> math.radians(60)
    / x. `4 w: Q" D7 ?& r: R+ y" x* d6 b
  7. 1.0471975511965976
复制代码
4 y1 v# w$ \6 L6 x  v
math.copysign(x,y)  把y的正负号加到x前面,可以使用0- x6 m; _+ K$ i1 Q4 {: {+ f3 ~
  1. #把y的正负号加到x前面,可以使用0
    : R0 D( o8 j- n, X; V) X
  2. copysign(x, y)/ L$ y+ ?( ~. u3 X5 n! \: X
  3. Return a float with the magnitude (absolute value) of x but the sign
    " f' o. ~0 U- b& N
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) + K. P; I  I; K" B5 U" X
  5. returns -1.0.4 `5 _9 k7 f2 {% d) v0 ~6 n. |
  6. # a  F6 Y, h8 C# }- A1 {
  7. >>> math.copysign(2,3)% x! f3 o" g- a. N
  8. 2.0
    * k5 `3 T& f4 G" r, d' f/ q+ D
  9. >>> math.copysign(2,-3)
    # g8 u) \! j8 i
  10. -2.0
    ; ^5 [3 `; ^1 c  ]* N
  11. >>> math.copysign(3,8)
    * J$ p' F; R1 ^
  12. 3.00 B) A1 o: S. C: ]5 B. G( t
  13. >>> math.copysign(3,-8)" x$ m( Z, a0 X5 [+ I3 A
  14. -3.0
复制代码
- I& E/ j1 i  O* w! V  y7 b* |5 m
math.exp(x)  返回math.e,也就是2.71828的x次方1 @7 W. N: y# b8 v4 ^- t
  1. #返回math.e,也就是2.71828的x次方
      L/ V, ]4 E# q  t6 C/ x$ w7 f
  2. exp(x)
    0 O& U* f5 `5 ^. L6 `0 @6 ~
  3. Return e raised to the power of x.' h$ ?0 y% v" e) t

  4. : v6 {* D: q% r+ L0 S. s, u
  5. >>> math.exp(1)) K% N. G# {0 t; q  Z& O
  6. 2.718281828459045
    7 q9 c& L! q% l/ i$ r
  7. >>> math.exp(2)
    % u' s8 N9 O( o. }0 {3 l
  8. 7.38905609893065: ?/ O: e) `+ x: }8 d& U- S
  9. >>> math.exp(3)
    3 c& e) a, V8 N" q
  10. 20.085536923187668
复制代码
" y2 D7 Z+ D3 ~8 V& j  }6 m
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 T! O. g+ e' v! R8 z# x# p
  1. #返回math.e的x(其值为2.71828)次方的值减1
    , r- L, v/ B; U" R% {+ i
  2. expm1(x)0 `7 w) L. H5 `/ p& J2 Z# Z
  3. Return exp(x)-1.
    4 C; y: c; t0 m; c1 q! W
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    7 d; W  j# ]% e

  5. " g" N. t- J1 l. v
  6. >>> math.expm1(1)+ e9 p. z% t' o2 D3 w) X) d
  7. 1.718281828459045
    + [3 x# _  R% x  Y
  8. >>> math.expm1(2), e9 b: z5 i' n6 Q6 x& l
  9. 6.38905609893065
    & b. O4 x( S# G& m4 g) V
  10. >>> math.expm1(3)
    # a+ _1 }% c: r8 q# H
  11. 19.085536923187668
复制代码
, B; p9 \+ h+ X; a5 g% L
math.fabs(x)  返回x的绝对值) H3 E4 P' F7 Q9 L! P: B
  1. #返回x的绝对值) W0 j# C: c. B5 E+ T9 b! H" k& _
  2. fabs(x), p, L8 p. Q( |( `
  3. Return the absolute value of the float x.
    6 S- H3 E3 Z% {! O  g6 ]
  4. 5 \) o: V  M1 J. T/ w/ a$ P3 O
  5. >>> math.fabs(-0.003)
    - R0 v- m; J$ b3 _+ L0 f
  6. 0.003; T) v+ }1 ?) u0 }+ T0 A: u9 U
  7. >>> math.fabs(-110)1 d1 _5 _% e1 \1 T7 G1 Z: @& F: M; P. X
  8. 110.0- h$ n/ |) T) l8 L% y2 v+ W; [
  9. >>> math.fabs(100)
    - y8 B% ?# |' G6 L1 L
  10. 100.0
复制代码
: E; a, `2 Z2 m% N  H
math.factorial(x)  取x的阶乘的值
$ U) ?; l; ~5 @5 u+ n2 m' x' w
  1. #取x的阶乘的值
    6 ~: @7 V2 G/ E( x
  2. factorial(x) -> Integral$ S  N* s" m. M3 u1 X9 Y; ]8 p
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    ( O, V8 _1 \9 ^1 H+ Y
  4. >>> math.factorial(1)
    % [' O- v) F( d8 @
  5. 1
    , S' H+ V# G+ G/ ~1 \. b; D3 d8 S
  6. >>> math.factorial(2)/ E8 O, t1 H+ B) p0 _
  7. 2
    : L* f! `& h8 D7 x
  8. >>> math.factorial(3)
    7 A+ a) X. ?7 ]
  9. 6; f  H0 x7 D) o% O* U; I# [
  10. >>> math.factorial(5)
    $ \. k7 m0 C' M
  11. 120
    4 X, X' `+ I6 ], T3 i
  12. >>> math.factorial(10)0 }6 L3 a5 Y. ?! c' C. \
  13. 3628800
复制代码

# B" M& @# f, M; smath.fmod(x,y)  得到x/y的余数,其值是一个浮点数0 {3 F- Q) j3 W
  1. #得到x/y的余数,其值是一个浮点数
    ( b& A' T& h' `$ Z
  2. fmod(x, y)' V) P6 a. \  B; |6 z
  3. Return fmod(x, y), according to platform C.  x % y may differ.5 H$ E% j/ i! u1 t9 \9 g$ x. c
  4. >>> math.fmod(20,3)
    7 ]2 p9 e4 p; q
  5. 2.08 [7 }) `- F! P4 y. p; Y, Q
  6. >>> math.fmod(20,7), ^; m& f/ Q' P
  7. 6.0
复制代码
0 Q% F: U: }8 p
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 l  i+ y) J$ v6 I% Y8 C$ p
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,! l2 U* j  q1 l! Q5 `$ \
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 q5 o1 q1 X3 I
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ) x, z$ v8 [  [. |% z
  4. frexp(x)
    - }4 B$ ]. ], R$ y3 O  D
  5. Return the mantissa and exponent of x, as pair (m, e).( U! y2 K. ^( O# K( y. O
  6. m is a float and e is an int, such that x = m * 2.**e.
    ( e2 ^, _. H, N! `
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.0 d9 U* Q) |4 U: F6 g
  8. >>> math.frexp(10)
    ; I# d" d0 P- U9 ?% S
  9. (0.625, 4)) ~7 k" w  s. [8 Y8 i2 n- E! F& o
  10. >>> math.frexp(75)8 `1 S, X& R! R5 i& c& @
  11. (0.5859375, 7)9 G7 [+ D2 |& h6 S9 @- Y  }# k
  12. >>> math.frexp(-40)3 Y4 m& H8 q8 L8 w
  13. (-0.625, 6)' g9 U4 m4 D  |
  14. >>> math.frexp(-100)
    & y" h+ a/ d3 A/ I: f* I
  15. (-0.78125, 7). l7 @7 M* C8 L& c
  16. >>> math.frexp(100)" j8 e/ U! x  s" P9 X8 m( w0 P5 W
  17. (0.78125, 7)
复制代码
. }( L1 h( Y" Q5 V' L
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列9 W: N% v3 r$ P) u* E: G8 Y
  1. #对迭代器里的每个元素进行求和操作
    8 j* r5 E1 m  F$ n5 @$ o! y
  2. fsum(iterable)
    ; M6 Z5 g" B2 Y* z; q* ?* g9 j3 J: i8 Y
  3. Return an accurate floating point sum of values in the iterable.
    ' b" ^1 N+ O1 C6 K- Y
  4. Assumes IEEE-754 floating point arithmetic.
    . A, G5 A6 T. b: i) Z
  5. >>> math.fsum([1,2,3,4])
    3 }" m; g; Z. b$ w2 H9 ^" I' N
  6. 10.0
    * c/ x! u3 |  `5 L! T! j
  7. >>> math.fsum((1,2,3,4))
    ; z0 Q- {$ x0 w
  8. 10.0" Z3 u# v% q* g) r
  9. >>> math.fsum((-1,-2,-3,-4))
    ) v6 [% R+ I+ w' {0 i
  10. -10.0
    ) x9 k% h' S  G# z2 ^: C/ u
  11. >>> math.fsum([-1,-2,-3,-4])$ i- J8 ?9 z8 `' V8 N9 r" Z8 n  L
  12. -10.0
复制代码
$ f4 @8 @! n! w6 ~6 W
math.gcd(x,y)  返回x和y的最大公约数( E" m: V8 O9 x" b
  1. #返回x和y的最大公约数
    / @+ @7 A, A1 t* U  ^* j
  2. gcd(x, y) -> int
    5 L9 p3 Y+ J' a+ D; J
  3. greatest common divisor of x and y
    , x/ N' {% U) r4 g
  4. >>> math.gcd(8,6)+ q. ^! `% Z! j1 U7 j" I
  5. 20 e, t3 H$ V* B9 U( G
  6. >>> math.gcd(40,20)/ V7 N" P/ y7 U8 W% ?2 t9 O7 t
  7. 20
    $ _1 V: Q; n- |' }% x4 Z
  8. >>> math.gcd(8,12)* m. w' y5 b8 w# M, A2 f( z
  9. 4
复制代码
" _7 `( \' }6 W6 |
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False4 D1 H2 M% M4 a& V# ~# X: }
  1. #得到(x**2+y**2),平方的值
    2 F0 ?: X' [; R0 d$ y" o/ G2 _
  2. hypot(x, y): U) t, f2 o7 x: Z
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ( O5 m$ N+ K% p  r+ M: r
  4. >>> math.hypot(3,4)% s& B  W# B( ]" m0 T4 J8 h
  5. 5.0
    6 b& \1 V! Q! g( w" k) S; ?
  6. >>> math.hypot(6,8)
    8 a8 v# M3 L1 V5 @* m
  7. 10.0
复制代码
0 j" U2 @1 r! P( W1 p
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
! i5 ]% V6 [3 {  q
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ) v: \4 o5 l, z" H8 b6 q
  2. isfinite(x) -> bool
    - r6 @0 Z- \- v: w
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    " f: ~# w) D# z* {; A% e' t
  4. >>> math.isfinite(100)6 Z; j) d! e, T: d  P
  5. True
    / d& N' K* Z9 U) K  T/ L
  6. >>> math.isfinite(0)
    3 p8 P# {4 r8 A$ F
  7. True( T* l7 S6 k$ L! ^1 H8 h
  8. >>> math.isfinite(0.1)# b1 C& C9 p6 d1 U
  9. True" W% \4 H( X0 Q! Q$ F. `
  10. >>> math.isfinite("a")
    $ G1 @1 O+ v- r" G" k1 h
  11. >>> math.isfinite(0.0001)
    7 f% E7 V: |& x2 I( t5 T7 v
  12. True
复制代码
  A2 H2 M: g( J0 Z: \
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False4 w/ f: @4 X6 t* u4 i( K" a& U
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    1 h, |9 i1 X# b$ i" m
  2. isinf(x) -> bool3 e% m) e/ j9 O
  3. Return True if x is a positive or negative infinity, and False otherwise.
    1 r3 U+ k* F2 ?7 p# A
  4. >>> math.isinf(234)( J9 w8 p5 w% V, ?
  5. False
    ; j" n# ]0 N# N; Q8 Y, N
  6. >>> math.isinf(0.1)
    $ F+ z9 ^  e+ K7 J3 m4 ^
  7. False
复制代码

" D/ O- M  z# K. D& C. F' T# emath.isnan(x)  如果x不是数字True,否则返回False
& s* J& q/ j+ Q/ z+ g" u1 d0 u
  1. #如果x不是数字True,否则返回False
    - g0 S/ ^( J4 }3 U( F' R# ~) s
  2. isnan(x) -> bool4 @. U/ E! v% U5 U# D" G9 L1 j! J
  3. Return True if x is a NaN (not a number), and False otherwise.
    ) e. y: s7 U, A, o3 p! ]
  4. >>> math.isnan(23)
    . {. ~6 \. v. Q% x1 S
  5. False" E8 G. P" `1 w# s2 f; Y  f* p
  6. >>> math.isnan(0.01)' o4 N) ], ?7 ?% O$ X1 K
  7. False
复制代码

) x7 m7 L; x& I( o' u4 imath.ldexp(x,i)  返回x*(2**i)的值; c5 M. @' U, d, `
  1. #返回x*(2**i)的值
    + `$ i8 A' X2 h7 V- |
  2. ldexp(x, i)1 b$ y4 p1 w8 N0 q
  3. Return x * (2**i).
    - |# M8 L. h" ]. S6 q9 L
  4. >>> math.ldexp(5,5)
    1 z: {* z1 o7 [  E* x, U0 \4 p% K
  5. 160.0
    , Y3 A2 m8 y) @3 D# [
  6. >>> math.ldexp(3,5)3 F1 W7 W" A! M+ Q9 [! s
  7. 96.0
复制代码
6 ]# j& A% D  T
math.log10(x)  返回x的以10为底的对数( ^1 [# y/ ]2 s- P% t  v9 ~  a
  1. #返回x的以10为底的对数
    4 F. m1 X$ J3 ^1 @( G" [8 Y& W
  2. log10(x)1 y( _% \/ u# U: ]. j
  3. Return the base 10 logarithm of x.! Q# g' H( q* W! V( C) ^+ t
  4. >>> math.log10(10)' x4 E7 ^6 b7 E1 b- U) e
  5. 1.0
    ; ?2 \. a; ], A3 X# q* N7 a4 O
  6. >>> math.log10(100)
    ; `2 p7 R/ Q5 m. S) {! j( U4 M
  7. 2.0
    ! C' J5 I, H! d0 f! p) [$ y- _
  8. #即10的1.3次方的结果为204 {4 H. x+ t( c1 T
  9. >>> math.log10(20)) L7 e% |2 m3 F' d" [
  10. 1.3010299956639813
复制代码

# ^3 w  ?: a  ~9 Y& s( h' Kmath.log1p(x)  返回x+1的自然对数(基数为e)的值
( y; k' b6 Y. b% y7 p- N/ \+ z
  1. #返回x+1的自然对数(基数为e)的值
    + f3 x, F3 k, M; A$ x7 k7 v
  2. log1p(x)& F( W; |0 W) {3 `5 V/ C* f
  3. Return the natural logarithm of 1+x (base e).
    ) Y5 e; S) ]/ |2 q# h
  4. The result is computed in a way which is accurate for x near zero.
    " z5 g3 L; U+ H% c; f2 H4 ^
  5. >>> math.log(10)
    " S) o! q* `" y/ ?6 w, |, q
  6. 2.302585092994046
    / E! O7 v6 f: T3 c# Q+ v$ r6 a
  7. >>> math.log1p(10)
    % u$ O: S* A, |. K+ r* G! s9 n
  8. 2.3978952727983707) B. a( b5 W3 F' d) I
  9. >>> math.log(11)- \+ H5 ?) U$ [8 i4 n6 p
  10. 2.3978952727983707
复制代码

; i8 `% r; p4 m& U) [8 gmath.log2(x)  返回x的基2对数
/ P$ R: c& ^, w1 D; i% A! x3 p$ O' ~
  1. #返回x的基2对数5 W* Q4 g" s- f5 O# W, p9 G, W3 }
  2. log2(x)
    9 A. l, k/ L& K, `% Q! n
  3. Return the base 2 logarithm of x., ]9 ]" F) w+ m8 x/ P* Z( v6 j0 w
  4. >>> math.log2(32)% R9 l! }* ?1 W/ c
  5. 5.0
    ' I* j: b( X6 \
  6. >>> math.log2(20)
    9 A5 o7 l8 k4 W& w, |$ \/ ?$ o' C
  7. 4.321928094887363
    2 K4 M* J: {$ h
  8. >>> math.log2(16)
      _% k8 J9 q1 ?+ b. O7 O
  9. 4.0
复制代码
! V3 @+ d" x- P+ ~* M
math.modf(x)  返回由x的小数部分和整数部分组成的元组
' Q5 G5 Q4 X) O( O" U4 t% Q
  1. #返回由x的小数部分和整数部分组成的元组" l, f7 h( S3 t6 z9 V
  2. modf(x)
    # ^* }/ G2 M# ?/ F  a
  3. Return the fractional and integer parts of x.  Both results carry the sign  T3 P% e8 c! \7 ]! a
  4. of x and are floats.
    % V3 {- p( l9 }' u, l+ e6 Q7 u3 l
  5. >>> math.modf(math.pi)6 I/ T! V2 @. d+ A& \* C% l6 ~2 o
  6. (0.14159265358979312, 3.0)( S- B, Z  U7 ^+ I' Q. j* g
  7. >>> math.modf(12.34)  \2 V  i! ~. ~
  8. (0.33999999999999986, 12.0)
复制代码

& C! k1 x) m; Mmath.sqrt(x)  求x的平方根
3 B; k9 o! |4 e0 B  P6 ^
  1. #求x的平方根
      y3 |- F( V" Q+ g
  2. sqrt(x)
    3 E- X& C* R/ x
  3. Return the square root of x.
    . k9 q5 S6 {/ Z9 V6 [& W0 D
  4. >>> math.sqrt(100)1 X. w/ X9 `. o! q8 ~" G
  5. 10.0
    ! |( c4 \( b5 T; M+ V$ I$ {
  6. >>> math.sqrt(16)
    ; w. a$ b* o& K0 Y  ~; E
  7. 4.0
    6 o4 C9 [. R4 L5 I! Y  w
  8. >>> math.sqrt(20): K( i! V, B' D7 i; b. o' m6 \
  9. 4.47213595499958
复制代码
6 i7 `6 A3 ]" w0 N6 o) L5 ~
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    , @8 X" W2 s8 g) n  S: L
  2. trunc(x:Real) -> Integral
    + a; b( \- h% [# f: ?. o# m) [
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.6 i2 L7 _$ n* W0 C5 h
  4. >>> math.trunc(6.789)
    1 |& h" j$ H, a/ _
  5. 6
    ) a- @0 a8 M" q4 _
  6. >>> math.trunc(math.pi)) k  p. l- ?  o2 @! R$ J! h) J& M
  7. 3
    $ ]4 r8 t1 k9 {9 }0 L' D
  8. >>> math.trunc(2.567)8 D3 k6 d/ \) c3 I; y' Y) j0 F
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-5-23 15:46 , Processed in 0.085441 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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