马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
; R! g5 f! T+ r: L& v
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
; V% {" k* q$ m( e! B3 |# S) ?
V8 X" G, ?% L6 \, B方法1:
7 C/ L4 D) W' g7 m% e- >>> import math" G6 {2 W2 g7 O: W
- >>> math.sqrt(9)" [! d9 t/ j% K7 ^# u
- 3.0
复制代码 方法2:
! Y3 c ~( @$ b# A' f- >>> from math import sqrt
7 ^, A# u K- `9 p5 m - >>> sqrt(9)
* E6 c1 s8 h1 f - 3.0
复制代码
3 n) D0 O/ G& W( J7 [
6 {9 ^1 u6 y2 s+ M6 Fmath.e 表示一个常量
) P; V( \" h% W6 s, a- #表示一个常量
- ~" e* a9 d/ Q# K$ e1 F - >>> math.e8 K* {' V5 b j. c( L/ H# [8 Y
- 2.718281828459045
复制代码 5 {, p: S( v7 a6 M2 H* m9 a, m
math.pi 数字常量,圆周率
9 ]5 o3 b z, b$ F2 J/ G8 W# x' v- #数字常量,圆周率
, q4 Y+ ]7 C4 ^: |* d - >>> print(math.pi)
8 g+ |+ |3 X9 } l& U - 3.141592653589793
复制代码
M$ d N; b5 I/ D' e8 Cmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
* \$ o+ W1 e8 P" u- #取大于等于x的最小的整数值,如果x是一个整数,则返回x6 s- _4 t, [! h
- ceil(x)* I( ?+ D. m( f ^8 x! h6 h
- Return the ceiling of x as an int.
3 }+ N. }0 M3 \% w. l - This is the smallest integral value >= x.
9 j: O: G" H' P& A! R
4 f$ @2 c) {1 I" [% u- >>> math.ceil(4.01)& C" t# }' Y, z% c$ @9 ?/ T+ L/ u
- 5
- M3 @* `" n; \- X3 K+ c - >>> math.ceil(4.99)- [; a A7 ~1 C7 d, _% v8 G
- 5
( e# n4 W; l# V) Q - >>> math.ceil(-3.99), |5 V7 g6 ` y( x, b1 Y& l8 S
- -35 [3 d% a& N; A: b( L4 \/ J
- >>> math.ceil(-3.01)3 X/ `6 ?" K/ O0 Q; E
- -3
复制代码 $ _+ r8 Y" q! ^ D( W
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身7 v' c( a: s, q1 `
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身2 h a- U# W" c- i
- floor(x)
0 y/ I, Q: n: d! F# t% b - Return the floor of x as an int.4 C1 I5 Z5 M5 o6 i6 k& u: V) A
- This is the largest integral value <= x.
/ a3 G, A) R3 `% } - >>> math.floor(4.1)
& W8 e1 T6 L; ]& Q6 I - 4, {4 _% N8 z6 O2 M! |# X
- >>> math.floor(4.999)
2 Y: F: c4 }3 `6 G0 [# }6 g - 4+ Y" A4 k7 D: h! K
- >>> math.floor(-4.999)# c, u- M' Q6 g. U, A
- -56 P: r, d0 Q1 b0 u" H9 o( C
- >>> math.floor(-4.01)
% p2 p! ]" `3 x - -5
复制代码
) k4 X' y( U3 n9 w6 k6 Z: {math.pow(x,y) 返回x的y次方,即x**y
9 \! e$ |5 @- }# n4 l2 E. A- #返回x的y次方,即x**y
* C8 f# K. J5 T1 B) | - pow(x, y)
$ `7 }) y+ P/ k! o - Return x**y (x to the power of y).' i2 g0 K7 D- N0 Q [
- >>> math.pow(3,4)
4 @1 r) \5 f/ y, M5 ?7 ^ - 81.0
% l0 a5 G R1 ` - >>>
4 ]9 l+ Y, i6 E# \ - >>> math.pow(2,7)
. O- B8 I: l/ F) |8 R% v* s \ - 128.0
复制代码 - V- z \7 _% d+ Q& ^, {3 ]
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)) q7 T) H9 q# u' X. e
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
$ E% s( t$ U( I7 R - log(x[, base])
- W5 F, v" t" L" }2 G+ ~ - Return the logarithm of x to the given base.
4 x* B+ j) v" J4 h - If the base not specified, returns the natural logarithm (base e) of x.
$ K. p7 |0 X: y* d% l$ _ - >>> math.log(10)
9 c$ n" e+ j! |% ^" U3 l - 2.302585092994046
/ ^- i, P# E; r - >>> math.log(11)' j8 z+ H" x( P
- 2.3978952727983707
5 Q0 {# p; m" O) W% y3 f - >>> math.log(20)
4 @# Q8 Y3 r' j$ G( D4 j5 d- o - 2.995732273553991
复制代码 ! i0 ^8 p) t; }8 k6 X+ o
math.sin(x) 求x(x为弧度)的正弦值
" w9 E7 v7 _2 h7 [% A/ d( a+ ]- #求x(x为弧度)的正弦值! p9 O5 p, Y6 v5 s' `
- sin(x)
6 e0 W, w) \' M' C; w% ~+ c - Return the sine of x (measured in radians). s: Z' |- b' p# O( P4 s
- >>> math.sin(math.pi/4)0 A9 k/ d1 H; Y7 w
- 0.7071067811865475
4 o3 O+ W1 G5 ]* g - >>> math.sin(math.pi/2)2 O' m" L$ O& u
- 1.0' r5 h/ J5 v7 x6 R7 K& H; Z4 F" o
- >>> math.sin(math.pi/3)' H& v4 V. {6 U/ J/ r, }. R
- 0.8660254037844386
复制代码 ) V" N. i" W/ `
math.cos(x) 求x的余弦,x必须是弧度
+ B9 _/ a3 g; r6 Z/ L- #求x的余弦,x必须是弧度6 C$ W" l X% \2 T# C
- cos(x)3 V9 s5 X9 [2 p* u
- Return the cosine of x (measured in radians).
; J, y9 _4 R* T8 h% k. y& e - #math.pi/4表示弧度,转换成角度为45度/ N4 Y7 A/ m! q. I( G+ Y
- >>> math.cos(math.pi/4)9 s& E) r+ F2 E; b
- 0.70710678118654768 r( y/ }' c0 i p2 h \
- math.pi/3表示弧度,转换成角度为60度
; d9 z5 x4 k/ k- J2 u - >>> math.cos(math.pi/3)+ J6 E- L+ N9 ~7 {3 a
- 0.5000000000000001
8 ^; K% R1 N ~! q& z' k - math.pi/6表示弧度,转换成角度为30度
1 h8 w: n8 l# g! i: v - >>> math.cos(math.pi/6). p; t2 |3 _3 o0 q
- 0.8660254037844387
复制代码
4 c: t( i! x% M" M- gmath.tan(x) 返回x(x为弧度)的正切值
5 N. `' C( L' v- X% b6 f- #返回x(x为弧度)的正切值4 Q: _; I% i! P$ C, s+ Y4 k- O
- tan(x)1 h" X0 R7 \/ Y4 S6 Q
- Return the tangent of x (measured in radians).
9 c' b) ~; d, i+ `& C - >>> math.tan(math.pi/4)8 O" h; ?6 f$ {! {# Q. L
- 0.99999999999999999 G R! \2 S2 b# d: t4 W" r) Q
- >>> math.tan(math.pi/6)
1 f" J) M1 |- `- Q, w3 H3 h% q - 0.5773502691896257 e5 R" x9 ^2 S9 w( n
- >>> math.tan(math.pi/3)9 E: }! c( v) o; g6 P% d
- 1.7320508075688767
复制代码 / q- Q! @2 @- V% ~
math.degrees(x) 把x从弧度转换成角度
& g: W# w7 u7 @% T- #把x从弧度转换成角度
% Y6 n3 C' r0 c V/ z4 i - degrees(x)- M9 w& s; l7 X2 R, g0 _# l+ K# T
- Convert angle x from radians to degrees.
& o1 C# b9 O) B; J) X- N
% J' x" ~' {( _/ f4 Q4 S- >>> math.degrees(math.pi/4)
% i. D: c; i4 _4 ~# N: I# H - 45.0
3 |5 O. {. K% ] - >>> math.degrees(math.pi)' Z, O' P0 v5 N9 \* P
- 180.0
) H _4 X2 ^9 G( q1 E9 o, T! x6 \ - >>> math.degrees(math.pi/6)
E) t' n8 V. T$ O# M3 Y - 29.999999999999996) b$ r M; R' ?+ q
- >>> math.degrees(math.pi/3)$ N2 ^7 C3 b( ?1 H$ L* @4 S
- 59.99999999999999
复制代码 . e) \7 w% @4 Z. B+ k% ? T
math.radians(x) 把角度x转换成弧度
5 [* h! W6 z! g. Y- #把角度x转换成弧度* J+ J8 C0 V) L1 v
- radians(x)
& Z$ G7 D; n6 _% r3 Y, n3 p2 { - Convert angle x from degrees to radians.* t4 m- f& ?$ v
- >>> math.radians(45)* U6 X" R% G. j. n
- 0.7853981633974483) o: e3 C" q; i3 ?; V& A
- >>> math.radians(60)
0 p: {+ V3 H1 X% t) Y& W- g - 1.0471975511965976
复制代码
# u" D4 X4 o, v, x) W* r, {math.copysign(x,y) 把y的正负号加到x前面,可以使用0- t( I+ v( C! ~$ b# y& P( N I$ P
- #把y的正负号加到x前面,可以使用0
0 b( Z' @: l4 m, k6 E4 R4 | - copysign(x, y)
9 _+ h: @! @, J - Return a float with the magnitude (absolute value) of x but the sign
9 o! y) C1 p* C# J$ w. p - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
7 }& d1 U; p+ B( h7 X( E' n - returns -1.0.
7 t* e' P& J$ e+ I - # u, g- N! U9 Y0 b9 L5 k
- >>> math.copysign(2,3)6 U; c2 t, ]' F$ y- ?5 ~
- 2.0
) Q; K% z- j8 i. n/ `" I$ {4 n L - >>> math.copysign(2,-3)2 A2 F/ {/ V* w# T& q. {4 V2 R
- -2.0
& Q8 @' ?2 E2 j$ K8 K2 n - >>> math.copysign(3,8)6 x+ i8 [1 @" T! e. G1 D5 @0 ?
- 3.0! I F. R8 @4 |) s( `
- >>> math.copysign(3,-8)
2 ]! w H- A; q5 f+ j7 b - -3.0
复制代码 ; v |6 \6 Y/ j
math.exp(x) 返回math.e,也就是2.71828的x次方& e1 ?# `6 u- ?8 d8 G4 o
- #返回math.e,也就是2.71828的x次方' a$ b6 \& Y8 {/ F9 W, u
- exp(x): O! |" [; J {6 O) Y6 g a0 Q
- Return e raised to the power of x.% m6 F3 v# R' f7 y T+ h
9 n- t- h& F& @9 m# b- >>> math.exp(1)! s3 D/ p! R* R* _% Z" n6 Z# A, \
- 2.718281828459045
Z7 J, B& U' @+ V0 H) \9 @ - >>> math.exp(2)
* H. \+ e! I* J5 a- { - 7.38905609893065" e* r- U" {+ P# Z% u
- >>> math.exp(3), d1 T' Q0 o7 i9 J9 W7 u
- 20.085536923187668
复制代码 3 @! E& Y4 n( }& V- _$ L" x
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1% _/ v# k' T r' Z6 m x* Q, V6 I
- #返回math.e的x(其值为2.71828)次方的值减1
: e; P* V `; X4 ? - expm1(x)
+ m& ^3 Z* P) X5 M - Return exp(x)-1.! R2 E6 c2 d- k+ Q* ]+ }! W7 Q4 D
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x./ w J# y; o/ w0 y3 @' a
3 g. {( P1 u) w- >>> math.expm1(1)
/ p% J6 H# X h5 J& t& b - 1.7182818284590454 n' g8 p& V2 b& m% i
- >>> math.expm1(2); V9 |$ |1 A/ r0 f1 q2 K# o7 p
- 6.38905609893065+ I2 t: G8 M$ o& I% o4 Z q/ F
- >>> math.expm1(3)% J9 T0 D$ N5 ^
- 19.085536923187668
复制代码
$ s# u3 e N. qmath.fabs(x) 返回x的绝对值" I4 b! G- a2 Y) F% @4 n) _/ T% }
- #返回x的绝对值
$ d" ?3 A$ b* L7 n: c& v7 d. r$ v - fabs(x)
$ t8 Y; Y5 ^3 ~' O' z0 a' a+ ? - Return the absolute value of the float x.
3 ~8 M& o3 [6 d) W1 G6 b/ x; \) ? - A, h+ N, d+ Z* S: j
- >>> math.fabs(-0.003); S6 i& U1 @1 W& g/ F
- 0.003
; R! a2 D; H2 M - >>> math.fabs(-110)# V: ^: w5 M8 K% M! h7 S
- 110.0
- k! I- ~2 K7 c6 d7 d- F+ ~' F, t' d - >>> math.fabs(100)0 g7 ^* x+ l) @
- 100.0
复制代码
1 j0 [" h4 y! y! s4 h; d- d# r Zmath.factorial(x) 取x的阶乘的值
- |6 f" `0 J4 s! ^! {1 U8 S6 m! w* _- #取x的阶乘的值
: ]" C$ B9 L+ l V1 H8 N. X - factorial(x) -> Integral
# ~' n- D8 l/ @9 o9 I+ S$ g2 w4 v: D8 i - Find x!. Raise a ValueError if x is negative or non-integral.
( t9 i. `5 Y% O# P - >>> math.factorial(1)
: _( Q& H" H: X `" d - 1
; C2 O# u! g8 s7 ` - >>> math.factorial(2)/ v, k$ T' U; w
- 21 M" V/ N6 x. Z0 f0 E1 g& y
- >>> math.factorial(3)9 C2 B' s. Q4 e P5 b; V" T
- 6# \7 S3 c7 M0 m
- >>> math.factorial(5)
3 c$ R& r4 T/ F4 ^ - 120
: p, ^! i: v! ]. o& P9 g9 N - >>> math.factorial(10)
7 ~/ f& n D; x$ Q8 l; J - 3628800
复制代码 - Z/ i7 t/ j' K7 h5 [: `2 a6 A s
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
$ `% p% Q- L" z7 a# e3 I' e- #得到x/y的余数,其值是一个浮点数! p2 S6 f8 _ A/ C9 f
- fmod(x, y)
6 y) H8 g% j+ j- m - Return fmod(x, y), according to platform C. x % y may differ.4 w: T; O7 N% h9 r( `" u# U
- >>> math.fmod(20,3)) G' v& G; n9 n4 a6 `9 B2 r
- 2.04 {! s7 Z1 @- H: w' l3 `
- >>> math.fmod(20,7)
% n- k1 r& Q3 c2 ], {+ i; h - 6.0
复制代码 * B. I" d% w& o ?
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 b5 Q/ p0 \* K6 L1 e" Y0 w
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,& D: ?: o/ V8 O7 _; D! |2 c6 \
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
# o5 x1 `$ l2 N1 f" R D b4 i* Q6 A9 m - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
; y7 C, M1 @) m - frexp(x)3 a* i( t Y) Q5 r9 l
- Return the mantissa and exponent of x, as pair (m, e).' I) D! j+ c: h
- m is a float and e is an int, such that x = m * 2.**e.& C, E% _) _9 T, U
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.! l, l( w y( V& @2 A! a3 {! r6 ]1 X
- >>> math.frexp(10)- i9 @) P/ r0 V7 t
- (0.625, 4): i3 w5 S; e: ~. u
- >>> math.frexp(75)2 ]* W, E+ f! E$ K0 B* o' m) w
- (0.5859375, 7)
1 g9 C, J$ v% ] - >>> math.frexp(-40)* ~" W( \4 b. l. z! S- L T
- (-0.625, 6)
9 r! r. w7 |& \+ \* @6 V T) b; P - >>> math.frexp(-100); J1 K6 X1 J' {( L& g& h
- (-0.78125, 7), B8 ]6 {$ R, V+ x/ s- G& z/ g
- >>> math.frexp(100)
# X5 p4 d3 p5 W+ I( f" h0 @! L7 @ - (0.78125, 7)
复制代码 : w6 ]; y; ]6 U; {3 O' g
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)+ r" e6 k* T$ i" D* L! J6 g/ y
- #对迭代器里的每个元素进行求和操作' w# T: |) U1 T" w( ]
- fsum(iterable)6 {* ?+ }' R! ]8 d
- Return an accurate floating point sum of values in the iterable.
# X( b% @0 ?8 K% q2 N( A - Assumes IEEE-754 floating point arithmetic.
. R5 Q+ n( V* b# F# J - >>> math.fsum([1,2,3,4])
- c, P# m" U9 s2 \ - 10.0
. W" d# u3 d. _6 Q' }# U& k - >>> math.fsum((1,2,3,4))
* I& j. m8 X. M1 ^+ Z3 p" g: a - 10.0
, D; ]: c3 c7 y0 l - >>> math.fsum((-1,-2,-3,-4))
2 D' C, E4 `, y7 Z+ K. U - -10.0( r1 Q; }8 d) @, ]% C$ H" b- l/ p& E
- >>> math.fsum([-1,-2,-3,-4])3 n# H8 N) D) h9 I6 H4 a% p" _; d
- -10.0
复制代码
. H l( N* F* U; t" c: Kmath.gcd(x,y) 返回x和y的最大公约数4 {, h* g. W( U2 [ u" C
- #返回x和y的最大公约数$ e" _# ?# n$ |" o
- gcd(x, y) -> int+ u! l) N" ]" N: g5 W9 s8 v
- greatest common divisor of x and y8 S- z4 f9 ?+ u/ R7 w' n5 W+ z
- >>> math.gcd(8,6)
R& _4 Y7 [3 |( \ h4 [; F! V - 2
% N. c* |, A4 \$ p5 E$ N - >>> math.gcd(40,20)% |+ H9 J0 }- {4 J5 u4 {
- 20" ?' b6 e! C8 D, ~9 T
- >>> math.gcd(8,12)5 u3 q: O+ j# n( ^! U
- 4
复制代码 0 U% P) S( [% ~7 M$ G/ v, \) J7 H
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False! j2 P9 j g$ K6 L" l
- #得到(x**2+y**2),平方的值, y3 G( G% t/ n
- hypot(x, y)
! z1 N5 F6 b! V% t - Return the Euclidean distance, sqrt(x*x + y*y).
0 \7 l2 W* B1 C) { - >>> math.hypot(3,4)
- @% L& F9 C: b- M - 5.0: l7 |9 F9 W+ f+ p
- >>> math.hypot(6,8)
2 B4 o9 Z. s0 I2 Z; O* ~2 E' [ - 10.0
复制代码 , \( ?0 \+ a) a, Z! B9 \5 i
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
g: F; W! H6 L' A- p: d o* W- #如果x是不是无穷大的数字,则返回True,否则返回False$ j8 @7 I( B) S: X6 F! A9 L: n7 G
- isfinite(x) -> bool
6 \7 Y7 a2 J) a( }) w+ X$ M - Return True if x is neither an infinity nor a NaN, and False otherwise.
' u' ~2 P9 t* h- k - >>> math.isfinite(100)
7 K# `$ M7 P# _; J - True7 t- T7 k% K5 F) s9 K7 r
- >>> math.isfinite(0)
! \: r$ [9 @: F - True
) _+ B. f8 ^. \3 ? - >>> math.isfinite(0.1)4 _& m- G7 E0 r/ X) s e
- True4 L& ]/ n3 M; L
- >>> math.isfinite("a")! D- d- Q. S' g2 ?/ V( z* _
- >>> math.isfinite(0.0001)
1 K* o) h% e- D% Q7 A - True
复制代码
% s$ r5 @! @, B0 O0 Jmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
$ t7 q8 d: C" ], G6 `- #如果x是正无穷大或负无穷大,则返回True,否则返回False
9 y2 ], M; t) H) G( ?8 k% Z - isinf(x) -> bool
# J- [5 V; L( y& q& J1 E - Return True if x is a positive or negative infinity, and False otherwise.
4 i- O. e9 f% D7 c; q; h3 ~ - >>> math.isinf(234)
7 ~' ~0 @' L% s. l" A - False
" D+ |* M" B1 _' V2 a - >>> math.isinf(0.1)
G6 c" f/ t( W# ~ - False
复制代码 0 W& z3 i2 W& V, F$ F7 y0 k E; H
math.isnan(x) 如果x不是数字True,否则返回False
! e8 F! {+ |# s" f0 v3 n0 s- #如果x不是数字True,否则返回False
9 }% c# Y8 }& i/ ?/ Z - isnan(x) -> bool6 M! m' A; p0 O, v, g
- Return True if x is a NaN (not a number), and False otherwise.
2 o( E- c* X5 d) o' h) q. W - >>> math.isnan(23)% K [/ R/ ^* a" `% @
- False
2 M% V6 _0 \* |% K7 P - >>> math.isnan(0.01)
# _3 d/ P1 s& ^. A6 T* a+ Z - False
复制代码
' }1 C; k* V' v3 `5 N, xmath.ldexp(x,i) 返回x*(2**i)的值
# C% ` A! q b6 O5 d( m6 K/ W1 e% E- #返回x*(2**i)的值
1 l9 e% u9 a4 |. a" i0 U - ldexp(x, i)/ o* W5 W4 K+ t4 Y4 P7 b
- Return x * (2**i)." \ j5 l! _$ O& V9 B c* p: ]+ X
- >>> math.ldexp(5,5)
: U1 T- Q* M+ H* S+ a- e% T" l - 160.0
" z) |* Y2 n" N$ m. J+ o' W$ o - >>> math.ldexp(3,5)! d3 t' G% i' p; e/ ?% d
- 96.0
复制代码 4 s! t! m: r4 R! C5 p8 F, i
math.log10(x) 返回x的以10为底的对数; t5 D: a# d2 m" O& o6 k
- #返回x的以10为底的对数
% w3 z: X; t6 ]" R6 Z- R0 i: a - log10(x)1 O: E! x* s, }1 f# h- P
- Return the base 10 logarithm of x.0 w7 w+ \" V# a5 p) [# e& a' [
- >>> math.log10(10)
- d# j- T% x( F4 T/ V( \ - 1.0; I) M; ^# e( K1 n6 m
- >>> math.log10(100)9 a, h0 [! N8 X2 b' k
- 2.0& Y, m# Y. _+ R G
- #即10的1.3次方的结果为200 b5 v) G$ [- N4 V* t; j* Q; o* s
- >>> math.log10(20)5 [5 A0 r% N# O$ X
- 1.3010299956639813
复制代码 7 C5 K! d* t. L% w; k" h
math.log1p(x) 返回x+1的自然对数(基数为e)的值
; P" j! m# q( z& A" e- #返回x+1的自然对数(基数为e)的值+ h% l/ {* ]* R& R& ~
- log1p(x). S* I! a+ O) I0 R( G/ k" d; ^
- Return the natural logarithm of 1+x (base e).
5 U2 F% U% a9 v( Y, u+ Y; V7 p& | - The result is computed in a way which is accurate for x near zero.
, w! N, r! ^ O( z; Y5 i9 A - >>> math.log(10)
6 J* C& w' n/ Z! I8 k( `; n3 _ - 2.302585092994046
4 _) ?5 |. g' @+ V2 r! G* M - >>> math.log1p(10)7 I; V/ _; L! R2 Z: d3 j3 u
- 2.39789527279837074 E2 d! g) j/ C# c
- >>> math.log(11)
- b1 M$ n6 t+ U! V; s2 m3 O - 2.3978952727983707
复制代码 & o- e9 \4 J& m2 A( J: {
math.log2(x) 返回x的基2对数
8 B. T0 t$ _5 a) g8 `. Z- #返回x的基2对数% q/ o* H; e5 D. E
- log2(x)
4 d& G' `1 {# c0 @( j - Return the base 2 logarithm of x.
7 u/ X9 ?& E; }$ y: h - >>> math.log2(32)8 S' D) W: o% [! O' }( J! F4 B4 x% |
- 5.0) _7 S5 k, k9 i5 Q5 O' T
- >>> math.log2(20)
: P+ p% `0 Z' G/ i9 T- E" [; K6 ?6 n - 4.3219280948873635 T. F3 s, s; j0 s" ]
- >>> math.log2(16)
# [7 ] P* ?3 `7 ~0 r5 g - 4.0
复制代码
, i3 v3 k9 g7 Y6 e/ {math.modf(x) 返回由x的小数部分和整数部分组成的元组2 ~- b6 @# m" d
- #返回由x的小数部分和整数部分组成的元组
9 Y7 B, V+ R r1 g% s% [ - modf(x)5 K2 r: e E3 m4 A( z$ Y4 `
- Return the fractional and integer parts of x. Both results carry the sign
* `% @7 \) y! N' w9 m3 r& d - of x and are floats.
0 f4 U* g( |6 i. g: [ - >>> math.modf(math.pi)+ x) j7 y; z+ ]- z; @2 g4 w5 v$ R
- (0.14159265358979312, 3.0)7 G8 ~5 \; a; X5 q8 F4 B, m5 d
- >>> math.modf(12.34)
. ?, u5 I) i/ A H - (0.33999999999999986, 12.0)
复制代码 & v4 I. v' c) D. n' @; n( r
math.sqrt(x) 求x的平方根. F2 k" p$ v; I
- #求x的平方根+ R# @5 M, O/ h: U7 t" f
- sqrt(x)
* g( G& K0 R( c1 C - Return the square root of x.4 T6 Y& |1 ~$ [' B. r
- >>> math.sqrt(100)9 @6 i! [3 @9 ~0 ^
- 10.01 u' X( {% J' \; l
- >>> math.sqrt(16), s0 W' I: ` Y" r: X2 k
- 4.0
' B/ q) |' K( E& C" o5 O - >>> math.sqrt(20)
& O! V4 O2 W9 z* u7 [, t' S - 4.47213595499958
复制代码 % U2 I. W& |) U9 d4 X# f5 k
math.trunc(x) 返回x的整数部分- #返回x的整数部分
& \7 a! W* }9 r& N - trunc(x:Real) -> Integral
. f3 U/ q" C0 y: [' C+ W& a - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.4 h8 C3 B" o0 o
- >>> math.trunc(6.789)
6 O) M$ f9 h9 Z" y0 _ - 6! F# @8 a" c: t$ D- Y4 Y
- >>> math.trunc(math.pi)1 [+ ^7 \) z5 o2 W0 n
- 3
: e$ T$ E4 [" k0 k - >>> math.trunc(2.567)% Y/ v' y1 M) H: @1 z- }4 R* b8 b
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|