新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
+ d) H% V5 c3 J
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。: [! M: H# S" c+ x

- i2 V' ?  e2 n# c; f方法1
8 [! S" B, J* @8 \/ I, p& f
  1. >>> import math
    0 E$ I/ u" @8 N- C
  2. >>> math.sqrt(9)
    - i3 b: i; B: `0 F! m5 T4 i
  3. 3.0
复制代码
方法2
' e+ K- N9 ]- v5 q$ ^3 S( r: F, p
  1. >>> from math import sqrt
    6 [8 y# o; F* v( H) R  E# w( Y# V& W9 n
  2. >>> sqrt(9)3 W0 I8 V+ j/ _) d9 {
  3. 3.0
复制代码
' i- ?/ \" Z, t) k" v! }9 `4 D


( w. v; S$ b2 Y2 K# G7 H* c
math.e  表示一个常量' ?, s! ~0 m5 ^! ~/ B5 ?
  1. #表示一个常量
    - S! s9 x) ~* o# U0 T4 A( J) r
  2. >>> math.e
    $ R0 R4 _" `; M
  3. 2.718281828459045
复制代码

! O/ u5 v% [7 E! F2 N7 k& tmath.pi  
数字常量,圆周率
3 P3 o; Y0 A1 N$ O; x0 ?, B
  1. #数字常量,圆周率
    , E$ Y3 r! F6 M3 _
  2. >>> print(math.pi)% M+ o* w8 L4 t* h
  3. 3.141592653589793
复制代码
  \! g! ^- F0 U5 _9 B
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

# U5 P: @5 }. S9 V
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    * q! G7 ^& A" z9 y8 j3 z
  2. ceil(x)
    + Z" V. s# }8 i  t" @* g
  3. Return the ceiling of x as an int.
    : @3 \/ \5 u# X
  4. This is the smallest integral value >= x.! K# w3 I2 L( w& V( O$ W6 [: D
  5. ; y. ?" s8 N8 T
  6. >>> math.ceil(4.01)2 C4 M3 Q9 G  G2 h+ ]
  7. 5, v' ?# N+ }5 J0 N! f
  8. >>> math.ceil(4.99)
    : r* C- D( V5 w! X
  9. 52 t$ g- X; F# G8 m8 F
  10. >>> math.ceil(-3.99)5 d6 N5 M% }# s* [
  11. -32 g& K5 v& b4 Y/ m% \
  12. >>> math.ceil(-3.01)6 A1 L" N5 t) s
  13. -3
复制代码

( ?* L! ~0 ]+ m' ?1 q9 p" o# ?math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
: q+ q9 ^2 }4 b2 f3 g: ^
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    , P" i% Q& z2 v. y4 K, B+ S8 D
  2. floor(x)3 S5 _# |) ?$ T. e2 v& j% u
  3. Return the floor of x as an int.5 h/ m% o5 K$ f$ f. F" k
  4. This is the largest integral value <= x.
    * A) k$ h! u* Q
  5. >>> math.floor(4.1)
    6 n- O# |; F: c) }. X2 X1 @
  6. 4+ I+ \5 s3 z2 r0 G8 B/ S8 B; M
  7. >>> math.floor(4.999)
    ) Z- y* Y" C+ ~) \
  8. 4
    ( E7 _9 G$ r' C1 s
  9. >>> math.floor(-4.999)1 P4 ~2 a; d3 x8 _) ~$ {1 y# G
  10. -5
    * i  M8 ~& S6 }, d3 v5 j
  11. >>> math.floor(-4.01)
    6 d6 j' T* t' s5 ~1 s; l  H4 J
  12. -5
复制代码
. D9 ?  F( \7 j2 C2 C
math.pow(x,y)  返回x的y次方,即x**y4 S5 X) i6 ^/ }1 \
  1. #返回x的y次方,即x**y
    - V: ?; Q! i  s8 S/ h
  2. pow(x, y)9 x, f6 w5 f- T, F% X, u, L
  3. Return x**y (x to the power of y).
    9 M; ~7 o/ ~  L. c0 j0 T0 |7 o
  4. >>> math.pow(3,4)9 r' f0 y; V' r
  5. 81.07 f/ X' @# d' o$ K" u, e
  6. >>> 6 k0 f; k$ Z' M- ^4 E
  7. >>> math.pow(2,7)2 ^$ g8 v) V" J% {
  8. 128.0
