马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
- s( B0 h/ X* J2 I2 w {5 _0 X
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。2 c: m3 t- K6 s% x) {5 _0 o9 N
# b U/ A( W, _# _2 e方法1:6 j5 ~# @2 i$ l, g! }/ U
- >>> import math
. R' z2 x/ t0 ~7 w - >>> math.sqrt(9)
3 T- @% _! U' S1 Q, h) o - 3.0
复制代码 方法2:
, M9 }, a H. Q% N" R- >>> from math import sqrt H- v; M' [9 e8 i$ O9 v
- >>> sqrt(9)* l: f0 H ~0 E* ^3 C7 ~
- 3.0
复制代码
0 L5 x9 ?5 i' ~ j" B# K+ N& N: f
/ ^! g" ^6 S: N3 D) |# I& N* g% \math.e 表示一个常量
" {* s* b) U- N3 ^- #表示一个常量
/ o# [, e* Y9 O! ?- H$ Y4 A0 ?7 ? - >>> math.e
; t7 _2 `$ |4 J3 `/ Q9 K# u) q) Y - 2.718281828459045
复制代码 : X" B7 s& v( D) a9 q4 Z
math.pi 数字常量,圆周率
u2 C& ^- i% e- #数字常量,圆周率" l; a+ ^6 I! Q
- >>> print(math.pi)
' V2 x0 v( f- r. o - 3.141592653589793
复制代码
, V* c- ?8 w$ i, ^( emath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x* u/ _* p7 C4 s; R, [
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
* C& x }9 V; {2 m! B - ceil(x)$ E+ L+ z1 z# ^+ K, W
- Return the ceiling of x as an int.8 |7 V6 }2 K, Q
- This is the smallest integral value >= x.
R4 u) T1 A8 b6 l1 r
% f. I+ m- m' A+ a9 f0 u- >>> math.ceil(4.01)
5 [9 [$ F0 o- g8 J# ?7 W' P6 V - 5" ?: I8 z% Y- R
- >>> math.ceil(4.99)
; J1 h3 @/ t" {+ ? - 5" _$ a4 v5 m! Z4 J# e
- >>> math.ceil(-3.99)
; [# d4 \ m* X0 t) t - -3
H: N5 L- Y; b2 h. @ - >>> math.ceil(-3.01)0 t: S" B4 E7 a" r% U. B6 |
- -3
复制代码
5 o) t3 l" {1 N0 j# t# a; Mmath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. a8 V; X' H1 {# s$ C1 @* l* w- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
7 K( a; O" e$ S3 Q+ A; ] - floor(x)9 f3 N# h( P" }! q9 f1 s$ k9 ^
- Return the floor of x as an int.
% Y( ~: v$ K! b% K5 r$ b6 `0 N - This is the largest integral value <= x.7 G0 F' ?2 h; U. r. R7 ?6 m' T
- >>> math.floor(4.1)1 ~! o& U( V+ s4 k
- 4" ^' X" n* B5 L/ m5 g9 c4 v
- >>> math.floor(4.999)
1 A' }' P0 a M3 {) A - 45 H" k' A/ ^8 y) |. i
- >>> math.floor(-4.999). E) o+ u7 v# G$ d) s& A/ R. O% A
- -55 i4 v) _' ^7 h$ L" U2 @$ m- X& _
- >>> math.floor(-4.01): R. ?. @" P) }; A! e; k& m
- -5
复制代码
- M6 ~# r- h& f0 h! @math.pow(x,y) 返回x的y次方,即x**y$ }8 j# o' a1 J) Z# B+ {
- #返回x的y次方,即x**y6 Z# V, H5 c9 d( [, f6 ]$ \: f
- pow(x, y)/ H; T* R! J+ C9 {0 k
- Return x**y (x to the power of y).% Z7 I( i9 A: d6 U, G7 S, ]
- >>> math.pow(3,4)
+ d& s9 A+ R! s1 C& Z- @8 d# ^4 }. n - 81.0
; U' m) \3 a+ y; P5 I - >>> * Z* h- D! |* k- c
- >>> math.pow(2,7)
5 \5 i9 X3 X6 b7 ^0 W1 A+ `* y - 128.0
复制代码
' R/ A- u& o5 F3 B0 Hmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
c2 S4 k, c0 e2 H8 D; l; v H: u! J7 k- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)9 P. m( y" O1 g+ R4 |+ l" I1 {3 o+ _
- log(x[, base])
- ^8 R2 b' J d1 O2 b6 I7 ?- k6 n - Return the logarithm of x to the given base.
s0 l% A' T4 Y+ q7 M5 w - If the base not specified, returns the natural logarithm (base e) of x.
6 r# x( N1 _8 J6 u - >>> math.log(10)( x- a! N+ s$ c* m
- 2.3025850929940468 A+ X) g# l2 W1 z5 S
- >>> math.log(11). c" i" O& {8 q$ ^ l) \
- 2.3978952727983707( }; ~0 R% ]3 Q+ E b: z
- >>> math.log(20)3 e/ r# h$ {' [0 w
- 2.995732273553991
复制代码
: n$ H% e# O9 j D- Y/ w/ amath.sin(x) 求x(x为弧度)的正弦值2 V5 o% q) }* h* A. d* J8 @
- #求x(x为弧度)的正弦值
! M3 d9 _5 c9 r - sin(x)) `+ Q" u% ^& D: Q8 |/ i
- Return the sine of x (measured in radians).
9 Y4 y9 [, S! M" V- s" H, H - >>> math.sin(math.pi/4)
; l( ?3 s; Q+ s2 N( p% U$ H% B, Q - 0.7071067811865475+ N' S) Q3 K$ h* `. ?! L
- >>> math.sin(math.pi/2)
# A4 ~+ X4 J: ~ - 1.03 d% T* C4 J% }& |
- >>> math.sin(math.pi/3)- q0 p' C/ X7 n/ ^# _
- 0.8660254037844386
复制代码 / I# ?3 U3 |$ k, M" X
math.cos(x) 求x的余弦,x必须是弧度; B9 I7 } K( v; e1 T
- #求x的余弦,x必须是弧度
) w2 b# Q- _* l" i+ R( K - cos(x)% Y( w0 p/ n# V7 C8 z5 I
- Return the cosine of x (measured in radians)./ Y% z# ]1 C I6 w, ?3 p8 Z) \" i
- #math.pi/4表示弧度,转换成角度为45度
' t( h/ M2 b4 n - >>> math.cos(math.pi/4)
% }( y1 F! z E) U2 U - 0.7071067811865476* h' ^: K3 l5 `; H
- math.pi/3表示弧度,转换成角度为60度
. ^1 V, \ o/ \- x( Z z/ { X, u - >>> math.cos(math.pi/3)
! g* b- M( o" D/ a: ~7 i# R& j7 ~ - 0.5000000000000001
! I3 h* b3 f/ U( D% L l. \ - math.pi/6表示弧度,转换成角度为30度
2 |, h9 ?; T, v* t$ s+ q - >>> math.cos(math.pi/6)
) e+ Q9 Y7 c& K' C) _- b- j) c - 0.8660254037844387
复制代码
; C4 K2 R# Q* Q4 cmath.tan(x) 返回x(x为弧度)的正切值
' y/ i2 m& a+ x" a& H- #返回x(x为弧度)的正切值- x. q" s: Q0 y. j5 }, M
- tan(x)
1 j' S' _4 S. Q - Return the tangent of x (measured in radians).
. Q1 H- `/ D% E4 c - >>> math.tan(math.pi/4)
& U, d$ i' y4 G - 0.9999999999999999
0 Z8 E5 w2 o0 U+ v. L8 o - >>> math.tan(math.pi/6)
# B# K, Z6 E% i& @ - 0.5773502691896257% I) v, t! Z* F ]
- >>> math.tan(math.pi/3)4 t9 S7 x* V7 I: l U1 U( f: ]
- 1.7320508075688767
复制代码 # j! @" U2 c. b4 \4 `, c
math.degrees(x) 把x从弧度转换成角度4 V, M: d& K7 B/ p- B2 j
- #把x从弧度转换成角度 u) E4 O8 p& V, E5 x2 l) x
- degrees(x)3 ^( T k1 o# g7 J2 v; A3 c
- Convert angle x from radians to degrees.
+ p& U: [9 J0 ], _' t$ K9 a$ }6 s - : Z2 U* H: m: e: u* _
- >>> math.degrees(math.pi/4)8 b( }/ o: R; K+ u$ Z2 h
- 45.0
; u, E8 O# e7 ?" J. [, t3 B - >>> math.degrees(math.pi)
# }7 t4 V1 \ R9 _! C7 q1 u - 180.0
% C4 q3 b0 R4 R: S - >>> math.degrees(math.pi/6)
* }3 D7 p; X9 _6 T3 T6 Q( h - 29.999999999999996
, Q. h( ]0 }$ a - >>> math.degrees(math.pi/3)
. i! z5 p; P( b% T% Q& X5 ] - 59.99999999999999
复制代码
( j: T! [, H. O: K" S. emath.radians(x) 把角度x转换成弧度
" B9 n/ l2 ~6 E7 ]. i- #把角度x转换成弧度
& C2 Q# t9 ~2 e6 U( O( k+ n# P' p% m - radians(x)
% c N. {* D; D2 A" B - Convert angle x from degrees to radians.
2 n. r* }. u; v& w! h2 w' f0 E; W - >>> math.radians(45) X# ~# q( ~7 |" H9 C
- 0.7853981633974483; ]$ [) v- }4 E) s2 z, ]
- >>> math.radians(60)2 a& T6 H1 A3 K ]5 I; ~$ L7 B
- 1.0471975511965976
复制代码
. [+ W u' @) p# r# Omath.copysign(x,y) 把y的正负号加到x前面,可以使用0" A9 R9 d- ^; v4 {6 {- n% `& ^
- #把y的正负号加到x前面,可以使用0
" W& e' I" B1 h3 i2 [$ |: M3 K5 G - copysign(x, y)
4 H. k$ Q* M+ F$ Z! B - Return a float with the magnitude (absolute value) of x but the sign
& W! N7 i' B0 {! w- h - of y. On platforms that support signed zeros, copysign(1.0, -0.0) 1 O# P: u7 X7 ]
- returns -1.0.' H0 k6 V* L# \# O: Q |5 S
% G$ m9 E- C0 Z6 o- >>> math.copysign(2,3)) V4 g& L6 \, g; } j& O5 f
- 2.0( D: J0 S, b, M8 m0 a. Z
- >>> math.copysign(2,-3)1 r& d9 V9 e$ U! _% Q" k
- -2.0
2 g1 k. X- n/ w3 c/ b - >>> math.copysign(3,8)/ x. p6 N0 F$ y- Q4 S- C
- 3.0
6 W, q- a0 M k - >>> math.copysign(3,-8)
: b& k- l; B# Y6 e% F+ h( `3 Q! i - -3.0
复制代码 ) d- _$ M0 ]# h$ z0 L
math.exp(x) 返回math.e,也就是2.71828的x次方7 b2 A; ^" J7 i9 u# {
- #返回math.e,也就是2.71828的x次方
, N s8 K3 U; ^) {( T/ e8 @$ i - exp(x)
# L- h: h: w% N - Return e raised to the power of x.
$ [( I1 k q' Y6 F5 C - / W2 B4 I6 c% @- k0 a
- >>> math.exp(1)
$ a* v" Y5 K# q4 E+ z - 2.718281828459045
, M, Z$ w% k* `4 Y9 e. w - >>> math.exp(2)* J7 m7 m, q& \7 z( }5 |
- 7.38905609893065
, {1 |3 R) H5 D/ K) W - >>> math.exp(3)7 i1 Y/ ]/ g* ^
- 20.085536923187668
复制代码
) d( `' ~$ B; \! q. _2 X4 b# N4 a; Umath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1$ b" k! s" Q6 W$ Q& J+ Z
- #返回math.e的x(其值为2.71828)次方的值减1
. @( K3 C, q" i5 V8 f/ H" `, y - expm1(x)* `& ]9 v) {$ c
- Return exp(x)-1.& h6 p& \0 X4 I7 E* S9 Q# ~9 n
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
' V, i s- ?8 P! }. j
H# n; j* @! U+ O, h/ t/ ?+ L% e- >>> math.expm1(1)# M5 ^3 ?8 i/ {+ L6 I1 G
- 1.718281828459045
( W3 _3 Z0 d. x# w y6 E. ` - >>> math.expm1(2) Q- b5 W( ~% A. \
- 6.38905609893065
7 O1 l7 W: b! `6 A( z( V - >>> math.expm1(3)1 ?; @4 e( H8 f' _- m+ M4 A
- 19.085536923187668
复制代码 ; ^/ d# h( y7 W- ]" t- j/ X# e8 v
math.fabs(x) 返回x的绝对值
* k4 k: g J. K2 ~+ l1 `- #返回x的绝对值 f! z" X5 \5 {. l5 I' c
- fabs(x)2 m1 s8 P8 a! O- s; [0 u8 n
- Return the absolute value of the float x.7 F% s h$ R+ L2 X
. U, w3 Q9 U; c: `* e7 h- >>> math.fabs(-0.003)
4 v, K: p' _7 K; t% w) N0 I; D - 0.003
: f |) M4 h" E! G - >>> math.fabs(-110)
" N4 e6 m% }0 n. V1 X( }4 D - 110.0$ h# v( r$ M6 @
- >>> math.fabs(100)
9 n9 ?4 V3 ]0 D2 @" { l - 100.0
复制代码 / d2 k& ~% X! w
math.factorial(x) 取x的阶乘的值+ q* B' L) J% a
- #取x的阶乘的值0 Y8 _( k( |* @' ?# i; k ~+ m
- factorial(x) -> Integral% ]" n% M$ f y, Z; Z
- Find x!. Raise a ValueError if x is negative or non-integral.
; F T y+ X6 _: m! V2 v - >>> math.factorial(1)0 x, V) k9 K& T& a, L/ C
- 1# D, F. Z$ g# o8 S
- >>> math.factorial(2)) @6 m( w, ^4 d _7 R' Z7 s: h
- 2
/ s! l% b4 f& l/ c* m - >>> math.factorial(3)
* D6 R) B; G! D - 6
; J3 s: t) }* h6 I8 M1 h1 } - >>> math.factorial(5)
) t8 s7 ~$ ]$ |3 s7 X. Q - 120
5 Z+ ?/ {: U ? - >>> math.factorial(10)
& p- a5 g% Y Y - 3628800
复制代码 ! F7 g+ k9 y) @3 Q5 d
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数
: c, |8 v" z8 ]3 w) y2 _) |& z- w- #得到x/y的余数,其值是一个浮点数
$ J/ p0 }& R5 f+ n4 F - fmod(x, y)
- P, k8 ~* W# D! l: W - Return fmod(x, y), according to platform C. x % y may differ.4 x' T' Z! M* Z& R; E9 y& T" V
- >>> math.fmod(20,3)3 n4 ]+ U7 m* |) g
- 2.08 x1 J/ Y! }" P( D. J
- >>> math.fmod(20,7)9 M3 ]1 e8 G7 y. J- H) N
- 6.0
复制代码
, j+ ?/ k9 U0 K6 B( V$ Cmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围
! A" B, J& f/ I- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
0 b1 S" d- O5 _2 c5 N/ i! M - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值" _8 k3 V. b! u) x) ?
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和16 I8 p9 M5 T6 E8 X
- frexp(x)
' X* ]! o4 \' R( |% D/ v) n+ _ - Return the mantissa and exponent of x, as pair (m, e)., t! [0 t2 |& M
- m is a float and e is an int, such that x = m * 2.**e.
( |1 J5 \5 B; V/ @4 i% j7 m! ^ - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.* [4 c* y% z) B/ W% x
- >>> math.frexp(10)
! D9 h, A1 c3 y- l T) w9 Z - (0.625, 4)
4 {# d- h w3 x - >>> math.frexp(75)
6 E. U6 O! `* t4 J" R* p+ G, ^ - (0.5859375, 7)) O' | z; ^/ E, q( e9 w1 _
- >>> math.frexp(-40)/ ^. z) r( S8 C1 Y" ^" i d
- (-0.625, 6)+ {' {: F% H }1 x1 @6 Y* R
- >>> math.frexp(-100)$ j, [% j! B$ y% T$ e
- (-0.78125, 7)4 p: \) |5 I$ P: ^
- >>> math.frexp(100)+ J5 \- `# x" u7 ^" \
- (0.78125, 7)
复制代码
+ _/ l) I* K) R8 c' a8 e' B( A$ J- @math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
2 W" H6 p8 T% ^; T- #对迭代器里的每个元素进行求和操作
! P9 Y; G% Z+ h# N3 h* t+ a8 K - fsum(iterable)
$ O+ N: X; l6 y$ r! C - Return an accurate floating point sum of values in the iterable.' T K# d1 m# w' q8 d: j+ N
- Assumes IEEE-754 floating point arithmetic.3 M: k% S% Q1 S' T1 h) `. Q
- >>> math.fsum([1,2,3,4]) D+ b+ I9 _) ` g6 C
- 10.0
: R, n/ N( a8 [, B! v* |2 D - >>> math.fsum((1,2,3,4))
+ M( h! \9 e+ E9 D0 u" V - 10.0# o0 U; t1 ?, G% E
- >>> math.fsum((-1,-2,-3,-4))# T7 t4 @2 `) m
- -10.0
; S5 G3 f0 k5 ?7 I" F$ e* w' y - >>> math.fsum([-1,-2,-3,-4])
% a6 j1 v) n; w( N - -10.0
复制代码
/ h( [7 y6 f7 [$ }5 t3 `9 v& ~0 \9 v, Imath.gcd(x,y) 返回x和y的最大公约数
( Q' O) o; B. g% r% {3 {- #返回x和y的最大公约数
% r! S# P" L1 y - gcd(x, y) -> int
1 r% l# u4 O, _& K2 I, E) ? - greatest common divisor of x and y
' s3 \; t9 U/ F7 ~' T! K" ^9 O* X - >>> math.gcd(8,6)
9 o* s% \+ ~" q& ~+ L8 t: X' w - 2/ D8 h+ q" _8 ~$ k7 ~
- >>> math.gcd(40,20)
+ d4 H; N% I/ \3 Q0 r - 200 n1 |% f% c( ]
- >>> math.gcd(8,12)* a. W) i, h( v4 e" t7 s4 i
- 4
复制代码 4 W! ?8 x8 l- C W$ `
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
& Z3 P2 Q& H3 H2 e5 W- #得到(x**2+y**2),平方的值
7 }! s" ]! H% N9 M3 L - hypot(x, y)6 r0 x$ c' L$ E0 o* }4 i9 `* r1 T4 Z
- Return the Euclidean distance, sqrt(x*x + y*y).. K9 L1 f3 e3 m* q0 m
- >>> math.hypot(3,4)4 I6 p. B7 l1 U
- 5.0' F. u6 y& b* L/ M2 o) y' Q* F
- >>> math.hypot(6,8)
2 _+ |" y' e J1 ?: k# M) U - 10.0
复制代码 - j/ r8 j, o& N* s% b# a
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False t5 g. S+ Q. e- q
- #如果x是不是无穷大的数字,则返回True,否则返回False
+ c9 E* s- K" L/ j- [ - isfinite(x) -> bool% e. h8 f% d1 B9 u9 b
- Return True if x is neither an infinity nor a NaN, and False otherwise.
- k9 p0 `* Y! I$ P3 f+ e - >>> math.isfinite(100)
, e1 W8 i# v: p - True
# P- r1 ]5 O5 j) y. `. R6 \ - >>> math.isfinite(0); ]- p$ l- A) p& _% H
- True& V( I: k; c8 D: A: m2 G6 T
- >>> math.isfinite(0.1)
. s8 B9 L2 o' @& N. a4 ~ - True; } F$ J. K; D6 ^
- >>> math.isfinite("a")
' z. F: V! C9 F6 G, D - >>> math.isfinite(0.0001)
, [; v# A, ~3 C! r3 m - True
复制代码
4 S8 ^. ?6 A" K9 t" r$ @math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
) F1 E/ I0 \5 Y! ^( a7 a8 E- #如果x是正无穷大或负无穷大,则返回True,否则返回False8 G$ n' H8 _, x) N9 W# O
- isinf(x) -> bool7 U" S4 N$ d" P
- Return True if x is a positive or negative infinity, and False otherwise.0 K+ ?2 E7 o7 m! s% s
- >>> math.isinf(234)6 v8 @) `; d% N) f; r3 q
- False
, L) G4 h* {9 W4 E2 _0 q8 p - >>> math.isinf(0.1)
2 b0 K9 @3 O; n9 j. N2 ? - False
复制代码
% I4 S- L$ P4 s: p* vmath.isnan(x) 如果x不是数字True,否则返回False7 d5 H, p4 G* E& k q4 @9 \- |
- #如果x不是数字True,否则返回False
$ w6 m7 Q* X) |* r - isnan(x) -> bool/ n9 y) P# _/ `0 w$ u' {5 I& c
- Return True if x is a NaN (not a number), and False otherwise.
; ]+ l' K3 ~# W - >>> math.isnan(23)/ y* t) ~( Y$ D2 f9 b* n' ^
- False
4 c5 w. Y) K4 P# h3 l; \ - >>> math.isnan(0.01)* |3 F# e4 G- w8 s' u: R. K, b
- False
复制代码
/ A# ^' g$ o5 H; I6 V ]math.ldexp(x,i) 返回x*(2**i)的值
9 y- ?6 O; V5 W7 J8 r5 e- #返回x*(2**i)的值
' {3 w$ p# z; Z f) Q) N, r( K, h; E) ] - ldexp(x, i)# @0 t$ d( o, V# i0 O
- Return x * (2**i).8 L. N* i5 a' r, Y
- >>> math.ldexp(5,5)) a- j# r0 k! N) N6 B
- 160.0
" O# u3 E8 X4 ? - >>> math.ldexp(3,5)# Q+ @9 K0 @) U# b5 K
- 96.0
复制代码
" x& J7 o6 [- qmath.log10(x) 返回x的以10为底的对数
! I6 C2 B* v* G: |- #返回x的以10为底的对数
3 C6 p# Q: S8 W$ G - log10(x)
# _8 B. k% w3 n6 z; [0 Y3 z# e: P - Return the base 10 logarithm of x.
6 y8 {+ b* ?! I% Q6 [6 k& j0 M6 J - >>> math.log10(10)( r8 H: {7 Y' [ P( x
- 1.07 W1 k: _. r( `$ l
- >>> math.log10(100)
" i0 l: p4 n8 i" b& U3 Q" R/ i - 2.0
! N L. Q w2 B1 k) \6 w - #即10的1.3次方的结果为20
" x) X2 X" B9 Y* |& o* \9 Q& i - >>> math.log10(20)
. o+ t. A5 }. k4 j0 { - 1.3010299956639813
复制代码 $ o3 @' U3 r# N( u2 @
math.log1p(x) 返回x+1的自然对数(基数为e)的值
6 |" y7 k, y: c6 _! D5 p% k& ^7 w- #返回x+1的自然对数(基数为e)的值6 j7 A* l! R" L
- log1p(x)$ D4 `: A0 w1 M. U
- Return the natural logarithm of 1+x (base e).
/ f0 M+ M* n! \: j8 M7 u7 O - The result is computed in a way which is accurate for x near zero.
4 I$ ^7 u I0 n, {- _0 |4 M, M# F% f3 g - >>> math.log(10): l. v, h+ e6 _. {7 c4 ~
- 2.302585092994046$ H' I, t9 m) a- _1 P' C5 {+ D0 I
- >>> math.log1p(10)
% w) R% l/ C; d7 \$ ` - 2.3978952727983707& L' K k* }5 C
- >>> math.log(11)
# ]+ O3 y# P! S- z2 J; d2 p( T - 2.3978952727983707
复制代码 7 S x) {$ A6 R' {+ K& J9 v
math.log2(x) 返回x的基2对数5 x5 e b( q2 B
- #返回x的基2对数
9 y+ B6 m, L2 z - log2(x)
& C: V' C8 ~7 P2 n# I1 C9 Y2 Q5 E: y- f - Return the base 2 logarithm of x.% d8 m. u1 l$ H2 M3 ]6 b
- >>> math.log2(32)
) d4 g# A+ i4 q9 `8 g - 5.0
$ u* ]$ X. `( i, @- l - >>> math.log2(20)$ A0 k" D0 O7 X4 M) \+ ]
- 4.321928094887363
2 F# g; }/ h' p. I+ s/ D! q - >>> math.log2(16): a! E* T) L9 P3 e) v9 F2 t
- 4.0
复制代码
6 }1 M% W1 z+ U! Q2 l% Mmath.modf(x) 返回由x的小数部分和整数部分组成的元组
$ ? r$ u' h- Q. Q0 S0 V- #返回由x的小数部分和整数部分组成的元组5 i0 Q8 [1 a( `$ [6 k
- modf(x)
! Y5 e) z% c7 @# Z8 m, l4 z" u - Return the fractional and integer parts of x. Both results carry the sign% c/ r9 C/ L: X$ O
- of x and are floats.$ {# v) N9 P) J5 X# ^& G( h
- >>> math.modf(math.pi)
" y. Y+ \6 N* i% q+ i/ { - (0.14159265358979312, 3.0)
3 ~; T, j; L% H- v- d. h; j3 ` - >>> math.modf(12.34)
" J/ q: ?" G0 K% H* Y5 X - (0.33999999999999986, 12.0)
复制代码
( W$ ^. x$ `. imath.sqrt(x) 求x的平方根
& E& U4 k4 l; s- #求x的平方根9 b4 Y0 {, c, L5 h9 X
- sqrt(x)
9 q! a4 T+ [' C7 _4 Q - Return the square root of x.
* P4 m' a- Z6 e' U - >>> math.sqrt(100)
* ~4 ^' w% ?6 Z$ Y - 10.07 G& Q% z7 m& l+ j
- >>> math.sqrt(16)
+ n. x. N' s/ E: ?9 n( U - 4.0
0 d- s, j1 y" c9 p# h. p' v# P - >>> math.sqrt(20)/ q. H/ R& c! C* m
- 4.47213595499958
复制代码 ( Q1 l/ Q+ W1 `# C3 L9 q9 r
math.trunc(x) 返回x的整数部分- #返回x的整数部分" F u7 }) n1 m9 n& G5 d
- trunc(x:Real) -> Integral
/ d5 @. N9 o3 z' i8 U - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
/ o' M' r* t6 U% K' M3 a i - >>> math.trunc(6.789)
p9 \2 B. l+ C" } - 6* `- K* L! x* F( ]0 c
- >>> math.trunc(math.pi)
B! B8 V2 h0 N/ L& Z - 3
7 U5 y& G/ D$ {0 j2 m - >>> math.trunc(2.567)
- ?; G0 X! N5 F; U - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|