新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x

* N# J5 p8 d  z8 B' N/ L【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
5 i0 H( e0 _) ?

# o  E# x) ]  g5 ~! B方法18 F$ s( ?, @- i; f* H
  1. >>> import math
    / K4 V" o5 L# E
  2. >>> math.sqrt(9)
    3 G' z0 F: s% q
  3. 3.0
复制代码
方法2
9 @$ T" b: F% O0 C3 o% `# I* i- y
  1. >>> from math import sqrt
    . J* P7 T+ Y; U. L2 |
  2. >>> sqrt(9); F6 W6 @, n' B) R4 w
  3. 3.0
复制代码
- e6 V7 b' I9 o' t2 p% Q" ~. f

( ], V( u4 l7 f1 e3 X6 R% ~3 y
math.e  表示一个常量0 c4 ~$ t- @0 I3 p" e
  1. #表示一个常量
    ; ?8 N/ j. K* {4 f
  2. >>> math.e2 N# j. `6 z  D
  3. 2.718281828459045
复制代码
0 M2 J7 W4 n5 G& r$ W
math.pi  
数字常量,圆周率

2 r1 H2 U, R4 [/ N
  1. #数字常量,圆周率# A; |+ S- k1 l2 y+ T# H: a" r
  2. >>> print(math.pi)2 x8 J9 u! K7 Z6 e
  3. 3.141592653589793
复制代码
& }7 u3 |3 f$ T
math.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
# `3 I' k9 J/ }1 Q; K1 Z
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x, E! X! D+ \- H7 ^# w
  2. ceil(x), X. m  g/ `2 x) P) @& I6 t
  3. Return the ceiling of x as an int.
    : @. z6 K! F# H
  4. This is the smallest integral value >= x.2 s7 K. o0 I/ `$ w1 W# y, u- q( l

  5. 7 e4 V, {3 \2 ^' |
  6. >>> math.ceil(4.01)- n7 m8 M) P2 s. a7 r/ x6 g
  7. 5
    * Z4 I; _3 u6 I& l
  8. >>> math.ceil(4.99)
    ' o/ s: Z% N& u
  9. 51 q; S" u3 E" u6 R- E% l9 `
  10. >>> math.ceil(-3.99)$ J2 f' b* Y, z$ l! Q
  11. -3
    / e* ~1 Z4 B8 f4 x4 f
  12. >>> math.ceil(-3.01)4 v3 t* `% i$ ^- H( _
  13. -3
复制代码
- B. c$ D, O/ i0 w% T. n
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身/ ]9 g+ d/ e# Z6 v. k' p* `, R( O# @
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 V5 p; Q$ |0 A) u- U& \$ }9 x
  2. floor(x), [) N% |- t0 Y2 X
  3. Return the floor of x as an int.
    $ t7 b+ T" E5 Z# J: _
  4. This is the largest integral value <= x.! b/ |: \; J3 \* }+ x5 ]
  5. >>> math.floor(4.1)' s* r+ k' Y& B; }% M6 Q6 |
  6. 4+ E: f7 W3 L8 \( O$ w
  7. >>> math.floor(4.999)( r( x* t1 N  Z% b( v" J
  8. 46 B; q* M! U5 t1 q
  9. >>> math.floor(-4.999)6 F4 @& z( d# S& H3 ^4 v
  10. -5
    . x8 m3 Z( b/ Y' _0 W# I
  11. >>> math.floor(-4.01)+ j, O4 ^% g4 u1 l+ o
  12. -5
复制代码
3 i( v! T2 Q( H( k$ d- Z' ?
math.pow(x,y)  返回x的y次方,即x**y: O2 s7 r# m$ i6 K2 Q1 v! J
  1. #返回x的y次方,即x**y
    $ z6 {/ T! c$ T/ a& d) y$ w1 d1 R, [$ I
  2. pow(x, y)
      c- v3 V1 I% w1 R8 `
  3. Return x**y (x to the power of y).5 ~! O  r4 @- C. F$ K
  4. >>> math.pow(3,4)
    % i4 I! S6 Y( f+ S
  5. 81.0
    4 k% K( Q% u1 T( _/ T
  6. >>>
    % H' q- ~' @; o% n$ f) u# \7 n
  7. >>> math.pow(2,7)+ `. t* w  k# b8 B: G- G' [9 Q3 [
  8. 128.0
复制代码
  b6 g& i6 F1 v6 b) M
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
3 g8 o, w* l, D% r, u
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    $ I" K2 |9 F$ h& Q
  2. log(x[, base])5 i/ O3 I) j' }$ k
  3. Return the logarithm of x to the given base.
    * f# }& G/ l+ W' t# f# b% c, \) X0 Y6 N
  4. If the base not specified, returns the natural logarithm (base e) of x.$ V, x! O, [, @
  5. >>> math.log(10)- i5 X, @0 d( K& d
  6. 2.3025850929940460 s) I( A( d9 \' g. Q1 Q
  7. >>> math.log(11)
    3 x6 C+ e# S, m0 s- K
  8. 2.3978952727983707
    2 \, y1 Y; m% G
  9. >>> math.log(20)4 s: L2 l0 [4 A$ Y6 Z
  10. 2.995732273553991
复制代码
! u# s' u# ?0 M4 T$ b
math.sin(x)  求x(x为弧度)的正弦值2 |5 z8 ^9 W% C, w  v4 c+ U% r9 }
  1. #求x(x为弧度)的正弦值
    ! ]3 z1 V0 u% G' Z0 @
  2. sin(x)
    + t2 z/ x: O2 t4 I! f$ p4 ]* t
  3. Return the sine of x (measured in radians).
    / _* Q0 P5 p+ Z0 e; i. ~: s- x4 K
  4. >>> math.sin(math.pi/4)
    2 \; \9 s4 u1 U6 U
  5. 0.7071067811865475
    - E( u& Q5 b  L7 G
  6. >>> math.sin(math.pi/2)
    6 S+ a: r- m2 G+ h/ G( n1 o2 r7 h
  7. 1.01 u. T2 r( h# i  V1 c. f
  8. >>> math.sin(math.pi/3)  W2 S! k! U' G* {$ J" U; U" t
  9. 0.8660254037844386
复制代码

3 ^! J! ^! r, W- |) D5 Vmath.cos(x)  求x的余弦,x必须是弧度
9 B' o. e8 F7 s. K* K6 a1 q
  1. #求x的余弦,x必须是弧度
    - T2 @4 e! z5 _
  2. cos(x)
    ! ]0 p0 h# B+ v  B
  3. Return the cosine of x (measured in radians).  }3 C/ v0 O4 d1 K2 |! z
  4. #math.pi/4表示弧度,转换成角度为45度
    5 M) A' C* O* u7 N+ m6 B  c2 _
  5. >>> math.cos(math.pi/4)1 D# v8 D5 M9 M/ w5 o4 Y
  6. 0.70710678118654765 y- B( O$ D$ Q' D5 H& p+ X
  7. math.pi/3表示弧度,转换成角度为60度( ]2 h7 ~+ S/ @" M; b7 e, p
  8. >>> math.cos(math.pi/3)
    + q  R3 b: ^; I
  9. 0.50000000000000013 H& G1 O# s, R, w5 B+ ]0 e) B
  10. math.pi/6表示弧度,转换成角度为30度
    / l  a, ~: D$ v- _8 @
  11. >>> math.cos(math.pi/6)" Y: {% d3 D$ p$ N& c, u
  12. 0.8660254037844387
复制代码

! D2 D) q) {) {math.tan(x)  返回x(x为弧度)的正切值
! @& b5 \& T/ }4 w
  1. #返回x(x为弧度)的正切值
    , `/ t0 u! ]- A* f0 G& ~4 Q
  2. tan(x)1 ?8 C; _6 @% A& b
  3. Return the tangent of x (measured in radians).
      y: y5 }  q- b$ o( ^/ D6 m8 Z
  4. >>> math.tan(math.pi/4)% j% k. B1 T  d8 R+ P
  5. 0.9999999999999999  ^8 M6 k3 a6 V& c* ^3 u$ E5 L
  6. >>> math.tan(math.pi/6)
    + `4 Y2 I5 K8 K6 p$ `7 o# @
  7. 0.5773502691896257
    9 ]( p, U' Z6 Y1 @
  8. >>> math.tan(math.pi/3)0 }5 [7 D/ ]) W
  9. 1.7320508075688767
复制代码

( A7 n' u6 `0 B$ g8 K  ymath.degrees(x)  把x从弧度转换成角度8 x, g+ C, b# O2 n4 Z3 v% z' M0 R8 b
  1. #把x从弧度转换成角度
    ; t* q3 ~; Q0 Q) a9 J, z
  2. degrees(x)
    0 q$ F7 n/ ^- N5 j2 B
  3. Convert angle x from radians to degrees.. X: h0 C' b7 E8 o. b% Q7 Q$ |
  4. $ N$ g9 o0 Y4 h. s+ x: G
  5. >>> math.degrees(math.pi/4)' M  p. C; i/ ~3 P8 p
  6. 45.0
    6 q9 P1 X- `. @: o2 Q
  7. >>> math.degrees(math.pi)
    9 h  Y& k$ E6 Y# `1 ]0 u
  8. 180.0/ j; y4 v1 X/ t
  9. >>> math.degrees(math.pi/6)1 W! t8 b$ [5 r1 V
  10. 29.999999999999996, n/ U' `( z, j+ Q% m  N
  11. >>> math.degrees(math.pi/3)
    $ W, r8 P* |8 t, X0 V- l5 t, h
  12. 59.99999999999999
复制代码

$ d" D+ G5 m/ ]. h( Imath.radians(x)  把角度x转换成弧度
4 _8 a- p$ m; v/ H; L# C
  1. #把角度x转换成弧度
    8 ~  r' i+ T+ R0 |: A3 o% D2 L/ t
  2. radians(x)
    8 i# Q- X, S8 Z4 w# c% F
  3. Convert angle x from degrees to radians.2 n6 B/ k% [; q" U$ d6 b$ J: u# r
  4. >>> math.radians(45)4 \5 T5 g- g. A9 |$ v' [$ L
  5. 0.7853981633974483
    / M+ @* B+ K( t& r* G8 f
  6. >>> math.radians(60)
    4 ?4 n& ~/ B: s
  7. 1.0471975511965976
复制代码
* c0 M+ C3 h2 d1 w- P
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
4 R. A" R; M$ ^% w8 a0 F
  1. #把y的正负号加到x前面,可以使用0
    - e/ n6 z* J" o# @4 L' ^: G, F
  2. copysign(x, y)
    0 c  B3 T  @4 z4 p2 m3 S6 a
  3. Return a float with the magnitude (absolute value) of x but the sign
    9 V% a2 b% ]3 Y) Z8 E! y
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0)
    , q% e) O2 _+ s) H+ y
  5. returns -1.0.
    1 A4 Z' h' l6 k' S4 `
  6. 8 d; q3 v1 }( f' y9 s$ B& |
  7. >>> math.copysign(2,3)
    / e5 f$ A/ b8 H$ C" `
  8. 2.04 k3 x& V( e9 X% R; G
  9. >>> math.copysign(2,-3)2 m- U* `# v/ R" Q
  10. -2.0
    ; P: t, j) P7 U% K
  11. >>> math.copysign(3,8)- `3 W: c* j8 Z! a
  12. 3.0( i* `' d+ _" H3 T. }6 \& Q+ y
  13. >>> math.copysign(3,-8)
    ! f# o0 Q; Z5 Z7 E
  14. -3.0
复制代码

" f1 d0 ^7 Y, ~5 u8 w/ \: _1 g9 cmath.exp(x)  返回math.e,也就是2.71828的x次方  [% D4 k& p3 u
  1. #返回math.e,也就是2.71828的x次方
    6 Q4 n5 a+ W( x
  2. exp(x)' t/ e9 }. {! s
  3. Return e raised to the power of x.: R) }" x9 l* d

  4. ( e; Z1 \1 ?8 p- I" N7 g
  5. >>> math.exp(1)
    7 M6 \% M; q9 N1 \1 [
  6. 2.7182818284590450 m) F9 u, \2 H3 D4 G& ?9 A4 k
  7. >>> math.exp(2)6 n9 L+ H9 M  [2 B; y' Y  d) ]: S
  8. 7.38905609893065; H% S" ]' F! v  R
  9. >>> math.exp(3)
    ' e- O' g3 j+ f- ~2 A+ H% x, u
  10. 20.085536923187668
复制代码

# }' l* x5 w5 r9 i+ g0 J+ u# wmath.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1" t/ }. I8 u- @3 o) T6 D" R
  1. #返回math.e的x(其值为2.71828)次方的值减1
    2 t/ [: n! x7 O$ g- z5 s/ M
  2. expm1(x)
    % f; P5 f# {( N( M' i& M8 ?
  3. Return exp(x)-1.% q0 i! l  S: c. V/ {
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    * ]/ ]# g! w/ Q' Z& y( q

  5. 4 E3 k7 U( K, h& Z9 J
  6. >>> math.expm1(1)
    " P8 T* w0 H& |, r
  7. 1.718281828459045
    ' g- ~) s: b" t! @# V  v
  8. >>> math.expm1(2)
    4 E0 d8 f9 T) C/ w# p1 ?3 N+ U5 T
  9. 6.389056098930658 f6 j: W) I2 D( Q( d' {
  10. >>> math.expm1(3)8 z: F; Z2 T  l* m- N' ^* J' z! Z
  11. 19.085536923187668
复制代码
2 o! k8 r$ N$ W+ v$ B
math.fabs(x)  返回x的绝对值0 O6 A. q5 y( l# h. q
  1. #返回x的绝对值
    6 J7 r2 f8 u- [2 s0 m- a
  2. fabs(x)
    ) k  B$ W  l  i; l( }
  3. Return the absolute value of the float x.' \6 I1 L/ y( m0 O

  4. 4 w) `2 t; d: U" w4 U+ a
  5. >>> math.fabs(-0.003)
    0 y0 Y* v; d9 E. [& @6 B! {7 y
  6. 0.003
    7 `% u1 X0 @- l
  7. >>> math.fabs(-110)$ R( F1 k5 |. c& x3 x  X) R% C; J
  8. 110.0
    / X+ L8 W; ]  K  p, w
  9. >>> math.fabs(100)4 G8 u! G" e, B3 e* [0 |1 U: f. L
  10. 100.0
复制代码

( F6 e+ g' A. ]3 W6 M/ Fmath.factorial(x)  取x的阶乘的值% S, i  ?- I# k! o) Y
  1. #取x的阶乘的值1 S# z) l' A3 ^1 ?' m
  2. factorial(x) -> Integral
    8 |! T; \1 F: T8 V; N4 w, O
  3. Find x!. Raise a ValueError if x is negative or non-integral.! n- ~, N2 M7 @. T1 O4 f" {
  4. >>> math.factorial(1)9 H  a6 P  `  r" U, i4 m
  5. 1
    # ~% W% H: J  A
  6. >>> math.factorial(2)
    7 L0 f1 G/ Z; _8 x
  7. 2. O0 b1 N. b- c( I* T" s3 q# X
  8. >>> math.factorial(3)
    ( @' n( q. s" n6 A3 b/ I* I1 ~
  9. 6
    3 B( q+ `% |5 e  W: i
  10. >>> math.factorial(5)5 m0 C! ^* f" C! w9 Q$ _
  11. 1206 z7 P/ m# ]4 h6 {% i$ X3 c
  12. >>> math.factorial(10)( A) K. n7 D  k+ t
  13. 3628800
复制代码
2 r: u0 p: W( i8 _' o. h
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数9 t7 R# S' d; y2 ?
  1. #得到x/y的余数,其值是一个浮点数
    " ^8 l7 I2 y1 A# X: \! O
  2. fmod(x, y)
    - N: O! z# W. B3 ^, B6 z
  3. Return fmod(x, y), according to platform C.  x % y may differ." q# q1 `! r; E7 r5 T" ^5 a
  4. >>> math.fmod(20,3), ], k5 n8 z4 i! l! d0 h
  5. 2.0
    5 i7 R$ z. Z7 ?' K
  6. >>> math.fmod(20,7)$ h% i3 g0 m& v* K% b8 ~
  7. 6.0
复制代码

$ O- d6 u4 j+ I4 @$ z& A4 I  U9 Rmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
% D& d" N) w; o& \
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    8 T7 C3 I5 N  H4 G6 P
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
    + M8 L% ~8 T/ ]1 E* i6 p( r7 M. c
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和14 r$ I1 o$ m4 D( X
  4. frexp(x)
    8 L" B$ P7 S9 @9 d
  5. Return the mantissa and exponent of x, as pair (m, e).: J3 }* D& F# j* P0 u
  6. m is a float and e is an int, such that x = m * 2.**e.6 l! f4 g4 R" X$ P2 `' O
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.9 D* ]% Z. N& C
  8. >>> math.frexp(10)) _8 x! f2 G, _& y, g
  9. (0.625, 4), D6 H7 k. m! o  |
  10. >>> math.frexp(75)9 X; M( Q* K( A! c9 [* z1 s* [. v
  11. (0.5859375, 7)* B# x3 d8 E) T2 k$ a  m+ @
  12. >>> math.frexp(-40)" i  w5 [' ~" R3 I
  13. (-0.625, 6)
    : F3 }& h, `  V$ U3 a$ M
  14. >>> math.frexp(-100)3 K6 @& V6 M% F6 I9 `9 l
  15. (-0.78125, 7). W# h, z/ X  r% t/ e/ a! |6 c
  16. >>> math.frexp(100)
    / A2 _  q8 V/ X7 V: s  ~* c
  17. (0.78125, 7)
复制代码

  \, u- J+ t. @& F( ?9 w( {math.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列* j6 |/ L$ j4 a& f& E
  1. #对迭代器里的每个元素进行求和操作
    8 K5 C6 G1 J  O& l* S/ O4 W8 x
  2. fsum(iterable)5 i( g; G5 M5 q2 ?! T: M
  3. Return an accurate floating point sum of values in the iterable.
      }" w9 R# L* A! u
  4. Assumes IEEE-754 floating point arithmetic.
    $ d9 E" R+ n# _. s; Q; |" t
  5. >>> math.fsum([1,2,3,4])' ]# a0 S  Q# `: j9 q
  6. 10.0
    7 j- g( i* c6 n4 |5 m% |4 v
  7. >>> math.fsum((1,2,3,4))
    . w; O) h% h, G) K) ~
  8. 10.0
    0 q/ q" ?: l+ U( P
  9. >>> math.fsum((-1,-2,-3,-4))
    * a" k5 D* s% o& B: G2 h' ~9 Q
  10. -10.0
    6 i/ }% m) y8 @/ g( h7 F& o
  11. >>> math.fsum([-1,-2,-3,-4])- T& w) x- d9 h2 o) ^9 D; n, B1 Q
  12. -10.0
复制代码

: g4 R0 o6 G: _0 `, t2 H. L2 [math.gcd(x,y)  返回x和y的最大公约数
4 T# e+ b' Q9 w3 R; e
  1. #返回x和y的最大公约数
    $ B1 i2 D, ?: w+ s
  2. gcd(x, y) -> int
    $ E# `' U+ B' ~/ L  o4 r/ r1 _! m
  3. greatest common divisor of x and y
    . u5 _6 k3 h- e( w( {+ Q
  4. >>> math.gcd(8,6)1 g) m4 d% C- M" \$ d, G% A
  5. 2
    : p% c$ ?1 H3 V9 F# y
  6. >>> math.gcd(40,20)
    8 \/ T( q$ J8 w1 O  R* J7 P) ]
  7. 20* c3 f5 j: A' W4 X' U
  8. >>> math.gcd(8,12)
    $ }5 O, N  o: V$ n7 Q
  9. 4
复制代码
# C1 b; V( c/ i* Y- Z
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False9 u) P1 u3 K+ U; t. O! w2 P  n8 i7 p
  1. #得到(x**2+y**2),平方的值
    ; E2 A6 Q5 P% u' |! K7 b) G
  2. hypot(x, y): ?/ D4 m) m+ _0 @$ U. X$ G1 f( Y
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    * O3 {! w9 L4 T" b
  4. >>> math.hypot(3,4)
    9 i( w4 f' x) P; k. S: o
  5. 5.0
    $ x- q; ~8 B( g  z; H  E
  6. >>> math.hypot(6,8)
    8 j0 m1 ?2 X# e9 P! q, L# B. T& ]
  7. 10.0
复制代码
' T) {7 j; h7 Q+ r: ^% p% o
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False# c. x% R. \' M9 C) N2 r3 z0 ?& u
  1. #如果x是不是无穷大的数字,则返回True,否则返回False
    5 r8 W( g$ l6 G7 ]! h7 v9 R+ t
  2. isfinite(x) -> bool
    ( y: \& b* C% r# a$ @
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.  O6 h0 ?2 n% t$ {, i
  4. >>> math.isfinite(100)
    9 r1 c  @- i# |/ \4 R: G
  5. True
    - K  z& j6 r- s8 w$ v
  6. >>> math.isfinite(0)! K( z, h6 v. g
  7. True
    ; o/ Y: h- ~+ g
  8. >>> math.isfinite(0.1)
    : f- }: v# ^- }
  9. True; O9 x' Q* ~" Y; X0 R
  10. >>> math.isfinite("a")
    ( E; y3 ?# h9 \/ u# ~* W
  11. >>> math.isfinite(0.0001)$ `8 Z5 W) z* q; [5 g
  12. True
复制代码
( q1 D: `! _& Z3 v$ u& Q: G& F2 a
math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False6 d9 G* R; s9 e1 d# c3 O& a! [
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False+ d2 Y. X6 ]) C1 b+ s: b
  2. isinf(x) -> bool
    7 k& W5 B' \$ [4 a+ w) O/ g6 ^
  3. Return True if x is a positive or negative infinity, and False otherwise.
    7 e! b) s. P. u3 X+ [
  4. >>> math.isinf(234)
    , s& \$ g8 `/ A
  5. False
    $ V! c' l1 F) `8 T* c; q& Z
  6. >>> math.isinf(0.1)  l- A6 C9 z+ I+ A: P+ F  M, |4 ~
  7. False
复制代码

- A* \+ V9 u6 P4 R, fmath.isnan(x)  如果x不是数字True,否则返回False' b* a3 ^3 O/ k" |. T' B
  1. #如果x不是数字True,否则返回False
    8 I: m' m, k' V0 @0 s3 O9 R2 _' u
  2. isnan(x) -> bool
    6 O7 G: E  F/ r4 B( }& o
  3. Return True if x is a NaN (not a number), and False otherwise.: z1 F( @; {3 D. w/ l& P
  4. >>> math.isnan(23)
    # ?& P. X* G& e0 _% j! f* N
  5. False. e' o, w. Q1 g7 y, t
  6. >>> math.isnan(0.01)
    ' T) w9 y/ ~. E8 @  E
  7. False
复制代码

& f+ ]- W% ~8 U' umath.ldexp(x,i)  返回x*(2**i)的值6 ]& i) B4 S3 `5 J$ y
  1. #返回x*(2**i)的值
    ( X% w9 p- O8 J" `+ G
  2. ldexp(x, i)% u  Y- ~& e% s% R" L* F2 c
  3. Return x * (2**i).
    ' h) b3 ], }& k$ L% M
  4. >>> math.ldexp(5,5)8 K" m! C# `( M# O6 F
  5. 160.08 a0 u: r. M( o
  6. >>> math.ldexp(3,5)
    / ]- i1 J) P% E, E) Z5 Q" Z% o5 Y
  7. 96.0
复制代码
. _2 B( w+ C& _
math.log10(x)  返回x的以10为底的对数6 d5 r0 A  o0 j+ x/ Z: Q
  1. #返回x的以10为底的对数' B& k( j7 i0 o
  2. log10(x)
    9 A! P; u) m  b
  3. Return the base 10 logarithm of x.! [; C1 y8 J  J" L2 v+ c
  4. >>> math.log10(10)6 h% F) a+ r: [8 b
  5. 1.0) N6 S( [8 x. p, u5 z
  6. >>> math.log10(100)2 E6 n  W" R* b- j) v, |" w
  7. 2.0) _5 O6 v$ Q0 J& d
  8. #即10的1.3次方的结果为20
    1 S2 R' x# I3 m, F: o/ D
  9. >>> math.log10(20)
    ' l" P4 R' K/ E* @( R, h
  10. 1.3010299956639813
复制代码

% H2 s. v8 D/ \7 d8 X, qmath.log1p(x)  返回x+1的自然对数(基数为e)的值& Y, B( G, ^2 M! U) ]7 H) z: s
  1. #返回x+1的自然对数(基数为e)的值6 _; Y0 b# a" ~  i
  2. log1p(x)
    2 [( `9 [- f: ]1 }7 V
  3. Return the natural logarithm of 1+x (base e)., s$ C- y. H$ r
  4. The result is computed in a way which is accurate for x near zero.
    8 B" E- k7 m3 }5 [
  5. >>> math.log(10)
    : X/ _+ m/ K% D1 M8 ^0 ]
  6. 2.3025850929940468 X. v* T8 z0 q* E& [: m
  7. >>> math.log1p(10)5 d0 u  t+ m& X3 v# w
  8. 2.3978952727983707
    8 q7 ^# U2 }$ z, ]& _  n/ b
  9. >>> math.log(11)) h1 |  g! y, s# d" @! R
  10. 2.3978952727983707
复制代码
. K: z% \; Q, U/ F  D  }! F
math.log2(x)  返回x的基2对数
! b4 i& a7 f, L0 A' x
  1. #返回x的基2对数
    ( {/ s/ w! ]  [7 P( `
  2. log2(x)
    ( G' D" J, c, B- ?1 o: \
  3. Return the base 2 logarithm of x.+ Z, f% @4 ^, h
  4. >>> math.log2(32)) l. F# h/ {2 j2 X, }
  5. 5.07 t4 B( H3 w4 J3 ?' D2 ~# l- W
  6. >>> math.log2(20)
    1 r# m) C, h6 p7 G3 p6 }$ c
  7. 4.321928094887363
    : t& w+ K& _  t, G
  8. >>> math.log2(16)* i2 y* S1 \9 P/ _& \6 o$ D/ k7 w) o: r
  9. 4.0
复制代码

% m' c) W; V, W" f2 C* lmath.modf(x)  返回由x的小数部分和整数部分组成的元组
7 p$ s/ j- M$ m/ x0 c
  1. #返回由x的小数部分和整数部分组成的元组, |( C' U5 H4 n! z
  2. modf(x)5 |' n  ^; l4 N/ J0 i
  3. Return the fractional and integer parts of x.  Both results carry the sign* @* w0 I  m0 m% \- ~" G
  4. of x and are floats.) B( ?% D" H: Z, S7 D
  5. >>> math.modf(math.pi)+ f2 `; K4 ]- e& \
  6. (0.14159265358979312, 3.0)' D) ~! h8 W4 W; H7 P
  7. >>> math.modf(12.34)- Y( S( O: ]' c, M1 h$ s
  8. (0.33999999999999986, 12.0)
复制代码

6 N8 r/ _9 h3 ]3 S( W, Amath.sqrt(x)  求x的平方根
8 U$ J. B) A  a* {/ c+ _: `
  1. #求x的平方根+ ]6 y% C3 g; |  V
  2. sqrt(x)2 |0 R( ]/ r8 n
  3. Return the square root of x.
    + L9 w# [/ S. {% p
  4. >>> math.sqrt(100); r5 f( p6 t5 d5 A
  5. 10.0/ M$ M( G* j% J/ O0 H
  6. >>> math.sqrt(16); \2 `# E# I" P. I  r  K. {% R2 b
  7. 4.0
    / W% `) e2 c1 k& e
  8. >>> math.sqrt(20)7 I+ W5 r3 S; O9 c* B1 ]
  9. 4.47213595499958
复制代码

" {9 t# K6 `2 K5 u8 ]0 Umath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
    # f0 Y5 o# S! t/ s, h
  2. trunc(x:Real) -> Integral
    3 O4 ?; i9 c0 e4 O% k! g
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.( ?9 `) x, T. t; d1 a; y+ H4 a, t! U
  4. >>> math.trunc(6.789)- B/ y! u5 s% b1 b
  5. 63 p) k2 `4 r7 W5 m7 k( y
  6. >>> math.trunc(math.pi)! Y# ^- M8 b6 u0 F" [9 r# Q0 }' U
  7. 37 V) \6 J( V8 h& x0 y
  8. >>> math.trunc(2.567)
    . u2 R3 c7 e6 J( j, s
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-21 22:12 , Processed in 0.087898 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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