马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
. ?- V& Z1 x. U4 g, g. Q3 V
【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
* B& X h# P/ {5 \
- H# k; s" N. [ ^4 @' h U方法1:* Y! F/ N; ^; }3 e* _1 j
- >>> import math
' K; I" K5 E: Y+ }( b' S2 i. F$ b - >>> math.sqrt(9)
& N2 l' q0 o4 @) z+ I1 S% `3 O - 3.0
复制代码 方法2:
6 s+ q; p7 |1 d! @) h( J- >>> from math import sqrt: N9 |/ m0 a* h8 _
- >>> sqrt(9)
' ~& {7 M" }* f& F t8 l1 j( c - 3.0
复制代码 7 t9 z4 H3 d6 f! V @% f1 q
6 A5 q4 q# {+ f' r; O7 ?3 ^math.e 表示一个常量) X3 E I! |' Z* u
- #表示一个常量 l. _6 ^- E" V
- >>> math.e
3 I) k2 F( U9 w# r4 E9 c - 2.718281828459045
复制代码 ' e3 l+ E4 m) h8 Y, r
math.pi 数字常量,圆周率/ N# }" a8 t/ u8 q% J% f( Z9 w
- #数字常量,圆周率
* ?4 F7 E1 @8 ^' F0 D - >>> print(math.pi): P7 x/ N& e v; U X! c: i8 c
- 3.141592653589793
复制代码 , b5 [/ f f% }6 j5 O
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x# n* A' B) Z: ]
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
! b, l0 z: m" H( V& d9 G7 b. O9 o% \ - ceil(x): \% Y7 H% f- Y
- Return the ceiling of x as an int.& A$ _& h1 V& l8 v
- This is the smallest integral value >= x.2 k3 [$ x i6 M3 Z: h q" H2 u
9 X# K6 h" ]4 X! L; h4 A1 F% {- >>> math.ceil(4.01), M8 C0 A+ K! d* o- A4 k2 K
- 5
, R5 g) b1 g' y - >>> math.ceil(4.99)+ H% F! P- J# A& `- E D; D1 y
- 5 m2 N; K8 {/ \4 z
- >>> math.ceil(-3.99)$ {: }2 N, a6 s& B, D. Q% j. j0 a
- -3+ k' a x& G; Y* z* B
- >>> math.ceil(-3.01)# u$ {! a: P, n8 Y9 |
- -3
复制代码
( q$ z3 s. S' f7 g9 V& {math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
c9 i- k" T9 i# M8 i- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
1 d9 f2 N# E5 V! u* W - floor(x)! K, I- @% w$ M* w9 x% R% J1 F- N
- Return the floor of x as an int.
4 H) T" o, g# ^ - This is the largest integral value <= x.: Z: k3 d z1 d5 \* D" M
- >>> math.floor(4.1)
& s- @' Z- T- p1 O - 4
; D7 ]# C6 {0 S% V2 b1 H - >>> math.floor(4.999)6 Y5 b" I7 z1 q' { [ O" X1 v
- 4
|( h8 a/ y. k' T) L. d/ P' S/ w/ [ - >>> math.floor(-4.999)
9 p9 l# U, `& R/ G - -5/ |) a, E! B: B3 q% E
- >>> math.floor(-4.01)
' g. Q0 r+ S5 A W3 g" B - -5
复制代码 : s0 t/ m2 q1 g0 W
math.pow(x,y) 返回x的y次方,即x**y9 B2 h) @1 O, u& K
- #返回x的y次方,即x**y# U6 X1 g1 a m Z* {; z2 n
- pow(x, y)
) S: C" j7 k0 _3 D - Return x**y (x to the power of y).
1 R# b% r* A Y - >>> math.pow(3,4)
, \* Q; ]4 d3 n# ]0 X2 c% K - 81.0
# D4 R) A S& b7 c3 ~: r h - >>>
) L; I. T4 p9 U, d - >>> math.pow(2,7) {+ c% n F, O3 z
- 128.0
复制代码
8 w. `" X$ q* G# N; J) M+ Amath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
+ C$ }! m1 m1 U) r" b. y$ W! a- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
, @- w' R4 J! d" P+ e" V$ ? - log(x[, base])
+ {9 O5 m8 H; G* w( N - Return the logarithm of x to the given base.
K+ h$ @! n1 @# T' D - If the base not specified, returns the natural logarithm (base e) of x.
% N1 H' b7 G5 T: x* \3 g8 r - >>> math.log(10)
, K0 J' f# z! Q- E2 g6 G - 2.302585092994046% c, s6 e& V+ b! N8 R
- >>> math.log(11)
- q, g& ^& J9 s8 N, q! J" \1 s - 2.3978952727983707
7 Y/ Y$ H1 Y. s! Q0 g4 i - >>> math.log(20)
/ C$ ]9 z: u% r/ F9 j2 U% [ - 2.995732273553991
复制代码 9 Y3 l" }( V+ H4 y
math.sin(x) 求x(x为弧度)的正弦值
2 I l+ Y4 W5 W' F1 U+ p: `( i) X- #求x(x为弧度)的正弦值1 u! F) @. [ d/ B
- sin(x)- E% l* G' w# k. S: Y8 x2 Z
- Return the sine of x (measured in radians).
' ]/ a( m& Z7 B9 [1 ~0 }, t3 O3 k - >>> math.sin(math.pi/4)
1 ]! l6 ]" L @- {9 }6 y9 J z+ K9 o - 0.7071067811865475- l1 @! {/ l9 r1 N+ A3 _4 {; f
- >>> math.sin(math.pi/2)
' q) [3 _2 G! m# F i2 l0 O - 1.0
; k, q: O" s p1 i- U! ` - >>> math.sin(math.pi/3)/ `( V* |' A: e3 H0 G
- 0.8660254037844386
复制代码 0 B& `' [, o& y8 q3 S
math.cos(x) 求x的余弦,x必须是弧度( Z' p/ E* [) s( {0 q+ X' V. A
- #求x的余弦,x必须是弧度. R. O& Q5 m# c" D' J) ]" T+ v
- cos(x)
$ j5 |) E5 d, o - Return the cosine of x (measured in radians).4 e: M, @8 g9 v) E
- #math.pi/4表示弧度,转换成角度为45度
- |9 D5 X7 z- r# T4 `- Z( z* Y - >>> math.cos(math.pi/4)
5 S9 r1 @) M# j! K - 0.7071067811865476
/ ?5 q" v9 `& z8 o - math.pi/3表示弧度,转换成角度为60度
$ V. f: T# o" P [5 N% ^2 m - >>> math.cos(math.pi/3)! `0 b9 L+ G& H/ x! j
- 0.5000000000000001/ _& g1 w7 s* C4 t/ x5 B
- math.pi/6表示弧度,转换成角度为30度
. w8 W, A0 `7 D0 b1 z3 ]( N' ~ - >>> math.cos(math.pi/6): u2 b: s3 R# X" l+ M
- 0.8660254037844387
复制代码
! a7 {# S ]$ B3 b% m2 x) qmath.tan(x) 返回x(x为弧度)的正切值
9 f- a. g0 J1 [3 g; L9 x* d- #返回x(x为弧度)的正切值0 z3 C- g t. v( M* _7 o* y' o1 r
- tan(x)
2 C0 @* ^. `( G& V+ J+ k - Return the tangent of x (measured in radians).1 ^& W- X: t* \ Z* `' `
- >>> math.tan(math.pi/4)! u L: E( n: j* B0 Z& {
- 0.9999999999999999& o0 C; y! i O. x
- >>> math.tan(math.pi/6)
; G3 I. N$ R0 g+ j4 ]" t - 0.5773502691896257
. r2 x) L9 o4 e9 a4 H8 H; b - >>> math.tan(math.pi/3)1 m1 R! x. @& h! c' v
- 1.7320508075688767
复制代码 / c/ l* }7 q$ z5 Q6 t( X
math.degrees(x) 把x从弧度转换成角度9 c0 ^) M3 E+ @# {3 B# X
- #把x从弧度转换成角度
8 {+ Z# s0 G8 r. w - degrees(x)+ L t( }0 @2 P; A9 B! E" A' W
- Convert angle x from radians to degrees.
+ d8 U0 y# m$ t8 d( H" k6 j7 q
/ J$ p1 ~, ]7 M. p- >>> math.degrees(math.pi/4)" M- l) r) Q8 ^0 B9 y
- 45.0
8 q/ |% U/ ~1 F2 b* ~2 | - >>> math.degrees(math.pi)+ L* P" o+ ^1 G
- 180.0
" A: e% J& h* r- [* F - >>> math.degrees(math.pi/6)3 Q* l. k1 R0 ]
- 29.999999999999996$ r! x3 T; J# q% Z% `6 L! ~ C/ t
- >>> math.degrees(math.pi/3)
2 V' Y9 H! f/ i! A& ^ - 59.99999999999999
复制代码 ; L1 H2 p( t. ^$ `0 L) e; N: {
math.radians(x) 把角度x转换成弧度
6 p7 ~7 A/ g2 J( k9 v4 A0 D. x- #把角度x转换成弧度8 `2 x# L, R+ x8 V, L$ n' S# q( L
- radians(x)
" j* N j3 e# H8 i P# O! P h - Convert angle x from degrees to radians.( M0 w3 @+ o. M8 D
- >>> math.radians(45)
! ^6 O! O/ P% A& p6 ^0 c$ _ - 0.7853981633974483
0 t. q8 b& k6 |& H' W- f3 F - >>> math.radians(60)* q( A ?, R8 r+ B
- 1.0471975511965976
复制代码
6 S# i, K& F5 e+ u' t$ o1 V& Dmath.copysign(x,y) 把y的正负号加到x前面,可以使用0( x1 V3 Z9 F9 O( C$ ]
- #把y的正负号加到x前面,可以使用0
6 }: @$ ~' O- I* h$ c - copysign(x, y)
, @/ l- Q* P! ^# [" {5 I( o - Return a float with the magnitude (absolute value) of x but the sign
4 c6 T8 ], s2 `* P) B! d% c/ |( h; v1 n - of y. On platforms that support signed zeros, copysign(1.0, -0.0) * d W- ]9 w/ B; F
- returns -1.0.* Q& d; ]2 b% m5 j/ _
: L1 V4 G! F+ Z0 t! d- >>> math.copysign(2,3)
/ K9 q% R; ^! A( ^/ q" u/ ~5 R) X - 2.0- _) h/ j h0 ?( ~' K5 S
- >>> math.copysign(2,-3); S F D& q9 s7 n4 b# v
- -2.0
" S/ e* F2 G) l' j2 |7 I - >>> math.copysign(3,8)
9 e, S( W- R, p" e) y- x) {) _ - 3.09 }4 n* M9 F6 C( a
- >>> math.copysign(3,-8)9 J/ W& q) x+ M J Y
- -3.0
复制代码 $ A, Y- d- z5 f" G
math.exp(x) 返回math.e,也就是2.71828的x次方
) b" P1 j" R2 S8 L, U- x- #返回math.e,也就是2.71828的x次方
# s! M1 Z" x/ M( w - exp(x)1 V$ E7 u! N* n" P2 V4 _# a
- Return e raised to the power of x.6 @7 ^1 Z9 A4 c$ {) \8 L
. t+ ^. P6 d; M4 B# \6 m- >>> math.exp(1)- h1 ~$ \" P3 I w4 k# K: U0 E7 E* G
- 2.718281828459045- Z! z. z) _' z! b. i9 v2 W- F# ~
- >>> math.exp(2)+ S, @) a5 E2 [ y5 D" S
- 7.38905609893065
* L5 i2 N1 z7 {+ l - >>> math.exp(3)3 X3 `5 ^: B7 Y' N/ R' |
- 20.085536923187668
复制代码
, G7 d+ s; `& p% `, w2 o- omath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减11 `5 \; i" b9 M
- #返回math.e的x(其值为2.71828)次方的值减1- ]( z5 |3 k5 ]( d
- expm1(x)
( R: q1 o3 G1 k: N% B- g - Return exp(x)-1.
7 ]! T) v3 [8 {+ t+ M& ] - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
: m1 [' q g: @ j
- |3 B( G4 J4 |) }- >>> math.expm1(1)
6 Z" W: u3 _% h1 o" U( o8 Z - 1.7182818284590455 u: w% I* Y1 v" ]) D6 {0 T9 b! Y
- >>> math.expm1(2)
1 U a0 [- y0 D Y) T - 6.38905609893065
/ o' K* g5 ]$ N3 p - >>> math.expm1(3)" h& N6 d) P# F, G$ x9 d4 x" H, N
- 19.085536923187668
复制代码
3 y$ a# n! v7 g: @math.fabs(x) 返回x的绝对值
, d6 @6 _; U; i; i- #返回x的绝对值
6 O+ P7 T6 d+ |8 a - fabs(x)
, G$ |4 r) @6 \$ |+ X' X - Return the absolute value of the float x.# }: V: ^: d3 ]9 g
- ( ]8 p s. B/ g$ X* N
- >>> math.fabs(-0.003)
$ l X( e1 L& {* R, r1 W$ U - 0.0033 A: `$ h* {7 w" i4 W
- >>> math.fabs(-110)2 W5 a4 X5 t5 b* O
- 110.0) G$ h1 M2 I6 z. X, `2 _- z
- >>> math.fabs(100)
$ O8 q. q$ u1 {1 `/ V* j3 S- c6 ^ - 100.0
复制代码
( a! r U3 Y3 Tmath.factorial(x) 取x的阶乘的值+ l( \ N9 z" d8 h3 a* H
- #取x的阶乘的值6 g! S9 l& w9 Z
- factorial(x) -> Integral
Q: ^# {" u5 i9 p, y& S/ L' } - Find x!. Raise a ValueError if x is negative or non-integral.
" i* s5 ]; | O+ P8 f - >>> math.factorial(1)
/ p& S/ \9 U; U% e: ^ - 1, }$ A/ l# y; D5 z: Q' B6 m
- >>> math.factorial(2). g: W2 \7 R; ^) b- u
- 2& G @4 o6 [3 G& M0 m5 ^
- >>> math.factorial(3)& G. t% u M' h
- 61 i$ C) s2 Y T- v1 u4 D
- >>> math.factorial(5)
] z b+ t, `% `6 [# H7 ~& } - 1205 m: J0 M! i9 ~+ H; e3 Q
- >>> math.factorial(10)
0 `+ ^, Z3 }- Q: E - 3628800
复制代码
* S% S: e! Q+ U% T/ nmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
( C8 |8 a: J2 X6 T; I5 o- w) T- #得到x/y的余数,其值是一个浮点数; v7 W8 F. O% t# A& X! {) m7 f# S
- fmod(x, y)* Y8 I2 {, V- V7 T. D" o1 _/ _
- Return fmod(x, y), according to platform C. x % y may differ.. N% k) V( _: X8 r5 h
- >>> math.fmod(20,3)
: j0 ^7 B: y" ~' `; }9 m) P2 F" w - 2.0) B. K W2 k7 U6 W) [! b2 V+ _
- >>> math.fmod(20,7)
9 a8 J8 @" \2 _4 B; S: l3 s" H) i - 6.0
复制代码
, {. E& ]) `& j. B7 B. x# Wmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围4 F% ]' U0 V# I" u
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,6 \* @- A4 s) i, z6 I1 Z, i
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值% I+ W( L6 U7 K' r
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
: {. k' l# U6 Q1 K1 a - frexp(x)
' e3 S0 y* }% H8 Z4 H7 [9 U" X6 ` - Return the mantissa and exponent of x, as pair (m, e).
" }& R; W* O+ i c( |! K, t - m is a float and e is an int, such that x = m * 2.**e.
5 w1 P4 k$ `8 O9 o0 X - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
1 J0 X& w1 K9 G/ o- p - >>> math.frexp(10)1 C) |! Q) ]* W. P$ C5 E# Q; K
- (0.625, 4)+ r" b1 M2 v; k
- >>> math.frexp(75)! r+ w+ e5 Q$ @- O
- (0.5859375, 7)2 {1 G p5 I4 s/ y" J4 X) ]
- >>> math.frexp(-40)
; h& L& N* }- B( W6 e! b) h5 ^/ { - (-0.625, 6)2 ^3 `$ |3 n* U
- >>> math.frexp(-100)" n! X, g1 e' x; ^' j$ T s1 e
- (-0.78125, 7)
( c5 f/ Q1 |% l- V2 Q ~, T - >>> math.frexp(100)- Z$ x2 S% B* b7 K2 v9 l4 i
- (0.78125, 7)
复制代码
8 W5 K R$ G( a) U, Y7 rmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)8 ]7 O7 B3 u, q0 J1 {/ Y* U" T
- #对迭代器里的每个元素进行求和操作' t( N( B4 V! ~, f
- fsum(iterable)
( h/ u+ m: J$ V# u - Return an accurate floating point sum of values in the iterable.
" w3 r6 `# W+ w6 U - Assumes IEEE-754 floating point arithmetic.! h" S* N3 z& v3 g7 ]1 d5 N
- >>> math.fsum([1,2,3,4])
% ^; h7 a: [- F- q - 10.0
1 i- O3 @, j. z0 c+ X! s; m8 u - >>> math.fsum((1,2,3,4))3 n# j% R# L$ J" N
- 10.0
% s. ?/ S4 {: c/ ` - >>> math.fsum((-1,-2,-3,-4))9 _+ _& t9 M. \% f
- -10.0
# y: X% p1 h4 ^/ ]) D - >>> math.fsum([-1,-2,-3,-4])
7 B7 ?. K8 R# e; X8 d" L - -10.0
复制代码
; h1 A' h) T9 E: r" Dmath.gcd(x,y) 返回x和y的最大公约数
7 j h7 x7 }+ X+ H- #返回x和y的最大公约数" X! X/ [( u% [. A/ q+ D) j X
- gcd(x, y) -> int
9 v7 ]& t: l! ~& M2 T - greatest common divisor of x and y
; `( J1 _1 N5 F$ d+ _) w - >>> math.gcd(8,6)
2 Z7 c) r! q4 `5 `- }8 U - 2, |& [, s# ?$ ]( ~0 [
- >>> math.gcd(40,20): d+ e4 p; z' _" U( G' k3 E
- 20
8 m& \ d5 ] n - >>> math.gcd(8,12)" e3 P8 c. H7 f+ z
- 4
复制代码 7 q. R* W6 m* [$ Z, S
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
. E9 P9 |# H+ c- #得到(x**2+y**2),平方的值, o: u3 s# B- d) ^
- hypot(x, y). R8 h$ w, C& g" C" {
- Return the Euclidean distance, sqrt(x*x + y*y)." v, {1 O" S6 `0 _ ]
- >>> math.hypot(3,4)
6 Z8 v2 D( D/ X# H - 5.0
% G% j0 T! n. O% R$ v# W* y - >>> math.hypot(6,8)
2 H5 s4 A: o. K* n - 10.0
复制代码 . v/ u/ d) H) Z, y+ w
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
- c- V. n% _7 H5 S# q- Z( t- #如果x是不是无穷大的数字,则返回True,否则返回False
. W4 M+ `# W4 B* i - isfinite(x) -> bool
3 g: X' j, h8 h0 }, i$ h - Return True if x is neither an infinity nor a NaN, and False otherwise.
' k" C, n1 G F) Y( C$ N! k$ U - >>> math.isfinite(100)
% t' ?( Z- K+ y2 P9 V# j - True
. F+ W" F) A" O5 n$ l3 O: n - >>> math.isfinite(0)
* |. {5 V. [1 v" Y- ~( v - True
: C3 {4 P# H4 j0 x+ o - >>> math.isfinite(0.1)! w/ e* n& D; _* I8 [2 \, b
- True
4 a9 n4 ]( ]" u* A z; a - >>> math.isfinite("a")
7 p$ P- u4 M7 G' @9 N - >>> math.isfinite(0.0001)
; F) n" f( _) I3 a# [, _" E - True
复制代码 ! f& @. W* I7 [ I1 |% A: Y
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False- B+ ?. k. i$ D) g, f
- #如果x是正无穷大或负无穷大,则返回True,否则返回False6 x; g5 ?5 F) \# }: G7 O
- isinf(x) -> bool4 e& q$ l9 W" S7 N; {( o4 x
- Return True if x is a positive or negative infinity, and False otherwise.* b0 z2 N$ m9 |6 n+ _
- >>> math.isinf(234)
) g2 @8 r8 J9 A9 ~3 i - False7 \0 C) a, E% w) s$ E4 h, ~% r
- >>> math.isinf(0.1)* Q1 R/ z% F( Y F; W* b# K
- False
复制代码 . s9 R7 E4 W1 t7 ^4 C' Z+ R r+ [
math.isnan(x) 如果x不是数字True,否则返回False
, A: Z* S! N' U1 O' q9 j5 [' p- #如果x不是数字True,否则返回False
' m* a& x' T' C7 [ - isnan(x) -> bool
1 I' @- K# a, T5 E7 y) t - Return True if x is a NaN (not a number), and False otherwise.3 U1 p% A. @# f& S
- >>> math.isnan(23)
1 A0 A7 B! [+ d6 {. |/ G+ {7 M. k - False4 B. ^7 R% @/ t% s4 c$ s R+ J
- >>> math.isnan(0.01)
4 t4 U! R5 [; Q; R9 H - False
复制代码
6 _0 L: w+ A2 T$ ^& @math.ldexp(x,i) 返回x*(2**i)的值5 |+ L2 }, _$ A: { }
- #返回x*(2**i)的值
B/ Y# g7 p( m - ldexp(x, i) a, Y- Z8 Y/ |& p& x
- Return x * (2**i).
) H0 b$ `( W' {5 N4 v - >>> math.ldexp(5,5)
! p5 W# L2 x! o+ ?% V" h - 160.0
! ?) i n+ j0 C* j5 J - >>> math.ldexp(3,5)
" M6 r: X" W/ Z9 |7 o% L - 96.0
复制代码
; b4 [; p; K0 F9 [, u, e5 M& S( Kmath.log10(x) 返回x的以10为底的对数" t: V4 e4 U/ t8 e
- #返回x的以10为底的对数' @5 x# ?0 P* s
- log10(x)+ ] B# B6 `6 i5 w8 Y e/ L
- Return the base 10 logarithm of x.
' v" X. p* @. c( V( j - >>> math.log10(10)* N3 U0 N1 |+ i& }
- 1.0$ U: \5 R$ Z( B
- >>> math.log10(100)
: M9 q8 |; x( ?$ ~/ [- p - 2.08 y( p( s! U3 i# n4 X) i
- #即10的1.3次方的结果为208 d z% S! |' n
- >>> math.log10(20)
. N; I- p# L- h3 H4 Q - 1.3010299956639813
复制代码 ( F- ^7 N6 R' d0 }5 i) E
math.log1p(x) 返回x+1的自然对数(基数为e)的值
" m) \7 E9 q2 g) Z" R0 o- b/ H- #返回x+1的自然对数(基数为e)的值# v# Z/ ?' Z% @. _* Q, k, N
- log1p(x)
9 e/ L" U& |+ F7 D9 B/ A& ] - Return the natural logarithm of 1+x (base e).1 K B$ e" @3 x2 `8 c
- The result is computed in a way which is accurate for x near zero.( t3 C' k, U/ k' l# ^6 y- L
- >>> math.log(10)
$ p0 S6 A/ @+ D' W; b5 X - 2.302585092994046
1 k, i& i6 _9 c- y7 m8 B5 g - >>> math.log1p(10)
& T7 d9 f! `6 j( s' `9 T - 2.3978952727983707
. d! @; ^0 C1 ^4 J; m - >>> math.log(11)1 n8 u8 k5 l. ]2 H8 n
- 2.3978952727983707
复制代码
0 t/ \& t! i c l" [4 m+ U% G- A2 ]math.log2(x) 返回x的基2对数4 t0 @7 z+ `# h3 w* u
- #返回x的基2对数
8 B- z0 W1 A% _ - log2(x)
* }8 P$ W$ R. W! z$ B' ] O# I; A) L4 T - Return the base 2 logarithm of x.
- h Z5 E9 q1 Q. N$ { - >>> math.log2(32); i) Q) v' v7 b; @3 G( ^5 H
- 5.0
! o% Y' u. k* p+ R! E5 W: h/ k7 @ - >>> math.log2(20)9 t6 | F' L# e2 I, s
- 4.321928094887363# T0 A! j! R- \% d- M! R
- >>> math.log2(16)# b- o, o( |& n
- 4.0
复制代码
2 H2 t/ ^' G+ Q( \* |6 G4 X$ Vmath.modf(x) 返回由x的小数部分和整数部分组成的元组
0 [3 `, l ~, [, t( R/ @/ f. C- #返回由x的小数部分和整数部分组成的元组# h [( e' b& V4 S: f8 l z
- modf(x)/ @6 D: ]7 n+ m
- Return the fractional and integer parts of x. Both results carry the sign
2 I: |; y8 ~5 S - of x and are floats.6 ]! {# T4 ~- W3 j/ F& b; |
- >>> math.modf(math.pi)6 L3 {& R8 J& D) H
- (0.14159265358979312, 3.0)
9 S5 T$ q( @; k# T/ L! D - >>> math.modf(12.34)! S* l% K0 J: A D
- (0.33999999999999986, 12.0)
复制代码
. W$ a% E9 @9 |( H7 K f% Kmath.sqrt(x) 求x的平方根
; U2 J- S/ T, w- #求x的平方根
/ Z8 O2 Q' ^4 N1 M - sqrt(x)9 q3 G& j0 t) y$ E
- Return the square root of x.% G7 H# i0 O' N5 O8 l
- >>> math.sqrt(100)& d9 t+ L& v0 P, Z2 Y: l- w0 Y
- 10.0
3 k5 ^8 P8 Z+ \$ S - >>> math.sqrt(16)
3 n) K4 [+ d6 `7 j4 {4 C8 D- r# L - 4.0. _* k6 U! N" I) r; j/ j' r) R
- >>> math.sqrt(20)
; S0 Q3 g2 H. B - 4.47213595499958
复制代码
+ A6 t5 R$ l& U- d1 Q( ]" q3 Qmath.trunc(x) 返回x的整数部分- #返回x的整数部分9 _; C$ c: \# N. h, s
- trunc(x:Real) -> Integral( x( l$ q/ n( O, d) h
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.1 u+ w1 {% B- B# v
- >>> math.trunc(6.789)
: h% w- F" ^) ]7 F, l+ z& g+ T - 6- A3 U) a; {4 S) h I- ?
- >>> math.trunc(math.pi)
2 h5 @1 x+ j" F I& z* T3 d - 3" y4 R9 b5 T C0 }! }( H. c
- >>> math.trunc(2.567)2 M3 m7 N) c Z) {. r2 _/ T0 b
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|