新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
5 H+ S& C! D+ _. T8 y
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。+ V' Q8 H1 h) ]- X. L; ~

5 D2 R# _* }. d方法1
7 B# H, H. K; _  C
  1. >>> import math3 l* ^6 ~' W+ p& H
  2. >>> math.sqrt(9)
      i, K* b/ h/ O" u7 |
  3. 3.0
复制代码
方法2
, w5 O. Y$ ?  N4 `7 k, ~
  1. >>> from math import sqrt5 k) F# D1 e7 l' A4 l+ g
  2. >>> sqrt(9)
    / Y  |/ z, ?* @) u
  3. 3.0
复制代码

5 _9 Z, w& K" g+ C1 e) o  d' X

) N3 [$ e% n. d+ t
math.e  表示一个常量
8 h% @/ n6 @9 i4 A
  1. #表示一个常量
    ' K4 g9 `, o, b) M; G* Z& q: c
  2. >>> math.e4 n" ?6 u  l2 T0 k9 J6 b: _
  3. 2.718281828459045
复制代码

+ ?  k9 s* |# O! i( Y2 N* Wmath.pi  
数字常量,圆周率

# ?! P& d7 V% }  d' B: g' t/ p5 O
  1. #数字常量,圆周率
    + z( @7 l$ q, `4 b/ k. G
  2. >>> print(math.pi)% c' `: q: I: O2 L0 F8 h
  3. 3.141592653589793
复制代码

3 k6 c8 S% A, v# rmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x

2 h( C7 K+ t" H$ Y
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    ) e& g: {* F) h) G
  2. ceil(x)
    4 S* m8 l9 o. F* P0 A; F
  3. Return the ceiling of x as an int.
    1 X: `2 E- w9 U
  4. This is the smallest integral value >= x.
    & B" @; q5 q6 G0 a

  5. . J- T4 H3 F, u5 z# n+ `+ r
  6. >>> math.ceil(4.01)- o) m6 T$ A# N4 V
  7. 5! W$ T# ]- }4 K$ \! s8 L! T
  8. >>> math.ceil(4.99)
    6 `' s  J' T$ y, O6 u* E3 X
  9. 5
    2 ?/ U9 M; w# q% F
  10. >>> math.ceil(-3.99), Y' L: O4 q' Z0 u/ `6 B" }
  11. -3" B- T% y4 I( P# _0 b
  12. >>> math.ceil(-3.01)
    $ ?& e( d, l, @& f
  13. -3
复制代码
/ v* A, ^8 g# m% D' b7 f  ?# `+ R
math.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. U/ [1 L1 m! }
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
    / y: o* q+ o% T6 E
  2. floor(x)* U- c* A1 L& x$ y
  3. Return the floor of x as an int.
    3 Y2 ]" E+ i. {: w; d% @
  4. This is the largest integral value <= x.
    5 w7 I4 r2 @0 C* ^5 D- E
  5. >>> math.floor(4.1)
    : b/ F( S$ k9 V% {
  6. 4$ S% t1 D7 p! k. o. z2 s
  7. >>> math.floor(4.999)
    * F' q' u' [  ~! I( X7 B/ x1 b
  8. 49 X8 P+ m8 `  d1 D. g& Z& W, v0 \/ g4 M+ l
  9. >>> math.floor(-4.999)
    / ]( k7 Q; C8 O
  10. -52 c1 Z2 F1 s$ f) h$ Y
  11. >>> math.floor(-4.01)+ O( ~, [3 a  Y- W* S1 j7 @
  12. -5
复制代码
' C% A' Q; t  h7 g* M5 I& K, x4 J
math.pow(x,y)  返回x的y次方,即x**y
8 V5 u+ [( f" i3 f6 c: r
  1. #返回x的y次方,即x**y
    , ~0 A2 C/ Y6 m. E! h1 j
  2. pow(x, y)+ _+ z! S, _: d1 j- P
  3. Return x**y (x to the power of y).7 L& w! i2 o7 c1 d$ O7 Z
  4. >>> math.pow(3,4)/ f6 t4 V: A+ d. Z' Q
  5. 81.08 g6 c0 W4 A& _0 j; d
  6. >>>
      q2 B7 ^8 D( c& O3 l
  7. >>> math.pow(2,7)
    6 m& ^: V, U+ P5 [
  8. 128.0
复制代码
' H- q) I% ~' R- r; A  \0 G
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
/ @# j9 {) G. i: N- e+ E
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)7 o7 z4 h3 {$ Z- h
  2. log(x[, base])
    , o/ W  j) E  O7 |! Y
  3. Return the logarithm of x to the given base.2 i) h, _3 k. F6 X; b
  4. If the base not specified, returns the natural logarithm (base e) of x.
    3 X* ~: a3 e) m7 \3 `
  5. >>> math.log(10)7 S& {3 K* I0 [7 v' {
  6. 2.302585092994046
    : {1 z$ s" k0 b0 X& ^
  7. >>> math.log(11)
    $ I# |8 w' m* T
  8. 2.39789527279837077 i( O6 z/ R2 `& w* z9 P  [' A# ~" `
  9. >>> math.log(20); q  W0 |+ M: p0 Q! b. N
  10. 2.995732273553991
复制代码
. h0 j" e5 G% A/ C8 @9 u" t4 I
math.sin(x)  求x(x为弧度)的正弦值
# ^) V# ^  V" v6 }
  1. #求x(x为弧度)的正弦值
    ! m& _# V2 l" l, G8 W% j
  2. sin(x)' q/ A- X! y; C# X& Z& E% ?
  3. Return the sine of x (measured in radians).- ^1 o6 t# @8 R  A
  4. >>> math.sin(math.pi/4)
    / Q# g* p7 \" U; a+ R" [% P
  5. 0.7071067811865475
    " K8 c: E" L0 t7 g) |( m& J
  6. >>> math.sin(math.pi/2)
    1 Y& Y' [/ @5 r
  7. 1.0
    , ^9 o. b) H1 D, U$ @% F+ g
  8. >>> math.sin(math.pi/3)
    5 H6 w* N& F: R# Q% ]; j
  9. 0.8660254037844386
复制代码

+ }6 k, D# H7 G  xmath.cos(x)  求x的余弦,x必须是弧度
, s4 ^' n% k% C2 ?$ z( H
  1. #求x的余弦,x必须是弧度
    5 H* J$ }/ N0 @8 s9 K$ b0 f
  2. cos(x)
    0 X/ |' {9 _- R' b
  3. Return the cosine of x (measured in radians).
    5 B" t* x) @  G8 G/ A6 B/ @* V
  4. #math.pi/4表示弧度,转换成角度为45度# q0 Y6 ~  o! y% I: [
  5. >>> math.cos(math.pi/4)5 u! H& Z; P) N& k, h9 i$ j! ~$ b- D
  6. 0.7071067811865476* y4 y) [+ F* U' }# i
  7. math.pi/3表示弧度,转换成角度为60度
    6 M. y5 h4 T1 V) H6 I/ v6 @! ?
  8. >>> math.cos(math.pi/3)
    6 z4 r; B: h1 r( ]
  9. 0.50000000000000010 |! }- e3 f+ f( q& _# q! J$ a
  10. math.pi/6表示弧度,转换成角度为30度# p+ l5 C3 s% N# T
  11. >>> math.cos(math.pi/6)# C. j9 T5 t; e
  12. 0.8660254037844387
复制代码
) a; i' l* c% l8 u8 s
math.tan(x)  返回x(x为弧度)的正切值; k0 C3 O6 B- S& d% A( h* i4 U% d
  1. #返回x(x为弧度)的正切值
    7 L. w# r7 A$ }4 k" q9 L. ~
  2. tan(x)
    * Z- v- x9 l: X4 ~
  3. Return the tangent of x (measured in radians).5 l( j1 y$ v# a/ g
  4. >>> math.tan(math.pi/4)% ]# @( P# {" ?( {% ]5 Q
  5. 0.9999999999999999
    5 b0 B1 o. Y9 r, r& n9 A
  6. >>> math.tan(math.pi/6)2 \! K3 j3 A9 P# m2 F! w! K
  7. 0.5773502691896257+ w' p) ]! U3 o
  8. >>> math.tan(math.pi/3)) z7 p8 t2 U  x% {6 }# R' p
  9. 1.7320508075688767
