马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 I# N% |. R# Q R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。1 Q" e n6 i) R: f! ?% {- m: H) o1 f
( A7 t9 `: H. M4 [8 m
方法1:- F! L$ ?! F0 f9 h5 ^& j/ C
- >>> import math V0 Z/ Z$ T. Y. ]9 j. {& Y
- >>> math.sqrt(9): F& y, q* H7 i5 a5 ?1 Q
- 3.0
复制代码 方法2:
' h$ X$ K- S7 H b e$ \- >>> from math import sqrt- T9 h" T! F/ [
- >>> sqrt(9)0 Y: B- ~7 K) D. Z) Q r# j+ P, L
- 3.0
复制代码
3 r- Z8 ]* O/ N0 R! x8 n% O Q( m
/ t4 N: C" b. C" ^math.e 表示一个常量2 Y* ?6 U' U/ K3 B5 Q4 [. b" q, D
- #表示一个常量
8 h% K2 R; W& ] - >>> math.e
1 t3 l% p2 Z3 R - 2.718281828459045
复制代码 . ^/ |8 ?5 {; f5 A- y. T5 @
math.pi 数字常量,圆周率+ ]6 {* B; C K# o3 T/ k- [* [! @1 s
- #数字常量,圆周率
6 n, f! R, H0 z. I4 X9 H - >>> print(math.pi)
a3 |3 J9 x' m& ]8 Q - 3.141592653589793
复制代码
B" q- e7 n8 ^math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x: X; U; W" U! }& Y9 M3 X
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
3 ?! b; N# `& R: C9 u3 [ - ceil(x)3 [/ x- y/ F% D) s
- Return the ceiling of x as an int.
% ]' h4 ?9 l6 f, L/ m! `, {$ j - This is the smallest integral value >= x.
4 A- ^6 u' J# f$ A2 s4 o
0 V9 Y: F/ Z. Q, h# p- N8 @- >>> math.ceil(4.01): h( i. r; P" T3 x0 c
- 5/ `8 Z2 Z" \% b
- >>> math.ceil(4.99)) X2 O& C( Y0 p: L9 {
- 5
4 ^3 O# | `1 B: L - >>> math.ceil(-3.99); {7 W' t5 ^6 t+ o$ D8 h2 U0 t
- -3, F1 ?, S+ n' V+ c- l
- >>> math.ceil(-3.01)
9 `3 P i: _" d' \, w) Y - -3
复制代码
& C* q* E9 f$ ]/ omath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 w+ _! E1 P% U4 c' P* l3 ?
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
3 a6 a, d' p: O& L* n9 l4 n! P - floor(x)! o9 y& A( v! f2 s9 p
- Return the floor of x as an int.
9 d! n% \; ?8 K' B! t$ G - This is the largest integral value <= x.4 P0 M) O/ a, o# E8 ~7 \
- >>> math.floor(4.1)5 o) }- ^+ q, D6 V
- 4
+ B; W! r6 A3 t) m+ } - >>> math.floor(4.999)8 o; z- A" W9 ?" Z% ?1 H
- 42 J) r: o' u* o8 i5 i3 W3 f
- >>> math.floor(-4.999)
6 b% q9 F' ?6 r( s, H - -50 p x" N: k% A2 l" Z2 { q
- >>> math.floor(-4.01)4 c7 K! S; v3 W' w6 Z% z: s/ |
- -5
复制代码 9 M |/ X X. C# o
math.pow(x,y) 返回x的y次方,即x**y/ \$ Z9 j8 }1 v# @% U
- #返回x的y次方,即x**y6 K/ M+ T% \/ M% v4 s' B9 Q& }: ]" L
- pow(x, y)
7 e: R' Q! `% S. x* R! _! [ - Return x**y (x to the power of y).. d$ `$ d2 W8 |# O. B
- >>> math.pow(3,4)9 O* T* J0 @( ]( o' m) g0 j3 L2 p' l
- 81.07 T: Q. x2 e5 \) L* a2 o! k
- >>> 3 x D: g# k2 ~" X4 }
- >>> math.pow(2,7)' }6 E, [) H+ n
- 128.0
复制代码
6 N* N7 Q# ?$ a+ m/ L3 q# C, ^math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 `( Z$ @% h4 y& `) g9 c! e
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base), P0 m3 F8 _+ w- c$ _
- log(x[, base])5 i4 x5 _6 ^2 N. k+ F
- Return the logarithm of x to the given base.
5 _( b% i% Q3 A- F- N7 G - If the base not specified, returns the natural logarithm (base e) of x.
0 G, [# o: D0 x; ^% v5 ^# C - >>> math.log(10)
( I/ g* [- R! F% F y. ^ - 2.302585092994046
7 C9 p) f$ `+ a$ H4 j) d - >>> math.log(11)
- W! A/ X, o5 Y. k0 m$ ^! n - 2.3978952727983707* t" o5 G1 B ?8 `8 a1 [
- >>> math.log(20)% D" w4 ^ s3 W% o) o
- 2.995732273553991
复制代码
9 P* G! O; g' J. r" z+ kmath.sin(x) 求x(x为弧度)的正弦值$ N' \( I: _- }# P2 Y v$ f
- #求x(x为弧度)的正弦值
! f: t/ V2 T" F: a& C- ^ - sin(x)" N! I9 T5 K: d
- Return the sine of x (measured in radians).+ f7 x) v( ]: c9 O- ]1 x4 T- i
- >>> math.sin(math.pi/4)* u7 ^' C6 X8 w. n
- 0.7071067811865475
k% q& @' Q; C9 R) D7 ~1 H2 }' e - >>> math.sin(math.pi/2)
- H5 N) R1 p% k. ~& A/ ^2 g3 j - 1.0
. J/ q9 [9 X$ l) M3 W7 x - >>> math.sin(math.pi/3): }( E" `/ O% w9 j) U5 M
- 0.8660254037844386
复制代码
9 B) Y- e) ^; A! N; Y3 N {5 D% v/ Xmath.cos(x) 求x的余弦,x必须是弧度3 L: k+ E( H+ H* h
- #求x的余弦,x必须是弧度
* L {) F- L% d" x8 L - cos(x)$ g3 F; b9 \% x2 P) w& o
- Return the cosine of x (measured in radians).: \7 O9 |. w5 M+ Z
- #math.pi/4表示弧度,转换成角度为45度
" K% A$ r3 U# N4 k% j - >>> math.cos(math.pi/4)% I9 E, e! e6 c
- 0.7071067811865476" N! A; N8 G( T
- math.pi/3表示弧度,转换成角度为60度% u" W! t2 e, ^7 l4 {9 f8 J+ ~. |
- >>> math.cos(math.pi/3)
" R0 A4 {; X6 Q1 [ - 0.5000000000000001; V0 P& W( V i) Z2 X; R1 j% r8 m
- math.pi/6表示弧度,转换成角度为30度
% t6 X8 p) |' i" ^ D - >>> math.cos(math.pi/6)4 T' n- J& D) v0 v
- 0.8660254037844387
复制代码 / v( }, [ m- c1 {9 b$ P4 p
math.tan(x) 返回x(x为弧度)的正切值/ ?( u V! ?" h. {& ^4 C
- #返回x(x为弧度)的正切值
" x8 |( A3 g3 E' I9 ]/ \ - tan(x)
1 \3 p4 r2 X y( S2 v - Return the tangent of x (measured in radians).
* L% U# z; t! b( A - >>> math.tan(math.pi/4)( d3 i- |6 Y4 R" g7 S
- 0.99999999999999993 o( c' B+ l" v) h/ x, B
- >>> math.tan(math.pi/6)- m, C5 Z" o( s
- 0.5773502691896257
- W( ?# H5 \7 g9 H: J: E' x1 t, I - >>> math.tan(math.pi/3)% h( J% n0 f5 B( x
- 1.7320508075688767
复制代码 4 x' A6 S6 h9 x; O
math.degrees(x) 把x从弧度转换成角度& Z$ l2 a; l, s8 b! Y
- #把x从弧度转换成角度2 r- l' n+ \) t& Y: `% n
- degrees(x)
4 a( ^: @- |3 d+ o4 x - Convert angle x from radians to degrees.
, Y! V6 Z2 e e7 @- ], s - * g: c1 D2 u8 v
- >>> math.degrees(math.pi/4)
- T2 I0 A h a( ~" R( H0 A - 45.0
1 I: R6 @, B5 k; h8 B - >>> math.degrees(math.pi)1 M9 l5 l6 J+ ^- a$ J5 q
- 180.0/ z. C; f3 F/ N: v& I# w
- >>> math.degrees(math.pi/6)
8 ? J" J: L4 E - 29.999999999999996
# C" e% g$ Y5 E6 }- X - >>> math.degrees(math.pi/3); H& K1 q$ e v3 F( W3 k: z6 n
- 59.99999999999999
复制代码 ; S5 N A) S0 Q$ U/ U8 Z
math.radians(x) 把角度x转换成弧度
) A0 ~$ @+ d! p0 o! N% s- #把角度x转换成弧度
( @& Q+ L4 Z1 ^0 \ - radians(x)
6 k0 w4 D' u2 S9 M- `" ]1 m& C - Convert angle x from degrees to radians.
- i3 T$ L" V3 @3 C( v4 ^! g - >>> math.radians(45)+ A' z o( E$ k \$ Q4 X+ c
- 0.7853981633974483
1 A; q& o6 f7 N! F9 Z( | - >>> math.radians(60)
/ x. `4 w: Q" D7 ?& r: R+ y" x* d6 b - 1.0471975511965976
复制代码 4 y1 v# w$ \6 L6 x v
math.copysign(x,y) 把y的正负号加到x前面,可以使用0- x6 m; _+ K$ i1 Q4 {: {+ f3 ~
- #把y的正负号加到x前面,可以使用0
: R0 D( o8 j- n, X; V) X - copysign(x, y)/ L$ y+ ?( ~. u3 X5 n! \: X
- Return a float with the magnitude (absolute value) of x but the sign
" f' o. ~0 U- b& N - of y. On platforms that support signed zeros, copysign(1.0, -0.0) + K. P; I I; K" B5 U" X
- returns -1.0.4 `5 _9 k7 f2 {% d) v0 ~6 n. |
- # a F6 Y, h8 C# }- A1 {
- >>> math.copysign(2,3)% x! f3 o" g- a. N
- 2.0
* k5 `3 T& f4 G" r, d' f/ q+ D - >>> math.copysign(2,-3)
# g8 u) \! j8 i - -2.0
; ^5 [3 `; ^1 c ]* N - >>> math.copysign(3,8)
* J$ p' F; R1 ^ - 3.00 B) A1 o: S. C: ]5 B. G( t
- >>> math.copysign(3,-8)" x$ m( Z, a0 X5 [+ I3 A
- -3.0
复制代码 - I& E/ j1 i O* w! V y7 b* |5 m
math.exp(x) 返回math.e,也就是2.71828的x次方1 @7 W. N: y# b8 v4 ^- t
- #返回math.e,也就是2.71828的x次方
L/ V, ]4 E# q t6 C/ x$ w7 f - exp(x)
0 O& U* f5 `5 ^. L6 `0 @6 ~ - Return e raised to the power of x.' h$ ?0 y% v" e) t
: v6 {* D: q% r+ L0 S. s, u- >>> math.exp(1)) K% N. G# {0 t; q Z& O
- 2.718281828459045
7 q9 c& L! q% l/ i$ r - >>> math.exp(2)
% u' s8 N9 O( o. }0 {3 l - 7.38905609893065: ?/ O: e) `+ x: }8 d& U- S
- >>> math.exp(3)
3 c& e) a, V8 N" q - 20.085536923187668
复制代码 " y2 D7 Z+ D3 ~8 V& j }6 m
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减15 T! O. g+ e' v! R8 z# x# p
- #返回math.e的x(其值为2.71828)次方的值减1
, r- L, v/ B; U" R% {+ i - expm1(x)0 `7 w) L. H5 `/ p& J2 Z# Z
- Return exp(x)-1.
4 C; y: c; t0 m; c1 q! W - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
7 d; W j# ]% e
" g" N. t- J1 l. v- >>> math.expm1(1)+ e9 p. z% t' o2 D3 w) X) d
- 1.718281828459045
+ [3 x# _ R% x Y - >>> math.expm1(2), e9 b: z5 i' n6 Q6 x& l
- 6.38905609893065
& b. O4 x( S# G& m4 g) V - >>> math.expm1(3)
# a+ _1 }% c: r8 q# H - 19.085536923187668
复制代码 , B; p9 \+ h+ X; a5 g% L
math.fabs(x) 返回x的绝对值) H3 E4 P' F7 Q9 L! P: B
- #返回x的绝对值) W0 j# C: c. B5 E+ T9 b! H" k& _
- fabs(x), p, L8 p. Q( |( `
- Return the absolute value of the float x.
6 S- H3 E3 Z% {! O g6 ] - 5 \) o: V M1 J. T/ w/ a$ P3 O
- >>> math.fabs(-0.003)
- R0 v- m; J$ b3 _+ L0 f - 0.003; T) v+ }1 ?) u0 }+ T0 A: u9 U
- >>> math.fabs(-110)1 d1 _5 _% e1 \1 T7 G1 Z: @& F: M; P. X
- 110.0- h$ n/ |) T) l8 L% y2 v+ W; [
- >>> math.fabs(100)
- y8 B% ?# |' G6 L1 L - 100.0
复制代码 : E; a, `2 Z2 m% N H
math.factorial(x) 取x的阶乘的值
$ U) ?; l; ~5 @5 u+ n2 m' x' w- #取x的阶乘的值
6 ~: @7 V2 G/ E( x - factorial(x) -> Integral$ S N* s" m. M3 u1 X9 Y; ]8 p
- Find x!. Raise a ValueError if x is negative or non-integral.
( O, V8 _1 \9 ^1 H+ Y - >>> math.factorial(1)
% [' O- v) F( d8 @ - 1
, S' H+ V# G+ G/ ~1 \. b; D3 d8 S - >>> math.factorial(2)/ E8 O, t1 H+ B) p0 _
- 2
: L* f! `& h8 D7 x - >>> math.factorial(3)
7 A+ a) X. ?7 ] - 6; f H0 x7 D) o% O* U; I# [
- >>> math.factorial(5)
$ \. k7 m0 C' M - 120
4 X, X' `+ I6 ], T3 i - >>> math.factorial(10)0 }6 L3 a5 Y. ?! c' C. \
- 3628800
复制代码
# B" M& @# f, M; smath.fmod(x,y) 得到x/y的余数,其值是一个浮点数0 {3 F- Q) j3 W
- #得到x/y的余数,其值是一个浮点数
( b& A' T& h' `$ Z - fmod(x, y)' V) P6 a. \ B; |6 z
- Return fmod(x, y), according to platform C. x % y may differ.5 H$ E% j/ i! u1 t9 \9 g$ x. c
- >>> math.fmod(20,3)
7 ]2 p9 e4 p; q - 2.08 [7 }) `- F! P4 y. p; Y, Q
- >>> math.fmod(20,7), ^; m& f/ Q' P
- 6.0
复制代码 0 Q% F: U: }8 p
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
3 l i+ y) J$ v6 I% Y8 C$ p- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,! l2 U* j q1 l! Q5 `$ \
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值2 q5 o1 q1 X3 I
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
) x, z$ v8 [ [. |% z - frexp(x)
- }4 B$ ]. ], R$ y3 O D - Return the mantissa and exponent of x, as pair (m, e).( U! y2 K. ^( O# K( y. O
- m is a float and e is an int, such that x = m * 2.**e.
( e2 ^, _. H, N! ` - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.0 d9 U* Q) |4 U: F6 g
- >>> math.frexp(10)
; I# d" d0 P- U9 ?% S - (0.625, 4)) ~7 k" w s. [8 Y8 i2 n- E! F& o
- >>> math.frexp(75)8 `1 S, X& R! R5 i& c& @
- (0.5859375, 7)9 G7 [+ D2 |& h6 S9 @- Y }# k
- >>> math.frexp(-40)3 Y4 m& H8 q8 L8 w
- (-0.625, 6)' g9 U4 m4 D |
- >>> math.frexp(-100)
& y" h+ a/ d3 A/ I: f* I - (-0.78125, 7). l7 @7 M* C8 L& c
- >>> math.frexp(100)" j8 e/ U! x s" P9 X8 m( w0 P5 W
- (0.78125, 7)
复制代码 . }( L1 h( Y" Q5 V' L
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)9 W: N% v3 r$ P) u* E: G8 Y
- #对迭代器里的每个元素进行求和操作
8 j* r5 E1 m F$ n5 @$ o! y - fsum(iterable)
; M6 Z5 g" B2 Y* z; q* ?* g9 j3 J: i8 Y - Return an accurate floating point sum of values in the iterable.
' b" ^1 N+ O1 C6 K- Y - Assumes IEEE-754 floating point arithmetic.
. A, G5 A6 T. b: i) Z - >>> math.fsum([1,2,3,4])
3 }" m; g; Z. b$ w2 H9 ^" I' N - 10.0
* c/ x! u3 | `5 L! T! j - >>> math.fsum((1,2,3,4))
; z0 Q- {$ x0 w - 10.0" Z3 u# v% q* g) r
- >>> math.fsum((-1,-2,-3,-4))
) v6 [% R+ I+ w' {0 i - -10.0
) x9 k% h' S G# z2 ^: C/ u - >>> math.fsum([-1,-2,-3,-4])$ i- J8 ?9 z8 `' V8 N9 r" Z8 n L
- -10.0
复制代码 $ f4 @8 @! n! w6 ~6 W
math.gcd(x,y) 返回x和y的最大公约数( E" m: V8 O9 x" b
- #返回x和y的最大公约数
/ @+ @7 A, A1 t* U ^* j - gcd(x, y) -> int
5 L9 p3 Y+ J' a+ D; J - greatest common divisor of x and y
, x/ N' {% U) r4 g - >>> math.gcd(8,6)+ q. ^! `% Z! j1 U7 j" I
- 20 e, t3 H$ V* B9 U( G
- >>> math.gcd(40,20)/ V7 N" P/ y7 U8 W% ?2 t9 O7 t
- 20
$ _1 V: Q; n- |' }% x4 Z - >>> math.gcd(8,12)* m. w' y5 b8 w# M, A2 f( z
- 4
复制代码 " _7 `( \' }6 W6 |
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False4 D1 H2 M% M4 a& V# ~# X: }
- #得到(x**2+y**2),平方的值
2 F0 ?: X' [; R0 d$ y" o/ G2 _ - hypot(x, y): U) t, f2 o7 x: Z
- Return the Euclidean distance, sqrt(x*x + y*y).
( O5 m$ N+ K% p r+ M: r - >>> math.hypot(3,4)% s& B W# B( ]" m0 T4 J8 h
- 5.0
6 b& \1 V! Q! g( w" k) S; ? - >>> math.hypot(6,8)
8 a8 v# M3 L1 V5 @* m - 10.0
复制代码 0 j" U2 @1 r! P( W1 p
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! i5 ]% V6 [3 { q- #如果x是不是无穷大的数字,则返回True,否则返回False
) v: \4 o5 l, z" H8 b6 q - isfinite(x) -> bool
- r6 @0 Z- \- v: w - Return True if x is neither an infinity nor a NaN, and False otherwise.
" f: ~# w) D# z* {; A% e' t - >>> math.isfinite(100)6 Z; j) d! e, T: d P
- True
/ d& N' K* Z9 U) K T/ L - >>> math.isfinite(0)
3 p8 P# {4 r8 A$ F - True( T* l7 S6 k$ L! ^1 H8 h
- >>> math.isfinite(0.1)# b1 C& C9 p6 d1 U
- True" W% \4 H( X0 Q! Q$ F. `
- >>> math.isfinite("a")
$ G1 @1 O+ v- r" G" k1 h - >>> math.isfinite(0.0001)
7 f% E7 V: |& x2 I( t5 T7 v - True
复制代码 A2 H2 M: g( J0 Z: \
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False4 w/ f: @4 X6 t* u4 i( K" a& U
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
1 h, |9 i1 X# b$ i" m - isinf(x) -> bool3 e% m) e/ j9 O
- Return True if x is a positive or negative infinity, and False otherwise.
1 r3 U+ k* F2 ?7 p# A - >>> math.isinf(234)( J9 w8 p5 w% V, ?
- False
; j" n# ]0 N# N; Q8 Y, N - >>> math.isinf(0.1)
$ F+ z9 ^ e+ K7 J3 m4 ^ - False
复制代码
" D/ O- M z# K. D& C. F' T# emath.isnan(x) 如果x不是数字True,否则返回False
& s* J& q/ j+ Q/ z+ g" u1 d0 u- #如果x不是数字True,否则返回False
- g0 S/ ^( J4 }3 U( F' R# ~) s - isnan(x) -> bool4 @. U/ E! v% U5 U# D" G9 L1 j! J
- Return True if x is a NaN (not a number), and False otherwise.
) e. y: s7 U, A, o3 p! ] - >>> math.isnan(23)
. {. ~6 \. v. Q% x1 S - False" E8 G. P" `1 w# s2 f; Y f* p
- >>> math.isnan(0.01)' o4 N) ], ?7 ?% O$ X1 K
- False
复制代码
) x7 m7 L; x& I( o' u4 imath.ldexp(x,i) 返回x*(2**i)的值; c5 M. @' U, d, `
- #返回x*(2**i)的值
+ `$ i8 A' X2 h7 V- | - ldexp(x, i)1 b$ y4 p1 w8 N0 q
- Return x * (2**i).
- |# M8 L. h" ]. S6 q9 L - >>> math.ldexp(5,5)
1 z: {* z1 o7 [ E* x, U0 \4 p% K - 160.0
, Y3 A2 m8 y) @3 D# [ - >>> math.ldexp(3,5)3 F1 W7 W" A! M+ Q9 [! s
- 96.0
复制代码 6 ]# j& A% D T
math.log10(x) 返回x的以10为底的对数( ^1 [# y/ ]2 s- P% t v9 ~ a
- #返回x的以10为底的对数
4 F. m1 X$ J3 ^1 @( G" [8 Y& W - log10(x)1 y( _% \/ u# U: ]. j
- Return the base 10 logarithm of x.! Q# g' H( q* W! V( C) ^+ t
- >>> math.log10(10)' x4 E7 ^6 b7 E1 b- U) e
- 1.0
; ?2 \. a; ], A3 X# q* N7 a4 O - >>> math.log10(100)
; `2 p7 R/ Q5 m. S) {! j( U4 M - 2.0
! C' J5 I, H! d0 f! p) [$ y- _ - #即10的1.3次方的结果为204 {4 H. x+ t( c1 T
- >>> math.log10(20)) L7 e% |2 m3 F' d" [
- 1.3010299956639813
复制代码
# ^3 w ?: a ~9 Y& s( h' Kmath.log1p(x) 返回x+1的自然对数(基数为e)的值
( y; k' b6 Y. b% y7 p- N/ \+ z- #返回x+1的自然对数(基数为e)的值
+ f3 x, F3 k, M; A$ x7 k7 v - log1p(x)& F( W; |0 W) {3 `5 V/ C* f
- Return the natural logarithm of 1+x (base e).
) Y5 e; S) ]/ |2 q# h - The result is computed in a way which is accurate for x near zero.
" z5 g3 L; U+ H% c; f2 H4 ^ - >>> math.log(10)
" S) o! q* `" y/ ?6 w, |, q - 2.302585092994046
/ E! O7 v6 f: T3 c# Q+ v$ r6 a - >>> math.log1p(10)
% u$ O: S* A, |. K+ r* G! s9 n - 2.3978952727983707) B. a( b5 W3 F' d) I
- >>> math.log(11)- \+ H5 ?) U$ [8 i4 n6 p
- 2.3978952727983707
复制代码
; i8 `% r; p4 m& U) [8 gmath.log2(x) 返回x的基2对数
/ P$ R: c& ^, w1 D; i% A! x3 p$ O' ~- #返回x的基2对数5 W* Q4 g" s- f5 O# W, p9 G, W3 }
- log2(x)
9 A. l, k/ L& K, `% Q! n - Return the base 2 logarithm of x., ]9 ]" F) w+ m8 x/ P* Z( v6 j0 w
- >>> math.log2(32)% R9 l! }* ?1 W/ c
- 5.0
' I* j: b( X6 \ - >>> math.log2(20)
9 A5 o7 l8 k4 W& w, |$ \/ ?$ o' C - 4.321928094887363
2 K4 M* J: {$ h - >>> math.log2(16)
_% k8 J9 q1 ?+ b. O7 O - 4.0
复制代码 ! V3 @+ d" x- P+ ~* M
math.modf(x) 返回由x的小数部分和整数部分组成的元组
' Q5 G5 Q4 X) O( O" U4 t% Q- #返回由x的小数部分和整数部分组成的元组" l, f7 h( S3 t6 z9 V
- modf(x)
# ^* }/ G2 M# ?/ F a - Return the fractional and integer parts of x. Both results carry the sign T3 P% e8 c! \7 ]! a
- of x and are floats.
% V3 {- p( l9 }' u, l+ e6 Q7 u3 l - >>> math.modf(math.pi)6 I/ T! V2 @. d+ A& \* C% l6 ~2 o
- (0.14159265358979312, 3.0)( S- B, Z U7 ^+ I' Q. j* g
- >>> math.modf(12.34) \2 V i! ~. ~
- (0.33999999999999986, 12.0)
复制代码
& C! k1 x) m; Mmath.sqrt(x) 求x的平方根
3 B; k9 o! |4 e0 B P6 ^- #求x的平方根
y3 |- F( V" Q+ g - sqrt(x)
3 E- X& C* R/ x - Return the square root of x.
. k9 q5 S6 {/ Z9 V6 [& W0 D - >>> math.sqrt(100)1 X. w/ X9 `. o! q8 ~" G
- 10.0
! |( c4 \( b5 T; M+ V$ I$ { - >>> math.sqrt(16)
; w. a$ b* o& K0 Y ~; E - 4.0
6 o4 C9 [. R4 L5 I! Y w - >>> math.sqrt(20): K( i! V, B' D7 i; b. o' m6 \
- 4.47213595499958
复制代码 6 i7 `6 A3 ]" w0 N6 o) L5 ~
math.trunc(x) 返回x的整数部分- #返回x的整数部分
, @8 X" W2 s8 g) n S: L - trunc(x:Real) -> Integral
+ a; b( \- h% [# f: ?. o# m) [ - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.6 i2 L7 _$ n* W0 C5 h
- >>> math.trunc(6.789)
1 |& h" j$ H, a/ _ - 6
) a- @0 a8 M" q4 _ - >>> math.trunc(math.pi)) k p. l- ? o2 @! R$ J! h) J& M
- 3
$ ]4 r8 t1 k9 {9 }0 L' D - >>> math.trunc(2.567)8 D3 k6 d/ \) c3 I; y' Y) j0 F
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|