马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 l2 Y1 X( I: a* R
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 J; B; ]# l- \# M
' ^1 a2 T& e0 \" s方法1:
8 l) i) r0 s9 D- >>> import math0 A1 a. Z9 Z: O9 K2 O
- >>> math.sqrt(9)
8 f" z* P2 v0 T* P - 3.0
复制代码 方法2:
4 J9 n& T0 c3 O; h& w) Z4 z- >>> from math import sqrt
- M2 M$ j9 _3 P' K! `' j - >>> sqrt(9)
. B& `3 f! m; H7 S8 A5 i; f9 b, _2 G/ P - 3.0
复制代码
' i3 S- v" [6 I: j* s8 P # M7 V$ o5 [7 u
math.e 表示一个常量0 ^% T2 b7 t) F9 a5 P
- #表示一个常量
, w* R: q: J& D1 L* | - >>> math.e# n& P u! g. p0 t7 [0 ]
- 2.718281828459045
复制代码
( Q# R9 L j, F8 V# t$ \math.pi 数字常量,圆周率
0 Q8 d: g" S7 ]" B- #数字常量,圆周率
K$ K2 M( \, }" j" G - >>> print(math.pi)
+ F# ]7 j3 V! B& d$ H9 C - 3.141592653589793
复制代码 7 C% e5 `/ I! Z" u! G
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
+ D* ?: Q6 J. S w. F, ?. O& K9 M- #取大于等于x的最小的整数值,如果x是一个整数,则返回x5 Z: I) Z& a. L9 Y5 E
- ceil(x)7 c! Z$ S; _/ w, Q
- Return the ceiling of x as an int.
7 c. q2 I! ^& P3 U+ ~# K- x' P - This is the smallest integral value >= x.
h+ B* I& j5 R, h, {# Y
4 G/ i7 v2 x$ L9 T! ]- >>> math.ceil(4.01)7 D! s" C( }' [
- 59 l/ u/ I; b8 s% P8 J- ]8 q8 E
- >>> math.ceil(4.99)
5 D; \) U9 ^0 Q) k7 x - 5! x/ ]% N4 p/ ], W( }8 f: o" `
- >>> math.ceil(-3.99). g* q4 ?6 R" @$ o; ~
- -3
' R" M0 n& i( O" C - >>> math.ceil(-3.01)
: T5 p4 c% {+ N8 K; t. J2 n - -3
复制代码
: h6 E/ D$ M. d) Amath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
0 q4 {, q/ w/ H) z: L" Y2 H- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 ?; @3 j3 c+ Q* H* J7 o/ I - floor(x)
! ?3 d& i P! P - Return the floor of x as an int.
/ O3 U% J" R4 A) E9 o* q$ J" v - This is the largest integral value <= x.6 m8 s7 p! U( ?0 q
- >>> math.floor(4.1)
3 M, ~9 y9 C A* c - 4
7 U8 m5 [1 o n# e5 Q - >>> math.floor(4.999)
2 f3 r" z" x" o. O9 f% A4 k - 4
% d1 ]+ `0 g: j2 G& L2 N# E* O5 r - >>> math.floor(-4.999)% S$ f& t7 T* H: i- {
- -5; z& C( J" y d8 O* ?. s6 @
- >>> math.floor(-4.01)
* @# } N* D; j+ B, k - -5
复制代码
1 w* V. C- F* ^, k5 L4 u" qmath.pow(x,y) 返回x的y次方,即x**y
1 S8 _% {6 e9 t- #返回x的y次方,即x**y
: H7 Y! c6 T5 ~9 ~ - pow(x, y)% t. A- p1 F+ d( u+ L* [& m; M
- Return x**y (x to the power of y).8 `- L7 _5 Z2 v+ F
- >>> math.pow(3,4): l0 j! F- \+ U& A
- 81.08 j$ W! _0 I* [* B* V& G6 {
- >>> 4 \& U# U Q w
- >>> math.pow(2,7)
0 v, }4 D, d& m9 u" Y6 L4 K" D B - 128.0
复制代码 2 K, A5 G K0 z
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)* j: o- G, h" h
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
" Z- }" l" ~( Y& \; y( z - log(x[, base])
# n( X( g2 i4 [. s" E1 [+ G - Return the logarithm of x to the given base.5 p- a) C4 e/ Q0 A: q" ?" F5 v* s
- If the base not specified, returns the natural logarithm (base e) of x.
7 R0 D J, z$ r) V% z - >>> math.log(10)# y8 g* }2 a3 g% `# I8 U6 }, u
- 2.302585092994046
, X2 a& k8 k+ Z5 X. h+ t - >>> math.log(11)3 y2 L: f+ N5 K
- 2.39789527279837074 y0 S2 a' |/ n
- >>> math.log(20)
/ ~* v) e6 W4 Z8 U6 T# K - 2.995732273553991
复制代码
]; l; w9 ~# J; T9 smath.sin(x) 求x(x为弧度)的正弦值8 v& Q& l6 l: g2 Y, ~; a
- #求x(x为弧度)的正弦值
: c' n9 C! P& e5 R6 n4 m: c - sin(x)
( a ~1 |, x1 a( x - Return the sine of x (measured in radians).* i$ S5 l5 X2 m. t
- >>> math.sin(math.pi/4)5 I ~5 [! D+ S# ?* K
- 0.7071067811865475
0 S% @8 j& D. T2 Z! x$ D - >>> math.sin(math.pi/2)
6 X: Q1 n# A' f# g# q - 1.0
9 _6 I0 B$ ^- I8 d O, y - >>> math.sin(math.pi/3)
3 P# ^+ I" {; I - 0.8660254037844386
复制代码 9 T& T5 D/ v R0 n! h
math.cos(x) 求x的余弦,x必须是弧度" O+ o0 J' [; g& Y( C. ~
- #求x的余弦,x必须是弧度# Z4 D, L ~# Q4 Z" u6 N
- cos(x)
, m; V+ t: J4 W - Return the cosine of x (measured in radians).
O# {- a+ P! I+ y' t - #math.pi/4表示弧度,转换成角度为45度0 m" P9 O; `' S: C B
- >>> math.cos(math.pi/4)
+ D* `7 ~% j) x S/ q6 o - 0.70710678118654760 n; s- ~) t- a$ [; ^7 ~3 u1 o# `
- math.pi/3表示弧度,转换成角度为60度3 J) P9 p; u I- n; Q3 o, D. z
- >>> math.cos(math.pi/3)! R' d0 t m7 R1 F
- 0.5000000000000001/ T3 ?, u; B8 e, ?4 A) j: | w. r
- math.pi/6表示弧度,转换成角度为30度
$ A5 [0 r# I& \5 e4 Q - >>> math.cos(math.pi/6)# w/ A* G" c2 X6 ?8 s \0 M
- 0.8660254037844387
复制代码 5 H4 b: o' s. Q5 S8 J K2 _, E
math.tan(x) 返回x(x为弧度)的正切值
9 {" o* l: p3 m J" r4 ~0 _- #返回x(x为弧度)的正切值
* I0 u+ s& O V' z/ b. e - tan(x)
) |1 }. ^4 |5 X - Return the tangent of x (measured in radians).
2 a( f; j0 J0 a- o4 b - >>> math.tan(math.pi/4)9 U: @* _( a2 {; |. N
- 0.9999999999999999% w8 {9 M f# ?- x2 @8 b
- >>> math.tan(math.pi/6)
0 t# E6 w% v/ D9 l- J - 0.5773502691896257
% V4 w; T; c9 b - >>> math.tan(math.pi/3)( M" b: P+ k( B+ I
- 1.7320508075688767
复制代码 ! w$ v6 H+ A# ]9 S* X2 r% |! v
math.degrees(x) 把x从弧度转换成角度
1 {, m6 z% L6 c: `* e- #把x从弧度转换成角度8 G9 z3 A/ s, ?, n* {2 H: v
- degrees(x)
5 a, V; E! X& _5 i - Convert angle x from radians to degrees.
/ P$ @6 x9 m+ Q# ?2 M0 K
/ L3 B$ k* F3 m* L- >>> math.degrees(math.pi/4) o o2 S( J1 }( x0 I4 u+ O& |
- 45.0
, F2 D E3 A* F/ ?9 w2 S - >>> math.degrees(math.pi)- p- ]( C% h) h A: \
- 180.0
9 \# a( }- ^: q - >>> math.degrees(math.pi/6)
: o ~7 h$ n! f* W( ]7 y1 Q - 29.999999999999996
/ i+ U" U/ w: n3 U; U# [ - >>> math.degrees(math.pi/3). J2 x, d0 e# ]; s
- 59.99999999999999
复制代码
8 ^( C. p( z3 F( i; s, hmath.radians(x) 把角度x转换成弧度4 C u( E8 c- b: C& |/ y. T% y
- #把角度x转换成弧度! A7 ?4 H6 s3 O( Q1 e0 l
- radians(x)
8 `- s' }' e% }1 N& R% k - Convert angle x from degrees to radians.
. ^2 O: ]+ N( @( q5 | g' L - >>> math.radians(45)- p& |$ ~2 u; F6 B5 ~
- 0.78539816339744833 L+ {- v0 k: g/ c8 W
- >>> math.radians(60)2 |9 Z5 L4 o3 A X2 i% t1 \# g5 N
- 1.0471975511965976
复制代码
1 O+ |: P+ n xmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
7 \9 p/ |& Y6 S, h& N- E+ ~: {- #把y的正负号加到x前面,可以使用0
+ t$ n4 J5 Q, d8 b9 a7 v& T4 W1 _' @ - copysign(x, y)4 h, b9 l7 B8 i: z9 g
- Return a float with the magnitude (absolute value) of x but the sign
8 c1 ?) K0 f5 k( e) y - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
3 H, y- }5 p! J) } - returns -1.0.6 G' E+ ]+ q( C; x3 L( Y' [' [
! M, h, U/ A" ~/ `1 S- >>> math.copysign(2,3)
3 l# r, l. k W* w3 x - 2.0
. E4 A$ P+ \" X$ ? - >>> math.copysign(2,-3)
! {2 B) j% ?* Z! P/ k& z! y1 i - -2.07 m- ?: j; z6 H! @1 u9 n% {) l
- >>> math.copysign(3,8)0 b" @3 b W9 s2 E
- 3.0
% n; r+ |8 t0 m& b; m! y - >>> math.copysign(3,-8)
7 l/ M. r* N c5 e4 v - -3.0
复制代码
1 u. [8 O# G9 Y1 p" emath.exp(x) 返回math.e,也就是2.71828的x次方; \ _7 `' A0 p: n# E; n0 m
- #返回math.e,也就是2.71828的x次方
( N, x& l* N4 B$ F - exp(x)1 ~& T9 C5 F6 n' N% P. ~/ b
- Return e raised to the power of x." m6 v h9 n9 c# F3 u) y
' y! \" d6 }5 S" c- >>> math.exp(1), K5 w" D) v. S1 I
- 2.718281828459045
, [( Y9 t7 |( l, M* ]( ]% p - >>> math.exp(2)4 k$ ^0 e* O* K. F4 T) X" m
- 7.389056098930653 u/ F. Q5 v8 ?, A9 m. ^/ [
- >>> math.exp(3)
) J, ?, P- |% g8 L+ l - 20.085536923187668
复制代码
, ^7 Z6 U8 l' J4 p$ G- D0 }/ jmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
) V+ y% {( T! n+ b/ C, L j- #返回math.e的x(其值为2.71828)次方的值减1
O2 r' T1 G" L" X3 }2 @8 \6 u: i9 G - expm1(x)& S. z4 ?# H: ?* c- t
- Return exp(x)-1.
$ g( m ~: h m$ U5 Y# U. L' v - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 I. n) o! I# b
- " A: O, \1 g0 r( z% P" n' V- }
- >>> math.expm1(1)8 l1 K8 f/ n! s! ~1 L
- 1.718281828459045
; p, N z6 H: \% j( q - >>> math.expm1(2)
1 T: @2 ^6 E% r0 m& g" ~ - 6.389056098930651 [! I2 D& Z$ y2 V
- >>> math.expm1(3)5 Z2 p+ ?) ~6 r, X; E$ t- p* i& i/ G
- 19.085536923187668
复制代码 . P! c3 U; ]; e$ S) r u( _) \
math.fabs(x) 返回x的绝对值3 z2 W- J0 d3 G5 z! l" D6 G' U
- #返回x的绝对值( B! q. A0 q8 ]
- fabs(x)8 k5 G. i0 {/ t& \' U% I
- Return the absolute value of the float x.
# a8 m1 m2 L% l& f
- O' o- g6 ~: l/ E! i' T- >>> math.fabs(-0.003)* e* u U4 Y6 T% {" K; `' M
- 0.003) K) A' m3 ^) X
- >>> math.fabs(-110)
! h% t( w! |' G - 110.0, ^+ j3 J0 E" g6 Y0 s8 K3 }
- >>> math.fabs(100). R: b) A! P# }8 h+ N' N6 I
- 100.0
复制代码 & l: T$ L* d* O% k
math.factorial(x) 取x的阶乘的值
2 k5 q9 w% ^6 a) o$ F6 b- #取x的阶乘的值$ Z: b: I6 {; ]5 B0 P
- factorial(x) -> Integral
" ]& g" \- K; ^, J6 U$ f W - Find x!. Raise a ValueError if x is negative or non-integral.
" @" c ^ Q( ~# A. t5 @' w( K. y - >>> math.factorial(1)
; {& H, k: h& m0 y - 15 P n4 ]5 M- \$ q6 X! b/ o+ _
- >>> math.factorial(2)
! l# C) ^5 H" Q# t* o% R - 2
* s) ~: v+ ?; c9 h9 e - >>> math.factorial(3)
+ P6 h& N2 S7 A2 @: N- @% S - 6" m- w5 e& d: v8 {
- >>> math.factorial(5)
# O% D7 _+ M2 ?0 D l, `2 a; { - 1201 {! L8 O2 w; M0 I: f/ V
- >>> math.factorial(10)3 y) ]& }+ M% B0 v+ ]. v
- 3628800
复制代码 1 [9 r- o8 L/ f. p' ], N
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数6 N8 ]/ J5 T# ?2 u
- #得到x/y的余数,其值是一个浮点数7 A1 x, y/ Y4 ~7 ^ {
- fmod(x, y)" N0 {/ V+ E( f7 U5 e
- Return fmod(x, y), according to platform C. x % y may differ.8 w$ v& E1 x2 X! T. R1 u8 F7 j: Z
- >>> math.fmod(20,3)* U: G5 j& Z- ~/ D+ Y. n
- 2.0
6 u4 w& G! g2 ?7 b8 N/ O - >>> math.fmod(20,7)5 Z2 u# k/ g x
- 6.0
复制代码 $ E' b, S% r. q8 k7 ]
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围- h9 H* \7 g+ k1 ~% |
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
3 e, `3 w7 u( B+ Z - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值0 H' D/ n2 i4 C
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和19 U9 X$ Z" A! A1 G0 P w
- frexp(x)
0 g% k/ J- W) u# k - Return the mantissa and exponent of x, as pair (m, e).
4 Z& }4 R1 z. [# S j' @; r( t6 z* D - m is a float and e is an int, such that x = m * 2.**e.( x) t8 D/ c t
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.8 d) _( U# u/ g# @: y
- >>> math.frexp(10)
! A+ [ L3 {1 Y2 i - (0.625, 4)
, F; V$ r, E7 l& I0 a0 Y4 k - >>> math.frexp(75)) V/ t9 m$ C1 m, Z
- (0.5859375, 7)
9 A! I% \+ }1 U5 o# J - >>> math.frexp(-40)# p( W6 X% `: ?4 e; E
- (-0.625, 6)- m* W7 y$ r( V% G8 Y
- >>> math.frexp(-100)
; M6 ~9 F( Z- N - (-0.78125, 7)0 [8 n) r: N6 y3 v9 i' q1 _" I( D, Z! d
- >>> math.frexp(100)9 }2 B3 n8 I* U3 L( D! `2 ^ l
- (0.78125, 7)
复制代码
) r+ w9 [( a/ u/ xmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
5 S4 z1 |$ m3 |7 m8 c- #对迭代器里的每个元素进行求和操作/ V& x' j& ~4 R. r! G9 {3 t) a
- fsum(iterable)
7 @- H" G' w. p! e" K - Return an accurate floating point sum of values in the iterable.
# c- v8 m& i+ [ m6 p. u! D! |6 _ - Assumes IEEE-754 floating point arithmetic.
0 `% L. N! y* E0 ~1 L' F- J* F7 [ - >>> math.fsum([1,2,3,4])
, ]3 p5 R! p! e0 }. @; v - 10.0
5 R$ L; F. V3 T# j3 { - >>> math.fsum((1,2,3,4)); d1 |: `8 P" }! p3 p
- 10.09 y/ L5 X$ q0 J6 V+ i0 V
- >>> math.fsum((-1,-2,-3,-4))# u( X a, w: t9 d* T' X$ P! p- e
- -10.0! J5 z: `, I0 U* L( s" v% v
- >>> math.fsum([-1,-2,-3,-4])! f; }. ?" E/ y ]6 n- s5 L. j
- -10.0
复制代码
, u7 {7 m" z. f; `$ x" }) h& ~/ imath.gcd(x,y) 返回x和y的最大公约数# U. z$ W' G( T' A# K0 A! n
- #返回x和y的最大公约数
2 l. J" g4 x# Z9 I; Q9 c - gcd(x, y) -> int
( Y; P5 ?. y6 p: e; C$ N3 K - greatest common divisor of x and y
7 ^, f' _" W4 ~ - >>> math.gcd(8,6)" T1 ~# ]3 C/ ?' ]! e" F
- 23 s9 @. p3 J g6 Z
- >>> math.gcd(40,20)
" x# x- ^$ D' f) y - 20
) V0 h y3 d) K. q% p( T, }% b; z# ` - >>> math.gcd(8,12)
, T9 x6 y. W. m/ X. `# s' M - 4
复制代码 e3 W7 |# h C- Q7 @, X, P
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
) R* i5 t P; m0 U- #得到(x**2+y**2),平方的值
% w9 ^ S6 s, N/ i1 e, z' ` - hypot(x, y)
+ A4 f" x# x! Q! M - Return the Euclidean distance, sqrt(x*x + y*y).
: R5 H' \" _0 S$ }* J - >>> math.hypot(3,4)
1 K; G1 ~" Z. m8 w - 5.02 ]$ c5 {+ z; q1 \
- >>> math.hypot(6,8)7 |& j5 y. q* k" k( i/ L8 n
- 10.0
复制代码
# x& X" ]9 u1 r2 qmath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False. f$ J3 N# z+ R- j
- #如果x是不是无穷大的数字,则返回True,否则返回False
: I! K8 Q7 S( d5 x. @ - isfinite(x) -> bool9 {" u. w2 W" }; h# p
- Return True if x is neither an infinity nor a NaN, and False otherwise.
6 y: \" f7 Z8 ^) {. G2 p# b( F - >>> math.isfinite(100)( L1 B+ ^% F5 z6 j2 \: X; C* P1 w
- True
: y5 d; o8 K$ @+ m0 }* S - >>> math.isfinite(0)
! \* v. `6 C8 M# W/ m+ _ - True
' l6 m0 a; u5 M - >>> math.isfinite(0.1) X% c0 T" A, d% V+ h/ F. `
- True, d3 p" q& \$ q$ Q5 K* l
- >>> math.isfinite("a")
4 C$ _( N4 \& x }$ J2 B% J - >>> math.isfinite(0.0001)- Y& O" y5 ~- ^- d, h: T
- True
复制代码 8 M- Z! w5 T( R$ V. W- C" d
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False2 n. T6 ]6 f7 M" M3 M
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
( d; z6 p1 X ?$ f* ^8 u - isinf(x) -> bool
, ]+ o1 g. Z4 W. M9 f9 W% ?" x - Return True if x is a positive or negative infinity, and False otherwise.* D6 a7 f' U$ r4 @# n/ H( y
- >>> math.isinf(234)6 {3 h$ U* Q) w; H
- False
9 y1 v" c( W5 |* ~9 _/ O - >>> math.isinf(0.1)
$ i# k6 J7 h8 u& \1 R4 }" | - False
复制代码 9 E* X4 F1 l* q& v
math.isnan(x) 如果x不是数字True,否则返回False+ N& L5 V2 q7 W. \8 L1 l
- #如果x不是数字True,否则返回False
9 N x. U( t& P - isnan(x) -> bool7 c& `- t/ i1 i& K' `0 Q& t" R
- Return True if x is a NaN (not a number), and False otherwise.& h7 u# c" ?7 c4 C& v
- >>> math.isnan(23)# d; y/ G& b! z2 J# } h0 c
- False
5 Y) u# C( O2 U - >>> math.isnan(0.01). z$ u8 R$ u: I0 q- j
- False
复制代码
( b9 q1 |* I& X' ^" Amath.ldexp(x,i) 返回x*(2**i)的值+ y- A4 _3 Z7 i
- #返回x*(2**i)的值
: h, D0 e" P. K) X( [2 o+ u - ldexp(x, i)2 L( b* C% A g" S
- Return x * (2**i).
. S" g: ]$ @' C+ r8 W - >>> math.ldexp(5,5)
# b& U* m; v9 }7 i - 160.0
; X/ p4 X6 f# o! q! i. m - >>> math.ldexp(3,5)- ^) l' u9 i' @/ ]0 d1 N
- 96.0
复制代码
& p: S3 W' E5 v- M5 U; Vmath.log10(x) 返回x的以10为底的对数- l/ P. R% M3 f8 c% [, G6 J
- #返回x的以10为底的对数; N, V; O' e% l& e
- log10(x)
& C+ c* X5 t5 V# p! _! H7 d - Return the base 10 logarithm of x.$ s+ R7 B, r/ ?8 [' R$ A; E1 Y
- >>> math.log10(10)+ f. o7 \ \+ ?: v h
- 1.0 a, o* ^% s ?8 r! t/ h5 U
- >>> math.log10(100)) b7 g. w3 Q5 h8 _' [, c
- 2.0
% ? M2 E9 d' F - #即10的1.3次方的结果为20
5 c$ z' T# P) p9 t3 u* p) u6 R - >>> math.log10(20)
, S$ |; _, {6 x2 [0 G - 1.3010299956639813
复制代码 + K, [8 g0 p; j/ X h+ r' G) Z0 G- d
math.log1p(x) 返回x+1的自然对数(基数为e)的值
) D$ x- z/ B+ x, o- #返回x+1的自然对数(基数为e)的值/ E; H6 U9 J j/ g/ t% M; D
- log1p(x)3 [6 E! E$ a) C& f& j
- Return the natural logarithm of 1+x (base e).$ M- h: X8 s% }$ d0 ^
- The result is computed in a way which is accurate for x near zero.) B' b+ y9 E N9 G
- >>> math.log(10)
/ Y- Y2 b6 q$ o$ y - 2.302585092994046' q h G: Q. V( @3 J
- >>> math.log1p(10)5 ?3 ]1 E" K, r; J6 N$ J1 s. H
- 2.3978952727983707# z4 ^* k ~9 Y( O4 T4 F
- >>> math.log(11)( o/ F: R" T, @
- 2.3978952727983707
复制代码
4 a3 E# t2 R& H6 I, wmath.log2(x) 返回x的基2对数/ B4 \% \# m: \0 ?
- #返回x的基2对数% N4 X: F, m: `
- log2(x)
& s* p. C" G/ b' M8 O - Return the base 2 logarithm of x./ Y( y/ [/ U1 f# _" F7 r' s
- >>> math.log2(32): U+ j4 D4 [# r9 ?% Z" q
- 5.0
& v Q- t/ y3 I6 C! q" x/ d - >>> math.log2(20)% V% T1 K* x* y9 M) W2 S
- 4.321928094887363
+ k3 U: i7 u! T0 H9 W$ A/ i - >>> math.log2(16); N/ V2 F+ F: L0 s/ {5 C
- 4.0
复制代码 + u8 ?0 n8 `0 n8 j" L. P
math.modf(x) 返回由x的小数部分和整数部分组成的元组: \4 N3 ~. d9 K* Y, o0 B: z
- #返回由x的小数部分和整数部分组成的元组5 |% H1 v2 n7 `7 d
- modf(x)
: \& o. d' E4 s1 ^ @2 A8 a - Return the fractional and integer parts of x. Both results carry the sign5 Z, w; ?& n7 C6 ?' K3 M
- of x and are floats.( A+ t2 c% a0 M, }! ]
- >>> math.modf(math.pi)3 N& j7 U6 `& i- A! s5 r3 }
- (0.14159265358979312, 3.0)
( k$ V/ W* ~3 B0 ? - >>> math.modf(12.34)
" E! x8 l* Q9 u1 D - (0.33999999999999986, 12.0)
复制代码
9 _& _" v U0 ]- q5 f3 rmath.sqrt(x) 求x的平方根2 |3 Z% o/ m% Z" y0 B, r
- #求x的平方根; \, A" F+ F+ l2 f% l; v# O
- sqrt(x)' W- _* H0 V4 G
- Return the square root of x.
! V7 C* w# `' V& z - >>> math.sqrt(100)' X7 S* n2 a6 @! R+ t, ^ H: s+ A$ n
- 10.0( U0 ~! v. y! R ]& z+ d1 Q
- >>> math.sqrt(16)
m- N' O( J) f" ~ - 4.0$ Z L1 M& L1 P0 U
- >>> math.sqrt(20)1 I" W; Y! B1 Z
- 4.47213595499958
复制代码
5 U# X% O1 ]# g& H9 C! Gmath.trunc(x) 返回x的整数部分- #返回x的整数部分/ O+ z9 w) p2 c# E" U8 D+ I
- trunc(x:Real) -> Integral
1 F) Y6 H4 j% U# _! b9 d$ D2 R, D# n2 e - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
9 u2 Q7 j8 a& `4 L - >>> math.trunc(6.789)3 N' O' t# t! F) k
- 6
( l; j% z" I6 ^1 o3 | - >>> math.trunc(math.pi)
1 `7 n* _( Q; ~& A7 ~" [ - 3$ `) A& a0 B' E" l3 q
- >>> math.trunc(2.567)
5 M, N4 N9 Z" L/ i - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|