新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
7 B3 d$ p# v6 k9 j1 Q& S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 B! e$ S  g' E  ~4 C

* b9 Q: s0 w6 V0 N) d方法1# f; r; B6 W+ v# ^
  1. >>> import math  _3 _: t# \7 l+ G; [# v# Z
  2. >>> math.sqrt(9)/ C5 K/ I2 g) Z) Z3 _. p, p+ T
  3. 3.0
复制代码
方法2) f7 X- o7 T. B& R4 K( l
  1. >>> from math import sqrt0 B3 N  F) }% _: Y+ Q& V# X3 @; o
  2. >>> sqrt(9)
    4 y3 ~! h4 y  o6 F& {
  3. 3.0
复制代码

% \# w: s0 v4 M
3 {# K, t, l' ]+ ]* D- @9 _
math.e  表示一个常量
0 r  H1 |% D0 }) q
  1. #表示一个常量
    " K. h( R! l$ e
  2. >>> math.e4 A& m$ F# d& V5 F/ f& c3 M
  3. 2.718281828459045
复制代码
, y2 s$ M/ Z* K
math.pi  
数字常量,圆周率

$ N( L+ N( T1 I( ?6 i; Z0 S
  1. #数字常量,圆周率
    7 @$ K* r+ W$ o, N. E  h: R5 q
  2. >>> print(math.pi)
    0 e" i3 w0 o. v4 z# L
  3. 3.141592653589793
复制代码

# O+ V0 X3 O. b! ~  Nmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

; P) L5 Z& ~4 }
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    . W. ]  V0 d% w* |0 k# x/ U7 a
  2. ceil(x). @5 B" k! ~4 P# X! a1 p
  3. Return the ceiling of x as an int.
    / }' W& F* t9 ^: D) H) @2 z! Y
  4. This is the smallest integral value >= x.
    1 M4 l9 M& v, ^; k: ]" s, i

  5. 3 D! U/ o% s, u: M$ g7 u6 C& l
  6. >>> math.ceil(4.01)2 |% m' h8 Y) B6 a3 Q/ u3 _- w% |
  7. 52 b, y0 R' ?+ _2 ~7 e! [% \" V+ i
  8. >>> math.ceil(4.99)
    # o) \5 X# p, z0 l) }
  9. 5
    / {9 S: I- J0 j
  10. >>> math.ceil(-3.99)
    " q8 R/ l' u% N& T5 t+ U4 P' s7 p/ ^
  11. -3
    ' Y, ]( `( e9 _# R4 x! A
  12. >>> math.ceil(-3.01)  b! ?5 k. F1 ]' ?1 k
  13. -3
