新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

' t) q$ c, ?) f. s$ y【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。6 B, n1 m4 V( i# K0 k

" ~  m6 F/ o) P( E3 K方法1
3 u) r7 }6 v* L. _
  1. >>> import math
    " G4 V# a1 k* c3 A* N: s; f3 T
  2. >>> math.sqrt(9)8 I$ q5 a, B( a6 J0 V
  3. 3.0
复制代码
方法2- x3 e* C8 l2 _$ C
  1. >>> from math import sqrt3 {7 A( P' Z. K- p/ n* B
  2. >>> sqrt(9)
    2 y/ u3 Q$ g% W
  3. 3.0
复制代码

- B3 F3 z4 `. V) S$ ?' U+ H- P1 X

. _# O$ ]- e4 [% p! i
math.e  表示一个常量6 G# l/ H: B2 y9 `/ f/ K
  1. #表示一个常量
    $ I+ j" T8 {9 f6 J0 k+ |# l
  2. >>> math.e  a3 u3 }0 x: o) g
  3. 2.718281828459045
复制代码

( e. N5 u' z; f  P5 D5 o4 J" U  Q2 vmath.pi  
数字常量,圆周率
7 T3 f' h; W4 C8 M8 J1 y
  1. #数字常量,圆周率
    5 i0 P( M) z  i/ g) O
  2. >>> print(math.pi)
    8 q7 B2 |" e% s) P9 l
  3. 3.141592653589793
复制代码

7 Z" Q, t6 O8 a1 t! G! g9 Nmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
# k" N. q2 B2 a7 D9 b7 M# W
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 S3 u. H6 q* ]' Z. z" v
  2. ceil(x)
    7 M7 f4 i+ z6 K7 Y
  3. Return the ceiling of x as an int.
    $ x0 u; Y- X: t) i" g6 b* Y
  4. This is the smallest integral value >= x.; ]' N5 l/ d, \; u: Y" ^1 X2 l9 D# t

  5. , K! }$ o+ b$ Q  V1 z; l) _  R
  6. >>> math.ceil(4.01)
    % }6 D  F: S3 Z" w' C& e
  7. 5
    - T+ ?/ a* Q0 H, I- L, O* ^- l
  8. >>> math.ceil(4.99)
    6 B# S8 O7 L- H: T' q* l
  9. 5
    ) p5 j9 n- i' e! N3 l* H$ [# h
  10. >>> math.ceil(-3.99)( x# f4 p4 G, B: B9 X6 k
  11. -34 M- I: z; I! D4 [+ _
  12. >>> math.ceil(-3.01)
    8 g. U9 z/ R; C5 u$ E$ c0 N
  13. -3
复制代码

4 o3 D" g3 I* b9 w" F% \math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 m5 ]5 V& a/ R, Z
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ u! C# G' S/ f. N+ ]
  2. floor(x)" X+ y. B: V5 \2 Z
  3. Return the floor of x as an int.
    8 h: z" ?5 v& l! C$ Y
  4. This is the largest integral value <= x.
    9 L* a- a3 C7 k4 x
  5. >>> math.floor(4.1)( w  W* e( x, A7 F
  6. 4; Q  M! j# r2 w- v
  7. >>> math.floor(4.999)
    - D7 D# V# Y% K' z
  8. 4, w  V( ~% H- D& O5 i( h
  9. >>> math.floor(-4.999)
    ) j1 q' ]% S  |! @; h6 [( Q1 G
  10. -5
    : b+ v) g3 {6 g: U7 H
  11. >>> math.floor(-4.01)
    - q$ S: O3 e: v) H! s0 ~+ z: I2 r
  12. -5
复制代码
( z  j8 F! Z1 D# d2 r# w
math.pow(x,y)  返回x的y次方,即x**y
1 E' f6 N, e" x- z
  1. #返回x的y次方,即x**y9 R1 S  Y" z  x, Q. |2 A; \) E
  2. pow(x, y)2 h' `; t8 ]6 o& E
  3. Return x**y (x to the power of y).
    0 ~# g( O+ V" G& W) R3 ]
  4. >>> math.pow(3,4)' Z- R' c% m! M$ n: Y
  5. 81.0
    ; z' z9 k9 \9 q; I/ F
  6. >>>
    9 Y9 ]7 v$ _" y
  7. >>> math.pow(2,7)
    - e" G- s. r% W+ I8 i1 d
  8. 128.0
复制代码

/ @0 W- m- M3 E, U: x! n) Gmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)' i/ k, ~' m  C; U! D5 N  |! g
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    7 p5 k1 O* H" ^9 E
  2. log(x[, base])
    $ ]! d1 T9 R! ~: h  t' b# q
  3. Return the logarithm of x to the given base.
    ( O. A, t/ G2 v- I1 H: {& @+ R& e
  4. If the base not specified, returns the natural logarithm (base e) of x.
    4 Z* x; H' n' J
  5. >>> math.log(10)8 I8 `; @# G5 T+ I
  6. 2.3025850929940460 d4 W6 K' E, t
  7. >>> math.log(11)% S. G% ~% U) F* Q- j
  8. 2.3978952727983707% Z, B" @: t" X6 k, k! I: B
  9. >>> math.log(20)7 p8 p8 U' `* r* T- M* D* Y) b
  10. 2.995732273553991
复制代码
; Q0 |; A; t) J3 I* R7 W+ l/ a! P2 ]. C
math.sin(x)  求x(x为弧度)的正弦值* i4 o9 V  E* C% {6 ~1 V! j+ f
  1. #求x(x为弧度)的正弦值5 ]; @1 d  x/ T- Z
  2. sin(x)) M1 Y( @& k0 K" a1 E1 d
  3. Return the sine of x (measured in radians).
    : i, i  I; `' D& k/ Y
  4. >>> math.sin(math.pi/4)7 u& n8 H/ \. h( P9 Z% G
  5. 0.7071067811865475# H" s' u( D# l/ i2 `8 k
  6. >>> math.sin(math.pi/2)
    2 t: A0 U5 \! O; a4 q
  7. 1.08 p* i7 @# M$ d- m  b. i3 q
  8. >>> math.sin(math.pi/3)3 i0 I0 X9 v' y1 E3 a  ^& P
  9. 0.8660254037844386
复制代码
0 a9 U5 s4 ~5 j# u0 m/ z0 c) p
math.cos(x)  求x的余弦,x必须是弧度& |0 p7 p5 C3 H( i, L$ Y
  1. #求x的余弦,x必须是弧度
    5 u9 Q" Q7 o1 z, y) Q, ~, @
  2. cos(x)# E' c3 M" n* v
  3. Return the cosine of x (measured in radians).
    ' r: D: H0 S% |* v. c& H
  4. #math.pi/4表示弧度,转换成角度为45度* L# l9 C: I) y8 `- X
  5. >>> math.cos(math.pi/4); ?2 E8 g1 I( |% e/ }
  6. 0.7071067811865476
    ( g( i5 b# i2 f
  7. math.pi/3表示弧度,转换成角度为60度+ A1 L  [& m3 w9 X
  8. >>> math.cos(math.pi/3)
    8 k! M- d2 P6 E8 t2 M2 r; C6 |+ }
  9. 0.50000000000000019 F& s5 p, h3 N# R
  10. math.pi/6表示弧度,转换成角度为30度
    + Z/ E! d4 o, R2 k) ]' l" X' [2 J
  11. >>> math.cos(math.pi/6)6 n. a; _8 Q7 `
  12. 0.8660254037844387
复制代码

/ E8 j1 {6 l$ kmath.tan(x)  返回x(x为弧度)的正切值
6 n0 k+ S; i) `& X1 {7 }
  1. #返回x(x为弧度)的正切值
    7 M3 k. I/ Y4 T: E1 Z
  2. tan(x)
    % \7 @, w  ~& t
  3. Return the tangent of x (measured in radians).3 Q  J- U$ `. d- r+ q' j
  4. >>> math.tan(math.pi/4)
    8 W' i! s$ @+ Q( s6 @
  5. 0.9999999999999999
    6 w/ @# y' R# o2 D7 c6 a9 `; `
  6. >>> math.tan(math.pi/6)) p& ~, o3 F+ X- V3 P9 Y" r# D
  7. 0.5773502691896257
    : [" @1 j8 y2 C# j# F& {
  8. >>> math.tan(math.pi/3)
    " R# ^; h" @/ n+ F( I8 U
  9. 1.7320508075688767
复制代码
; b. m. d* T1 L2 M6 m3 F4 Q+ W& C
math.degrees(x)  把x从弧度转换成角度$ I( i7 o  A" `2 K) D6 O
  1. #把x从弧度转换成角度
    1 ^9 [& q* Y2 A' L
  2. degrees(x)9 t' c3 d) B7 y
  3. Convert angle x from radians to degrees.- M$ l1 R  D. S; E- _& F0 _

  4. 0 o8 S* H- @7 W
  5. >>> math.degrees(math.pi/4)- A9 m, [! G4 m
  6. 45.0
    " I/ P4 j2 X( j9 h" K/ k) }
  7. >>> math.degrees(math.pi)
    ' L- @0 ^) c3 m! S9 F  Q
  8. 180.0% Q$ U0 P" {1 L8 C* p1 |
  9. >>> math.degrees(math.pi/6)& g: h: ?, d; V5 L
  10. 29.999999999999996
    6 o# f; O* S4 G& [
  11. >>> math.degrees(math.pi/3)  w0 q8 C: P. G* o; D
  12. 59.99999999999999
复制代码

  h+ E6 Z. H  b( T! ?math.radians(x)  把角度x转换成弧度/ n% U2 N9 [1 X4 E) K; t
  1. #把角度x转换成弧度
    + C/ R& v3 A. K- E4 r' j+ Q0 A
  2. radians(x). Q) Z. X5 v: w7 S8 w
  3. Convert angle x from degrees to radians.. N2 d- I& X0 e( J7 w% W
  4. >>> math.radians(45)/ s. ]! X8 `! o* l# _8 S6 ^
  5. 0.7853981633974483
    - @2 i9 C2 G! @, [% M
  6. >>> math.radians(60)
    3 Y5 C% @4 X) F' Y% k! d; g
  7. 1.0471975511965976
复制代码

) w1 s( |( [, O+ [1 T$ I, [' m/ e, pmath.copysign(x,y)  把y的正负号加到x前面,可以使用0
: D: ]& K$ D7 I2 d3 x. R) e' y
  1. #把y的正负号加到x前面,可以使用0
    ( E' _8 @& q9 y, N+ u
  2. copysign(x, y). T- {( C% R) G# r+ K. T  B8 F6 b
  3. Return a float with the magnitude (absolute value) of x but the sign + `& X1 n( ]/ ^, Z1 R7 k
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) , @2 o2 b, B3 i( h7 A" Z+ b
  5. returns -1.0.& F7 z6 U- F$ N$ u/ g  p  [7 J5 E

  6. : G  _5 x( i) X4 G9 {" v3 {1 P5 |
  7. >>> math.copysign(2,3)  w+ g6 M7 _7 x( M. B7 q; ?
  8. 2.03 c, T7 n' }. h( z: i2 ^
  9. >>> math.copysign(2,-3)* `9 {1 U- l% m- y! J9 q
  10. -2.0
    ) T+ g& x/ H* S6 R& w$ Q- H
  11. >>> math.copysign(3,8)' k+ N2 b4 |3 h: a! Q& M$ B5 {
  12. 3.0
    ) I4 j+ A2 Z  G( p: `7 a& c
  13. >>> math.copysign(3,-8)
    0 A& j4 R" I9 k3 T  X. Z( m
  14. -3.0
复制代码
3 r2 s7 I! N' E/ I
math.exp(x)  返回math.e,也就是2.71828的x次方0 X: {( d# V5 S. q
  1. #返回math.e,也就是2.71828的x次方
    $ s3 [  V  p% k/ m) D8 @* x" W
  2. exp(x)
    . L0 R% d0 A. u5 C
  3. Return e raised to the power of x.; N& I" b( o* P. c  Q
  4. ' q, K& y4 K9 h. h$ b
  5. >>> math.exp(1)7 i) f+ i% C0 l% h7 g3 Q
  6. 2.718281828459045
    ; t8 e# d, `. V: q0 `& G: I
  7. >>> math.exp(2)8 ~; U# P: t% n
  8. 7.38905609893065
    - ?. O; L% t9 d! N0 a+ P
  9. >>> math.exp(3)
    * n8 S* b) s+ E0 d
  10. 20.085536923187668
复制代码
2 \1 K$ Z" x# x7 u7 I, h
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1- B' `  ]" `" e; g6 v# K7 U' l7 o
  1. #返回math.e的x(其值为2.71828)次方的值减15 W4 ?4 S( L; `+ \' U
  2. expm1(x)! _' Y5 l; M* q5 E: y
  3. Return exp(x)-1.
    6 r: K! ?* ^. G( `
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.; @& H1 v* ?6 W+ g

  5. ; P+ F3 g) g, o( [) d2 l3 U+ P
  6. >>> math.expm1(1)  v9 z( w: [9 j2 q, Q; r3 E
  7. 1.718281828459045. E5 ^# `% ?3 s- G+ x" h! ^4 `
  8. >>> math.expm1(2)
    % F3 s) X- m( a) g  T
  9. 6.38905609893065( F/ _; D5 \1 W& A$ r
  10. >>> math.expm1(3)
    + i! h- P2 G6 a# \; \- U
  11. 19.085536923187668
复制代码
% M4 _" z$ ]# O" F$ W
math.fabs(x)  返回x的绝对值& b" o7 f6 r5 ]; D9 p; K5 l
  1. #返回x的绝对值" r& R" H3 o; M/ `" y* F
  2. fabs(x)
    5 i0 A+ x; D: m5 P* ?
  3. Return the absolute value of the float x.
    9 o( @* |" s% r# @

  4. ' j: y7 ]# U: F# }1 [- E, ]
  5. >>> math.fabs(-0.003)
    ; i! v3 Y/ s4 X  a$ [
  6. 0.003
    6 T" Z" d! m5 @$ a
  7. >>> math.fabs(-110)0 u) L$ v' _4 E) S
  8. 110.0# V! ]/ P9 z" x) @6 x
  9. >>> math.fabs(100)! B. x5 l+ ^% n  h0 x
  10. 100.0
复制代码

/ R' Z' Y* a8 _. q6 p, O; pmath.factorial(x)  取x的阶乘的值; `8 m) i) B% R6 U
  1. #取x的阶乘的值
    & }6 ]( g7 p9 g1 B
  2. factorial(x) -> Integral/ x& H; X$ d. h
  3. Find x!. Raise a ValueError if x is negative or non-integral.* \- J$ j4 O! M( W9 b5 b4 m
  4. >>> math.factorial(1)* _; m& F6 A$ p- Q4 n$ L) l5 K& d) D
  5. 1
    ( P1 F& |0 J; [3 O: \' Y
  6. >>> math.factorial(2)
    9 s' u( Z" r4 m
  7. 21 W2 s+ g$ p6 G  p% j* p+ g) K
  8. >>> math.factorial(3)
    1 U  T! w8 U: l5 e  G+ V
  9. 6" [9 L( y2 h! x. e% O
  10. >>> math.factorial(5)2 \3 E1 D. R( q. u
  11. 120' R6 r# m/ a7 n9 b" d& K8 E0 y$ E
  12. >>> math.factorial(10)/ ~. f( O0 q, x0 w$ q
  13. 3628800
复制代码
( W+ b9 ]3 b) S$ m% m
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数2 |$ S% W+ M/ U1 _8 u: H: S' o
  1. #得到x/y的余数,其值是一个浮点数
    7 @7 h* H% ]; |& o0 K
  2. fmod(x, y)  n* P. M* t, J% M6 s3 N
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    1 \3 j, I- \' E' q, A. Z0 c4 _
  4. >>> math.fmod(20,3)# I) ]) ^, q7 B  d" _2 L
  5. 2.0
    % H; W* O' ]/ Z- U9 U
  6. >>> math.fmod(20,7)
    # A' h' J- ]% n5 a' R
  7. 6.0
复制代码

# x. ^' O& ^* d7 s; tmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围1 O" L9 ]' z+ t1 j/ a$ M" ^
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    4 b( F/ c0 r4 |& O
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值, K# Q% ?; C# Q# f; t/ J3 n! G% [0 ~
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    , e6 s8 u% r# n6 g
  4. frexp(x)
    - i8 W: Z9 f' s( p, p0 ~
  5. Return the mantissa and exponent of x, as pair (m, e)./ E' H! g9 q3 k1 k
  6. m is a float and e is an int, such that x = m * 2.**e.
    / j3 A$ n* R) v) z* \5 A
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- N7 R2 a$ P; a* y9 ]+ d
  8. >>> math.frexp(10)
    2 y7 j% `8 P6 A5 c/ @+ a$ L
  9. (0.625, 4)5 [5 f$ L. P: {* G
  10. >>> math.frexp(75)9 s' x! R8 l& `5 C8 ]+ u0 b  T
  11. (0.5859375, 7): H  g1 i. e7 E5 }$ k/ d
  12. >>> math.frexp(-40)% V2 b! r4 l8 N9 F
  13. (-0.625, 6)
    * L: v! `, f; m" r+ S
  14. >>> math.frexp(-100)* f. v5 q( W2 z
  15. (-0.78125, 7)
    8 S( j. _6 h- ^  t
  16. >>> math.frexp(100)
    : m' a" p2 s" U: A& N9 k6 A4 c4 l4 Q
  17. (0.78125, 7)
复制代码
% _& A) i; J* i& M" i( p
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
& \8 D) g) I  p; j
  1. #对迭代器里的每个元素进行求和操作  q" k, n1 s% v- `& o
  2. fsum(iterable)' [) O  Q9 V; }: s# F
  3. Return an accurate floating point sum of values in the iterable.  f1 a* x9 ^) u( O) {
  4. Assumes IEEE-754 floating point arithmetic.5 a* b7 _2 N. L  V
  5. >>> math.fsum([1,2,3,4])2 k- q; Y2 s! S  C8 g8 m( {2 v
  6. 10.05 f' @9 M8 W5 X% I* C3 x2 }, J6 H
  7. >>> math.fsum((1,2,3,4))- U: S& E7 j/ n7 t
  8. 10.0. @& U3 k3 H0 o  F$ l) m) o
  9. >>> math.fsum((-1,-2,-3,-4))0 a8 V+ u7 N4 C4 @1 Y& z
  10. -10.02 a* R* m4 s' d2 H
  11. >>> math.fsum([-1,-2,-3,-4])3 h& W+ z, \& E+ ~
  12. -10.0
复制代码

; U! Y$ o  U+ ~) s( Cmath.gcd(x,y)  返回x和y的最大公约数
% ~" Q4 u3 I9 Y4 i& K1 E
  1. #返回x和y的最大公约数
    7 Y$ A) n) m! g% p; u
  2. gcd(x, y) -> int: y/ |+ H0 V1 s8 B
  3. greatest common divisor of x and y9 S" W8 J( {* C+ K
  4. >>> math.gcd(8,6)
      e0 `& X& ]0 l% s; h
  5. 2
    ; \0 S1 l" f5 q' C
  6. >>> math.gcd(40,20)
    3 {0 a3 E* S) g' H. e; v
  7. 20) c5 T( D" j; R
  8. >>> math.gcd(8,12)
    ! l3 l8 f9 T$ f2 b  K3 |
  9. 4
复制代码

2 `6 v2 m& t7 bmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False4 `/ L9 W1 L* `0 q" g! B
  1. #得到(x**2+y**2),平方的值
    & ^# Z% \# w! F  R+ ?
  2. hypot(x, y)
    ) k$ g+ E/ y6 ?2 s: k! W
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ! p& R1 H/ J6 ]
  4. >>> math.hypot(3,4)1 m  M) v! s- g: }
  5. 5.0& u$ R1 p7 L5 H6 B7 _2 q7 `( s9 f- E
  6. >>> math.hypot(6,8)
    # X7 B3 g  b, S
  7. 10.0
复制代码

8 a) H8 c0 a) }; {) ]math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False1 B4 c, ?" H: H& G" G7 y
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    ( n2 v9 }2 P2 C
  2. isfinite(x) -> bool* T; c: b+ ~, E6 x: Y* Y; g4 P
  3. Return True if x is neither an infinity nor a NaN, and False otherwise./ E2 s% f  `) R- a
  4. >>> math.isfinite(100)
    - |- }. p4 u. ~; ^
  5. True
    5 N" n5 W. ~# @: E& X3 o* y  ]: p6 C
  6. >>> math.isfinite(0)
    * m# m! `7 C# J* }- T1 V
  7. True
    # M! z9 u  M! g- V7 m9 b! e
  8. >>> math.isfinite(0.1)6 n* ^$ |3 d  o5 z' l, l' @
  9. True
    6 w3 `3 ]: @/ Y2 g
  10. >>> math.isfinite("a")" `, `, x2 d, Q( G
  11. >>> math.isfinite(0.0001)
    $ j. B. N  z* Y, t5 |
  12. True
复制代码
. J' `% @3 `6 g
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False% K/ s0 s4 e- R3 }- c
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False$ d7 m0 C# K& c
  2. isinf(x) -> bool
    ' i( R8 I9 N1 j
  3. Return True if x is a positive or negative infinity, and False otherwise.$ y/ M+ b- a, A2 n
  4. >>> math.isinf(234)
    ' H3 O/ O) x# k0 N
  5. False
    2 O! V4 |: u1 L* e* C( e: a) q
  6. >>> math.isinf(0.1)* {2 i, _) Z& B# D8 j# B
  7. False
复制代码

$ [9 u8 ^' y* j2 a% ymath.isnan(x)  如果x不是数字True,否则返回False
. g# C1 v3 w! D6 G& U, Q
  1. #如果x不是数字True,否则返回False4 a( ]' w$ {5 K/ r4 e
  2. isnan(x) -> bool
    5 B, N4 i. J/ m1 s$ r
  3. Return True if x is a NaN (not a number), and False otherwise.! m2 G3 R5 o8 ^6 k# q% w8 B
  4. >>> math.isnan(23)
    ' G3 R/ Y  p6 i7 s/ h8 Z, y7 o
  5. False4 A( N1 A# `% b# y
  6. >>> math.isnan(0.01)
    ( q2 r$ R( Z: C* F
  7. False
复制代码
8 n4 n  z$ V& R5 }) e7 z
math.ldexp(x,i)  返回x*(2**i)的值
8 Q" |! |# z; H
  1. #返回x*(2**i)的值% ?9 `, @7 T7 G  T8 K  _1 J& O0 ~) m
  2. ldexp(x, i)/ n" L& ~1 E5 t6 w" a. K
  3. Return x * (2**i).6 D4 F  G  P# ~+ q/ X% j6 n0 J, p
  4. >>> math.ldexp(5,5)
    % n+ x* n$ ~- q) U
  5. 160.00 R" p! J& t8 T
  6. >>> math.ldexp(3,5)
    2 ~# X  m( y  b
  7. 96.0
