新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

新大榭软件管家 V5.8 Excel版 微信版 发布 财务/仓库/生产/销售/采购/行政/人事/校园 客服中心 - 办公软件 - 网站设计 - 广告招商

新大榭镜像 - 官方Web实验室 - 加入收藏 - 设为首页 广告是为了更好的发展 欢迎我区企业及商家赞助本站 首页文字黄金广告位(赞助)公益广告免费发布

新大榭论坛 门户 查看主题

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

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

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

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

x
# o0 I5 S2 Z( H4 l  N1 ~8 N
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。- i* P4 {9 L" W$ b* z

6 [( N5 c0 L' A% j# @. D方法1
: P5 ?0 M) m! H4 T: `0 }4 `
  1. >>> import math
    ' Z3 U# Z6 e* p( l; I
  2. >>> math.sqrt(9)* q  T, n4 Q" f+ @( `- v
  3. 3.0
复制代码
方法2
& M$ W; a" r0 a3 b* ?
  1. >>> from math import sqrt/ S/ b0 Z2 B- t& ^' B+ T
  2. >>> sqrt(9)
    4 {+ U: ?4 X  r8 Y" ^6 {1 j1 T
  3. 3.0
复制代码

8 {5 [- D) D* h) S! V% @8 P* ~

' R6 X9 x9 j! \' v# t% B1 c2 G, a
math.e  表示一个常量
1 {! F  E$ D2 ?6 f- d- Z) G. |
  1. #表示一个常量
    " {7 X+ s( K% L7 @) L- t( y8 _1 s
  2. >>> math.e3 G8 c% d" G6 Q  \
  3. 2.718281828459045
复制代码

9 }; u0 x6 u# }* N; b/ f  {: amath.pi  
数字常量,圆周率
3 m' ^7 E, d3 J
  1. #数字常量,圆周率( O# ?) N: C2 U. U, f! L
  2. >>> print(math.pi)* t8 X, M' G3 E4 z' I0 c6 Y
  3. 3.141592653589793
复制代码

2 k# a: M" o1 z; i# ~math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

) ?! r$ V+ y. B/ h( h, w& {' a- B
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x: F5 a7 x9 F9 R- Q
  2. ceil(x)) A# ?: _% v+ P: b1 }0 i& J5 \
  3. Return the ceiling of x as an int.( w$ Q' g% F+ Z
  4. This is the smallest integral value >= x.4 M; e" |- Q/ S. T! h3 h

  5. * a, B6 t& A+ z4 @  E6 g: f* s/ v
  6. >>> math.ceil(4.01)1 P- a) b: E+ E" h0 f* {& n' A! D
  7. 5
    , {/ H2 F+ Y, c8 k
  8. >>> math.ceil(4.99)7 q+ l) y, F, r! h3 x
  9. 5
    4 X2 C# W, b/ U4 [+ k3 x; Y
  10. >>> math.ceil(-3.99)
    ( s! c: `8 f; [# X/ d
  11. -3% g& o2 z& D+ v7 T3 o( D
  12. >>> math.ceil(-3.01)
      {0 t- i7 f, `% z
  13. -3
复制代码
2 c8 ?. X$ D# \+ p1 ^7 ^7 P, g
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
) V, A$ s3 F0 f* K' C8 M. |
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身0 s0 ?; s% ?4 A& B
  2. floor(x), Y* n- ?* U# M+ u8 E0 ~
  3. Return the floor of x as an int.
    , F, M$ w3 I2 |" d4 D. k* A
  4. This is the largest integral value <= x.
    : a8 l9 S# I. C+ m7 ]5 H4 y- T3 Y
  5. >>> math.floor(4.1)4 \: K( z0 h% R- ]
  6. 4
    . j& L& u7 D3 m9 n0 n" ]# W$ J* B
  7. >>> math.floor(4.999)% Q" F5 k9 U% \8 W8 f
  8. 4
    4 s5 A( A; k% @6 R# n9 k
  9. >>> math.floor(-4.999)
    . `( H" o* M. J, X7 [
  10. -5
    6 G  B+ X) B" _- `$ E  q8 V
  11. >>> math.floor(-4.01)6 j# x5 m6 H& I( s8 [9 P1 w
  12. -5
复制代码
! k3 e& o/ R, s8 z- c
math.pow(x,y)  返回x的y次方,即x**y
3 z/ Z3 j7 Q/ E' R! j* }* g
  1. #返回x的y次方,即x**y/ n1 @+ E0 F$ t+ v
  2. pow(x, y); P* [: f$ E  E3 B% O7 T( a% f
  3. Return x**y (x to the power of y).
    " ]' i! x: [7 D, c7 j
  4. >>> math.pow(3,4)
    ; z( Y* z) u: @* ^) s; k' `9 r, S
  5. 81.0
    5 i! }2 D# Y% c) w, f+ {
  6. >>> % f( D$ |; G: {
  7. >>> math.pow(2,7)& B4 j- e/ P/ u4 O. |! u
  8. 128.0
复制代码

4 N7 B* Q) K# s7 [( G% ^math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
8 E) t2 T: E+ V; z$ Y
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 L3 @7 N$ |  r' q8 v$ B+ q7 q6 Z
  2. log(x[, base])1 P) |# J2 Q9 I1 M8 y& m; }
  3. Return the logarithm of x to the given base.
    & {6 P  K  X6 z( Q; @1 p. E
  4. If the base not specified, returns the natural logarithm (base e) of x.1 e/ f- ^* s4 H8 t' i* a
  5. >>> math.log(10)4 q! ]  z- m0 n% V2 ~
  6. 2.302585092994046
    0 r/ \! Y0 _6 w
  7. >>> math.log(11)- ]- }) r1 {" q4 A
  8. 2.3978952727983707
    # F# i, H6 \4 v
  9. >>> math.log(20)
    * h* R* k5 Q- M6 y0 ?9 i
  10. 2.995732273553991
复制代码
1 G, d+ t7 _/ Y) a8 X+ w
math.sin(x)  求x(x为弧度)的正弦值, f. S; Z! {' D3 X* C) X7 z
  1. #求x(x为弧度)的正弦值
    % X& h! r, F  B) b. b
  2. sin(x)
    , F4 }4 |) l2 C3 L- [
  3. Return the sine of x (measured in radians).
    ! R$ V3 z0 G# `  F( Q
  4. >>> math.sin(math.pi/4)
    8 @' k: v5 @/ P
  5. 0.7071067811865475
    9 Q+ w( c3 Q/ v" b( B7 l
  6. >>> math.sin(math.pi/2)
    ' U6 V! n: g/ e5 ?4 ^
  7. 1.0
    ) ~; W8 k- q/ k5 G
  8. >>> math.sin(math.pi/3)- M) [$ Y+ e5 k2 y" w& T- f- R
  9. 0.8660254037844386
复制代码

$ l& ~5 X0 H' y) }( o3 Z% X0 i( kmath.cos(x)  求x的余弦,x必须是弧度6 a7 I* A, U: h8 [# q: W* I+ t
  1. #求x的余弦,x必须是弧度) x! j% C3 o9 ]' R0 M
  2. cos(x)
    4 D# [; M# Q- J$ I& n& i* \  r5 W
  3. Return the cosine of x (measured in radians).4 [# [: J9 M( k( X/ u& m+ T9 g
  4. #math.pi/4表示弧度,转换成角度为45度7 s% T  n! E! x' B8 h; ~
  5. >>> math.cos(math.pi/4)# M5 q. V$ A" a: ?" t
  6. 0.7071067811865476& D+ P" e, P5 T. K! T, J
  7. math.pi/3表示弧度,转换成角度为60度
    ( t7 Q- n6 |$ |' T/ P
  8. >>> math.cos(math.pi/3)
    5 H  r( G  V7 r# O3 I& Y
  9. 0.5000000000000001( e# z- J9 B" s, c' u% t, k
  10. math.pi/6表示弧度,转换成角度为30度
    ( U5 ^  Z: p3 x& W& A! r; J
  11. >>> math.cos(math.pi/6)4 c: u* v( U/ A
  12. 0.8660254037844387
复制代码
; m; n/ }1 w! w  o/ X* i
math.tan(x)  返回x(x为弧度)的正切值+ `4 Z* }, }( m9 ~* z
  1. #返回x(x为弧度)的正切值! J+ v5 z) i7 c/ o8 s3 ?( J! y
  2. tan(x)
    6 W2 M; i! @8 I9 j- o" n  l; O- b$ [
  3. Return the tangent of x (measured in radians).
    * G$ U5 v5 L) d' n
  4. >>> math.tan(math.pi/4)! }4 H4 d. x8 D# x0 ^' v( W( }. S
  5. 0.9999999999999999
    2 P) U0 J8 Z# c3 O; N6 ?; H
  6. >>> math.tan(math.pi/6)6 B& d1 Y. C; w0 t7 r
  7. 0.5773502691896257
    1 X: G0 y8 g$ g" u( ^+ o
  8. >>> math.tan(math.pi/3)
    " x( W+ N8 d: |: E% ]% V
  9. 1.7320508075688767
复制代码
# V% u- z$ A$ `0 x" J, o) N0 s
math.degrees(x)  把x从弧度转换成角度
7 I8 F: T5 k9 \/ N3 \
  1. #把x从弧度转换成角度4 |3 t4 m% ^' i1 z5 N4 F
  2. degrees(x)
    2 P# l6 v6 f  w" ~5 f% V. c5 d
  3. Convert angle x from radians to degrees.
    6 X1 F' [! O5 a. Q9 c& v( i' D

  4. 0 A$ x. b. g9 r& S  c# Q
  5. >>> math.degrees(math.pi/4)) e3 u  ~8 c' ?$ ^# Z0 [
  6. 45.07 t1 v! c- ?# o/ Z- b, m( j1 y3 c
  7. >>> math.degrees(math.pi)
      ?) |0 W' X4 c5 [+ W/ V$ p
  8. 180.0
    4 ^+ i; o( b9 H8 \- R
  9. >>> math.degrees(math.pi/6)) e  z# d6 B7 X  ^; h: G
  10. 29.999999999999996
    + Q3 B2 p/ ^9 B4 ?4 p$ J5 ~7 F0 v
  11. >>> math.degrees(math.pi/3)
    / A9 a" S4 s- p9 F- f0 L9 t( L$ b& s
  12. 59.99999999999999
复制代码
9 N6 d; e! c4 Q/ ?. R8 u
math.radians(x)  把角度x转换成弧度9 Y( h- O- h& A8 s) W- \7 G
  1. #把角度x转换成弧度# b. W9 k% m- q& Q/ V. z/ O9 x" ~/ ?
  2. radians(x)% s4 H4 E. k1 U3 O
  3. Convert angle x from degrees to radians.
    ) u! }& Z/ f9 i; e
  4. >>> math.radians(45)6 }9 `+ G8 e0 z# e: a6 w! W
  5. 0.78539816339744832 v' k3 H% K0 V" Q0 n4 D
  6. >>> math.radians(60)7 Y/ J0 z; ~0 {( {5 c$ i1 n
  7. 1.0471975511965976
复制代码
7 s4 z' Q6 p, m$ c6 P) J5 F- N
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
& n+ c$ X# M' h
  1. #把y的正负号加到x前面,可以使用0
    / u* a5 p5 W5 `  \2 r0 [
  2. copysign(x, y)
      A6 i; G7 m3 t
  3. Return a float with the magnitude (absolute value) of x but the sign 3 [6 P- A1 G' q/ E; N) a! a7 N: f4 ^: O
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    0 X: M* b* x/ u2 _( S
  5. returns -1.0.
    4 ~& s, _" c1 ?7 G  O( H3 z

  6. - m  _  v% M0 ]4 |! b  l
  7. >>> math.copysign(2,3)3 s0 s& p7 F3 O; ]
  8. 2.0( W& i6 ^5 L) I
  9. >>> math.copysign(2,-3)
    % Q3 o0 |; {/ T: `5 w  @
  10. -2.0
    * P6 O# _/ Q4 G2 j; f
  11. >>> math.copysign(3,8)
    1 F% o& D0 i1 ]
  12. 3.0
    % v( X: ?8 B. C2 N" D* |1 X+ [. \
  13. >>> math.copysign(3,-8)
    6 ]; S* I! [0 V2 q; W
  14. -3.0
复制代码

( u6 K6 B3 W3 l7 I7 x% B* F/ `math.exp(x)  返回math.e,也就是2.71828的x次方$ |9 |7 {6 {$ p! U! B; b
  1. #返回math.e,也就是2.71828的x次方
    9 t: ?! c3 c& k0 b2 e
  2. exp(x)
    : S# x1 R* W1 E+ {) Z( j; e
  3. Return e raised to the power of x.  r* Y7 \+ g- E
  4. 9 j! v! g( y  S1 f1 z$ U
  5. >>> math.exp(1)! a5 @$ E2 j: {; a+ P# Q$ X
  6. 2.718281828459045
    + O6 E4 Q  i! n* {
  7. >>> math.exp(2)" o; ^1 q: `( J& ?: d# f
  8. 7.38905609893065
    ' @& Z  q7 E8 H2 W. N
  9. >>> math.exp(3)8 M+ W% }; V( Q
  10. 20.085536923187668
复制代码

8 X, F" s/ v* s9 j6 R7 imath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减12 L5 J& I! ?8 P1 }. Y6 \* z# T% Z
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ; i) t) `7 q" n# ^2 v% S6 L
  2. expm1(x)& T8 H+ K( c: n
  3. Return exp(x)-1.9 C2 A4 y7 z) }2 ]* r5 L$ h
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    / H7 d& t( T! E( D; m5 G. u
  5. 4 w1 F8 M0 K" e, r+ M% u
  6. >>> math.expm1(1)
      U" q% V  K! b- v
  7. 1.718281828459045% `9 s. X, ^- s  ]8 z' y/ a( M" _+ {
  8. >>> math.expm1(2)
    # G0 W# J2 K! C: C; v! P) V! P" e
  9. 6.38905609893065# K( Y- s/ |$ e+ \
  10. >>> math.expm1(3)
    - g+ u; A2 p" t) V) F$ U5 X/ H
  11. 19.085536923187668
复制代码

% L# p3 x& N% A; v0 R6 H" bmath.fabs(x)  返回x的绝对值/ d2 ~8 r+ O7 w0 j. y
  1. #返回x的绝对值1 I- e( R5 M7 @8 ]" K  o
  2. fabs(x)
    ; P! Y2 ^+ U: z, H- V1 c; h
  3. Return the absolute value of the float x.
    ! v. m2 V, W8 O( G, h/ }! Q

  4. 5 v0 d* }1 q3 n1 F+ ?
  5. >>> math.fabs(-0.003)" F7 [3 T2 ]1 c* ]3 o
  6. 0.003/ m6 M4 `. X$ `9 k9 c  [
  7. >>> math.fabs(-110)
    * o, ~7 B8 M/ T0 ~8 e& V, N
  8. 110.01 h' g  l! a: v( k& d
  9. >>> math.fabs(100)
      l% X3 h' l$ B" x' K% g! J# S
  10. 100.0
复制代码

# s2 o! Y# O& ^$ o# y0 \5 Y0 rmath.factorial(x)  取x的阶乘的值
" i3 z2 R6 x2 l3 X0 D
  1. #取x的阶乘的值! q4 j& w+ C: Z. z
  2. factorial(x) -> Integral
    8 _- C- _4 A: n& \, m
  3. Find x!. Raise a ValueError if x is negative or non-integral.% ]# ?# X" m% ?/ F9 \1 s/ a3 t
  4. >>> math.factorial(1)
    $ e" M0 i* `/ i' g7 h. Z" N
  5. 1
    % P: t6 i9 W# U! {# s" C; [
  6. >>> math.factorial(2); B* g/ A! b& p6 i3 m
  7. 2
    2 c7 k" g' e' B6 A) w
  8. >>> math.factorial(3)$ q2 ^; w' Q% D" [" x  |% q0 X
  9. 6- l0 W9 h' B3 P1 r, c$ L6 s% C- k
  10. >>> math.factorial(5)
    0 M" p$ y( _, p- y- X
  11. 120  I6 E/ j6 y) N7 W
  12. >>> math.factorial(10), M+ r6 a6 `0 c0 ~
  13. 3628800
复制代码
' n. d% f5 ^1 b$ T" m; z& y
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数8 K7 E* F9 Z) C: E7 u: O+ w1 t
  1. #得到x/y的余数,其值是一个浮点数
    + d% g! e! ^- g+ |7 z
  2. fmod(x, y); N& S9 O- A1 y9 H
  3. Return fmod(x, y), according to platform C.  x % y may differ.6 ~, ~/ D6 ~& y' T/ q8 d
  4. >>> math.fmod(20,3)
      D/ H( G" r( K! C1 q- X
  5. 2.0: Z! }! N: H7 N+ N( H! F3 r
  6. >>> math.fmod(20,7)! o8 Q  }* f. l0 K, L9 p, h
  7. 6.0
复制代码

' `7 l- E" b( Y: C2 Ymath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围7 r, w4 ^' a% b0 f# f2 j
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,+ R3 [& o# W$ M+ C
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值& W) g3 I; e8 @, k6 F3 D
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    * F% P7 P6 c' {- j+ g0 V
  4. frexp(x)
    ! V/ e: ~& g4 ]7 r& X! w. J4 P
  5. Return the mantissa and exponent of x, as pair (m, e).
    & ]' D2 d% t) f( h) |. Q
  6. m is a float and e is an int, such that x = m * 2.**e.
    7 t6 T/ I, ~0 ^# Z7 y6 n, q
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    - b6 C0 ~# N! a) L. G" r+ w
  8. >>> math.frexp(10)' V7 {3 c: a/ T
  9. (0.625, 4)# y: m& n- v. ~7 P5 V
  10. >>> math.frexp(75)1 u* T$ B- y6 Q8 B
  11. (0.5859375, 7)8 q9 y* A6 L! {" k% Q" I& M
  12. >>> math.frexp(-40)
    4 U# i0 |( B* O
  13. (-0.625, 6)
    2 ]3 b0 U1 W# S" y/ g- C, r
  14. >>> math.frexp(-100)
    ' O) f& A3 }  _: }8 y
  15. (-0.78125, 7)
    % d: ^3 b( s# q; B% z, v0 x2 I! a
  16. >>> math.frexp(100)
    ' @9 N7 \6 u/ V4 w4 N% O
  17. (0.78125, 7)
复制代码

- l/ o7 P# @/ vmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列& V- `/ {# K& i& \
  1. #对迭代器里的每个元素进行求和操作% B7 e+ L" Q. n2 G  E
  2. fsum(iterable)$ o" U% w  ^: e& R& y
  3. Return an accurate floating point sum of values in the iterable.
    ' c; C7 t' C5 i! M% G7 m
  4. Assumes IEEE-754 floating point arithmetic.: J9 \+ E( u3 j5 m. ~
  5. >>> math.fsum([1,2,3,4])
    ; i  h3 H9 _1 ]5 ^* I
  6. 10.0
    . ?3 v! E. Z' F" w; e' x' t
  7. >>> math.fsum((1,2,3,4))
    / f, S) X1 w0 [4 R4 ~# \
  8. 10.0
    0 V2 e$ r( Y, T6 I: u! n6 }2 O
  9. >>> math.fsum((-1,-2,-3,-4))1 a* f* E. |' U( {% f4 X
  10. -10.0& }0 q- `0 m" t( v% s
  11. >>> math.fsum([-1,-2,-3,-4])
    0 {: C2 r0 d, k* H
  12. -10.0
复制代码
4 X- p1 r; Y4 Z  p$ a! u
math.gcd(x,y)  返回x和y的最大公约数
% c* T3 e. @# a% a3 s+ Z" L- O% \
  1. #返回x和y的最大公约数* C& u  c# H; j1 X: {( j4 F
  2. gcd(x, y) -> int
    5 m0 U; j4 G; A$ p) h$ d9 `+ s
  3. greatest common divisor of x and y
    ) z$ J7 ~. `- H. _) v
  4. >>> math.gcd(8,6)6 b! w& b, E6 Y( t, |
  5. 2
    4 u/ g: n9 A. i3 p9 i
  6. >>> math.gcd(40,20)5 o- r2 p4 u" d, N) M2 v- y$ T+ N# i
  7. 20( _  ~1 _9 N6 }6 {/ L8 @: a
  8. >>> math.gcd(8,12)
    , j) _: i  \0 w( T, r' ^, N7 J
  9. 4
复制代码
* c- K4 c$ k# q- r
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False; y# Z+ q! U5 d) x
  1. #得到(x**2+y**2),平方的值& g* T# d% h7 x3 g& S$ Z
  2. hypot(x, y)+ ~5 U7 v6 L/ P2 R! S% k
  3. Return the Euclidean distance, sqrt(x*x + y*y).
      @. \. F+ D: V( g8 T
  4. >>> math.hypot(3,4)
    ) M$ O! }' f/ K+ L$ {9 d
  5. 5.0
    # G/ c% c0 y% W; c5 ^% Y2 S" N
  6. >>> math.hypot(6,8)$ _7 Q; ]; Z: Y4 n4 O& W
  7. 10.0
复制代码
* W- R" _$ W) f& O# _- g# p" i' M
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False' V) j2 I; J8 |! `( @9 \+ h0 H( E
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ; j0 B+ l% B7 m! ?6 x6 U
  2. isfinite(x) -> bool  v- f& j2 G; E! D
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.8 s- Q5 O7 v: U6 }3 B) |- y
  4. >>> math.isfinite(100)
      U- q% }! q$ S: r1 j" v. B: Q
  5. True
    % |/ x' ~  u6 y9 A9 W
  6. >>> math.isfinite(0)
    ' q. e1 G, r, _& }/ r7 N
  7. True& W2 E0 ~6 }! |% k2 W
  8. >>> math.isfinite(0.1)
    6 E3 U( _/ r5 k. R2 O6 e
  9. True
    1 ?; b) M1 G( g' C' _% z: J
  10. >>> math.isfinite("a")) w/ |5 s3 k9 w* I
  11. >>> math.isfinite(0.0001)1 C" K# C3 G: F9 ?. \2 O" H
  12. True
