新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

1 J7 @2 `; Z! ^( `【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 x) E/ V2 b. c5 C
5 T# ?4 ?0 [# y' T3 w: V
方法1" s4 E0 C( e; Q& a0 n* i
  1. >>> import math6 `( X. G7 R9 }. C  e* ^
  2. >>> math.sqrt(9)
    0 V4 @# [. b9 M9 T7 X
  3. 3.0
复制代码
方法2
6 M. }+ ]8 E% A
  1. >>> from math import sqrt
    $ y( }+ `3 ~/ }
  2. >>> sqrt(9)
    - v5 Z) T, Y+ H, B7 }4 }
  3. 3.0
复制代码

7 I0 e6 H5 Z% u$ ]$ Q( q
3 v! b/ z- ~) {/ t5 j
math.e  表示一个常量
/ d' n# x! \/ \1 W( j1 N8 }4 ~/ ~
  1. #表示一个常量" [5 d) s1 R, E6 x6 t
  2. >>> math.e  u3 z. {5 `' u' y
  3. 2.718281828459045
复制代码

' _* Y3 X( F8 R% f7 ]math.pi  
数字常量,圆周率

( T# C8 J6 E, \' g9 g, l2 I
  1. #数字常量,圆周率3 L. a) Q+ o/ v% ~! x
  2. >>> print(math.pi)7 f! e2 l! S$ m: i5 s6 q$ D# u
  3. 3.141592653589793
复制代码
3 ?# V! p; g' \/ U2 g' V
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

$ c' x- `" |0 L) H7 M1 o) e. F
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x: c, t; H1 z' N5 z; ?, |  J; b9 S
  2. ceil(x)3 b$ f, o2 I; R/ {
  3. Return the ceiling of x as an int.* l3 l, _+ O. W( Z) ^6 t2 q
  4. This is the smallest integral value >= x.
    $ v$ E" o: h: I) D9 D
  5. , B" i$ ?4 p- R' Q5 r5 @
  6. >>> math.ceil(4.01)
    / c  G7 M: V: U, a3 @7 r
  7. 5
    ) u; n  a) _9 J, |6 r
  8. >>> math.ceil(4.99)# D" u( I: l0 H( D
  9. 5
    : x) F5 p- {) \+ u1 p; G
  10. >>> math.ceil(-3.99); }" B. T$ k. k' Y0 I4 D
  11. -3
    ( s* c  A' ?0 y! J, q. Y
  12. >>> math.ceil(-3.01)
    ( a6 {! [# v! t1 ~; t! z+ V; N2 v
  13. -3
复制代码

) U  T) x0 L3 U" z2 G1 gmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% i& u9 {5 M1 o' }1 j$ f, U
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    9 P* q4 O. b0 C; [! l+ ?
  2. floor(x)
    ) f1 M; L: Z; B) g
  3. Return the floor of x as an int.
    0 D; X: X1 L- K5 E
  4. This is the largest integral value <= x.
    . i. |# o' ?' }0 M9 }& L) {; b3 K1 ^
  5. >>> math.floor(4.1)
    , z4 A& n, S& s- W9 O( i/ g
  6. 4
    % j# T+ W2 p' p! T/ {6 W$ |
  7. >>> math.floor(4.999)2 q' h' F- r' d7 ^' W: I- j/ s
  8. 4
    + d  N7 f/ x+ X
  9. >>> math.floor(-4.999)
    ) j8 G0 [1 R7 ^, _! Y
  10. -5
    ' v" i* J' P( F) o4 e6 S
  11. >>> math.floor(-4.01)
    ! g% K) X: E4 Y( w+ Y6 [
  12. -5
复制代码
" r2 p1 m2 G9 ~# _( R$ e" A
math.pow(x,y)  返回x的y次方,即x**y/ y) R* e& ~. u4 g2 G
  1. #返回x的y次方,即x**y
    3 p1 o+ J  c2 E: ?! G% S. x
  2. pow(x, y)
    9 y. b% r2 j6 t* S3 H! G, G# `6 D4 B" e& ^
  3. Return x**y (x to the power of y)., U( v! ~# m# P) ^( E
  4. >>> math.pow(3,4)2 A/ T$ O  x3 G$ R; t
  5. 81.0
    . D7 t/ z( C% \
  6. >>> ) c) V& `5 K4 a/ \7 R
  7. >>> math.pow(2,7)
    ' n1 R# V, [6 ?; _
  8. 128.0
复制代码

; U$ i3 ]3 ^% {+ j7 {! {$ |math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
( }( Q6 l# P+ K/ Y% E
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    3 f  m" o4 O, V! |% M: u; V
  2. log(x[, base])- e8 Y* s: _& |9 P, @  D# s  V3 W
  3. Return the logarithm of x to the given base.
    & c7 c! B$ N$ D
  4. If the base not specified, returns the natural logarithm (base e) of x.
    3 e& m0 D( h+ m% |! V# w" Q
  5. >>> math.log(10)$ X; [+ x5 J3 l+ `; r
  6. 2.302585092994046
    / _1 l6 x+ V& m+ W( X" g
  7. >>> math.log(11)
    ; N. F# V" x. g& @' N& k4 x% u
  8. 2.3978952727983707+ E' f% M% e2 q- V  S4 h) C
  9. >>> math.log(20)
    2 Y1 Y# o+ ~3 j% _+ v3 ?
  10. 2.995732273553991
复制代码
( t7 S- i  t3 I9 ?: ~" S
math.sin(x)  求x(x为弧度)的正弦值
8 {" s) e  q$ `% Y2 J9 X
  1. #求x(x为弧度)的正弦值0 j7 w' M9 t& E* K' O) Y- P. G
  2. sin(x)
    5 H7 x4 r& H7 ~5 Z
  3. Return the sine of x (measured in radians).
    ) s% U# ~% M- w( {3 _
  4. >>> math.sin(math.pi/4)
    5 j) _' E, y, M- G9 T2 e7 a: d+ w
  5. 0.70710678118654755 G) ]. F# g$ Y9 f+ X3 k
  6. >>> math.sin(math.pi/2)
    4 K. l4 t4 {4 P: d) ?/ n3 T
  7. 1.0% G- e8 s! c( f8 z0 T  F
  8. >>> math.sin(math.pi/3)
    2 t. }: F8 R: L5 g0 n# `. x
  9. 0.8660254037844386
复制代码
% ~+ F% m$ A$ g) e' m& C4 q+ R
math.cos(x)  求x的余弦,x必须是弧度: I: e+ K) K# d; \
  1. #求x的余弦,x必须是弧度# c) Q" Y8 I! J9 G3 l0 E" h% O
  2. cos(x)5 D+ U1 X7 `, @4 M. Y7 m. K5 G8 b
  3. Return the cosine of x (measured in radians).$ Z. n8 v% f# q+ ]
  4. #math.pi/4表示弧度,转换成角度为45度( B  E+ D& C9 {% C, o
  5. >>> math.cos(math.pi/4)+ [& x- ~3 L, t
  6. 0.7071067811865476
    , R3 r8 M& i$ g; |- D  t
  7. math.pi/3表示弧度,转换成角度为60度
    , j) x! |: J% d# z3 g; F
  8. >>> math.cos(math.pi/3)( Y, ]9 k# C- s; p
  9. 0.5000000000000001
    : h7 d$ t+ S) a& J! ]& ]( p# _1 I
  10. math.pi/6表示弧度,转换成角度为30度
    + |$ m- ~1 u/ v* z3 N0 d. u7 _2 ]
  11. >>> math.cos(math.pi/6), I4 R4 B, p$ w; {
  12. 0.8660254037844387
复制代码

2 {" B, Y/ M4 i& W$ e) Tmath.tan(x)  返回x(x为弧度)的正切值
. F/ O; _9 {  V% l$ E3 }
  1. #返回x(x为弧度)的正切值- k& s& C8 g9 B- p# o( F( g
  2. tan(x)* R( U& u; x* v9 N
  3. Return the tangent of x (measured in radians).' f4 m. F, W) @" _2 D* J
  4. >>> math.tan(math.pi/4)1 D# W9 m" Y: N1 H4 {
  5. 0.9999999999999999
      v9 c) H; n* v& C) A
  6. >>> math.tan(math.pi/6)
    & h3 b1 K+ z2 c
  7. 0.5773502691896257! X! h0 n8 V0 A* o% z
  8. >>> math.tan(math.pi/3)
    3 _# v2 I& S' l7 E9 `3 n9 @0 A
  9. 1.7320508075688767