复制代码
( X1 W; A) h) X; [+ w2 w, p0 E3 Q
math.log10(x)  返回x的以10为底的对数
) {1 Z; R5 h& ^  f6 l3 W8 {
  1. #返回x的以10为底的对数# f, B8 L( U4 s. z( W) a% {& _
  2. log10(x): L3 ~" p- a# C) l  G+ K* ?5 s
  3. Return the base 10 logarithm of x.! H' t3 o0 I* Y  g1 F4 _5 O
  4. >>> math.log10(10)
    ; h) O! {2 A$ b; g3 b9 w
  5. 1.0" u: M; e2 R; i# Q6 b% Q: |
  6. >>> math.log10(100)" s/ z+ U# D0 y) G9 E" d
  7. 2.0
    3 h6 g& Z- J' t/ J/ c" b9 n
  8. #即10的1.3次方的结果为204 r4 s% p, Z3 V1 q
  9. >>> math.log10(20)! |+ h; U) C$ B7 s) G9 E
  10. 1.3010299956639813
复制代码
% H5 T5 p; R# E# W) O
math.log1p(x)  返回x+1的自然对数(基数为e)的值2 ~" p, Z* C3 h0 V$ J
  1. #返回x+1的自然对数(基数为e)的值
    7 x& b3 B4 ?# W
  2. log1p(x)
    - u# ^" l, I  |( B, S
  3. Return the natural logarithm of 1+x (base e).
    $ F: q' y% s8 }# Z4 w3 d8 M
  4. The result is computed in a way which is accurate for x near zero.
    " Y; |9 u' U: L, W$ z" Q6 i. E( Z
  5. >>> math.log(10)
    ' q& S& Q! `: h) P& i& \
  6. 2.302585092994046
    7 Z, y% B4 l+ r# @" }! `$ H2 I
  7. >>> math.log1p(10)! `8 X9 N$ z2 R' f( O
  8. 2.3978952727983707
    ( i& ?1 Z! X+ K) n7 l
  9. >>> math.log(11)$ K) F5 k3 ~. M5 j" T4 s
  10. 2.3978952727983707
复制代码
7 M+ L9 Q* U: ~; ]6 k( a
math.log2(x)  返回x的基2对数
0 `/ I4 G; H1 a1 S6 ^8 U7 ^
  1. #返回x的基2对数
    * A+ z% W, o0 F* f0 j4 V
  2. log2(x)
    6 M# E) U2 r& h& h/ R2 y. f; x
  3. Return the base 2 logarithm of x.
    % Y" h! _6 n' l& S! _+ Y
  4. >>> math.log2(32)
    ' }* \( Y5 Z2 }& J* O: P2 `
  5. 5.0
    ) ?/ b  b" z5 S2 ^- m( E& ?
  6. >>> math.log2(20)
      ]: q/ k- U6 d( N  f) j4 H
  7. 4.321928094887363- ]/ g2 }8 E2 E/ l) P
  8. >>> math.log2(16)0 r* V; y+ g3 n8 V$ Q
  9. 4.0
