马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" [) i @ K, g8 |/ [8 Q8 i' W$ k6 v; h【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
" ^+ S% f, y. c8 }6 ]: h( s# p. `# p" ?1 z
方法1: J c( ~: p$ V' [
- >>> import math
8 j" P* h/ N% v# b3 r1 g9 p - >>> math.sqrt(9)
2 @; a; N( ~7 V) C+ o - 3.0
复制代码 方法2:5 g) m# H( [6 a8 U' h& Y
- >>> from math import sqrt
6 X7 ~6 S0 X: Z! \. p - >>> sqrt(9)" @0 K5 D) _; g1 B" \( S* J+ W% P7 B
- 3.0
复制代码 5 l+ |4 {4 l8 z8 o
; g' y5 G6 E N" J0 _* a9 cmath.e 表示一个常量3 q; K! w& i1 {3 `9 p% s6 p3 G0 } B
- #表示一个常量2 Z2 e- J# m/ }$ P. o5 J+ x
- >>> math.e7 ^* Y) T, N. r. I7 J
- 2.718281828459045
复制代码 7 b, X0 l4 x% |3 O; z
math.pi 数字常量,圆周率
: _) }$ O4 ?7 b2 ^* Y" v2 b- #数字常量,圆周率
7 d) [. E( v! z$ B/ D - >>> print(math.pi)
- w( t! U' ^9 |/ {2 \ - 3.141592653589793
复制代码
& M. i: K* Z4 J( Smath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x- n" A! O, n( h) l& ?
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
5 z( a/ U7 |; M8 K0 | - ceil(x)
+ b2 V) @$ c- ?: \# n& S" v5 e - Return the ceiling of x as an int.. D4 P# D( V# t. Y- X' J$ n$ u! |
- This is the smallest integral value >= x.
9 e( ^9 g3 D) O; q+ d - ! e7 Y( s4 W) H% q, O
- >>> math.ceil(4.01)3 n: X) K. \% F! M8 V
- 5# u7 P. j7 o1 }; J( Q( @2 ?* f
- >>> math.ceil(4.99)
5 D5 ?9 j. n0 }) E! |1 |% L H9 Y - 5
0 x& K' t& O- W2 |# n - >>> math.ceil(-3.99); Z, W5 k* z; J8 `" p8 J8 x
- -3; q4 S; |( h8 j: f$ b
- >>> math.ceil(-3.01)# C; S6 c- s- b0 P" A# k* G f6 T
- -3
复制代码
2 y. Y- }1 T; xmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
& r. E; T* m1 G2 f. D) k- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身1 n0 c& e# b+ I( u* W, a& D
- floor(x). G8 j @8 C- T5 u
- Return the floor of x as an int.+ G9 h& L8 }( ^2 O
- This is the largest integral value <= x.
3 }& |4 K! W7 w" I5 A - >>> math.floor(4.1)/ H0 N1 I2 ?+ t, R7 H
- 46 K( i" r" h7 n' D6 T
- >>> math.floor(4.999)
, d8 N3 v+ _% Z$ A - 4/ m- T ^; U3 d, a( a% z5 D/ w
- >>> math.floor(-4.999)4 \0 c+ u$ }+ ^: o) H: d% Q
- -5- o& U. g8 R- }% Q6 c C8 |# z
- >>> math.floor(-4.01)- s3 C* h8 v" _4 G
- -5
复制代码 ' F+ ?3 p7 z& e
math.pow(x,y) 返回x的y次方,即x**y; H" g: _& @: E
- #返回x的y次方,即x**y* b7 N. U/ Y( _# j9 `$ V. W6 O1 a
- pow(x, y)
6 j; v. O0 H, s. g$ Y - Return x**y (x to the power of y).) c" C( [/ o8 R3 t8 @' b
- >>> math.pow(3,4)
0 E' @- w# r9 C/ h& | - 81.0
( Q! Y" |" j; Z - >>> " y" v9 k( E9 W! X2 |7 g
- >>> math.pow(2,7)2 f7 n* M, ], n0 u5 [
- 128.0
复制代码
- z) s0 Z: y" P: D* } p, ~math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 [2 @: b) {; m' S$ n' c% V- j. w; \& M- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" B% B& P' q% z - log(x[, base])
- L- X6 C6 R& Z7 [; ^+ X - Return the logarithm of x to the given base.
, T; A4 w" X: J2 @8 R - If the base not specified, returns the natural logarithm (base e) of x.
0 b/ ^6 W2 A! f - >>> math.log(10)
9 V5 ]( O; \' _* Q a* V1 ~. R - 2.302585092994046
7 S5 b+ L/ T# p1 D0 t; C; i - >>> math.log(11): p1 r z: A7 [4 ?
- 2.3978952727983707/ z5 f( o; q% D
- >>> math.log(20)
v6 k5 R8 _' P; u% ]% a8 m - 2.995732273553991
复制代码
/ f$ L( i- X3 M& F# o k2 V0 o- pmath.sin(x) 求x(x为弧度)的正弦值
: W9 o# i# _; i- #求x(x为弧度)的正弦值# P: N& S, d! T( a. P7 n
- sin(x)
, P$ k( c& z3 @; P4 Q9 a3 z - Return the sine of x (measured in radians).1 y( r, t7 n( h2 ?( F
- >>> math.sin(math.pi/4)
# G3 i0 E/ Y( C7 j - 0.7071067811865475
: V+ r5 J+ p3 V( g" s | - >>> math.sin(math.pi/2)
9 ~8 U) b, G" x k& V: v' u - 1.07 ]* S) | V# y
- >>> math.sin(math.pi/3)
6 c! p j& K. i2 U5 i' o* E - 0.8660254037844386
复制代码 ; M/ e7 L6 T7 e0 v( T5 e# X j
math.cos(x) 求x的余弦,x必须是弧度
* A h. W4 |. Z8 m& S- #求x的余弦,x必须是弧度
L3 r- J3 ^2 X9 t) N* `) R6 N% F9 A - cos(x)
3 t6 V" b& B7 ]% o - Return the cosine of x (measured in radians). H8 B% }1 h( l$ `- W' r
- #math.pi/4表示弧度,转换成角度为45度0 g7 ^: w* J: [: t6 m/ Q
- >>> math.cos(math.pi/4)% _* @! Z1 @2 X) D1 r
- 0.7071067811865476
& j( T: y( R+ s9 ^7 f# m - math.pi/3表示弧度,转换成角度为60度/ R$ a& V9 a9 w( d; A* P
- >>> math.cos(math.pi/3)
1 d' ?2 E' T: X' C - 0.5000000000000001
6 h, T( B! K) M - math.pi/6表示弧度,转换成角度为30度, Z5 ^9 O$ L/ A1 l. w' l
- >>> math.cos(math.pi/6)
, p) @; ^2 U6 f9 Y - 0.8660254037844387
复制代码
6 P! f; }9 {; lmath.tan(x) 返回x(x为弧度)的正切值" n, G% O$ g0 t: l' R
- #返回x(x为弧度)的正切值
+ M) T& A) _' Y O9 \2 A - tan(x)4 I9 ^8 P) y7 d Z/ Q T s
- Return the tangent of x (measured in radians).
/ V4 U+ W, }6 i8 V- d2 M - >>> math.tan(math.pi/4)
. W& U- U- ^# m* D( K3 Q0 ^ - 0.99999999999999999 D- V( t. J( F) z% H- i# j
- >>> math.tan(math.pi/6)7 g4 I( |2 R. h8 N
- 0.5773502691896257+ B. w4 Q: S6 a" p0 A
- >>> math.tan(math.pi/3)
' z# R/ `8 V' q+ \/ r+ E4 F - 1.7320508075688767
复制代码 8 s# f/ n" b$ W+ X$ c0 r% }% u
math.degrees(x) 把x从弧度转换成角度, Y; m7 W: h2 z5 x! i2 {
- #把x从弧度转换成角度+ A/ e; i' ^, U8 x+ I; A3 _
- degrees(x); N2 x& p( D# K! t4 i
- Convert angle x from radians to degrees.& v* p! `8 O* I, o$ |
+ F; K) `. ^$ l [' V- >>> math.degrees(math.pi/4)
. J# N, I5 I5 T& o - 45.0
$ Y n+ U& @' A/ u5 i - >>> math.degrees(math.pi)
6 J7 `$ a' q3 k A' k6 G9 Q - 180.0
! z4 }' Y+ N% s, K( w - >>> math.degrees(math.pi/6)
' O: n7 i; H" v6 I& t/ p - 29.9999999999999965 E$ G5 L! E: w& D9 Z9 H
- >>> math.degrees(math.pi/3)
: i9 {/ U8 W: L9 K, t) R T - 59.99999999999999
复制代码 " I% v( P& `9 D1 N9 |4 A5 Z
math.radians(x) 把角度x转换成弧度$ G; U8 Q1 P/ O" t. m( l
- #把角度x转换成弧度4 B/ Z' U/ b9 t/ A" l8 z* o) b. ]
- radians(x)
" c! l. D5 Z' v/ ^ D y - Convert angle x from degrees to radians.
; f) E& F3 f) a9 R) D - >>> math.radians(45) ?% p6 k: s! ?- h# h
- 0.7853981633974483
/ Z; E2 D3 z Y8 V0 A, N4 b - >>> math.radians(60)
- |+ t S- e$ B - 1.0471975511965976
复制代码 4 O1 _( r$ E7 Y0 x8 h
math.copysign(x,y) 把y的正负号加到x前面,可以使用0
: t+ _& W9 J/ ^4 i- #把y的正负号加到x前面,可以使用0
5 Z6 _+ ]9 j$ m' a5 q5 a - copysign(x, y)
+ b' _" ^, B* D% F' ~/ }$ G+ K - Return a float with the magnitude (absolute value) of x but the sign
, ?: ~0 Q5 \" w; m( f! C - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
3 @) y" \& K! Y - returns -1.0.& M; h" ~- Q; `1 w
, j7 O$ i$ j. G. I2 U1 G) Q5 Z- >>> math.copysign(2,3)
" y+ b+ a j c M* \/ e - 2.0- {2 z- v) s! ^( c N \ L z
- >>> math.copysign(2,-3)
; k$ I' k- d/ f" h - -2.0' d: j p( C2 N2 t" g2 G( x
- >>> math.copysign(3,8)
" R" @) C9 d! [; J- a6 S - 3.09 \% @* C6 ^( K# X! r) e
- >>> math.copysign(3,-8)3 N2 j, m; w/ x+ }- I+ Z4 t) h
- -3.0
复制代码 + ]$ f! w2 H) J' J
math.exp(x) 返回math.e,也就是2.71828的x次方7 B0 ~4 Z( @. ]2 d1 v
- #返回math.e,也就是2.71828的x次方$ k" V( @* T5 F$ r* f
- exp(x)
2 A$ E/ Q* K6 q; }5 }" X - Return e raised to the power of x.9 l* y7 R7 r+ ~- F! t M$ l' U
. W7 [* I& Y& m. D* a6 @- >>> math.exp(1)
8 c$ R2 ?4 L1 k. F9 c( [2 u - 2.718281828459045& w. r E) z4 j( S4 U2 `
- >>> math.exp(2)
8 p8 K! E7 K* c# i; @: Q8 f - 7.38905609893065. h0 ~, s. Z4 }0 A# U, K, b2 f
- >>> math.exp(3)8 a; P$ l+ e1 i5 t8 P( U9 z" o
- 20.085536923187668
复制代码
% N! g# S }& Z; |' `( @7 Qmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
5 I( P8 U1 G& M" V, |' Y- #返回math.e的x(其值为2.71828)次方的值减1
: p4 {2 J0 y! b7 O - expm1(x)
7 U, N, h6 r Y - Return exp(x)-1.
# c- w; L2 j# u: g- ` - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
" y* n( a8 X, s8 L. |
- T* u$ @6 j9 A! ]+ {- >>> math.expm1(1)
: `+ m7 z! |" e" P$ \* T - 1.7182818284590452 V& O5 ^6 `7 H; H& y- v k, `
- >>> math.expm1(2)
% C7 x7 A/ V% x- D8 G( e& G - 6.38905609893065
7 q- u" X" l& k3 ?5 }* U$ x e/ X - >>> math.expm1(3)4 |. a X5 J) r6 u& a7 r9 W
- 19.085536923187668
复制代码
) ]4 [) B7 M4 gmath.fabs(x) 返回x的绝对值
I) ]9 N0 K& p; Q: T# y# v) `- #返回x的绝对值" i& g _$ \1 |% o3 {$ ^5 f
- fabs(x)
& Z. n" S4 N- q( e1 Z - Return the absolute value of the float x.& K1 w6 y% Y" x2 l: `
- ; d& \) Z# X Q
- >>> math.fabs(-0.003)6 J8 h3 }6 j2 `, V9 S
- 0.003
! _ {) l: u w. R - >>> math.fabs(-110)) Z1 z q8 p1 R* U& I
- 110.0
" _- p, k+ t# U) J4 I' Q - >>> math.fabs(100)" ^2 d! Z# M! D: Q! C* }
- 100.0
复制代码
0 D8 q+ `3 X! Z9 Imath.factorial(x) 取x的阶乘的值
# @1 a6 c0 e- {& u, w, A% P- #取x的阶乘的值( ?. R; c) N6 Y
- factorial(x) -> Integral
* d- ?9 {8 A* I* C H - Find x!. Raise a ValueError if x is negative or non-integral.
+ N2 N- X9 x0 _ ~' c" | - >>> math.factorial(1)
+ W0 \ @! H. S' r1 n - 1* J5 n0 U% U) [" k
- >>> math.factorial(2)
, b+ _7 ^- u5 e6 ~1 E+ _. t: Y4 d+ i - 28 C' s: q h0 P% S% V
- >>> math.factorial(3)/ [: n1 z+ [8 ]; {! v7 o; q" D
- 6
: f. g7 [, V& `, ?" \- g - >>> math.factorial(5)
) [7 f @. v3 S/ s; E3 ` - 120
# ~& v3 h; A, \8 L8 ^ - >>> math.factorial(10)
- r1 b5 g c! G9 a - 3628800
复制代码 / a" |9 k& h7 Y4 M0 p
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
9 ]; N3 E' j/ o' }" X. ]- #得到x/y的余数,其值是一个浮点数$ {' C) B. q) ]8 s" U
- fmod(x, y)
9 [/ Y, E/ E- d4 p$ ~; V - Return fmod(x, y), according to platform C. x % y may differ.
% h$ i, H* w: G$ y - >>> math.fmod(20,3)
8 G4 S7 K4 i! u; T; J! s) f - 2.0
. i1 M) l; G! b - >>> math.fmod(20,7)! A! b9 A$ a1 n% X$ ]5 ]& D
- 6.0
复制代码 1 K; l' f. ^$ b* h) Z& n* p! a
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围: R8 ~5 x( I$ ^" m0 N/ e9 ` q3 E
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
/ A" E( w0 c8 b+ R L! b6 | - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值5 n6 z* k L) T" X( y$ X
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和17 ?/ t! r! v; Y0 r. @' [) S
- frexp(x). y: g$ B0 T: x( o3 h
- Return the mantissa and exponent of x, as pair (m, e).
- ^" `9 ]& _9 s3 A3 s; T7 w - m is a float and e is an int, such that x = m * 2.**e.2 k6 F& B3 g& c, h
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.) f0 L7 K- [0 T* S9 I
- >>> math.frexp(10)
0 H) _* m. V6 | - (0.625, 4)
+ W* S) c% p' y( y5 @, i - >>> math.frexp(75)( x1 C. G( _4 X! v# L V% b% |7 W0 Q
- (0.5859375, 7)
2 H+ H: v. Z* ^5 s; c - >>> math.frexp(-40)7 n) T: L ?) d# T
- (-0.625, 6)3 Y( u F1 S6 S) t- U2 I
- >>> math.frexp(-100)
3 t7 B; R# w* a; t6 T, z - (-0.78125, 7)
e2 ?* S, c: X+ m6 b' A! } - >>> math.frexp(100)
# F1 q( h0 v4 Y" h z - (0.78125, 7)
复制代码 g" A- ?; n* N: `8 f% S
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)# M. @# i: B0 Q" S/ e
- #对迭代器里的每个元素进行求和操作3 J3 O# M1 D7 L+ J D4 m
- fsum(iterable) ~. L+ S, g1 q, f6 u3 B. b
- Return an accurate floating point sum of values in the iterable.
4 u5 r* W. F; [* O" s - Assumes IEEE-754 floating point arithmetic.1 W) m1 I3 ~/ @* [ D2 R! B
- >>> math.fsum([1,2,3,4])6 r5 ~4 ?7 E# P2 o- m" \
- 10.07 C# m" s4 U- d0 i+ W) d6 O
- >>> math.fsum((1,2,3,4))
& r7 `9 Q" e5 m6 I) W$ y - 10.0
# h1 q1 b/ z- r6 T - >>> math.fsum((-1,-2,-3,-4))
; u/ v k6 x/ o' B4 \ - -10.0
' Q6 o% Y+ H3 T/ E& x( x6 S* L4 [ - >>> math.fsum([-1,-2,-3,-4])
' @9 o8 j3 t" u+ U$ p - -10.0
复制代码
( m0 }; X1 z* w" J5 p4 r# @math.gcd(x,y) 返回x和y的最大公约数/ R5 Y( _; y. M; H; o2 T
- #返回x和y的最大公约数
0 e+ N+ P- V. ] W1 X2 f) G - gcd(x, y) -> int6 h8 k0 c1 N# I2 d/ J/ d
- greatest common divisor of x and y
Y; E/ m3 M7 @2 ^1 L# l - >>> math.gcd(8,6)" F% C/ e6 d9 V9 ^9 |
- 2# V5 S/ S5 |! \+ W( H6 G
- >>> math.gcd(40,20): [! B/ b7 d4 L$ S& Z+ }
- 20
" J% K1 P$ {0 F: c - >>> math.gcd(8,12)
1 f4 y+ ~1 k: v+ V( B - 4
复制代码 * `, Q" x" l2 @2 @$ G' q
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False9 ~/ N6 z$ ]6 j3 s9 M' r: x# ^3 G) F
- #得到(x**2+y**2),平方的值
2 Z( `3 H. l& R6 U4 p; e- ? - hypot(x, y)* i5 {9 r3 T" Z, ] [; {' ~
- Return the Euclidean distance, sqrt(x*x + y*y).; b' V3 O3 j$ c1 ^/ p
- >>> math.hypot(3,4)2 L: M4 `- |+ P9 z" R# j- A1 s
- 5.0
3 h. A; S/ a- d - >>> math.hypot(6,8)
$ _, O$ l; X" e v) A - 10.0
复制代码 4 o3 n+ L1 F4 w. X/ A1 |
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
) f' ~% l1 ]. A: ]& L% ]- #如果x是不是无穷大的数字,则返回True,否则返回False% ~1 h, d4 L, o& I. {
- isfinite(x) -> bool
/ E8 b- R% G. m9 s( C - Return True if x is neither an infinity nor a NaN, and False otherwise.4 L1 z+ n5 }2 }9 o
- >>> math.isfinite(100)! j* v! r$ ^$ h1 l
- True
3 U" p5 \; l1 B9 i - >>> math.isfinite(0)
: b$ P" u. l: a4 o- D( H8 P - True" |% `9 d- r+ R9 m1 O, k/ ~
- >>> math.isfinite(0.1)
% |% k6 T/ |4 e# w: P9 s5 ?$ U - True2 W3 U& W/ t# ?% S3 D1 X$ N7 ^
- >>> math.isfinite("a")
E; {* l- j Y5 T - >>> math.isfinite(0.0001)
/ X0 `5 T3 {$ }6 H$ X9 b& ^ P - True
复制代码
2 k1 x1 H, h: S/ ^2 lmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False4 \' y+ P \. `
- #如果x是正无穷大或负无穷大,则返回True,否则返回False! D1 k8 m" C. ?$ z! ]" v, ^! c9 S
- isinf(x) -> bool5 X) T: y O; ~" l
- Return True if x is a positive or negative infinity, and False otherwise.
6 p2 c( F9 i6 [/ s* q - >>> math.isinf(234)
8 s" }+ `$ `- K$ g - False
# A5 W. ` c! |4 Q' x3 f7 p - >>> math.isinf(0.1)3 h& @7 |: |! E- h6 A+ l
- False
复制代码 * [/ W% Q# z( E1 Z4 ~. u! u
math.isnan(x) 如果x不是数字True,否则返回False3 L6 j- Q( L1 s
- #如果x不是数字True,否则返回False' q6 U* z$ m% i+ y; p
- isnan(x) -> bool1 _8 K. r! t7 C4 e
- Return True if x is a NaN (not a number), and False otherwise.# Z! _4 Z5 v3 D. e: H4 ~# ?. [
- >>> math.isnan(23)- ]* G, P$ J6 c2 z2 W
- False
. i9 |& W- A8 A5 u - >>> math.isnan(0.01)
+ m, w$ t. u+ b' o - False
复制代码 $ ~* x ^5 R5 h* c4 u9 a
math.ldexp(x,i) 返回x*(2**i)的值
2 y, O1 c& j [" Z9 v1 s- #返回x*(2**i)的值4 I" T1 L/ z. D* W) D* [
- ldexp(x, i)5 e7 _5 M. P v% Q
- Return x * (2**i).4 S ~& d& H: \8 }- {
- >>> math.ldexp(5,5)
. N7 B( G9 w# m* y+ {/ C4 H' H - 160.0' y+ h, b+ M$ F+ ?: G% G3 }
- >>> math.ldexp(3,5)" o) W2 n# e# v8 g( W( w% p9 N4 U
- 96.0
复制代码
2 j# }+ E/ K" J# @+ J7 Nmath.log10(x) 返回x的以10为底的对数: y& r0 V9 c) b+ Y, \
- #返回x的以10为底的对数
' O8 u; ]2 l k# {1 F, \( M - log10(x)
6 z6 T5 S+ Z4 c8 U9 [# ` - Return the base 10 logarithm of x.+ ~0 y9 m$ M w( s8 J: m7 j
- >>> math.log10(10)
: N- o* R! D; N% L( M. s. Q$ [' @$ D( L - 1.0
% N4 y. v8 q2 h# P* O$ s - >>> math.log10(100)
9 n( O+ }5 D, e6 L0 e, g/ T! i9 D Y - 2.0
1 n7 W ?) U+ Y9 j' c1 U - #即10的1.3次方的结果为20
6 ]+ W s% a, `& r" [ t; U6 l - >>> math.log10(20)
) k% y5 Z( f* @! S- a6 @6 y - 1.3010299956639813
复制代码
4 b2 B N! I0 s" `4 Umath.log1p(x) 返回x+1的自然对数(基数为e)的值1 |$ s% ?5 ~2 ^2 a5 m
- #返回x+1的自然对数(基数为e)的值
+ K2 K5 O' D& J$ [. W, H - log1p(x)$ H( @0 B+ I/ X
- Return the natural logarithm of 1+x (base e).2 @ M6 U! `! J4 R
- The result is computed in a way which is accurate for x near zero." N) W4 i* i" r0 E* ?( D
- >>> math.log(10)9 P4 O) v, v: v8 m+ V. q
- 2.302585092994046
1 \; \, O8 {) q' a& n$ @6 K) t. O - >>> math.log1p(10)* e+ K, F8 A6 w
- 2.3978952727983707, E* C; n* k1 Z
- >>> math.log(11)& v8 i2 i, q" W4 m
- 2.3978952727983707
复制代码
: c2 s# b6 h _' X! X; mmath.log2(x) 返回x的基2对数 A* y$ ^3 b: n3 u+ U5 }5 j: C. y
- #返回x的基2对数
" {2 p! }+ t, P/ l/ V$ f - log2(x)9 G( j( e' H3 o( Z
- Return the base 2 logarithm of x.# b. I' u( b( P7 Z
- >>> math.log2(32)1 Y6 D' W1 N1 @
- 5.0: |' l) ]1 l3 ?. B0 D5 B9 |5 f
- >>> math.log2(20)
' p0 C0 {, k. \, @; [6 | - 4.321928094887363
$ d' E U$ [6 C- \ - >>> math.log2(16)
S" {! l# r6 n$ d - 4.0
复制代码 4 s9 e) g/ Q+ f- u1 M6 P
math.modf(x) 返回由x的小数部分和整数部分组成的元组5 x0 J& p* f6 {( B5 m
- #返回由x的小数部分和整数部分组成的元组4 P5 Y) O8 D# I z7 i8 T
- modf(x)
* }0 t: H% V( _" u9 J7 L, I" X - Return the fractional and integer parts of x. Both results carry the sign
|/ M" c* a/ b) j' K9 b- Z+ g6 y - of x and are floats.
- @; b3 A; }. x- z1 \( z% I$ ~/ W - >>> math.modf(math.pi)
) c9 M2 d. R% v; M9 i, b' D - (0.14159265358979312, 3.0)
. P- f# H2 u, U( V - >>> math.modf(12.34)& v! w+ U. L6 R- d5 B& ~ t
- (0.33999999999999986, 12.0)
复制代码 ; a7 P+ _3 s# L) n, \
math.sqrt(x) 求x的平方根
( o) s0 ?$ p& T3 w9 r1 h- #求x的平方根0 T! x3 Z1 l U& w( ~7 ` S3 C
- sqrt(x)
3 z* D( U }0 w# L, V - Return the square root of x.
2 K# W. V, `8 t9 y5 f3 `& E - >>> math.sqrt(100)
8 L& x) F& _& q; @1 U: S - 10.0
. h6 L4 @7 ]0 _5 t5 R! ^4 j! w - >>> math.sqrt(16)/ r; ^- y% w+ o- c5 ~' }
- 4.0
( ~1 u6 r3 B4 H0 n9 Q - >>> math.sqrt(20)6 W! d, |; D( w0 t, T- g8 z: [
- 4.47213595499958
复制代码
; \& Q1 f# B9 A3 V2 wmath.trunc(x) 返回x的整数部分- #返回x的整数部分
. q5 L m: \; w; h0 Y+ q% H% R - trunc(x:Real) -> Integral
) `3 q7 i+ _% b2 W- I - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
* ^ v& C6 W: `( r9 N! ~; t+ q% U - >>> math.trunc(6.789)
0 z; U# R" x: q8 r* }6 r* d - 6
3 I: p- g2 P, H9 \8 { - >>> math.trunc(math.pi)1 H, X" j' i$ @; `. Q, o2 f9 M
- 3
/ d. W8 l7 b# Z. S$ d - >>> math.trunc(2.567)
/ f1 V4 {: s2 ?3 L! u4 x- Y - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |