新大榭论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

新大榭论坛 门户 查看主题

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

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

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

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

x
' s9 d6 I1 e5 }9 o0 y2 \
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
2 n6 u& k, R1 R
9 S+ s7 B4 ?& }9 V5 _
方法1
- V* P: W+ ?& ]. `- ]3 }
  1. >>> import math4 q/ G% G" @( B
  2. >>> math.sqrt(9)
      r  _% _) n7 h4 U, g- z
  3. 3.0
复制代码
方法2! ]! S1 l5 w3 }6 Z, Y* O$ s9 g
  1. >>> from math import sqrt/ M% F+ X0 R9 V: M& A0 D
  2. >>> sqrt(9)
    ! C/ r. \; p8 L
  3. 3.0
复制代码
* M7 w; F0 x& t2 e

* ~1 v$ j( T  O8 M
math.e  表示一个常量9 U7 _: P2 b7 e: `4 S; v
  1. #表示一个常量. H- r) ~; s7 g9 z
  2. >>> math.e' I) r) W/ U& |2 Q' p1 k% y
  3. 2.718281828459045
复制代码

% z* G' q, o5 V4 W( a$ imath.pi  
数字常量,圆周率

1 |6 M* n; W# {
  1. #数字常量,圆周率
    3 ~  t& \# M; ~) u9 Z: q# L+ P1 t
  2. >>> print(math.pi)
    ; j% `* C6 H6 t$ K- ?( L
  3. 3.141592653589793
复制代码

1 Q, i$ p7 F: @2 e! rmath.ceil(x)  
取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 S7 x* c) u* J$ q9 v
  1. #取大于等于x的最小的整数值,如果x是一个整数,则返回x
    0 g" ^$ j4 E6 C: O' M, Y
  2. ceil(x)9 M, e# X1 B( c! r* [
  3. Return the ceiling of x as an int.8 _8 O/ p$ h& h  Q* Q: c2 h" j
  4. This is the smallest integral value >= x.
    9 U( P" `; r" T$ {) n
  5. 1 G) k* i& x+ K( ]/ f3 _: W" \4 e
  6. >>> math.ceil(4.01)
    7 r0 w3 p; J* @$ \* g4 ^! \. D
  7. 56 ^( W. e6 [2 B! @9 N
  8. >>> math.ceil(4.99)/ H& z5 L5 S, m0 Q, C
  9. 57 ^& e2 [( m/ l# n- Q
  10. >>> math.ceil(-3.99)/ P2 j9 [( ^: k
  11. -3
    8 V# o. R" R7 M- K4 j' S' \3 {
  12. >>> math.ceil(-3.01)
    6 U2 b9 R) e1 w  N: A% K
  13. -3
复制代码

/ A  V4 Z* y0 H$ a6 v6 Rmath.floor(x)  取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 w- q  U/ T8 n0 G1 N& j  }: T" n' W6 A
  1. #取小于等于x的最大的整数值,如果x是一个整数,则返回自身- L9 a# ]9 J7 e& s
  2. floor(x)
    7 c5 ~" O$ E, ~' r- }/ C% k
  3. Return the floor of x as an int.
      [8 _" [+ X/ @1 y- O
  4. This is the largest integral value <= x.
    % ^4 ~& {# l# u
  5. >>> math.floor(4.1)
    7 l7 M6 S5 L$ g
  6. 4
    # z0 t8 a$ @. @' ]* ^* {
  7. >>> math.floor(4.999)
    8 `. {+ f7 b! P* [1 l6 Y
  8. 40 @4 J6 O5 z7 I1 x9 k+ D
  9. >>> math.floor(-4.999); Y; Y& F0 }6 O. Z' L) B' G
  10. -51 ~3 f% A1 X+ z1 R% E
  11. >>> math.floor(-4.01)1 G6 q2 K6 r6 o( u9 E1 c' g) N9 I$ Z
  12. -5
复制代码

8 |# z( C, r, A1 _1 r& [math.pow(x,y)  返回x的y次方,即x**y% X; {  i; P  q, `" D
  1. #返回x的y次方,即x**y
    2 H4 O9 w: \* U# w9 g
  2. pow(x, y)
    ; R4 N9 _" Z8 y$ M5 v& N- p
  3. Return x**y (x to the power of y).
    + z$ a0 [8 \" v3 t( H
  4. >>> math.pow(3,4)4 M2 r% j- m0 N( H' b/ H7 o
  5. 81.08 M- h5 ~/ ]/ i0 m" z0 ]6 G$ R
  6. >>>
    . t8 m/ M5 h; g/ P9 x- d0 }( w
  7. >>> math.pow(2,7)" `5 @/ d4 J) l3 h& N/ J
  8. 128.0
复制代码
8 Q0 J& D6 s; i# T# x
math.log(x)  返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* A/ |- j3 ]1 i- g- N: `7 F! t
  1. #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
    6 l: U. o6 Q! M6 ~% U5 Q
  2. log(x[, base])! q) I# l8 z5 d
  3. Return the logarithm of x to the given base., l+ M# V" w  L8 F
  4. If the base not specified, returns the natural logarithm (base e) of x.
    " u: |9 ]# u3 D8 u& K2 Y+ I, h; f
  5. >>> math.log(10)
    ! B- T3 {6 [- @7 e1 P
  6. 2.302585092994046
    5 X( |) [) o8 _7 [- r( `$ Y) D
  7. >>> math.log(11)
    % L" U+ W% y' o
  8. 2.3978952727983707
    % L. t' i* n* P% E8 V3 c* Y
  9. >>> math.log(20)+ P  W/ B5 E' p# Y6 M, D  `
  10. 2.995732273553991
复制代码

8 X$ E2 H8 N% ]- h# e, R- Y0 Vmath.sin(x)  求x(x为弧度)的正弦值2 e$ A& E+ O+ k% `/ \, U! d
  1. #求x(x为弧度)的正弦值
    & i" S1 J( |6 f( p: M
  2. sin(x)7 |9 S, R  r1 {+ t3 S. v
  3. Return the sine of x (measured in radians).7 s: D0 B+ F- H( O7 L" [
  4. >>> math.sin(math.pi/4)
    - o) v8 Z3 a( u) s
  5. 0.70710678118654751 {, Y( Y  Z+ I  k. \9 R
  6. >>> math.sin(math.pi/2)
    7 R3 h0 C- ^4 B+ P# R- t
  7. 1.0
    ' ~7 C$ A3 b5 y  ^, i) C
  8. >>> math.sin(math.pi/3)
    " j$ E0 [3 q7 e+ Z3 z  |
  9. 0.8660254037844386
复制代码
& s3 T& a7 Q/ z7 G! M9 U6 L
math.cos(x)  求x的余弦,x必须是弧度
5 v/ U+ K' k7 s) n
  1. #求x的余弦,x必须是弧度2 v- w# U' w" A
  2. cos(x)$ z. X3 q5 t# Y6 `
  3. Return the cosine of x (measured in radians).
    * z6 g1 r7 }1 ?; q4 ~
  4. #math.pi/4表示弧度,转换成角度为45度0 j, D8 R8 x; j% l! w
  5. >>> math.cos(math.pi/4)
    * R) v( b: W8 \! I5 N
  6. 0.7071067811865476
    3 z/ T  m. ^  I( B  U+ m4 |
  7. math.pi/3表示弧度,转换成角度为60度5 z  R7 z( m$ J( J6 ?
  8. >>> math.cos(math.pi/3)6 m# N0 o* u- ]4 H
  9. 0.5000000000000001
    & g  Z; U+ N$ K  e- }
  10. math.pi/6表示弧度,转换成角度为30度
    ' ^" X. Q: f) j' k& R6 c
  11. >>> math.cos(math.pi/6): y1 J6 y  l; \) a. r+ [2 ~
  12. 0.8660254037844387
复制代码
" @! u+ `: A9 ], w
math.tan(x)  返回x(x为弧度)的正切值
+ K" q' J! }8 Q3 y
  1. #返回x(x为弧度)的正切值1 u$ w) H2 i8 y, j4 H6 ~
  2. tan(x)# v  k( Q& V7 M2 N8 ~7 f3 n! {
  3. Return the tangent of x (measured in radians).
    / u: v5 w( [0 f9 m1 o! M  u
  4. >>> math.tan(math.pi/4)
    . j5 |( P- E1 b' x- G( L- ~
  5. 0.9999999999999999
    $ N) l3 L( m! k5 {) v& h* v9 @+ v* X! Q
  6. >>> math.tan(math.pi/6)
    ) s. ]- E) w, f
  7. 0.57735026918962573 [5 r9 [- ^! T  O9 X  ]' ?5 I
  8. >>> math.tan(math.pi/3)0 ?, q0 ?  S& x0 S  Z
  9. 1.7320508075688767
复制代码
- d: E0 F* e" I/ g, m
math.degrees(x)  把x从弧度转换成角度- J+ _  F; S, j6 s$ e0 t' C+ z
  1. #把x从弧度转换成角度9 P0 u/ ?0 p3 ~3 d" G
  2. degrees(x): d* X9 D3 w2 ^1 J: x5 P' |
  3. Convert angle x from radians to degrees.
    ; E- Z7 K# ?: ?) G5 t4 A% v% `7 G; G

  4. % R% e' E! s# _$ N
  5. >>> math.degrees(math.pi/4)
    - z" q* s+ `3 k5 `' M0 q2 [
  6. 45.09 P2 |: N$ s+ r4 {
  7. >>> math.degrees(math.pi); M0 s/ ?+ V1 ?3 A! w
  8. 180.02 p& N7 F! L) Z6 s
  9. >>> math.degrees(math.pi/6)/ R& \5 x4 ^# \' T$ R0 a; I3 T
  10. 29.999999999999996
    3 w4 E" m# |' y
  11. >>> math.degrees(math.pi/3)
    ; `$ |" M! @( F* K$ d1 k
  12. 59.99999999999999
复制代码

9 F% ~; N+ O7 ]1 dmath.radians(x)  把角度x转换成弧度5 p! i( Z! L2 }' N0 g# F' y7 Q
  1. #把角度x转换成弧度
    " E( i+ Z; Q9 j" R/ C$ {
  2. radians(x)
    + Y3 d0 [& U+ |2 [# x4 |
  3. Convert angle x from degrees to radians.
    3 E! r' u( t/ Q9 }0 }& C
  4. >>> math.radians(45)
    + v* |' c* _7 Z$ p$ g- v  t
  5. 0.7853981633974483( ]5 P6 f+ k4 V; a& v
  6. >>> math.radians(60)
    + o5 G4 u! [- g# }
  7. 1.0471975511965976
复制代码
$ U/ |! D9 {$ D9 ?9 @. T( }" g
math.copysign(x,y)  把y的正负号加到x前面,可以使用0
+ k: m* Y# [5 C, f0 }) r- J8 T
  1. #把y的正负号加到x前面,可以使用0* q- I* I) t0 R* B4 c$ a4 j
  2. copysign(x, y)8 V( l4 Y7 E% Z  B* [3 s# _4 q
  3. Return a float with the magnitude (absolute value) of x but the sign
    " s0 ~8 s. ]* l; E0 |
  4. of y. On platforms that support signed zeros, copysign(1.0, -0.0) ) I$ F6 H0 Y# f. M" C6 g
  5. returns -1.0.9 X1 Z2 h& m- C, V0 y
  6. 0 z* @- b- Q, C$ G* [( q+ g
  7. >>> math.copysign(2,3)% V& j4 y7 X! N& O% o. g
  8. 2.0
    6 U: L7 n9 {$ x0 A9 `9 C! |$ q  Y
  9. >>> math.copysign(2,-3)
    & f+ t' D) _/ t: W5 q' L( O
  10. -2.0  F1 H! E6 N9 s- _& Z
  11. >>> math.copysign(3,8)- N" @$ O/ w- u1 }. m  K0 W% h1 C1 \
  12. 3.0
      v7 A, b2 A9 E3 f- l
  13. >>> math.copysign(3,-8), i1 s9 F0 `6 O! Z5 g
  14. -3.0
复制代码

( A& o0 N& I4 vmath.exp(x)  返回math.e,也就是2.71828的x次方7 V6 V' }. C/ i# o$ z; p+ g
  1. #返回math.e,也就是2.71828的x次方1 K$ ]- C4 ?' ]! s  o5 \$ F5 ]
  2. exp(x)
    9 L: i0 c( O2 b1 k- r& k
  3. Return e raised to the power of x.
    9 u) M, m& A9 [5 `, B: b/ F
  4. ' [, Q7 t+ K, I
  5. >>> math.exp(1)
    ) {& {/ P: b( Z+ k! T7 g9 y, i
  6. 2.718281828459045
    0 w4 E" [% I+ q, u0 g
  7. >>> math.exp(2)
    + u8 F: B; a. K" Q  t
  8. 7.38905609893065  J; n4 A4 a/ S: k: \$ z' Z3 W
  9. >>> math.exp(3)
    ( W' O$ ^8 t; s3 h0 n" C' Z0 w: O
  10. 20.085536923187668
复制代码
% x' Q6 }- D8 `+ f9 U, I7 H
math.expm1(x)  返回math.e的x(其值为2.71828)次方的值减1
0 A8 w  a' g! T' C; t
  1. #返回math.e的x(其值为2.71828)次方的值减13 O/ B- j; |1 Y
  2. expm1(x)
    # Z& g; I  _$ t4 @8 D- z
  3. Return exp(x)-1.
    ! ~; J$ |6 E$ i8 a9 g. M( ]( A
  4. This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.! m  z4 e* Z) F3 p3 S
  5. / {. X% g* g' R5 Y6 k& p- \7 @
  6. >>> math.expm1(1)0 L- d' s% g" I# d0 @  _
  7. 1.718281828459045
    : ?6 S2 {- q" k. w. S
  8. >>> math.expm1(2)
    " A! u9 S" ?* n/ c
  9. 6.389056098930652 r) j: Y6 k9 {
  10. >>> math.expm1(3)
      B0 L  |( g) [7 e+ [+ I
  11. 19.085536923187668
复制代码
8 O6 A. L8 p% Y' M- N) ]( W
math.fabs(x)  返回x的绝对值
& V' X+ l) C7 l; W7 ]
  1. #返回x的绝对值+ ^! X9 F' ]/ a' X6 G
  2. fabs(x)* d7 V+ V" m9 b2 |+ c" v9 A; n8 [$ }
  3. Return the absolute value of the float x.  u8 |( m+ k3 }: q

  4. : r$ D5 X. Y4 @0 s( M* W% o5 H
  5. >>> math.fabs(-0.003)1 \/ c$ D$ |  V4 H( s- B+ V9 X
  6. 0.003! K5 e& w/ b+ a8 r
  7. >>> math.fabs(-110)
    9 E/ u/ ?( ?% y( T4 r
  8. 110.08 K6 U9 W) V" f6 C. p
  9. >>> math.fabs(100)
    " p" B6 U6 W4 h/ n! [: u$ s
  10. 100.0
复制代码
( h: n( f9 m! J& e
math.factorial(x)  取x的阶乘的值6 a9 r0 n4 Q! t
  1. #取x的阶乘的值; f  H2 H4 _+ n+ C" G* [
  2. factorial(x) -> Integral
    5 o7 ^0 \5 U7 E" I
  3. Find x!. Raise a ValueError if x is negative or non-integral.
    0 d3 a% b9 q0 w3 l! t/ l  F  `
  4. >>> math.factorial(1)+ O& b( X( F% P
  5. 1" t% q; p2 H$ n+ l4 F
  6. >>> math.factorial(2)$ I8 P* ?% C, ^: W/ ]. s4 t/ F
  7. 2& S( z0 z, U8 v4 U/ ~  g% a6 |
  8. >>> math.factorial(3)
    $ \% F2 C- U% S9 I- w; w! h6 H
  9. 6
    7 w# |" q) d* ^
  10. >>> math.factorial(5)& j6 K* k, |. b% O. C; {- h/ e
  11. 1209 ~5 S( [5 k0 Q
  12. >>> math.factorial(10)$ E$ M1 `# w3 {# I0 i: g1 V. w& L
  13. 3628800
复制代码
! ?9 \6 C9 O+ T2 D( V- l
math.fmod(x,y)  得到x/y的余数,其值是一个浮点数  U. ?& C4 [. K" ~" Z+ U! B
  1. #得到x/y的余数,其值是一个浮点数  V: u7 N/ m* G/ T" u7 s' O8 d
  2. fmod(x, y)
    * q: h5 y! e8 r- n8 X! q* i
  3. Return fmod(x, y), according to platform C.  x % y may differ." h% o- ?" H/ t. N$ ]
  4. >>> math.fmod(20,3)
    ! `  r5 N+ V! [. A
  5. 2.0# Z1 f' e* L0 D; f; A
  6. >>> math.fmod(20,7)
    , \( e' R/ F/ b: p
  7. 6.0
复制代码

5 A( M8 S9 k+ A- Q- O8 r; dmath.frexp(x)  返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
6 ~7 G0 S8 Z% b5 U+ o2 C5 u1 E/ b
  1. #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,2 ?( [  ?6 O* j* z% a* y. I
  2. #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 [" \% U/ D3 X! K% D
  3. #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1* [/ K1 |1 Z- v' n* R$ ]1 o
  4. frexp(x)
    # p9 K2 l8 U# `$ }- k1 g4 J
  5. Return the mantissa and exponent of x, as pair (m, e).
    7 {1 E; i- B% l& T
  6. m is a float and e is an int, such that x = m * 2.**e.
    8 L$ r& o- J6 |2 V. u0 F
  7. If x is 0, m and e are both 0.  Else 0.5 <= abs(m) < 1.0.- T* H+ R# {% i0 j- x" A) L
  8. >>> math.frexp(10)
    9 P0 T1 ?4 v( Y2 `( b. H- |, r
  9. (0.625, 4): U" W  J8 ?; Y: F6 ~7 E
  10. >>> math.frexp(75)
    * L. o( q/ X$ F) ~" `# y
  11. (0.5859375, 7)* o) m4 O4 X' }% x
  12. >>> math.frexp(-40)
    ! M3 Z5 d' n- t* O' U5 w
  13. (-0.625, 6), k' ?, Y- s. j/ t
  14. >>> math.frexp(-100)
    # V9 ]1 G; }! S
  15. (-0.78125, 7). ?; [; H( E% g) k' A
  16. >>> math.frexp(100)
    . ~& T- d5 z5 u% {. x+ o( O6 T
  17. (0.78125, 7)
复制代码

; q: y* u2 J# W+ o& J& W; zmath.fsum(seq)  对迭代器里的每个元素进行求和操作:seq 代表 序列. w3 P  q# J7 }! [: W8 T9 X
  1. #对迭代器里的每个元素进行求和操作# T# a/ z- t+ S6 a  c' W& q
  2. fsum(iterable)* `) [+ I, U, R9 j/ F2 H+ Z5 \6 H/ E
  3. Return an accurate floating point sum of values in the iterable.
    ; u. J' Z. O8 q4 ~( q
  4. Assumes IEEE-754 floating point arithmetic.
    , \$ h. P* }5 V5 C. H
  5. >>> math.fsum([1,2,3,4])
    , \6 Q. \: u8 }$ S' D0 _( ^  r4 A
  6. 10.0
    . @( g$ J- \8 _: P$ e( ]  ~" b$ O- Y
  7. >>> math.fsum((1,2,3,4))
    , G- O% e; U+ u; ^+ M5 o' \
  8. 10.0/ A2 M9 D9 {5 d, U
  9. >>> math.fsum((-1,-2,-3,-4))
    2 n3 _3 @( Z( D* _3 ], U' z
  10. -10.00 Y/ K' H7 s9 w/ g) @+ B
  11. >>> math.fsum([-1,-2,-3,-4])
    ' D9 A* v5 S8 g3 [- B3 M
  12. -10.0
复制代码

/ G) ]4 B7 u- Z* ]- ?6 wmath.gcd(x,y)  返回x和y的最大公约数* W1 j3 x3 x) C. c6 o
  1. #返回x和y的最大公约数0 z" F1 `9 ?4 P
  2. gcd(x, y) -> int7 j/ Z/ a4 t6 h5 Y+ t# Q0 r
  3. greatest common divisor of x and y
    % q. l9 R6 w: I7 [2 }
  4. >>> math.gcd(8,6)
    1 i* Q2 P" X5 B- F' q4 i; C! A
  5. 2
    " a# C2 b6 e% U" L. V! S, ^* O0 u
  6. >>> math.gcd(40,20)
    ( w0 v( A  R5 @) ]
  7. 20# J* [) E1 F% U
  8. >>> math.gcd(8,12)
    : m6 e5 C6 ~( `7 A5 T2 E1 c
  9. 4
复制代码

% L' [' ?4 @$ b  @+ ~; Qmath.hypot(x,y)  如果x是不是无穷大的数字,则返回True,否则返回False5 a0 e: A: }) U' e$ F
  1. #得到(x**2+y**2),平方的值
    6 t8 a/ S; |) `( ]
  2. hypot(x, y)& m4 _4 r6 H# m  b1 t& w
  3. Return the Euclidean distance, sqrt(x*x + y*y).
    3 u" W1 v$ j( e. ?" K3 Z
  4. >>> math.hypot(3,4)
    * F% o+ y1 N5 t# M- r9 v( d) i+ b
  5. 5.0
    7 a$ a1 Q8 O9 t- ]  w
  6. >>> math.hypot(6,8)1 m! G- H# w7 R, o# V
  7. 10.0
复制代码
8 ^; R: y: ]# n9 N% ^* P
math.isfinite()  如果x是正无穷大或负无穷大,则返回True,否则返回False9 p7 y: U2 ?3 i9 y1 ]: b
  1. #如果x是不是无穷大的数字,则返回True,否则返回False% M4 u2 u8 R3 x
  2. isfinite(x) -> bool
    ( |+ J0 s" L0 z( g1 R9 k# q. F! k
  3. Return True if x is neither an infinity nor a NaN, and False otherwise.
    , f9 S% F) n4 o/ }2 m
  4. >>> math.isfinite(100)
    % ^9 b8 E1 E) @
  5. True
    2 v; D9 {% `& g
  6. >>> math.isfinite(0)
    : d1 A8 Y, F' o
  7. True
    9 @8 ~, W/ O0 |0 |8 T# u) ?' S6 ^
  8. >>> math.isfinite(0.1)
    1 h+ K% i* p% p7 {1 H! `
  9. True* ]3 r  c% r% C: ^. U
  10. >>> math.isfinite("a")7 v' a; @  c/ q( r
  11. >>> math.isfinite(0.0001)
    # V( X1 [$ G" R- N( F; W1 F, I6 t1 I. c
  12. True
复制代码

) I3 {, g# o5 u1 a3 j+ A' [0 r& Umath.isinf(x)  如果x是正无穷大或负无穷大,则返回True,否则返回False. M7 t: ]4 L) l2 L- c" ~& W
  1. #如果x是正无穷大或负无穷大,则返回True,否则返回False
    * Y" i! C. Q9 f6 n$ v% I
  2. isinf(x) -> bool
    4 Z/ i0 Q0 X/ r/ K
  3. Return True if x is a positive or negative infinity, and False otherwise.
    / G5 b: y3 e8 ]9 r9 X: t$ q
  4. >>> math.isinf(234)
    1 S7 @9 Z' V& \
  5. False
    / ]* |; ^0 N: Y% j
  6. >>> math.isinf(0.1)
    % o( M; L" ?( \. {/ }3 I7 x
  7. False
复制代码

2 P" m% R9 A/ d& ?math.isnan(x)  如果x不是数字True,否则返回False
* ]/ F% ]% g' E  s6 E1 V
  1. #如果x不是数字True,否则返回False, `/ L* N$ p+ e; w( [
  2. isnan(x) -> bool
    ) R3 c( ~8 x2 }/ [/ I4 n
  3. Return True if x is a NaN (not a number), and False otherwise.: `2 {, u5 s3 d$ q
  4. >>> math.isnan(23)
      x/ \5 ^3 |1 y& p# T% e
  5. False
    4 ]! \9 f2 \& R& |* \
  6. >>> math.isnan(0.01)
    0 D8 L$ H: p- q) @3 s
  7. False
复制代码
2 ]( H2 U$ [3 @/ l+ C7 z* Q
math.ldexp(x,i)  返回x*(2**i)的值
) `( ]% E  c( G! l: n* ~; z
  1. #返回x*(2**i)的值$ V2 g8 Q" j* V. ]- {2 d
  2. ldexp(x, i)4 Y! k8 K" d5 k3 z
  3. Return x * (2**i).
    ) p) X( `/ D/ b8 L/ P8 j: X2 o4 C
  4. >>> math.ldexp(5,5)3 Y/ n/ q6 s$ t2 E
  5. 160.0
    ! q- `- P. p6 ?
  6. >>> math.ldexp(3,5)9 |; h0 \  o; v0 Y+ F/ ?
  7. 96.0
复制代码

$ y6 U2 C: R/ m3 b3 q1 [! omath.log10(x)  返回x的以10为底的对数
! Z3 E8 n. c( `! Z" E
  1. #返回x的以10为底的对数
    + ?4 N' d4 K: `$ @# P
  2. log10(x)# _5 r; {& Y* a3 ?' m
  3. Return the base 10 logarithm of x.0 q$ k) x$ r4 z( Y3 K+ I4 t0 m
  4. >>> math.log10(10)
      L5 j$ V! ~+ t  L
  5. 1.0
    6 A/ k9 S& c! |3 u- q" g; v( F; K7 n
  6. >>> math.log10(100)+ W: S& I$ z9 U* W* Y- L9 C
  7. 2.0* `3 W8 d, ]; v( t8 Z. u
  8. #即10的1.3次方的结果为20
    , h  ~0 }9 E0 X; T8 A1 m
  9. >>> math.log10(20)( w3 _3 e# I  O" J/ ?0 \
  10. 1.3010299956639813
复制代码
; {$ }& G, R: \9 ~& a. ^
math.log1p(x)  返回x+1的自然对数(基数为e)的值
7 T" _( b5 t. O, Z. F0 b
  1. #返回x+1的自然对数(基数为e)的值
    0 @! r, Z7 j2 c" P" ]5 d
  2. log1p(x)0 y- l9 d& t" r! g4 i5 j
  3. Return the natural logarithm of 1+x (base e).
    # n3 ~: n" R% w  k1 ^! f% K- v
  4. The result is computed in a way which is accurate for x near zero.
    8 @. @, ^& m  Q3 D0 L
  5. >>> math.log(10)
    0 C( S8 ?% g/ ?0 T% _" @9 p- A
  6. 2.302585092994046
    ! ?1 f) y- h/ T! s/ M; M- b& o6 s
  7. >>> math.log1p(10)
    8 x3 V  d! P: f: m1 h
  8. 2.3978952727983707% w' T; c: [4 F! S4 s
  9. >>> math.log(11)$ c4 v+ }4 L, N/ s7 F- e
  10. 2.3978952727983707
复制代码

. L- X2 k4 Q) N5 a/ Ymath.log2(x)  返回x的基2对数
$ M0 ^7 l7 S* _& }, z0 I
  1. #返回x的基2对数# p( u. ]$ f* G/ ]1 D1 q: e
  2. log2(x)
    * g$ t: g9 W. J# z$ a$ O$ Z6 }
  3. Return the base 2 logarithm of x.: {1 p, Y4 ]0 D& G, Z' N+ T
  4. >>> math.log2(32)
      ~) b$ i; o! o1 R, g2 L0 Z
  5. 5.0
    & q$ q% ^- v. s( @4 u2 V3 X
  6. >>> math.log2(20)
    9 ^8 Q1 y- I! L5 _+ x
  7. 4.3219280948873639 I1 e% u/ a5 X8 L
  8. >>> math.log2(16)
    2 I' c( X% }9 o( I% g8 u, t+ y
  9. 4.0
复制代码

5 k- @* _/ N# ^, \# A) W9 N$ bmath.modf(x)  返回由x的小数部分和整数部分组成的元组
. m$ e0 d& [/ U  g" H# C
  1. #返回由x的小数部分和整数部分组成的元组
    # n- b; h8 U& _& o% n5 ^
  2. modf(x)
    & s" z" z3 d: d5 x7 }
  3. Return the fractional and integer parts of x.  Both results carry the sign$ ?6 O- ^: f# k7 x# M) b
  4. of x and are floats.
    8 D9 V. R  s! k4 B3 `
  5. >>> math.modf(math.pi)/ h7 W4 p) x) S) I
  6. (0.14159265358979312, 3.0)
    & b+ v4 k) H5 r, b4 |1 r3 a
  7. >>> math.modf(12.34)
    - I' j6 ~7 ]1 C* s4 _
  8. (0.33999999999999986, 12.0)
复制代码

! A3 n2 ~5 L* }4 dmath.sqrt(x)  求x的平方根0 `1 `' W, s. P% R: Z" o  k+ T+ M7 K
  1. #求x的平方根
      v# V1 B+ n* _! ~5 H
  2. sqrt(x)6 D- {7 F/ ^: a6 W
  3. Return the square root of x.% F4 r. g, [  c2 ~3 X
  4. >>> math.sqrt(100)! G9 u( e1 s6 }; \- B/ n5 }- i
  5. 10.0) U+ i. X# n! _7 I
  6. >>> math.sqrt(16)2 F; V3 I' \" f' E) J+ `
  7. 4.0
    7 U" P+ {, `' q. m1 `2 i
  8. >>> math.sqrt(20)5 r& g& R6 e7 q+ n' t0 \( }4 u& D
  9. 4.47213595499958
复制代码

8 @1 q6 [9 u. I2 `$ E( J% Bmath.trunc(x)  返回x的整数部分
  1. #返回x的整数部分
      l/ ^4 B4 u% q
  2. trunc(x:Real) -> Integral7 H/ f2 r+ W" j3 ~% F
  3. Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
    3 H2 C' R$ u8 s5 g, c
  4. >>> math.trunc(6.789)3 [3 t0 p( W/ F) y
  5. 6
    4 a6 B8 A# {/ n/ G9 P( u  E+ z
  6. >>> math.trunc(math.pi)+ Y' o6 D4 ^5 q
  7. 3( c: j! v& J: ]: r/ Z
  8. >>> math.trunc(2.567)6 W: H9 {% L/ q" m1 p( V
  9. 2
复制代码
:其中蓝色字体部分是高中需要掌握的基本语法

最新评论

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

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

GMT+8, 2025-11-23 23:46 , Processed in 0.080927 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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