新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

( I  H2 ?" C! z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 l3 [4 b% |& P4 o' K  u

- k8 [4 J* n( v; [4 G4 O方法10 o5 k+ e7 T0 u2 U
  1. >>> import math
    + F! B( |) N; ~3 I: ~0 \
  2. >>> math.sqrt(9)
    & ~( ^' r( \* K9 [# z# |
  3. 3.0
复制代码
方法25 d+ A5 N0 X. ], l, |
  1. >>> from math import sqrt
    1 C0 }! k# N2 z
  2. >>> sqrt(9)
    ! s! u% v3 h$ e) J6 y( @* z
  3. 3.0
复制代码

1 v$ P2 S; `% |& n
7 j+ G* S9 j! d- q: K2 q5 f
math.e  表示一个常量
! u9 ]) O3 j  _6 D' s+ n
  1. #表示一个常量3 G' l5 g+ @- V, o- m7 I- c
  2. >>> math.e
    9 M* H2 I0 ?/ G1 ^8 S* `
  3. 2.718281828459045
复制代码
4 Q+ s3 c% }# F' q
math.pi  
数字常量,圆周率

; V  {5 O; I9 P' R* r! Z
  1. #数字常量,圆周率
    / s9 C0 p- M# u3 L
  2. >>> print(math.pi)
    ) ~, s" Q2 [" d- P0 x" g
  3. 3.141592653589793
复制代码

; k5 H9 V3 H  Jmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

1 S  A" I, W$ t& v9 E
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    8 J9 I1 e0 U3 a
  2. ceil(x)
    4 d2 U/ a; [5 e# B8 H+ z1 F
  3. Return the ceiling of x as an int.
    ; {6 a7 B+ g( {! k; H8 F- M" C
  4. This is the smallest integral value >= x.
    " R7 j) L# o0 Y: z7 C. S1 W

  5. - o! j8 b/ f0 \2 t
  6. >>> math.ceil(4.01)
    & ^; j, Q4 p: R5 b7 Z* j2 |4 t
  7. 59 s: K% S( i( I3 D0 B
  8. >>> math.ceil(4.99)/ k' I9 [7 x& B4 v# E1 x, o6 Z* j) e
  9. 5
    # A: N3 M; z8 q" d
  10. >>> math.ceil(-3.99)1 W* G- {, E  l( q2 u7 x7 a/ c. ?
  11. -3
    3 ^+ F' F: u( `  I: y, ]
  12. >>> math.ceil(-3.01)0 |& j' H" _( |5 b
  13. -3
复制代码
2 N; q8 k) p0 L* ^: y/ w  U# ^% O
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 f& P0 v; P9 m0 B
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    # p8 J; o4 }1 G( ]& O
  2. floor(x)! C, z4 b4 j2 r' |1 i2 f
  3. Return the floor of x as an int.
    # I; P  b0 o, A. ~& L# d; Y- A
  4. This is the largest integral value <= x.9 Z" n6 _: i0 P% y9 a( j( P
  5. >>> math.floor(4.1)+ H4 ~) S3 }$ M1 ], ]" X7 b' O: B
  6. 45 J% K: n# I# }  s* s/ I
  7. >>> math.floor(4.999)
    . q) b+ x) c7 K4 y) q2 R+ y
  8. 4, S/ |& W: G8 v+ K3 }7 x& D
  9. >>> math.floor(-4.999)1 M4 z* ]1 J) m) w9 Q
  10. -5
    ; m, t, a1 {  \* o0 }% J
  11. >>> math.floor(-4.01)0 L: H; o7 `  G
  12. -5
复制代码
# W! X+ f$ }) k( T+ ?' ]% W. g+ I
math.pow(x,y)  返回x的y次方,即x**y5 U0 U. j' k8 [9 i. R2 I
  1. #返回x的y次方,即x**y
    % Y* i5 L7 C) S  K* A8 a
  2. pow(x, y). |; A. _) E2 q; p4 g  Q
  3. Return x**y (x to the power of y)." F2 Z) I3 n: H' B, D0 n
  4. >>> math.pow(3,4)
    ; g3 [! `: ~, c! b+ R3 }- [' _4 h$ {
  5. 81.0+ s+ N) j: i% S- v
  6. >>>
    8 d+ P' p* f- q* m8 F" t% b
  7. >>> math.pow(2,7)
    : v: F" Y) {' v% L- k
  8. 128.0
