马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
. h- X4 _" a; x9 v【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
4 I5 Z% N( P5 t1 Y8 m& |& N" G. e& Y+ p* h) G( w! f- E
方法1:
: s3 k! |* I" f4 [- >>> import math
; ?4 ~2 j; {! H5 H - >>> math.sqrt(9)
& N; T2 s/ M- I0 x: d - 3.0
复制代码 方法2:% Z, q: p( b. S3 y* P; H
- >>> from math import sqrt
# g! C3 }5 y# n9 |3 Q5 B# N; e& i/ X, m$ I - >>> sqrt(9), P- W" w% t- [8 h- g# O, f
- 3.0
复制代码
% R% S0 s9 Q6 V3 y ! z, ^; {/ R4 j7 z& _% B% T6 h
math.e 表示一个常量) I7 \ o# {. Z& C
- #表示一个常量& F! D5 o+ ?9 O
- >>> math.e
3 B E, ^ F* x5 l, H - 2.718281828459045
复制代码 3 r& j* `9 W7 g S+ w# H
math.pi 数字常量,圆周率
# ?5 R4 b( T" R1 P- #数字常量,圆周率
/ Y3 D8 _/ v$ } - >>> print(math.pi), D3 u! n& o* o- `) q
- 3.141592653589793
复制代码 0 T8 X2 I2 v; A$ H3 c# t* e/ {
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 ? s. S4 D" Z$ E4 H: a3 ~6 A" h: `- #取大于等于x的最小的整数值,如果x是一个整数,则返回x H. v5 v$ r8 j
- ceil(x)! h5 \+ H2 G7 b/ v0 E6 k, k' P
- Return the ceiling of x as an int.& _5 f/ I8 Y2 a0 p/ I8 B! Z5 @2 q
- This is the smallest integral value >= x.
' ~! v6 d c& Q! k) `
# A# k( m5 P0 h) D- >>> math.ceil(4.01)* {$ p. r1 X5 H1 Y9 v& u9 \( x# q
- 5
! i$ W) ]3 C4 u, j( t3 f U - >>> math.ceil(4.99)8 ~) } e* a7 j) l( a- {
- 5
! p# O' J4 e; L2 P0 J+ r6 o. `+ W2 ?8 e - >>> math.ceil(-3.99): ^. b! E' ^4 r3 r3 `9 y" @/ ~
- -3
- q" O' u: z+ K6 s7 w3 G7 ~5 C - >>> math.ceil(-3.01) W) L: `& E) }' z8 C
- -3
复制代码 ( I. D; j) }6 r% s
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
$ c0 M( g. m& @4 n* J; o. d- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身) K6 o8 J8 ~4 u) \ H
- floor(x)
g( e$ A! V; B7 o/ C! I( J - Return the floor of x as an int.2 E9 x9 m7 R! m& [% r! Z4 }
- This is the largest integral value <= x.
, c, t6 v0 v1 q2 k7 c6 [* _ n - >>> math.floor(4.1)
7 u! N. v- x6 ~% \- {, w, V - 41 s$ x+ [% h. c0 H
- >>> math.floor(4.999)* ? g$ G* O9 Q9 `6 e
- 4
( J4 E9 ?$ [5 U+ p8 n" E/ _ - >>> math.floor(-4.999)6 f. M3 |' N. K# g9 A- k
- -5
3 g$ p/ r2 p! T: W, g# T/ H+ @) y - >>> math.floor(-4.01)
1 \- T) _3 ^+ A% [ - -5
复制代码
$ o$ Y; H9 v3 Z* W% b/ ~' A# Rmath.pow(x,y) 返回x的y次方,即x**y* j9 q$ ^' D" t& P2 p+ R3 h
- #返回x的y次方,即x**y" o5 c8 N2 }( K/ J4 M/ U
- pow(x, y)1 R; Z; M# k2 V0 R
- Return x**y (x to the power of y).1 }$ H/ E/ `, H' P6 r# t
- >>> math.pow(3,4)4 G( u+ b4 _& P6 n, @1 }( I$ ?
- 81.0' x" p1 }' m" j' Y& [5 }$ i) q2 \
- >>> 2 f* U2 @ X8 [! g+ i* R! B
- >>> math.pow(2,7)- y+ j8 o! ~8 J8 \# Q$ K
- 128.0
复制代码
9 o/ J3 Q- A) C, y7 q, ?0 |% Umath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
1 }1 R. I+ W% n' O- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)
0 k0 |' L \& a+ ]& i - log(x[, base])* d5 c" Y9 n8 ~; L% y; e3 n6 d2 L# P2 `* S
- Return the logarithm of x to the given base.- K1 d6 F8 t- ]2 h- m( N- K
- If the base not specified, returns the natural logarithm (base e) of x.
) a t5 E9 B! {- x - >>> math.log(10)
9 A: g1 w3 n& Y4 n8 Q7 U - 2.302585092994046
/ K3 `* T* O" w3 I8 p - >>> math.log(11)5 _( F# P4 E: k8 i% A
- 2.3978952727983707# X0 V3 \4 ]+ V2 K- A
- >>> math.log(20)+ f4 C3 Y+ ^* N& Q2 C8 }2 p; B, O8 g
- 2.995732273553991
复制代码 * M2 r/ P- Q) v7 n4 N5 w. k
math.sin(x) 求x(x为弧度)的正弦值9 S) ^4 |% n' o5 {/ O- T( [$ I2 g
- #求x(x为弧度)的正弦值9 z$ X0 |! E. D1 x; ^0 [
- sin(x): o2 q: B: D! j) E2 D$ l- M
- Return the sine of x (measured in radians).
1 T% Y) z- V. P - >>> math.sin(math.pi/4)) X" A5 t0 X+ x! i% S4 `0 d
- 0.7071067811865475# Q- c3 U! T4 t/ t$ o6 w
- >>> math.sin(math.pi/2)5 L( w8 X7 g* m8 y, a
- 1.09 g% I9 j% ?8 ~3 ~8 H2 e
- >>> math.sin(math.pi/3)
) J# K4 w4 m2 Y/ C2 n - 0.8660254037844386
复制代码
: }4 P0 s6 d5 Z' a+ e& ~math.cos(x) 求x的余弦,x必须是弧度# r1 e3 e9 s3 N
- #求x的余弦,x必须是弧度1 A' Q$ j! k/ L& b
- cos(x)
$ J+ h" g) e% j - Return the cosine of x (measured in radians).+ @; j) {7 w. @( Y: t: C
- #math.pi/4表示弧度,转换成角度为45度
, r9 Z4 V, n% |' H$ i - >>> math.cos(math.pi/4)
q9 z" H0 _5 ]/ f p+ F3 w! W' f - 0.70710678118654763 U" i0 O- B0 c' S1 Q4 H; L4 y! F
- math.pi/3表示弧度,转换成角度为60度
* t" o j& P' M. {& p0 |8 Z - >>> math.cos(math.pi/3)
0 z! i# D4 C, ]1 B ]* H - 0.5000000000000001
8 ~* S5 G# D, H% T: x9 c/ a - math.pi/6表示弧度,转换成角度为30度 u% k. J+ H9 p
- >>> math.cos(math.pi/6)
2 W9 M G9 t4 {! p% q9 D - 0.8660254037844387
复制代码
e& h; }7 |" S% q3 v# }8 g7 Fmath.tan(x) 返回x(x为弧度)的正切值
: U8 E/ A) o$ c' h* d- #返回x(x为弧度)的正切值" P+ G+ X" l9 R& T4 c* _8 ?
- tan(x); W' L7 Z7 ]" l7 D
- Return the tangent of x (measured in radians).& `* {6 j2 n1 s& a- c Y
- >>> math.tan(math.pi/4)
- i2 s! [* v3 E: Z0 { - 0.9999999999999999
, B9 F3 L t9 n0 x8 H - >>> math.tan(math.pi/6)0 V) ^5 F# \, W' A
- 0.5773502691896257
5 H( @; N8 @% X, p - >>> math.tan(math.pi/3)
1 U; f s- G2 }; c' b - 1.7320508075688767
复制代码 8 O N M% \/ ~% g1 m
math.degrees(x) 把x从弧度转换成角度
9 Y" n$ i% a( j/ U1 O, Q- #把x从弧度转换成角度
6 N7 t+ B; E2 b - degrees(x)3 D, O) r. N2 q9 q2 @
- Convert angle x from radians to degrees.
! S8 A9 p8 M& m' j - " j t/ A6 v5 S
- >>> math.degrees(math.pi/4)
8 ^3 S5 o+ @4 G: H! l2 {( f) `5 y - 45.0) k9 M2 v% O8 I
- >>> math.degrees(math.pi)
# r! k4 g% F' \0 `3 { - 180.0
% X& b5 B" e" j9 C% G - >>> math.degrees(math.pi/6)$ p/ [* ~$ ?5 e/ `3 b
- 29.9999999999999962 L$ q- p( X0 d J: W! ]) i
- >>> math.degrees(math.pi/3)
9 ^( J5 ?0 a1 h" e$ c" F8 V& Y - 59.99999999999999
复制代码
4 s8 S9 `! g: wmath.radians(x) 把角度x转换成弧度
9 V- f" u% ^6 b( B5 ~- #把角度x转换成弧度6 ~6 L7 {5 _, w7 @( `
- radians(x)2 q3 }# ] @& _; Y2 N2 i
- Convert angle x from degrees to radians.% i8 J- n4 ^- t: u: [' Z5 [( C
- >>> math.radians(45); s# P# x. Z0 n7 }' w5 S
- 0.78539816339744837 [+ M* Q3 t8 N2 a- C4 }5 ~4 S ^
- >>> math.radians(60)
: N! t8 p) `* a& z6 s# ~ - 1.0471975511965976
复制代码
Z* w5 \+ ^' R. a' K; L0 M. t2 Vmath.copysign(x,y) 把y的正负号加到x前面,可以使用0& Q8 G4 h* a' w6 p% G+ P
- #把y的正负号加到x前面,可以使用0
^9 w' V6 f0 V2 W% M( U - copysign(x, y)
; N$ {" D/ G& H( D, w8 Z. F! Q - Return a float with the magnitude (absolute value) of x but the sign . o7 u7 g8 ?# ^- u
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & D H9 D) n( r j
- returns -1.0.8 _0 i5 ]+ t( Q" c& ^ m- e4 C
- 2 ~# j+ j, Y, {+ a5 k& @! ?7 n
- >>> math.copysign(2,3)
& a% [, X- h: F. H, k P - 2.0
. r1 F7 ~4 H" D9 a8 D: r& K: Q% v0 C) | - >>> math.copysign(2,-3): G2 i2 C; U% u& u3 }
- -2.0
8 f# w2 i/ r0 @& H" ?2 ~4 ^# L - >>> math.copysign(3,8)! b/ g- O$ W8 J7 M2 U9 [
- 3.0+ b: D& e/ @- ]# Y
- >>> math.copysign(3,-8)
6 U9 w% t% @5 J% S! M - -3.0
复制代码
& A) E9 ~7 n' [' l1 Q2 e, i7 u+ Dmath.exp(x) 返回math.e,也就是2.71828的x次方$ S( d3 K8 C6 v
- #返回math.e,也就是2.71828的x次方1 w& Y9 i3 M5 p
- exp(x)3 f! L) f$ Q( h6 f
- Return e raised to the power of x.7 o- I& {* m0 s
6 D" d" g0 H" S; @9 q- >>> math.exp(1)" u! ~8 L+ a) a6 O" F
- 2.718281828459045
! \/ M# a/ [2 ~+ j/ p - >>> math.exp(2)
$ T6 v0 {9 c, f% {3 _ - 7.389056098930658 f0 [- W0 B& \4 Y3 X* T7 {
- >>> math.exp(3) @3 d6 j/ ]% ?8 a: j+ X
- 20.085536923187668
复制代码 8 B, g* Z8 N5 f1 S9 {
math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1) D1 z$ w+ u5 r: q
- #返回math.e的x(其值为2.71828)次方的值减1
7 j/ x: f0 M8 @3 d5 | - expm1(x)* O$ r) y/ ^. k/ y5 r& K) f
- Return exp(x)-1.
" `4 m- |$ j# n' Z. X8 f) e - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
& _: `& }! s4 U" z. k" f - & \- b7 q; x& z+ n3 @
- >>> math.expm1(1)- M `* k& n; M7 l9 f
- 1.718281828459045
, R6 C, }3 Z8 I' g - >>> math.expm1(2)
* E, l: k; ]1 Q A6 X7 n - 6.38905609893065
I3 Q8 K j2 x% p1 p9 [3 u! N - >>> math.expm1(3)
) z9 E4 H2 N9 w# O I5 c; A - 19.085536923187668
复制代码 . S7 C* \% X: n0 S9 N5 T! m
math.fabs(x) 返回x的绝对值
6 ` D8 N2 ]! O5 W/ H c- #返回x的绝对值5 p/ H2 L! z2 Z% T
- fabs(x)( J- Y) x* G6 m) t I- B. G
- Return the absolute value of the float x.
4 C# }+ M$ }8 A' D. h" S - ' H: z$ x8 I! F8 J1 e
- >>> math.fabs(-0.003)- x9 |5 E: e7 y& G4 X
- 0.003
# b1 i4 N. g9 m( j0 @! F3 i. _7 e - >>> math.fabs(-110)
, L' y" p$ t, u( L" m) Z - 110.0
" [, o) F4 y+ k' }" Y' B, R - >>> math.fabs(100)0 m2 D4 E* o+ d$ m( k7 j/ M9 @
- 100.0
复制代码
/ @0 d4 e5 `. K# M1 Hmath.factorial(x) 取x的阶乘的值
/ p# x( Y% j, z) i- #取x的阶乘的值
; K1 U) @% h2 Y4 t( x0 ]$ E - factorial(x) -> Integral: B7 G y9 o$ J0 P3 U+ g
- Find x!. Raise a ValueError if x is negative or non-integral.
- S% h2 g. X) @* E! T - >>> math.factorial(1)& n; n- a, t+ K+ ]; v1 { [
- 1
' o- q/ P; f7 I' F( ` - >>> math.factorial(2)0 ?# f% ^3 ]! c! ]/ J
- 2
- r; t4 a! _) z - >>> math.factorial(3)
# `' h+ z9 M% J% ~ - 6% n8 }( I) [; a" [1 @) ]; j) j
- >>> math.factorial(5): E3 r) d5 ]: W' w9 W3 V% p6 t( w, m
- 120
" o+ s) N- t$ w% x/ v - >>> math.factorial(10)
+ @! G4 s5 ^7 @9 [2 T - 3628800
复制代码 8 r; G# t: A6 ~! _
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数6 h3 _' N/ L- x' Z, t+ k0 c; y! n
- #得到x/y的余数,其值是一个浮点数 D( N1 N7 r3 Y( X$ f o
- fmod(x, y)
$ u: {% m" o8 a - Return fmod(x, y), according to platform C. x % y may differ.3 ?! k/ O |4 T! s
- >>> math.fmod(20,3)
I: J1 S# k$ D! m" P - 2.0) E& S& ?! C! q% E( A2 s2 H) e
- >>> math.fmod(20,7)
+ N9 H/ \- q! g: p' n - 6.0
复制代码 ; o |4 Z9 _0 ~; m
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围9 e8 S! U$ h6 G' h# N1 W
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
9 E% m+ N. ]7 S - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
' N2 u3 ]2 N9 j$ T( l4 }7 q' C - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和10 S- e" X5 g Q
- frexp(x)' u: w" z6 B8 s# M* n
- Return the mantissa and exponent of x, as pair (m, e).
- J- c2 u$ R$ f8 p) R$ x/ [ - m is a float and e is an int, such that x = m * 2.**e.
" n, [ g9 r% ]! Q - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.: w, r; y; K' `' g" E' H( P% B
- >>> math.frexp(10)
' p6 n* x1 Y3 e8 U - (0.625, 4)
' z J& [; P H/ i. v: ]; L4 |5 V - >>> math.frexp(75)
7 ?7 e6 L0 _0 i2 L G5 D - (0.5859375, 7)
" H. F# l ?$ i+ x) V8 [6 y4 D - >>> math.frexp(-40)
( ]5 C9 T7 `- R5 D+ X - (-0.625, 6)
X2 X, x! P, R# V4 _. k( l% Q) \ - >>> math.frexp(-100)8 v4 ]: A7 {, B, P+ ~: U
- (-0.78125, 7)3 F" m5 ] T) C. D4 U
- >>> math.frexp(100)1 ^* d9 R1 j3 c* w- n
- (0.78125, 7)
复制代码
3 k- _& a4 e; u; G1 I9 v2 A! j, Ymath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
2 C& f' ]" o+ Y& Q- ^4 k& n- #对迭代器里的每个元素进行求和操作9 Z+ {( {) r# v0 {. w, m( Q
- fsum(iterable)2 N' {/ u) ]" x0 |& \2 d
- Return an accurate floating point sum of values in the iterable.
# \7 b/ T, V+ b6 `: @3 q! c" y2 w - Assumes IEEE-754 floating point arithmetic.! Z# @* F1 {/ w4 F1 ~& L b" S, o
- >>> math.fsum([1,2,3,4])5 O+ @& U. i) h" X) K" A/ p3 ~5 s
- 10.0$ J' d8 i2 _& _, u4 {7 z8 o3 L
- >>> math.fsum((1,2,3,4))$ `3 L6 k6 \) K7 S. {
- 10.0$ G9 m0 K! f+ ^5 V
- >>> math.fsum((-1,-2,-3,-4))
* Z3 k: T4 ?+ P8 K/ l* l& A3 d - -10.0
' f$ P1 \6 M$ v* [: F# z - >>> math.fsum([-1,-2,-3,-4])
& B! p/ y" D; b' l - -10.0
复制代码 # L: V/ Q: B' n6 _
math.gcd(x,y) 返回x和y的最大公约数) d3 s. D7 e B8 p6 P5 o
- #返回x和y的最大公约数
! ?) [* T3 E' p; r8 L - gcd(x, y) -> int& o" b+ @0 Z& H: W3 r Q
- greatest common divisor of x and y) |2 L1 i3 l y
- >>> math.gcd(8,6)- Y& v$ L( o0 \8 ? J. I$ {
- 2
: f1 B9 ] U/ o$ t! Y - >>> math.gcd(40,20)
3 M: a" C0 l& u$ j5 i: w) m$ g' f - 20
5 i+ O7 Q V, s8 F0 L3 V - >>> math.gcd(8,12)
" m" U% {3 Y! K! R- ? - 4
复制代码
7 O8 m R6 |7 J2 j1 T6 Cmath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
0 e7 r8 q% V8 c; I1 \- #得到(x**2+y**2),平方的值
* G/ H2 E' d& o - hypot(x, y)
% @0 c) P6 ^( q, [5 r - Return the Euclidean distance, sqrt(x*x + y*y).
8 @+ X% g$ }1 M& r - >>> math.hypot(3,4)7 E7 M; c! |( ^
- 5.0: a9 n0 t7 d- n
- >>> math.hypot(6,8)
5 {4 i) L% L" E* X5 Y9 B - 10.0
复制代码
% @* h; S: P, O& umath.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
! r6 N3 s& a0 ^0 Q! `4 {- #如果x是不是无穷大的数字,则返回True,否则返回False$ \9 d* u0 z9 G+ v- Q
- isfinite(x) -> bool
1 V# `/ _, s# a9 Z' M - Return True if x is neither an infinity nor a NaN, and False otherwise.
) @4 s9 O3 K8 I' ]$ D - >>> math.isfinite(100)* O) @7 P+ `" O+ ]' Z$ _# Z( ~
- True1 W/ b2 m; |+ Z' v6 r
- >>> math.isfinite(0)
( X1 D" W/ D% T% `" D - True" T8 R* N6 s) S. h
- >>> math.isfinite(0.1)$ g3 T, l, f; j6 l. ?8 b
- True
# M8 I/ e1 l8 L$ w5 s - >>> math.isfinite("a"). c: G4 M. A1 k0 ^$ m9 |7 [
- >>> math.isfinite(0.0001)
$ S) L, W/ k+ C) Y+ X/ ]( O - True
复制代码
7 Q7 V5 Q0 P1 Kmath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
) C: A0 x u! n0 D) a- q- #如果x是正无穷大或负无穷大,则返回True,否则返回False
1 B" s* F3 R7 p8 G - isinf(x) -> bool7 M" A5 k f7 a4 s0 e f
- Return True if x is a positive or negative infinity, and False otherwise.. {% S: D6 j& w1 W, ~$ y
- >>> math.isinf(234)& N) I. D% b) J4 y
- False
/ Z7 z" ]2 ]% E# b3 C - >>> math.isinf(0.1)4 \* U' y7 ^# Y A
- False
复制代码 ! i6 ?7 [2 }6 R
math.isnan(x) 如果x不是数字True,否则返回False- J- V. l7 ?. A9 B+ x
- #如果x不是数字True,否则返回False
. p1 s/ K5 l3 s0 Z: K7 R - isnan(x) -> bool6 X2 `! X7 |7 {. Q
- Return True if x is a NaN (not a number), and False otherwise.
# Q8 N# k0 J* w7 M! W9 C; ^ x - >>> math.isnan(23)
( Z9 u5 [9 o5 r6 \ - False
1 Z. m9 |7 ?; E7 ^, q0 h - >>> math.isnan(0.01)
2 S$ V% l$ ]% X" L - False
复制代码
/ q9 B8 X- W! \& d" {( p+ kmath.ldexp(x,i) 返回x*(2**i)的值( ]8 D$ T$ i2 q
- #返回x*(2**i)的值
) I- P2 ~ ?+ Y9 `3 ^0 @ - ldexp(x, i)7 u3 x% {( B* \( S, H. ^
- Return x * (2**i).& |, a R! r& T0 [; @# @
- >>> math.ldexp(5,5); [2 ?) C& C0 z1 r1 W! |6 x0 _
- 160.0% y6 |3 _# D. `7 }# V: s# y5 {
- >>> math.ldexp(3,5)
( p+ M# E4 E# u8 }- K+ l/ A! v! S - 96.0
复制代码 ! Y4 |: [* W* J/ f7 @) Y3 k+ g8 o+ V
math.log10(x) 返回x的以10为底的对数
0 V7 i" [. i" M8 }( G5 n- #返回x的以10为底的对数0 N( r$ {, [! \ X
- log10(x)
. @: F& v6 Z% B; u ?% M( K0 F) J ` - Return the base 10 logarithm of x.
; Z6 F9 O5 V' q9 x+ C" ] - >>> math.log10(10)* m r9 Z3 G+ g3 C
- 1.0# \) ^8 K' B. K3 I3 S9 G
- >>> math.log10(100)0 }. r0 V+ g9 s: b! _: I; z8 q) n
- 2.0! a. | Y4 \$ z+ N% D% r3 q( ~. W- }
- #即10的1.3次方的结果为206 {' M$ e# w- F- Y! t" c3 X
- >>> math.log10(20)
, u& _# {. Q; d! p - 1.3010299956639813
复制代码 I3 I# p0 O3 p$ H7 z! h
math.log1p(x) 返回x+1的自然对数(基数为e)的值) I1 X" R6 N" O9 l7 s7 F( q, ^
- #返回x+1的自然对数(基数为e)的值) V# ^( F% f% c I
- log1p(x)" O9 U& [* L, k+ r5 @" w+ k
- Return the natural logarithm of 1+x (base e).
, V ^- [ h: V - The result is computed in a way which is accurate for x near zero.8 y3 @3 H! t; p4 f
- >>> math.log(10); H! B0 O! G* j/ D
- 2.302585092994046
0 W2 D: R g. `8 E; | - >>> math.log1p(10)
% g2 h, b) I( Q. ^3 s - 2.39789527279837078 O2 L) [5 D8 a3 E" S9 d7 p' L9 W( ]
- >>> math.log(11): ~0 q4 P' E' p
- 2.3978952727983707
复制代码 $ n4 |. K: E! h- q' L
math.log2(x) 返回x的基2对数5 ^# t4 \- _$ \% S: ]5 t
- #返回x的基2对数9 h. i7 ^- X! o: K) r
- log2(x)
% S' O' j9 h9 v7 V+ R( K# K - Return the base 2 logarithm of x.
# s$ C$ x, \+ N& `5 Q% H7 ~8 B - >>> math.log2(32)3 \( [- z" y7 N+ M8 I' c
- 5.0
# k( U, [7 g [' d - >>> math.log2(20)9 ~# F' t: ]" ?& \& b. e
- 4.321928094887363) D# W0 i% S& L7 X7 s, Y2 Q
- >>> math.log2(16)
j/ G5 P/ Y, u# T1 i7 Q - 4.0
复制代码
) J6 G; I/ l* d" ?2 L( q. Q! ymath.modf(x) 返回由x的小数部分和整数部分组成的元组6 {) z+ i. O \" N' z
- #返回由x的小数部分和整数部分组成的元组
5 b" C) G+ `5 c) U0 G4 K* N: J - modf(x)
- l% t) y' z) m n- }6 r - Return the fractional and integer parts of x. Both results carry the sign# l+ y5 d" ^$ F
- of x and are floats.5 `; u& b" ], y3 J5 @8 [
- >>> math.modf(math.pi). d) C* Y- ~6 X; J. L/ R5 k
- (0.14159265358979312, 3.0)
& U. x* e0 s" A0 N8 J3 j5 J @ - >>> math.modf(12.34)& b: y, k4 J+ R5 u, U s
- (0.33999999999999986, 12.0)
复制代码 * T0 F/ b6 d) C& G: U0 s- J( T
math.sqrt(x) 求x的平方根
" R( | l/ |1 Q E) _1 s2 {- J& E- #求x的平方根
3 L' Z$ i9 Z9 n6 T - sqrt(x)9 A0 D8 m/ B( K7 y# i2 {5 n9 U
- Return the square root of x.( b, h- V, @- b& b
- >>> math.sqrt(100)# q+ O/ f j: P8 b3 ?: s" O
- 10.0
/ \9 u: X3 v* L& Y. a - >>> math.sqrt(16)0 m& A# q/ t2 {9 \6 \
- 4.03 M4 {9 a0 H* Z* F/ c9 D
- >>> math.sqrt(20)
& R. X: ~5 T6 M- `# p! E$ P1 @) Q$ R - 4.47213595499958
复制代码
/ W- A- b$ }% v- o# k8 a* K) Xmath.trunc(x) 返回x的整数部分- #返回x的整数部分( R+ E B4 A1 h. _0 O
- trunc(x:Real) -> Integral
`( l% \2 M0 }+ Z# A - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
1 X: |: c+ _* a: ?; W5 S - >>> math.trunc(6.789)
6 s% X X1 {2 Z1 ?8 F - 6
" }2 c$ {- H: q( t0 \) t! E - >>> math.trunc(math.pi)
- [3 y- i. u8 `" K# `9 K - 30 |/ `7 O3 ?) N. C$ r- U8 m
- >>> math.trunc(2.567)
7 d5 X4 V) |) @9 K% A' k; r) T - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|