新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

# h/ j; }' C' R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。$ C# O# ~# Q# P" w0 K( }
( m5 c) r9 [& W8 m6 k0 i- g: H
方法1! m7 T. J/ O) r" N  E3 z
  1. >>> import math
    ! B/ @( o0 C4 l& N& w
  2. >>> math.sqrt(9)
    ! j, Z: Q* _3 j. X( V! r
  3. 3.0
复制代码
方法24 T) S; w( M( E8 e
  1. >>> from math import sqrt
    $ p( Z% R4 b& A9 I
  2. >>> sqrt(9)3 a/ M! w8 z, b
  3. 3.0
复制代码

, d8 Z1 f- }6 P0 E

: O' q% _  N" u7 {
math.e  表示一个常量
4 K7 n& H& ^0 x# Z( s
  1. #表示一个常量
    " b$ o% m0 }; p. e) N
  2. >>> math.e
    7 [7 Z/ o" u8 P1 X
  3. 2.718281828459045
复制代码
, f4 p; W* P! b  s8 y( f+ K' K2 C
math.pi  
数字常量,圆周率
& S5 ]# ^  j1 T# e7 N
  1. #数字常量,圆周率$ F' n/ P) a1 `
  2. >>> print(math.pi)( x' N5 a) d; z0 c5 F. T
  3. 3.141592653589793
复制代码

' m& N  @3 O, H/ w, {math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
) h! k- @; W4 F$ P$ F- {4 o; B  ~
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x( j+ _( @5 q( c' b: Z
  2. ceil(x)8 |  L" N  R. N/ j9 H
  3. Return the ceiling of x as an int.; J8 ?' j9 R4 K- J5 c, q
  4. This is the smallest integral value >= x.
    - }8 E, t6 L+ E9 C! S; }3 _

  5. + [  h' H$ N- I) x# [+ K
  6. >>> math.ceil(4.01)
    ' q% x, i: J+ i8 G$ |
  7. 5
    # I9 e+ w( U7 [+ V% U* k' @
  8. >>> math.ceil(4.99)
    $ H% D; y: R! y( L' q
  9. 58 `) r+ N- B) g- z0 O. t
  10. >>> math.ceil(-3.99)
    3 P% L6 X  j: z4 v
  11. -3
    % V! s7 s: _3 a& J: R3 h
  12. >>> math.ceil(-3.01)
    : a9 j# x& d) r" P# M$ O
  13. -3
复制代码

8 X# _3 F% b7 ]9 Z: P# S: m8 L/ Ymath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, N/ B  L  X& Y& v% K. s
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身  p$ F8 V7 k4 a7 u" J5 {, g
  2. floor(x)
    $ b/ R  s: I! Y% m  N
  3. Return the floor of x as an int.7 Z5 Q9 W/ M0 ~1 ?9 r! _
  4. This is the largest integral value <= x.
    % \( S& ~# w1 I. P$ f* w5 y2 J8 K7 k
  5. >>> math.floor(4.1)
    , Q+ V3 m) h; ?: g. e# {
  6. 4
    , R7 p1 h" `! C2 n. T; `- u
  7. >>> math.floor(4.999)
    5 _$ B; Q- p9 w
  8. 4
    7 U0 |$ B$ n  x' ~2 ]7 y# a0 p
  9. >>> math.floor(-4.999)
    7 \9 I( g1 J2 b0 n5 L( ^
  10. -59 \& S/ A) [! i
  11. >>> math.floor(-4.01)7 x+ X- n  `! m, }  f+ m
  12. -5
复制代码
0 Y. {0 n2 {8 y2 W
math.pow(x,y)  返回x的y次方,即x**y
- Q3 b& ~  A# }$ Y
  1. #返回x的y次方,即x**y
    ) X. p* u& V- Y, e5 D
  2. pow(x, y)% }7 S2 W) R2 M" L
  3. Return x**y (x to the power of y).. a' e8 ?  L8 C$ V
  4. >>> math.pow(3,4)5 ~. w9 f6 a& M
  5. 81.07 T" R& G4 A; V& \" b1 ^
  6. >>>
    ) c4 A# h$ @; \; V$ w3 A- {
  7. >>> math.pow(2,7)
    * ^$ {1 c4 ]' q6 j
  8. 128.0
复制代码
" o& }1 J# }' m  U3 r1 s! _) |
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# \  d! m' L4 ~4 Q
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* h! ]8 g+ U5 @9 y% g+ R
  2. log(x[, base])+ o+ T0 m0 c; A
  3. Return the logarithm of x to the given base.
    / [; _4 S: t% O7 D
  4. If the base not specified, returns the natural logarithm (base e) of x.
    3 p% @; S- h! u$ \
  5. >>> math.log(10)
    % z9 P. b6 U+ `3 M  o
  6. 2.302585092994046
    ; V6 B) D. L" d1 S, ]. M% K3 T( T
  7. >>> math.log(11)
    1 a8 ?) ]. s3 f* H2 w- Q7 v* ~) h
  8. 2.39789527279837078 y3 t0 I# M0 X5 b
  9. >>> math.log(20)  v. h, x5 z. k: G. B2 ^5 }
  10. 2.995732273553991
复制代码
7 c. \9 Z* G# Z, t! k7 X" c
math.sin(x)  求x(x为弧度)的正弦值
1 a. y; x& u% h" b% n1 k: c' F
  1. #求x(x为弧度)的正弦值
    ' d5 L5 T" m' m9 V* J3 z# J
  2. sin(x)% i4 f9 a0 Q6 e9 r& O5 Y2 m9 T
  3. Return the sine of x (measured in radians).
    : M  G% Y6 g3 h! {3 ]
  4. >>> math.sin(math.pi/4)3 H& h" h; w& Z. _' v- V  A
  5. 0.70710678118654752 h, A! Q$ y/ i. S# R
  6. >>> math.sin(math.pi/2)
    / }, Z8 y# Q  x. e+ i8 R9 m8 j
  7. 1.02 a, b$ h+ W, k1 @
  8. >>> math.sin(math.pi/3)8 ^/ V9 H4 i  n3 y( k* ^2 m1 C
  9. 0.8660254037844386
复制代码
9 @( w5 x- G9 ^% n! L- X+ D+ U$ n7 W
math.cos(x)  求x的余弦,x必须是弧度
! {- U% y3 l# x$ {
  1. #求x的余弦,x必须是弧度
    & H% r2 k$ P; ~, l( C: y5 D$ R
  2. cos(x)& g! t- w3 m: X$ v& U
  3. Return the cosine of x (measured in radians).! I8 S) x" N- F  Y: v8 i
  4. #math.pi/4表示弧度,转换成角度为45度
    & p' x) r+ l/ r/ B% v* ~" F
  5. >>> math.cos(math.pi/4)
    2 k8 z+ G2 {. U+ M  ~0 z
  6. 0.7071067811865476% B  {+ ~) d& Q! n: E
  7. math.pi/3表示弧度,转换成角度为60度5 O! ^& R# y& k4 C* H: C+ z* {* P
  8. >>> math.cos(math.pi/3)
    " A. `8 M, h3 s% r- [8 g8 w
  9. 0.5000000000000001
    # k8 A0 y, k+ Q
  10. math.pi/6表示弧度,转换成角度为30度- L; L( r+ k9 k1 ^  F! s2 h
  11. >>> math.cos(math.pi/6)
    * y- ?: q* @7 \( [
  12. 0.8660254037844387
复制代码
5 @. D! x2 S5 S$ [2 |  c
math.tan(x)  返回x(x为弧度)的正切值
; b/ z" d8 ]  J4 P& v4 g+ M' V4 f9 N: Y
  1. #返回x(x为弧度)的正切值* q! h" e  c* o% Z2 Q$ y3 m0 Q
  2. tan(x)2 }" h8 D  K' G# g, O$ U
  3. Return the tangent of x (measured in radians).  k4 |& p* c4 y0 G: X- d- }* a( g
  4. >>> math.tan(math.pi/4)+ @" ]5 I- E9 l
  5. 0.9999999999999999
    ( `+ I1 f0 }7 C7 f5 R
  6. >>> math.tan(math.pi/6)
    & D# ]3 t: p6 \9 p
  7. 0.5773502691896257' B( `4 M1 c8 R$ L, Q7 o7 f4 F- K
  8. >>> math.tan(math.pi/3)* y  g! Q" V; x: y; W7 K
  9. 1.7320508075688767
复制代码

! m3 V) G* [3 s+ k3 H5 lmath.degrees(x)  把x从弧度转换成角度
' N6 G2 a+ I' g0 ]6 n
  1. #把x从弧度转换成角度
    3 {6 P0 X+ y. y7 o/ K4 p. P
  2. degrees(x)" e$ `+ f# E  X. i9 L
  3. Convert angle x from radians to degrees.% C2 _! ^1 V( g" O, k# j! u

  4.   V  {: ]/ B. O4 u& T, C
  5. >>> math.degrees(math.pi/4)
    # }, x: Y, ]5 K/ |0 ~
  6. 45.0
    2 ?1 c2 O2 t  b9 h5 g+ }( n( [4 e* a
  7. >>> math.degrees(math.pi)
    ) T8 e7 ^1 i2 U3 a$ A- g
  8. 180.0! A2 p) r; W$ @) I# C9 ]
  9. >>> math.degrees(math.pi/6)# Y" H' D* n( S; o+ _
  10. 29.999999999999996* z; o' J) Y7 r& R8 z' |9 c
  11. >>> math.degrees(math.pi/3)
    ; J& `$ M0 }* e, j# T
  12. 59.99999999999999
复制代码

; }( s  d) L. Amath.radians(x)  把角度x转换成弧度
( Z2 D* M! U2 I! s* {2 b! {! p
  1. #把角度x转换成弧度) ]% L( d/ i4 S* L& X
  2. radians(x)
    - _. |. W* L) C' A: H' a- W  \, Q& ~
  3. Convert angle x from degrees to radians.
    2 \! F8 x) ]* d1 q; s6 j; U
  4. >>> math.radians(45)# r6 t! ], r5 R) ~$ v5 h- y
  5. 0.7853981633974483
    8 n7 A+ b) C" S  |, n* l7 K
  6. >>> math.radians(60)
    3 q$ ~; i1 |3 P# ~( H" ^, T
  7. 1.0471975511965976
复制代码
+ ^7 A8 ^( ~1 V; v6 o# B( j
math.copysign(x,y)  把y的正负号加到x前面,可以使用0" X+ p/ a! E* I  {. U: c
  1. #把y的正负号加到x前面,可以使用0  C. B. o2 [  d3 }. _- u
  2. copysign(x, y)
    * a% b7 u6 }5 o6 f9 h9 t* h
  3. Return a float with the magnitude (absolute value) of x but the sign   W) t9 y, r" V  w$ C6 D+ U. I
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    ; Y; s5 R! L: Y- u
  5. returns -1.0.( i$ `3 a5 m8 I1 w

  6. 6 F8 s! J. R* X! n3 W6 h
  7. >>> math.copysign(2,3)- T: K0 o& ]( ?
  8. 2.0% R/ d; p5 @0 ~2 \6 f; V7 C" L
  9. >>> math.copysign(2,-3)" ]3 M( Q1 A$ m
  10. -2.0
    ; y0 ]/ k" V! s  m, |
  11. >>> math.copysign(3,8)
    5 k9 M' [- U' Y: w( D5 B; B
  12. 3.01 B9 d' {1 Q6 A! C* X3 b$ n
  13. >>> math.copysign(3,-8)
      G  @& _' o5 C3 l0 t) |
  14. -3.0
复制代码

0 e* ]6 \$ u" x' ?5 l1 U, ~math.exp(x)  返回math.e,也就是2.71828的x次方
4 E5 x* k5 D4 }2 l- P
  1. #返回math.e,也就是2.71828的x次方1 u+ a- v, M+ v$ q
  2. exp(x), j; p- s+ \) @7 X" `. V6 A
  3. Return e raised to the power of x.% p% k5 I9 e( B6 y6 s2 K
  4. 7 E! L* ?* N# g5 z3 _# z
  5. >>> math.exp(1)
    5 k9 R' x4 a5 k8 {4 E, c+ ]
  6. 2.718281828459045
    : t' i* \% ^7 b) b
  7. >>> math.exp(2)3 I1 S5 j$ ~+ o3 ?! x, ^3 ~% Q
  8. 7.38905609893065
    ( n6 e" J$ |) X/ |: o
  9. >>> math.exp(3)
    8 i3 y( \" e& J* f# @& U
  10. 20.085536923187668
复制代码

- ]% z' P  k( ~& |9 d# {math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
( X$ e  d% |8 Z7 ^
  1. #返回math.e的x(其值为2.71828)次方的值减1! \" F) H+ ^$ ?  w' i% i
  2. expm1(x)
    3 C0 ~& w$ c5 O; ?: k1 r
  3. Return exp(x)-1.% d# N7 f# @* E3 p. s& u0 P
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.5 G0 L: }' [: c3 v: H5 Y

  5. 9 C) |" B0 n% f
  6. >>> math.expm1(1)& P$ R- W6 [- Y  Z$ R
  7. 1.718281828459045
    9 @8 J. ]" n) ~$ Z
  8. >>> math.expm1(2)
    9 P  o3 M  e- |' h+ D9 c
  9. 6.389056098930652 e$ A/ l' t- k# k2 Y/ C# T0 R
  10. >>> math.expm1(3)
    # w0 m- ~7 T3 C1 w8 t. d8 w
  11. 19.085536923187668
复制代码
! g) U& q; l* z1 P' |0 |
math.fabs(x)  返回x的绝对值3 V/ W; A8 V) C, O6 m1 \
  1. #返回x的绝对值
      x- c; m% M! S) N! E) U
  2. fabs(x)
    5 ~( E- Q/ Z2 n6 ?( z& D' O
  3. Return the absolute value of the float x.7 z4 J7 b" k, D2 I" o; w- J5 J

  4. " Q7 D, J" G. ~# E1 G& L
  5. >>> math.fabs(-0.003)1 j+ n" T* E7 o1 B1 v6 L2 V
  6. 0.003
    " t2 j3 [* p& g$ m* l
  7. >>> math.fabs(-110)
    ( h; w! o$ A) [5 \! W8 u9 s" a
  8. 110.04 q0 a* ]. {+ ~$ \4 x4 p! X) Y1 [
  9. >>> math.fabs(100)% T) q3 x  |' u* g/ {- ~
  10. 100.0
复制代码
8 g( x8 @5 {: s: P5 N
math.factorial(x)  取x的阶乘的值
2 [7 x6 q9 n2 _/ E
  1. #取x的阶乘的值
    ; T+ ?* L4 R3 r" ?: O) v
  2. factorial(x) -> Integral
    ( [' |, W, A7 p% A* f4 u
  3. Find x!. Raise a ValueError if x is negative or non-integral.* a3 h1 h2 y. U. \4 Q
  4. >>> math.factorial(1)
    6 u8 w' E9 F9 f) p
  5. 1
    - q' M: y0 c2 m9 G5 V9 \
  6. >>> math.factorial(2)
    6 a/ A7 I$ `0 t3 K3 i
  7. 2, i. K& g0 T# }# Z% R4 R( ^
  8. >>> math.factorial(3)/ x" J7 P% ~  V6 ~
  9. 6
    # Z* p% R/ n6 Q( A9 T$ _
  10. >>> math.factorial(5)
    % _4 Q3 m/ ?* i/ U2 ^) ]( ?( Y- f5 H
  11. 120
    2 D3 ^9 G0 J- x' H  v5 X
  12. >>> math.factorial(10)
    . c4 A4 B* Q& Z2 d) T
  13. 3628800
复制代码

5 @: X  ]$ l" ^) x: O4 S) j& @math.fmod(x,y)  得到x/y的余数,其值是一个浮点数2 Y0 K4 t: _1 e$ G! \
  1. #得到x/y的余数,其值是一个浮点数
    % d; f  B+ f4 J9 _. R2 V
  2. fmod(x, y)
    # u  H9 K7 x( V5 k, r3 S
  3. Return fmod(x, y), according to platform C.  x % y may differ.8 O: u, X, J# S" o( i: s. G' e
  4. >>> math.fmod(20,3)
    / m3 u& |: C+ X; y6 Q
  5. 2.0
    7 w4 }0 ?. _$ S* A  o
  6. >>> math.fmod(20,7)8 D& }/ W3 C3 r9 t- u1 h, x
  7. 6.0
复制代码
( t* l" Z" u) I2 q. u( P  S
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
/ d4 r* t9 u( v' i% Y1 Z. y6 E
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 z1 k2 l7 h. x) c1 j8 [! T- ]* i
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 N7 r' _0 ~6 v- T) q3 `
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 ?: C0 t$ r0 O
  4. frexp(x)5 {4 L8 q0 G0 r/ G! v
  5. Return the mantissa and exponent of x, as pair (m, e).- K4 q. U9 v6 Q0 d
  6. m is a float and e is an int, such that x = m * 2.**e.( w  o. M' B. d6 [
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.0 Y! T6 i6 B+ s4 z0 z
  8. >>> math.frexp(10)3 R& m. ]3 x0 N. s
  9. (0.625, 4)2 @+ T- f/ y' j; F; g+ ^: _3 U
  10. >>> math.frexp(75)
    / O# W  j) H5 q, f
  11. (0.5859375, 7). u* Q5 G# D+ }7 i$ N& A0 I
  12. >>> math.frexp(-40)
    ) e6 }3 `4 ^; t
  13. (-0.625, 6)
    3 y; C  [- P' [7 I
  14. >>> math.frexp(-100)
    & C9 u2 t" \( f! k) F: n
  15. (-0.78125, 7), Q' u4 d- t3 q4 \. J: z
  16. >>> math.frexp(100)
    5 c  d" }: _7 ]9 r  C
  17. (0.78125, 7)
复制代码
. R. Z3 S1 V/ y. _2 U; w
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列. S# F, g- a8 h
  1. #对迭代器里的每个元素进行求和操作
    - t$ u: Q- D6 W& Z4 y: X
  2. fsum(iterable)
    ' u3 g( k. `! B' w
  3. Return an accurate floating point sum of values in the iterable.# H; V- P* k4 b: N& F3 u1 N# Q* O
  4. Assumes IEEE-754 floating point arithmetic.: i, [3 W3 v- S0 q* g& \8 L5 I
  5. >>> math.fsum([1,2,3,4])% P7 f! r: ?- @* R6 ?
  6. 10.0
    8 r# \6 n& y0 U" L
  7. >>> math.fsum((1,2,3,4))
    " d- J7 D& j9 D
  8. 10.0
    ) i0 l1 @; V! q7 _: S1 z6 R
  9. >>> math.fsum((-1,-2,-3,-4))
    . u4 @2 D5 M  h/ g
  10. -10.03 V2 \9 V1 O  l# ~: M$ S
  11. >>> math.fsum([-1,-2,-3,-4])  d6 z/ b7 r4 S1 ~. t
  12. -10.0
复制代码
. P! U* y  i8 l0 i% B- [
math.gcd(x,y)  返回x和y的最大公约数. q) I  G# d3 |6 t$ t' s) I4 O/ u
  1. #返回x和y的最大公约数
    " l0 z% L: d, z; _9 G
  2. gcd(x, y) -> int1 W. Z+ V2 @" w1 S
  3. greatest common divisor of x and y7 a; x, q1 \8 I
  4. >>> math.gcd(8,6)9 l4 g0 g' B  J; i
  5. 2" Y9 V: l) ^7 q$ `* U8 {' M- q# @
  6. >>> math.gcd(40,20)
    / W4 v  c/ h, R" E* i
  7. 20
    8 J/ O3 A% s4 x* b$ f
  8. >>> math.gcd(8,12)4 ^) i' S; x; N: B. M& S: g1 M
  9. 4
复制代码
* W$ G1 {% w$ M, }* P, }
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False' H, |- o- Z+ x6 G" M
  1. #得到(x**2+y**2),平方的值( g( K3 T, n# r- J( W
  2. hypot(x, y)
    9 x3 t, b( z( m% b7 ?1 K
  3. Return the Euclidean distance, sqrt(x*x + y*y).4 P, U/ m* z) k# }/ S4 m
  4. >>> math.hypot(3,4)
    , N( s/ @8 ^6 W$ ]
  5. 5.0
    ' u+ B7 B/ V# N% {9 |8 i: l
  6. >>> math.hypot(6,8)
    * B( E! G& @) Y  A; _7 \
  7. 10.0
复制代码
+ U+ j! E: }( Y- F7 [
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False; N* a( \4 ]& c
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    5 a* D. B: E/ S) B& L
  2. isfinite(x) -> bool
    / B+ z5 `& F6 G6 ^7 M5 T$ m# g! _  `
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    + W3 @2 z9 i5 j4 \& c
  4. >>> math.isfinite(100)$ ^% c  B6 C; c7 C5 ]3 W4 ]
  5. True! v$ t* w) n% t
  6. >>> math.isfinite(0)5 a9 a0 ]$ g6 g2 H3 [8 [
  7. True! \: @* d$ v1 D2 d# R. I/ U! D
  8. >>> math.isfinite(0.1)
    : i( e* r  m3 w6 I& j% `  ]
  9. True9 d8 K, _$ y& z( w$ f
  10. >>> math.isfinite("a")
    ; P9 h* E# l5 C/ F0 e1 T4 [
  11. >>> math.isfinite(0.0001)$ _+ @  r' t# }! L6 l7 G8 e
  12. True
复制代码
! {! ?5 @8 o0 R) |$ }
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
. i9 B) @! E8 K" H
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    # E0 N9 {4 v# F( j& Q: v/ w
  2. isinf(x) -> bool! Z% Y3 i, V, W7 s( r- ?5 O1 N' i
  3. Return True if x is a positive or negative infinity, and False otherwise.% ?/ @9 t8 v3 |* Y! j* U$ R
  4. >>> math.isinf(234)* S  U% H8 _0 w: m2 R# `
  5. False1 b1 j3 S: J* r$ E+ D3 I' Y3 h
  6. >>> math.isinf(0.1)
    " \: \0 J+ H! i0 d, \7 o5 K
  7. False
复制代码
# R- b# D% B4 Y. }* w
math.isnan(x)  如果x不是数字True,否则返回False8 b( W% ~7 T4 \2 a
  1. #如果x不是数字True,否则返回False
    + Q# R4 L9 H0 A4 A4 `0 q; N6 S3 p' O
  2. isnan(x) -> bool& d! _+ C  s. V+ `& W3 Y6 P" C8 I
  3. Return True if x is a NaN (not a number), and False otherwise.6 k6 L; @/ b4 e& T: K4 a; M
  4. >>> math.isnan(23)
    9 I' Q% a& n# c/ O1 d; c7 x/ x
  5. False8 r6 d) H" r& b0 ?$ C. `% O5 |$ _
  6. >>> math.isnan(0.01)0 f- f! _: b- }  E: Q/ D
  7. False
复制代码

6 Q% M; p' H) n1 A% xmath.ldexp(x,i)  返回x*(2**i)的值
4 {- n( P. a7 ~  n* A( [
  1. #返回x*(2**i)的值. f. Q& e! x. ]' j) Y6 D: p; P- t
  2. ldexp(x, i)
    3 l0 ^0 r" H$ P3 J9 @" ]
  3. Return x * (2**i).
    9 \5 j' u' D" Q1 c4 m$ I
  4. >>> math.ldexp(5,5)/ B9 H; J5 x; j% N/ k  O
  5. 160.0
    6 w  Z; P7 b4 {
  6. >>> math.ldexp(3,5)" B' T6 C" Q+ N. e7 p1 O+ L
  7. 96.0
复制代码
8 a* u! {$ w& Y' c2 ]9 O
math.log10(x)  返回x的以10为底的对数
  j$ }  [9 i- q+ @* j/ a
  1. #返回x的以10为底的对数
    ) a/ R3 R3 I: Y+ h; T/ p
  2. log10(x)* p; K4 K; Q0 f
  3. Return the base 10 logarithm of x.
    - |  K: U+ E6 M# P* u& ^
  4. >>> math.log10(10)' {8 L1 N4 @/ I3 V; }
  5. 1.0
    / M1 K6 t/ G3 t
  6. >>> math.log10(100)
    : r7 d9 D0 j- a6 [
  7. 2.0
    % G. H4 w0 D. [) J
  8. #即10的1.3次方的结果为209 V" x! B# m! f6 k- D$ V$ l
  9. >>> math.log10(20)
    & F0 S4 n; S; Y/ ~! u
  10. 1.3010299956639813
复制代码
9 u) e" b, v3 c7 J# x4 p6 \. ^- x
math.log1p(x)  返回x+1的自然对数(基数为e)的值
/ l4 F6 e3 |& a/ W, Z
  1. #返回x+1的自然对数(基数为e)的值, B& B+ W( C, z6 {: |7 o) `
  2. log1p(x)
    2 o# }2 F* f% b0 b( T
  3. Return the natural logarithm of 1+x (base e).
    6 n6 F: l2 ^, h* }5 A) E" P5 v
  4. The result is computed in a way which is accurate for x near zero.. {+ D0 P. p0 N% e) I: {/ S
  5. >>> math.log(10)
    + ^% r2 B% E, [& @) {. ^
  6. 2.302585092994046# d0 p# x3 N6 m8 E2 u$ u+ K6 v
  7. >>> math.log1p(10). i( E- C" u0 h% l4 h6 j6 I$ S& }: p
  8. 2.39789527279837073 B: W) v$ i6 H7 X! C
  9. >>> math.log(11)
    ' ]* ^8 ~- x. `1 h: t0 r- k* R
  10. 2.3978952727983707
复制代码
! R  [4 y: n0 g. d! J8 b: c
math.log2(x)  返回x的基2对数
3 L- L; \9 i* r6 a+ d3 \
  1. #返回x的基2对数
    , `; t' m, r4 h. ^! H3 F1 I+ ^
  2. log2(x)
    * U+ o$ G  m7 G
  3. Return the base 2 logarithm of x.6 L# y( X6 X+ G7 H
  4. >>> math.log2(32)
    5 }4 d5 W! ^" n  ~: n* s
  5. 5.0
    # k/ \# I! w: p! N
  6. >>> math.log2(20)1 U5 ]; {* {1 w5 u! B1 r
  7. 4.321928094887363, g5 C) b1 Y# Q
  8. >>> math.log2(16)
    # b( c1 U2 n5 f9 m$ \6 _' ^6 A- y
  9. 4.0
复制代码
7 m4 G6 e, M: V: y' t
math.modf(x)  返回由x的小数部分和整数部分组成的元组( x  O, {; d9 C! W& ^. ]; j# J
  1. #返回由x的小数部分和整数部分组成的元组
    & v: y- ?. s! n! P
  2. modf(x)7 S0 f! H8 m# N( c: W* G
  3. Return the fractional and integer parts of x.  Both results carry the sign# u9 l, ]8 m+ D
  4. of x and are floats.% T0 j  F( C0 |1 O2 _
  5. >>> math.modf(math.pi)
    - Z% A) p5 x1 t1 k- v3 R
  6. (0.14159265358979312, 3.0)
    6 q& I. K& \5 [6 z
  7. >>> math.modf(12.34)
    2 c0 q% G2 ]8 g/ a( `1 S) k( r
  8. (0.33999999999999986, 12.0)
复制代码
! k# h% u* j2 _8 I( y
math.sqrt(x)  求x的平方根
& J! _: T; p" k8 X- {7 p! m
  1. #求x的平方根
      L2 l2 _; w6 a" ^
  2. sqrt(x)" [! {; ~0 v: g8 J( d
  3. Return the square root of x.2 j6 n8 b) g6 {9 @' z) B
  4. >>> math.sqrt(100)
    ' J5 W8 s8 K5 B1 E
  5. 10.0
    4 R4 H4 ~3 ]% ~. l% @1 K
  6. >>> math.sqrt(16)5 R5 F8 G3 ~  a* }3 W; t% Q& i
  7. 4.0
    " C, H) e5 ~1 e! a
  8. >>> math.sqrt(20); }3 L6 H7 p, h: I4 \; b# W
  9. 4.47213595499958
复制代码
$ i: F( c' e; z* Q: H' `
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; H  z3 }! h9 Z# N& p
  2. trunc(x:Real) -> Integral& W  n$ L. W) z6 l3 W4 z* X5 d# W
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 n7 X& ?3 d& F* X  g" s* h! F
  4. >>> math.trunc(6.789)
    ( f# l" A* V* x; T  D
  5. 6. [3 Q3 |$ w( B* }+ g8 @3 _
  6. >>> math.trunc(math.pi): \# q5 ]) N; ?0 s" l) |; B9 {
  7. 3
    ; I4 d+ h+ U; i5 s
  8. >>> math.trunc(2.567)8 X% o9 ?* B  U- L# Y7 k' Y1 S6 q
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-21 22:42 , Processed in 0.079066 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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