马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
7 B3 d$ p# v6 k9 j1 Q& S
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。7 B! e$ S g' E ~4 C
* b9 Q: s0 w6 V0 N) d方法1:# f; r; B6 W+ v# ^
- >>> import math _3 _: t# \7 l+ G; [# v# Z
- >>> math.sqrt(9)/ C5 K/ I2 g) Z) Z3 _. p, p+ T
- 3.0
复制代码 方法2:) f7 X- o7 T. B& R4 K( l
- >>> from math import sqrt0 B3 N F) }% _: Y+ Q& V# X3 @; o
- >>> sqrt(9)
4 y3 ~! h4 y o6 F& { - 3.0
复制代码
% \# w: s0 v4 M 3 {# K, t, l' ]+ ]* D- @9 _
math.e 表示一个常量
0 r H1 |% D0 }) q- #表示一个常量
" K. h( R! l$ e - >>> math.e4 A& m$ F# d& V5 F/ f& c3 M
- 2.718281828459045
复制代码 , y2 s$ M/ Z* K
math.pi 数字常量,圆周率
$ N( L+ N( T1 I( ?6 i; Z0 S- #数字常量,圆周率
7 @$ K* r+ W$ o, N. E h: R5 q - >>> print(math.pi)
0 e" i3 w0 o. v4 z# L - 3.141592653589793
复制代码
# O+ V0 X3 O. b! ~ Nmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
; P) L5 Z& ~4 }- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
. W. ] V0 d% w* |0 k# x/ U7 a - ceil(x). @5 B" k! ~4 P# X! a1 p
- Return the ceiling of x as an int.
/ }' W& F* t9 ^: D) H) @2 z! Y - This is the smallest integral value >= x.
1 M4 l9 M& v, ^; k: ]" s, i
3 D! U/ o% s, u: M$ g7 u6 C& l- >>> math.ceil(4.01)2 |% m' h8 Y) B6 a3 Q/ u3 _- w% |
- 52 b, y0 R' ?+ _2 ~7 e! [% \" V+ i
- >>> math.ceil(4.99)
# o) \5 X# p, z0 l) } - 5
/ {9 S: I- J0 j - >>> math.ceil(-3.99)
" q8 R/ l' u% N& T5 t+ U4 P' s7 p/ ^ - -3
' Y, ]( `( e9 _# R4 x! A - >>> math.ceil(-3.01) b! ?5 k. F1 ]' ?1 k
- -3
复制代码 7 s! C0 n- [; D
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
% c0 m+ \0 x% ~- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身$ c# O, Z0 Q, a
- floor(x)
5 }! n$ V* F g4 H - Return the floor of x as an int.$ w% t u3 I- I5 b
- This is the largest integral value <= x.& E; ]' s! r1 G6 \4 V! P$ ^
- >>> math.floor(4.1)9 W* |! _" i6 u B
- 4, M! m7 u. z! i0 [, z
- >>> math.floor(4.999)
0 e0 ?7 V/ T. p4 d+ _" y( L - 4) }4 @/ u. j* d5 L( N2 p3 Y
- >>> math.floor(-4.999)' Z( V! i- r) ?: U
- -5
8 E5 P( ^; g/ s3 N8 ? - >>> math.floor(-4.01)* R) U6 X0 U$ I7 a
- -5
复制代码
2 J2 Q' W- i9 H& a8 f2 Amath.pow(x,y) 返回x的y次方,即x**y5 k% r: c7 i \, j: \
- #返回x的y次方,即x**y
; d; Z$ X4 S9 u+ N. O. p/ c% ` - pow(x, y)* f6 y2 {( o$ d6 g+ { `
- Return x**y (x to the power of y).
- i* r6 l/ E- y" w$ o c - >>> math.pow(3,4) \, R) p- J8 {+ }2 v
- 81.03 }% [! b+ q: d4 o0 `
- >>>
' |0 @+ g/ y; r$ u5 ^) D" L1 _ - >>> math.pow(2,7)
; o% `9 s6 z* j - 128.0
复制代码
2 G' t7 J0 `8 h7 S5 b% Jmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)/ ?9 M' g3 d6 V
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
) }) x: z3 L+ @" y" j4 I4 \ - log(x[, base])
! h$ L3 V5 w: f; t: ] - Return the logarithm of x to the given base.5 }' z( ^( U3 t+ Y. `4 g0 L* T* M
- If the base not specified, returns the natural logarithm (base e) of x.
, g0 w8 X7 B8 J6 o U - >>> math.log(10)
\$ n1 @1 q9 }% N& a+ f - 2.302585092994046- y: K% Z& h2 m2 n& G) a# ?7 `
- >>> math.log(11)0 c B9 c3 N' c% S
- 2.3978952727983707& ~8 k7 j" v* x: b. j, h$ W. b, t n1 i
- >>> math.log(20)! {7 p6 E9 h" y* W8 f" X
- 2.995732273553991
复制代码
- f' K2 V1 r5 }6 _math.sin(x) 求x(x为弧度)的正弦值# X P; o, c3 M7 E4 w
- #求x(x为弧度)的正弦值
7 M; t" Z* m: H' G - sin(x)- c: ]0 @3 y9 S
- Return the sine of x (measured in radians).6 e( z* }: l% L0 s0 r
- >>> math.sin(math.pi/4)
2 }2 m$ K8 d7 {2 Z - 0.70710678118654754 [3 p' U' B/ m& E
- >>> math.sin(math.pi/2)
. L4 L; G5 j& ?, H4 b/ n6 N - 1.0
! ~0 @4 R) U9 u: x9 @, o - >>> math.sin(math.pi/3): e/ _( Y1 L) A; g
- 0.8660254037844386
复制代码
( Y# j% ^9 ^! S1 V( r# Wmath.cos(x) 求x的余弦,x必须是弧度
: p6 h* y1 V( Q- #求x的余弦,x必须是弧度- ~; y. |- U. q) ^
- cos(x)3 v: } j! {3 H$ z5 R) N
- Return the cosine of x (measured in radians).) C* w, ^ ~( w' ?# h
- #math.pi/4表示弧度,转换成角度为45度3 u' m) ~" H* F% Z3 s
- >>> math.cos(math.pi/4)+ S: J$ m; f/ k/ R* c
- 0.7071067811865476* K" b/ a( ~2 F( G, r/ b# L
- math.pi/3表示弧度,转换成角度为60度
$ L4 q+ H! O5 B5 X - >>> math.cos(math.pi/3)
' ]& N' u; `9 }- \1 G6 {! Y - 0.50000000000000012 D) J' d2 f4 r* ^
- math.pi/6表示弧度,转换成角度为30度; C. o3 r, E3 v4 V* o1 _
- >>> math.cos(math.pi/6)
! l8 [ q" o! u& U* T' k8 R - 0.8660254037844387
复制代码 ; s* o c* ~0 \& i8 X8 X8 s
math.tan(x) 返回x(x为弧度)的正切值* N. S' F$ `0 X3 W5 ]0 |' n
- #返回x(x为弧度)的正切值
3 C- d) ~7 [6 u3 m! f - tan(x)% Y" m' T; M O' y8 k! R8 \# [+ ?
- Return the tangent of x (measured in radians).
# G2 \6 L, j9 z3 @7 W - >>> math.tan(math.pi/4)& y% G7 K) y, ^- ?9 f2 S
- 0.9999999999999999& } w5 r0 r4 Z
- >>> math.tan(math.pi/6)
) b2 d3 o( D0 ~! n" j - 0.5773502691896257+ c. @" h8 G/ ?, E; i" S1 B" ~
- >>> math.tan(math.pi/3)9 x" A/ t! A) s& Q' c$ W h% U: C
- 1.7320508075688767
复制代码 " [6 S+ B" n8 F$ _
math.degrees(x) 把x从弧度转换成角度
4 X6 g9 P# Z) P( u) X* b- #把x从弧度转换成角度
8 a* s- \7 B) ~% z - degrees(x)1 d1 N6 v( W# {8 _! Q& L
- Convert angle x from radians to degrees.) d" k0 G% |4 x R: K! M0 w& m" v
- - l5 ?! W* o; U
- >>> math.degrees(math.pi/4)! p( l3 _7 S( E, o9 Z4 u2 A/ Y9 @
- 45.0
! G) y; \# K' I* o+ e - >>> math.degrees(math.pi)! J$ w1 K/ P4 R2 I8 @/ [* Z, p, `' r7 x
- 180.0
! c: Z5 U1 M j& P - >>> math.degrees(math.pi/6)
) d* G0 D, p0 v - 29.999999999999996! D8 e, w3 x5 b
- >>> math.degrees(math.pi/3)2 s- h/ l. G2 V, X/ u* v
- 59.99999999999999
复制代码
( z U$ Y& L! b. g+ Dmath.radians(x) 把角度x转换成弧度% A- Z7 D% j" Q2 f& x3 {3 i& v6 J/ I
- #把角度x转换成弧度
0 Z. M; F. ^6 ]! b - radians(x); s7 @2 o d- |% E2 ]
- Convert angle x from degrees to radians.
0 }- L7 q, W4 t# o' D$ r- o2 b: Y/ F - >>> math.radians(45)
/ @: z; A" O; m- I! ` - 0.78539816339744839 {. n6 |$ {0 q, h1 N; _
- >>> math.radians(60)
Q8 N2 {) |- }& I0 j - 1.0471975511965976
复制代码 / h/ v2 Q# S2 y4 k2 [% B# i2 q5 u
math.copysign(x,y) 把y的正负号加到x前面,可以使用0: A. _" M& S! s \
- #把y的正负号加到x前面,可以使用0# L% A- N5 u b% B( R* J2 n! d
- copysign(x, y)
+ s4 d Z6 F3 ]4 N - Return a float with the magnitude (absolute value) of x but the sign $ O5 r+ C: U: m. _. N' [3 i
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
4 b# [+ E+ h. _+ O+ K# M/ d - returns -1.0.
2 v$ i0 }& V2 A% ` - 8 ?" q/ U% o: V2 t. k# h
- >>> math.copysign(2,3)
! V' p0 j% Z [7 u, M: o7 ^ - 2.0
+ H$ a( ]7 `. u. _ - >>> math.copysign(2,-3)
& H( H6 m$ m' a t - -2.0
9 |+ T( x! R- S* Y/ [# ~ - >>> math.copysign(3,8)
. \' o- t1 s' z: M - 3.0
; B8 X& z4 e! s - >>> math.copysign(3,-8)9 q. e/ h% R( {; m* |: M/ w
- -3.0
复制代码 + h# b) L; F) P7 x) y, L0 L4 ` s
math.exp(x) 返回math.e,也就是2.71828的x次方0 w% p6 R7 I: H$ }6 l" m% L
- #返回math.e,也就是2.71828的x次方
9 X" @' G( P8 z$ x - exp(x)
2 r5 B5 k. g& @/ N/ E2 A4 ]1 M# U - Return e raised to the power of x.9 b1 K9 V. e! B! r; y
- / \* C: M' |7 W. q' Y
- >>> math.exp(1)+ S0 P* ?' V5 d: S+ U* |+ f: L
- 2.718281828459045; w/ i4 J, O/ g; h7 ~8 L r& O/ _
- >>> math.exp(2)1 I& D; R' T: a9 i# E4 l+ P
- 7.389056098930658 b$ ^, I* K6 t; N( ]& f
- >>> math.exp(3)* |( R9 j4 W- `2 g# W. |
- 20.085536923187668
复制代码 8 }1 {: {& [9 G" N# X( l! R
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1. N y, t4 ?+ X
- #返回math.e的x(其值为2.71828)次方的值减1' ]' c* N. [- u1 b7 {
- expm1(x)+ d4 M" W" z6 `* Q5 F
- Return exp(x)-1." |, u4 U* ^* O( b: M
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ X$ e$ ]; U) S. _" n% d$ W2 \
- / r5 @) d& A/ v) d) Y" |
- >>> math.expm1(1)8 s; r) l) |2 M1 K& E! c+ z+ q
- 1.718281828459045
2 F2 `8 O/ Z4 B- S" ] - >>> math.expm1(2)& U5 V" b8 v' \
- 6.38905609893065, ~3 H% L. n7 B: |
- >>> math.expm1(3)+ y, V7 } G i$ B: [6 h1 o- ]
- 19.085536923187668
复制代码
% _0 y4 w* K' _) L- Q9 F4 n( x, N2 Gmath.fabs(x) 返回x的绝对值! ^5 H0 ^# {9 G- S2 }: l
- #返回x的绝对值
+ [: T" h) }, m( y - fabs(x)
f( @( Z; \6 k - Return the absolute value of the float x.
3 l3 y6 b' N9 {1 U) r4 l/ z% }
; k" N4 Z0 U) i' e! k0 V. _% a6 V- >>> math.fabs(-0.003)
5 d% ~' c& W) i% C9 E( g$ V - 0.003: i7 ]( [7 o+ F
- >>> math.fabs(-110)
% U# B% u' X9 L2 |7 B- N) q( l - 110.0
5 T: o- s+ w! M: H7 q) B9 B, h. F' K( } - >>> math.fabs(100)
: v% ~' C) G# E/ ?+ \ - 100.0
复制代码
* C ]# b/ o) Wmath.factorial(x) 取x的阶乘的值
; r& C, Q& l4 o( Q5 r* ?: d8 t4 S- #取x的阶乘的值; p S: E9 B( \8 V6 U: s
- factorial(x) -> Integral
' @* L/ i2 V5 c/ N0 n! x - Find x!. Raise a ValueError if x is negative or non-integral.1 v* k+ _8 |5 z! V" M/ B9 ?2 g
- >>> math.factorial(1)& Q9 Z6 A$ C( [6 j( \
- 1
, ]5 M+ f5 Y5 ~ - >>> math.factorial(2)
5 H2 H+ H3 b& z3 T9 p0 L( M) [ - 2& e$ ~- o" \7 i! g& e
- >>> math.factorial(3)5 c: t8 x5 G8 a9 k
- 68 i/ b. k3 g2 M3 G' o
- >>> math.factorial(5)
/ W( }$ d9 T6 y( [ - 120
6 S* N* L6 q9 p7 b - >>> math.factorial(10)
7 M# ?( K$ l2 Y& k9 W5 G# j - 3628800
复制代码
3 ^, G, g0 b' q/ b0 n) Bmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
& P. {: y! ~6 {! L( G/ `) l- #得到x/y的余数,其值是一个浮点数
* I9 e; e5 z$ x2 } - fmod(x, y)6 o p% O$ L& Z9 k7 n$ j
- Return fmod(x, y), according to platform C. x % y may differ. [, }) ]' \+ H4 R% P
- >>> math.fmod(20,3)
9 [1 H& L7 o8 v9 W - 2.0/ t3 c1 d* ^! N% I- Z) T9 _( E
- >>> math.fmod(20,7)
. u, u" h; h* D1 `, W- \& ^3 r: m - 6.0
复制代码 ' h3 Q/ M4 Q% A1 K( O! z1 `" f0 _
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围8 Z) i. o& _8 ]# x
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
( I( u' k$ V6 _6 \) d; h - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 s4 k. I& e. k$ R3 p, P& e
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1" X. o' ]4 n6 \2 O
- frexp(x)
3 I* y; B# }& Z: v3 b) L; V& l6 l - Return the mantissa and exponent of x, as pair (m, e).: o$ y( e9 R, Z: y& v; q$ p
- m is a float and e is an int, such that x = m * 2.**e.
1 ^3 S' k1 U5 ^. t - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
& q5 K. _! [) Z. r G; ~" ]! ?& t. Q - >>> math.frexp(10)! P! f$ G5 n. Q* ~( D$ F4 f
- (0.625, 4)1 G! a. m4 R) X0 _# [+ F" ]2 n: B
- >>> math.frexp(75)% X4 Z. `! P) H1 P" r- n' F- d$ J
- (0.5859375, 7)
, Z7 [2 X8 Y$ q - >>> math.frexp(-40)
1 j. S, O, \* b, g, r% v - (-0.625, 6)" _ V8 B; H) d! N
- >>> math.frexp(-100)$ d. f& o( [- t: S
- (-0.78125, 7)
) q& |$ z$ b h - >>> math.frexp(100)
# `1 \7 U/ Z; [' w' n - (0.78125, 7)
复制代码
7 l. {- e" _ V+ ?5 P5 Wmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)! {; W7 E/ c$ u8 \. q6 u! U3 J
- #对迭代器里的每个元素进行求和操作
: g" [6 \9 A: j0 C2 e - fsum(iterable)
' N( @; a8 I" G W - Return an accurate floating point sum of values in the iterable.1 u$ e9 r3 L; l! H5 Y' w- ~9 |
- Assumes IEEE-754 floating point arithmetic.
6 i, O- e/ K% l - >>> math.fsum([1,2,3,4])
. |. m! I: ^4 R3 A2 F8 t - 10.01 e4 B2 y2 O% y. {* B& E2 X" W
- >>> math.fsum((1,2,3,4)), ^9 ~+ L9 K1 k g, R/ |
- 10.0
; d* ~- a: z- b9 Z" { - >>> math.fsum((-1,-2,-3,-4))
/ g4 O Y7 f. y/ N; ^. s" v - -10.0! q w5 \# n: i3 r: S
- >>> math.fsum([-1,-2,-3,-4])
2 ^ Z4 c8 _' g! O } - -10.0
复制代码
: c& y9 G1 f0 E6 ymath.gcd(x,y) 返回x和y的最大公约数& \" z1 c3 U) c; G X9 s
- #返回x和y的最大公约数4 R8 b1 h2 z) l4 G! @+ V
- gcd(x, y) -> int& Y- N' g: `% F+ Z3 E" ]
- greatest common divisor of x and y6 {- x$ H% @7 @0 d( a+ I
- >>> math.gcd(8,6)8 i, a. _7 o$ B- ?6 C U
- 2) E& |# E1 b+ P) ~8 c6 |
- >>> math.gcd(40,20). t7 q: `6 ?( v3 x
- 206 H- C! v) ^* _8 w
- >>> math.gcd(8,12)2 N0 v$ Y( v. [: Z* K" o
- 4
复制代码 # U- d% Z6 M0 [7 N$ F7 I
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
; D" ~& [* {$ @- #得到(x**2+y**2),平方的值' c( l7 G1 o7 j# E1 t6 n
- hypot(x, y)
6 @, q0 b. B; k9 R+ e' w - Return the Euclidean distance, sqrt(x*x + y*y).
$ c: E6 P8 z" ?7 [( f* g$ h4 n& t" E - >>> math.hypot(3,4)
. \& W$ w7 U5 \- y$ p$ ?- D: @ - 5.0( n1 ]- W5 f% d
- >>> math.hypot(6,8)
! s3 L% P N5 R - 10.0
复制代码
, z5 k0 U t8 L; B# ?3 @7 Kmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False7 R3 J0 _# ?) A% w5 Z6 B0 T
- #如果x是不是无穷大的数字,则返回True,否则返回False% _ h, E. \& S' d( U
- isfinite(x) -> bool; X# Z( o2 Z% f5 d% ?1 n
- Return True if x is neither an infinity nor a NaN, and False otherwise.
% e5 D, c1 @* o - >>> math.isfinite(100)
! I4 @6 k' Q ?$ q - True
' V$ S" A: O) w2 w - >>> math.isfinite(0)
2 K1 X2 Z& z! |9 V( D - True7 \% P7 F2 ]" x c. }( e
- >>> math.isfinite(0.1)
7 T) v8 |2 H8 G+ X - True* U, r( M0 P' u! y0 B# O) c, @
- >>> math.isfinite("a")
% S/ I8 t( X- I/ Z! W% e' K. Z9 j - >>> math.isfinite(0.0001)
1 Z: p0 t9 N0 @- ^* b - True
复制代码
2 ]; Q" s8 T2 O2 Amath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
9 N' h0 W- H9 r0 F3 L( {2 j- #如果x是正无穷大或负无穷大,则返回True,否则返回False
0 Q5 U L- t( S9 y! ? - isinf(x) -> bool
3 ]. r0 L! |5 d) V - Return True if x is a positive or negative infinity, and False otherwise.8 p) p4 m! H; H2 R' q5 K
- >>> math.isinf(234)" J( G% H. Z% h
- False; P1 Z( C9 A( Y
- >>> math.isinf(0.1)
; Q! p g$ V2 }8 ?# g) i7 {1 } - False
复制代码
5 \9 G) ]$ z6 x6 z. P) Umath.isnan(x) 如果x不是数字True,否则返回False- {; a; \9 C: K! _% E R
- #如果x不是数字True,否则返回False
$ S; H/ N! e6 b0 M. m* o - isnan(x) -> bool* t+ L, M4 `, }5 s: s
- Return True if x is a NaN (not a number), and False otherwise.
. a7 M3 ~6 q* O& W - >>> math.isnan(23)" h1 y- S1 o. A
- False
) i! b/ h8 a* D* ~" I - >>> math.isnan(0.01)
; ]7 |% n4 v! L3 Y( q n - False
复制代码 o [4 W- R; j/ S' c' p0 ~! `8 S
math.ldexp(x,i) 返回x*(2**i)的值/ k: W& D8 z$ U' D
- #返回x*(2**i)的值
2 s- G4 Q2 J5 @ - ldexp(x, i); R. `* R+ m. x% ^0 y& c
- Return x * (2**i).8 _- t8 ?# d+ X7 c ]& M; K! y
- >>> math.ldexp(5,5), i" w# ~. G. L3 i5 }( f
- 160.0* ^; F' E9 l7 A4 J+ O/ f* q7 G) j: c6 `
- >>> math.ldexp(3,5)
9 P* ^) H% {3 u- L! j - 96.0
复制代码 " g, M+ x6 X. f! `" A" H
math.log10(x) 返回x的以10为底的对数
/ p& q$ Y5 O# l% h+ ]9 a1 U1 w- #返回x的以10为底的对数, N" }. c5 @1 t
- log10(x)
0 o$ i, h. B3 h! G$ \5 q0 m9 f- W - Return the base 10 logarithm of x.+ Z! H! ~# w% F1 ]
- >>> math.log10(10)
( S2 a: p1 u5 M/ p- _) R4 O, f - 1.0$ d0 Z0 Z; s) q, N' P% c6 |6 B
- >>> math.log10(100)
3 O! l1 j; f0 F9 ^1 [ - 2.0! Q1 a& R$ L9 k& f
- #即10的1.3次方的结果为208 u: r3 v* _ F: B/ P' i
- >>> math.log10(20)5 }& G/ P, o5 J
- 1.3010299956639813
复制代码 - t& D" l2 S% f" x C! ^% p* k
math.log1p(x) 返回x+1的自然对数(基数为e)的值
1 [$ t, w5 Q* e' m- #返回x+1的自然对数(基数为e)的值
3 g! T/ a- W! r) b - log1p(x)# i9 [0 A! X6 g) P& s
- Return the natural logarithm of 1+x (base e).
* f; Z& P/ W R - The result is computed in a way which is accurate for x near zero.1 I2 w( {; {9 o. u
- >>> math.log(10)
* F; ^) s4 E& i3 p% ~ - 2.302585092994046. S+ _8 Z+ k2 n- z/ P% U5 ]9 n# G
- >>> math.log1p(10): E7 d" Y/ x4 X5 Q y* O
- 2.3978952727983707
4 w/ r6 r0 `) n9 t3 t - >>> math.log(11)
* ^1 r2 d: ~" w5 o* g0 t - 2.3978952727983707
复制代码 - O* {! m g4 k0 A7 F, O M4 `% G
math.log2(x) 返回x的基2对数# A# f$ N) {( k
- #返回x的基2对数
: n: c: Y) X% {: b3 q( g- B: w9 K - log2(x)' [: P' m- O( I8 M. N
- Return the base 2 logarithm of x.
* @9 ~. Y N! z# a2 L( F - >>> math.log2(32)/ l8 ^: f2 o/ o/ L$ e* k
- 5.0; a- t! S4 R5 ^; H* b6 M" _% H' O
- >>> math.log2(20)7 E$ j/ O6 H" w" P4 x
- 4.321928094887363% O U) B& H1 B* N& X
- >>> math.log2(16)
& Y! w" e, Z6 _* x$ [* ] - 4.0
复制代码 $ T5 [6 F! F$ O0 x2 |! P- S# ?
math.modf(x) 返回由x的小数部分和整数部分组成的元组* C+ i6 x7 A( w+ l6 G
- #返回由x的小数部分和整数部分组成的元组) K3 N- K7 t! X& d+ @
- modf(x)* \( H$ r* } z+ H) K
- Return the fractional and integer parts of x. Both results carry the sign
2 ?7 Y' N+ v4 ?9 y$ y E. Z/ P: f: R$ T - of x and are floats.
: u+ M- }) @" a* r# [0 ?2 V0 v" c/ c - >>> math.modf(math.pi)
( ?8 y) d7 _; C9 S3 f v5 h& q. M) P - (0.14159265358979312, 3.0)
$ v$ c0 V' C+ m+ w. { N) F - >>> math.modf(12.34)2 \; U$ `1 ]( T, D- H2 t* V
- (0.33999999999999986, 12.0)
复制代码 ' S1 ~2 K8 Z! `' B
math.sqrt(x) 求x的平方根
( `& q! |. O5 w9 o) X+ R1 K- #求x的平方根+ K: s; G% d7 {* k( N7 o
- sqrt(x)7 s- j' x1 u, w+ q
- Return the square root of x.
# `& L: u* u, D3 W5 M( `9 d - >>> math.sqrt(100)) R: R7 \, Y) \7 _0 U G1 s: b5 ~
- 10.0( w9 G( I! f; a' F
- >>> math.sqrt(16)- y9 H% ]; s; `% d5 x
- 4.0
6 M2 v, L7 y. V8 E: q - >>> math.sqrt(20)
& G2 x7 Y3 r6 x# p; ~0 |6 Z7 V - 4.47213595499958
复制代码
$ C3 S* G8 m# W, ~2 Ymath.trunc(x) 返回x的整数部分- #返回x的整数部分
* x$ A2 h5 M* j) ^/ o& t - trunc(x:Real) -> Integral9 @6 L, R3 A w7 Q) w% I9 X
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method. _ w' r) z9 f/ n
- >>> math.trunc(6.789)
) J1 g# G6 e3 w6 O k) \, x6 P# I* D - 6) K: M+ L5 o: l2 D
- >>> math.trunc(math.pi)6 r# f# ~1 n- B( n6 V( F
- 3
! F0 I3 k" T1 E5 G5 D' I, Y - >>> math.trunc(2.567)* i' k! Y( V+ m# T0 n& {
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|