新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
4 Q6 o. g/ \; b1 ?
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。" {/ h9 k3 t6 y* x% [
) Y' r+ ]) F, ?# @( ^2 V) c% E
方法1% o, w5 R: n# F( s
  1. >>> import math6 Y1 i  Y  X+ I% I9 r- G1 C
  2. >>> math.sqrt(9)$ [" q! |+ |( a7 E/ P( i
  3. 3.0
复制代码
方法2
; F" w7 ]" |; ^$ J, P
  1. >>> from math import sqrt
    , {0 D4 D; ~% b. S. {
  2. >>> sqrt(9)
    * t+ p8 d0 ~8 a1 n! m" V' `
  3. 3.0
复制代码
; I, `  Z6 Q# c' ]( L) U8 T


+ f- x# P( ]' X1 M( m% i4 G2 e
math.e  表示一个常量
) T, [- r7 N* E
  1. #表示一个常量
    1 ~) W" \! _4 N
  2. >>> math.e' U* k$ A6 L; Z4 O
  3. 2.718281828459045
复制代码
9 v$ j0 ?1 h  y
math.pi  
数字常量,圆周率
* q: X. F0 i3 g$ L
  1. #数字常量,圆周率/ b5 P+ y. [3 W
  2. >>> print(math.pi)1 }* m- A; Y7 s( E; \6 I
  3. 3.141592653589793
复制代码
1 T* ~5 c% t! o! V' Z  ~6 s1 ^# s
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

/ p/ d2 W( t" G: D, |3 G% J6 j1 {
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    7 J0 \; r' G/ I* B" a$ X
  2. ceil(x)! y/ n: Z( ~( b+ s# u
  3. Return the ceiling of x as an int.
    ( p$ U! b- i9 B( K
  4. This is the smallest integral value >= x.1 r. l2 O0 G+ x5 d2 e- L6 r5 _

  5. ) ?  Q* `- U: U- @- i
  6. >>> math.ceil(4.01)
    0 A- Y. l9 _7 Z' v0 i
  7. 5
    % F. l/ c5 W1 I  k5 d- \8 K7 d7 t
  8. >>> math.ceil(4.99)
    7 p. t4 ?% u% V9 G2 M* q5 }
  9. 5
    5 S! p  F1 a3 s# t  L
  10. >>> math.ceil(-3.99)
    $ D& r! p1 M" g. ?& D6 s
  11. -3  H! \; F2 D( \' }
  12. >>> math.ceil(-3.01)
    # q1 e1 H! M, |9 M4 }3 Z
  13. -3
复制代码
7 R# ?! e% F' J4 Q
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
- D5 A4 u( v; L7 ~$ ?
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) J/ t% R6 W; C8 x+ J
  2. floor(x)' P5 B+ z8 l9 b  M* E8 |
  3. Return the floor of x as an int.9 j5 x' @3 g+ I8 \) W
  4. This is the largest integral value <= x.
    3 _. P! ^. n, U8 c
  5. >>> math.floor(4.1)  Y4 ^' Z1 r& m( C3 O
  6. 4
    6 D. s% @) M0 S! e$ j
  7. >>> math.floor(4.999)' V; c1 z6 c( {/ h4 r
  8. 4
    * }! O) y( X" J2 ^6 |
  9. >>> math.floor(-4.999)& F9 w) f+ _6 E( C$ K. x
  10. -5" G" k# Q; h. T' C( w) c
  11. >>> math.floor(-4.01)* `; A% S/ [  e
  12. -5
复制代码
* y! p' J( [1 |, f. l
math.pow(x,y)  返回x的y次方,即x**y3 F0 \/ {# `7 h3 _' y5 \5 Q
  1. #返回x的y次方,即x**y
    : C5 ]3 C" c' d  z) @' p1 L% ?
  2. pow(x, y)
    ; p; L% F* {8 n/ F2 B
  3. Return x**y (x to the power of y).0 T3 v+ f0 x9 y2 G. n8 H8 B& U
  4. >>> math.pow(3,4)6 I; o( r3 s. i
  5. 81.0
    1 x7 c7 o) L2 N0 q
  6. >>>
    1 f- x/ }% D0 R; D, z& u7 P6 w
  7. >>> math.pow(2,7)
    - t: u1 B7 n1 g2 @) B3 \
  8. 128.0
