新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
/ ~4 u. B, |( [: P
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
+ M" d( b9 c+ i1 w+ f* ?5 k
3 G1 w" x' n5 n0 F
方法1
8 @+ ~0 k9 \3 r: w. F1 [5 e
  1. >>> import math# x6 v; L' S# e' y% G
  2. >>> math.sqrt(9)
    2 m' A" @3 n9 }( z/ W7 |: K4 D
  3. 3.0
复制代码
方法2
4 D' O& H& t/ ?7 K' D  ?
  1. >>> from math import sqrt
    . l- ?7 ~2 J+ H2 }
  2. >>> sqrt(9)' Y) n+ d- q% q" U
  3. 3.0
复制代码

; h9 X6 l, t+ t  B' M# ]6 a

4 }$ t4 O4 {* l" ?+ B! [2 p' y
math.e  表示一个常量
; S; `1 [5 W  n
  1. #表示一个常量
    6 ]/ M+ B7 G/ W3 a) n
  2. >>> math.e
    8 ~0 T# Z: [. V/ d' i2 j' E7 V8 m
  3. 2.718281828459045
复制代码

) {: S  V7 `' q. `9 nmath.pi  
数字常量,圆周率
5 e0 y4 N: o- j& t' ]
  1. #数字常量,圆周率
    9 x2 m4 W" \, s8 N- @
  2. >>> print(math.pi)2 e* G7 a* p0 F  U! M% _4 ]
  3. 3.141592653589793
复制代码

, d5 P- _. W6 s/ Vmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

( W% p' J( w  q" }
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x8 {, l9 U6 k2 V
  2. ceil(x)8 F6 S( n+ d7 x
  3. Return the ceiling of x as an int.# \9 V: g2 `$ o! }/ f/ G' x
  4. This is the smallest integral value >= x.6 B, @; @3 Y7 y# T' I, b

  5. + r/ }3 u1 T; p' P
  6. >>> math.ceil(4.01)
    + I8 `  a3 z/ Z
  7. 5
    ) C) r- n& t3 c
  8. >>> math.ceil(4.99)$ a  c  D$ ], w
  9. 59 C7 Q; x2 A# n( V+ u& U7 T
  10. >>> math.ceil(-3.99)
    ) w2 L( x: L0 c' V
  11. -3. F3 {" e% E  X1 [; g% b
  12. >>> math.ceil(-3.01)/ r  g3 S. F1 j& G$ ?- }: ]% h
  13. -3
复制代码
1 x; L6 \- K# Z- M
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
6 M# t. N' x5 y
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身# A1 _4 `1 k: D
  2. floor(x)( p9 s: v; Z0 c, x8 `3 s
  3. Return the floor of x as an int.8 r9 c1 P3 d2 K* H, h
  4. This is the largest integral value <= x.6 n7 j1 `5 d" a
  5. >>> math.floor(4.1)( w$ p* h: z% f. w& ?8 L
  6. 4. u9 `9 D( N0 o
  7. >>> math.floor(4.999)8 u6 B6 q. ]$ D+ ]! {$ h7 L
  8. 4
    - E* c- J8 K9 n$ y
  9. >>> math.floor(-4.999)3 T" f) K7 q. _# J% j1 `; u
  10. -5
    ! V# X* S4 a5 a: ^+ t9 l3 y! }
  11. >>> math.floor(-4.01)
    : a; ^  {+ {3 f- h+ L! s7 D0 {! E7 s
  12. -5
复制代码
# n$ X! H0 k1 Y& M/ N; I5 g& ^0 o
math.pow(x,y)  返回x的y次方,即x**y! Q: O, X% }, L- k! Q, X; }
  1. #返回x的y次方,即x**y
    " \( m6 I# ^! r! ]
  2. pow(x, y)! L5 K7 [) g. |/ J- k0 ?
  3. Return x**y (x to the power of y).+ d& P: e. S- [
  4. >>> math.pow(3,4): o9 R! o# X* j
  5. 81.0/ p, _( U, a5 V/ D# S; z9 G) Z
  6. >>> 1 J) Y; [# }, R
  7. >>> math.pow(2,7)
    7 v/ ^% X5 k5 G! G
  8. 128.0
复制代码

: X: |/ p# R' M: g5 M6 nmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 O! T' n6 C/ _) S6 ~9 b
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)% o* H% b9 A$ s/ r3 x
  2. log(x[, base])
    + B  V4 A; x: y# b& _* {
  3. Return the logarithm of x to the given base.
    1 j( _/ e3 X4 n5 ^+ l6 i
  4. If the base not specified, returns the natural logarithm (base e) of x.
    - I) {# U5 u! y. J/ _
  5. >>> math.log(10)6 ~3 ~9 h; F1 ?4 Q; p
  6. 2.302585092994046, ]- |1 V# c2 [
  7. >>> math.log(11)
    1 _9 D6 N$ m; I" q% B
  8. 2.3978952727983707
    $ X' u) [( l2 j  ?. p: K
  9. >>> math.log(20)( Q9 e. V- J9 U) h$ u0 v
  10. 2.995732273553991
复制代码

2 S) _+ j# @6 E- T' t) Umath.sin(x)  求x(x为弧度)的正弦值0 E, b  A* |6 m; F: ?
  1. #求x(x为弧度)的正弦值
    ; K% I: B% W  I3 E7 N: f
  2. sin(x)
    4 b& F& Q) K0 B
  3. Return the sine of x (measured in radians).2 x/ d, q  H( a' W* |9 B/ O8 `7 f
  4. >>> math.sin(math.pi/4)
    ' B5 }, V# z" R2 Q- X. h5 |
  5. 0.7071067811865475% }% w. ^0 e  L2 c  [2 v
  6. >>> math.sin(math.pi/2)& N0 f6 S! b5 `% D
  7. 1.04 ^4 J# }" C( x7 f9 l1 ?# G
  8. >>> math.sin(math.pi/3)+ D) I/ E# [, s# o
  9. 0.8660254037844386
复制代码

! e2 Z1 g0 v% @; @$ v5 umath.cos(x)  求x的余弦,x必须是弧度0 B( G+ ~0 B, F7 M) l: r# q
  1. #求x的余弦,x必须是弧度& G0 W9 s7 t2 L, `% H. K
  2. cos(x)" p' d$ b# I% X- k. N8 k. `
  3. Return the cosine of x (measured in radians).* I: Z) n0 L+ K& z
  4. #math.pi/4表示弧度,转换成角度为45度' o" q( C0 d- w# N. Y- I. U
  5. >>> math.cos(math.pi/4)/ C6 H" E- i, P) t2 G# X
  6. 0.7071067811865476
    0 ]+ ~/ H, y! {
  7. math.pi/3表示弧度,转换成角度为60度
    ' h& f. }* v; P5 @2 ?0 K' F8 H
  8. >>> math.cos(math.pi/3): `: o4 s7 B  ?" e) u
  9. 0.50000000000000010 [( d' z& v) c
  10. math.pi/6表示弧度,转换成角度为30度
    . a) E6 S% t2 ?1 K6 C8 x
  11. >>> math.cos(math.pi/6)
    7 b2 [3 L6 {2 c$ z' `3 u3 C
  12. 0.8660254037844387
复制代码

" y5 [/ P5 d- T+ t" C9 ^$ hmath.tan(x)  返回x(x为弧度)的正切值  _( a  F3 S# a
  1. #返回x(x为弧度)的正切值! a7 R- F% a4 ?
  2. tan(x)
    7 w/ \  f& @5 l; N' p" B. F( Z0 V
  3. Return the tangent of x (measured in radians).
    , d9 z+ d0 p/ A& E% u$ ?, p- C
  4. >>> math.tan(math.pi/4)
    - X. G0 p% y. T
  5. 0.99999999999999999 \+ {; R$ U& S/ b7 T5 R
  6. >>> math.tan(math.pi/6)
    5 B7 E+ x. {) k) B2 n1 y" {
  7. 0.5773502691896257
    ; @7 w2 }. E4 B1 z( q" \4 l
  8. >>> math.tan(math.pi/3)& i9 w* J' l) V  F, g
  9. 1.7320508075688767
复制代码

1 @3 b$ X2 I% D+ s* \math.degrees(x)  把x从弧度转换成角度
0 g( V7 N3 ^+ S3 [3 C& A: n6 R# |
  1. #把x从弧度转换成角度) \5 A" Q1 G1 g
  2. degrees(x)9 j+ Y! ~7 m) @6 `. D( G
  3. Convert angle x from radians to degrees.
    3 v+ i$ O" w2 Z& l7 l+ s
  4. : D7 d# ~. n: Q# ^4 s0 c
  5. >>> math.degrees(math.pi/4)
    4 J  s( N; Q7 h6 J" G! N  W7 d
  6. 45.0! v! P: p; U$ F  ~* J
  7. >>> math.degrees(math.pi)4 Y. e% ^! J8 d; ?0 x3 n4 v
  8. 180.0
    - b6 w$ N2 B3 X4 e
  9. >>> math.degrees(math.pi/6)
    + z- z9 q  t* p7 u
  10. 29.9999999999999962 ~: L2 n+ a9 j* q' F; l1 I( l
  11. >>> math.degrees(math.pi/3)+ U% N# k) P+ s2 a0 h% F8 N
  12. 59.99999999999999
复制代码
" t# M4 M2 z" n  ^
math.radians(x)  把角度x转换成弧度
: ]( q3 u6 s- u3 `- O6 g5 l! k
  1. #把角度x转换成弧度
    : V$ h0 W( j% L) W, N1 I
  2. radians(x)5 ^) W+ G7 {0 z7 [0 M/ j" ?
  3. Convert angle x from degrees to radians.
    , O; p& r$ W+ b! U3 @0 E
  4. >>> math.radians(45)! t) a! }, [2 d: E
  5. 0.7853981633974483
    . |5 G! O$ N- d* u
  6. >>> math.radians(60)
    ) y8 t7 F, X/ L5 t7 p
  7. 1.0471975511965976
复制代码
0 F( j/ h6 }; y" c3 G* c( H" N
math.copysign(x,y)  把y的正负号加到x前面,可以使用0$ q5 K" i2 a* d; T* `
  1. #把y的正负号加到x前面,可以使用06 C6 n5 }( X( i, p
  2. copysign(x, y); `. b5 d/ g# O3 P3 u& S+ L# o$ N
  3. Return a float with the magnitude (absolute value) of x but the sign + R! y* _" o: ^$ |" @
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ( z2 j1 m& u' E! t  Y# h: G
  5. returns -1.0.
    ( G7 F) F$ M& d1 y0 B# i

  6. ( |; c- D- L! d7 d+ g1 f
  7. >>> math.copysign(2,3)8 h& c1 N$ j( H+ [/ V4 N6 g
  8. 2.0
    . o# I" ?9 s3 M. g" q9 m0 G
  9. >>> math.copysign(2,-3)
    $ z: @+ O. R# g5 m
  10. -2.0
    7 E7 U1 V- B4 x! r% R$ q2 B
  11. >>> math.copysign(3,8)+ }1 }- {- \, H1 R/ S% Q/ k
  12. 3.0+ \/ \! e6 p/ ]' o7 {) }
  13. >>> math.copysign(3,-8)4 b: o8 d; R$ s, }, z
  14. -3.0
复制代码

! e2 v" t6 @! r# [# x( \3 Vmath.exp(x)  返回math.e,也就是2.71828的x次方/ }" ~( _& ~$ U5 `5 O$ @
  1. #返回math.e,也就是2.71828的x次方
    6 V5 d6 o5 j! b6 n9 K) r. H! t
  2. exp(x)6 W; N8 C/ J* i3 B% p( C( }0 V
  3. Return e raised to the power of x./ a& u7 f& D& q' S. k( N' z

  4. & Y8 J! q( p& X
  5. >>> math.exp(1)
    " F) }" ?2 T3 R+ n" {& h/ t  E7 F+ ]
  6. 2.718281828459045
    . \7 X1 A- o) [/ O6 `$ o$ E
  7. >>> math.exp(2): P; z- l0 e, ]9 Z
  8. 7.38905609893065
    8 C6 \0 Z: f( U! G
  9. >>> math.exp(3)
    6 j1 {2 ]4 F, W4 R) F
  10. 20.085536923187668
复制代码

1 ~* [; i" {/ u- v: O! b0 V; amath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减18 j( m+ ~. p! Z3 q, C0 i
  1. #返回math.e的x(其值为2.71828)次方的值减1& V! k% p# b1 I0 S
  2. expm1(x). O  S' ]9 V0 |* S2 p" z
  3. Return exp(x)-1.
    * ~2 h1 }, f' _. k4 R" Y1 ~0 K
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.' {. @0 B$ l9 L# N) l
  5. ' F' x! r9 |# y6 B2 ^
  6. >>> math.expm1(1)
    ; p& x: m8 ?  R  `4 T' ?* V
  7. 1.718281828459045' F5 U$ W8 ^  K" j
  8. >>> math.expm1(2)7 E: x/ G4 v7 E3 ?- M9 J9 s: }
  9. 6.38905609893065$ N) e" j" I* I$ {6 I
  10. >>> math.expm1(3)  J/ f8 l. M. o/ K0 @$ m8 H
  11. 19.085536923187668
复制代码

9 n9 a' m2 _9 zmath.fabs(x)  返回x的绝对值' N" g! }! {! g. {9 a! W
  1. #返回x的绝对值
    - e! V5 I! j; k; c4 j% Y* y
  2. fabs(x)6 f% Q) E0 I) b8 n3 b2 q9 t
  3. Return the absolute value of the float x.2 i2 Z  `. S7 Q6 `/ l2 z. e
  4. 4 y6 Y) T3 {3 R
  5. >>> math.fabs(-0.003)! ^$ ]7 F, c" w, ^1 i6 Y- o) i
  6. 0.003
    1 U' g( Y8 a, m7 {# `4 |
  7. >>> math.fabs(-110)
    # g3 G  n) k# M7 [! z
  8. 110.0
    ! T2 G( Q: `& F+ w6 w# D4 o( N
  9. >>> math.fabs(100)+ w% i9 I- ~: N! ^% Y1 ~: D
  10. 100.0
复制代码

& Z9 i' ?6 C" q0 O3 I; ~math.factorial(x)  取x的阶乘的值0 x1 i* [; B6 g) X
  1. #取x的阶乘的值7 D: E& [9 k: w6 k
  2. factorial(x) -> Integral
    7 G# t0 o" E) w$ ^6 e  y
  3. Find x!. Raise a ValueError if x is negative or non-integral.) j6 q* [6 O+ O/ B
  4. >>> math.factorial(1)" U# A5 ?0 \* p) z9 L  `* q4 E
  5. 1- s4 g5 ]' C1 ?  e& @
  6. >>> math.factorial(2)) ?6 p# u/ v* h' I
  7. 23 B  Y% n) f8 b
  8. >>> math.factorial(3)/ T5 S6 q5 a0 w. a, A# [% j5 N& W
  9. 6
    4 {# u6 Y8 [9 X) M2 {8 `
  10. >>> math.factorial(5)
    % c% Q- h8 o4 |6 T* B8 Q# s( Z
  11. 120: [) p2 y# k9 G7 A1 b- c# q
  12. >>> math.factorial(10)# C7 f! q$ U! \  z$ j' {+ T
  13. 3628800
复制代码

( }; M. h' h+ @math.fmod(x,y)  得到x/y的余数,其值是一个浮点数' ?1 w) m( Z6 \  v1 d
  1. #得到x/y的余数,其值是一个浮点数
    ( Y' Y: m, }8 ~  Y( V9 `% a6 L: y
  2. fmod(x, y)
    2 x+ Z8 P2 `  O' v0 w& g
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    3 y3 W0 ^# U# Q0 L9 [& ?
  4. >>> math.fmod(20,3)" t& T" y8 z; H! V3 S6 W; G
  5. 2.0
    : j$ L2 g* ~" @% {5 `0 S9 @9 P& t
  6. >>> math.fmod(20,7)7 a( {6 x' ?$ ^- G
  7. 6.0
复制代码

/ j2 N8 ?) S4 ]8 l# pmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围& w+ I1 \$ R- \+ X4 z2 R4 @9 ]( v
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    ' k9 H( O: }9 {& y9 _% G
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 b2 F, v  }4 E$ d. M4 I
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1: m$ ]% u! X( _; ]/ m
  4. frexp(x)
    % j/ e& F$ y: [, g. w( c. @7 ]
  5. Return the mantissa and exponent of x, as pair (m, e).
    5 Q, D1 A6 C3 C; b; {+ N
  6. m is a float and e is an int, such that x = m * 2.**e.& `- T2 Q& w# O; e; G, H. i& I9 y
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
      M2 h+ C9 J' G5 t& j7 q/ u2 M
  8. >>> math.frexp(10)9 R+ ]  s4 C4 |' D$ V- V
  9. (0.625, 4)4 {! V2 B3 ]( }, W  B) y) X
  10. >>> math.frexp(75)3 z% O+ }* J! t$ ^% j
  11. (0.5859375, 7)
    / T4 w$ p/ Z% ?* B
  12. >>> math.frexp(-40)0 c2 W) |  n5 Q" p2 N* U5 W% x9 q
  13. (-0.625, 6)
    " x, e% J9 N8 E+ y  V: s; I% K) l
  14. >>> math.frexp(-100)
    5 F$ I# z  B, V
  15. (-0.78125, 7)4 s3 v9 C% K; O* v5 Y( U( h
  16. >>> math.frexp(100)
    8 v; e" I; z2 r  H5 J- C# [
  17. (0.78125, 7)
复制代码
) N+ Q; u$ A- r; g; e
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
$ r$ p2 U& f+ @
  1. #对迭代器里的每个元素进行求和操作
    9 E4 {; h' O8 W5 r, H4 h/ q
  2. fsum(iterable)
    ' q! S: r+ L0 P  D+ W' J5 \2 F
  3. Return an accurate floating point sum of values in the iterable.
    + u( I1 W6 g# {4 K6 I: `( a+ I* T
  4. Assumes IEEE-754 floating point arithmetic." B) w6 a+ E4 ?8 b' @
  5. >>> math.fsum([1,2,3,4])
    ' {; F+ a3 W$ H* A+ Y* Y# \
  6. 10.0; ~* U7 s: u) `! H! l* U$ x( U
  7. >>> math.fsum((1,2,3,4))8 J7 J; S$ z$ x% D
  8. 10.0# ~" l3 E" ~+ t& f( ]& C' ^
  9. >>> math.fsum((-1,-2,-3,-4))
    % Q& C% p2 h& n! v
  10. -10.0
    1 y' x/ L* _0 l. R' q
  11. >>> math.fsum([-1,-2,-3,-4])( ~! ~" g8 o7 z; {6 {- G
  12. -10.0
复制代码
7 I9 p/ i6 R% c- y4 }/ e
math.gcd(x,y)  返回x和y的最大公约数, O! K% u# t. t7 J: y: r/ Y
  1. #返回x和y的最大公约数
    " G1 n. @/ w3 U
  2. gcd(x, y) -> int
    9 G; z8 I5 v0 e  }( q0 X4 w3 _* y
  3. greatest common divisor of x and y  E* x* Z$ r% }9 a, m
  4. >>> math.gcd(8,6)
    " \. U, Y/ p' m. V; U
  5. 2
    % p$ i9 P+ V% ^; _$ T# s5 V
  6. >>> math.gcd(40,20)
    4 [8 W+ L6 O: X; I2 h  d
  7. 20
    0 Y9 E7 B+ E/ ]' P8 Q
  8. >>> math.gcd(8,12)
    . A% W6 h: W8 @1 f. F) F. k
  9. 4
复制代码
/ H3 G: s" f/ |* J* e1 _% p
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False+ n) L( h. A% C& E
  1. #得到(x**2+y**2),平方的值
    ( K7 Y/ O1 s+ ]$ W
  2. hypot(x, y)# @$ I, r3 o# s3 Z1 t" m
  3. Return the Euclidean distance, sqrt(x*x + y*y).# f2 Q4 T' S; D
  4. >>> math.hypot(3,4)  `4 \  C& e2 S# h4 I. c" C2 l
  5. 5.03 G( j$ y: F- m
  6. >>> math.hypot(6,8)6 j' d: w. Y' h" U% Z5 Z. g
  7. 10.0
复制代码

! G' H: t% r: z# ^math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False! F7 x* b2 e2 Z8 g+ U
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    / _% f0 F( h4 ]2 ?
  2. isfinite(x) -> bool
    # ^+ S# Y' [0 d$ N% |4 e/ O
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.$ Z" n9 g2 ~; b, r; K! v+ `
  4. >>> math.isfinite(100)
    & v: o) }2 |- I2 J* ?" ^( Q
  5. True* G& K# b- T6 g+ t' z0 d! `5 Y8 F
  6. >>> math.isfinite(0)
    + m- {; Q; Q6 I
  7. True
    ( P9 B4 @% Z, O+ z$ u5 v4 g
  8. >>> math.isfinite(0.1)) @. x& J6 O- j$ p  U& i
  9. True0 e$ D% u) O0 \
  10. >>> math.isfinite("a")* u1 i7 f" N( O) {: h& Y- N
  11. >>> math.isfinite(0.0001)$ \$ Y  M! y9 m, M$ U# u2 [6 _
  12. True
