马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
# h/ j; }' C' R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。$ C# O# ~# Q# P" w0 K( }
( m5 c) r9 [& W8 m6 k0 i- g: H
方法1:! m7 T. J/ O) r" N E3 z
- >>> import math
! B/ @( o0 C4 l& N& w - >>> math.sqrt(9)
! j, Z: Q* _3 j. X( V! r - 3.0
复制代码 方法2:4 T) S; w( M( E8 e
- >>> from math import sqrt
$ p( Z% R4 b& A9 I - >>> sqrt(9)3 a/ M! w8 z, b
- 3.0
复制代码
, d8 Z1 f- }6 P0 E
: O' q% _ N" u7 {math.e 表示一个常量
4 K7 n& H& ^0 x# Z( s- #表示一个常量
" b$ o% m0 }; p. e) N - >>> math.e
7 [7 Z/ o" u8 P1 X - 2.718281828459045
复制代码 , f4 p; W* P! b s8 y( f+ K' K2 C
math.pi 数字常量,圆周率& S5 ]# ^ j1 T# e7 N
- #数字常量,圆周率$ F' n/ P) a1 `
- >>> print(math.pi)( x' N5 a) d; z0 c5 F. T
- 3.141592653589793
复制代码
' m& N @3 O, H/ w, {math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x) h! k- @; W4 F$ P$ F- {4 o; B ~
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x( j+ _( @5 q( c' b: Z
- ceil(x)8 | L" N R. N/ j9 H
- Return the ceiling of x as an int.; J8 ?' j9 R4 K- J5 c, q
- This is the smallest integral value >= x.
- }8 E, t6 L+ E9 C! S; }3 _
+ [ h' H$ N- I) x# [+ K- >>> math.ceil(4.01)
' q% x, i: J+ i8 G$ | - 5
# I9 e+ w( U7 [+ V% U* k' @ - >>> math.ceil(4.99)
$ H% D; y: R! y( L' q - 58 `) r+ N- B) g- z0 O. t
- >>> math.ceil(-3.99)
3 P% L6 X j: z4 v - -3
% V! s7 s: _3 a& J: R3 h - >>> math.ceil(-3.01)
: a9 j# x& d) r" P# M$ O - -3
复制代码
8 X# _3 F% b7 ]9 Z: P# S: m8 L/ Ymath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
, N/ B L X& Y& v% K. s- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身 p$ F8 V7 k4 a7 u" J5 {, g
- floor(x)
$ b/ R s: I! Y% m N - Return the floor of x as an int.7 Z5 Q9 W/ M0 ~1 ?9 r! _
- This is the largest integral value <= x.
% \( S& ~# w1 I. P$ f* w5 y2 J8 K7 k - >>> math.floor(4.1)
, Q+ V3 m) h; ?: g. e# { - 4
, R7 p1 h" `! C2 n. T; `- u - >>> math.floor(4.999)
5 _$ B; Q- p9 w - 4
7 U0 |$ B$ n x' ~2 ]7 y# a0 p - >>> math.floor(-4.999)
7 \9 I( g1 J2 b0 n5 L( ^ - -59 \& S/ A) [! i
- >>> math.floor(-4.01)7 x+ X- n `! m, } f+ m
- -5
复制代码 0 Y. {0 n2 {8 y2 W
math.pow(x,y) 返回x的y次方,即x**y
- Q3 b& ~ A# }$ Y- #返回x的y次方,即x**y
) X. p* u& V- Y, e5 D - pow(x, y)% }7 S2 W) R2 M" L
- Return x**y (x to the power of y).. a' e8 ? L8 C$ V
- >>> math.pow(3,4)5 ~. w9 f6 a& M
- 81.07 T" R& G4 A; V& \" b1 ^
- >>>
) c4 A# h$ @; \; V$ w3 A- { - >>> math.pow(2,7)
* ^$ {1 c4 ]' q6 j - 128.0
复制代码 " o& }1 J# }' m U3 r1 s! _) |
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
# \ d! m' L4 ~4 Q- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* h! ]8 g+ U5 @9 y% g+ R
- log(x[, base])+ o+ T0 m0 c; A
- Return the logarithm of x to the given base.
/ [; _4 S: t% O7 D - If the base not specified, returns the natural logarithm (base e) of x.
3 p% @; S- h! u$ \ - >>> math.log(10)
% z9 P. b6 U+ `3 M o - 2.302585092994046
; V6 B) D. L" d1 S, ]. M% K3 T( T - >>> math.log(11)
1 a8 ?) ]. s3 f* H2 w- Q7 v* ~) h - 2.39789527279837078 y3 t0 I# M0 X5 b
- >>> math.log(20) v. h, x5 z. k: G. B2 ^5 }
- 2.995732273553991
复制代码 7 c. \9 Z* G# Z, t! k7 X" c
math.sin(x) 求x(x为弧度)的正弦值
1 a. y; x& u% h" b% n1 k: c' F- #求x(x为弧度)的正弦值
' d5 L5 T" m' m9 V* J3 z# J - sin(x)% i4 f9 a0 Q6 e9 r& O5 Y2 m9 T
- Return the sine of x (measured in radians).
: M G% Y6 g3 h! {3 ] - >>> math.sin(math.pi/4)3 H& h" h; w& Z. _' v- V A
- 0.70710678118654752 h, A! Q$ y/ i. S# R
- >>> math.sin(math.pi/2)
/ }, Z8 y# Q x. e+ i8 R9 m8 j - 1.02 a, b$ h+ W, k1 @
- >>> math.sin(math.pi/3)8 ^/ V9 H4 i n3 y( k* ^2 m1 C
- 0.8660254037844386
复制代码 9 @( w5 x- G9 ^% n! L- X+ D+ U$ n7 W
math.cos(x) 求x的余弦,x必须是弧度
! {- U% y3 l# x$ {- #求x的余弦,x必须是弧度
& H% r2 k$ P; ~, l( C: y5 D$ R - cos(x)& g! t- w3 m: X$ v& U
- Return the cosine of x (measured in radians).! I8 S) x" N- F Y: v8 i
- #math.pi/4表示弧度,转换成角度为45度
& p' x) r+ l/ r/ B% v* ~" F - >>> math.cos(math.pi/4)
2 k8 z+ G2 {. U+ M ~0 z - 0.7071067811865476% B {+ ~) d& Q! n: E
- math.pi/3表示弧度,转换成角度为60度5 O! ^& R# y& k4 C* H: C+ z* {* P
- >>> math.cos(math.pi/3)
" A. `8 M, h3 s% r- [8 g8 w - 0.5000000000000001
# k8 A0 y, k+ Q - math.pi/6表示弧度,转换成角度为30度- L; L( r+ k9 k1 ^ F! s2 h
- >>> math.cos(math.pi/6)
* y- ?: q* @7 \( [ - 0.8660254037844387
复制代码 5 @. D! x2 S5 S$ [2 | c
math.tan(x) 返回x(x为弧度)的正切值
; b/ z" d8 ] J4 P& v4 g+ M' V4 f9 N: Y- #返回x(x为弧度)的正切值* q! h" e c* o% Z2 Q$ y3 m0 Q
- tan(x)2 }" h8 D K' G# g, O$ U
- Return the tangent of x (measured in radians). k4 |& p* c4 y0 G: X- d- }* a( g
- >>> math.tan(math.pi/4)+ @" ]5 I- E9 l
- 0.9999999999999999
( `+ I1 f0 }7 C7 f5 R - >>> math.tan(math.pi/6)
& D# ]3 t: p6 \9 p - 0.5773502691896257' B( `4 M1 c8 R$ L, Q7 o7 f4 F- K
- >>> math.tan(math.pi/3)* y g! Q" V; x: y; W7 K
- 1.7320508075688767
复制代码
! m3 V) G* [3 s+ k3 H5 lmath.degrees(x) 把x从弧度转换成角度
' N6 G2 a+ I' g0 ]6 n- #把x从弧度转换成角度
3 {6 P0 X+ y. y7 o/ K4 p. P - degrees(x)" e$ `+ f# E X. i9 L
- Convert angle x from radians to degrees.% C2 _! ^1 V( g" O, k# j! u
V {: ]/ B. O4 u& T, C- >>> math.degrees(math.pi/4)
# }, x: Y, ]5 K/ |0 ~ - 45.0
2 ?1 c2 O2 t b9 h5 g+ }( n( [4 e* a - >>> math.degrees(math.pi)
) T8 e7 ^1 i2 U3 a$ A- g - 180.0! A2 p) r; W$ @) I# C9 ]
- >>> math.degrees(math.pi/6)# Y" H' D* n( S; o+ _
- 29.999999999999996* z; o' J) Y7 r& R8 z' |9 c
- >>> math.degrees(math.pi/3)
; J& `$ M0 }* e, j# T - 59.99999999999999
复制代码
; }( s d) L. Amath.radians(x) 把角度x转换成弧度
( Z2 D* M! U2 I! s* {2 b! {! p- #把角度x转换成弧度) ]% L( d/ i4 S* L& X
- radians(x)
- _. |. W* L) C' A: H' a- W \, Q& ~ - Convert angle x from degrees to radians.
2 \! F8 x) ]* d1 q; s6 j; U - >>> math.radians(45)# r6 t! ], r5 R) ~$ v5 h- y
- 0.7853981633974483
8 n7 A+ b) C" S |, n* l7 K - >>> math.radians(60)
3 q$ ~; i1 |3 P# ~( H" ^, T - 1.0471975511965976
复制代码 + ^7 A8 ^( ~1 V; v6 o# B( j
math.copysign(x,y) 把y的正负号加到x前面,可以使用0" X+ p/ a! E* I {. U: c
- #把y的正负号加到x前面,可以使用0 C. B. o2 [ d3 }. _- u
- copysign(x, y)
* a% b7 u6 }5 o6 f9 h9 t* h - Return a float with the magnitude (absolute value) of x but the sign W) t9 y, r" V w$ C6 D+ U. I
- of y. On platforms that support signed zeros, copysign(1.0, -0.0)
; Y; s5 R! L: Y- u - returns -1.0.( i$ `3 a5 m8 I1 w
6 F8 s! J. R* X! n3 W6 h- >>> math.copysign(2,3)- T: K0 o& ]( ?
- 2.0% R/ d; p5 @0 ~2 \6 f; V7 C" L
- >>> math.copysign(2,-3)" ]3 M( Q1 A$ m
- -2.0
; y0 ]/ k" V! s m, | - >>> math.copysign(3,8)
5 k9 M' [- U' Y: w( D5 B; B - 3.01 B9 d' {1 Q6 A! C* X3 b$ n
- >>> math.copysign(3,-8)
G @& _' o5 C3 l0 t) | - -3.0
复制代码
0 e* ]6 \$ u" x' ?5 l1 U, ~math.exp(x) 返回math.e,也就是2.71828的x次方
4 E5 x* k5 D4 }2 l- P- #返回math.e,也就是2.71828的x次方1 u+ a- v, M+ v$ q
- exp(x), j; p- s+ \) @7 X" `. V6 A
- Return e raised to the power of x.% p% k5 I9 e( B6 y6 s2 K
- 7 E! L* ?* N# g5 z3 _# z
- >>> math.exp(1)
5 k9 R' x4 a5 k8 {4 E, c+ ] - 2.718281828459045
: t' i* \% ^7 b) b - >>> math.exp(2)3 I1 S5 j$ ~+ o3 ?! x, ^3 ~% Q
- 7.38905609893065
( n6 e" J$ |) X/ |: o - >>> math.exp(3)
8 i3 y( \" e& J* f# @& U - 20.085536923187668
复制代码
- ]% z' P k( ~& |9 d# {math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
( X$ e d% |8 Z7 ^- #返回math.e的x(其值为2.71828)次方的值减1! \" F) H+ ^$ ? w' i% i
- expm1(x)
3 C0 ~& w$ c5 O; ?: k1 r - Return exp(x)-1.% d# N7 f# @* E3 p. s& u0 P
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.5 G0 L: }' [: c3 v: H5 Y
9 C) |" B0 n% f- >>> math.expm1(1)& P$ R- W6 [- Y Z$ R
- 1.718281828459045
9 @8 J. ]" n) ~$ Z - >>> math.expm1(2)
9 P o3 M e- |' h+ D9 c - 6.389056098930652 e$ A/ l' t- k# k2 Y/ C# T0 R
- >>> math.expm1(3)
# w0 m- ~7 T3 C1 w8 t. d8 w - 19.085536923187668
复制代码 ! g) U& q; l* z1 P' |0 |
math.fabs(x) 返回x的绝对值3 V/ W; A8 V) C, O6 m1 \
- #返回x的绝对值
x- c; m% M! S) N! E) U - fabs(x)
5 ~( E- Q/ Z2 n6 ?( z& D' O - Return the absolute value of the float x.7 z4 J7 b" k, D2 I" o; w- J5 J
" Q7 D, J" G. ~# E1 G& L- >>> math.fabs(-0.003)1 j+ n" T* E7 o1 B1 v6 L2 V
- 0.003
" t2 j3 [* p& g$ m* l - >>> math.fabs(-110)
( h; w! o$ A) [5 \! W8 u9 s" a - 110.04 q0 a* ]. {+ ~$ \4 x4 p! X) Y1 [
- >>> math.fabs(100)% T) q3 x |' u* g/ {- ~
- 100.0
复制代码 8 g( x8 @5 {: s: P5 N
math.factorial(x) 取x的阶乘的值
2 [7 x6 q9 n2 _/ E- #取x的阶乘的值
; T+ ?* L4 R3 r" ?: O) v - factorial(x) -> Integral
( [' |, W, A7 p% A* f4 u - Find x!. Raise a ValueError if x is negative or non-integral.* a3 h1 h2 y. U. \4 Q
- >>> math.factorial(1)
6 u8 w' E9 F9 f) p - 1
- q' M: y0 c2 m9 G5 V9 \ - >>> math.factorial(2)
6 a/ A7 I$ `0 t3 K3 i - 2, i. K& g0 T# }# Z% R4 R( ^
- >>> math.factorial(3)/ x" J7 P% ~ V6 ~
- 6
# Z* p% R/ n6 Q( A9 T$ _ - >>> math.factorial(5)
% _4 Q3 m/ ?* i/ U2 ^) ]( ?( Y- f5 H - 120
2 D3 ^9 G0 J- x' H v5 X - >>> math.factorial(10)
. c4 A4 B* Q& Z2 d) T - 3628800
复制代码
5 @: X ]$ l" ^) x: O4 S) j& @math.fmod(x,y) 得到x/y的余数,其值是一个浮点数2 Y0 K4 t: _1 e$ G! \
- #得到x/y的余数,其值是一个浮点数
% d; f B+ f4 J9 _. R2 V - fmod(x, y)
# u H9 K7 x( V5 k, r3 S - Return fmod(x, y), according to platform C. x % y may differ.8 O: u, X, J# S" o( i: s. G' e
- >>> math.fmod(20,3)
/ m3 u& |: C+ X; y6 Q - 2.0
7 w4 }0 ?. _$ S* A o - >>> math.fmod(20,7)8 D& }/ W3 C3 r9 t- u1 h, x
- 6.0
复制代码 ( t* l" Z" u) I2 q. u( P S
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
/ d4 r* t9 u( v' i% Y1 Z. y6 E- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,3 z1 k2 l7 h. x) c1 j8 [! T- ]* i
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值6 N7 r' _0 ~6 v- T) q3 `
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 ?: C0 t$ r0 O
- frexp(x)5 {4 L8 q0 G0 r/ G! v
- Return the mantissa and exponent of x, as pair (m, e).- K4 q. U9 v6 Q0 d
- m is a float and e is an int, such that x = m * 2.**e.( w o. M' B. d6 [
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.0 Y! T6 i6 B+ s4 z0 z
- >>> math.frexp(10)3 R& m. ]3 x0 N. s
- (0.625, 4)2 @+ T- f/ y' j; F; g+ ^: _3 U
- >>> math.frexp(75)
/ O# W j) H5 q, f - (0.5859375, 7). u* Q5 G# D+ }7 i$ N& A0 I
- >>> math.frexp(-40)
) e6 }3 `4 ^; t - (-0.625, 6)
3 y; C [- P' [7 I - >>> math.frexp(-100)
& C9 u2 t" \( f! k) F: n - (-0.78125, 7), Q' u4 d- t3 q4 \. J: z
- >>> math.frexp(100)
5 c d" }: _7 ]9 r C - (0.78125, 7)
复制代码 . R. Z3 S1 V/ y. _2 U; w
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列). S# F, g- a8 h
- #对迭代器里的每个元素进行求和操作
- t$ u: Q- D6 W& Z4 y: X - fsum(iterable)
' u3 g( k. `! B' w - Return an accurate floating point sum of values in the iterable.# H; V- P* k4 b: N& F3 u1 N# Q* O
- Assumes IEEE-754 floating point arithmetic.: i, [3 W3 v- S0 q* g& \8 L5 I
- >>> math.fsum([1,2,3,4])% P7 f! r: ?- @* R6 ?
- 10.0
8 r# \6 n& y0 U" L - >>> math.fsum((1,2,3,4))
" d- J7 D& j9 D - 10.0
) i0 l1 @; V! q7 _: S1 z6 R - >>> math.fsum((-1,-2,-3,-4))
. u4 @2 D5 M h/ g - -10.03 V2 \9 V1 O l# ~: M$ S
- >>> math.fsum([-1,-2,-3,-4]) d6 z/ b7 r4 S1 ~. t
- -10.0
复制代码 . P! U* y i8 l0 i% B- [
math.gcd(x,y) 返回x和y的最大公约数. q) I G# d3 |6 t$ t' s) I4 O/ u
- #返回x和y的最大公约数
" l0 z% L: d, z; _9 G - gcd(x, y) -> int1 W. Z+ V2 @" w1 S
- greatest common divisor of x and y7 a; x, q1 \8 I
- >>> math.gcd(8,6)9 l4 g0 g' B J; i
- 2" Y9 V: l) ^7 q$ `* U8 {' M- q# @
- >>> math.gcd(40,20)
/ W4 v c/ h, R" E* i - 20
8 J/ O3 A% s4 x* b$ f - >>> math.gcd(8,12)4 ^) i' S; x; N: B. M& S: g1 M
- 4
复制代码 * W$ G1 {% w$ M, }* P, }
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False' H, |- o- Z+ x6 G" M
- #得到(x**2+y**2),平方的值( g( K3 T, n# r- J( W
- hypot(x, y)
9 x3 t, b( z( m% b7 ?1 K - Return the Euclidean distance, sqrt(x*x + y*y).4 P, U/ m* z) k# }/ S4 m
- >>> math.hypot(3,4)
, N( s/ @8 ^6 W$ ] - 5.0
' u+ B7 B/ V# N% {9 |8 i: l - >>> math.hypot(6,8)
* B( E! G& @) Y A; _7 \ - 10.0
复制代码 + U+ j! E: }( Y- F7 [
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False; N* a( \4 ]& c
- #如果x是不是无穷大的数字,则返回True,否则返回False
5 a* D. B: E/ S) B& L - isfinite(x) -> bool
/ B+ z5 `& F6 G6 ^7 M5 T$ m# g! _ ` - Return True if x is neither an infinity nor a NaN, and False otherwise.
+ W3 @2 z9 i5 j4 \& c - >>> math.isfinite(100)$ ^% c B6 C; c7 C5 ]3 W4 ]
- True! v$ t* w) n% t
- >>> math.isfinite(0)5 a9 a0 ]$ g6 g2 H3 [8 [
- True! \: @* d$ v1 D2 d# R. I/ U! D
- >>> math.isfinite(0.1)
: i( e* r m3 w6 I& j% ` ] - True9 d8 K, _$ y& z( w$ f
- >>> math.isfinite("a")
; P9 h* E# l5 C/ F0 e1 T4 [ - >>> math.isfinite(0.0001)$ _+ @ r' t# }! L6 l7 G8 e
- True
复制代码 ! {! ?5 @8 o0 R) |$ }
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
. i9 B) @! E8 K" H- #如果x是正无穷大或负无穷大,则返回True,否则返回False
# E0 N9 {4 v# F( j& Q: v/ w - isinf(x) -> bool! Z% Y3 i, V, W7 s( r- ?5 O1 N' i
- Return True if x is a positive or negative infinity, and False otherwise.% ?/ @9 t8 v3 |* Y! j* U$ R
- >>> math.isinf(234)* S U% H8 _0 w: m2 R# `
- False1 b1 j3 S: J* r$ E+ D3 I' Y3 h
- >>> math.isinf(0.1)
" \: \0 J+ H! i0 d, \7 o5 K - False
复制代码 # R- b# D% B4 Y. }* w
math.isnan(x) 如果x不是数字True,否则返回False8 b( W% ~7 T4 \2 a
- #如果x不是数字True,否则返回False
+ Q# R4 L9 H0 A4 A4 `0 q; N6 S3 p' O - isnan(x) -> bool& d! _+ C s. V+ `& W3 Y6 P" C8 I
- Return True if x is a NaN (not a number), and False otherwise.6 k6 L; @/ b4 e& T: K4 a; M
- >>> math.isnan(23)
9 I' Q% a& n# c/ O1 d; c7 x/ x - False8 r6 d) H" r& b0 ?$ C. `% O5 |$ _
- >>> math.isnan(0.01)0 f- f! _: b- } E: Q/ D
- False
复制代码
6 Q% M; p' H) n1 A% xmath.ldexp(x,i) 返回x*(2**i)的值
4 {- n( P. a7 ~ n* A( [- #返回x*(2**i)的值. f. Q& e! x. ]' j) Y6 D: p; P- t
- ldexp(x, i)
3 l0 ^0 r" H$ P3 J9 @" ] - Return x * (2**i).
9 \5 j' u' D" Q1 c4 m$ I - >>> math.ldexp(5,5)/ B9 H; J5 x; j% N/ k O
- 160.0
6 w Z; P7 b4 { - >>> math.ldexp(3,5)" B' T6 C" Q+ N. e7 p1 O+ L
- 96.0
复制代码 8 a* u! {$ w& Y' c2 ]9 O
math.log10(x) 返回x的以10为底的对数
j$ } [9 i- q+ @* j/ a- #返回x的以10为底的对数
) a/ R3 R3 I: Y+ h; T/ p - log10(x)* p; K4 K; Q0 f
- Return the base 10 logarithm of x.
- | K: U+ E6 M# P* u& ^ - >>> math.log10(10)' {8 L1 N4 @/ I3 V; }
- 1.0
/ M1 K6 t/ G3 t - >>> math.log10(100)
: r7 d9 D0 j- a6 [ - 2.0
% G. H4 w0 D. [) J - #即10的1.3次方的结果为209 V" x! B# m! f6 k- D$ V$ l
- >>> math.log10(20)
& F0 S4 n; S; Y/ ~! u - 1.3010299956639813
复制代码 9 u) e" b, v3 c7 J# x4 p6 \. ^- x
math.log1p(x) 返回x+1的自然对数(基数为e)的值
/ l4 F6 e3 |& a/ W, Z- #返回x+1的自然对数(基数为e)的值, B& B+ W( C, z6 {: |7 o) `
- log1p(x)
2 o# }2 F* f% b0 b( T - Return the natural logarithm of 1+x (base e).
6 n6 F: l2 ^, h* }5 A) E" P5 v - The result is computed in a way which is accurate for x near zero.. {+ D0 P. p0 N% e) I: {/ S
- >>> math.log(10)
+ ^% r2 B% E, [& @) {. ^ - 2.302585092994046# d0 p# x3 N6 m8 E2 u$ u+ K6 v
- >>> math.log1p(10). i( E- C" u0 h% l4 h6 j6 I$ S& }: p
- 2.39789527279837073 B: W) v$ i6 H7 X! C
- >>> math.log(11)
' ]* ^8 ~- x. `1 h: t0 r- k* R - 2.3978952727983707
复制代码 ! R [4 y: n0 g. d! J8 b: c
math.log2(x) 返回x的基2对数
3 L- L; \9 i* r6 a+ d3 \- #返回x的基2对数
, `; t' m, r4 h. ^! H3 F1 I+ ^ - log2(x)
* U+ o$ G m7 G - Return the base 2 logarithm of x.6 L# y( X6 X+ G7 H
- >>> math.log2(32)
5 }4 d5 W! ^" n ~: n* s - 5.0
# k/ \# I! w: p! N - >>> math.log2(20)1 U5 ]; {* {1 w5 u! B1 r
- 4.321928094887363, g5 C) b1 Y# Q
- >>> math.log2(16)
# b( c1 U2 n5 f9 m$ \6 _' ^6 A- y - 4.0
复制代码 7 m4 G6 e, M: V: y' t
math.modf(x) 返回由x的小数部分和整数部分组成的元组( x O, {; d9 C! W& ^. ]; j# J
- #返回由x的小数部分和整数部分组成的元组
& v: y- ?. s! n! P - modf(x)7 S0 f! H8 m# N( c: W* G
- Return the fractional and integer parts of x. Both results carry the sign# u9 l, ]8 m+ D
- of x and are floats.% T0 j F( C0 |1 O2 _
- >>> math.modf(math.pi)
- Z% A) p5 x1 t1 k- v3 R - (0.14159265358979312, 3.0)
6 q& I. K& \5 [6 z - >>> math.modf(12.34)
2 c0 q% G2 ]8 g/ a( `1 S) k( r - (0.33999999999999986, 12.0)
复制代码 ! k# h% u* j2 _8 I( y
math.sqrt(x) 求x的平方根
& J! _: T; p" k8 X- {7 p! m- #求x的平方根
L2 l2 _; w6 a" ^ - sqrt(x)" [! {; ~0 v: g8 J( d
- Return the square root of x.2 j6 n8 b) g6 {9 @' z) B
- >>> math.sqrt(100)
' J5 W8 s8 K5 B1 E - 10.0
4 R4 H4 ~3 ]% ~. l% @1 K - >>> math.sqrt(16)5 R5 F8 G3 ~ a* }3 W; t% Q& i
- 4.0
" C, H) e5 ~1 e! a - >>> math.sqrt(20); }3 L6 H7 p, h: I4 \; b# W
- 4.47213595499958
复制代码 $ i: F( c' e; z* Q: H' `
math.trunc(x) 返回x的整数部分- #返回x的整数部分; H z3 }! h9 Z# N& p
- trunc(x:Real) -> Integral& W n$ L. W) z6 l3 W4 z* X5 d# W
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.2 n7 X& ?3 d& F* X g" s* h! F
- >>> math.trunc(6.789)
( f# l" A* V* x; T D - 6. [3 Q3 |$ w( B* }+ g8 @3 _
- >>> math.trunc(math.pi): \# q5 ]) N; ?0 s" l) |; B9 {
- 3
; I4 d+ h+ U; i5 s - >>> math.trunc(2.567)8 X% o9 ?* B U- L# Y7 k' Y1 S6 q
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|