复制代码

* s5 N: g% @& U9 l1 `& s& Rmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# t' h/ z. G8 Q# G/ g1 [
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    + z1 P. l( `5 E3 A
  2. log(x[, base])* D2 O8 n5 y/ ]( A0 T# W3 k) `
  3. Return the logarithm of x to the given base., N+ q% s, B% p! z3 t
  4. If the base not specified, returns the natural logarithm (base e) of x.: q& V- B) ~2 h9 r0 h; G
  5. >>> math.log(10); d" a2 r3 ~$ e( b* Q* e2 B9 l( P
  6. 2.302585092994046
    ! g3 c0 o' g4 B! Q$ z; o6 E+ \
  7. >>> math.log(11)8 g4 Y. h, V3 l/ d* _$ U: Y7 _
  8. 2.39789527279837078 m+ [8 q0 m9 i" [* X
  9. >>> math.log(20). R& u5 V( V0 p+ F" G( U
  10. 2.995732273553991
复制代码
/ K2 t. I1 k. Q) w; h+ u( O3 T9 J
math.sin(x)  求x(x为弧度)的正弦值
# z/ L& \2 J# s& d7 u1 U/ w" Q! ^
  1. #求x(x为弧度)的正弦值
    ) ~6 _" L: i/ O8 ~. Q2 P8 ^
  2. sin(x)0 F+ s1 n; i% \3 D$ |, y
  3. Return the sine of x (measured in radians).& h2 H# p- \5 Y7 V7 k/ n8 x2 H
  4. >>> math.sin(math.pi/4)* Q" t- `& W, ^, h
  5. 0.7071067811865475
    9 g- q7 q( |% O3 J% r
  6. >>> math.sin(math.pi/2)9 M5 L, {" f3 m
  7. 1.0& j5 Y2 b! f! ?) T6 T
  8. >>> math.sin(math.pi/3)
    & z( I: C; ?; @/ k$ j: q" ?/ H
  9. 0.8660254037844386
复制代码
, J7 N- f/ [1 i) q. w
math.cos(x)  求x的余弦,x必须是弧度2 r+ L* {1 O* K# T4 U, }
  1. #求x的余弦,x必须是弧度' Y( I( B1 m' e7 Z+ P, e$ s
  2. cos(x)
    3 q" ^& y3 {7 f* U6 v1 w4 F
  3. Return the cosine of x (measured in radians)./ D/ }- P( \0 H2 j: w" B( D$ X
  4. #math.pi/4表示弧度,转换成角度为45度) s- |! R$ _  Q. `2 h8 W
  5. >>> math.cos(math.pi/4)
      T( J3 m9 s0 [5 W! Z3 X
  6. 0.7071067811865476; R0 p, H" o# F' I/ U8 i
  7. math.pi/3表示弧度,转换成角度为60度7 W! ?/ O9 l1 q0 \/ h
  8. >>> math.cos(math.pi/3)
    2 @, x6 ?7 [1 P
  9. 0.50000000000000018 O8 E. o3 q$ f2 ?. I
  10. math.pi/6表示弧度,转换成角度为30度0 T/ F/ t6 X- A6 B; Q. L, Q
  11. >>> math.cos(math.pi/6)& `# i9 S% f' ?- j
  12. 0.8660254037844387
复制代码
1 ^4 N, t7 Q" U/ ^9 ~
math.tan(x)  返回x(x为弧度)的正切值  q" j4 _5 T0 O
  1. #返回x(x为弧度)的正切值1 K6 y1 u. D, O4 W- ?7 s: A1 j
  2. tan(x)+ ]% {0 `# p0 ^. Y0 [
  3. Return the tangent of x (measured in radians).
      S" R2 N2 c) }9 p4 {) j! z/ e
  4. >>> math.tan(math.pi/4)& h( C5 ^( [0 P' V' V& I0 F
  5. 0.9999999999999999
    7 [+ v8 E% m* J) \: K1 K
  6. >>> math.tan(math.pi/6)3 @6 i) y; U* O  C, Y! T$ @4 z
  7. 0.5773502691896257
    8 s+ z$ i) @' _
  8. >>> math.tan(math.pi/3)6 Y+ X( _- q6 N/ [+ K( d
  9. 1.7320508075688767
复制代码
# `9 }5 _" H: D5 Q0 E
math.degrees(x)  把x从弧度转换成角度
0 J: O- R! ^" a5 I: r9 K: q
  1. #把x从弧度转换成角度5 Q% H1 d9 {& [
  2. degrees(x)
    & S! z3 h: n+ ^( [# e" C1 s2 u
  3. Convert angle x from radians to degrees.$ J- _5 t8 F8 I9 X/ i

  4. . ^9 F' R9 R9 \
  5. >>> math.degrees(math.pi/4)
    * ?( E! X) ?5 U, B; h
  6. 45.0
    . p+ A7 w3 h) ^- m1 Z$ Z; B; ~
  7. >>> math.degrees(math.pi)! t9 L' P* [1 p7 \
  8. 180.0
    / V1 e- w/ X) D
  9. >>> math.degrees(math.pi/6)
    % N1 }7 P" a6 P
  10. 29.999999999999996
    0 q" b' Z5 w5 w3 _* k8 W
  11. >>> math.degrees(math.pi/3)
    , F: H' P/ L6 u6 [
  12. 59.99999999999999
复制代码

" I6 m7 Y: [, C3 umath.radians(x)  把角度x转换成弧度# X4 q$ N4 c& @7 k/ s8 O
  1. #把角度x转换成弧度* Q! D- h# t" G
  2. radians(x)+ B1 \; G* k9 ~3 m. N5 L- T# m# G
  3. Convert angle x from degrees to radians.( b( f! f  W1 W; u2 U. z0 {) v' H( G
  4. >>> math.radians(45)" ^! @3 q/ a; R; m
  5. 0.78539816339744836 d6 l/ w% Q# F
  6. >>> math.radians(60)
    ; }+ _& l  W2 p; v% u0 y$ {
  7. 1.0471975511965976
复制代码
7 D3 o1 Y0 I+ W/ C) v) m: \; `
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
7 L% F, P5 E+ J9 x
  1. #把y的正负号加到x前面,可以使用0
    ) x: C8 j$ ?) |+ V8 U
  2. copysign(x, y)
    8 F$ D% ]6 s2 x3 c/ I" T5 ?( j& l3 K
  3. Return a float with the magnitude (absolute value) of x but the sign
    9 ]* y. U  o( _4 ~
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 D: P! [  T+ J6 e" x2 ^/ n  y! m
  5. returns -1.0./ y' V6 T3 V0 U# A

  6. - N2 n+ B4 H! l5 F  V
  7. >>> math.copysign(2,3). \- @6 @, t2 [5 u5 d. d3 P
  8. 2.0, T9 r! B& U  x+ v* O# O. L
  9. >>> math.copysign(2,-3), f1 S+ B# [' d) {8 Q. P. ~  m
  10. -2.0$ H& a. q. g2 L6 |8 j
  11. >>> math.copysign(3,8)# \0 h, o; C; l0 o
  12. 3.0# ^0 @$ F' ^1 S' ]
  13. >>> math.copysign(3,-8)' W- S# ]# _7 z2 P4 `
  14. -3.0
复制代码
% W4 s& v5 S" h, Q1 L' K7 I. a
math.exp(x)  返回math.e,也就是2.71828的x次方
; X+ E3 C, ~2 L
  1. #返回math.e,也就是2.71828的x次方
    * y- K, K! p- _
  2. exp(x)
    $ X: }* {& [' k" |
  3. Return e raised to the power of x.+ A; l( D0 h1 ~, |
  4. 2 A; p$ p& R& Z2 n* U# E
  5. >>> math.exp(1); Y! ]5 F# [' b5 o( ?' d% a
  6. 2.718281828459045
    / q& }* ?% U( s& K, G  b
  7. >>> math.exp(2)- m5 B& l9 @1 w, R1 y" ]# }- W
  8. 7.38905609893065
    / z( t. D+ k5 H6 L, K; E' y: U
  9. >>> math.exp(3)
    ; z3 C7 q( @, J/ Y/ w% X9 S  R( _
  10. 20.085536923187668
复制代码

8 w* O1 B4 v) R* bmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
( \  b- H8 `: B& q  U0 ~
  1. #返回math.e的x(其值为2.71828)次方的值减10 |( B' Q: ~- k9 f# S
  2. expm1(x): ^3 [) h5 @. R$ I
  3. Return exp(x)-1.4 v0 `# s0 U( v6 d) e/ _7 E" J  ~
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    " Q5 a# \  _/ e1 k5 _

  5.   |0 K# w9 T4 z
  6. >>> math.expm1(1)
    , A, |) t( f: l% E% I& D- u
  7. 1.718281828459045: X* x  Q& S  E. u/ `7 Z
  8. >>> math.expm1(2)9 x7 T" i# s* Z1 e9 }# k
  9. 6.38905609893065
    " W4 ]  w& k; M1 t
  10. >>> math.expm1(3)
    ; b7 p, m- Q* A0 Y; A
  11. 19.085536923187668
复制代码

# X5 a9 f& D* H8 Y5 {3 `8 A3 c, Mmath.fabs(x)  返回x的绝对值# L- ?2 [" f' W
  1. #返回x的绝对值0 t3 X. p$ n) x2 M) F5 T! q
  2. fabs(x)9 Z/ S& a( P$ i4 G8 B5 Y/ B: e
  3. Return the absolute value of the float x.
    1 g+ [" R" m1 ?" S" d
  4. 6 E+ o; Y& H) a/ v
  5. >>> math.fabs(-0.003): S  T6 S7 k5 P' `! U8 s7 d
  6. 0.0036 Q' A6 P$ h  X* Y6 g
  7. >>> math.fabs(-110)8 {* D: B& c7 ]# s# \/ Y
  8. 110.0& S# f5 D5 y+ G2 ^0 H
  9. >>> math.fabs(100)
    9 v, u) a# l) P; w' c: [% v
  10. 100.0
复制代码
6 @& \2 A/ m0 A  b2 K
math.factorial(x)  取x的阶乘的值4 `/ B& E5 ?8 n
  1. #取x的阶乘的值
    . o5 r& c! R% F( ^! g3 N1 V
  2. factorial(x) -> Integral2 j2 u" ^% g0 W- T: _9 X- V3 P4 k
  3. Find x!. Raise a ValueError if x is negative or non-integral.* g! N$ i3 z8 o/ B; r$ q* E6 T* p( v
  4. >>> math.factorial(1)8 K0 Z7 `1 y( B0 e
  5. 1  i- L; e' w+ {, u
  6. >>> math.factorial(2)
    $ l* t' e! G2 E8 Q) i; |
  7. 2
    ; s/ R% P' T# V9 H% @
  8. >>> math.factorial(3)4 r; q9 C0 i& G, C) C
  9. 6
    ; @9 a3 w) ^" E1 \- }1 M' t
  10. >>> math.factorial(5)
      b5 ]6 v) U/ d4 i1 j
  11. 1204 Y$ a2 g+ N7 i
  12. >>> math.factorial(10)
    / {2 \  L4 S# A6 M3 M* i( T. C
  13. 3628800
复制代码
( J3 `4 A) R7 D, n
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
! r8 O! O( \6 P1 @9 q+ D6 p8 c
  1. #得到x/y的余数,其值是一个浮点数, \! o  @2 X& g2 m
  2. fmod(x, y)
    9 ?: {6 t! O( v  {# s. J2 x2 J4 A( V
  3. Return fmod(x, y), according to platform C.  x % y may differ.
      [8 [% Y  |- Y: z  }5 X3 g; `5 I* F
  4. >>> math.fmod(20,3)
    & v2 U1 x( U: A. K
  5. 2.0
    * I' `! R5 l$ Y( y$ J# `% L
  6. >>> math.fmod(20,7)5 ~  }- A6 `& r
  7. 6.0
复制代码
* ~" a  S+ y) W; u  B
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 w0 ?6 R& X- M0 {" a8 k% w: X
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,4 F. |) Q5 C- Q2 D! y
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ) I4 g" j, k* w$ J- M
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    ; j1 d% g+ @( W$ s
  4. frexp(x)5 m! l0 z, r6 l9 H$ a) o
  5. Return the mantissa and exponent of x, as pair (m, e).8 [  M7 y6 n# p, }+ G
  6. m is a float and e is an int, such that x = m * 2.**e.+ A5 I2 f0 h" G
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    9 F( W9 x5 K6 _% p- N
  8. >>> math.frexp(10)! M) a& B8 t$ E3 c; e; u
  9. (0.625, 4)7 [4 v1 {6 J: P
  10. >>> math.frexp(75)" L7 i( Y) k, Q; c
  11. (0.5859375, 7), d' h8 M( Z2 j* `/ G" Y$ K
  12. >>> math.frexp(-40)
      m2 e  D  L1 h6 ?
  13. (-0.625, 6)2 q. L# R$ q8 x
  14. >>> math.frexp(-100)% r4 C) Q9 `' T" [
  15. (-0.78125, 7)/ i" ?  V+ u" ~3 r+ I6 b
  16. >>> math.frexp(100)
    9 A5 F- y: v* C& i% k  M
  17. (0.78125, 7)
复制代码

. u) v* Q" b; L5 n2 l& h+ Fmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列0 ?& B* A8 |/ }, G
  1. #对迭代器里的每个元素进行求和操作# ^% Z2 i, F; a5 _0 t
  2. fsum(iterable)
    " X( n' R$ i/ c, h5 w
  3. Return an accurate floating point sum of values in the iterable.
    4 D% U# w9 g- I" i
  4. Assumes IEEE-754 floating point arithmetic.
    & c# ?. H$ O0 L: _7 L
  5. >>> math.fsum([1,2,3,4])
    ( s% j/ _. x' U
  6. 10.0+ [. h/ g& w2 V: `! e7 {+ o( j
  7. >>> math.fsum((1,2,3,4))
    ( I0 w- E7 P3 {
  8. 10.0
    - R  O% F8 h) A. p
  9. >>> math.fsum((-1,-2,-3,-4))3 i  O2 t4 i- I- C
  10. -10.05 X  T  z7 u" Q) A1 h+ |5 T
  11. >>> math.fsum([-1,-2,-3,-4])  w1 @! Z  ]+ p# x9 p/ F
  12. -10.0
复制代码
% E. {6 i2 r( \6 F% |
math.gcd(x,y)  返回x和y的最大公约数
& I/ d2 C" z: [5 R! W
  1. #返回x和y的最大公约数
    ! g8 |1 c6 b4 G3 T( Z+ Q$ U
  2. gcd(x, y) -> int7 t* _6 W, E4 W
  3. greatest common divisor of x and y5 l6 @4 ?& n+ s4 J7 u! U. K, c
  4. >>> math.gcd(8,6)6 ]. F0 b& K0 @7 O
  5. 2
    ; ^+ a% w: ~9 }- G2 T* D1 S1 q8 a
  6. >>> math.gcd(40,20)
    ) b2 \1 b" r5 ^6 J, a! |
  7. 209 z2 r" j% K! P
  8. >>> math.gcd(8,12)1 A8 `, F/ R) I7 S
  9. 4
复制代码

4 h6 Y. G5 H& lmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False0 t: A; `" v: D: p' O8 M) q) X
  1. #得到(x**2+y**2),平方的值
    * B  }8 o0 K6 N* B" k  {
  2. hypot(x, y)# U. f' M. m3 |4 Q, b, y$ P2 S
  3. Return the Euclidean distance, sqrt(x*x + y*y).3 ~% e* z, o1 K# u; a& ^$ x+ v
  4. >>> math.hypot(3,4)+ N4 }3 E6 g& E2 g/ Z6 p. _
  5. 5.0
    . [% r* z& X7 s, F+ S3 m
  6. >>> math.hypot(6,8)+ J& I$ j  ~4 C" Q  u% o6 }9 y
  7. 10.0
复制代码
# R8 g, a& O  f& A  J4 j
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
3 l& z. j; S' M
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    8 ^$ F) l8 a+ g" l- t& }8 K
  2. isfinite(x) -> bool0 V  H8 @( k8 M& f
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.* A; e5 O4 t) a( H9 E0 d" h
  4. >>> math.isfinite(100)
    $ k$ J4 d$ F9 B5 d" r
  5. True4 E# u; \: Z. R) S
  6. >>> math.isfinite(0). Q9 f& t7 u3 n5 \8 k
  7. True$ d+ o3 ~3 y& V# ~* x. _
  8. >>> math.isfinite(0.1)
    3 z% u7 ?# y" @8 c4 ]0 k. A
  9. True% B% u: N% @# c3 ^7 e, N0 @. D
  10. >>> math.isfinite("a")# u) n, ]# U- p. H5 q: j' q) e
  11. >>> math.isfinite(0.0001)
    4 s' O7 A/ f& c  g* }
  12. True
复制代码

5 h3 N7 k: Y1 s! `* ?7 |5 L$ Hmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
! i% x$ H1 V6 Z- Q4 {
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    ; H/ J# v, B* C$ U( B
  2. isinf(x) -> bool7 d7 D. D5 h) x. D" H
  3. Return True if x is a positive or negative infinity, and False otherwise.
    , d4 ~( |, a2 e6 x) Y& |/ d
  4. >>> math.isinf(234)
    - v5 \5 T' s* ?' W$ T1 k4 Z! j4 R
  5. False
    % q3 `: k9 `2 t+ z2 |$ Q7 `
  6. >>> math.isinf(0.1)
    * z" W9 T8 [8 m% ^; u$ ^# L7 O
  7. False
复制代码

" T& K9 t: D2 \8 s, ]math.isnan(x)  如果x不是数字True,否则返回False
& q& D/ n( ~; b
  1. #如果x不是数字True,否则返回False, Z3 t, H/ p2 r3 c8 p, a
  2. isnan(x) -> bool9 W2 e4 V' @3 t
  3. Return True if x is a NaN (not a number), and False otherwise.. e" i( u: G/ K; F( _+ o
  4. >>> math.isnan(23)
    ; C: y* y# I7 Y* ], B, w
  5. False9 k# G, h1 E( |* E3 S
  6. >>> math.isnan(0.01)4 d# l6 Z5 a( y# t" J- }0 Z7 j0 s
  7. False
复制代码
$ b5 }4 D3 I! ~* r% j
math.ldexp(x,i)  返回x*(2**i)的值
9 {% x, j; a! G5 A4 h" Q
  1. #返回x*(2**i)的值; h5 R- ^  k* [9 t& K7 Y8 G, W  q- g
  2. ldexp(x, i)( _) q  Z% _8 H3 k0 r, g2 p1 _
  3. Return x * (2**i).  P7 e4 h1 G  ^
  4. >>> math.ldexp(5,5)
    $ [* k. o; y9 s- P1 Q
  5. 160.0
    3 V3 s* H, C  ^) i& {- L$ \
  6. >>> math.ldexp(3,5)
    . e/ x0 c! S' ^* z, q- O9 f, A
  7. 96.0
复制代码

0 \% d* C* g3 c) i8 qmath.log10(x)  返回x的以10为底的对数
, I7 ^+ v$ ]8 j0 y! Q* m$ `
  1. #返回x的以10为底的对数
    " Z8 J4 T% H+ \) K# e9 L2 Q
  2. log10(x)
    2 z) W* D% z( ~, v# M: J
  3. Return the base 10 logarithm of x.
    & ^3 K4 c. S! g
  4. >>> math.log10(10)2 F5 O, w, |' z2 y  k5 _
  5. 1.0
    9 E) [0 O5 [% v  L% K, i" }) Y
  6. >>> math.log10(100); f. B5 E: T7 i
  7. 2.02 g% m5 G3 T% j: q# t
  8. #即10的1.3次方的结果为20/ E+ q5 a) y% y" M- e2 p. }
  9. >>> math.log10(20)
    # l. I+ j2 z- x! I% |- ~
  10. 1.3010299956639813
复制代码

% D2 X6 a6 z& z, _6 u/ @, A  ?+ `( P& L, omath.log1p(x)  返回x+1的自然对数(基数为e)的值
6 C" \' S7 }$ u% J& ^
  1. #返回x+1的自然对数(基数为e)的值! _6 R6 G' l+ X0 r* {
  2. log1p(x)
    : E* }+ f1 d' P1 e! _8 R: M
  3. Return the natural logarithm of 1+x (base e).# y" X6 s1 }# K0 z& D6 P/ G" l) K
  4. The result is computed in a way which is accurate for x near zero.
    ' w# ]: b: t( m7 `* ]. H
  5. >>> math.log(10)
    ( J  g1 p& j- }! `
  6. 2.302585092994046
      n: m2 i2 g* ^( H2 R* [6 \, \
  7. >>> math.log1p(10)4 U8 ^, j+ b0 J+ X
  8. 2.3978952727983707* D/ f- [2 j4 O( E% G$ c2 [% B7 j
  9. >>> math.log(11)( P" T% l; ?9 n/ x* i
  10. 2.3978952727983707
复制代码
. u+ Q* T5 x# v+ w3 K% X
math.log2(x)  返回x的基2对数
% q- j# f! q8 R0 D; ]
  1. #返回x的基2对数, S0 H+ }0 v4 `" X7 P9 A  A) q
  2. log2(x)8 E9 Q/ V3 ?9 L$ K$ \' B  m1 Q
  3. Return the base 2 logarithm of x.& ?1 k$ n) ]8 B/ Q4 ~
  4. >>> math.log2(32)/ a" S3 ]$ a, I
  5. 5.07 e. t$ J. z$ ~; G0 X0 I
  6. >>> math.log2(20)% L% J% i0 H) U2 a- C1 j& X3 q: @
  7. 4.321928094887363
    4 q8 k# P1 w9 ?( ~7 R1 u
  8. >>> math.log2(16)
    ! x4 s$ s/ B5 l. ?: ]& J* x
  9. 4.0
复制代码
9 h" ], E! {" C5 S8 s9 d
math.modf(x)  返回由x的小数部分和整数部分组成的元组
5 f+ r' c% L& I# e1 W+ K* }
  1. #返回由x的小数部分和整数部分组成的元组4 r2 b4 l* D7 _9 h5 T- a: }
  2. modf(x)9 S1 X+ o% \" x1 O( l1 K
  3. Return the fractional and integer parts of x.  Both results carry the sign
    * @" V' m* k# c# F2 C
  4. of x and are floats.& }! R$ Q2 c+ N. M& a8 h: c) p
  5. >>> math.modf(math.pi)1 ?( u% I; [8 s0 ?% @1 S- m2 d
  6. (0.14159265358979312, 3.0)
    2 v% h& i3 `* }9 ]. W
  7. >>> math.modf(12.34)+ [, T% l7 ~: Z" F! A/ T$ G& W
  8. (0.33999999999999986, 12.0)
复制代码
% k! Y. Q$ @9 O; [
math.sqrt(x)  求x的平方根
+ g1 E2 F7 H2 C5 ?- S2 x) L' g
  1. #求x的平方根
    ! B6 i( c5 Z- X; A
  2. sqrt(x)
    / \( A' L$ ?4 U$ }
  3. Return the square root of x.% _3 b3 S# w/ c) W4 F% H
  4. >>> math.sqrt(100)# C7 j9 Y- n) ^9 R
  5. 10.07 s& ^7 v: G" o' A
  6. >>> math.sqrt(16)
    0 A4 A( K* X3 X; z" n: Z$ K$ o- N
  7. 4.09 U1 C; [# K0 p- t. ^
  8. >>> math.sqrt(20)
    ) p- B0 i/ A( {( L; `1 u% Q5 d
  9. 4.47213595499958
复制代码

2 z3 J2 B6 b9 r  I. C. C) E/ J4 ]math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    : `# k0 y- W' D. a+ X
  2. trunc(x:Real) -> Integral
    # @$ K& h( F+ g$ l/ P
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    3 E, S' \! y+ _
  4. >>> math.trunc(6.789)6 R& a2 E, z  `3 k  \+ f6 n! k$ _
  5. 6
    + `- K3 Y; }5 b( {& K7 y
  6. >>> math.trunc(math.pi)- l' U5 T% H3 t' Q2 x. b
  7. 3
    , I. L: o$ [( p8 f
  8. >>> math.trunc(2.567)7 @$ }8 j2 |6 u
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-20 22:27 , Processed in 0.082489 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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