复制代码
+ x. @. T/ E, a
math.degrees(x)  把x从弧度转换成角度. }/ K3 x( [" y- A
  1. #把x从弧度转换成角度' P# K4 R; G. T" [4 p6 V
  2. degrees(x)
    + z' Y; j) U9 @% V' \6 ^* Z
  3. Convert angle x from radians to degrees.  J- e4 E1 ^. @/ \

  4. : {  D& u' m6 O- M  O
  5. >>> math.degrees(math.pi/4)
    2 _, F7 U2 u- e) Y% k5 M/ W
  6. 45.0
    / r) R- u8 U5 x( \. p
  7. >>> math.degrees(math.pi)/ P3 W2 @8 X' ^( r3 @
  8. 180.0$ Y. c  {5 z( j1 s8 i6 O0 r- T7 N
  9. >>> math.degrees(math.pi/6)
    " t) U) h6 z! M' N( m" p  [; {
  10. 29.999999999999996
    1 c, y7 M' x/ h6 F6 t  Y' s! T
  11. >>> math.degrees(math.pi/3)
    * ]& p; Y1 V. ^& L/ R
  12. 59.99999999999999
复制代码

  w- i* Z! k, Y7 l! Q1 Q3 Y. Tmath.radians(x)  把角度x转换成弧度
: [  p3 ^( R4 k! h1 m# A; S
  1. #把角度x转换成弧度9 |9 ~; V6 Z8 X6 ?
  2. radians(x)
    - A9 t: j7 n2 j& J% z
  3. Convert angle x from degrees to radians.) G5 a7 E( Z6 S
  4. >>> math.radians(45), p+ s6 Q2 X2 a
  5. 0.7853981633974483
    2 |( O8 J- M! W" w2 p( W
  6. >>> math.radians(60)
    ! g1 q7 o1 M8 }+ |0 \2 E" h
  7. 1.0471975511965976
复制代码
( l; k! F# C# u
math.copysign(x,y)  把y的正负号加到x前面,可以使用0( p9 ]/ U- k* k
  1. #把y的正负号加到x前面,可以使用04 \$ x4 f2 I/ J- V7 |
  2. copysign(x, y)
      B& Y' L$ P, f- }* }' j/ p- o' v
  3. Return a float with the magnitude (absolute value) of x but the sign ( Q1 Q+ p2 Y/ l& X6 p
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) % n# b( s) ?6 d- J. Z2 ~0 H* J
  5. returns -1.0.5 ]* C! p) M3 X9 {6 s& C
  6. 9 `1 w0 N& Y* _2 N, W& A- B
  7. >>> math.copysign(2,3)
    ' @- v( Z. D  C. ]+ O
  8. 2.0. I, x% a: V! Y; A' m, q5 N
  9. >>> math.copysign(2,-3)
      {6 C* @& \4 n0 T, g  S8 A
  10. -2.0
    5 b: ]. N) t& i
  11. >>> math.copysign(3,8)4 f  Z5 ~9 D5 b' J/ z- A7 L0 ]9 r4 u
  12. 3.0
    * ?( m" c0 R" {9 D" \
  13. >>> math.copysign(3,-8)
    - Y8 ?$ t$ c* B. F6 z% C
  14. -3.0
复制代码

3 a& X! W, O$ B6 |( A( wmath.exp(x)  返回math.e,也就是2.71828的x次方
4 R# W( t9 ~/ {0 h# g- A
  1. #返回math.e,也就是2.71828的x次方; V; c; N% C4 o' `/ m
  2. exp(x)9 c; F& T  J7 C: p2 _
  3. Return e raised to the power of x.
    + U9 h9 a# A6 i! v6 u# Y6 p

  4. * I0 M( C( ~' A# _, y2 _* r9 H
  5. >>> math.exp(1)' L6 v: U9 M( |* M- p# b( {. O
  6. 2.7182818284590452 ]8 R% U9 R3 ?: D
  7. >>> math.exp(2)2 R) j8 V9 B& ?; \) o
  8. 7.389056098930657 d* B# g4 X7 |- T" Y
  9. >>> math.exp(3)
    # [( f3 R3 k7 i9 d. I
  10. 20.085536923187668
复制代码

2 j+ V7 S! _1 {: G$ F$ j, }math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
% {: E: ]3 O) m' j4 w0 Z
  1. #返回math.e的x(其值为2.71828)次方的值减1* e# I3 o: O% p5 _  t
  2. expm1(x)6 K# f8 e' ?( T& a
  3. Return exp(x)-1.
    0 n- _7 [8 ]: ]7 i- n9 D
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
    , }( E( E/ L2 y5 x1 `
  5. 6 u3 `7 O, u! v: I# f. L
  6. >>> math.expm1(1)
    9 K' C  N. f, A! s7 k# D$ P
  7. 1.718281828459045
    0 V. q( p1 T  y8 J% ~+ C
  8. >>> math.expm1(2)7 R* r2 g7 ]  v5 x
  9. 6.38905609893065; B- B* {$ H2 _' p0 G1 J: N( q; [
  10. >>> math.expm1(3). W* [( ?7 A  j; V/ J8 `
  11. 19.085536923187668
复制代码

3 k  y; l; K- v. }8 D; B( x, ymath.fabs(x)  返回x的绝对值4 H- e9 o. }; Z" w' E% X# f
  1. #返回x的绝对值
    - f% _8 {5 n) i
  2. fabs(x)
    9 \, {" u, o, U/ e- _6 u+ H# U
  3. Return the absolute value of the float x.  Q1 H  K( j. n; w- r$ G' c
  4. # z7 I7 y8 v) \# ]
  5. >>> math.fabs(-0.003)
    $ a) V1 x  Z& q
  6. 0.003
    ! V! t& T- l& |+ X- e8 v) H
  7. >>> math.fabs(-110)
    ; e* c6 `( p; k; z" Q  V
  8. 110.0) P* O9 Q- e, p. W" ]
  9. >>> math.fabs(100)' I# b: I8 g1 f5 s2 o7 m& x1 `
  10. 100.0
复制代码

+ P% f+ B' g+ d- [math.factorial(x)  取x的阶乘的值+ q1 f5 V# v1 q9 ], Y9 n9 j
  1. #取x的阶乘的值
    4 v% G: B, E5 k' l8 o3 C: N  E+ Y; `
  2. factorial(x) -> Integral8 c6 Y8 Z4 B0 \3 j# [
  3. Find x!. Raise a ValueError if x is negative or non-integral.4 P" S% a6 u$ Y2 v
  4. >>> math.factorial(1)
    0 J& s) f) P9 r$ u( J4 _& i
  5. 1
    - ?6 b- o) i6 c+ S" h, @% N
  6. >>> math.factorial(2)* v+ O) s8 F  _/ T
  7. 2- G( {& H8 _, k) z0 [- @  s3 M  O
  8. >>> math.factorial(3)+ o& S6 Z4 y  M/ b, N1 _
  9. 6
    ( V0 _' X) w' `8 H5 n
  10. >>> math.factorial(5)
    ( n! V0 M- J6 T
  11. 1200 }/ E% B1 \) R4 j3 L: o. M1 e
  12. >>> math.factorial(10)1 n. j5 `; h3 X
  13. 3628800
复制代码
1 t: k, O5 Y/ |: r- }
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数8 y. E9 [& B9 h3 @( k- Y0 B
  1. #得到x/y的余数,其值是一个浮点数
    + V+ ]; i& t: u( g, }. d  o
  2. fmod(x, y)
    7 O( M7 t: L: E2 p
  3. Return fmod(x, y), according to platform C.  x % y may differ.9 |  j- v. u! T# _" i0 R
  4. >>> math.fmod(20,3)8 t& k5 i: Q% _: t7 W' ?
  5. 2.0
    # M* J; t; L, ~
  6. >>> math.fmod(20,7)# g2 C7 U" N; X" Q, T1 T5 ]0 j: }" ?$ V" U
  7. 6.0
复制代码
& @- }; o% U$ k7 T  p" k8 f5 Q
math.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围$ N' `5 F8 O, }0 o; U7 g  \
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
    5 H( e# V5 d: H, a# ?; {5 B
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值7 r9 J% u4 p, p- |) u3 k
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
    & M: \- z, p0 J  G8 H
  4. frexp(x)) ~+ l( E9 [+ `- a. v
  5. Return the mantissa and exponent of x, as pair (m, e).
    ( ?$ U% C9 w- p9 A9 [
  6. m is a float and e is an int, such that x = m * 2.**e.+ ?8 u  g3 q$ z& I( L! }
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.
    8 g2 Y/ X/ L8 S/ r8 l6 u* j  v3 N
  8. >>> math.frexp(10)
      Y8 h% F1 b$ G
  9. (0.625, 4)- r! @4 j4 s9 F" ]" I
  10. >>> math.frexp(75)$ G$ G' W7 Q5 k! u  f
  11. (0.5859375, 7)
    8 `8 D; d- ~$ z8 M6 ^2 I6 R
  12. >>> math.frexp(-40)
    ( I( U' U" h! Q/ K% X: a9 @7 x
  13. (-0.625, 6)/ R9 U5 z7 j; l* M5 a/ ?. K
  14. >>> math.frexp(-100)
    * [8 y% Z) ^% @3 k& c& ]" ~
  15. (-0.78125, 7)' ?* s. M6 u7 t$ x, x8 \: z, ~
  16. >>> math.frexp(100)5 V( [5 M# q7 @2 q" A: v
  17. (0.78125, 7)
复制代码

8 T+ k! i- F6 D; U5 |: J/ A& Nmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列5 k* u* ^- S3 m" d) ~9 c3 P5 ]0 J
  1. #对迭代器里的每个元素进行求和操作- o4 N. `- K+ {& h. b. O& _8 @
  2. fsum(iterable)6 u7 l; k4 E" t5 u5 k5 R- x" W& W9 ?
  3. Return an accurate floating point sum of values in the iterable.
    - n: B3 m9 k  s; |# A  }
  4. Assumes IEEE-754 floating point arithmetic.
    0 O! z' I$ x: o; W) a
  5. >>> math.fsum([1,2,3,4])
    1 q  \! m9 k8 u- r
  6. 10.03 `5 E! o8 o: K& a$ l, b
  7. >>> math.fsum((1,2,3,4))( E( t( n+ ?8 K& a) p+ j# W6 B
  8. 10.02 x/ w# Z  Y# x1 ^
  9. >>> math.fsum((-1,-2,-3,-4))4 ]: N2 [! @& [" F/ R! q. Z; e% Z
  10. -10.0
    3 k3 M5 h& t: w+ I$ N  D
  11. >>> math.fsum([-1,-2,-3,-4])
    6 e2 I4 q# {2 a# d9 Z
  12. -10.0
复制代码
# y# [# x3 V# J+ ]1 A% t/ |
math.gcd(x,y)  返回x和y的最大公约数
! q1 Q; t8 G& t7 O" M* h) n4 H
  1. #返回x和y的最大公约数
    6 i* a) c5 W8 f
  2. gcd(x, y) -> int2 J0 _: q2 {  E% u3 u' }1 H
  3. greatest common divisor of x and y# s7 o  Q( Y2 V9 A$ W0 }
  4. >>> math.gcd(8,6)
    - |/ l( x. S- }/ B, ?
  5. 27 c' U7 Q/ ^6 d2 z0 j+ w
  6. >>> math.gcd(40,20)1 w7 x0 }3 {* h2 r1 R! U+ K
  7. 20
    & G2 v! G1 ]- ^; ^: \
  8. >>> math.gcd(8,12); W) j% X* G5 N( x
  9. 4
复制代码
, b" A+ k" [! c3 v( s9 J
math.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False' a& a) B1 X7 \; P7 V2 m$ I
  1. #得到(x**2+y**2),平方的值
    / O$ B/ W6 U; }/ R/ v/ I
  2. hypot(x, y)
    ) v. w9 S+ z/ u, u" B  ^$ f
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    ! N$ U6 p% P/ V! N4 r
  4. >>> math.hypot(3,4)5 c1 G5 ~- b$ |
  5. 5.0, C: P+ z" ^9 p" }
  6. >>> math.hypot(6,8)7 f" M0 m- c" Q3 X$ t2 }+ x* s
  7. 10.0
复制代码
" t0 [7 e5 E  W+ \4 G8 J/ Y9 H
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False
0 u! R& o( J$ N" p
  1. #如果x是不是无穷大的数字,则返回True,否则返回False- H* y. c6 B7 R2 O8 b. Q
  2. isfinite(x) -> bool( i9 M% j% M) W, y* P
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    0 \8 l3 R/ R) r. K9 m2 D
  4. >>> math.isfinite(100)8 z, C' ]/ D7 A0 |- e4 o6 m! N/ B
  5. True! e) F4 H6 P, V
  6. >>> math.isfinite(0)
    / f8 Y9 ]# l* t  K$ B
  7. True
    , f! g/ C$ v7 T% J9 S. T, C1 j9 x
  8. >>> math.isfinite(0.1)
    , q$ `6 t- u% c3 @7 t" k! P9 p6 R
  9. True
    0 l! w8 A" Z7 I/ U5 }
  10. >>> math.isfinite("a")6 _! L, c" n0 y+ D
  11. >>> math.isfinite(0.0001)
    , `+ f/ o% s+ C; z6 F" j
  12. True
复制代码

4 \( M+ v5 f$ ]. ^math.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False) Y. g& w% l% }; K
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    4 h; T) Y5 N- S% ?! D$ c8 x
  2. isinf(x) -> bool
    6 ?2 d: ]* g- g1 i; O
  3. Return True if x is a positive or negative infinity, and False otherwise.- N5 z  {9 O# Z' o' L& x- U
  4. >>> math.isinf(234)
    2 T2 T! r% S# E2 }+ u; i
  5. False
    ! ?1 U9 E9 K' g
  6. >>> math.isinf(0.1)8 L8 {. X( o, F* J5 i8 @
  7. False
复制代码
. O  }( }) ], O0 m# j' ^
math.isnan(x)  如果x不是数字True,否则返回False8 W! j. q$ d! ?* s
  1. #如果x不是数字True,否则返回False
    6 x% l5 `" c3 p, v3 i( s; g
  2. isnan(x) -> bool
    & e+ h& i! g8 `& j! _5 g
  3. Return True if x is a NaN (not a number), and False otherwise.
    * Q6 e3 Z7 {0 B2 m/ c7 a
  4. >>> math.isnan(23)
    6 `# [+ C5 a0 p/ J( b
  5. False7 X  S& {8 `3 G! ]: u. Q
  6. >>> math.isnan(0.01)
    ( s  F% m% H5 I6 d  O
  7. False
复制代码
+ q5 e2 e3 `# n: x2 b! `( ?
math.ldexp(x,i)  返回x*(2**i)的值8 K' G! K  E+ O
  1. #返回x*(2**i)的值
    ! y6 _3 F5 p. _+ l& m
  2. ldexp(x, i)
    # R, n. c5 [' {. Z1 b
  3. Return x * (2**i).
    ( H9 i% E2 H) y( e% _
  4. >>> math.ldexp(5,5)" s% c$ l2 M7 W- x+ B
  5. 160.0) K  ^" J% M" z5 ~- B/ k- `, i
  6. >>> math.ldexp(3,5)6 \6 a7 i3 ?9 z. M
  7. 96.0
复制代码
0 M6 S0 V9 L0 E7 O5 C; h
math.log10(x)  返回x的以10为底的对数
7 _# y: n/ T: D3 C; y* l
  1. #返回x的以10为底的对数  Z8 r% E" Z# w# `1 \0 S4 L3 g* v& a
  2. log10(x)
    ) w. y, p0 z5 _6 l. W/ W# f
  3. Return the base 10 logarithm of x.
    + A; Q% x3 g& @5 `3 U2 h
  4. >>> math.log10(10), S$ j0 o' ?% Q* _! m
  5. 1.0
    1 q# Q: Z7 l* i1 i+ `. A
  6. >>> math.log10(100)
    # A  h" M, f: k* y
  7. 2.0
    6 O& i3 w8 d5 `& y
  8. #即10的1.3次方的结果为20
    + [5 N! ?$ H  H6 U
  9. >>> math.log10(20)# l  m2 _  ~1 c; [) M: k
  10. 1.3010299956639813
复制代码

$ c4 T9 w0 i/ |math.log1p(x)  返回x+1的自然对数(基数为e)的值6 Y( T" X$ H" A3 {( x) @
  1. #返回x+1的自然对数(基数为e)的值! [. |: E+ [+ ~
  2. log1p(x)
    + f5 c* p9 O  u, _) `, ^
  3. Return the natural logarithm of 1+x (base e).1 J& s6 d& Q* K- w. P* c7 B, |
  4. The result is computed in a way which is accurate for x near zero.
    / d4 ?& Y; K4 l% i$ D$ M
  5. >>> math.log(10)
    7 Z- u4 \( S5 k
  6. 2.302585092994046
    2 M8 x0 H5 ]5 z" t" G- `; O
  7. >>> math.log1p(10), _( s  v2 Y+ D7 G+ F
  8. 2.3978952727983707
    % u* l$ U/ b* ?+ A
  9. >>> math.log(11)
    7 y2 T0 k; @9 l5 `' e
  10. 2.3978952727983707
复制代码

' e! _, U: P$ e2 }math.log2(x)  返回x的基2对数
7 ]3 v1 z! N. T; T# b. _; U
  1. #返回x的基2对数
    $ O) R- W/ |- U) U) ?6 X
  2. log2(x)% o* z2 E4 m$ R8 H4 `7 D
  3. Return the base 2 logarithm of x.
    % `( ^% Q- X  a  _
  4. >>> math.log2(32)- B6 d4 H  k+ z3 Z- Q
  5. 5.0" r) l$ Q3 {. Q- i5 z: @3 j
  6. >>> math.log2(20)
    2 }$ ~3 ?, h  @2 Q0 Z% r# G
  7. 4.3219280948873639 Z( X/ ^8 S! S' j% A
  8. >>> math.log2(16)
    4 ]4 H) G8 P+ N* ?+ d/ B8 m( s6 b& H
  9. 4.0
复制代码

3 ]" k( e  \: F! Y3 A3 }; Hmath.modf(x)  返回由x的小数部分和整数部分组成的元组
' T) _7 x" _8 _3 u  \2 E0 F
  1. #返回由x的小数部分和整数部分组成的元组3 ]4 e  T* b  s
  2. modf(x)5 E7 U7 g, u: `7 L% Z  X
  3. Return the fractional and integer parts of x.  Both results carry the sign. |, R1 x- \, |7 p+ K
  4. of x and are floats.
    & ?* h4 R: ^# T2 [" D
  5. >>> math.modf(math.pi)
    : q3 G# j/ q0 u; X' A
  6. (0.14159265358979312, 3.0)
    : T/ O: S" u3 a" Q) R4 [6 M6 M0 y
  7. >>> math.modf(12.34)
    * ?; R( e' R6 k" h7 y. Z8 W
  8. (0.33999999999999986, 12.0)
复制代码
" g+ ?5 ]* h& O7 }7 g
math.sqrt(x)  求x的平方根  \0 j# v* e" ^; {  J
  1. #求x的平方根
    ( O9 o) E8 k3 X' A" _2 A
  2. sqrt(x)  I  w. e2 ?) j9 s6 i/ F3 R0 y
  3. Return the square root of x.
    * l: G6 D" U0 P
  4. >>> math.sqrt(100)
    ) @  o5 b, A5 O
  5. 10.0; ~9 k: _$ P$ y
  6. >>> math.sqrt(16)
    , f; J  N# c, T6 z: h+ j
  7. 4.0
    3 q8 Q; G: \# X; ]& C
  8. >>> math.sqrt(20)% E7 x8 u5 }2 ?: z% r
  9. 4.47213595499958
复制代码
2 j" p( w. h9 \$ Q+ j4 z
math.trunc(x)  返回x的整数部分
  1. #返回x的整数部分: Q$ z3 d3 x6 s" s
  2. trunc(x:Real) -> Integral
    " a; b! h$ |# Q) E1 a3 a- q
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.' T9 z  P- O! s& g
  4. >>> math.trunc(6.789)# `" b2 z/ {! s( U
  5. 60 p7 \/ g! K5 p7 O( }0 O4 A
  6. >>> math.trunc(math.pi)
      r  _& r4 d, q, \! \
  7. 31 P3 o" O/ y7 Q; b) f/ Q/ ~
  8. >>> math.trunc(2.567)
    6 [  F* d: L9 c: |
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

GMT+8, 2026-6-6 12:11 , Processed in 0.087120 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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