复制代码

# C1 U* A# _" j5 v2 e. l4 \math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
- T3 n( W7 h8 L/ |7 D8 C
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False3 y* Q* z* ^# {, p( j# L, y
  2. isinf(x) -> bool
    / D+ N. l% `7 Z" i: Y+ \  t, V
  3. Return True if x is a positive or negative infinity, and False otherwise./ o( n5 p3 S4 T$ Q- }
  4. >>> math.isinf(234)& o6 A  b( l, q- P9 }2 g; y1 a; `
  5. False
    & q, C/ b9 H. s5 |
  6. >>> math.isinf(0.1)0 u4 j1 k* ?; S' @
  7. False
复制代码

  y) A. l- T  gmath.isnan(x)  如果x不是数字True,否则返回False* o! A& o+ Q/ ]9 |  P& X
  1. #如果x不是数字True,否则返回False
      R" {6 v7 z( o# k
  2. isnan(x) -> bool) k! K" i5 C* r( g9 r
  3. Return True if x is a NaN (not a number), and False otherwise./ N, D/ V0 z8 {0 t/ c6 b
  4. >>> math.isnan(23)  e  C% m7 I* R( r+ E1 @
  5. False  E+ U1 P& R9 O4 X1 Y
  6. >>> math.isnan(0.01)( y9 M+ b% b: {- s$ U/ |$ a# L
  7. False
复制代码

  u1 |8 [$ h$ I  e- B- N9 Y8 Mmath.ldexp(x,i)  返回x*(2**i)的值% k3 a& J# E( D4 P8 }7 \" w
  1. #返回x*(2**i)的值6 F8 G. ]& [' Q1 N
  2. ldexp(x, i)
    ) K# H# W: ^6 \& U3 G9 m! m
  3. Return x * (2**i).
    . i+ s3 [* @- y" g( P3 h
  4. >>> math.ldexp(5,5)& z# U" R6 m/ d6 r
  5. 160.0
    3 G# N/ d  K( j& b! r
  6. >>> math.ldexp(3,5)
    1 W* A! q1 v/ o# o0 {" w8 c) Y
  7. 96.0
复制代码

+ X3 c9 z+ U6 g" F1 `math.log10(x)  返回x的以10为底的对数
* h/ s$ e6 L: i) _: w# n0 K
  1. #返回x的以10为底的对数
    " y7 c) f; ^7 {/ n# e& E- t
  2. log10(x)2 M2 B% o' C: o7 h# j9 e, }
  3. Return the base 10 logarithm of x.3 H- Q6 p2 \9 J  L4 V6 H% W6 z
  4. >>> math.log10(10)
      u1 ?4 ]! y/ a! y/ P
  5. 1.0
    $ A+ ~; P$ Z5 G4 x
  6. >>> math.log10(100)
    . `1 m/ O4 n6 S% T& d
  7. 2.0
    $ \5 B. O* X0 W3 |0 B4 M$ T' H* _" b
  8. #即10的1.3次方的结果为20
    ( |4 r6 m$ v/ D+ X
  9. >>> math.log10(20)/ ]  {3 s3 B% h( r' Y' C
  10. 1.3010299956639813
复制代码

) Q4 u: |" S: o6 p, {  omath.log1p(x)  返回x+1的自然对数(基数为e)的值3 T1 `+ k. c. Q0 y( Z1 b1 w
  1. #返回x+1的自然对数(基数为e)的值( R! H7 x! U/ s0 k
  2. log1p(x)
    * ]! z' M7 p. B3 s9 n$ J
  3. Return the natural logarithm of 1+x (base e).1 c( k3 {! E4 J0 ?7 f; a
  4. The result is computed in a way which is accurate for x near zero.  Y1 m' ?" H  N4 h- i- t: U+ a# B
  5. >>> math.log(10)4 o: b5 [$ N* r
  6. 2.3025850929940467 d, W3 q8 v* J0 ?
  7. >>> math.log1p(10)
    8 |5 C: H; `3 M3 d7 c! P
  8. 2.3978952727983707
    ; B& S8 e# h3 b) p) ?
  9. >>> math.log(11)3 P& K; d4 M- E- i: I+ J+ B
  10. 2.3978952727983707
复制代码
6 `3 Q/ F4 H7 B* A
math.log2(x)  返回x的基2对数
2 I; z9 Z9 A: X' @. N; m2 S+ q: Y
  1. #返回x的基2对数% }' @0 o- {* j  `2 }/ y  v
  2. log2(x)3 l2 T" ~# x6 j' z/ A
  3. Return the base 2 logarithm of x.
    ' @; J) D1 @6 y- {; ]
  4. >>> math.log2(32)
    & Z/ S+ H/ A; ~  n, {* C
  5. 5.03 h6 A! }0 W) X2 l
  6. >>> math.log2(20)+ G3 X/ ]7 S+ [; |/ K9 f3 g; U+ C% F& V
  7. 4.321928094887363
    ; y& E% H. l; n9 s# }
  8. >>> math.log2(16)
    & t( w. ]0 n' k
  9. 4.0
复制代码

) Y/ I; Y- n* O% f( Qmath.modf(x)  返回由x的小数部分和整数部分组成的元组9 x# D9 @, U1 E$ o
  1. #返回由x的小数部分和整数部分组成的元组' {& T8 C' E1 b" U+ c. Y, a
  2. modf(x)
    7 Z- X- d# C+ i3 ]7 X# B
  3. Return the fractional and integer parts of x.  Both results carry the sign
    8 ]6 _9 Q: L% L) B
  4. of x and are floats./ P5 P5 g2 ~" {3 C( c8 }8 [
  5. >>> math.modf(math.pi)
      X5 `  H# y- l- s0 k% b
  6. (0.14159265358979312, 3.0): R# l) ^+ n  }8 h
  7. >>> math.modf(12.34)
    - E1 D, q% R7 y- e: h. G: u
  8. (0.33999999999999986, 12.0)
复制代码

* P/ r6 H& C8 m  @math.sqrt(x)  求x的平方根
% K( @; G/ Q9 ]4 C7 f" O
  1. #求x的平方根! }0 T) i' r! O! B( ?! C+ w2 G' n
  2. sqrt(x)* Y1 Y8 b. h. h) y+ ^
  3. Return the square root of x.
    2 m& e& M3 d& u: L1 H/ F1 n
  4. >>> math.sqrt(100): |7 k% K8 u$ m, r* I
  5. 10.0( h! l4 d- n; B  i0 V+ V+ l! t. _, q2 ?
  6. >>> math.sqrt(16)
    6 @# M3 P1 i& p0 i* c
  7. 4.0+ @. H! v' ?; Q$ ]6 [
  8. >>> math.sqrt(20)1 ~: }0 c1 B; v, ~
  9. 4.47213595499958
复制代码

5 l4 v1 `$ b+ F% e; Fmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    + y5 h; W4 n, k; F3 |& n/ ^! Z
  2. trunc(x:Real) -> Integral0 ]: z2 |% Z6 `* g( F
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    , P0 Z) I% Z& ?% `6 s$ r( t/ V
  4. >>> math.trunc(6.789)% _# \0 B3 \4 p4 m% v7 u
  5. 6
    , x8 j# a( ]4 b+ G+ b
  6. >>> math.trunc(math.pi)
    ; s, U5 b+ L! w+ e7 w; L; \3 y8 g9 g2 q
  7. 34 o. F% q& u, I, [
  8. >>> math.trunc(2.567)
    ' i/ a  R& A/ C* g
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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