复制代码

0 ~: d" r1 I/ {math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False4 z- ~* F* ^. d0 a, n  M
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    , C- y# Z( h" ^4 M$ J2 E3 O
  2. isinf(x) -> bool
    ) v$ Y* v7 k; ?0 P$ r3 {
  3. Return True if x is a positive or negative infinity, and False otherwise.
    ; o3 c; B2 ~) D8 e" Y3 a& F* k
  4. >>> math.isinf(234)
    8 I4 @! C" o& S, P- G4 T6 ]1 n  X
  5. False
    * ]6 |4 V' |8 {2 d) v: ?
  6. >>> math.isinf(0.1)
    8 a4 J9 g; p  n! V2 U$ `1 w
  7. False
复制代码
; d' K' T0 k1 m2 f* S
math.isnan(x)  如果x不是数字True,否则返回False" c  g5 {4 S3 J# s
  1. #如果x不是数字True,否则返回False
    % U! |: j5 o/ v
  2. isnan(x) -> bool% X$ k4 L' n) d% k, r5 I2 b9 C
  3. Return True if x is a NaN (not a number), and False otherwise.- j1 a9 t7 l& c
  4. >>> math.isnan(23)
    ! H  n$ }/ u; O8 z
  5. False
    2 g. |; q9 Z* k3 T9 _
  6. >>> math.isnan(0.01)" d1 S/ z8 ^$ T& c1 s7 L3 w: T
  7. False
复制代码
& J& f! V& \, y* h6 l
math.ldexp(x,i)  返回x*(2**i)的值9 I- S/ s  |  u4 O  ?: I/ Q
  1. #返回x*(2**i)的值
    ( f3 V2 \; {: Y* |/ b& w
  2. ldexp(x, i)
    9 p4 [2 N% k7 m3 s1 h  T
  3. Return x * (2**i).
    8 P8 W$ ~- o, b% k1 W6 C' Q
  4. >>> math.ldexp(5,5)
    " j* M: u" s7 q
  5. 160.0
    0 u6 O6 M7 C; L& V, e, s6 c$ H! Z& \
  6. >>> math.ldexp(3,5)/ R; [0 P& A7 l# T# m% N" x
  7. 96.0
复制代码

! h! t7 j  B9 v3 x7 Vmath.log10(x)  返回x的以10为底的对数6 c) D& }8 {7 P7 L2 ~, `
  1. #返回x的以10为底的对数
      J2 V" m5 w  R& U  i* b
  2. log10(x): s  ]; ~9 x+ P) ]7 u4 l
  3. Return the base 10 logarithm of x.
    * r; a  P. J) R8 K, X3 ?6 f
  4. >>> math.log10(10)
    3 {1 M$ ?- E- Y- H+ d, F3 V! n
  5. 1.0: b' Z  |$ m# C6 b% G. `: t) D
  6. >>> math.log10(100)
    . }+ x  @" z3 F: U$ a
  7. 2.0+ q+ |5 r% I8 H  I$ Y% j( T
  8. #即10的1.3次方的结果为20# B7 c7 E- g$ f) l8 x3 {
  9. >>> math.log10(20)/ x8 U) `' W  c. Q
  10. 1.3010299956639813
复制代码
4 |: x/ `% ?2 `% G9 P$ Q, [
math.log1p(x)  返回x+1的自然对数(基数为e)的值/ B; I/ X  h# Q; Y: I' b
  1. #返回x+1的自然对数(基数为e)的值2 V+ f! s$ A; z8 i* p
  2. log1p(x)) p0 c3 N9 x$ b# s# Z' Z
  3. Return the natural logarithm of 1+x (base e).. L' w- A$ s1 t" R" ^
  4. The result is computed in a way which is accurate for x near zero.( ~8 d# c& Y  z  n0 q1 C* A
  5. >>> math.log(10)
    7 W$ `1 x! h$ z3 G. ?  K& o
  6. 2.302585092994046
    ; J4 a0 b0 K  p; e
  7. >>> math.log1p(10)8 V- C" ~# J" t9 T- H( [
  8. 2.39789527279837072 E2 R" I2 U" O$ j5 e4 \( Z" w, z
  9. >>> math.log(11)- z. a! o# F+ b  @; E; E
  10. 2.3978952727983707
复制代码

% R$ ~( [! c1 P* A. omath.log2(x)  返回x的基2对数# v! \9 s+ i$ q1 i5 y8 T
  1. #返回x的基2对数* r7 v/ N9 n  l7 x8 K
  2. log2(x)
      x+ a# w3 W9 h( \
  3. Return the base 2 logarithm of x.: {& V& n+ f2 b$ w' |6 D
  4. >>> math.log2(32)
    0 \! G" [8 ]6 e( K  t- ~" M& K
  5. 5.0
    . D: G1 x! g7 x$ B' H) v
  6. >>> math.log2(20)8 @+ {* Y' W& |4 W4 `- C
  7. 4.3219280948873639 g+ Q# L8 |7 l  E6 n
  8. >>> math.log2(16)9 I, d# V6 i6 l* _: t
  9. 4.0
复制代码
3 k( E: [6 g, v# R2 N, J8 P
math.modf(x)  返回由x的小数部分和整数部分组成的元组
. g. h5 F( L+ a6 ~' }6 \9 l
  1. #返回由x的小数部分和整数部分组成的元组
    ! G2 H9 V' B' V( C" {1 n. o5 r
  2. modf(x); V$ d7 A3 W* g; }2 m& p
  3. Return the fractional and integer parts of x.  Both results carry the sign
    7 B( @9 v9 e. u* p: u' o( ?
  4. of x and are floats.2 g& O( H6 u, ^7 U) i7 O% O
  5. >>> math.modf(math.pi)" g, \) d6 F8 B% s8 v- G
  6. (0.14159265358979312, 3.0)
    0 K7 L" Y. w5 t, b
  7. >>> math.modf(12.34)
    ! K5 x6 n$ i1 I$ \
  8. (0.33999999999999986, 12.0)
复制代码

& z5 H8 H' b* K+ Nmath.sqrt(x)  求x的平方根
, k4 i4 ?" N+ k- Y# d* X* F
  1. #求x的平方根
    # e3 z, r- P) g$ k3 o+ j" l% ^
  2. sqrt(x)
    3 P3 i# [- d6 V* g
  3. Return the square root of x.
    $ `, p* L' q3 o3 n' w7 P
  4. >>> math.sqrt(100)
    9 n/ X  l  W& Q4 X* k
  5. 10.0) R. W1 Q, p0 x# T, ]* f2 W4 H8 l
  6. >>> math.sqrt(16)
    ; B0 D; f& X, n: t8 e5 w
  7. 4.08 f% c; v+ K% p
  8. >>> math.sqrt(20)0 p2 y1 E* A7 K. \, m
  9. 4.47213595499958
复制代码
) Q' b" W7 J9 a, I6 S: ~# o+ N! Y
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    $ F! B# ^8 i7 K/ s, N: t6 m
  2. trunc(x:Real) -> Integral9 S7 }; y, r9 h4 n
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    % H( P4 e) w" v
  4. >>> math.trunc(6.789)2 T- J, l, p' ^9 E% t
  5. 6
    8 O9 a" }/ ]" N* o# N
  6. >>> math.trunc(math.pi)# |: l( m1 r4 g9 h' @& K; W0 _# Q  b9 T
  7. 34 i8 w0 z! \) b5 n1 U- k# n
  8. >>> math.trunc(2.567)& q8 M0 W% T7 v; C9 x' S
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2024-4-20 10:25 , Processed in 0.073564 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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