复制代码

* }( D+ C( p7 f0 U' Jmath.degrees(x)  把x从弧度转换成角度
1 ?3 I- p) y0 \  J  w
  1. #把x从弧度转换成角度
    2 ]2 J9 z9 P; U% z' t( L- u
  2. degrees(x)
    7 C5 ]# s4 i1 w5 Y- z
  3. Convert angle x from radians to degrees.8 O9 Q( K* M. l7 w9 n2 v4 m
  4. 2 o( G6 V2 @9 V3 ^! e
  5. >>> math.degrees(math.pi/4)
    9 E* I% t$ L4 O' a
  6. 45.0
    7 {0 M- g% X4 L7 |% i
  7. >>> math.degrees(math.pi)
    1 }8 p& n( [2 o
  8. 180.0
    * D4 N" F9 o5 k  s
  9. >>> math.degrees(math.pi/6)
    6 q% @( D2 C* z7 i5 X
  10. 29.999999999999996
    - |% n$ D. ~; e* |! l8 w7 `& F$ W
  11. >>> math.degrees(math.pi/3)! ]% U- g7 b5 t; J! s, Y  i1 ^7 c& L
  12. 59.99999999999999
复制代码
3 X! ~) B4 u  `* p- F
math.radians(x)  把角度x转换成弧度
& l+ @% V4 \6 w( Q
  1. #把角度x转换成弧度3 a9 e; {! W' e1 r0 k7 C
  2. radians(x)
    " W( D- G/ @  q' I  u" t
  3. Convert angle x from degrees to radians.8 |+ c) c1 j( p' Z  v2 Z
  4. >>> math.radians(45): s. L: n: q8 u7 t
  5. 0.7853981633974483$ I% D8 X6 I' |4 C
  6. >>> math.radians(60)
    9 T+ N$ V  u# i5 K" F; U/ F
  7. 1.0471975511965976
复制代码

3 A# E  R$ b, N( z$ Mmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
1 P) S( r. K, ^8 |5 x; S
  1. #把y的正负号加到x前面,可以使用0
    ) O+ q. \( [; v
  2. copysign(x, y)+ \7 l' ~7 g6 U2 ~- o
  3. Return a float with the magnitude (absolute value) of x but the sign
    $ F8 e2 `6 ~: V/ {4 }; H9 X: x5 n
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , U: b  o8 {3 E( q# ^9 G
  5. returns -1.0.* F- t" {1 J: O) {- Y
  6. . @" c/ }" v% f. Y$ D6 J6 n
  7. >>> math.copysign(2,3)5 x! ]6 S  m+ j' L  r# ?& P
  8. 2.0  M% q# J- F5 o' ~; v9 p$ \
  9. >>> math.copysign(2,-3)+ G7 r9 o; Q, F; Z' [2 l
  10. -2.0% |$ X8 K$ Z3 @7 R- W
  11. >>> math.copysign(3,8)
    ( k  X& ]0 C% r. y
  12. 3.0
    ( Y7 b* M  k9 b
  13. >>> math.copysign(3,-8)( I3 L" T# H& m
  14. -3.0
复制代码
  Q* T$ l- s0 k5 H; S
math.exp(x)  返回math.e,也就是2.71828的x次方% \6 I) \/ j. D) |# J
  1. #返回math.e,也就是2.71828的x次方
    / O* o# a; z' Y) T. S
  2. exp(x)
    $ u0 R. w1 B* ~0 o4 C, K- Z# _
  3. Return e raised to the power of x.  }2 u: f2 Y: n' ?( t

  4. ! X7 w6 A, K* S; {0 Z; P2 `$ ~; `5 |
  5. >>> math.exp(1); n0 U) \. l; J. Z; ~
  6. 2.718281828459045
    ! C6 S3 k7 C; u0 n1 C2 o
  7. >>> math.exp(2)
    " z- m8 F' l# ~" `
  8. 7.389056098930659 u2 Y- k7 J2 `0 p. J! [
  9. >>> math.exp(3); l. a8 `. @4 _' S+ B
  10. 20.085536923187668
复制代码
# p* j6 h3 l1 J6 `" Z
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减15 @7 g2 ]6 T! R) T9 x% [. ~4 P
  1. #返回math.e的x(其值为2.71828)次方的值减1
    : J8 N5 {" C' P5 Y3 ~2 c. y
  2. expm1(x)
    + @0 N- z/ w' U' }5 H. ~
  3. Return exp(x)-1.2 s  O4 S/ A5 p7 v+ t: |
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    2 h9 K7 F0 m0 V' r6 ~# l& n

  5. 6 ^! u* v7 i+ L1 [" Z4 I$ n
  6. >>> math.expm1(1)
    1 Z# ]) s  ?4 L3 a2 g5 r4 k
  7. 1.718281828459045
      D8 ]4 \0 f) t8 ~
  8. >>> math.expm1(2)8 e, ?8 ~7 M4 B& \
  9. 6.38905609893065
    $ [3 \' P) l  M# q% n
  10. >>> math.expm1(3)+ ~" G7 w# g+ p2 n0 m3 r6 T+ H
  11. 19.085536923187668
复制代码
7 u) Z9 c3 e  I7 x
math.fabs(x)  返回x的绝对值
+ K+ A; j1 H3 t9 J  ^' h) c
  1. #返回x的绝对值
    . P- d" c) f" y
  2. fabs(x)
    $ o$ R( [0 I/ I8 ^. x
  3. Return the absolute value of the float x.
    & h0 L$ u1 m+ x# V( H0 r
  4. - O$ X4 R5 e! H+ s1 r; B
  5. >>> math.fabs(-0.003)' g3 P3 R: e7 M" q0 B% c
  6. 0.003' \3 C* Z. m; V) c0 W
  7. >>> math.fabs(-110)
    * U# b5 {7 l+ D
  8. 110.0+ `5 q2 _: T/ d" E) C
  9. >>> math.fabs(100)4 ^, T3 y7 ~* I! f
  10. 100.0
复制代码

: }8 x7 A6 |2 W* m8 T& `' Zmath.factorial(x)  取x的阶乘的值
% p; x, v# K0 p% L& T& v. ^
  1. #取x的阶乘的值
    0 i4 n9 d! v7 v# C. e4 f9 R
  2. factorial(x) -> Integral2 Z& D7 K4 x! R  q9 Q
  3. Find x!. Raise a ValueError if x is negative or non-integral.9 s- A0 `2 t% a, q; l( X* F
  4. >>> math.factorial(1)# {: E7 F9 D5 R. ?: Q+ y
  5. 1
    - `+ i" s3 K; S& ~8 Z# r
  6. >>> math.factorial(2)
    5 M: {7 O9 H$ ^& x3 G" ?2 J% W
  7. 2
    8 O, W0 b( I* F8 `5 u! h% f* R
  8. >>> math.factorial(3)
    & T3 \7 E9 d! e! B/ ?, s0 {/ Q
  9. 60 Y. s* H9 I5 X" [
  10. >>> math.factorial(5)& s1 |+ V8 `2 Q1 b: p+ G. r
  11. 120- Z+ D- H/ e; A/ k2 s
  12. >>> math.factorial(10)1 `7 [3 c& |' G: V7 U* N) G! H3 ]
  13. 3628800
复制代码
! G4 T  B  ?2 C' e2 w/ f% r/ S
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数: _) K: v, Q/ s
  1. #得到x/y的余数,其值是一个浮点数7 T9 N3 l. i# l' e) r
  2. fmod(x, y)
    ) r: V0 [3 `; Y+ V# c" H- M
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    $ ^$ R1 [9 h0 M  s: N  f
  4. >>> math.fmod(20,3)
      ]0 R" Z" V9 m" z( Z# k
  5. 2.00 [+ I3 R9 \( N
  6. >>> math.fmod(20,7)) r7 U2 i% ~  ]2 p8 u* h
  7. 6.0
复制代码

" j5 `2 [! S% ]0 Vmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
: _+ f7 h6 `' j1 @, O0 J) o
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    9 w4 \( j; c% G+ e8 W
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    3 Y6 X8 ]" a( L; A6 n: L% k2 K# F. \
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 S8 G+ z! a, P- F7 g# Q- N  G, d
  4. frexp(x)+ S+ L( O  c# z
  5. Return the mantissa and exponent of x, as pair (m, e).  t% E: F. b, c! J* J+ i
  6. m is a float and e is an int, such that x = m * 2.**e.3 z4 e* {& `8 f5 S/ d" d
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    : k" o# a, L" u4 {' O1 W
  8. >>> math.frexp(10)$ r& N. ^2 l9 \/ Z$ _
  9. (0.625, 4)
    7 _( p! s5 T3 k+ f. n1 j
  10. >>> math.frexp(75)5 V, e8 C# a) m) U; n5 m
  11. (0.5859375, 7)
    2 V- t3 q6 L9 S5 [. s, K/ n- L- @
  12. >>> math.frexp(-40)+ e" R: ?' l" V" t% M$ E
  13. (-0.625, 6)
    / p4 N, e/ s6 K* ]: s- @" M9 }4 C# P
  14. >>> math.frexp(-100)
    ' e% m7 P8 w6 @/ u9 N
  15. (-0.78125, 7)
    7 i. t" G% m5 [9 \% _0 C
  16. >>> math.frexp(100)! g1 {5 c5 {* x8 ^
  17. (0.78125, 7)
复制代码

1 J) o1 o6 I- V3 smath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
% _; [: H0 z4 P, T- z
  1. #对迭代器里的每个元素进行求和操作2 |+ A: }& @7 @$ r+ V3 Z
  2. fsum(iterable)
    : K+ q+ F9 P# c* w4 w9 E7 C+ a4 b
  3. Return an accurate floating point sum of values in the iterable.
    8 U8 w: w4 G, K4 m2 b6 w) }
  4. Assumes IEEE-754 floating point arithmetic.
    & X+ F; N  T9 v1 I: D8 U" o  Q: y* v
  5. >>> math.fsum([1,2,3,4])
    9 w" ?6 U' u3 q% C0 A
  6. 10.0
    # M+ H- b- H, J! l& p
  7. >>> math.fsum((1,2,3,4))
    & j$ ~% n) W$ r+ c% ~1 X; v
  8. 10.08 @  Z4 {* U! ]' N3 a
  9. >>> math.fsum((-1,-2,-3,-4))
    ; [% O, p8 T( o
  10. -10.0. z/ _5 Z# @2 t
  11. >>> math.fsum([-1,-2,-3,-4])+ O, E( A/ T; E6 W# B- B
  12. -10.0
复制代码

; c$ s7 q# a; f5 j% Ymath.gcd(x,y)  返回x和y的最大公约数
% L. ^8 @( x2 T
  1. #返回x和y的最大公约数
    6 w9 K- f5 c- g1 `$ J
  2. gcd(x, y) -> int2 b/ w" R# g3 v4 _  @7 ?, ?
  3. greatest common divisor of x and y0 O2 T" b; C* a& ^, n) \) |
  4. >>> math.gcd(8,6)
    ( ~# G8 M4 Z$ C( S3 U2 {) |
  5. 2
    5 `0 T/ y! [% P! X. ~. a. k
  6. >>> math.gcd(40,20), {! ^5 r" j0 I7 \: w# R( V- b
  7. 20
    ! e/ ^# t3 [, R3 l+ K$ v
  8. >>> math.gcd(8,12)
    , {- i. n; |5 m
  9. 4
复制代码

) O& j7 Q+ O8 q" d9 D$ u: Y. Wmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 w  O8 F3 D* {+ _8 E
  1. #得到(x**2+y**2),平方的值% F5 e* ?  `0 n
  2. hypot(x, y). v. t4 m. I- l9 |
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    6 C& `  e9 j" o
  4. >>> math.hypot(3,4)
    ! L; s: e) Y3 x2 M8 g6 [* R, g
  5. 5.00 ]) H8 o0 _$ `5 Z4 J* g# ]+ n. R! ?
  6. >>> math.hypot(6,8)
    7 W; [5 U2 U+ ]
  7. 10.0
复制代码
3 B3 E7 i& V3 B7 `3 H
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
( D" H: {: ?4 B5 _; j8 i- S
  1. #如果x是不是无穷大的数字,则返回True,否则返回False! y1 l& L4 l, i
  2. isfinite(x) -> bool
    " X( M9 t( M0 E
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.4 \6 D2 z* I  W  q$ G+ G5 R
  4. >>> math.isfinite(100)
    2 O# ~* g, V. {5 X. U
  5. True* U  y( ^! l) L6 q9 z9 J; x" J3 n
  6. >>> math.isfinite(0)9 I% m: V3 q  Q- w$ g% ?! _% L
  7. True
    1 F6 J7 F2 r- T% F: B+ |$ N' y
  8. >>> math.isfinite(0.1)% h: i) n. P2 {  J
  9. True& b; d: I2 r* D2 B
  10. >>> math.isfinite("a")
    6 B+ `# S. w7 W7 {' ~% A
  11. >>> math.isfinite(0.0001)
    0 K; Q6 h0 a" T1 x4 g3 _# R2 y
  12. True
复制代码

. n) U' e4 k1 E0 _: Gmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False4 P8 M9 t3 e- k/ \
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False2 M  m5 |; P6 g* j( ^
  2. isinf(x) -> bool% v+ |/ g5 ~+ J/ F* e7 ?
  3. Return True if x is a positive or negative infinity, and False otherwise.( u1 }0 u8 G! a" X& }
  4. >>> math.isinf(234)
    * E  N( U: J( {+ {" i: ~
  5. False- z, a- U- ^; [
  6. >>> math.isinf(0.1). ?5 P& I! c' u- U- d
  7. False
复制代码

6 l/ Q" ]% B! I& x, I1 T* Omath.isnan(x)  如果x不是数字True,否则返回False
4 F: B- g6 l; _5 u& r
  1. #如果x不是数字True,否则返回False  i% ~' P( o: N' ^( ^
  2. isnan(x) -> bool9 H2 Z# E5 R  J! D( E2 Q
  3. Return True if x is a NaN (not a number), and False otherwise.1 A# Q- S1 _7 I( a5 |% P8 q
  4. >>> math.isnan(23)
    1 d4 R% r* {2 u' ?6 T  a& P
  5. False' @% M9 c! k% v9 `, J, I
  6. >>> math.isnan(0.01)1 @) q1 n& k' D' `* y
  7. False
复制代码
& r: _) z; n7 X3 e; }! T& H
math.ldexp(x,i)  返回x*(2**i)的值, k! p$ C9 F% W
  1. #返回x*(2**i)的值
    7 n  C  b. J$ X9 {. a1 U
  2. ldexp(x, i)2 w* m) u' Y9 i0 A) b
  3. Return x * (2**i).' B& R% d9 {7 n/ s6 B
  4. >>> math.ldexp(5,5)+ P8 Y1 v8 d+ T3 \& R
  5. 160.0
    * l2 y3 Q0 k* z- @0 {9 ]
  6. >>> math.ldexp(3,5)% B, Z  Z8 {; Z: a1 }4 N
  7. 96.0
复制代码
2 U  c- ]) x, M8 @4 {
math.log10(x)  返回x的以10为底的对数
) e/ \( M5 T  t4 B* \
  1. #返回x的以10为底的对数6 o& f9 R3 P0 z
  2. log10(x)4 N* c+ A2 l! E* D6 k+ q
  3. Return the base 10 logarithm of x.
    5 Q2 Y/ F/ K- f- q0 J
  4. >>> math.log10(10)
    # d: Y7 O& b. P3 a- \
  5. 1.0
    7 T+ L# J/ L3 C  Z+ X+ n
  6. >>> math.log10(100)
    - ]7 _7 Y8 n  N: e; V/ m4 F
  7. 2.0) l; c% ]3 `* t$ m
  8. #即10的1.3次方的结果为20  K5 h/ L8 x% S2 v4 Z  V7 _
  9. >>> math.log10(20)/ A- ?/ Z8 `( s* a4 e& b
  10. 1.3010299956639813
复制代码

/ s0 H3 |$ j' G4 Omath.log1p(x)  返回x+1的自然对数(基数为e)的值( ~( k* e1 f& Y4 y& P" N* F
  1. #返回x+1的自然对数(基数为e)的值
    9 r5 @2 W; Z; {
  2. log1p(x)
    - `; G. Q3 p6 Y+ ?/ u( f
  3. Return the natural logarithm of 1+x (base e).6 Z/ S3 r) M; q& r' b9 a, s
  4. The result is computed in a way which is accurate for x near zero.
    , i2 k3 l9 v$ V. f* P
  5. >>> math.log(10), {3 `' t6 D; P$ D. W6 I& X
  6. 2.302585092994046
    8 V4 h. R  ~& K0 }  u0 l8 ]* i
  7. >>> math.log1p(10)) v0 d" t2 u# T, A
  8. 2.39789527279837077 F# [* ]) u7 l! u* H: C, Z# a5 G
  9. >>> math.log(11)$ K4 [. m  Y1 @
  10. 2.3978952727983707
复制代码

2 f. a! i! L+ V/ b. dmath.log2(x)  返回x的基2对数
7 w! [% B0 C+ ?! t; [
  1. #返回x的基2对数
    , S7 A+ @6 {6 d
  2. log2(x)
    2 ^' B6 n5 K8 T' ^1 \- A2 L0 k
  3. Return the base 2 logarithm of x." x/ k% c" A/ D9 M& T4 B4 c$ W8 I
  4. >>> math.log2(32)
    ) X: `1 Y6 b! {% x6 ?; [, y( p% H
  5. 5.0
    / ]* z/ W/ X- c; |
  6. >>> math.log2(20)" A0 J: \) M. K7 U
  7. 4.321928094887363
    # g' p# w0 b/ T* u! Y+ B( \
  8. >>> math.log2(16)1 m! ^6 H5 ]+ h: V4 u7 y# x. }/ q1 X
  9. 4.0
复制代码

  w% E. E: O% Y; Emath.modf(x)  返回由x的小数部分和整数部分组成的元组
; a& N4 M8 d4 O
  1. #返回由x的小数部分和整数部分组成的元组
    9 f  R! C9 ]5 Y' C$ \% L
  2. modf(x)
    . E& U3 r4 a& g0 Y7 N, U/ J
  3. Return the fractional and integer parts of x.  Both results carry the sign
    , s+ @- ?( {6 E
  4. of x and are floats.5 q! e: I% o" i6 C
  5. >>> math.modf(math.pi)+ q# ~  o( }8 k8 f, Y- v
  6. (0.14159265358979312, 3.0)
    - A6 L! V) W6 W! U6 V7 R
  7. >>> math.modf(12.34)& R2 h  \1 i) }. O
  8. (0.33999999999999986, 12.0)
复制代码

  A0 |; U6 G5 W" q# a- Y1 jmath.sqrt(x)  求x的平方根# d* V" F/ H/ B5 S" F( e' a7 D
  1. #求x的平方根( y8 m1 _3 a! [2 \4 q6 g
  2. sqrt(x)1 _: o8 c' y/ @7 _
  3. Return the square root of x.% z/ b: V9 R2 V( g* Y  J7 P9 u
  4. >>> math.sqrt(100)  |9 L# j# M/ Z. T0 d
  5. 10.0! y: s/ ^4 y/ U" F
  6. >>> math.sqrt(16)1 ]2 i' B; R2 \# o3 S& k. x( I
  7. 4.0
    ! C' C6 V9 O. T7 b1 S# t
  8. >>> math.sqrt(20)6 \+ w" f0 R& v: f$ {5 |
  9. 4.47213595499958
复制代码

+ W5 Y7 j: H7 }, Y7 Hmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分8 z( Q8 b4 }! `9 Q. e. }. p
  2. trunc(x:Real) -> Integral
    ' @+ \, G! Z+ l5 n  ~; j
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.* R* h7 ]& T# R
  4. >>> math.trunc(6.789)
    7 @/ K# R; i& ]' f: z1 s
  5. 6
    4 a1 k3 r. ]+ C
  6. >>> math.trunc(math.pi)
    + U. b$ v' i( t7 k3 X( h6 f! c, l
  7. 3
    7 {( |+ h$ B# ]+ h( A0 `. Z% R
  8. >>> math.trunc(2.567)4 G. e4 I" L* {, R0 C# }3 E
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-12 09:15 , Processed in 0.081428 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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