马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
( I H2 ?" C! z【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
7 l3 [4 b% |& P4 o' K u
- k8 [4 J* n( v; [4 G4 O方法1:0 o5 k+ e7 T0 u2 U
- >>> import math
+ F! B( |) N; ~3 I: ~0 \ - >>> math.sqrt(9)
& ~( ^' r( \* K9 [# z# | - 3.0
复制代码 方法2:5 d+ A5 N0 X. ], l, |
- >>> from math import sqrt
1 C0 }! k# N2 z - >>> sqrt(9)
! s! u% v3 h$ e) J6 y( @* z - 3.0
复制代码
1 v$ P2 S; `% |& n 7 j+ G* S9 j! d- q: K2 q5 f
math.e 表示一个常量
! u9 ]) O3 j _6 D' s+ n- #表示一个常量3 G' l5 g+ @- V, o- m7 I- c
- >>> math.e
9 M* H2 I0 ?/ G1 ^8 S* ` - 2.718281828459045
复制代码 4 Q+ s3 c% }# F' q
math.pi 数字常量,圆周率
; V {5 O; I9 P' R* r! Z- #数字常量,圆周率
/ s9 C0 p- M# u3 L - >>> print(math.pi)
) ~, s" Q2 [" d- P0 x" g - 3.141592653589793
复制代码
; k5 H9 V3 H Jmath.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
1 S A" I, W$ t& v9 E- #取大于等于x的最小的整数值,如果x是一个整数,则返回x
8 J9 I1 e0 U3 a - ceil(x)
4 d2 U/ a; [5 e# B8 H+ z1 F - Return the ceiling of x as an int.
; {6 a7 B+ g( {! k; H8 F- M" C - This is the smallest integral value >= x.
" R7 j) L# o0 Y: z7 C. S1 W
- o! j8 b/ f0 \2 t- >>> math.ceil(4.01)
& ^; j, Q4 p: R5 b7 Z* j2 |4 t - 59 s: K% S( i( I3 D0 B
- >>> math.ceil(4.99)/ k' I9 [7 x& B4 v# E1 x, o6 Z* j) e
- 5
# A: N3 M; z8 q" d - >>> math.ceil(-3.99)1 W* G- {, E l( q2 u7 x7 a/ c. ?
- -3
3 ^+ F' F: u( ` I: y, ] - >>> math.ceil(-3.01)0 |& j' H" _( |5 b
- -3
复制代码 2 N; q8 k) p0 L* ^: y/ w U# ^% O
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身6 f& P0 v; P9 m0 B
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
# p8 J; o4 }1 G( ]& O - floor(x)! C, z4 b4 j2 r' |1 i2 f
- Return the floor of x as an int.
# I; P b0 o, A. ~& L# d; Y- A - This is the largest integral value <= x.9 Z" n6 _: i0 P% y9 a( j( P
- >>> math.floor(4.1)+ H4 ~) S3 }$ M1 ], ]" X7 b' O: B
- 45 J% K: n# I# } s* s/ I
- >>> math.floor(4.999)
. q) b+ x) c7 K4 y) q2 R+ y - 4, S/ |& W: G8 v+ K3 }7 x& D
- >>> math.floor(-4.999)1 M4 z* ]1 J) m) w9 Q
- -5
; m, t, a1 { \* o0 }% J - >>> math.floor(-4.01)0 L: H; o7 ` G
- -5
复制代码 # W! X+ f$ }) k( T+ ?' ]% W. g+ I
math.pow(x,y) 返回x的y次方,即x**y5 U0 U. j' k8 [9 i. R2 I
- #返回x的y次方,即x**y
% Y* i5 L7 C) S K* A8 a - pow(x, y). |; A. _) E2 q; p4 g Q
- Return x**y (x to the power of y)." F2 Z) I3 n: H' B, D0 n
- >>> math.pow(3,4)
; g3 [! `: ~, c! b+ R3 }- [' _4 h$ { - 81.0+ s+ N) j: i% S- v
- >>>
8 d+ P' p* f- q* m8 F" t% b - >>> math.pow(2,7)
: v: F" Y) {' v% L- k - 128.0
复制代码
3 B# k5 V( E3 s9 Gmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! v$ p# ?/ Z+ Y( w& M9 J) m
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)8 B* p! W2 Q- u7 \9 q
- log(x[, base])1 P& j _: [* ]+ ], {/ |+ G" T' v
- Return the logarithm of x to the given base.# v$ q& \; T. t% ~+ L) b3 P- }
- If the base not specified, returns the natural logarithm (base e) of x.
% D# H" b$ X/ W- h: a7 K( | - >>> math.log(10)# g( h+ d7 x/ P* F
- 2.302585092994046
- w( l3 A2 Z% s: {5 x6 u2 ~. I) w# I - >>> math.log(11)
8 i, F9 z1 p" x5 _/ U2 Q- @8 S - 2.39789527279837072 r2 f5 x8 s2 ?7 d3 D' C
- >>> math.log(20)
# Y- r2 q/ ^2 ] - 2.995732273553991
复制代码 : v+ G0 [$ j/ W* W8 _
math.sin(x) 求x(x为弧度)的正弦值( }. E% m% P# O Q& w/ @
- #求x(x为弧度)的正弦值0 i. m- A9 x2 G/ h1 W) q
- sin(x)6 }* J" Q7 i i
- Return the sine of x (measured in radians).
& n1 e# J {2 ~3 y# X2 h$ } - >>> math.sin(math.pi/4)$ U2 F: y; ^9 ?# ~2 p7 I3 e
- 0.7071067811865475
- k2 x5 X0 {( J% b - >>> math.sin(math.pi/2)9 i! }- w9 t7 J" @7 x% m7 i) Y
- 1.0# u8 q9 ?# `: F( _" W
- >>> math.sin(math.pi/3)
! @8 _% i# E9 V# { - 0.8660254037844386
复制代码
6 E! `0 L2 D* K# a5 v5 @. U' Amath.cos(x) 求x的余弦,x必须是弧度8 B( y+ t$ e- }8 C) e/ F
- #求x的余弦,x必须是弧度
1 g' A& _% ^7 r - cos(x)% D' O7 X1 }6 Z! R: ~4 N
- Return the cosine of x (measured in radians).
, z5 p6 t2 O& A& o - #math.pi/4表示弧度,转换成角度为45度$ M; h X/ l' Q7 e
- >>> math.cos(math.pi/4)
& k* A: h! T" v9 T5 L! n - 0.7071067811865476' B9 N1 I& x/ ^0 W( Q# E! p+ V! |
- math.pi/3表示弧度,转换成角度为60度
0 M2 F, q' l! n* m G - >>> math.cos(math.pi/3)
& s- d1 t" [9 v7 r5 I5 ] - 0.5000000000000001
# A6 C: V+ r/ O- _7 C, i - math.pi/6表示弧度,转换成角度为30度
9 @) R1 S9 \" x) u7 p8 ?$ p - >>> math.cos(math.pi/6)
+ k6 w0 ^+ {2 W- Q9 b% H6 h - 0.8660254037844387
复制代码
& W1 [' M8 `' [, q+ C2 F: fmath.tan(x) 返回x(x为弧度)的正切值' _6 A1 B6 k4 E$ _' ?; e* e- v
- #返回x(x为弧度)的正切值6 ]$ E4 y7 e8 g) {+ t/ p6 J
- tan(x)
2 k+ w* P5 x) X+ o9 h: c2 y - Return the tangent of x (measured in radians).
' v; \( `/ V4 M t V$ `5 i& @7 E4 z - >>> math.tan(math.pi/4)
% B4 y. r- b$ z0 w) u/ a0 m R - 0.9999999999999999
# G' N5 z" Z3 U8 }; W9 @ - >>> math.tan(math.pi/6)
8 ^7 y( Z5 I- Q8 z( P+ I. d9 V; p$ n# x - 0.5773502691896257
$ p5 z: R" K, j) N3 U - >>> math.tan(math.pi/3)/ N" N$ u9 x# a' |( ] m$ {0 x/ W
- 1.7320508075688767
复制代码 / s+ q/ w3 L* K/ m
math.degrees(x) 把x从弧度转换成角度
7 `7 B- s" C% W" c- #把x从弧度转换成角度1 q. }9 S: M( l5 P+ ^9 F: ~
- degrees(x)2 ^. v7 @# y! q l8 U( }
- Convert angle x from radians to degrees.
9 ?+ n8 P& j# J5 U7 O: Q |+ c, S0 v - , b. e, e/ p5 u) M* Z0 h! A
- >>> math.degrees(math.pi/4)& `7 C- K0 A$ x+ F; p
- 45.0. q) I. V. N$ U; `! q
- >>> math.degrees(math.pi)9 l( u, Q8 j& W; ^; _) {7 Y- K: Q' \
- 180.0% l6 @" s4 f) D4 c" M
- >>> math.degrees(math.pi/6)2 @$ \: p" I, V9 A8 T! h4 o8 B
- 29.999999999999996
8 d" M0 ~5 G) N. X) I6 o - >>> math.degrees(math.pi/3)
) q' d7 @6 Q9 p$ n0 M - 59.99999999999999
复制代码
. ?- W/ W @. P" y* Gmath.radians(x) 把角度x转换成弧度# ~# d, G9 W( R
- #把角度x转换成弧度
& P, f/ L7 M( e - radians(x)' V3 F+ F, ?% F* c! R F
- Convert angle x from degrees to radians.
/ Z6 J3 X4 C+ y8 N - >>> math.radians(45)/ m n% w5 C8 v( Z% u. V" z% ~
- 0.7853981633974483$ e: @ X/ @5 s. q- E
- >>> math.radians(60), {& u9 n4 X4 a& {0 V$ }6 Q/ V
- 1.0471975511965976
复制代码 , Z% g5 w# B0 }! o0 y8 e6 ~
math.copysign(x,y) 把y的正负号加到x前面,可以使用07 W. n7 Z* m5 @+ i0 _# E/ B& O
- #把y的正负号加到x前面,可以使用02 b2 Z6 s$ G$ q3 _1 ]- R
- copysign(x, y)' Z+ V; M2 g+ E3 @7 I
- Return a float with the magnitude (absolute value) of x but the sign U. k% R4 q* o& F8 l. g8 S3 _
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) & B- ]+ M' _/ p3 K# N, u
- returns -1.0.
3 t: A+ a8 r& u- _ - # P4 S. q' H V; b$ K. P$ d
- >>> math.copysign(2,3)/ r! C- Z- U. R- @& U
- 2.0: r9 n& c* v+ K% d/ ]
- >>> math.copysign(2,-3)
% G( x# s+ B* w' ~1 N, R3 B - -2.0
0 y3 A( G, R5 b/ i - >>> math.copysign(3,8) l8 S) S( U5 h
- 3.0
) Z. q" d. W6 g+ `/ _1 D - >>> math.copysign(3,-8)* ^$ n+ o% b) u$ X" S
- -3.0
复制代码
( c% O. V, ]# Pmath.exp(x) 返回math.e,也就是2.71828的x次方2 y' Y/ g9 c& C9 O9 Q. F
- #返回math.e,也就是2.71828的x次方
% E+ x# l. J$ ~0 { - exp(x)
9 c' @( E1 l5 ^) C6 p$ W; Q- U1 c W - Return e raised to the power of x.
! W5 O" }! ]5 V5 J+ s- | - 5 D7 o; |6 y% m! ~4 q6 Q
- >>> math.exp(1)
. ]9 Z3 d/ t/ {' N - 2.718281828459045
" u8 `% i9 ?( Y! F' s$ F - >>> math.exp(2)
4 y/ B! j& Q0 R1 k! X" ]" ?1 } - 7.38905609893065
2 p9 V) I! `6 f) h - >>> math.exp(3)
1 r3 p7 p' a* {5 g( S - 20.085536923187668
复制代码
9 G; M. w/ g9 vmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1# {$ g5 s% ~3 k6 H& i4 t
- #返回math.e的x(其值为2.71828)次方的值减1: l% W$ ~5 u5 ?; j) K& V1 d
- expm1(x)
% g0 o) d% Q0 h- s4 x0 v - Return exp(x)-1. A3 \9 q8 ?5 E" L4 p7 ?
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
) p" ~4 r. W' h+ S1 B) K6 d: Q - # Z5 l) N- W+ V5 _; {4 r: e0 Y
- >>> math.expm1(1)
6 o6 _3 [# W, q. s" b g7 z - 1.718281828459045
7 {* o" b b! O" N' I+ { - >>> math.expm1(2)
9 B' S, D9 u+ U4 t& }( R" Z - 6.38905609893065
9 t: S3 A9 y4 u+ V4 g, Z. t - >>> math.expm1(3)
9 e; q! V7 W( ^5 J4 z - 19.085536923187668
复制代码
8 D! K, K) i- r, ~9 [8 o/ jmath.fabs(x) 返回x的绝对值
# h4 _/ f" X+ @ K- #返回x的绝对值
' B2 r& F, B% @: r6 Q$ L5 T - fabs(x)
! e- U& o- O! V9 n4 q2 `, I& Y" Y - Return the absolute value of the float x.
) b4 N1 w! {* n, D n9 @, t
6 R2 S8 U" ?% _- p3 f9 G# u- >>> math.fabs(-0.003)
V+ p$ P2 [% I# D! b - 0.003
9 x7 L' D6 k, c5 M. x: Y. q* d - >>> math.fabs(-110)- o- o- N/ t" y' j& n( U
- 110.0! z2 {/ q$ p$ b
- >>> math.fabs(100)! e% U, v: v* L. Q ?# M( L+ W" Y
- 100.0
复制代码 / p5 N- _5 v2 f y4 f' k, j
math.factorial(x) 取x的阶乘的值3 t4 |$ w+ J/ P" e+ T, C
- #取x的阶乘的值8 H7 H7 k2 H: U- e6 N% }
- factorial(x) -> Integral- I% R( W% ~ ?' I4 _9 W8 z$ N
- Find x!. Raise a ValueError if x is negative or non-integral.4 o; D8 E; O9 A Q" ?5 E
- >>> math.factorial(1): H& D1 O- V3 M% C& ]3 T! _
- 1% S b( X* r4 P5 @
- >>> math.factorial(2)
8 P2 P7 V2 {0 ?+ {- @ - 2) l. x$ P9 @$ [5 c4 H* K& b
- >>> math.factorial(3)
) B# W2 u1 R' J1 H0 N& g* {. R - 6
9 c8 u0 d3 A6 o; Y, ^ - >>> math.factorial(5)6 j7 k7 @* g9 }3 J. [ \
- 120. D" |, k- d L! p
- >>> math.factorial(10)) D+ ]1 r' d1 L; L1 P' N/ A# `
- 3628800
复制代码 % C2 N) i8 _& `2 r! P
math.fmod(x,y) 得到x/y的余数,其值是一个浮点数9 v: H* M, f' d4 C# f
- #得到x/y的余数,其值是一个浮点数& N' n/ @* `% }1 U* _
- fmod(x, y)3 p' c1 }& a8 o9 O2 P% g
- Return fmod(x, y), according to platform C. x % y may differ.
2 @# d6 Q/ g8 J, D( A - >>> math.fmod(20,3)9 q4 A& q5 F f) B
- 2.0
0 H( V! b0 w( b: ^4 T - >>> math.fmod(20,7), H3 N2 d% M1 u" N
- 6.0
复制代码
$ }* W7 @; k5 b# g8 Qmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围' y' m2 T6 e( y; u
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
2 c; b( W1 Y6 H4 K2 {* P - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值9 p) \4 F+ d9 u! ]: t- ?1 F
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
7 w4 B3 C& {4 h! z# R# E - frexp(x); }1 T/ V9 {/ j0 C
- Return the mantissa and exponent of x, as pair (m, e).& I0 T5 r$ t G" H
- m is a float and e is an int, such that x = m * 2.**e.1 ?2 z4 |+ G5 N4 o
- If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
. p0 k* g2 }/ U9 B8 t" f - >>> math.frexp(10)
7 }0 p& {9 E( _8 C5 l7 U - (0.625, 4)
1 R) @5 u% ?5 d9 t - >>> math.frexp(75). l8 |5 v9 b4 D% O3 T5 s
- (0.5859375, 7)4 V, y, I+ Z V6 @& [4 u J+ K& B; I
- >>> math.frexp(-40)( m$ A' f. A" {" h: G. p( P
- (-0.625, 6)" y+ e: z) K0 u6 A
- >>> math.frexp(-100); n$ P; p/ q. f) G
- (-0.78125, 7)
: d. h, j9 S# |' M$ W$ P2 _ - >>> math.frexp(100)
' U! b+ B# P0 u2 i) j" { - (0.78125, 7)
复制代码
9 \- ^" Z/ H4 ymath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)2 C, Z/ m% d; ?% p5 |6 h: H
- #对迭代器里的每个元素进行求和操作
- G9 o3 {7 _# R5 K% a# z, x - fsum(iterable)
& `% s9 c0 M0 u. @4 o) E - Return an accurate floating point sum of values in the iterable.% X& Y( p* ?* Q0 w( v4 W$ Z
- Assumes IEEE-754 floating point arithmetic.6 j3 _! K% h/ N/ T: [$ v
- >>> math.fsum([1,2,3,4])
# [9 A, l; m& S2 T8 L - 10.08 U7 X: g* Q, U2 H
- >>> math.fsum((1,2,3,4))* M: n' {5 L: [7 R
- 10.0
7 b7 C z! z+ o- x - >>> math.fsum((-1,-2,-3,-4))9 P' U. \, r D. v
- -10.0
$ K% @* x/ ]2 }/ a) M1 H: ] - >>> math.fsum([-1,-2,-3,-4])! }4 D& t7 {% I% g$ l1 d
- -10.0
复制代码
, A. m3 Y2 N" f% j. Lmath.gcd(x,y) 返回x和y的最大公约数
/ Q9 l! w, b z( T- #返回x和y的最大公约数
% f% ?3 Y4 i2 e2 U& b - gcd(x, y) -> int P' k, l0 q3 `5 n' j* i* h
- greatest common divisor of x and y/ h. V$ H. [$ g2 h
- >>> math.gcd(8,6)
" @! ?2 ^; C q1 V, B& ^ - 2* Y, W: G7 [2 j, o* b
- >>> math.gcd(40,20)* z- j& U0 n& p* l4 Q# R! |* e
- 20
% {( U( n, O% Z - >>> math.gcd(8,12)) W! `' E% b* u( b2 T# ~
- 4
复制代码 + r, R1 T2 Q- X4 K8 j/ C
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False8 }: w* h4 W: u' J0 e9 z
- #得到(x**2+y**2),平方的值& B0 k# q0 V! K `4 M u
- hypot(x, y)( o: \& f% v7 R/ H. D. n9 c
- Return the Euclidean distance, sqrt(x*x + y*y).
$ w( @; W: L' J1 }& L7 T) @0 c/ d - >>> math.hypot(3,4)0 i6 V8 e- V6 p" p: c
- 5.05 i' J" \+ _( K* w
- >>> math.hypot(6,8)4 c* u. @; m1 `- A' k( L
- 10.0
复制代码 # t3 z1 p1 j' V( m* }- [- m$ t2 I
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False+ V. ` A& A. D+ u9 _- k# N5 M
- #如果x是不是无穷大的数字,则返回True,否则返回False I7 |3 k1 u. N# W1 G4 Q1 }) N
- isfinite(x) -> bool
; \6 w. H! e! t - Return True if x is neither an infinity nor a NaN, and False otherwise.; Q. i2 r+ P% p: y
- >>> math.isfinite(100)7 g% l! D, m# }. h2 U/ G! P: t
- True! P+ o, B, s& f9 B% Y6 {& }& h
- >>> math.isfinite(0)
) B. H, @! j, ~$ f# X% z e: j - True2 Q/ t9 g9 c) F" f% g0 S, m. a/ l
- >>> math.isfinite(0.1)
" E z& W; j! s! D. |- O/ | - True" }+ b) r0 j4 l2 D7 E
- >>> math.isfinite("a")
6 g3 `. ?5 _8 E - >>> math.isfinite(0.0001)9 k6 N. H7 e* Q' l y) P% z
- True
复制代码 1 h5 ?# @" o- B7 f5 {0 x/ w# _
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
1 a y: v6 k9 C# [' s- #如果x是正无穷大或负无穷大,则返回True,否则返回False# Z" X' f& `3 l# R2 J+ H
- isinf(x) -> bool/ ^1 P- {9 J; y& @7 w3 M
- Return True if x is a positive or negative infinity, and False otherwise.- p( ?2 Q& `7 x X5 [$ s
- >>> math.isinf(234)
& S! Z/ v* o4 T, z( l9 v* B: Q - False- n8 o6 o8 V |7 Z: d
- >>> math.isinf(0.1). r9 M7 o# U# o' n1 e
- False
复制代码 * b/ |9 P( ?5 J6 J0 R1 C: z# K
math.isnan(x) 如果x不是数字True,否则返回False
. x- U; a" O* _1 Y9 b- #如果x不是数字True,否则返回False5 T9 M% Y# Y! {
- isnan(x) -> bool
( X( ]9 e! n$ C" \% O& r) w) J9 w, u - Return True if x is a NaN (not a number), and False otherwise.
6 C I' r9 D2 [# c* s - >>> math.isnan(23)) f1 y. _' V' J5 |
- False
& }8 D6 s% T% e( u+ e7 } - >>> math.isnan(0.01): ~6 {8 d5 j# S3 h" l9 _
- False
复制代码 ( |! j1 A- o! ?2 e& O
math.ldexp(x,i) 返回x*(2**i)的值
/ x) D# k6 t l/ L- #返回x*(2**i)的值
7 d7 o1 @" V0 c% y/ ^8 ` - ldexp(x, i)0 U6 Z* E# r. A1 S0 b/ p
- Return x * (2**i).
' c6 T# I. x+ M9 d - >>> math.ldexp(5,5)9 f- H) Y+ ~3 E+ d. E
- 160.0
1 r0 P) p/ X' O) s; j+ T% T - >>> math.ldexp(3,5)3 M: l( V& q; W' R% J
- 96.0
复制代码
! w0 E; O7 U% E1 Q6 w) Amath.log10(x) 返回x的以10为底的对数
$ O( @3 X( u2 r2 P' _; d- #返回x的以10为底的对数
# N0 E* T* D: s0 U2 o7 A" f - log10(x)! b# f/ V* g( M
- Return the base 10 logarithm of x.
r" u8 S& h2 {! S2 m& X - >>> math.log10(10)# }* [; M5 C" L2 b8 f6 F; l; p
- 1.0
9 Y* X& T5 Y8 `8 z+ E# T- m2 _* a - >>> math.log10(100)
9 C' s S* W( A9 ]' p+ I5 I+ f - 2.00 c# D3 x3 ~6 z- B9 W
- #即10的1.3次方的结果为20& J" t0 o0 a$ c
- >>> math.log10(20)6 J+ T# q+ Q4 I" a8 b" R# K
- 1.3010299956639813
复制代码 1 q; }! Q% D% L- i
math.log1p(x) 返回x+1的自然对数(基数为e)的值
8 `6 l: Y: d. R b+ n( u- #返回x+1的自然对数(基数为e)的值
0 [/ v: v" F6 S, t @ - log1p(x)
- D# r j T, |" Q - Return the natural logarithm of 1+x (base e).; l9 X+ ~9 J/ ]0 O7 ^; w! q- B$ H
- The result is computed in a way which is accurate for x near zero.
5 N: k0 A2 n* K6 r; | - >>> math.log(10)
; O. O# b' q$ F8 S, {+ B) y - 2.302585092994046
" G5 I) B4 H: \; W" P, M - >>> math.log1p(10)9 R7 ^& g4 L, L- V% I/ z# S# ~3 T
- 2.3978952727983707
$ F& r0 }( P, l3 X( q) p5 y - >>> math.log(11)( P% J2 u2 s$ h" }
- 2.3978952727983707
复制代码
: H. x* P& ?5 j) H; mmath.log2(x) 返回x的基2对数
; J9 Q$ r! |# P" L0 J( X7 r- #返回x的基2对数+ a8 h$ x' l: j7 s
- log2(x)
& P, b* {8 ]; t% {5 v' I - Return the base 2 logarithm of x." M6 `- u7 `! ]1 Z! Z
- >>> math.log2(32)/ `1 i Y) {" B
- 5.0/ @6 l/ m% s6 [3 `" W5 g# n
- >>> math.log2(20)
& ]0 [5 N1 `( y) ]5 J7 p$ y - 4.3219280948873639 f) s) i# @- p4 U7 t
- >>> math.log2(16)
& H! Q! R: U9 B* H3 O/ Q) L: |0 l" h - 4.0
复制代码
/ o3 l/ y' J7 `( r: Y* Bmath.modf(x) 返回由x的小数部分和整数部分组成的元组
/ U2 b* |/ @; F2 B/ w% `- #返回由x的小数部分和整数部分组成的元组
4 Q, u6 b6 X% T4 N. }8 j - modf(x)3 z6 n6 g% X! W, ]6 R* \! {
- Return the fractional and integer parts of x. Both results carry the sign
) O4 j% v4 R( k& \- t2 n+ T9 ` - of x and are floats.' D, n2 _$ R3 x: `2 f* B8 y
- >>> math.modf(math.pi)7 K& Q$ C! [) C
- (0.14159265358979312, 3.0)
. o/ @4 l# K/ z: X - >>> math.modf(12.34)
( k5 j7 o/ t/ B1 a8 V) A& A - (0.33999999999999986, 12.0)
复制代码
) \# E/ d6 R) H5 b; K" Gmath.sqrt(x) 求x的平方根9 e y% M9 D2 t7 ?# r5 _
- #求x的平方根
% D7 L; c5 J' s+ o9 U - sqrt(x)! n0 r. u+ X, i x, ~: K
- Return the square root of x.. Z3 Q; L; T$ H0 O' u6 K
- >>> math.sqrt(100). k9 M2 Z0 h5 l; b
- 10.0
9 Q5 G f& U% C! Q6 O! @ - >>> math.sqrt(16)
4 }9 O: c% J5 V, ^- U$ n, o* J - 4.0. z7 M) F- _8 S3 R
- >>> math.sqrt(20)& ]2 X$ a* L6 n. A h0 K: f
- 4.47213595499958
复制代码 ' Y8 y; _; y- y3 m: |* o
math.trunc(x) 返回x的整数部分- #返回x的整数部分
3 o0 r$ P. \& R5 V% w( H9 W - trunc(x:Real) -> Integral
( j- e+ c4 c& r* _: x! _ - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
7 Z2 u2 w% ` x" m9 [ - >>> math.trunc(6.789)
6 t4 }. x8 {- W& P: Y1 B - 65 D2 L6 B/ q6 g1 U% Y: h! g4 ]
- >>> math.trunc(math.pi)
8 G3 N) I; Y6 Z" F" ^+ p - 39 H- c; I$ k6 K0 E' B
- >>> math.trunc(2.567)
( _6 ^( O4 {5 W d# m# F - 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|