复制代码
7 s! C0 n- [; D
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% c0 m+ \0 x% ~
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ c# O, Z0 Q, a
  2. floor(x)
    5 }! n$ V* F  g4 H
  3. Return the floor of x as an int.$ w% t  u3 I- I5 b
  4. This is the largest integral value <= x.& E; ]' s! r1 G6 \4 V! P$ ^
  5. >>> math.floor(4.1)9 W* |! _" i6 u  B
  6. 4, M! m7 u. z! i0 [, z
  7. >>> math.floor(4.999)
    0 e0 ?7 V/ T. p4 d+ _" y( L
  8. 4) }4 @/ u. j* d5 L( N2 p3 Y
  9. >>> math.floor(-4.999)' Z( V! i- r) ?: U
  10. -5
    8 E5 P( ^; g/ s3 N8 ?
  11. >>> math.floor(-4.01)* R) U6 X0 U$ I7 a
  12. -5
复制代码

2 J2 Q' W- i9 H& a8 f2 Amath.pow(x,y)  返回x的y次方,即x**y5 k% r: c7 i  \, j: \
  1. #返回x的y次方,即x**y
    ; d; Z$ X4 S9 u+ N. O. p/ c% `
  2. pow(x, y)* f6 y2 {( o$ d6 g+ {  `
  3. Return x**y (x to the power of y).
    - i* r6 l/ E- y" w$ o  c
  4. >>> math.pow(3,4)  \, R) p- J8 {+ }2 v
  5. 81.03 }% [! b+ q: d4 o0 `
  6. >>>
    ' |0 @+ g/ y; r$ u5 ^) D" L1 _
  7. >>> math.pow(2,7)
    ; o% `9 s6 z* j
  8. 128.0
复制代码

2 G' t7 J0 `8 h7 S5 b% Jmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ ?9 M' g3 d6 V
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    ) }) x: z3 L+ @" y" j4 I4 \
  2. log(x[, base])
    ! h$ L3 V5 w: f; t: ]
  3. Return the logarithm of x to the given base.5 }' z( ^( U3 t+ Y. `4 g0 L* T* M
  4. If the base not specified, returns the natural logarithm (base e) of x.
    , g0 w8 X7 B8 J6 o  U
  5. >>> math.log(10)
      \$ n1 @1 q9 }% N& a+ f
  6. 2.302585092994046- y: K% Z& h2 m2 n& G) a# ?7 `
  7. >>> math.log(11)0 c  B9 c3 N' c% S
  8. 2.3978952727983707& ~8 k7 j" v* x: b. j, h$ W. b, t  n1 i
  9. >>> math.log(20)! {7 p6 E9 h" y* W8 f" X
  10. 2.995732273553991
复制代码

- f' K2 V1 r5 }6 _math.sin(x)  求x(x为弧度)的正弦值# X  P; o, c3 M7 E4 w
  1. #求x(x为弧度)的正弦值
    7 M; t" Z* m: H' G
  2. sin(x)- c: ]0 @3 y9 S
  3. Return the sine of x (measured in radians).6 e( z* }: l% L0 s0 r
  4. >>> math.sin(math.pi/4)
    2 }2 m$ K8 d7 {2 Z
  5. 0.70710678118654754 [3 p' U' B/ m& E
  6. >>> math.sin(math.pi/2)
    . L4 L; G5 j& ?, H4 b/ n6 N
  7. 1.0
    ! ~0 @4 R) U9 u: x9 @, o
  8. >>> math.sin(math.pi/3): e/ _( Y1 L) A; g
  9. 0.8660254037844386
复制代码

( Y# j% ^9 ^! S1 V( r# Wmath.cos(x)  求x的余弦,x必须是弧度
: p6 h* y1 V( Q
  1. #求x的余弦,x必须是弧度- ~; y. |- U. q) ^
  2. cos(x)3 v: }  j! {3 H$ z5 R) N
  3. Return the cosine of x (measured in radians).) C* w, ^  ~( w' ?# h
  4. #math.pi/4表示弧度,转换成角度为45度3 u' m) ~" H* F% Z3 s
  5. >>> math.cos(math.pi/4)+ S: J$ m; f/ k/ R* c
  6. 0.7071067811865476* K" b/ a( ~2 F( G, r/ b# L
  7. math.pi/3表示弧度,转换成角度为60度
    $ L4 q+ H! O5 B5 X
  8. >>> math.cos(math.pi/3)
    ' ]& N' u; `9 }- \1 G6 {! Y
  9. 0.50000000000000012 D) J' d2 f4 r* ^
  10. math.pi/6表示弧度,转换成角度为30度; C. o3 r, E3 v4 V* o1 _
  11. >>> math.cos(math.pi/6)
    ! l8 [  q" o! u& U* T' k8 R
  12. 0.8660254037844387
复制代码
; s* o  c* ~0 \& i8 X8 X8 s
math.tan(x)  返回x(x为弧度)的正切值* N. S' F$ `0 X3 W5 ]0 |' n
  1. #返回x(x为弧度)的正切值
    3 C- d) ~7 [6 u3 m! f
  2. tan(x)% Y" m' T; M  O' y8 k! R8 \# [+ ?
  3. Return the tangent of x (measured in radians).
    # G2 \6 L, j9 z3 @7 W
  4. >>> math.tan(math.pi/4)& y% G7 K) y, ^- ?9 f2 S
  5. 0.9999999999999999& }  w5 r0 r4 Z
  6. >>> math.tan(math.pi/6)
    ) b2 d3 o( D0 ~! n" j
  7. 0.5773502691896257+ c. @" h8 G/ ?, E; i" S1 B" ~
  8. >>> math.tan(math.pi/3)9 x" A/ t! A) s& Q' c$ W  h% U: C
  9. 1.7320508075688767
复制代码
" [6 S+ B" n8 F$ _
math.degrees(x)  把x从弧度转换成角度
4 X6 g9 P# Z) P( u) X* b
  1. #把x从弧度转换成角度
    8 a* s- \7 B) ~% z
  2. degrees(x)1 d1 N6 v( W# {8 _! Q& L
  3. Convert angle x from radians to degrees.) d" k0 G% |4 x  R: K! M0 w& m" v
  4. - l5 ?! W* o; U
  5. >>> math.degrees(math.pi/4)! p( l3 _7 S( E, o9 Z4 u2 A/ Y9 @
  6. 45.0
    ! G) y; \# K' I* o+ e
  7. >>> math.degrees(math.pi)! J$ w1 K/ P4 R2 I8 @/ [* Z, p, `' r7 x
  8. 180.0
    ! c: Z5 U1 M  j& P
  9. >>> math.degrees(math.pi/6)
    ) d* G0 D, p0 v
  10. 29.999999999999996! D8 e, w3 x5 b
  11. >>> math.degrees(math.pi/3)2 s- h/ l. G2 V, X/ u* v
  12. 59.99999999999999
复制代码

( z  U$ Y& L! b. g+ Dmath.radians(x)  把角度x转换成弧度% A- Z7 D% j" Q2 f& x3 {3 i& v6 J/ I
  1. #把角度x转换成弧度
    0 Z. M; F. ^6 ]! b
  2. radians(x); s7 @2 o  d- |% E2 ]
  3. Convert angle x from degrees to radians.
    0 }- L7 q, W4 t# o' D$ r- o2 b: Y/ F
  4. >>> math.radians(45)
    / @: z; A" O; m- I! `
  5. 0.78539816339744839 {. n6 |$ {0 q, h1 N; _
  6. >>> math.radians(60)
      Q8 N2 {) |- }& I0 j
  7. 1.0471975511965976
复制代码
/ h/ v2 Q# S2 y4 k2 [% B# i2 q5 u
math.copysign(x,y)  把y的正负号加到x前面,可以使用0: A. _" M& S! s  \
  1. #把y的正负号加到x前面,可以使用0# L% A- N5 u  b% B( R* J2 n! d
  2. copysign(x, y)
    + s4 d  Z6 F3 ]4 N
  3. Return a float with the magnitude (absolute value) of x but the sign $ O5 r+ C: U: m. _. N' [3 i
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    4 b# [+ E+ h. _+ O+ K# M/ d
  5. returns -1.0.
    2 v$ i0 }& V2 A% `
  6. 8 ?" q/ U% o: V2 t. k# h
  7. >>> math.copysign(2,3)
    ! V' p0 j% Z  [7 u, M: o7 ^
  8. 2.0
    + H$ a( ]7 `. u. _
  9. >>> math.copysign(2,-3)
    & H( H6 m$ m' a  t
  10. -2.0
    9 |+ T( x! R- S* Y/ [# ~
  11. >>> math.copysign(3,8)
    . \' o- t1 s' z: M
  12. 3.0
    ; B8 X& z4 e! s
  13. >>> math.copysign(3,-8)9 q. e/ h% R( {; m* |: M/ w
  14. -3.0
复制代码
+ h# b) L; F) P7 x) y, L0 L4 `  s
math.exp(x)  返回math.e,也就是2.71828的x次方0 w% p6 R7 I: H$ }6 l" m% L
  1. #返回math.e,也就是2.71828的x次方
    9 X" @' G( P8 z$ x
  2. exp(x)
    2 r5 B5 k. g& @/ N/ E2 A4 ]1 M# U
  3. Return e raised to the power of x.9 b1 K9 V. e! B! r; y
  4. / \* C: M' |7 W. q' Y
  5. >>> math.exp(1)+ S0 P* ?' V5 d: S+ U* |+ f: L
  6. 2.718281828459045; w/ i4 J, O/ g; h7 ~8 L  r& O/ _
  7. >>> math.exp(2)1 I& D; R' T: a9 i# E4 l+ P
  8. 7.389056098930658 b$ ^, I* K6 t; N( ]& f
  9. >>> math.exp(3)* |( R9 j4 W- `2 g# W. |
  10. 20.085536923187668
复制代码
8 }1 {: {& [9 G" N# X( l! R
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1. N  y, t4 ?+ X
  1. #返回math.e的x(其值为2.71828)次方的值减1' ]' c* N. [- u1 b7 {
  2. expm1(x)+ d4 M" W" z6 `* Q5 F
  3. Return exp(x)-1." |, u4 U* ^* O( b: M
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ X$ e$ ]; U) S. _" n% d$ W2 \
  5. / r5 @) d& A/ v) d) Y" |
  6. >>> math.expm1(1)8 s; r) l) |2 M1 K& E! c+ z+ q
  7. 1.718281828459045
    2 F2 `8 O/ Z4 B- S" ]
  8. >>> math.expm1(2)& U5 V" b8 v' \
  9. 6.38905609893065, ~3 H% L. n7 B: |
  10. >>> math.expm1(3)+ y, V7 }  G  i$ B: [6 h1 o- ]
  11. 19.085536923187668
复制代码

% _0 y4 w* K' _) L- Q9 F4 n( x, N2 Gmath.fabs(x)  返回x的绝对值! ^5 H0 ^# {9 G- S2 }: l
  1. #返回x的绝对值
    + [: T" h) }, m( y
  2. fabs(x)
      f( @( Z; \6 k
  3. Return the absolute value of the float x.
    3 l3 y6 b' N9 {1 U) r4 l/ z% }

  4. ; k" N4 Z0 U) i' e! k0 V. _% a6 V
  5. >>> math.fabs(-0.003)
    5 d% ~' c& W) i% C9 E( g$ V
  6. 0.003: i7 ]( [7 o+ F
  7. >>> math.fabs(-110)
    % U# B% u' X9 L2 |7 B- N) q( l
  8. 110.0
    5 T: o- s+ w! M: H7 q) B9 B, h. F' K( }
  9. >>> math.fabs(100)
    : v% ~' C) G# E/ ?+ \
  10. 100.0
复制代码

* C  ]# b/ o) Wmath.factorial(x)  取x的阶乘的值
; r& C, Q& l4 o( Q5 r* ?: d8 t4 S
  1. #取x的阶乘的值; p  S: E9 B( \8 V6 U: s
  2. factorial(x) -> Integral
    ' @* L/ i2 V5 c/ N0 n! x
  3. Find x!. Raise a ValueError if x is negative or non-integral.1 v* k+ _8 |5 z! V" M/ B9 ?2 g
  4. >>> math.factorial(1)& Q9 Z6 A$ C( [6 j( \
  5. 1
    , ]5 M+ f5 Y5 ~
  6. >>> math.factorial(2)
    5 H2 H+ H3 b& z3 T9 p0 L( M) [
  7. 2& e$ ~- o" \7 i! g& e
  8. >>> math.factorial(3)5 c: t8 x5 G8 a9 k
  9. 68 i/ b. k3 g2 M3 G' o
  10. >>> math.factorial(5)
    / W( }$ d9 T6 y( [
  11. 120
    6 S* N* L6 q9 p7 b
  12. >>> math.factorial(10)
    7 M# ?( K$ l2 Y& k9 W5 G# j
  13. 3628800
复制代码

3 ^, G, g0 b' q/ b0 n) Bmath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
& P. {: y! ~6 {! L( G/ `) l
  1. #得到x/y的余数,其值是一个浮点数
    * I9 e; e5 z$ x2 }
  2. fmod(x, y)6 o  p% O$ L& Z9 k7 n$ j
  3. Return fmod(x, y), according to platform C.  x % y may differ.  [, }) ]' \+ H4 R% P
  4. >>> math.fmod(20,3)
    9 [1 H& L7 o8 v9 W
  5. 2.0/ t3 c1 d* ^! N% I- Z) T9 _( E
  6. >>> math.fmod(20,7)
    . u, u" h; h* D1 `, W- \& ^3 r: m
  7. 6.0
复制代码
' h3 Q/ M4 Q% A1 K( O! z1 `" f0 _
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 Z) i. o& _8 ]# x
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ( I( u' k$ V6 _6 \) d; h
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 s4 k. I& e. k$ R3 p, P& e
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" X. o' ]4 n6 \2 O
  4. frexp(x)
    3 I* y; B# }& Z: v3 b) L; V& l6 l
  5. Return the mantissa and exponent of x, as pair (m, e).: o$ y( e9 R, Z: y& v; q$ p
  6. m is a float and e is an int, such that x = m * 2.**e.
    1 ^3 S' k1 U5 ^. t
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    & q5 K. _! [) Z. r  G; ~" ]! ?& t. Q
  8. >>> math.frexp(10)! P! f$ G5 n. Q* ~( D$ F4 f
  9. (0.625, 4)1 G! a. m4 R) X0 _# [+ F" ]2 n: B
  10. >>> math.frexp(75)% X4 Z. `! P) H1 P" r- n' F- d$ J
  11. (0.5859375, 7)
    , Z7 [2 X8 Y$ q
  12. >>> math.frexp(-40)
    1 j. S, O, \* b, g, r% v
  13. (-0.625, 6)" _  V8 B; H) d! N
  14. >>> math.frexp(-100)$ d. f& o( [- t: S
  15. (-0.78125, 7)
    ) q& |$ z$ b  h
  16. >>> math.frexp(100)
    # `1 \7 U/ Z; [' w' n
  17. (0.78125, 7)
复制代码

7 l. {- e" _  V+ ?5 P5 Wmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列! {; W7 E/ c$ u8 \. q6 u! U3 J
  1. #对迭代器里的每个元素进行求和操作
    : g" [6 \9 A: j0 C2 e
  2. fsum(iterable)
    ' N( @; a8 I" G  W
  3. Return an accurate floating point sum of values in the iterable.1 u$ e9 r3 L; l! H5 Y' w- ~9 |
  4. Assumes IEEE-754 floating point arithmetic.
    6 i, O- e/ K% l
  5. >>> math.fsum([1,2,3,4])
    . |. m! I: ^4 R3 A2 F8 t
  6. 10.01 e4 B2 y2 O% y. {* B& E2 X" W
  7. >>> math.fsum((1,2,3,4)), ^9 ~+ L9 K1 k  g, R/ |
  8. 10.0
    ; d* ~- a: z- b9 Z" {
  9. >>> math.fsum((-1,-2,-3,-4))
    / g4 O  Y7 f. y/ N; ^. s" v
  10. -10.0! q  w5 \# n: i3 r: S
  11. >>> math.fsum([-1,-2,-3,-4])
    2 ^  Z4 c8 _' g! O  }
  12. -10.0
复制代码

: c& y9 G1 f0 E6 ymath.gcd(x,y)  返回x和y的最大公约数& \" z1 c3 U) c; G  X9 s
  1. #返回x和y的最大公约数4 R8 b1 h2 z) l4 G! @+ V
  2. gcd(x, y) -> int& Y- N' g: `% F+ Z3 E" ]
  3. greatest common divisor of x and y6 {- x$ H% @7 @0 d( a+ I
  4. >>> math.gcd(8,6)8 i, a. _7 o$ B- ?6 C  U
  5. 2) E& |# E1 b+ P) ~8 c6 |
  6. >>> math.gcd(40,20). t7 q: `6 ?( v3 x
  7. 206 H- C! v) ^* _8 w
  8. >>> math.gcd(8,12)2 N0 v$ Y( v. [: Z* K" o
  9. 4
复制代码
# U- d% Z6 M0 [7 N$ F7 I
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
; D" ~& [* {$ @
  1. #得到(x**2+y**2),平方的值' c( l7 G1 o7 j# E1 t6 n
  2. hypot(x, y)
    6 @, q0 b. B; k9 R+ e' w
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    $ c: E6 P8 z" ?7 [( f* g$ h4 n& t" E
  4. >>> math.hypot(3,4)
    . \& W$ w7 U5 \- y$ p$ ?- D: @
  5. 5.0( n1 ]- W5 f% d
  6. >>> math.hypot(6,8)
    ! s3 L% P  N5 R
  7. 10.0
复制代码

, z5 k0 U  t8 L; B# ?3 @7 Kmath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False7 R3 J0 _# ?) A% w5 Z6 B0 T
  1. #如果x是不是无穷大的数字,则返回True,否则返回False% _  h, E. \& S' d( U
  2. isfinite(x) -> bool; X# Z( o2 Z% f5 d% ?1 n
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    % e5 D, c1 @* o
  4. >>> math.isfinite(100)
    ! I4 @6 k' Q  ?$ q
  5. True
    ' V$ S" A: O) w2 w
  6. >>> math.isfinite(0)
    2 K1 X2 Z& z! |9 V( D
  7. True7 \% P7 F2 ]" x  c. }( e
  8. >>> math.isfinite(0.1)
    7 T) v8 |2 H8 G+ X
  9. True* U, r( M0 P' u! y0 B# O) c, @
  10. >>> math.isfinite("a")
    % S/ I8 t( X- I/ Z! W% e' K. Z9 j
  11. >>> math.isfinite(0.0001)
    1 Z: p0 t9 N0 @- ^* b
  12. True
复制代码

2 ]; Q" s8 T2 O2 Amath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
9 N' h0 W- H9 r0 F3 L( {2 j
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    0 Q5 U  L- t( S9 y! ?
  2. isinf(x) -> bool
    3 ]. r0 L! |5 d) V
  3. Return True if x is a positive or negative infinity, and False otherwise.8 p) p4 m! H; H2 R' q5 K
  4. >>> math.isinf(234)" J( G% H. Z% h
  5. False; P1 Z( C9 A( Y
  6. >>> math.isinf(0.1)
    ; Q! p  g$ V2 }8 ?# g) i7 {1 }
  7. False
复制代码

5 \9 G) ]$ z6 x6 z. P) Umath.isnan(x)  如果x不是数字True,否则返回False- {; a; \9 C: K! _% E  R
  1. #如果x不是数字True,否则返回False
    $ S; H/ N! e6 b0 M. m* o
  2. isnan(x) -> bool* t+ L, M4 `, }5 s: s
  3. Return True if x is a NaN (not a number), and False otherwise.
    . a7 M3 ~6 q* O& W
  4. >>> math.isnan(23)" h1 y- S1 o. A
  5. False
    ) i! b/ h8 a* D* ~" I
  6. >>> math.isnan(0.01)
    ; ]7 |% n4 v! L3 Y( q  n
  7. False
复制代码
  o  [4 W- R; j/ S' c' p0 ~! `8 S
math.ldexp(x,i)  返回x*(2**i)的值/ k: W& D8 z$ U' D
  1. #返回x*(2**i)的值
    2 s- G4 Q2 J5 @
  2. ldexp(x, i); R. `* R+ m. x% ^0 y& c
  3. Return x * (2**i).8 _- t8 ?# d+ X7 c  ]& M; K! y
  4. >>> math.ldexp(5,5), i" w# ~. G. L3 i5 }( f
  5. 160.0* ^; F' E9 l7 A4 J+ O/ f* q7 G) j: c6 `
  6. >>> math.ldexp(3,5)
    9 P* ^) H% {3 u- L! j
  7. 96.0
复制代码
" g, M+ x6 X. f! `" A" H
math.log10(x)  返回x的以10为底的对数
/ p& q$ Y5 O# l% h+ ]9 a1 U1 w
  1. #返回x的以10为底的对数, N" }. c5 @1 t
  2. log10(x)
    0 o$ i, h. B3 h! G$ \5 q0 m9 f- W
  3. Return the base 10 logarithm of x.+ Z! H! ~# w% F1 ]
  4. >>> math.log10(10)
    ( S2 a: p1 u5 M/ p- _) R4 O, f
  5. 1.0$ d0 Z0 Z; s) q, N' P% c6 |6 B
  6. >>> math.log10(100)
    3 O! l1 j; f0 F9 ^1 [
  7. 2.0! Q1 a& R$ L9 k& f
  8. #即10的1.3次方的结果为208 u: r3 v* _  F: B/ P' i
  9. >>> math.log10(20)5 }& G/ P, o5 J
  10. 1.3010299956639813
复制代码
- t& D" l2 S% f" x  C! ^% p* k
math.log1p(x)  返回x+1的自然对数(基数为e)的值
1 [$ t, w5 Q* e' m
  1. #返回x+1的自然对数(基数为e)的值
    3 g! T/ a- W! r) b
  2. log1p(x)# i9 [0 A! X6 g) P& s
  3. Return the natural logarithm of 1+x (base e).
    * f; Z& P/ W  R
  4. The result is computed in a way which is accurate for x near zero.1 I2 w( {; {9 o. u
  5. >>> math.log(10)
    * F; ^) s4 E& i3 p% ~
  6. 2.302585092994046. S+ _8 Z+ k2 n- z/ P% U5 ]9 n# G
  7. >>> math.log1p(10): E7 d" Y/ x4 X5 Q  y* O
  8. 2.3978952727983707
    4 w/ r6 r0 `) n9 t3 t
  9. >>> math.log(11)
    * ^1 r2 d: ~" w5 o* g0 t
  10. 2.3978952727983707
复制代码
- O* {! m  g4 k0 A7 F, O  M4 `% G
math.log2(x)  返回x的基2对数# A# f$ N) {( k
  1. #返回x的基2对数
    : n: c: Y) X% {: b3 q( g- B: w9 K
  2. log2(x)' [: P' m- O( I8 M. N
  3. Return the base 2 logarithm of x.
    * @9 ~. Y  N! z# a2 L( F
  4. >>> math.log2(32)/ l8 ^: f2 o/ o/ L$ e* k
  5. 5.0; a- t! S4 R5 ^; H* b6 M" _% H' O
  6. >>> math.log2(20)7 E$ j/ O6 H" w" P4 x
  7. 4.321928094887363% O  U) B& H1 B* N& X
  8. >>> math.log2(16)
    & Y! w" e, Z6 _* x$ [* ]
  9. 4.0
复制代码
$ T5 [6 F! F$ O0 x2 |! P- S# ?
math.modf(x)  返回由x的小数部分和整数部分组成的元组* C+ i6 x7 A( w+ l6 G
  1. #返回由x的小数部分和整数部分组成的元组) K3 N- K7 t! X& d+ @
  2. modf(x)* \( H$ r* }  z+ H) K
  3. Return the fractional and integer parts of x.  Both results carry the sign
    2 ?7 Y' N+ v4 ?9 y$ y  E. Z/ P: f: R$ T
  4. of x and are floats.
    : u+ M- }) @" a* r# [0 ?2 V0 v" c/ c
  5. >>> math.modf(math.pi)
    ( ?8 y) d7 _; C9 S3 f  v5 h& q. M) P
  6. (0.14159265358979312, 3.0)
    $ v$ c0 V' C+ m+ w. {  N) F
  7. >>> math.modf(12.34)2 \; U$ `1 ]( T, D- H2 t* V
  8. (0.33999999999999986, 12.0)
复制代码
' S1 ~2 K8 Z! `' B
math.sqrt(x)  求x的平方根
( `& q! |. O5 w9 o) X+ R1 K
  1. #求x的平方根+ K: s; G% d7 {* k( N7 o
  2. sqrt(x)7 s- j' x1 u, w+ q
  3. Return the square root of x.
    # `& L: u* u, D3 W5 M( `9 d
  4. >>> math.sqrt(100)) R: R7 \, Y) \7 _0 U  G1 s: b5 ~
  5. 10.0( w9 G( I! f; a' F
  6. >>> math.sqrt(16)- y9 H% ]; s; `% d5 x
  7. 4.0
    6 M2 v, L7 y. V8 E: q
  8. >>> math.sqrt(20)
    & G2 x7 Y3 r6 x# p; ~0 |6 Z7 V
  9. 4.47213595499958
复制代码

$ C3 S* G8 m# W, ~2 Ymath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    * x$ A2 h5 M* j) ^/ o& t
  2. trunc(x:Real) -> Integral9 @6 L, R3 A  w7 Q) w% I9 X
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.  _  w' r) z9 f/ n
  4. >>> math.trunc(6.789)
    ) J1 g# G6 e3 w6 O  k) \, x6 P# I* D
  5. 6) K: M+ L5 o: l2 D
  6. >>> math.trunc(math.pi)6 r# f# ~1 n- B( n6 V( F
  7. 3
    ! F0 I3 k" T1 E5 G5 D' I, Y
  8. >>> math.trunc(2.567)* i' k! Y( V+ m# T0 n& {
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

新大榭七周年,感谢由您!

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

GMT+8, 2025-11-26 17:35 , Processed in 0.110020 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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