复制代码

3 B# k5 V( E3 s9 Gmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! v$ p# ?/ Z+ Y( w& M9 J) m
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 B* p! W2 Q- u7 \9 q
  2. log(x[, base])1 P& j  _: [* ]+ ], {/ |+ G" T' v
  3. Return the logarithm of x to the given base.# v$ q& \; T. t% ~+ L) b3 P- }
  4. If the base not specified, returns the natural logarithm (base e) of x.
    % D# H" b$ X/ W- h: a7 K( |
  5. >>> math.log(10)# g( h+ d7 x/ P* F
  6. 2.302585092994046
    - w( l3 A2 Z% s: {5 x6 u2 ~. I) w# I
  7. >>> math.log(11)
    8 i, F9 z1 p" x5 _/ U2 Q- @8 S
  8. 2.39789527279837072 r2 f5 x8 s2 ?7 d3 D' C
  9. >>> math.log(20)
    # Y- r2 q/ ^2 ]
  10. 2.995732273553991
复制代码
: v+ G0 [$ j/ W* W8 _
math.sin(x)  求x(x为弧度)的正弦值( }. E% m% P# O  Q& w/ @
  1. #求x(x为弧度)的正弦值0 i. m- A9 x2 G/ h1 W) q
  2. sin(x)6 }* J" Q7 i  i
  3. Return the sine of x (measured in radians).
    & n1 e# J  {2 ~3 y# X2 h$ }
  4. >>> math.sin(math.pi/4)$ U2 F: y; ^9 ?# ~2 p7 I3 e
  5. 0.7071067811865475
    - k2 x5 X0 {( J% b
  6. >>> math.sin(math.pi/2)9 i! }- w9 t7 J" @7 x% m7 i) Y
  7. 1.0# u8 q9 ?# `: F( _" W
  8. >>> math.sin(math.pi/3)
    ! @8 _% i# E9 V# {
  9. 0.8660254037844386
复制代码

6 E! `0 L2 D* K# a5 v5 @. U' Amath.cos(x)  求x的余弦,x必须是弧度8 B( y+ t$ e- }8 C) e/ F
  1. #求x的余弦,x必须是弧度
    1 g' A& _% ^7 r
  2. cos(x)% D' O7 X1 }6 Z! R: ~4 N
  3. Return the cosine of x (measured in radians).
    , z5 p6 t2 O& A& o
  4. #math.pi/4表示弧度,转换成角度为45度$ M; h  X/ l' Q7 e
  5. >>> math.cos(math.pi/4)
    & k* A: h! T" v9 T5 L! n
  6. 0.7071067811865476' B9 N1 I& x/ ^0 W( Q# E! p+ V! |
  7. math.pi/3表示弧度,转换成角度为60度
    0 M2 F, q' l! n* m  G
  8. >>> math.cos(math.pi/3)
    & s- d1 t" [9 v7 r5 I5 ]
  9. 0.5000000000000001
    # A6 C: V+ r/ O- _7 C, i
  10. math.pi/6表示弧度,转换成角度为30度
    9 @) R1 S9 \" x) u7 p8 ?$ p
  11. >>> math.cos(math.pi/6)
    + k6 w0 ^+ {2 W- Q9 b% H6 h
  12. 0.8660254037844387
复制代码

& W1 [' M8 `' [, q+ C2 F: fmath.tan(x)  返回x(x为弧度)的正切值' _6 A1 B6 k4 E$ _' ?; e* e- v
  1. #返回x(x为弧度)的正切值6 ]$ E4 y7 e8 g) {+ t/ p6 J
  2. tan(x)
    2 k+ w* P5 x) X+ o9 h: c2 y
  3. Return the tangent of x (measured in radians).
    ' v; \( `/ V4 M  t  V$ `5 i& @7 E4 z
  4. >>> math.tan(math.pi/4)
    % B4 y. r- b$ z0 w) u/ a0 m  R
  5. 0.9999999999999999
    # G' N5 z" Z3 U8 }; W9 @
  6. >>> math.tan(math.pi/6)
    8 ^7 y( Z5 I- Q8 z( P+ I. d9 V; p$ n# x
  7. 0.5773502691896257
    $ p5 z: R" K, j) N3 U
  8. >>> math.tan(math.pi/3)/ N" N$ u9 x# a' |( ]  m$ {0 x/ W
  9. 1.7320508075688767
复制代码
/ s+ q/ w3 L* K/ m
math.degrees(x)  把x从弧度转换成角度
7 `7 B- s" C% W" c
  1. #把x从弧度转换成角度1 q. }9 S: M( l5 P+ ^9 F: ~
  2. degrees(x)2 ^. v7 @# y! q  l8 U( }
  3. Convert angle x from radians to degrees.
    9 ?+ n8 P& j# J5 U7 O: Q  |+ c, S0 v
  4. , b. e, e/ p5 u) M* Z0 h! A
  5. >>> math.degrees(math.pi/4)& `7 C- K0 A$ x+ F; p
  6. 45.0. q) I. V. N$ U; `! q
  7. >>> math.degrees(math.pi)9 l( u, Q8 j& W; ^; _) {7 Y- K: Q' \
  8. 180.0% l6 @" s4 f) D4 c" M
  9. >>> math.degrees(math.pi/6)2 @$ \: p" I, V9 A8 T! h4 o8 B
  10. 29.999999999999996
    8 d" M0 ~5 G) N. X) I6 o
  11. >>> math.degrees(math.pi/3)
    ) q' d7 @6 Q9 p$ n0 M
  12. 59.99999999999999
复制代码

. ?- W/ W  @. P" y* Gmath.radians(x)  把角度x转换成弧度# ~# d, G9 W( R
  1. #把角度x转换成弧度
    & P, f/ L7 M( e
  2. radians(x)' V3 F+ F, ?% F* c! R  F
  3. Convert angle x from degrees to radians.
    / Z6 J3 X4 C+ y8 N
  4. >>> math.radians(45)/ m  n% w5 C8 v( Z% u. V" z% ~
  5. 0.7853981633974483$ e: @  X/ @5 s. q- E
  6. >>> math.radians(60), {& u9 n4 X4 a& {0 V$ }6 Q/ V
  7. 1.0471975511965976
复制代码
, Z% g5 w# B0 }! o0 y8 e6 ~
math.copysign(x,y)  把y的正负号加到x前面,可以使用07 W. n7 Z* m5 @+ i0 _# E/ B& O
  1. #把y的正负号加到x前面,可以使用02 b2 Z6 s$ G$ q3 _1 ]- R
  2. copysign(x, y)' Z+ V; M2 g+ E3 @7 I
  3. Return a float with the magnitude (absolute value) of x but the sign   U. k% R4 q* o& F8 l. g8 S3 _
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) & B- ]+ M' _/ p3 K# N, u
  5. returns -1.0.
    3 t: A+ a8 r& u- _
  6. # P4 S. q' H  V; b$ K. P$ d
  7. >>> math.copysign(2,3)/ r! C- Z- U. R- @& U
  8. 2.0: r9 n& c* v+ K% d/ ]
  9. >>> math.copysign(2,-3)
    % G( x# s+ B* w' ~1 N, R3 B
  10. -2.0
    0 y3 A( G, R5 b/ i
  11. >>> math.copysign(3,8)  l8 S) S( U5 h
  12. 3.0
    ) Z. q" d. W6 g+ `/ _1 D
  13. >>> math.copysign(3,-8)* ^$ n+ o% b) u$ X" S
  14. -3.0
复制代码

( c% O. V, ]# Pmath.exp(x)  返回math.e,也就是2.71828的x次方2 y' Y/ g9 c& C9 O9 Q. F
  1. #返回math.e,也就是2.71828的x次方
    % E+ x# l. J$ ~0 {
  2. exp(x)
    9 c' @( E1 l5 ^) C6 p$ W; Q- U1 c  W
  3. Return e raised to the power of x.
    ! W5 O" }! ]5 V5 J+ s- |
  4. 5 D7 o; |6 y% m! ~4 q6 Q
  5. >>> math.exp(1)
    . ]9 Z3 d/ t/ {' N
  6. 2.718281828459045
    " u8 `% i9 ?( Y! F' s$ F
  7. >>> math.exp(2)
    4 y/ B! j& Q0 R1 k! X" ]" ?1 }
  8. 7.38905609893065
    2 p9 V) I! `6 f) h
  9. >>> math.exp(3)
    1 r3 p7 p' a* {5 g( S
  10. 20.085536923187668
复制代码

9 G; M. w/ g9 vmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1# {$ g5 s% ~3 k6 H& i4 t
  1. #返回math.e的x(其值为2.71828)次方的值减1: l% W$ ~5 u5 ?; j) K& V1 d
  2. expm1(x)
    % g0 o) d% Q0 h- s4 x0 v
  3. Return exp(x)-1.  A3 \9 q8 ?5 E" L4 p7 ?
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    ) p" ~4 r. W' h+ S1 B) K6 d: Q
  5. # Z5 l) N- W+ V5 _; {4 r: e0 Y
  6. >>> math.expm1(1)
    6 o6 _3 [# W, q. s" b  g7 z
  7. 1.718281828459045
    7 {* o" b  b! O" N' I+ {
  8. >>> math.expm1(2)
    9 B' S, D9 u+ U4 t& }( R" Z
  9. 6.38905609893065
    9 t: S3 A9 y4 u+ V4 g, Z. t
  10. >>> math.expm1(3)
    9 e; q! V7 W( ^5 J4 z
  11. 19.085536923187668
复制代码

8 D! K, K) i- r, ~9 [8 o/ jmath.fabs(x)  返回x的绝对值
# h4 _/ f" X+ @  K
  1. #返回x的绝对值
    ' B2 r& F, B% @: r6 Q$ L5 T
  2. fabs(x)
    ! e- U& o- O! V9 n4 q2 `, I& Y" Y
  3. Return the absolute value of the float x.
    ) b4 N1 w! {* n, D  n9 @, t

  4. 6 R2 S8 U" ?% _- p3 f9 G# u
  5. >>> math.fabs(-0.003)
      V+ p$ P2 [% I# D! b
  6. 0.003
    9 x7 L' D6 k, c5 M. x: Y. q* d
  7. >>> math.fabs(-110)- o- o- N/ t" y' j& n( U
  8. 110.0! z2 {/ q$ p$ b
  9. >>> math.fabs(100)! e% U, v: v* L. Q  ?# M( L+ W" Y
  10. 100.0
复制代码
/ p5 N- _5 v2 f  y4 f' k, j
math.factorial(x)  取x的阶乘的值3 t4 |$ w+ J/ P" e+ T, C
  1. #取x的阶乘的值8 H7 H7 k2 H: U- e6 N% }
  2. factorial(x) -> Integral- I% R( W% ~  ?' I4 _9 W8 z$ N
  3. Find x!. Raise a ValueError if x is negative or non-integral.4 o; D8 E; O9 A  Q" ?5 E
  4. >>> math.factorial(1): H& D1 O- V3 M% C& ]3 T! _
  5. 1% S  b( X* r4 P5 @
  6. >>> math.factorial(2)
    8 P2 P7 V2 {0 ?+ {- @
  7. 2) l. x$ P9 @$ [5 c4 H* K& b
  8. >>> math.factorial(3)
    ) B# W2 u1 R' J1 H0 N& g* {. R
  9. 6
    9 c8 u0 d3 A6 o; Y, ^
  10. >>> math.factorial(5)6 j7 k7 @* g9 }3 J. [  \
  11. 120. D" |, k- d  L! p
  12. >>> math.factorial(10)) D+ ]1 r' d1 L; L1 P' N/ A# `
  13. 3628800
复制代码
% C2 N) i8 _& `2 r! P
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数9 v: H* M, f' d4 C# f
  1. #得到x/y的余数,其值是一个浮点数& N' n/ @* `% }1 U* _
  2. fmod(x, y)3 p' c1 }& a8 o9 O2 P% g
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    2 @# d6 Q/ g8 J, D( A
  4. >>> math.fmod(20,3)9 q4 A& q5 F  f) B
  5. 2.0
    0 H( V! b0 w( b: ^4 T
  6. >>> math.fmod(20,7), H3 N2 d% M1 u" N
  7. 6.0
复制代码

$ }* W7 @; k5 b# g8 Qmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围' y' m2 T6 e( y; u
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    2 c; b( W1 Y6 H4 K2 {* P
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 p) \4 F+ d9 u! ]: t- ?1 F
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    7 w4 B3 C& {4 h! z# R# E
  4. frexp(x); }1 T/ V9 {/ j0 C
  5. Return the mantissa and exponent of x, as pair (m, e).& I0 T5 r$ t  G" H
  6. m is a float and e is an int, such that x = m * 2.**e.1 ?2 z4 |+ G5 N4 o
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    . p0 k* g2 }/ U9 B8 t" f
  8. >>> math.frexp(10)
    7 }0 p& {9 E( _8 C5 l7 U
  9. (0.625, 4)
    1 R) @5 u% ?5 d9 t
  10. >>> math.frexp(75). l8 |5 v9 b4 D% O3 T5 s
  11. (0.5859375, 7)4 V, y, I+ Z  V6 @& [4 u  J+ K& B; I
  12. >>> math.frexp(-40)( m$ A' f. A" {" h: G. p( P
  13. (-0.625, 6)" y+ e: z) K0 u6 A
  14. >>> math.frexp(-100); n$ P; p/ q. f) G
  15. (-0.78125, 7)
    : d. h, j9 S# |' M$ W$ P2 _
  16. >>> math.frexp(100)
    ' U! b+ B# P0 u2 i) j" {
  17. (0.78125, 7)
复制代码

9 \- ^" Z/ H4 ymath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列2 C, Z/ m% d; ?% p5 |6 h: H
  1. #对迭代器里的每个元素进行求和操作
    - G9 o3 {7 _# R5 K% a# z, x
  2. fsum(iterable)
    & `% s9 c0 M0 u. @4 o) E
  3. Return an accurate floating point sum of values in the iterable.% X& Y( p* ?* Q0 w( v4 W$ Z
  4. Assumes IEEE-754 floating point arithmetic.6 j3 _! K% h/ N/ T: [$ v
  5. >>> math.fsum([1,2,3,4])
    # [9 A, l; m& S2 T8 L
  6. 10.08 U7 X: g* Q, U2 H
  7. >>> math.fsum((1,2,3,4))* M: n' {5 L: [7 R
  8. 10.0
    7 b7 C  z! z+ o- x
  9. >>> math.fsum((-1,-2,-3,-4))9 P' U. \, r  D. v
  10. -10.0
    $ K% @* x/ ]2 }/ a) M1 H: ]
  11. >>> math.fsum([-1,-2,-3,-4])! }4 D& t7 {% I% g$ l1 d
  12. -10.0
复制代码

, A. m3 Y2 N" f% j. Lmath.gcd(x,y)  返回x和y的最大公约数
/ Q9 l! w, b  z( T
  1. #返回x和y的最大公约数
    % f% ?3 Y4 i2 e2 U& b
  2. gcd(x, y) -> int  P' k, l0 q3 `5 n' j* i* h
  3. greatest common divisor of x and y/ h. V$ H. [$ g2 h
  4. >>> math.gcd(8,6)
    " @! ?2 ^; C  q1 V, B& ^
  5. 2* Y, W: G7 [2 j, o* b
  6. >>> math.gcd(40,20)* z- j& U0 n& p* l4 Q# R! |* e
  7. 20
    % {( U( n, O% Z
  8. >>> math.gcd(8,12)) W! `' E% b* u( b2 T# ~
  9. 4
复制代码
+ r, R1 T2 Q- X4 K8 j/ C
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False8 }: w* h4 W: u' J0 e9 z
  1. #得到(x**2+y**2),平方的值& B0 k# q0 V! K  `4 M  u
  2. hypot(x, y)( o: \& f% v7 R/ H. D. n9 c
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    $ w( @; W: L' J1 }& L7 T) @0 c/ d
  4. >>> math.hypot(3,4)0 i6 V8 e- V6 p" p: c
  5. 5.05 i' J" \+ _( K* w
  6. >>> math.hypot(6,8)4 c* u. @; m1 `- A' k( L
  7. 10.0
复制代码
# t3 z1 p1 j' V( m* }- [- m$ t2 I
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False+ V. `  A& A. D+ u9 _- k# N5 M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False  I7 |3 k1 u. N# W1 G4 Q1 }) N
  2. isfinite(x) -> bool
    ; \6 w. H! e! t
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.; Q. i2 r+ P% p: y
  4. >>> math.isfinite(100)7 g% l! D, m# }. h2 U/ G! P: t
  5. True! P+ o, B, s& f9 B% Y6 {& }& h
  6. >>> math.isfinite(0)
    ) B. H, @! j, ~$ f# X% z  e: j
  7. True2 Q/ t9 g9 c) F" f% g0 S, m. a/ l
  8. >>> math.isfinite(0.1)
    " E  z& W; j! s! D. |- O/ |
  9. True" }+ b) r0 j4 l2 D7 E
  10. >>> math.isfinite("a")
    6 g3 `. ?5 _8 E
  11. >>> math.isfinite(0.0001)9 k6 N. H7 e* Q' l  y) P% z
  12. True
复制代码
1 h5 ?# @" o- B7 f5 {0 x/ w# _
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
1 a  y: v6 k9 C# [' s
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False# Z" X' f& `3 l# R2 J+ H
  2. isinf(x) -> bool/ ^1 P- {9 J; y& @7 w3 M
  3. Return True if x is a positive or negative infinity, and False otherwise.- p( ?2 Q& `7 x  X5 [$ s
  4. >>> math.isinf(234)
    & S! Z/ v* o4 T, z( l9 v* B: Q
  5. False- n8 o6 o8 V  |7 Z: d
  6. >>> math.isinf(0.1). r9 M7 o# U# o' n1 e
  7. False
复制代码
* b/ |9 P( ?5 J6 J0 R1 C: z# K
math.isnan(x)  如果x不是数字True,否则返回False
. x- U; a" O* _1 Y9 b
  1. #如果x不是数字True,否则返回False5 T9 M% Y# Y! {
  2. isnan(x) -> bool
    ( X( ]9 e! n$ C" \% O& r) w) J9 w, u
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 C  I' r9 D2 [# c* s
  4. >>> math.isnan(23)) f1 y. _' V' J5 |
  5. False
    & }8 D6 s% T% e( u+ e7 }
  6. >>> math.isnan(0.01): ~6 {8 d5 j# S3 h" l9 _
  7. False
复制代码
( |! j1 A- o! ?2 e& O
math.ldexp(x,i)  返回x*(2**i)的值
/ x) D# k6 t  l/ L
  1. #返回x*(2**i)的值
    7 d7 o1 @" V0 c% y/ ^8 `
  2. ldexp(x, i)0 U6 Z* E# r. A1 S0 b/ p
  3. Return x * (2**i).
    ' c6 T# I. x+ M9 d
  4. >>> math.ldexp(5,5)9 f- H) Y+ ~3 E+ d. E
  5. 160.0
    1 r0 P) p/ X' O) s; j+ T% T
  6. >>> math.ldexp(3,5)3 M: l( V& q; W' R% J
  7. 96.0
复制代码

! w0 E; O7 U% E1 Q6 w) Amath.log10(x)  返回x的以10为底的对数
$ O( @3 X( u2 r2 P' _; d
  1. #返回x的以10为底的对数
    # N0 E* T* D: s0 U2 o7 A" f
  2. log10(x)! b# f/ V* g( M
  3. Return the base 10 logarithm of x.
      r" u8 S& h2 {! S2 m& X
  4. >>> math.log10(10)# }* [; M5 C" L2 b8 f6 F; l; p
  5. 1.0
    9 Y* X& T5 Y8 `8 z+ E# T- m2 _* a
  6. >>> math.log10(100)
    9 C' s  S* W( A9 ]' p+ I5 I+ f
  7. 2.00 c# D3 x3 ~6 z- B9 W
  8. #即10的1.3次方的结果为20& J" t0 o0 a$ c
  9. >>> math.log10(20)6 J+ T# q+ Q4 I" a8 b" R# K
  10. 1.3010299956639813
复制代码
1 q; }! Q% D% L- i
math.log1p(x)  返回x+1的自然对数(基数为e)的值
8 `6 l: Y: d. R  b+ n( u
  1. #返回x+1的自然对数(基数为e)的值
    0 [/ v: v" F6 S, t  @
  2. log1p(x)
    - D# r  j  T, |" Q
  3. Return the natural logarithm of 1+x (base e).; l9 X+ ~9 J/ ]0 O7 ^; w! q- B$ H
  4. The result is computed in a way which is accurate for x near zero.
    5 N: k0 A2 n* K6 r; |
  5. >>> math.log(10)
    ; O. O# b' q$ F8 S, {+ B) y
  6. 2.302585092994046
    " G5 I) B4 H: \; W" P, M
  7. >>> math.log1p(10)9 R7 ^& g4 L, L- V% I/ z# S# ~3 T
  8. 2.3978952727983707
    $ F& r0 }( P, l3 X( q) p5 y
  9. >>> math.log(11)( P% J2 u2 s$ h" }
  10. 2.3978952727983707
复制代码

: H. x* P& ?5 j) H; mmath.log2(x)  返回x的基2对数
; J9 Q$ r! |# P" L0 J( X7 r
  1. #返回x的基2对数+ a8 h$ x' l: j7 s
  2. log2(x)
    & P, b* {8 ]; t% {5 v' I
  3. Return the base 2 logarithm of x." M6 `- u7 `! ]1 Z! Z
  4. >>> math.log2(32)/ `1 i  Y) {" B
  5. 5.0/ @6 l/ m% s6 [3 `" W5 g# n
  6. >>> math.log2(20)
    & ]0 [5 N1 `( y) ]5 J7 p$ y
  7. 4.3219280948873639 f) s) i# @- p4 U7 t
  8. >>> math.log2(16)
    & H! Q! R: U9 B* H3 O/ Q) L: |0 l" h
  9. 4.0
复制代码

/ o3 l/ y' J7 `( r: Y* Bmath.modf(x)  返回由x的小数部分和整数部分组成的元组
/ U2 b* |/ @; F2 B/ w% `
  1. #返回由x的小数部分和整数部分组成的元组
    4 Q, u6 b6 X% T4 N. }8 j
  2. modf(x)3 z6 n6 g% X! W, ]6 R* \! {
  3. Return the fractional and integer parts of x.  Both results carry the sign
    ) O4 j% v4 R( k& \- t2 n+ T9 `
  4. of x and are floats.' D, n2 _$ R3 x: `2 f* B8 y
  5. >>> math.modf(math.pi)7 K& Q$ C! [) C
  6. (0.14159265358979312, 3.0)
    . o/ @4 l# K/ z: X
  7. >>> math.modf(12.34)
    ( k5 j7 o/ t/ B1 a8 V) A& A
  8. (0.33999999999999986, 12.0)
复制代码

) \# E/ d6 R) H5 b; K" Gmath.sqrt(x)  求x的平方根9 e  y% M9 D2 t7 ?# r5 _
  1. #求x的平方根
    % D7 L; c5 J' s+ o9 U
  2. sqrt(x)! n0 r. u+ X, i  x, ~: K
  3. Return the square root of x.. Z3 Q; L; T$ H0 O' u6 K
  4. >>> math.sqrt(100). k9 M2 Z0 h5 l; b
  5. 10.0
    9 Q5 G  f& U% C! Q6 O! @
  6. >>> math.sqrt(16)
    4 }9 O: c% J5 V, ^- U$ n, o* J
  7. 4.0. z7 M) F- _8 S3 R
  8. >>> math.sqrt(20)& ]2 X$ a* L6 n. A  h0 K: f
  9. 4.47213595499958
复制代码
' Y8 y; _; y- y3 m: |* o
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    3 o0 r$ P. \& R5 V% w( H9 W
  2. trunc(x:Real) -> Integral
    ( j- e+ c4 c& r* _: x! _
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    7 Z2 u2 w% `  x" m9 [
  4. >>> math.trunc(6.789)
    6 t4 }. x8 {- W& P: Y1 B
  5. 65 D2 L6 B/ q6 g1 U% Y: h! g4 ]
  6. >>> math.trunc(math.pi)
    8 G3 N) I; Y6 Z" F" ^+ p
  7. 39 H- c; I$ k6 K0 E' B
  8. >>> math.trunc(2.567)
    ( _6 ^( O4 {5 W  d# m# F
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-9 17:41 , Processed in 0.082812 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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