复制代码
/ e  h, a  f# G
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
9 e/ a3 |2 X, @1 P
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    & E2 X) O. _1 y1 C$ ]$ ?
  2. log(x[, base])
    . K* ?! N: t. F0 Y; e9 S; N
  3. Return the logarithm of x to the given base.
    6 j2 I  v. U! }2 O
  4. If the base not specified, returns the natural logarithm (base e) of x.
    , E0 |. p5 x9 ?% @) S( m
  5. >>> math.log(10)
    0 j9 g4 y! r2 C
  6. 2.302585092994046
    ( ^! o+ z) J% F
  7. >>> math.log(11)+ ^; X6 a4 i, B; K7 A$ s) l8 I
  8. 2.39789527279837077 X1 a1 R/ k# b2 x4 T4 d
  9. >>> math.log(20)7 y1 c, r' M/ l  J2 i3 R6 Q
  10. 2.995732273553991
复制代码
% U* j& U4 R4 N9 T6 r( g
math.sin(x)  求x(x为弧度)的正弦值
% T3 b- B& U  Y# T: ?  y
  1. #求x(x为弧度)的正弦值/ d2 ~: ]% ^/ v5 w# ?
  2. sin(x)
    8 E+ B# `, ~7 W
  3. Return the sine of x (measured in radians).5 H' {  `3 a/ t
  4. >>> math.sin(math.pi/4)
    4 i' w" I3 S$ O9 {) v
  5. 0.7071067811865475; c6 ]9 d& `8 I1 H( v
  6. >>> math.sin(math.pi/2)5 S' M5 s5 ]% k( Y9 b
  7. 1.0, t- q- j7 ^5 [3 S2 v
  8. >>> math.sin(math.pi/3)
    # O* t. F* O3 p4 [" b/ ?8 k
  9. 0.8660254037844386
复制代码

- w4 R! L# s/ o4 ^8 Z! O7 e  lmath.cos(x)  求x的余弦,x必须是弧度7 ^0 r' [# t# G# T% B4 |
  1. #求x的余弦,x必须是弧度
    ' u7 p' ?" P$ X: X
  2. cos(x)
    8 k3 J3 L6 r$ o0 W
  3. Return the cosine of x (measured in radians).* p1 z  v( ?, ]+ s9 W2 K% ~
  4. #math.pi/4表示弧度,转换成角度为45度( ]& h  u" n) K; s
  5. >>> math.cos(math.pi/4)% ]  J* K' M+ q7 i" s1 W. H
  6. 0.70710678118654760 P: ^7 {7 H2 Y
  7. math.pi/3表示弧度,转换成角度为60度
    ' L4 C4 @3 e4 y' n7 ~3 `
  8. >>> math.cos(math.pi/3)
    ( d/ H& k7 Z( z- ]! G$ u7 o+ M% N0 r
  9. 0.5000000000000001
    . M# \( C9 F2 X
  10. math.pi/6表示弧度,转换成角度为30度9 b. f7 n, w5 D( k( s& X3 ~: u% u3 W
  11. >>> math.cos(math.pi/6)8 J7 H0 ]" ?) p
  12. 0.8660254037844387
复制代码
8 o3 e' f, n+ j# o
math.tan(x)  返回x(x为弧度)的正切值
: J2 Q0 O2 F3 ]1 }* E
  1. #返回x(x为弧度)的正切值/ {$ e% L) o; r& c* e
  2. tan(x); H1 ~0 n6 v$ c; W2 a3 h
  3. Return the tangent of x (measured in radians).
    5 u/ f6 Z7 H# A2 `
  4. >>> math.tan(math.pi/4)
    # `* a0 K& m3 F+ y! e
  5. 0.9999999999999999. Z; w( ^% p' k5 p( b& K7 s
  6. >>> math.tan(math.pi/6)9 j% M6 x! d0 z( M
  7. 0.5773502691896257. K$ p+ B$ H# Q1 D0 }8 G+ v: k
  8. >>> math.tan(math.pi/3)
    - j# ^1 B/ `4 s; o! e: c' v- z2 \
  9. 1.7320508075688767
复制代码

: [% l; m2 d' `/ W  Imath.degrees(x)  把x从弧度转换成角度
6 f" v! R' }7 m6 ]5 C  ]( \. b
  1. #把x从弧度转换成角度
    ' O/ a# Y! R( C) X* m
  2. degrees(x)
    2 i" X. h; H; P1 f
  3. Convert angle x from radians to degrees.
    * ?0 R: t0 ^* c

  4. 1 ~5 ~8 d" w, l4 _/ {& v
  5. >>> math.degrees(math.pi/4)( O0 o6 r. }7 R& a
  6. 45.00 I3 b% \( s  D! q- x
  7. >>> math.degrees(math.pi)# x. D/ D6 ]1 y5 R2 T0 i/ k, g0 o3 s
  8. 180.0
    / h6 r' k/ T* K: ?" _9 b
  9. >>> math.degrees(math.pi/6)
    / g9 C1 F, w$ C% S9 A9 j
  10. 29.9999999999999964 [+ C- k" Q% H) ]0 k
  11. >>> math.degrees(math.pi/3)
    9 _! _$ I6 `' x
  12. 59.99999999999999
复制代码

3 Y$ r/ Z  Q. ^8 q$ Z6 Imath.radians(x)  把角度x转换成弧度$ w3 {2 c) p; |) k
  1. #把角度x转换成弧度4 m  {+ D; e6 e9 c
  2. radians(x)/ x* B  R; h- q# C
  3. Convert angle x from degrees to radians.& h* O8 |1 F+ @; V9 V) W
  4. >>> math.radians(45)
    - `+ ^( _6 ?5 o
  5. 0.7853981633974483
    6 l# l& ]% h3 r: l) y
  6. >>> math.radians(60)
    : N8 D* y3 _* s' @' x- {/ P7 M
  7. 1.0471975511965976
复制代码
4 A1 _7 [3 F, o$ X
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
3 m7 _$ s( n$ o9 w( _( d) R8 H
  1. #把y的正负号加到x前面,可以使用0
    ) `( R& ^/ q' ]; N9 U; [+ g
  2. copysign(x, y)
    ; i4 f  ~0 O3 A' _1 o6 W0 d
  3. Return a float with the magnitude (absolute value) of x but the sign 9 }. [4 U, k1 v# f; J  g, \' S9 d, {
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) 4 U& v4 I2 E  k+ T* e- p' _" ?* u
  5. returns -1.0.
    : B9 `' z. g. u+ m+ V- Z3 [5 \
  6. 7 Y5 L( R$ i! e7 H4 f- l' y% U
  7. >>> math.copysign(2,3)
    * [, X( K; Q$ J
  8. 2.0
    " }8 b& ?, X; a" ?
  9. >>> math.copysign(2,-3)# \( y( [! G% Q( N
  10. -2.04 I/ b( h9 h; R8 F# v1 J' Y( f1 M/ S
  11. >>> math.copysign(3,8)
    ! Y5 ?. V5 [8 }( E; v# i; {
  12. 3.0- e0 B# ]+ q) _7 P; e- Y: {' d
  13. >>> math.copysign(3,-8)
    . K+ {' y$ W. g& |$ K$ s: q8 X
  14. -3.0
复制代码
# s6 n& J0 ^" n) M5 L" ?$ ?3 i% k! Z
math.exp(x)  返回math.e,也就是2.71828的x次方4 A: B' g% O4 o7 O
  1. #返回math.e,也就是2.71828的x次方
    9 m; d: L0 }/ B/ d7 \3 f
  2. exp(x)9 E: K8 f1 ^* b: |$ U: B1 p
  3. Return e raised to the power of x.- I7 E0 E" V) p0 ]3 C

  4. + b) ]- Y' p! A- ~2 A
  5. >>> math.exp(1), b/ A" Z: @0 r* p0 ?' y3 {. u( w
  6. 2.718281828459045' l: t) e8 m6 ^
  7. >>> math.exp(2)
    - v' u/ q0 p1 ^5 `
  8. 7.38905609893065" D! a! K+ q1 B4 g1 k& b
  9. >>> math.exp(3)
    4 W6 K% p2 y# A# ^5 u% b
  10. 20.085536923187668
复制代码

* T$ f6 Q; z8 x$ N) H8 gmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
1 Z, O8 N' n+ h
  1. #返回math.e的x(其值为2.71828)次方的值减1
    ; h$ n0 K4 F: [. b
  2. expm1(x), P  o0 A  ?: e; D
  3. Return exp(x)-1.
    5 k1 x! C' ~# Y5 o- V
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.! Y; f: U( Q, |, r5 E
  5. , w7 b* e# o* @9 @8 R$ {3 F; x2 `) b
  6. >>> math.expm1(1); a" K# I. q! H( B" X
  7. 1.718281828459045) q' i; [; F) z+ o9 k( O
  8. >>> math.expm1(2)
    8 A) i; U9 q8 U3 s7 J6 \% ^
  9. 6.38905609893065+ v* ~3 E* g! x' e. d
  10. >>> math.expm1(3)5 H7 B9 B! n0 x, i0 c
  11. 19.085536923187668
复制代码

; [; q% p: P) k" w& Dmath.fabs(x)  返回x的绝对值1 U) @; h: A. k1 |& j
  1. #返回x的绝对值
    $ g6 c4 b3 \- f; X7 k
  2. fabs(x)5 H/ q6 V0 d. V3 i8 U/ S( u
  3. Return the absolute value of the float x.0 C8 L3 n' ?0 N, r( N

  4. 9 ~, n& C8 @1 Y. ^  J7 q; \
  5. >>> math.fabs(-0.003)
    , H# a( P! M& n; x" P) Y8 s# _
  6. 0.003
    - k8 m3 A- K' Y; H5 F$ p8 b; l
  7. >>> math.fabs(-110)
    ( ^5 G3 Z2 d8 V$ U
  8. 110.0; B6 o: P5 i; |: c/ k! q2 R9 ]
  9. >>> math.fabs(100); B8 V6 x- H; f4 N7 L9 ~
  10. 100.0
复制代码

6 u/ `4 T8 U/ J) i; fmath.factorial(x)  取x的阶乘的值5 r( t. L& h+ ]% V( u
  1. #取x的阶乘的值, p: f- C4 [5 o4 o; j1 K/ |5 t
  2. factorial(x) -> Integral3 O2 e! e* }3 T& _0 j9 x! B, `: ~
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    " C5 W+ a. w7 \0 y; B7 r( D2 x
  4. >>> math.factorial(1)$ o2 q- T+ I2 S# T0 p2 l
  5. 1" Z. C7 s! w$ ?9 T6 B5 G
  6. >>> math.factorial(2)! f; |! k0 J! t/ Q9 _4 h
  7. 25 X8 z5 i0 _( k: L6 h/ l. d
  8. >>> math.factorial(3)% ^9 k- |0 u# A& k  }3 @; J
  9. 6
    , ]- v; p6 C2 R  o4 t  A3 T
  10. >>> math.factorial(5)3 [* E( a) b( p8 s$ ]# b
  11. 120
    ' t) A* b7 Y& [5 Q6 G
  12. >>> math.factorial(10)% S% W; P1 z& _" t% j7 Z
  13. 3628800
复制代码

/ r  c# t5 K- H% U& s+ ymath.fmod(x,y)  得到x/y的余数,其值是一个浮点数
( \9 v! K  Q, Q0 i! i6 X9 Q
  1. #得到x/y的余数,其值是一个浮点数! ]/ Z/ j* a$ r4 y. h8 u2 S9 `
  2. fmod(x, y)
    & G3 Q( g5 s8 C0 P5 A' b
  3. Return fmod(x, y), according to platform C.  x % y may differ.
    + V  V3 @' ^1 w1 E
  4. >>> math.fmod(20,3)
      E/ z1 j. d; f$ E) P) T0 v
  5. 2.0
    ! l( ?( v4 @% V8 Z9 c1 X
  6. >>> math.fmod(20,7); ?* f' {+ m; ?, D' a& F
  7. 6.0
复制代码
( _! O# v0 o6 S1 n+ |. U; e
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
9 r* l% W% X& s) h: O! d4 }% Y+ @; ^
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    * f" ]3 e- r2 L% l, M, G
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值* i1 l( _% b1 L
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    - ?, |1 \0 u- v% k4 K
  4. frexp(x)
    / V% u" O: d1 |# v, ?  W
  5. Return the mantissa and exponent of x, as pair (m, e).
    2 k8 b  Z+ j8 P6 _, o
  6. m is a float and e is an int, such that x = m * 2.**e.  b" v( F, \1 U4 C0 I% [1 K2 V5 j
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.: L: @. f1 S9 o$ x" E# j, g
  8. >>> math.frexp(10)% V. |6 ]. A' z$ ]" ]
  9. (0.625, 4)( X: H* e% S" J6 l
  10. >>> math.frexp(75)) O7 s* u* y* N2 D* y
  11. (0.5859375, 7)/ L/ k1 d# R. r. }8 q$ u
  12. >>> math.frexp(-40)
    ) `8 }# z! p- R4 U. q$ D  w
  13. (-0.625, 6)5 y" l- v- N! c# I$ N# `' m- q
  14. >>> math.frexp(-100)* S0 C9 {& t' D. r: x: _% J! U
  15. (-0.78125, 7)( G1 O* d5 Q- [
  16. >>> math.frexp(100)
    # u  w- N- c, d+ z2 [) f
  17. (0.78125, 7)
复制代码

; G( i5 x% e. D& ]; T* h( b. r* umath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列
3 \& x, b0 B4 v9 i! D# i
  1. #对迭代器里的每个元素进行求和操作8 c+ I6 t8 N$ a: X# E( P3 t- T
  2. fsum(iterable)8 ^: O. T  {2 z* K) [! f
  3. Return an accurate floating point sum of values in the iterable.
    ' L7 U) F  a6 ?6 K$ u
  4. Assumes IEEE-754 floating point arithmetic.6 q" S! ^4 E# u! J  _  n% ?$ c
  5. >>> math.fsum([1,2,3,4])+ L- K: t% I/ W3 K' m
  6. 10.0
    / e0 c2 K& l$ Q4 W) x
  7. >>> math.fsum((1,2,3,4))
    : |# w& O, y0 l2 y7 ~
  8. 10.0
    7 @4 Z/ y0 k3 s: l* {: q
  9. >>> math.fsum((-1,-2,-3,-4))6 Z; v- n  c% D9 l& n: b
  10. -10.0/ L) a0 p+ ]. Q5 _3 q5 T
  11. >>> math.fsum([-1,-2,-3,-4])& |- K1 {" h' ~3 B2 }3 ]
  12. -10.0
复制代码
, r$ }' N% t; W5 `- B1 J  p! {
math.gcd(x,y)  返回x和y的最大公约数
1 X6 w. R' y' v- T3 T3 z% S
  1. #返回x和y的最大公约数! w# U9 G7 |: p# S4 ?! S! j, B
  2. gcd(x, y) -> int" [2 ^5 X; \9 j" ~( t
  3. greatest common divisor of x and y  C. ?4 y2 j* Q' n4 i2 |
  4. >>> math.gcd(8,6)
    " n7 q9 Z! o% l9 F
  5. 2& @) ^2 ^0 ]1 s0 k5 G$ l7 q; Y) J
  6. >>> math.gcd(40,20)
    ' E% v9 z! f% I
  7. 203 S! n9 ^3 N) i: X4 u  E
  8. >>> math.gcd(8,12)
    : m. J* }0 b, `4 p' x/ g
  9. 4
复制代码

( q& p( s$ d! j+ H. n% gmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False
, g& n1 Z% u$ Z& z) b- _
  1. #得到(x**2+y**2),平方的值5 q3 S  ^# N+ |6 t& y# }
  2. hypot(x, y)# i$ `! D  v  r
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ! H, O5 W: D/ |1 w# F7 A: \
  4. >>> math.hypot(3,4)
    6 J# u& Y" d: |6 E6 y- S4 N
  5. 5.0
    + M$ s/ [9 D  D! O' Z
  6. >>> math.hypot(6,8); K, W6 }- f+ E7 `! d
  7. 10.0
复制代码

7 V$ _$ J! r- `; Omath.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False! D( |* F' U; V
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    9 V1 `9 t8 S2 |: W$ M/ ]1 n- E
  2. isfinite(x) -> bool- Q: K3 C5 ?6 d7 r; X# y6 [0 M
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.4 d8 S5 W/ a$ m5 w. T" Q! L
  4. >>> math.isfinite(100)* H( a3 D2 i3 ]! ^! |' q
  5. True
    6 n2 t+ G) ~, ^' z. G- F2 d/ i' f
  6. >>> math.isfinite(0)
    ( K' |4 r7 [3 H2 L7 W$ l
  7. True5 I' |- Q" g: d: @% v3 H
  8. >>> math.isfinite(0.1)/ n* t6 @/ p: L* c. y
  9. True" t& Z% [. l' m+ q5 ]$ o: s
  10. >>> math.isfinite("a")
      U1 k  [4 U1 Y" a* {" `' z/ R' S) }
  11. >>> math.isfinite(0.0001)( V/ `+ N9 o2 R* e; U1 c
  12. True
复制代码

* ^# v* u1 G+ Y, vmath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False8 ]3 }$ g) h" k9 m' q# I' f
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False8 I# f2 b9 a8 v- g9 c
  2. isinf(x) -> bool
    ; y' }  Z4 N! U, h" e# `- T1 m
  3. Return True if x is a positive or negative infinity, and False otherwise.
    7 u" y. E1 [6 ^- z+ L% I) a! ^: e* U0 j4 i
  4. >>> math.isinf(234)9 L4 c- X# x" {0 ^7 D
  5. False% M6 J$ O/ \9 y0 z) Y6 w: X; ?/ O6 s
  6. >>> math.isinf(0.1)9 I8 l+ I& {3 H
  7. False
复制代码
7 _( w8 Y1 Q1 h
math.isnan(x)  如果x不是数字True,否则返回False
% e$ Z0 N7 D: ~; y3 ?
  1. #如果x不是数字True,否则返回False
    1 G/ |; \9 o+ l( M4 N! w, ]5 A
  2. isnan(x) -> bool
    . Y% j& C1 ~/ s- i; i
  3. Return True if x is a NaN (not a number), and False otherwise.& V/ s. h/ z1 {$ H+ u! g1 a
  4. >>> math.isnan(23)3 r: H4 }, n* ^. f) O6 E' a
  5. False
    1 q  w3 R$ k! E. j$ Y) X! _
  6. >>> math.isnan(0.01)
    ' f# S7 H& S+ Y% @0 _: d( \/ @
  7. False
复制代码
4 T  U* |; y, {# F: `
math.ldexp(x,i)  返回x*(2**i)的值
! {- w$ b0 }' d7 H* j$ G3 B
  1. #返回x*(2**i)的值
    & X. R8 u8 T8 `) ~
  2. ldexp(x, i)) @: }$ w( b6 h: j  F  S
  3. Return x * (2**i).
    0 j# {8 J8 `3 o$ D0 o7 W, n5 ~; p
  4. >>> math.ldexp(5,5)
    $ F. T7 i' s( w& l+ f: g3 j
  5. 160.03 D2 B) u% m6 l4 A/ _* b; |; w2 j
  6. >>> math.ldexp(3,5)+ I' B4 C' R' w* W
  7. 96.0
复制代码
, t- c/ }9 M3 [2 \: L1 g
math.log10(x)  返回x的以10为底的对数! t7 E! E$ B' }7 X
  1. #返回x的以10为底的对数/ Z" X4 T8 @% w/ {+ i3 F
  2. log10(x)
    1 M  g' R$ |  P5 C
  3. Return the base 10 logarithm of x.3 }! r! d  O+ M" r7 X4 i0 X
  4. >>> math.log10(10). o0 |5 \) |$ B) g. F9 F
  5. 1.0
    3 p9 L: U" c0 j# U) @2 j8 [& h
  6. >>> math.log10(100)" g6 u: q4 f5 ^
  7. 2.03 H$ q  {% e6 R. O
  8. #即10的1.3次方的结果为20  w3 L4 y, y& k1 g
  9. >>> math.log10(20)  t; k6 y/ z4 I; j- U& N% ]
  10. 1.3010299956639813
复制代码
: v. H$ [9 R1 B5 O
math.log1p(x)  返回x+1的自然对数(基数为e)的值& s" @% Y+ u  A# |. N  _
  1. #返回x+1的自然对数(基数为e)的值
    3 ^) d. V, ?9 q3 ?. U1 G
  2. log1p(x)4 `. x3 N/ Q7 v, _
  3. Return the natural logarithm of 1+x (base e).. L! S2 v, t7 L4 z
  4. The result is computed in a way which is accurate for x near zero.
    . r4 s6 M) Z- K! ]; C; Z
  5. >>> math.log(10)# j! h5 J- [3 V& f- R
  6. 2.302585092994046
    ' r+ ^/ ?- }9 d9 l
  7. >>> math.log1p(10)/ h! w+ Q+ \. u( ?. E
  8. 2.3978952727983707& @; p- f$ l  G4 v7 q, W$ g1 w
  9. >>> math.log(11)+ M, ]: J" `1 Z7 j. o. j) n9 ~
  10. 2.3978952727983707
复制代码
3 {, K5 s5 E/ y: j8 Q
math.log2(x)  返回x的基2对数
/ H/ p8 L; {6 W  a
  1. #返回x的基2对数+ b- {# _: b. l8 @
  2. log2(x)- h  ?; P4 V" V! k! c8 P
  3. Return the base 2 logarithm of x.
    4 H$ C( P+ ]2 r1 |. J4 E# d
  4. >>> math.log2(32)
    * t) P% x- H, e2 C3 P5 \
  5. 5.0
    , P3 q$ x2 G6 r) g0 ^/ y0 }% o
  6. >>> math.log2(20)
    4 H8 u0 p/ F0 ]$ t5 y" K
  7. 4.321928094887363. w, z+ k) C, S8 {# _; E
  8. >>> math.log2(16)
    4 E/ P1 F6 ?) s8 T! l+ I
  9. 4.0
复制代码
. B% I5 g2 A% C: ~+ C5 `% B5 Q) o
math.modf(x)  返回由x的小数部分和整数部分组成的元组! Q# C0 X# l3 p
  1. #返回由x的小数部分和整数部分组成的元组# Q5 r7 T1 m, j+ W+ \' d
  2. modf(x)
    8 `2 Z# o6 Q6 ?0 b1 Z
  3. Return the fractional and integer parts of x.  Both results carry the sign& j1 w9 V, ?0 G" ^! F' d
  4. of x and are floats.7 H% Z6 ?+ k( H8 I+ w& @
  5. >>> math.modf(math.pi); b6 ^& b: \+ K9 c/ G# [) C
  6. (0.14159265358979312, 3.0)( w; {5 t- ]: \: N9 `( E5 T2 _5 l* t
  7. >>> math.modf(12.34)" e- K. E8 S* C, m% z9 J1 J- S
  8. (0.33999999999999986, 12.0)
复制代码

/ ?5 y5 t4 e' B* S2 ?math.sqrt(x)  求x的平方根7 Q$ }1 i5 q; V0 V& i% G' E* _1 C
  1. #求x的平方根4 P) d; ~7 a' B, ?0 J% F
  2. sqrt(x)4 @/ u- U" Z- _
  3. Return the square root of x.
    7 s' x9 S! b' F3 B( U" Q0 L
  4. >>> math.sqrt(100)5 H( o  [: f+ K
  5. 10.0# B* l, Y( x3 x- `' i6 n
  6. >>> math.sqrt(16)
    7 C/ u2 X% i# l8 |6 r
  7. 4.0& ?% c8 O3 v3 z9 `; V5 @, p
  8. >>> math.sqrt(20)
    0 I0 p1 ~: N; q; R+ q! `2 z- K& i
  9. 4.47213595499958
复制代码

, e  C2 d' {9 K$ B- O  M  J4 ^math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分; W; i* q* H" [: H
  2. trunc(x:Real) -> Integral9 u" d* Y* t9 l9 n( b" [
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( u& }7 j% V( `
  4. >>> math.trunc(6.789); a% k4 V$ V$ o5 P. K  E
  5. 6' i: d6 r4 x/ @
  6. >>> math.trunc(math.pi)
    6 z0 p. P" F5 K& E  a
  7. 35 `% ?# P" K2 K( O) [4 C
  8. >>> math.trunc(2.567)
    ! [; D$ r. e8 V
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-2-23 23:34 , Processed in 0.076868 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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