新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
: x  _/ F8 u" }' m( \) R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。0 u5 l9 x. F+ l/ S+ r+ T
7 O3 K. ?: e! @
方法1
0 z* D) h2 y% Z  T; s
  1. >>> import math& v: z4 O( I3 a# S7 C
  2. >>> math.sqrt(9)3 O- I- Q  s* [4 o
  3. 3.0
复制代码
方法25 ~4 F: Y' Z! y* `7 O
  1. >>> from math import sqrt
    ' n& p7 Y: @( H% e) D$ K
  2. >>> sqrt(9)8 S7 H2 @6 a: ^  u8 d7 z  g. |/ K2 @& n
  3. 3.0
复制代码
; b$ @7 I4 T$ u3 l2 e) J' S. v" t


( R6 G2 T: v1 j; ~$ ^
math.e  表示一个常量
7 r; F- [9 Z1 A5 ?3 S% I/ ~' ^
  1. #表示一个常量+ f5 Z+ T" p  l; j$ t
  2. >>> math.e
    , {$ D5 Y# N+ @
  3. 2.718281828459045
复制代码

, j3 h( U. v- X6 v" G. U, r* O& _* Vmath.pi  
数字常量,圆周率

) j; h7 |$ l( H$ Z% P( h8 A
  1. #数字常量,圆周率
    5 u) }6 `- k! e% ~& p+ y3 g
  2. >>> print(math.pi)
    3 e! `* e$ T  x$ R! M$ @* B
  3. 3.141592653589793
复制代码
8 [3 p1 |0 N# t3 ]6 H) y% B" g
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

( ?+ Q- ]: ?- P# [, o( y- S
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    + B6 A& J' Z% c- X$ t/ ?9 z6 X
  2. ceil(x)
    9 l; _- ]3 d/ v& {
  3. Return the ceiling of x as an int.& f) K3 e7 y% w- G* y9 U% c4 t+ W
  4. This is the smallest integral value >= x.* d1 r( {+ E; O. J8 I
  5. * P% C4 C& P; q+ E2 \( W, }
  6. >>> math.ceil(4.01)
    4 }1 w- |5 ~# X7 X' f* D
  7. 5! q8 S' j8 ?/ ]' ]* ^
  8. >>> math.ceil(4.99)
    1 g# M! K/ M& H, v4 P; Q9 z
  9. 5) U' Y# t+ [" j; l5 ^
  10. >>> math.ceil(-3.99)
    $ e0 z' Z2 O6 C4 y. d7 Y
  11. -30 u0 [: n0 v6 @) k: F) g* L
  12. >>> math.ceil(-3.01)7 N5 Q  Y  H7 O9 V" c$ a; b2 H% J
  13. -3
复制代码

1 c3 r* o1 U* m1 O( U1 B( umath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身9 Q, o. q6 z- Y2 M; i  p4 K8 d# s
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    # n- O$ R0 ?, A/ [. w7 N
  2. floor(x)) M, Y5 H, l# N6 k& v
  3. Return the floor of x as an int.
    0 O" t6 A0 f! S2 n
  4. This is the largest integral value <= x.
    $ f; e, Y1 o* h6 j7 @* p
  5. >>> math.floor(4.1)' J/ P  j1 ~! m9 o' I5 `
  6. 4, Y# S/ s  X& D, C6 E. [. o- I
  7. >>> math.floor(4.999)
    ( a8 Z$ W! q3 Y" K" m( E
  8. 4
    # W' k8 [4 W1 k' K+ }
  9. >>> math.floor(-4.999)$ J, R; i5 H& [$ S
  10. -5
    : ^4 z1 i/ r5 w% K7 W* S
  11. >>> math.floor(-4.01)- j( J: ~. {( O& @, H/ U8 w; ^
  12. -5
复制代码

/ T7 H* r" h" E6 \6 vmath.pow(x,y)  返回x的y次方,即x**y2 J" Z: {- P# m6 b6 W& k! ~
  1. #返回x的y次方,即x**y8 O3 x* r. K' o' v9 S) d
  2. pow(x, y)4 g8 \4 k* b' g! Z
  3. Return x**y (x to the power of y).
    & B2 x0 u0 h' r1 H' Z
  4. >>> math.pow(3,4)
    ( }5 ^2 ~( O3 u% B$ r6 L* b
  5. 81.0
    7 _; h7 r. H; m1 o: C0 v# Q6 ^
  6. >>>
    % G0 r+ I1 N, R& V: c
  7. >>> math.pow(2,7)
    ! @/ t+ D  G/ }% z, C2 b
  8. 128.0
复制代码

* v+ y, d5 ]8 {2 n# j7 ?& j  |9 rmath.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
4 k) D9 @- C- c4 y0 F1 w
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    8 k4 Q# T  b5 k( H/ m
  2. log(x[, base])
    ! c* r  h3 j! O* w' g' @- l
  3. Return the logarithm of x to the given base.
    2 M! W, s( g) \) o- G
  4. If the base not specified, returns the natural logarithm (base e) of x.$ N) W4 O  R$ s: x. H
  5. >>> math.log(10)
    9 g2 p/ L! k8 D/ S* N! e1 B* R
  6. 2.302585092994046, f& h9 A' ~. Y; ^9 v; N+ s1 w$ ]) j
  7. >>> math.log(11)! O) ^+ h. \- p$ F+ N
  8. 2.3978952727983707
    6 P9 }- h) G3 s  U% Q* Y
  9. >>> math.log(20)
    : i$ _. s) y$ C( {- `0 V: W9 [
  10. 2.995732273553991
复制代码
4 B4 [' e, r: B0 a8 b
math.sin(x)  求x(x为弧度)的正弦值
$ G" y+ z7 g' {$ y& P6 j6 j- F! k  o
  1. #求x(x为弧度)的正弦值
    9 m# G" D4 e3 Y1 q1 H7 |
  2. sin(x)
    " f: l" A' x- a4 n' \9 Y3 x6 C8 \0 {& [
  3. Return the sine of x (measured in radians).$ n$ G- l! V5 q. H& E1 H8 S5 K
  4. >>> math.sin(math.pi/4)
    , V9 ^# i6 t. L) ]
  5. 0.7071067811865475! }0 U5 K( l- X, S+ B# I5 t  ]7 L
  6. >>> math.sin(math.pi/2)
    0 z: x4 N1 ?% D+ L
  7. 1.0
    $ k; V4 v( r/ m; \4 i
  8. >>> math.sin(math.pi/3)
    2 N' N. c& g" }1 f, _; @7 _
  9. 0.8660254037844386
复制代码
( R/ q6 S  k" i! }( h* g
math.cos(x)  求x的余弦,x必须是弧度, R! @7 B4 j; h
  1. #求x的余弦,x必须是弧度5 M3 {5 ?( b6 m3 ^: O
  2. cos(x)7 {8 w# k& {2 Z( j
  3. Return the cosine of x (measured in radians).. L+ X6 w+ D5 Z, g' G
  4. #math.pi/4表示弧度,转换成角度为45度8 Q! k- c& \+ m+ O) T- x+ v
  5. >>> math.cos(math.pi/4)
    3 i* G" A6 |  K  F2 n' {: V) ?
  6. 0.7071067811865476
    1 H% s1 z3 a/ E
  7. math.pi/3表示弧度,转换成角度为60度
    3 R: ~$ j3 N* N" m
  8. >>> math.cos(math.pi/3), g8 g. I: G+ o) r: N4 v
  9. 0.5000000000000001' v, x" M' W2 o+ n' {* b  v
  10. math.pi/6表示弧度,转换成角度为30度
    5 C& @5 ?; \. J2 d7 [
  11. >>> math.cos(math.pi/6)
    " S% L6 Y( O; a
  12. 0.8660254037844387
复制代码
; s( e# h4 V# c  _! g& l# @
math.tan(x)  返回x(x为弧度)的正切值
* K3 r* `5 @2 g  ^; z$ \5 }1 m
  1. #返回x(x为弧度)的正切值0 w8 j; y' i8 a& Z
  2. tan(x)
    ! K* b1 q+ H; b6 x" b
  3. Return the tangent of x (measured in radians).
    . P3 n, w; l" L# s+ U/ }5 s  v
  4. >>> math.tan(math.pi/4)( }' Q) W4 W, X) s3 u: z; ^
  5. 0.9999999999999999* j% ?" y. m0 V4 r2 p1 d# n4 `
  6. >>> math.tan(math.pi/6)1 p5 e# Q% k% e. r5 l+ x! W1 J
  7. 0.5773502691896257
      v" }# O5 \7 o; L
  8. >>> math.tan(math.pi/3)
    # X) U8 |( K: }* S$ x7 @( g* m
  9. 1.7320508075688767
复制代码

' I$ G2 X2 G0 Y; G* s9 Y8 Omath.degrees(x)  把x从弧度转换成角度! y& S' q( D) P6 g9 Y0 j
  1. #把x从弧度转换成角度6 q9 d( b; A0 L
  2. degrees(x)# _6 f* Z, W- d/ I$ q$ Z. G& W
  3. Convert angle x from radians to degrees.! y" R' i' p9 v% S

  4. + Y8 E7 f' I" y. Y% F
  5. >>> math.degrees(math.pi/4)# q& T* X/ x/ _4 }$ p+ ^( Y" s
  6. 45.0* g' U. k( D% H( c5 s$ _1 v, P* H
  7. >>> math.degrees(math.pi)2 p- L; s4 K; T9 \: U/ o3 {! K
  8. 180.0
    - ^. o  f( |) e+ @- n
  9. >>> math.degrees(math.pi/6)
    7 z# D# I  c+ L* x
  10. 29.999999999999996% E4 ]  c; I' b
  11. >>> math.degrees(math.pi/3)
      n6 Z2 Z9 [3 q' W/ V
  12. 59.99999999999999
复制代码
3 w( T: Z: q# m+ k
math.radians(x)  把角度x转换成弧度% k. r& D/ x5 \/ |+ p0 x
  1. #把角度x转换成弧度
      F0 U; A4 ?' d
  2. radians(x)2 {: c0 @; }6 N1 @" h/ y
  3. Convert angle x from degrees to radians.7 S! A* K/ F! k9 m7 A0 y# {
  4. >>> math.radians(45)( e& F# y  k' a: E8 W0 K
  5. 0.7853981633974483
    - ~9 x8 A1 f  q& [' q
  6. >>> math.radians(60)
    7 q4 r1 r5 O+ M" C. d+ @8 n
  7. 1.0471975511965976
复制代码

2 K( r- P; F+ z; l' X; h  L/ M( A. Dmath.copysign(x,y)  把y的正负号加到x前面,可以使用0: S! Z2 i7 X: S# ?2 B' e. q7 ]) @
  1. #把y的正负号加到x前面,可以使用0
    8 h9 ~" ?5 e9 N$ c% h: ]
  2. copysign(x, y)& K3 c( F9 S! ~7 h& F9 D
  3. Return a float with the magnitude (absolute value) of x but the sign 0 s3 ~+ A8 S8 Q3 X
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    % X/ ]) K5 u5 c& {
  5. returns -1.0.
    $ T0 ~. u( @- z4 z2 n- B1 P0 x$ `: W

  6. 4 Q! l+ P% l7 U( {' i
  7. >>> math.copysign(2,3)" u# J; N# x- {; k' M0 r: i/ t
  8. 2.0- M$ G% ]9 P' {7 M! C& ^
  9. >>> math.copysign(2,-3)( ?% r& \4 i' G6 `8 P6 U0 O
  10. -2.0
    ; g2 @( w" H+ l5 |3 u* X; Q
  11. >>> math.copysign(3,8): R" h& T; a6 e+ Q$ _. l, a  P" z
  12. 3.0
    5 |0 Q1 v% ~9 q& P3 k
  13. >>> math.copysign(3,-8)
    - w3 _6 A7 h  a. F( u8 |
  14. -3.0
复制代码
+ n" Q9 X. _' R, O
math.exp(x)  返回math.e,也就是2.71828的x次方# g( N7 }4 A/ p$ a- i( M) J: B, |( `
  1. #返回math.e,也就是2.71828的x次方
    % a2 F) d+ q' M: F1 }
  2. exp(x)- ~6 X. v1 l- Z
  3. Return e raised to the power of x.: t+ Q% w$ h2 P. k8 }6 E/ e6 ?" L

  4. % D6 D. H/ e% E5 r5 }9 ]
  5. >>> math.exp(1)( C9 S, T/ v) X
  6. 2.718281828459045& u2 ~2 g* r- {7 ?4 A
  7. >>> math.exp(2)6 f5 T0 }6 ~( F3 {5 t- ~# @4 W9 D
  8. 7.38905609893065
    % L5 V! F# I& v  Q
  9. >>> math.exp(3)
    - M9 o( Y# M' h* _( O
  10. 20.085536923187668
复制代码
, E  f3 \* z$ e* l* j
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减14 W( ~/ R1 _! u( T# |5 R3 V
  1. #返回math.e的x(其值为2.71828)次方的值减1" i) ^( {. h: s0 q" Q0 x; A: v2 S3 x
  2. expm1(x)
    1 J+ S! Y0 M: R) T
  3. Return exp(x)-1.  q$ Z& C8 `% C) X4 ^. M* m1 S
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.' Z( q/ I9 ?3 b# f
  5. 7 B+ Q+ |1 }) \& E
  6. >>> math.expm1(1): J6 m" S/ D/ M3 m6 v3 K) n
  7. 1.718281828459045
    % b$ m' q% Y  q* d0 |
  8. >>> math.expm1(2)
    ; P4 I% a+ \* _: F" {
  9. 6.38905609893065
    ' d/ X! C8 T( g% U' L! [1 H% V: u
  10. >>> math.expm1(3)- e4 i/ i- D. x) k: J3 d
  11. 19.085536923187668
复制代码
7 f2 H4 e3 t9 h" j2 F( P* S
math.fabs(x)  返回x的绝对值# X" P( j. q: D- v7 i" [, A
  1. #返回x的绝对值0 |6 W$ C" o+ i$ K% I0 v
  2. fabs(x)
    ; e# h" v! n4 K  g
  3. Return the absolute value of the float x.
    5 D( q& P7 w/ D9 N* \0 e) b

  4. 8 K+ _' a7 G; V# q# P5 y' F' d1 ^4 S
  5. >>> math.fabs(-0.003)
    ! c3 s1 X0 G0 D7 }5 i& S7 a  i
  6. 0.003
    ( R. l+ Q/ d! [. A2 q
  7. >>> math.fabs(-110)' @, q) V/ e) U6 E0 j  _
  8. 110.0* L% o7 V: z- G2 o( D
  9. >>> math.fabs(100)
    2 g$ z% I# H2 D8 l$ @7 t9 v
  10. 100.0
复制代码
( M+ H3 X: f' @0 a9 \
math.factorial(x)  取x的阶乘的值  n" t* K  H! ^$ r) {
  1. #取x的阶乘的值( `: D- p. g! T
  2. factorial(x) -> Integral/ T  ^# j5 O* Y1 _
  3. Find x!. Raise a ValueError if x is negative or non-integral.* v# V% J* q3 T8 F! |9 i8 M: }$ Q
  4. >>> math.factorial(1)
    8 ]5 q$ F5 s. N4 T
  5. 1/ I9 P2 `7 I2 M! q4 P
  6. >>> math.factorial(2)
    , }7 k7 v4 x9 e6 t' a) y7 {
  7. 2
    * N2 ~/ a0 N) S
  8. >>> math.factorial(3)* w. L' E0 r1 g9 O8 X
  9. 6
    $ r& t) _# c# Y% j) q) R4 y) ^
  10. >>> math.factorial(5)- m+ b: ]& Q. i* f* Q# V
  11. 120
    + [+ u/ E$ n/ F0 W7 Q
  12. >>> math.factorial(10)# B: W- t6 m- P' Q
  13. 3628800
复制代码
! P. z1 y- x* N1 }
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数
' e- ]: g9 k2 S
  1. #得到x/y的余数,其值是一个浮点数/ h9 m* j6 a9 J1 G9 ?
  2. fmod(x, y), ]8 K. V; q! u
  3. Return fmod(x, y), according to platform C.  x % y may differ., {3 l, L6 n, O2 ]- T
  4. >>> math.fmod(20,3)
    + g$ t% V$ ]5 Y  {
  5. 2.0: h) \* _3 O- k( c) I
  6. >>> math.fmod(20,7)
    6 m+ U. e, d+ ^  ]! ?* _" t1 K
  7. 6.0
复制代码

8 A  s" }) f( B) g7 [math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
" o$ K! j8 ~6 V$ E/ e( j. k- f
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    2 k! K- I' s7 O; e5 a
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    ' \0 Q# O7 U2 Z
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    2 c4 W6 _: E" @2 X- N
  4. frexp(x)
    5 J7 Z% k$ W6 [$ @  U/ M: M$ @1 t
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 z# ]0 |+ V! s1 j
  6. m is a float and e is an int, such that x = m * 2.**e.
    & g1 Y) L  a8 W$ C2 k
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    7 n  _, O+ k! p+ b$ B
  8. >>> math.frexp(10)
    ! y( Z2 t8 j: c! p3 c- U
  9. (0.625, 4); U- P! v# I3 d! V* ~6 _4 y: _
  10. >>> math.frexp(75). G: J: v' s# ^# Z, C6 c( N/ [  B
  11. (0.5859375, 7). ?7 O- N: _! v2 R8 _' @3 \
  12. >>> math.frexp(-40)% P8 b# E9 q% K; \6 z  C  p3 i5 t3 \3 Q
  13. (-0.625, 6), C* b" `: ?1 ~
  14. >>> math.frexp(-100)
    ) \# |/ f# u" [
  15. (-0.78125, 7)
    9 S& F! u3 B2 {
  16. >>> math.frexp(100)
    ( `5 n2 ?+ Y$ U; z
  17. (0.78125, 7)
复制代码
( N  D. |. y+ i" `
math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
! ^/ S7 L& F/ s1 Q5 g+ ^/ m* Z2 }  E
  1. #对迭代器里的每个元素进行求和操作- i5 A$ w7 H% u4 O
  2. fsum(iterable)9 E; ?; \& p( \# W9 S
  3. Return an accurate floating point sum of values in the iterable.
    ! A5 e! j4 K7 P$ W. U' m$ O
  4. Assumes IEEE-754 floating point arithmetic.2 n, L, D+ Y  p& p* G. N  ~
  5. >>> math.fsum([1,2,3,4])) W' E, S  x2 \3 h& o& F% s
  6. 10.0' }6 K2 _  [% a+ a3 x5 \
  7. >>> math.fsum((1,2,3,4))
    9 v9 ?, x% z! p% d& @+ U; H/ u" {
  8. 10.0; r4 b6 Q" o. n  g$ q, I
  9. >>> math.fsum((-1,-2,-3,-4))
    7 s- v' h& C9 c( z; A$ ?
  10. -10.0
    2 s7 W, B& ~. @) H! z2 i7 I* q
  11. >>> math.fsum([-1,-2,-3,-4])' t1 y  ]' Q5 ^
  12. -10.0
复制代码

4 K. J# i# s, Q# Ymath.gcd(x,y)  返回x和y的最大公约数- j/ n9 r: q4 g
  1. #返回x和y的最大公约数
    ' o4 i' T2 O7 |) Y# i5 X
  2. gcd(x, y) -> int7 r- V: }( F# E/ B- v$ W) w3 k) S
  3. greatest common divisor of x and y
    5 \5 H0 R3 j) t
  4. >>> math.gcd(8,6)
    # h* [- B/ t9 d9 }/ X3 g
  5. 2" t6 ~  c2 l! X  n( e! q# @
  6. >>> math.gcd(40,20)
    9 l) I% {! T8 K7 R
  7. 20
    $ M$ L) p' E$ z! w3 F
  8. >>> math.gcd(8,12)2 u) T9 c2 t) t& c
  9. 4
复制代码

! ~9 Z5 X0 \7 ^% X. N' F2 W( i$ _+ omath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False% M1 i8 i+ C" Y4 S/ q( {
  1. #得到(x**2+y**2),平方的值
    2 V# i6 k: H# }  u' f
  2. hypot(x, y), a1 L6 e* f% H3 v3 @
  3. Return the Euclidean distance, sqrt(x*x + y*y).& R! Y1 L, c0 d( u2 J, m, R
  4. >>> math.hypot(3,4)8 l3 x4 t" m0 u) q0 Y5 l
  5. 5.0
    ' P0 E! E$ j6 f2 d
  6. >>> math.hypot(6,8)3 c) l1 N' K3 i- p+ d2 u6 d/ ^! y
  7. 10.0
复制代码

2 G: X% Z7 ~; T6 d( s7 J5 ]1 }% @math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False( B; {  c( ?+ {
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    # H; l! y6 Q! q1 G6 J+ d! Z
  2. isfinite(x) -> bool
      b+ [) i4 x% D. U$ @8 o
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.& }# Y" D! S  E& T- E5 e& W, p# }
  4. >>> math.isfinite(100)
    ' ^3 }5 M# P! m2 R7 y
  5. True7 {$ m6 ]! [- B: [1 [% Q: V
  6. >>> math.isfinite(0)% ^9 s" I4 U: N5 U
  7. True) M) E: f: c2 I( K  n+ q! ~  O5 h5 e
  8. >>> math.isfinite(0.1)& {8 k. s4 L  \2 s
  9. True
    0 h, w3 f$ G+ Z+ \( |& E# B
  10. >>> math.isfinite("a")# k0 y9 }" ~/ X# W: k" L! M: f
  11. >>> math.isfinite(0.0001)9 A6 f7 {, O( Z) U" ^
  12. True
复制代码

) L9 {- ^/ g8 X# Dmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False
" x* b6 G& ]+ [+ l2 E8 V
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    4 |" [0 c% y4 t% V
  2. isinf(x) -> bool& M. M& t! `/ g7 G& A
  3. Return True if x is a positive or negative infinity, and False otherwise.
    " D) M7 ^0 Q* E( a
  4. >>> math.isinf(234)
    # |4 e+ D9 F  ^3 \% j* L
  5. False/ {* X# H+ O& `. O- m5 S4 Z
  6. >>> math.isinf(0.1)+ m  ?1 ~% `9 b7 s
  7. False
复制代码
5 ], e$ {  `8 y2 y: b
math.isnan(x)  如果x不是数字True,否则返回False
4 Q8 f8 C7 R) o- }2 t2 y2 R* H3 t5 h
  1. #如果x不是数字True,否则返回False
    ) N' k( l- L) G
  2. isnan(x) -> bool9 B  K, n  u( |! e4 I6 U
  3. Return True if x is a NaN (not a number), and False otherwise.
    6 Y8 _; ^. M" T  X9 \( {/ f
  4. >>> math.isnan(23)5 M$ w8 ^% K8 A4 t
  5. False
    : Z5 L1 ]. n8 B3 N9 _5 l
  6. >>> math.isnan(0.01)
    6 o6 a& u% Y* [# P7 D
  7. False
复制代码

- S* `! L) J5 `* Kmath.ldexp(x,i)  返回x*(2**i)的值9 }) c0 S" z, F1 ], l
  1. #返回x*(2**i)的值0 F3 K, B. ~& a5 e
  2. ldexp(x, i)
    9 c7 w0 m* i  x9 Y) Z
  3. Return x * (2**i).
    , ]  s, t; _; P' }; [
  4. >>> math.ldexp(5,5)  `) f  A/ ]$ s
  5. 160.0
    - `5 X& f# n. H: F
  6. >>> math.ldexp(3,5)
    6 D1 U) ]5 o. s- T: E
  7. 96.0
复制代码

, I* j! Y  v% gmath.log10(x)  返回x的以10为底的对数1 w- ?, L* W7 ~# c, e2 ?/ P
  1. #返回x的以10为底的对数
    , l. V5 T( z$ l& ?$ [
  2. log10(x)
    0 p3 d- z' ~6 A1 |  C# p1 V$ y
  3. Return the base 10 logarithm of x.
    ; C& m2 M) [  ^# ?; k6 g- t1 @4 v; j
  4. >>> math.log10(10)
    # N# J% r: m1 R* Z9 ]
  5. 1.0
    ( X" Z3 d5 E" l3 K; J1 ^
  6. >>> math.log10(100)
    2 u. b4 j  P; \5 ^$ n
  7. 2.0
    - X- z* d5 g, F. F  n
  8. #即10的1.3次方的结果为20
    " x, B4 D) D- `" l8 B. b" B% q+ `
  9. >>> math.log10(20)
      \8 ^: T$ |' F" l; m
  10. 1.3010299956639813
复制代码

8 z9 P( [  q1 \4 B7 Q" K6 Lmath.log1p(x)  返回x+1的自然对数(基数为e)的值# _# t% S# v; g' d- N
  1. #返回x+1的自然对数(基数为e)的值
    1 b9 ]) t' R/ L7 R. y
  2. log1p(x)% V' W4 F9 X1 t8 U# p
  3. Return the natural logarithm of 1+x (base e).4 Z$ `8 s4 ?0 c; b, T; o
  4. The result is computed in a way which is accurate for x near zero.
    4 N" P9 G5 \- Z% R$ B) w
  5. >>> math.log(10)0 {/ J. x5 j% B3 v
  6. 2.302585092994046  \6 B- I/ O0 a; W+ w1 C' g
  7. >>> math.log1p(10)
    * F, i; f$ H) }5 D* S$ ]- F" u
  8. 2.3978952727983707
    ) Q9 Z; [5 [) I+ o
  9. >>> math.log(11)" d1 Z2 L" M9 F7 U
  10. 2.3978952727983707
复制代码

9 ?) u. j9 i$ l8 ?# K" xmath.log2(x)  返回x的基2对数
/ V1 `0 w8 Y1 U8 u' D6 X$ |& \; x' |
  1. #返回x的基2对数  t1 k& p" z: Q+ K) M7 z; n9 }. o
  2. log2(x)
    . o  C1 F/ \* W, c9 ?5 s4 {  |! G
  3. Return the base 2 logarithm of x.
    / g- k$ z$ Y9 s9 V; v8 f
  4. >>> math.log2(32)( f& F2 f9 V. K: P3 e1 D* S
  5. 5.0
    , k% V! J7 A: B: n/ d, t) P
  6. >>> math.log2(20)
    1 I3 o8 l7 i! v, O7 i
  7. 4.321928094887363' v7 \7 q+ N, q' U1 x% w( h
  8. >>> math.log2(16)6 E0 p$ N% a# w2 E- H1 O0 d7 P
  9. 4.0
复制代码
* N; g7 H. a! r9 A
math.modf(x)  返回由x的小数部分和整数部分组成的元组( I  \) q* H. `, ^+ C7 a
  1. #返回由x的小数部分和整数部分组成的元组
    : j. i' a4 C% Q. R. W
  2. modf(x)
    0 c- l9 r- q  F4 `/ a9 L' O
  3. Return the fractional and integer parts of x.  Both results carry the sign
    1 Q+ _6 M- L5 O1 b
  4. of x and are floats./ c% M, M, F! T. i
  5. >>> math.modf(math.pi)4 ^0 b, M5 [. E  Y0 c/ Z8 q
  6. (0.14159265358979312, 3.0)# x% K9 `. m: V/ i8 B# ^% m
  7. >>> math.modf(12.34)6 M1 k  p1 t# t$ i( ^
  8. (0.33999999999999986, 12.0)
复制代码
& q3 g7 _& a$ Z1 C
math.sqrt(x)  求x的平方根
4 c0 d. o& A) j6 V% w! M
  1. #求x的平方根( P$ l. z# g7 Y0 D9 e4 t
  2. sqrt(x)
    $ u& G4 a& X% t5 p6 e: _, R
  3. Return the square root of x.
    1 c4 O: |; ?6 @$ C# G7 l
  4. >>> math.sqrt(100)3 z8 E. M- H$ e+ }8 m
  5. 10.0
    / @: X( Q( I$ U. x
  6. >>> math.sqrt(16)4 Z/ K# B1 ]% p& y4 G! j
  7. 4.0
    ! s% k' `. P- L1 K; |
  8. >>> math.sqrt(20)+ K% K! _9 K; i6 l6 y9 n, P- `  a5 F
  9. 4.47213595499958
复制代码
1 t/ H) t  ^" _) ]7 `$ X
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    . c# Z/ z2 E+ \9 {+ C5 q9 i
  2. trunc(x:Real) -> Integral
    0 M5 _' Y# `* J
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.# K: R/ z4 J3 @
  4. >>> math.trunc(6.789)1 c8 k- ^* B% g5 N( [
  5. 62 h$ l9 G1 o+ M# @
  6. >>> math.trunc(math.pi)+ Z: b6 q2 }9 }1 \! ^( i; H
  7. 3; E" ]; f; L$ E8 r. G( \7 T% V' p* m) y
  8. >>> math.trunc(2.567)
    & j, T( `8 O1 A$ x# I
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-27 18:56 , Processed in 0.080772 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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