复制代码

+ n9 |  Q) V- W2 k" l3 v8 b; _, Rmath.modf(x)  返回由x的小数部分和整数部分组成的元组
# x8 L- P# B( m5 C% s4 Q
  1. #返回由x的小数部分和整数部分组成的元组
    1 i' Q% @$ T2 O. a# Y
  2. modf(x)' d4 ]0 v, O! }) f! s6 m+ Y
  3. Return the fractional and integer parts of x.  Both results carry the sign
    % W: m6 ~  I/ [7 M
  4. of x and are floats.
    & B$ ^9 @: b5 c8 E( s6 @/ I
  5. >>> math.modf(math.pi)6 w3 {) w" f5 Z8 a
  6. (0.14159265358979312, 3.0)# [. Q* @4 N2 u! s: g
  7. >>> math.modf(12.34)" q3 ?6 `( N% G( L1 m
  8. (0.33999999999999986, 12.0)
复制代码

: q" w2 g& v% y7 l' ~math.sqrt(x)  求x的平方根  Q5 D& k+ N: f# Q& `7 e' K
  1. #求x的平方根
    : `, X: f5 v- a2 }5 d( }. B$ @" {
  2. sqrt(x)4 _6 y& n" B$ c2 n) C  F8 L1 i
  3. Return the square root of x.
    ' i9 m# n" Y% m4 D
  4. >>> math.sqrt(100)2 }8 A  n4 k% r0 R
  5. 10.0- y7 M3 d4 l3 N7 U$ F$ ]
  6. >>> math.sqrt(16)
    $ ^$ `5 p' N9 t3 O6 H- w
  7. 4.0
    : I& G' w9 L8 e( ^8 o6 b
  8. >>> math.sqrt(20)
    7 p; h1 ^: s( |% A
  9. 4.47213595499958
复制代码
& A6 _& a  R2 V1 F3 g
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    ; g. J+ o3 z8 g
  2. trunc(x:Real) -> Integral
    # q! z7 r% r. v+ M6 o' j' L- E
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    ; x' x$ b1 o9 F* v9 q/ A
  4. >>> math.trunc(6.789)" h* `8 Q1 ^' U. F
  5. 6
    4 S8 u( m) s. D" w7 ~- s( a  j; J
  6. >>> math.trunc(math.pi)
    8 E# w, e& j$ y$ i
  7. 3
    7 L& E- t* ]6 O& i4 q
  8. >>> math.trunc(2.567)
    ) u- F3 L# W1 d/ @/ J2 b% b: M
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-3-7 23:41 , Processed in 0.074409 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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