马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
" ^: E3 a8 g& U7 i, O6 b' ~) a+ c; v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
5 s+ W7 d$ w2 W/ \3 X7 \& `3 U
( O f( j$ `( c方法1:
# K. g; O H# b' e1 \2 j- >>> import math
5 I1 r8 I" z$ x! J- k - >>> math.sqrt(9)" a$ d7 H2 D* l
- 3.0
复制代码 方法2:
% D# F% \4 v& A! c. I" | m/ A- >>> from math import sqrt
, ]# S' s9 r1 c4 K - >>> sqrt(9)! `3 k0 A T; P: a' O
- 3.0
复制代码
6 \ }$ u) Q8 D. T
/ w; u; B1 f ^2 ~3 J- S: |: R3 Rmath.e 表示一个常量! ?$ `0 I% d0 Q
- #表示一个常量9 J+ N& s) ]- A4 ~' _$ w" U! H
- >>> math.e
+ A$ ^4 c. D; B- _ - 2.718281828459045
复制代码 " g2 z0 K' \: i9 y& _8 K& A
math.pi 数字常量,圆周率
: k2 r' ]# G1 u0 j$ \; h( Y, o- #数字常量,圆周率5 z4 [7 q6 ~4 m. @) Y4 n4 R
- >>> print(math.pi)
2 y0 [) n' i" |' H1 i - 3.141592653589793
复制代码 6 I `5 w8 j2 u
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x$ n% E k6 l2 r+ X: n3 G
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x# Q' D" ~' X' ~5 f
- ceil(x)1 ~, N( T v% w9 E
- Return the ceiling of x as an int.
+ a1 v) M/ A# D- N - This is the smallest integral value >= x.
* q1 `- w& e: l2 Q8 T9 W+ S' a& j
, M" B U- s4 w) w2 Q7 P0 g+ [/ a/ p- >>> math.ceil(4.01)3 C" N3 v2 p* M% z; o5 k
- 5& p" ]) i* P7 p2 X
- >>> math.ceil(4.99) d5 P% s; o$ {, h( ^: |
- 5
& A/ u. v! g9 V2 n& ~0 u - >>> math.ceil(-3.99)+ U' m, r# P; s/ ]: K4 [
- -3
7 F& z/ }; u& B0 j6 Z3 j - >>> math.ceil(-3.01)
/ ^, w3 r# e7 j2 z! E4 ? - -3
复制代码
5 q8 a h8 h5 @) t% i9 Tmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
+ d, _3 k% r7 a* V- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
' O# A, f7 |# d - floor(x)
. E# |9 \# J' y6 X- \2 ` - Return the floor of x as an int.
9 q8 x6 O- ^) _, e: z# Z - This is the largest integral value <= x.9 Y. G: k" ]. P7 \+ W
- >>> math.floor(4.1)) ^: d6 T2 i+ y. N# {% S- J ]6 d: t2 W
- 48 O- G) q3 L" _/ O; O$ _* i
- >>> math.floor(4.999)
; ~( o, J6 P9 q, H$ @; v - 46 m8 D1 w" U/ T$ s( h( S
- >>> math.floor(-4.999)- r: \! S! \! z) S% _, h
- -5
3 l, A* x/ e! X8 [ - >>> math.floor(-4.01): L, v/ _, H1 h* p$ H
- -5
复制代码 3 x& o0 u5 q) N! u, s# J
math.pow(x,y) 返回x的y次方,即x**y
4 Q4 G; [8 x |) H- #返回x的y次方,即x**y
) k @* c9 t1 b% n8 j - pow(x, y)
3 y2 p7 {) Q# J% B& w) s - Return x**y (x to the power of y).2 d$ {7 @& D& N- e- G
- >>> math.pow(3,4)5 k, F; j) ]) p; X; B3 C+ j
- 81.02 b& {5 J8 g( j+ A- S
- >>>
. A; M" U- C2 z* B2 U3 P - >>> math.pow(2,7)* t( P: |4 z' K) b2 C4 n% F
- 128.0
复制代码 3 \8 a( v* `6 H/ t, j* ~
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
5 M; O0 |4 S7 `9 B2 P2 i$ A- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
: X; m# s" E: C: S# c- ]+ W - log(x[, base])) c" d o+ H7 C8 o: O$ Q
- Return the logarithm of x to the given base.8 L+ k8 P! m/ b$ Q
- If the base not specified, returns the natural logarithm (base e) of x.
7 h0 I1 e! @( U+ b# G! a8 }, a - >>> math.log(10)* [+ K& {6 i! R! W3 p) Y7 g: Z' _
- 2.302585092994046 g) \+ ]2 i% J' s
- >>> math.log(11). z% h h# m% c, n: R* V) W1 `5 j
- 2.3978952727983707
: Y& H/ ?1 O9 m1 @& m - >>> math.log(20)
8 F9 Q. x; {9 r9 b( W7 t" M* I8 a - 2.995732273553991
复制代码 7 \3 }& h; z( N. B
math.sin(x) 求x(x为弧度)的正弦值
- T4 N' }1 \* l- #求x(x为弧度)的正弦值; A' }6 D D7 k! b/ O0 n
- sin(x)
/ Y" y p& e+ j' }' }9 ?( J - Return the sine of x (measured in radians).6 v7 p0 q+ H$ Q. L6 w7 l6 s
- >>> math.sin(math.pi/4)
- ` `2 J0 f- Q1 w* K6 T. m( A - 0.7071067811865475: [) o% H2 i1 {+ N- N
- >>> math.sin(math.pi/2)( L2 y: r3 G/ ^3 g
- 1.0
3 x/ K& v0 f# c6 ], ~% | T$ M; X8 P - >>> math.sin(math.pi/3)9 O1 P. v8 l2 |3 J. z
- 0.8660254037844386
复制代码 9 n) d$ P" ^1 ]+ p
math.cos(x) 求x的余弦,x必须是弧度
1 X, {7 O6 R3 j, w/ v' y: }5 Q- #求x的余弦,x必须是弧度2 x4 o9 {! B- ^
- cos(x)/ y9 S8 P; u# d( g/ y' Y! L
- Return the cosine of x (measured in radians).& G# ?* i* C* p2 J0 L) G! a
- #math.pi/4表示弧度,转换成角度为45度
; Q- m R1 K( {8 h: d5 i - >>> math.cos(math.pi/4)
3 P% Z7 ]2 S$ y$ r. b, t( F! ? - 0.7071067811865476$ r3 E& k0 B& u" `$ o
- math.pi/3表示弧度,转换成角度为60度) ^9 \; _/ U7 C2 D7 P
- >>> math.cos(math.pi/3), p. B/ i7 E9 x, W. R0 B5 \2 B9 c, W
- 0.5000000000000001
) y3 w, D& D: B7 P$ G3 C% x- d - math.pi/6表示弧度,转换成角度为30度; o5 N+ q( O6 r" T I
- >>> math.cos(math.pi/6)
! Z" y/ L3 ?0 E% h5 K - 0.8660254037844387
复制代码
+ [1 p4 y# ?. H* |% pmath.tan(x) 返回x(x为弧度)的正切值
# g9 R Y; Q- S. b* g- #返回x(x为弧度)的正切值
, d1 g+ J" C9 w7 D& P2 p6 i7 \1 _ - tan(x)
4 A8 l a7 n2 R6 @, w) K v - Return the tangent of x (measured in radians).
3 Q0 M2 O8 D* e7 j, L - >>> math.tan(math.pi/4)
7 A* {2 F. `- W( G, P. | - 0.9999999999999999) ?; a9 m7 t+ p9 ` f/ {( S, E
- >>> math.tan(math.pi/6)) n" p# b0 ~" m5 W) c7 G
- 0.5773502691896257
4 }7 N2 N& k: B0 }7 K. Z - >>> math.tan(math.pi/3)
; K5 U- M8 b+ D& G' @" m - 1.7320508075688767
复制代码
- z Q7 y& F1 _8 }! J7 Qmath.degrees(x) 把x从弧度转换成角度5 J+ ]( X1 ?6 l: E& j0 D' o
- #把x从弧度转换成角度
& C& _- m r2 i - degrees(x)
4 X7 h; t7 T5 s, o* v4 f9 o/ D - Convert angle x from radians to degrees., g1 f) e! Y# ?' O6 D
. p/ A' D3 |& [- >>> math.degrees(math.pi/4)
: E0 X! }+ J1 P S) h - 45.0
! Y) ?8 {0 x, Z: G1 G. Q - >>> math.degrees(math.pi)5 G: I8 I$ k6 o g
- 180.0
! A! i: G. m8 [) P* k+ E' f5 m - >>> math.degrees(math.pi/6)
6 Z& W4 J8 I) Z - 29.999999999999996 p' E) R- ~/ |: |8 K! y& N
- >>> math.degrees(math.pi/3)
* D/ F; S( E1 F" u% a5 E! ~ - 59.99999999999999
复制代码 1 ^, Y! I2 v: u$ x+ ~% k, t
math.radians(x) 把角度x转换成弧度
# v5 H! E8 n; L9 U5 q6 @) _% y- #把角度x转换成弧度
0 F1 b0 O* L) C. W- n) F6 P - radians(x)
5 h& l1 N; n' y5 j3 }1 n! g - Convert angle x from degrees to radians.
! {: c: Q+ q: ~" q# [1 t/ R2 C' M - >>> math.radians(45)- f, Z* E! d5 P$ y! w. l
- 0.78539816339744835 p4 B! v' `, G' @% D
- >>> math.radians(60)
8 y. N8 u* E, ^% u - 1.0471975511965976
复制代码
$ l1 R, Y/ p9 f: ?% }math.copysign(x,y) 把y的正负号加到x前面,可以使用04 K3 |6 u3 x6 R- x6 W1 e5 W) v) K& ~: j! w
- #把y的正负号加到x前面,可以使用0
7 P: X8 m! E2 E1 Y% Y* z# { - copysign(x, y)( F% k$ |/ v9 m# A# {9 J( M
- Return a float with the magnitude (absolute value) of x but the sign
7 V8 T; _8 S% M6 I: f; D - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 8 h7 _) c3 S/ L' E/ k4 H
- returns -1.0.
' @4 i1 @* W7 {( J( k$ E2 c& f
( H- U( M: ]. q4 {' }3 G8 u- >>> math.copysign(2,3)6 h" t$ s0 h! k) A/ P: k
- 2.06 J' e! E$ p& j8 h9 a3 M2 |
- >>> math.copysign(2,-3)& L% ^( ?1 O* u
- -2.0' r* y7 _% C; W9 D# N. x+ V
- >>> math.copysign(3,8)
! ]7 L5 u* Z1 L2 [- q5 |+ T - 3.0 T4 T" \( R( H5 i/ z3 N& i9 F h7 ]
- >>> math.copysign(3,-8)" p# Q7 O* v$ y/ `: }6 _9 F
- -3.0
复制代码
' B2 V2 C/ }! Nmath.exp(x) 返回math.e,也就是2.71828的x次方
7 d+ |7 u$ @% g1 r- #返回math.e,也就是2.71828的x次方# J+ k" c: C% ]
- exp(x)- X _9 z* J( |( W
- Return e raised to the power of x.
7 t" _1 f8 W4 k
9 x7 v+ M1 @6 J3 m/ e: ]- >>> math.exp(1)& f# n! T+ e" w) O
- 2.718281828459045
9 p3 q3 n% Y( G. V; v - >>> math.exp(2)
c& }2 e$ e8 Q; j - 7.38905609893065
+ k' K0 W$ P: D. F; ~ - >>> math.exp(3). L2 N+ b& Q5 a+ ?. M2 R6 d
- 20.085536923187668
复制代码 & Q6 _: d7 j$ v# F" F0 d0 s1 h9 L+ Q/ m
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
+ R0 E! D1 g0 H: V6 x6 g! ]2 a" l! C- #返回math.e的x(其值为2.71828)次方的值减19 j- @7 e6 l) N9 B$ j
- expm1(x)6 w) j# t% y2 Z8 p3 I- E& ?
- Return exp(x)-1.
5 [) E7 Z! ~, S( X. u* u - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
% B. t! A2 e* q2 p; Z - & r3 j- }' {, a* W4 C
- >>> math.expm1(1)
) g8 G! e( k2 ]7 _# n - 1.7182818284590456 Q, z* A& O3 F" J
- >>> math.expm1(2)
0 X5 k' f2 U$ D - 6.38905609893065; Q$ R9 A% O% C. W
- >>> math.expm1(3)
@7 d( X5 {: T2 ^: y: C" _ - 19.085536923187668
复制代码
& [6 `; @% n+ M1 I- f+ \: U+ ]math.fabs(x) 返回x的绝对值
2 ?% I" ]' E% ~- #返回x的绝对值
) \# I8 k5 }! Q& u; E0 Z5 \/ Z - fabs(x)" n3 Z9 o8 d# \" z, s, f
- Return the absolute value of the float x.0 U# n _3 A( X+ r. F! s
" a: S9 x5 `7 b* e3 t- >>> math.fabs(-0.003)
+ B" X n! J9 e% W - 0.003
) h+ w% M7 B5 b& D - >>> math.fabs(-110)
& w, ~7 B j9 z4 I" M - 110.06 ]0 C, b8 Z R6 U+ ]
- >>> math.fabs(100)
/ {7 W- S( n7 x/ T - 100.0
复制代码
. A J' `) B+ jmath.factorial(x) 取x的阶乘的值! k& o* _: e$ F5 C* r3 e1 S
- #取x的阶乘的值
" @+ ?* L( Y% u - factorial(x) -> Integral7 U2 S3 G2 x, u4 h7 X% R! Q
- Find x!. Raise a ValueError if x is negative or non-integral.0 E9 R; V+ T0 ^: _! O
- >>> math.factorial(1)0 k( _, T$ W& c. I! l
- 1
A' j3 c% ]$ }4 U - >>> math.factorial(2)
5 i& y9 t& d) O" _& _- D - 2
, Y% ` ~6 G2 O - >>> math.factorial(3)9 j& l8 ~/ e; ]. s' z2 ^! o* ~
- 6
y- g, |) g+ J X) A% N* d - >>> math.factorial(5)
$ H3 @! D& X5 Q" Q3 f - 1209 D* D: p$ `4 _. {5 X P
- >>> math.factorial(10)
/ y0 v" l( A. B0 X* W8 ~1 Q - 3628800
复制代码
& h2 P! k" S: N8 k# E" Vmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数: z7 S( H2 W* {8 N5 F+ P6 K
- #得到x/y的余数,其值是一个浮点数$ o1 V b6 K- h& E' J. k3 H( M
- fmod(x, y)% k8 ?7 O! {" W8 R8 I2 }
- Return fmod(x, y), according to platform C. x % y may differ.! b4 h1 k1 ]$ f% T8 b) b
- >>> math.fmod(20,3)$ J2 z' s* m; ~9 O( a; o8 A
- 2.0. A- J/ N2 T: K
- >>> math.fmod(20,7)
% R' ?' @1 c9 `0 q' u" C - 6.0
复制代码 6 J' Z# J! L. w. b7 d2 R; `
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
4 U# a$ k" H! S' d9 O- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
6 x1 B% K0 x" g4 ~8 H; {- ` - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值. l& u" X( R( `. v( \
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和15 ]4 o6 p& O) n+ u
- frexp(x)% V4 s$ T0 t4 y: k, s3 ]0 |. W
- Return the mantissa and exponent of x, as pair (m, e).
$ P9 d/ V7 J1 s5 B% V - m is a float and e is an int, such that x = m * 2.**e.# _$ [3 w% ]8 _" b! `, g$ y; I4 e
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
2 j2 _2 c, ]7 z7 r) m/ ~ - >>> math.frexp(10)) x9 ] R8 X' S" I' F" e# Y
- (0.625, 4)0 J# {) Q) `9 K0 g( c+ `
- >>> math.frexp(75)
) L' p2 g9 f+ S$ Y - (0.5859375, 7)
* T( U* z3 K: [2 M8 c3 D2 \ - >>> math.frexp(-40)$ y B! Q" l4 R4 p: J
- (-0.625, 6)/ X8 c* a1 M4 E
- >>> math.frexp(-100)
* m2 _& B' l$ q8 ^% }8 @9 V - (-0.78125, 7)
/ U$ h2 G& t {4 P# a- c - >>> math.frexp(100)+ |& Y( ~/ f2 y2 ?4 o& V
- (0.78125, 7)
复制代码 8 u' m6 V( i/ j, k4 m$ [6 J
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
7 _. \) t) E2 w* w% \- #对迭代器里的每个元素进行求和操作
8 b0 J$ S+ Q# a' \% @0 X6 B1 H - fsum(iterable)( B; m2 T' `- p% b6 b7 G
- Return an accurate floating point sum of values in the iterable.
- V8 s0 h3 q% L b7 l4 Z$ v, p4 A - Assumes IEEE-754 floating point arithmetic.
' }, X$ c9 c! ~& w: { - >>> math.fsum([1,2,3,4])! K! i h, A& @% P
- 10.0
2 }! R' ~- t* G% s6 }% x' l- Q - >>> math.fsum((1,2,3,4))
: R% g1 b9 H9 W- A) K) l8 K - 10.0
# w6 \, L9 o: P" Q0 {. q3 R - >>> math.fsum((-1,-2,-3,-4))
; R) N7 d" |# Z. j - -10.0
. q( e# E( R* B* K* q - >>> math.fsum([-1,-2,-3,-4])8 b5 j+ G* m* [5 \- s9 l
- -10.0
复制代码 ! p4 P+ |8 e6 m. K+ ]) C, Z
math.gcd(x,y) 返回x和y的最大公约数
! y, E1 z) c) `) o- S- #返回x和y的最大公约数
: p8 j* x) d) N. s: C' o - gcd(x, y) -> int) G6 p* x0 `2 C" p8 l
- greatest common divisor of x and y. q- V8 e# r o" l0 F) v2 V
- >>> math.gcd(8,6)
2 { p* ?: i/ s: o# g - 2- w- j. o7 z( d& L; g# z
- >>> math.gcd(40,20)
. `; p8 t+ t O0 i; D - 200 h7 D1 g0 F& K
- >>> math.gcd(8,12)
( @9 |7 N! }7 P( Q! ` - 4
复制代码
8 I$ T4 i5 k) O- c1 R0 n ^math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False# s$ r, T! H# B7 a$ ~
- #得到(x**2+y**2),平方的值0 `) q$ n6 n9 K
- hypot(x, y)
) m- x5 M$ R, u+ J$ n7 j - Return the Euclidean distance, sqrt(x*x + y*y).
/ U# K! j8 n- i. F2 S4 _ - >>> math.hypot(3,4)
$ Y1 X/ T' u4 R. I% n - 5.0
# c/ W' n4 t1 f. R$ t - >>> math.hypot(6,8)# Q* S* h5 D8 i. S7 l5 W7 T, `
- 10.0
复制代码
+ K+ t; ?* O, }# H- w9 ?" Z3 p* \math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False6 C3 l$ C( c# _5 u$ i
- #如果x是不是无穷大的数字,则返回True,否则返回False" f% u6 X" }% y' }( S4 E. q
- isfinite(x) -> bool; E# R4 E( d" Y0 ^2 A* b. L& s
- Return True if x is neither an infinity nor a NaN, and False otherwise.
; O. w3 r5 C7 d - >>> math.isfinite(100)
. [( H& D- n1 v9 r# w - True8 u' f+ \3 G. o2 S- s
- >>> math.isfinite(0)
9 m, r& \3 f3 d+ w- v$ j) I - True
8 L* |# N& c4 J6 {. X8 R - >>> math.isfinite(0.1)4 f S1 q/ q- \+ H$ \
- True+ D2 i. j; ]. u3 s, t
- >>> math.isfinite("a")- ~7 ~+ Q* \4 R+ s* \4 }5 U
- >>> math.isfinite(0.0001)
& Q# V% `( f V' Q2 E/ T* i - True
复制代码
# F+ X" g! c. g- v0 {& D9 Tmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False3 N8 t" M; \0 P. Q
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
5 H/ T6 V% }$ O1 w) b - isinf(x) -> bool" M9 h5 `1 z2 X# p. x
- Return True if x is a positive or negative infinity, and False otherwise.
' |" V. c! z [. M7 w0 ?# P - >>> math.isinf(234): t$ D) R+ F$ L. @ U) g3 p
- False8 [3 D. G* ?* M2 L2 g+ `5 f K
- >>> math.isinf(0.1)
: K7 \' I( z9 g9 N" q) J' P% k - False
复制代码
" q( }; }% O: C0 `1 f# d0 Lmath.isnan(x) 如果x不是数字True,否则返回False, {& `9 k: \% b: k; X6 h4 @. R
- #如果x不是数字True,否则返回False; C. [: Y0 O( T7 v& |# }
- isnan(x) -> bool2 }+ k6 q# ^; d
- Return True if x is a NaN (not a number), and False otherwise.
, C% K$ I7 C; `0 B7 V2 r% v - >>> math.isnan(23)
( i. X/ p2 ~2 @) n) Q. l/ P' z; p) { - False
: _1 H. K" P' c1 I - >>> math.isnan(0.01)
9 i( b: c1 H: ^4 B* ^$ ]7 W - False
复制代码
. T" x/ t1 F( Y& a! y Emath.ldexp(x,i) 返回x*(2**i)的值7 `; k" j! r' Y; U' e
- #返回x*(2**i)的值/ n% P* {6 x% | Q- E6 v2 m
- ldexp(x, i)+ m6 F# n' }) H) j( c
- Return x * (2**i).+ i2 d% g- U4 a# h5 j
- >>> math.ldexp(5,5)
) L' J" ^* x" N - 160.0
# A& H# v$ T+ V. f9 q - >>> math.ldexp(3,5)2 o* s6 y1 N2 w" k" y
- 96.0
复制代码
; g% O& e; A! u% W. h, @math.log10(x) 返回x的以10为底的对数
2 K. r2 D; A. i) ^# k; K! X, R1 i- #返回x的以10为底的对数
2 t; r* Z8 Q- x6 p) S. }$ p - log10(x)* C6 _4 Z7 F6 C' \. Z& p
- Return the base 10 logarithm of x.
; t" u( B% N I3 L6 A/ F$ d$ U - >>> math.log10(10)( t( [# @; Y( `4 i
- 1.0
& B5 S) v" p% s- T - >>> math.log10(100)
7 F2 j' t! i; P, q0 u - 2.0 A ], Q7 k3 K& `7 k
- #即10的1.3次方的结果为20
+ I+ i4 C0 x- E$ g) c - >>> math.log10(20)
: L- |0 f0 W$ f4 A3 }( J e& ^ - 1.3010299956639813
复制代码
g, W5 u9 Z. D/ _, o$ @# zmath.log1p(x) 返回x+1的自然对数(基数为e)的值8 `9 g! J/ `8 Y
- #返回x+1的自然对数(基数为e)的值
/ q: R& P9 l* e& k) v' u, a - log1p(x)
' n; G9 O/ z- e, w- B! `! K - Return the natural logarithm of 1+x (base e).+ [1 k3 y' S( e, m* G2 _
- The result is computed in a way which is accurate for x near zero.1 G0 k/ s- m+ }7 a t
- >>> math.log(10)
, S4 |$ R; S! S) D6 K4 c' U - 2.3025850929940469 E) r* u: n! S! |( i% d1 i
- >>> math.log1p(10)
g- v: v; }% }1 T4 O - 2.39789527279837071 a+ X T1 y8 R ^( n% q5 @
- >>> math.log(11)$ F( s2 Y9 R7 t) V3 W) g" _
- 2.3978952727983707
复制代码
* ? D. g2 N5 b; d% Qmath.log2(x) 返回x的基2对数
4 N5 m' q8 F( X4 h- #返回x的基2对数" c' l. [: X$ @- |: |5 i/ G. d% |
- log2(x)
4 [4 x, K/ k# v: c - Return the base 2 logarithm of x.
3 j2 B- c- r W F* E, C - >>> math.log2(32): N6 R: x# \' }
- 5.0
, G. E* J2 A# j$ L) Z5 \: b8 f - >>> math.log2(20)
$ K8 J5 z' p. N& ` - 4.3219280948873633 B% I: ~" N- _6 ^0 B. e) L
- >>> math.log2(16)& p4 ^# A3 G- W# w$ W
- 4.0
复制代码 ! ^; Q3 i# g9 ^1 g5 L# l! z* R* o
math.modf(x) 返回由x的小数部分和整数部分组成的元组
9 i: z2 F ]+ U j- #返回由x的小数部分和整数部分组成的元组- {* ^) M/ s2 {& R
- modf(x)
6 w0 S% t# d T+ ~* K; J2 @ - Return the fractional and integer parts of x. Both results carry the sign
4 N' p+ }" g T8 h - of x and are floats.
8 E0 I' q& E! B+ c6 _2 z - >>> math.modf(math.pi)
( D) j$ k& s' c1 n! x& r - (0.14159265358979312, 3.0)) l# u& r8 O- Y$ J4 g
- >>> math.modf(12.34)8 v o; A; |7 ?/ v! ?% u
- (0.33999999999999986, 12.0)
复制代码 : x6 {2 `& e2 }9 _3 |( W9 o; f
math.sqrt(x) 求x的平方根
6 R5 K# w4 [- ]- y- #求x的平方根
+ v; H9 w1 |1 l( | - sqrt(x)
! Y( I0 O! T1 P4 l7 u! @# r$ {! T - Return the square root of x.7 P/ l7 z: t9 v
- >>> math.sqrt(100)
2 B/ e5 V& _: j2 T5 g - 10.0
* \9 |- w- X4 D3 H) y - >>> math.sqrt(16)# J! A8 E$ r. A* u9 |8 K0 \1 n
- 4.07 n$ f! k `: R0 ~" o8 ]
- >>> math.sqrt(20). a: r1 W/ G6 `# C( \$ a
- 4.47213595499958
复制代码
/ [$ O- y/ H( j, I" Amath.trunc(x) 返回x的整数部分- #返回x的整数部分
' s" y$ \& o* t6 d* s - trunc(x:Real) -> Integral6 W- u* l" k9 s4 K' o& L9 n
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
3 ?8 a J( Y1 }/ ]. q9 w - >>> math.trunc(6.789) ` A$ F5 ]* O: Z; }3 K" E& r
- 6: W( L. \+ j: C! H- ~/ G% ?
- >>> math.trunc(math.pi)
% _' O! D. K. d- z0 K - 3; H8 w: f( h2 @# g5 P
- >>> math.trunc(2.567)0 p2 P- `6 E9 T5 p
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|