|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 z" B: c) b& ?! C5 l. @0 T( R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。
9 } [. R+ K9 ]0 g, [* b! g2 Z3 E" h! Z( V% V
方法1:9 `0 E& C" c6 P% ?( ]
- >>> import math; W; B0 ^+ z$ j Z
- >>> math.sqrt(9)
; S6 J& n3 v- ]* U7 X - 3.0
复制代码 方法2:1 n5 N: I7 A; j0 v
- >>> from math import sqrt+ R! @4 k% {& V) q; z5 Z
- >>> sqrt(9)
, R/ R3 B- ?' g0 P. V - 3.0
复制代码
2 Y4 g' S' j5 |" A2 P) Y/ {/ h3 Q
& Q- e k" F- |1 Vmath.e 表示一个常量( Y5 c8 z6 I* |& M3 {$ u
- #表示一个常量2 _ K6 G" \: `5 ~9 ~9 G( b
- >>> math.e- V2 k. ]) F7 o/ M4 n" K3 z" M9 O4 G& R( {
- 2.718281828459045
复制代码
* q# Y4 a/ f+ a7 y% f$ amath.pi 数字常量,圆周率* b5 M. u: f, r1 ]0 a9 r
- #数字常量,圆周率
9 { y. t; }7 U9 H7 G - >>> print(math.pi)
* k0 b. R8 L* w9 O' | - 3.141592653589793
复制代码 6 W, D* [) D) P9 d" V; r- S
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x* g" K5 k/ v& d( n, M' `5 F( [
- #取大于等于x的最小的整数值,如果x是一个整数,则返回x$ M# V% P( Q7 {$ S
- ceil(x)
* O& R; Q% m7 q* k - Return the ceiling of x as an int.1 ~1 A6 U* N- ?4 Y8 l
- This is the smallest integral value >= x./ ~; G: }9 P W5 V
- : v j% g! {3 ?
- >>> math.ceil(4.01); E: J# D+ u7 c+ g7 F! i+ K
- 5
3 \" x+ R) n& `( ^; J0 J; @ - >>> math.ceil(4.99), `! d: m" D( e1 p
- 5
# ^: j! A# }1 n6 E7 g. ~" F$ @ - >>> math.ceil(-3.99)
6 I! Y0 k% e% X/ L! r4 @' \+ q7 A" ` - -3! T& b* K6 L, l5 o1 l/ m# }
- >>> math.ceil(-3.01)
: o. {3 u: y3 I. A - -3
复制代码 $ d7 |, X* i0 M% [( d
math.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身+ W6 h6 P' |6 H( q' k/ `
- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身
. H: ^5 J/ X2 P - floor(x)
g Z7 |$ S, g- z - Return the floor of x as an int.0 m# z/ ~% E5 W- ?# a2 g7 }
- This is the largest integral value <= x.
# s, s: N: ]2 G) b - >>> math.floor(4.1)) [, Q$ a: K2 |0 r+ o( h
- 4
2 @ `* @. C. D, F$ y( k5 s - >>> math.floor(4.999)) }% s* g* H) b8 N5 V3 n O0 H
- 4( r( u8 N4 u9 q5 H9 S
- >>> math.floor(-4.999)
& S9 {& W3 S" l/ \8 P8 g) ? - -5
- R9 E2 r' O5 g- G `: Z - >>> math.floor(-4.01)
- I/ P7 r" ~ w$ _& h - -5
复制代码
) f/ @& X2 y9 \3 ~: x+ Z, g$ omath.pow(x,y) 返回x的y次方,即x**y
. c6 |) R) s; Y* d- #返回x的y次方,即x**y; }4 t, g3 }6 }; ^ b
- pow(x, y): D0 r5 p- M) M+ z5 G5 _3 f
- Return x**y (x to the power of y).
! G& I$ P- X' w1 p/ b - >>> math.pow(3,4)% m; U# T' ^, [5 D1 l2 D
- 81.0
, o6 Y8 o3 G) ^ - >>>
/ D2 j* [/ l C - >>> math.pow(2,7)# Y$ N) T5 D4 j
- 128.0
复制代码 - b+ j9 ? b" I/ ]. d* j! f' q
math.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)2 X+ N: K! Z& g1 |$ L* e
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)! T* u, g% R* u" f/ N8 J* C
- log(x[, base]). T( _) I9 N9 U7 q X/ T$ Z( q
- Return the logarithm of x to the given base.
0 N/ S: a$ B! |* a/ C; Q - If the base not specified, returns the natural logarithm (base e) of x.
: I/ e6 G9 f4 V: v - >>> math.log(10)2 f4 a2 P! v4 ]+ N3 n
- 2.3025850929940467 Z7 X, V$ O- {( e4 x
- >>> math.log(11)( a9 b* b- P. S3 \
- 2.3978952727983707
' ~2 U5 W% l2 u" M" ^' s - >>> math.log(20)
: j2 d3 R6 H# @9 A4 D - 2.995732273553991
复制代码 3 T2 B/ q% | y6 }; r7 R
math.sin(x) 求x(x为弧度)的正弦值
8 z$ ]8 _. S# Z: w- #求x(x为弧度)的正弦值
& ~) @! `9 q( `/ k1 A( Q - sin(x)
8 E. H5 ?7 b' Z4 N - Return the sine of x (measured in radians).
9 b9 B; W/ W2 Q& N5 U X I - >>> math.sin(math.pi/4)( i8 ?8 B" n, r
- 0.7071067811865475$ g3 |" r( Z9 }7 k& o6 D! _6 h1 P6 ^3 I
- >>> math.sin(math.pi/2)' C. I5 o, P8 ]! |
- 1.0! R$ ^6 f2 I6 h0 v, E
- >>> math.sin(math.pi/3)( F* Y3 w. v5 |4 O' v0 G8 W; N
- 0.8660254037844386
复制代码
4 z. B& A1 b* _$ W! p( V7 nmath.cos(x) 求x的余弦,x必须是弧度) F* I3 s1 s: v' A/ i/ N
- #求x的余弦,x必须是弧度
, J7 b8 ]! m( F! c/ g3 h, ]1 {3 ]; a - cos(x)
% D% {, {" O7 }- I9 R+ t - Return the cosine of x (measured in radians).: d+ z2 l: ]3 r- c: _+ N0 g1 N
- #math.pi/4表示弧度,转换成角度为45度6 { j1 C) A9 M0 P0 Z' R
- >>> math.cos(math.pi/4)" \( P2 d0 g% \9 W
- 0.7071067811865476
, L3 _9 N( v, C6 u - math.pi/3表示弧度,转换成角度为60度
7 I% L: }9 s" t1 O6 c - >>> math.cos(math.pi/3)
# b8 c. M1 \: N! m0 r - 0.5000000000000001
( k4 p$ @; q3 ?% {+ a - math.pi/6表示弧度,转换成角度为30度$ C5 u" J0 |5 U% L) f
- >>> math.cos(math.pi/6)% R; c/ |" d9 R7 H
- 0.8660254037844387
复制代码
: x, P4 k8 R+ Fmath.tan(x) 返回x(x为弧度)的正切值* c' _' O2 y4 W0 u1 k' w- t
- #返回x(x为弧度)的正切值
" D: H9 ]- w' ~/ w0 P+ u - tan(x)8 K0 a1 u. E c! \2 {
- Return the tangent of x (measured in radians).( @ w; i$ }* B" a) d+ d6 e' q
- >>> math.tan(math.pi/4)- K- M& ^; o5 j- e+ z
- 0.9999999999999999# \9 _0 W9 T" b' j3 ]3 K: A7 }) M
- >>> math.tan(math.pi/6)9 }9 G4 V- L1 R# ^9 V7 ~
- 0.5773502691896257
0 f' U/ u3 d& e6 k9 s - >>> math.tan(math.pi/3)
) Q) Q6 A0 V6 q2 M: M s - 1.7320508075688767
复制代码 2 w+ o8 g' u3 R) x `, A- }
math.degrees(x) 把x从弧度转换成角度
0 ^' M8 j: M* D- #把x从弧度转换成角度. @! l9 {/ f: W* \* w+ j, \% S
- degrees(x)! C- p1 T; p2 E }7 t
- Convert angle x from radians to degrees.. C) z' \: Y ?! n1 G) N
$ j: C7 Q. h% J8 B3 Z9 Z" g- >>> math.degrees(math.pi/4)
8 r) f8 ?; ?/ n/ y5 l0 { - 45.0
+ X! i( |9 E! L4 |( o - >>> math.degrees(math.pi)5 u; Q x9 \- }1 ^2 r
- 180.0- t% x9 O7 ~ a3 J+ @* p- O
- >>> math.degrees(math.pi/6)
- S0 f. R! y. j" Y; Z - 29.9999999999999967 m6 z3 H+ Z" t% _ Q# ]
- >>> math.degrees(math.pi/3)
$ T5 ~6 c; O/ f& W+ ~( c. Z4 r: q O - 59.99999999999999
复制代码 ! |0 I6 y* f" L F/ X- x- l( _
math.radians(x) 把角度x转换成弧度
" H# X% u1 H! k! C( D U- #把角度x转换成弧度& x, v* v' J: G: _9 }+ j
- radians(x)# ^6 e$ E! M2 t& w+ l7 u# ], X$ }
- Convert angle x from degrees to radians.$ D! L& O# G' C& R" {* P* }, h
- >>> math.radians(45)
- G$ G9 P& X# f - 0.7853981633974483
& q. i8 i) c* F" | - >>> math.radians(60)
3 @) g4 q1 v) \0 k - 1.0471975511965976
复制代码 ) Z0 _- B4 k! L
math.copysign(x,y) 把y的正负号加到x前面,可以使用0* @6 y; O) h$ z4 Z |7 h0 o
- #把y的正负号加到x前面,可以使用0
. y U# v, d/ P" |8 w2 d - copysign(x, y)
- k4 G% Q7 T: ~" l, A: K% x0 x - Return a float with the magnitude (absolute value) of x but the sign 1 m6 |0 Y4 q; i4 c/ O
- of y. On platforms that support signed zeros, copysign(1.0, -0.0) $ u* X8 l( O6 n, G2 M/ g
- returns -1.0.* F# B9 B: r) r& y) P( o4 `
- ; Q$ P" y' q' @9 z) P: S
- >>> math.copysign(2,3)4 ~6 q6 a) h; s
- 2.0
% k6 e& J) G$ P+ J* z' j4 [- T - >>> math.copysign(2,-3)/ z8 A9 n0 c, \. q \
- -2.0$ T. J: l% W$ l0 ]3 y ^8 T
- >>> math.copysign(3,8)
7 ]8 [- b2 c2 j/ g1 ` - 3.0/ U% Q+ E5 v% D* ?. Y
- >>> math.copysign(3,-8)
* X: `& F* `9 x: N2 I - -3.0
复制代码 - q4 g9 L! Z& a# r2 T% A0 n3 j
math.exp(x) 返回math.e,也就是2.71828的x次方
" f: [; X( D* o% e L- #返回math.e,也就是2.71828的x次方
, |3 R" K/ P" X% S! a: X( X" {6 [ - exp(x)4 ]' b* H( s' B6 I/ l: C! V; e9 M
- Return e raised to the power of x.
+ K" D- G7 }% k; b n e+ @3 ]7 F - . a3 V0 X! m* m4 A) |% F% V
- >>> math.exp(1)+ o7 ]3 c. w" W) I4 m1 ~ A# J& T
- 2.718281828459045* V5 T, N0 z; B: _! n/ ?) A
- >>> math.exp(2)7 m! f* Y# ?1 n: _: i3 P' p
- 7.389056098930655 h m% \- Y+ }" h
- >>> math.exp(3)$ z7 U4 i' t9 d5 m4 C$ }" |6 q
- 20.085536923187668
复制代码
: f E% z! D% g$ ~" d1 {* zmath.expm1(x) 返回math.e的x(其值为2.71828)次方的值减10 c8 G9 C# @2 l2 j& v
- #返回math.e的x(其值为2.71828)次方的值减1
2 r( @/ M$ K6 K0 X - expm1(x)
+ E: `) ?" o3 n( ? - Return exp(x)-1. x+ @) g6 o7 w% K& j/ X
- This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
9 V w& g1 m# T' q
/ T6 U1 h, W) S! I# S: y; F- >>> math.expm1(1). |. a6 q) K6 S3 {
- 1.718281828459045
3 S! }) s3 K1 a7 }. x( U% t, L! q - >>> math.expm1(2)
; O+ B [& P& u( z+ l. y - 6.38905609893065
N; _6 [' u+ a, |, w - >>> math.expm1(3)8 R3 H% i- o3 d' C- x7 m
- 19.085536923187668
复制代码
$ s3 \- _) u/ O8 i6 b, U) @! ]math.fabs(x) 返回x的绝对值0 c: Z# C; h4 U h/ e
- #返回x的绝对值
& G% n' b7 n$ `5 a' R" s - fabs(x)
2 y, ?! F3 m( `3 ^8 @+ L0 T - Return the absolute value of the float x.
8 [5 ^' I. f- f5 n8 H8 X. A - : S8 |" I* R. ]
- >>> math.fabs(-0.003)
4 L& S# l' f, c2 g0 e# G - 0.003
. ~8 T: o# C R$ Y3 L. A) M - >>> math.fabs(-110)
9 \" z4 G2 g4 Y2 [- G( J7 x \ - 110.0* H8 D4 |3 A. S7 R6 j
- >>> math.fabs(100)+ C7 \" ]# V4 A+ I
- 100.0
复制代码 ; _) e- p. P" J; K- O3 e
math.factorial(x) 取x的阶乘的值
l8 ^$ t+ k9 H: Y- #取x的阶乘的值* c9 g; O0 t+ n! `
- factorial(x) -> Integral. d8 D: {/ D5 Z+ P. H
- Find x!. Raise a ValueError if x is negative or non-integral.
9 c S* e2 M7 c x N3 a$ S6 j - >>> math.factorial(1)* ]4 i+ |, N& e+ [3 j" L
- 1
$ e' o* {9 O4 ]: j2 r6 y - >>> math.factorial(2). d. p9 Q7 K0 K# N2 E
- 2
% J0 ^: U2 Z, M - >>> math.factorial(3)
, x* i4 X- Q8 i; ~ - 6* Z) s1 O4 ?: a% z% ?* ^
- >>> math.factorial(5)0 {/ S Y4 M1 G/ y& V
- 120
# V1 _5 R5 L, ] - >>> math.factorial(10)4 v8 v) O7 X' L* q
- 3628800
复制代码
( x, r7 g+ b/ R: i+ d& wmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数6 g- U* G k. ? {) S
- #得到x/y的余数,其值是一个浮点数/ ?1 t! S& z m6 m# r
- fmod(x, y)
' `8 u# R) \* P3 U/ D - Return fmod(x, y), according to platform C. x % y may differ.. j* c& i# _4 ]+ I$ n7 E2 h+ z
- >>> math.fmod(20,3)
, j' @$ B2 ]% k3 t& ~, Z - 2.0
$ E% t9 w/ G0 W; t - >>> math.fmod(20,7) Z+ V3 ?6 O( q6 b! Z2 H' ]! @
- 6.0
复制代码
q$ s( |$ H) @" W( X7 J+ T% kmath.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围$ ^2 a3 }2 [+ I4 \* @7 e
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围," E' n3 C5 q9 w
- #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值3 O. \7 N/ X2 W
- #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
{% O m. k. c) C* [9 C - frexp(x)9 { `: R/ E* }; u4 D
- Return the mantissa and exponent of x, as pair (m, e).
3 l7 P8 T' i! `- L - m is a float and e is an int, such that x = m * 2.**e.
- ?( U# C; ~& V& e - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.4 J2 x5 t; ^5 o! L, D! c# M4 w
- >>> math.frexp(10): ~6 }' O; o o* e* z; Y# t
- (0.625, 4)9 @" D; z+ E6 H- O
- >>> math.frexp(75)
0 e' i0 @5 X5 e4 `) h1 _9 | - (0.5859375, 7)
7 z) O% m( R* a; m" O: c - >>> math.frexp(-40)
' v3 v& C6 Y; |* c g - (-0.625, 6)
6 U4 r* W- A( y - >>> math.frexp(-100)
" W" h; L5 Z4 Q3 o f- W - (-0.78125, 7)
* f0 |- ^6 }5 B! V/ g! w2 x# ? - >>> math.frexp(100): D: D8 O8 L' u6 m2 n) t
- (0.78125, 7)
复制代码
! T9 Q4 V7 {0 k0 fmath.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
1 p9 i3 ?5 s7 j6 R( K- #对迭代器里的每个元素进行求和操作4 o. w& x8 J: E! n
- fsum(iterable)* f. }8 w: ~7 i+ H
- Return an accurate floating point sum of values in the iterable.) @9 O9 K0 p7 W# b0 j
- Assumes IEEE-754 floating point arithmetic.3 Z: C: t: j6 _1 ?
- >>> math.fsum([1,2,3,4])
; {+ C' v0 ~2 M+ L - 10.0
! f# |% a# r- G1 A" w5 Y* s - >>> math.fsum((1,2,3,4))
$ B3 X4 n# q3 Q |% | - 10.0! A: ~9 Q) n5 k6 Z% |6 [# N
- >>> math.fsum((-1,-2,-3,-4))$ ]: `/ E! R4 k# J7 B+ Z$ [6 ^
- -10.0
4 |1 H/ n# k' K: a% Y- I% n. Z5 k - >>> math.fsum([-1,-2,-3,-4])
9 \0 |7 ^7 V( N( C! e% e3 s6 K - -10.0
复制代码
# A- E+ j& {& m8 Y, Vmath.gcd(x,y) 返回x和y的最大公约数
' V# ]- O0 U+ T& `' W1 L+ f- #返回x和y的最大公约数, Z0 h: A0 g5 g9 j
- gcd(x, y) -> int
' }7 r( R4 |: m+ W) { - greatest common divisor of x and y! d, z3 C2 q! y
- >>> math.gcd(8,6)
# F' f% v& X5 M0 R# v# m - 2# k5 M g# y8 d0 g* p' b. Y0 M! J& W
- >>> math.gcd(40,20)
/ I+ @7 |! I+ e; p - 205 M" z: d% Z" F, E
- >>> math.gcd(8,12)5 G v7 s) O k
- 4
复制代码
/ ^9 d( }6 e$ D7 ?% z" _: imath.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
; X+ x& K1 i9 b- R# D- #得到(x**2+y**2),平方的值
, k; k$ f( O6 s# k- k7 x+ u# Y0 e, f - hypot(x, y)
( Q! b5 t) r! m' Q& P& g; K- t - Return the Euclidean distance, sqrt(x*x + y*y).+ r) l5 k- R s
- >>> math.hypot(3,4)2 Y' x8 L/ A* O" F" ]! U, `, z
- 5.0
" m) C9 `( V' J! C# h5 w - >>> math.hypot(6,8)
0 z: b0 D& Q$ i4 X* O - 10.0
复制代码 5 @( u1 q+ e6 M6 l6 d
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False0 g2 i# _2 I" }. }1 J% ]
- #如果x是不是无穷大的数字,则返回True,否则返回False
) `" y R) c! V7 a - isfinite(x) -> bool1 ~% U( y4 Q8 }& `3 q4 D x+ h' P6 T( {
- Return True if x is neither an infinity nor a NaN, and False otherwise. z! G! y6 M8 `& G5 {
- >>> math.isfinite(100)
3 h# h' v/ X& ]" Z, _ - True
# g6 H# E; w; r* {, z7 o0 O2 f - >>> math.isfinite(0)
( g; m2 f) M% e) p - True
* K8 E( T# B2 n2 v/ [ - >>> math.isfinite(0.1)1 Y$ k3 T; M3 }5 W+ |8 F% n
- True
+ \' z3 w4 w4 \" o S - >>> math.isfinite("a")1 q+ b' @2 W5 d* s3 }
- >>> math.isfinite(0.0001)
/ _) K# h. g' x% Y1 B! z3 K2 ~ - True
复制代码
0 d) _% o" P8 c* Imath.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False
) S1 T$ e) v3 b; m& F6 o- #如果x是正无穷大或负无穷大,则返回True,否则返回False
+ [/ e: n) v' w" }: m7 }, J - isinf(x) -> bool) n8 Q9 k9 o S- V* H/ Y! v
- Return True if x is a positive or negative infinity, and False otherwise.
4 J$ I. a3 b4 W/ K7 b) ~# U - >>> math.isinf(234)5 w6 S4 B( o! H% M
- False+ `1 l$ o) @: G1 R7 u2 [. \
- >>> math.isinf(0.1)
& Y$ k. F+ U* f) Z2 t4 [! G2 j - False
复制代码
4 u' c/ y3 e# r4 S7 cmath.isnan(x) 如果x不是数字True,否则返回False+ E2 t1 F6 M1 W1 l* A; t
- #如果x不是数字True,否则返回False i% Y6 u" f! b$ o- d& i% u6 z
- isnan(x) -> bool" X" `% R9 {" a4 K) I
- Return True if x is a NaN (not a number), and False otherwise.
2 D; T+ e& B0 d( S - >>> math.isnan(23) H* f. W) C- {$ i# C3 [/ B7 }
- False
. M8 l/ ^$ h6 e1 `9 k9 J j4 M' K - >>> math.isnan(0.01); o* U9 m$ k# w# B9 z
- False
复制代码
# E6 Q. \7 R; v) _math.ldexp(x,i) 返回x*(2**i)的值5 X/ J+ q& _* V8 ~
- #返回x*(2**i)的值1 _, p& `0 X) H6 v! j1 ?( V* _
- ldexp(x, i)
% F/ M7 B) J K9 P+ p5 ], C - Return x * (2**i).2 d8 i: ~ m& ~+ p" N; _
- >>> math.ldexp(5,5)
' J9 c8 I) K d! x - 160.0
0 A3 C4 r6 z |7 I9 m# @ - >>> math.ldexp(3,5)0 \. ?6 x9 V1 J8 {* {& b7 H% F+ b, a
- 96.0
复制代码 ' [/ H7 J1 A3 g4 w
math.log10(x) 返回x的以10为底的对数
$ ]; l; M9 G$ M' `- #返回x的以10为底的对数
& Q- v$ _1 Y" a1 ~0 j8 p" y# S* i - log10(x); Y2 P5 u6 L* X: I) P) V
- Return the base 10 logarithm of x.; s; o# j* \5 F; k
- >>> math.log10(10)9 G9 S9 Z+ O* T- W$ Q
- 1.0
, J3 e' v7 u' \, R% M - >>> math.log10(100)) J3 e1 \0 a+ Z- q
- 2.0
0 l8 \+ I) z; s! E: W! ~ - #即10的1.3次方的结果为20" p: l4 _( [( h
- >>> math.log10(20)
* H+ U6 e2 ]# }! { - 1.3010299956639813
复制代码 ; S6 F+ _, U0 ]+ r* j
math.log1p(x) 返回x+1的自然对数(基数为e)的值
3 n% i+ Y* n7 \( a% w( f( }- #返回x+1的自然对数(基数为e)的值/ @, _" f( {6 ^9 J6 F8 _5 M z
- log1p(x)6 Y9 V6 m+ F* y& Z5 g a9 w
- Return the natural logarithm of 1+x (base e).
8 t% p8 h X, h" W - The result is computed in a way which is accurate for x near zero.
3 @/ [2 Y* \% {5 y - >>> math.log(10)
/ q" ^: L! P* U4 N, H - 2.302585092994046
; n6 H7 m* o. f& O9 {* V5 J" ~ - >>> math.log1p(10)3 @/ ~) ^- M1 S3 ?0 O
- 2.3978952727983707" ?4 a' ?. x6 T; T E: g/ T; @! s
- >>> math.log(11)- _, K9 [# L& l5 `: ^
- 2.3978952727983707
复制代码
0 p2 h2 o7 z. T. l V* C% d2 _+ }math.log2(x) 返回x的基2对数" v3 a3 Z1 G; R2 x2 @) v$ f) B7 d
- #返回x的基2对数. K4 ^2 L# s1 |6 e
- log2(x)
* x1 ~/ B" ~+ |- h4 |: H* s - Return the base 2 logarithm of x. N {3 x, ~2 V3 q6 G: k' F$ G9 g
- >>> math.log2(32)
& a; y% d4 w% q9 | - 5.0( [4 R5 j9 P$ y3 V
- >>> math.log2(20)
' z k9 r5 W) w/ Y' E x - 4.321928094887363& O% k5 y3 A6 z% @7 Q/ n
- >>> math.log2(16): S" ^) C) M, A- m
- 4.0
复制代码 & z/ u8 ?4 H# P( {
math.modf(x) 返回由x的小数部分和整数部分组成的元组- i/ f% ^4 c7 \' V0 Q
- #返回由x的小数部分和整数部分组成的元组# O4 ?! A" P: j, ^/ z/ R
- modf(x); X8 D+ Y+ R0 n+ q4 b3 h7 D
- Return the fractional and integer parts of x. Both results carry the sign
0 E6 k+ N L8 x! N; I: Z- r- } - of x and are floats.+ C' `$ D: ?1 T% X+ }
- >>> math.modf(math.pi)
8 @- G: y- K1 H3 k/ f9 N - (0.14159265358979312, 3.0) Y2 d' p9 C$ p* M2 Y" o( a/ W
- >>> math.modf(12.34)
2 a( R, S- R8 ~3 B - (0.33999999999999986, 12.0)
复制代码 , e7 Y/ K9 p2 H0 G
math.sqrt(x) 求x的平方根
; j1 T8 |$ A# P- #求x的平方根
; N. M9 I2 M7 p1 W. V0 r6 P9 | - sqrt(x)
, Y+ |5 p$ b5 r. E' G$ u - Return the square root of x.
+ o* L- R; [. P) J6 M - >>> math.sqrt(100)
5 d) S/ G, W# u - 10.0
0 `$ a- l9 k/ A& l7 e' ]; i3 T3 y - >>> math.sqrt(16). E7 Q: Q i) y+ S. v+ U
- 4.0) }: X: N6 b, n. s( v
- >>> math.sqrt(20)5 t& q4 [& Q8 k% b8 {% Y! {) ^' y. p5 J
- 4.47213595499958
复制代码
8 C6 {0 v2 ^' t, N! ?math.trunc(x) 返回x的整数部分- #返回x的整数部分
V8 ]( b, n$ m% U+ W& i ^5 m - trunc(x:Real) -> Integral' ] k: r9 E! b* q" I" {
- Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.! X% [% P$ T5 O% Q! }4 O4 q$ |. U; e) D
- >>> math.trunc(6.789): a% B6 z2 C6 U" N5 L
- 6$ w4 R. W4 T5 G' T. Y3 q
- >>> math.trunc(math.pi)
/ x' W% C" E( e% d$ b; H& i - 3
7 [ n, y) H( K/ A; E9 Z7 w5 q0 ] - >>> math.trunc(2.567)/ [. b$ ?5 `# S
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|