马上注册,结交更多好友,享用更多功能,让你轻松玩转新大榭论坛!
您需要 登录 才可以下载或查看,没有账号?注册
x
! f9 B B; z. Z$ T, R【注意】在使用之前,必须先导入math模块,以开方函数 sqrt 为例,介绍2种引入方式及各自使用方法。' q; g% E+ X3 l8 P" O- k2 W9 b1 {( N
K! C+ [8 q) z( f方法1:
: [7 Q; Y1 G2 O/ M9 A- >>> import math
6 ~0 C0 u3 _) c- a' S } - >>> math.sqrt(9)
" \0 N3 \$ R2 b- i) B6 c: ? - 3.0
复制代码 方法2: E6 s- ]4 _6 O3 `
- >>> from math import sqrt; z. D4 p; |5 I# `$ V* D& @1 t
- >>> sqrt(9)
* V, x' B* u$ X! a: W" r - 3.0
复制代码
/ z8 X: C- Q/ P$ v . |6 H# B( f7 J0 g% l2 E
math.e 表示一个常量
) I) \5 B- N8 z/ J4 Q3 z( E- #表示一个常量
. W+ c# H5 Y5 c/ \" R |0 a! F" c# m - >>> math.e
- d) W; U5 o) z' c - 2.718281828459045
复制代码 5 I% t. r* {3 a( z) t$ B
math.pi 数字常量,圆周率
* Y& O7 y* d ^( |+ |* }- #数字常量,圆周率( L( p8 l: Y) Q+ s% [( l; f
- >>> print(math.pi)
6 X& U- ~' k K8 Y8 z N - 3.141592653589793
复制代码 , s9 [7 r% b8 B
math.ceil(x) 取大于等于x的最小的整数值,如果x是一个整数,则返回x
9 z% J% ~ b/ k, z6 b/ m6 V- #取大于等于x的最小的整数值,如果x是一个整数,则返回x% F. Q3 T9 q. A }# j& T
- ceil(x). @9 M b5 `8 M. e- p G: p; H
- Return the ceiling of x as an int.6 l8 G% C+ {" q( W7 j" |
- This is the smallest integral value >= x.
$ c' N& ~: `/ O9 T# H0 \
, O U. @4 ~; D# ]6 @- >>> math.ceil(4.01)& }* G6 A1 s( z3 H6 Z* b
- 5
& D0 Z6 |4 z+ Q+ K, \$ f: F - >>> math.ceil(4.99)
4 k( L$ H# _$ n9 @5 D/ [8 W - 5
& z/ {' ^' z m \$ a1 o - >>> math.ceil(-3.99)
) D* S" q4 C9 x* K* T - -3
' V* x Z% q7 o* \% u4 L - >>> math.ceil(-3.01)4 g# A# p3 S5 l4 C: d
- -3
复制代码
, m# x3 s& _ N5 G! f, emath.floor(x) 取小于等于x的最大的整数值,如果x是一个整数,则返回自身
8 P( H7 f5 P9 h" b1 N T4 I/ s' r" @- #取小于等于x的最大的整数值,如果x是一个整数,则返回自身5 P% [: E3 q& }$ [2 E- i
- floor(x)' d6 b" X, A2 t
- Return the floor of x as an int.
! y3 R0 b- _! M; Z2 _; B& S$ E8 y - This is the largest integral value <= x.
/ R/ ]8 ~* M* D6 Q1 d5 y - >>> math.floor(4.1)
0 O7 b1 |% G4 ?$ T+ P( E - 4
8 D$ ^& l! k# ^, `; ^1 ] - >>> math.floor(4.999)
2 V- h2 [8 r2 l. o* T4 f9 @ - 46 l" `% L2 C2 M
- >>> math.floor(-4.999)8 `/ X8 h) ?) b `: ?; T; r5 m
- -5
6 Q+ |1 D7 X, w; X3 l3 Z! B - >>> math.floor(-4.01)
7 {9 b4 V+ s3 F/ V* g4 | - -5
复制代码 " [" D. }3 P! y8 X2 D7 |3 _( n. n
math.pow(x,y) 返回x的y次方,即x**y
( [6 Q! v% U9 O+ l. a& K* u- #返回x的y次方,即x**y
) [0 T( D; E4 m; C% M1 C - pow(x, y)
9 n; Z0 U# [( u @6 }% E( [ - Return x**y (x to the power of y).& \0 |. j' \% f( q7 J" V
- >>> math.pow(3,4)
2 T+ x U. b5 s1 S; D) K P - 81.0
2 H% M5 u! u2 s. x0 W9 \ - >>> ( S9 j% Y: a8 h3 e
- >>> math.pow(2,7)
, M4 c' {7 ?* G! \/ f5 [/ i - 128.0
复制代码
+ _% O4 [3 ?/ H9 K- P$ tmath.log(x) 返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)# F1 L4 N& `' P7 L8 N
- #返回x的自然对数,默认以e为基数,base参数给定时,将x的对数返回给定的base,计算式为:log(x)/log(base)5 G! K* t' k8 C5 s
- log(x[, base])
5 r; j. B7 ~; \, B - Return the logarithm of x to the given base.
6 ?, |$ M4 a' v( K" I - If the base not specified, returns the natural logarithm (base e) of x.
2 a4 ?7 D0 a' q& U$ ?3 o - >>> math.log(10)5 w2 i" X* G- X) v5 L
- 2.302585092994046/ y* W8 x0 `& {+ ~
- >>> math.log(11)1 d$ C+ @- ^) P, K
- 2.3978952727983707
7 T! F/ z5 N0 q* K- A+ T - >>> math.log(20)
/ H, r( R; |: M J& @% Q - 2.995732273553991
复制代码
5 E- P% C. [' p1 l$ w1 T; umath.sin(x) 求x(x为弧度)的正弦值. t1 K, c$ |+ T2 j0 @% r* H/ q! Y0 D
- #求x(x为弧度)的正弦值
$ C" ~$ y7 V" G1 n/ M5 o5 s8 H - sin(x)0 B/ A5 e+ k0 P1 ?! ^
- Return the sine of x (measured in radians).
; J9 B$ G; J) [0 f9 y; \, G - >>> math.sin(math.pi/4)
+ B, o, n* P, R/ Q - 0.7071067811865475# W2 k/ k; Q/ V2 O' G: m
- >>> math.sin(math.pi/2)
, B9 C0 L+ N0 d - 1.0+ b5 O4 a4 B. J3 \2 u; O
- >>> math.sin(math.pi/3)
; z4 g: x0 V: u! p4 n - 0.8660254037844386
复制代码
/ \2 u$ u# d) S/ u& Fmath.cos(x) 求x的余弦,x必须是弧度% J- k3 F: }. @& y$ ? J
- #求x的余弦,x必须是弧度
0 ?4 y' i' l' }" k - cos(x)0 w4 t A' c2 r% Z/ D y
- Return the cosine of x (measured in radians).
, s3 ~0 y) M0 R& H5 v8 o @5 h - #math.pi/4表示弧度,转换成角度为45度# \* R8 l4 g3 t( c7 `9 W
- >>> math.cos(math.pi/4)
: j! O" ?7 @7 S' N' ` - 0.7071067811865476
. w2 I) [) J% Y" X( q3 B - math.pi/3表示弧度,转换成角度为60度. L4 V) P. _+ X3 M# y
- >>> math.cos(math.pi/3), q8 w9 s* r: \' ~
- 0.5000000000000001
- n; P& d" k1 @: y& c - math.pi/6表示弧度,转换成角度为30度2 h( a$ X/ C+ v& [- A; |# A
- >>> math.cos(math.pi/6)
, X8 `" Z1 n: n& W0 a, p - 0.8660254037844387
复制代码
4 O+ ^0 r+ f9 L2 jmath.tan(x) 返回x(x为弧度)的正切值3 ~# K$ t2 U- D9 o) B
- #返回x(x为弧度)的正切值1 P* k0 V q+ o
- tan(x)1 I+ B1 P1 V1 m; _
- Return the tangent of x (measured in radians).
( g% H7 G5 F% b* r: G& { - >>> math.tan(math.pi/4)' F* L4 Y% C5 Q: f/ V
- 0.9999999999999999! v& A; X: c" L9 j! V8 g5 {/ c) x) o" j
- >>> math.tan(math.pi/6)$ x/ i) U4 l/ Y2 i* d
- 0.5773502691896257( P# o/ W* l4 r
- >>> math.tan(math.pi/3)
- }8 ^! S2 C. G# f0 W) W4 d, _0 v4 b - 1.7320508075688767
复制代码
( k! J/ K* ~3 c# g2 Amath.degrees(x) 把x从弧度转换成角度) ]* v2 J/ h3 W: {6 e
- #把x从弧度转换成角度
$ |& |: `9 w) ~" E$ e- r$ ?' F - degrees(x)* P j) v3 e3 z$ s( e
- Convert angle x from radians to degrees.
% a9 ?0 \/ g. a& X; I0 u Q A - . t" ~4 q F6 Z3 \
- >>> math.degrees(math.pi/4)4 b/ w: v& W" E7 h& a( ]8 O# {: u3 E% l
- 45.0
! x8 `: ]5 E4 c5 d, v - >>> math.degrees(math.pi)& t( o8 p$ S& Z) _% a p5 _+ ~
- 180.0
) ]/ b# R5 \, G, ~ - >>> math.degrees(math.pi/6)
7 ]. C9 s9 q9 A; w) k" n& S - 29.999999999999996
3 k7 Q6 @, |: \; \8 D1 |0 d - >>> math.degrees(math.pi/3)
1 ~& z) x, {3 f2 u" X m - 59.99999999999999
复制代码 + g, b/ `8 Q& s' o( D/ _# P8 U
math.radians(x) 把角度x转换成弧度
' L: g" a- z# u3 p* @/ e3 x- #把角度x转换成弧度1 [4 A7 ]% `- _6 R
- radians(x)
: i& G7 P8 K5 P& D, y) ]1 e# H w - Convert angle x from degrees to radians.
/ N/ \: t9 j; y - >>> math.radians(45)7 D. k- o+ t8 _
- 0.78539816339744839 q8 M" t3 A" {; k# W" n
- >>> math.radians(60)1 K, f6 L+ Y5 C8 I- \. ?
- 1.0471975511965976
复制代码
y' k5 r8 K& J, Pmath.copysign(x,y) 把y的正负号加到x前面,可以使用0
9 S6 R) |) m" [! Q* `, I) R, U- #把y的正负号加到x前面,可以使用08 h! k" F% H6 N( H( g
- copysign(x, y)
" O+ a+ d* |. h+ t. L( E9 O7 p - Return a float with the magnitude (absolute value) of x but the sign
0 ]) f# R% [* ^2 _ - of y. On platforms that support signed zeros, copysign(1.0, -0.0)
% A" \) o. b" f+ O3 m+ j" I0 A - returns -1.0.
+ N8 X/ L' x6 F: g - / H/ H. Y2 S- E, Z6 x5 n' t
- >>> math.copysign(2,3)
7 M A* K' D4 m& e - 2.0. W; W; `% R3 Q( N
- >>> math.copysign(2,-3)& ^+ i2 M- S8 _5 B; Y3 z8 X* t
- -2.0
) H- A7 J2 g( \0 t5 D% J* E - >>> math.copysign(3,8)
& R! X) ]) W0 y2 L( j - 3.0
3 o4 }" h5 D# e* F - >>> math.copysign(3,-8)
, c4 X& C1 D# ?" m' |3 b - -3.0
复制代码
; D3 o8 b" o N# B# l6 P8 \7 S6 ^math.exp(x) 返回math.e,也就是2.71828的x次方0 X9 F/ H8 A9 Q" _
- #返回math.e,也就是2.71828的x次方- O" V7 \3 N7 w' G9 z
- exp(x)
9 Y- \2 o s# S+ D8 V" T - Return e raised to the power of x.
1 D& D6 Y# T) ~5 T) u* C$ x5 Q - ; z: G! E# @( v. K: }
- >>> math.exp(1)% \9 {+ U) U$ L- O! o! D
- 2.718281828459045 e6 i8 X& V# H
- >>> math.exp(2)
9 G& D/ R+ t$ B% E; ? Y - 7.38905609893065
- `3 j9 [" t# C, t2 c2 h - >>> math.exp(3)
2 t, H! \ O+ }) \1 {. h& M0 ~ - 20.085536923187668
复制代码
/ E" S3 i+ W+ W5 ~ `math.expm1(x) 返回math.e的x(其值为2.71828)次方的值减1
. X) J4 r- h) ^; M; L# I. P# }- #返回math.e的x(其值为2.71828)次方的值减1. A. f8 b4 y1 q/ Z: i
- expm1(x)5 Z% c- }4 G; q5 E
- Return exp(x)-1.
0 G* _5 ]9 s' ] - This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.7 G1 j, \0 r. \/ C" B
- ! [; L% }- |& M
- >>> math.expm1(1)
6 y% I/ e) O* {4 [# ?, Z* ^ - 1.718281828459045
+ W, u! ^ Y$ f9 @ - >>> math.expm1(2)0 _( Y/ |. ? U8 L
- 6.38905609893065
' y; Z/ [6 ~/ R& e- A - >>> math.expm1(3)! D1 a0 B" {( E; B0 K4 u
- 19.085536923187668
复制代码
: s8 ?$ r4 ?: D, X! j1 }: Imath.fabs(x) 返回x的绝对值
9 T( |, l" B( d: j3 s6 w- #返回x的绝对值 a0 T' u1 `( f5 b5 {
- fabs(x)& @7 S# L" b4 E! G8 v' T
- Return the absolute value of the float x.
3 b$ [3 H7 U+ x3 P - , l/ O% _* P& v+ J Z# K
- >>> math.fabs(-0.003)2 a% t7 U* ?3 l( r# Q2 A
- 0.0037 k# }, q' X+ o
- >>> math.fabs(-110)+ {5 }2 S: U- L- @* z) h! C3 X4 Q
- 110.0
4 U% E2 _1 [1 L1 G, d1 { - >>> math.fabs(100)# @3 Q8 t. x/ P h8 X( ?
- 100.0
复制代码
, c$ a% a( c8 x% D# L" {2 ^math.factorial(x) 取x的阶乘的值# F& U X( F' Q# s# A. ^
- #取x的阶乘的值# D1 j2 Z8 G% z# @
- factorial(x) -> Integral' {( P& X" ^3 h/ ]
- Find x!. Raise a ValueError if x is negative or non-integral.
7 G+ j5 f. A2 M( M; M1 W5 y3 J. O - >>> math.factorial(1)& d! W/ `6 y, r8 U: W* J% ]$ ^
- 1. t1 a1 D4 O! V! U6 y$ w
- >>> math.factorial(2)
+ |3 L% @7 i1 f - 2
' B3 a' I# P% {8 z+ ^ - >>> math.factorial(3)
" I a! D& y( K5 ? a - 6
+ v Z# N8 ^& @' l0 k) Y: K - >>> math.factorial(5)
1 i+ T7 N( B& ?7 l/ \ - 120
! W4 v' o. M. B( R - >>> math.factorial(10)/ Y/ Y, X7 V' M& _. H: l
- 3628800
复制代码
# d# |/ ~( y3 m$ rmath.fmod(x,y) 得到x/y的余数,其值是一个浮点数
2 a0 W8 t/ ]4 K2 U& L- #得到x/y的余数,其值是一个浮点数
- @% g$ y6 C8 m4 j9 _) x7 Z% O - fmod(x, y)
* k6 j5 p) m/ r5 }; b% y3 V) j - Return fmod(x, y), according to platform C. x % y may differ.% X9 h1 a$ t7 ~$ d
- >>> math.fmod(20,3)
+ d* v6 J) W4 j - 2.0
4 d q% j# t, g9 [- x - >>> math.fmod(20,7)
9 {3 x5 t B5 x - 6.0
复制代码 + b D: ^8 u' X1 E- g* Z8 D4 k- x
math.frexp(x) 返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围# j- ]6 V3 g; g' A' R
- #返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,
& @9 r5 P" W1 S/ d% v7 q; V - #2**e的值在这个范围内,e取符合要求的最大整数值,然后x/(2**e),得到m的值
$ U+ c# |& K# r) i/ B4 } ]2 U( K - #如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
, B3 I- m _5 l7 g1 F5 K; q - frexp(x)
; d: t9 B/ e4 R# ?: v - Return the mantissa and exponent of x, as pair (m, e).
* N) J/ G2 \$ |- C9 V" p - m is a float and e is an int, such that x = m * 2.**e.
3 S% N8 S6 v) P6 ~. j - If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.# V# [: Q4 D! \5 d
- >>> math.frexp(10)
+ w, z- ^, ]! Q2 W - (0.625, 4)
: I3 K7 W% h: w, n- ?$ q! X- i - >>> math.frexp(75)& d C8 e2 N: ]6 W
- (0.5859375, 7)
- D, Z$ z9 O# C% N) t: A! F - >>> math.frexp(-40)! M% G; R5 q P4 f1 M$ q- Y. \( ^4 n
- (-0.625, 6)' `% R4 K% X7 K( R$ L+ s) [
- >>> math.frexp(-100)- r. @( [4 X! s' ?5 \
- (-0.78125, 7)
/ a, U! ^( d8 h+ ~* {5 J, j/ B - >>> math.frexp(100)- z; X9 ~$ E4 g
- (0.78125, 7)
复制代码 " g Z g. G3 Q
math.fsum(seq) 对迭代器里的每个元素进行求和操作(注:seq 代表 序列)
" n% H0 X8 U V( u& Z- #对迭代器里的每个元素进行求和操作
: l! U0 I! C. l+ `0 j9 h; F - fsum(iterable)
8 F) _+ }1 c. X- I5 S/ q - Return an accurate floating point sum of values in the iterable.7 u, V4 K0 q7 a* F4 a/ \; g6 v
- Assumes IEEE-754 floating point arithmetic.
$ J3 e* p0 W$ v B o X- b - >>> math.fsum([1,2,3,4])
' a8 K0 \) v/ E* K: Q( k - 10.0+ n3 |8 z, M0 b! {# I# ?
- >>> math.fsum((1,2,3,4))
9 K D. m& f9 q% D; i/ m0 K - 10.0 H7 H9 W0 N4 ]/ R @0 X
- >>> math.fsum((-1,-2,-3,-4))
8 q4 C$ p( u9 w - -10.0. S7 G$ Y; m U; f+ p2 B
- >>> math.fsum([-1,-2,-3,-4]); F2 S" z _0 |6 ~
- -10.0
复制代码
) h0 w4 d- ]0 K# |" E1 D" }math.gcd(x,y) 返回x和y的最大公约数2 L. A9 |3 M* x' I
- #返回x和y的最大公约数
1 r$ ?( ]9 X/ b8 g( q, y - gcd(x, y) -> int
/ d; _9 T7 X8 b - greatest common divisor of x and y
) U$ d* V, Z$ Y/ v5 q. S9 e- F& u! m6 r" H - >>> math.gcd(8,6)
6 f6 t f4 m, c* q, P) W - 2
9 o* f1 V0 ^) n% N9 R2 |" m+ B* i8 Z - >>> math.gcd(40,20)
5 Q: o# z0 P& B0 G8 o ~ - 20
, W" K! {2 J: A8 n - >>> math.gcd(8,12)
: X5 K# g. E7 t$ w) |, t! n - 4
复制代码 1 F3 Y% M. |) {" k' ~1 v/ `5 M* G
math.hypot(x,y) 如果x是不是无穷大的数字,则返回True,否则返回False
: H9 A4 P( a- e( g6 y- #得到(x**2+y**2),平方的值
: |! V) t. L' @; \$ e - hypot(x, y)- _ O- B. n( x3 @
- Return the Euclidean distance, sqrt(x*x + y*y).
3 H) R% s& f; s* A" H. ] - >>> math.hypot(3,4). B; g. J( i; K% G8 R
- 5.0/ l7 m' g" E: a
- >>> math.hypot(6,8)
* D6 y1 U. T0 j% N' g& D - 10.0
复制代码 : j, U3 F/ w2 E1 [
math.isfinite() 如果x是正无穷大或负无穷大,则返回True,否则返回False
% c0 q- o2 j( U! a5 q* G( M+ P- #如果x是不是无穷大的数字,则返回True,否则返回False
! [& g5 H. i- E$ C9 I- E - isfinite(x) -> bool
) k+ U' ^; u1 k' m - Return True if x is neither an infinity nor a NaN, and False otherwise.
D$ O3 |# h* t) o - >>> math.isfinite(100)3 f( N3 Q+ q" D3 i% g# O6 C$ q
- True
8 P" h7 r& r! c" R0 J - >>> math.isfinite(0)
6 f4 j2 T4 e3 | - True
6 R; m3 [2 Z0 R3 l8 K& I3 U - >>> math.isfinite(0.1)4 J# b4 O1 Q/ f2 y. }$ @2 k* x
- True
6 T% c l0 X! j/ d: q - >>> math.isfinite("a")
, e. L2 _2 ]* h! o - >>> math.isfinite(0.0001)
5 r) \! J; X4 m' r - True
复制代码 3 D" b2 w; q0 w
math.isinf(x) 如果x是正无穷大或负无穷大,则返回True,否则返回False M/ y4 b2 r9 z& V; D7 `1 T9 C
- #如果x是正无穷大或负无穷大,则返回True,否则返回False
4 o9 Y1 G6 o9 d {6 U - isinf(x) -> bool
( q0 S: |: x! D# m( N - Return True if x is a positive or negative infinity, and False otherwise.: o6 Z/ m0 b: h5 v
- >>> math.isinf(234)7 m; w9 p3 Y/ A
- False# w, Z) O7 O2 c6 M7 d1 ^: F
- >>> math.isinf(0.1)4 ^: C' N2 u! M+ P* K5 ?
- False
复制代码 ( Q7 ~3 }7 K8 O$ L6 v/ _% o
math.isnan(x) 如果x不是数字True,否则返回False
% x) i! c! ]3 \3 y" l5 l7 |- #如果x不是数字True,否则返回False! M9 Z/ A+ M- G; ~* K
- isnan(x) -> bool# N5 Q5 ~6 K [# Y
- Return True if x is a NaN (not a number), and False otherwise.% M. Q2 [: V7 X; V D O- A& `0 Y, }
- >>> math.isnan(23)
* b- W- ]+ E5 U: b1 D9 [# ]3 l - False
7 M4 S) O1 { ?/ K/ K5 b - >>> math.isnan(0.01)6 {" E5 i H3 [
- False
复制代码 6 A3 m6 k) ?. n( p6 ^7 ~
math.ldexp(x,i) 返回x*(2**i)的值
* X. Z8 `: b% }! J! ^3 R- #返回x*(2**i)的值
9 _% t6 r; w5 n; B - ldexp(x, i)$ o( l0 q% s3 O3 ^3 k; Q
- Return x * (2**i).' v9 k; |1 ^. a. B" ~, [
- >>> math.ldexp(5,5). P. h2 d4 j( l3 o' k
- 160.0& k1 B/ b4 i9 v
- >>> math.ldexp(3,5)) G8 B! K0 [ E5 m+ s
- 96.0
复制代码
( Q# p+ U1 u8 J2 Imath.log10(x) 返回x的以10为底的对数7 ~3 v; c1 ~7 @9 J- F
- #返回x的以10为底的对数) q! U5 d+ P u' Q. m6 v
- log10(x)
+ m& Z2 k' S$ z4 H - Return the base 10 logarithm of x.
7 \) C+ L: n% c0 L. R# \8 N% n - >>> math.log10(10)
_: C; b- C# a6 S8 M' x2 ? - 1.0
9 F% t/ b0 z* b, B" s, l - >>> math.log10(100)6 Y% o' K5 z1 u2 X/ J
- 2.0
: I( s8 u t- e6 @9 _. f - #即10的1.3次方的结果为20* t/ p N/ T o3 K! K
- >>> math.log10(20)
) O/ g! g* Q, P( W' U; F# K( x3 v5 u - 1.3010299956639813
复制代码
, a x U. H( [) c2 R, d4 g- i6 \* bmath.log1p(x) 返回x+1的自然对数(基数为e)的值
; o$ G$ i1 u [0 z9 o ~- #返回x+1的自然对数(基数为e)的值" M/ J7 P5 o8 b9 B0 ~! {
- log1p(x)% K/ H+ E) W0 s8 s* Z1 |6 q( Q, `
- Return the natural logarithm of 1+x (base e).* B* |9 f! V) v% w# V, b( s6 U% l
- The result is computed in a way which is accurate for x near zero.# t6 l. u# `! w
- >>> math.log(10)1 T6 }' I* S! w) q
- 2.3025850929940467 }7 d+ y5 N8 X5 O
- >>> math.log1p(10)
0 s9 U/ F. h2 [; l - 2.3978952727983707
! M) x7 _) Y1 D( u - >>> math.log(11)
/ W8 w, X1 R4 {. v2 @ - 2.3978952727983707
复制代码
- K6 v% S' Q) @! Q$ amath.log2(x) 返回x的基2对数
' E. m: `5 [* @- #返回x的基2对数2 a& u/ Z/ Q1 T
- log2(x)$ H! N/ D4 F" Q# d" E! k2 Q# o1 L
- Return the base 2 logarithm of x.
3 f' p8 p8 i4 O% E. i1 s - >>> math.log2(32)
4 X, }2 C/ a9 g/ s3 x9 u+ q - 5.06 D4 @0 O/ ^5 e+ @0 z9 B# l9 z
- >>> math.log2(20)
# h( o# g& N& m - 4.321928094887363; X! S% S8 @0 _. c4 @& E- Y2 G
- >>> math.log2(16)
+ y8 g7 d4 ^4 m1 n - 4.0
复制代码 & J9 ?' P" h; G) ?. [
math.modf(x) 返回由x的小数部分和整数部分组成的元组/ b2 S6 s6 i, q
- #返回由x的小数部分和整数部分组成的元组* \' t. y7 @ T
- modf(x)
9 X, X1 c0 P0 Q( y3 R. g - Return the fractional and integer parts of x. Both results carry the sign& t& i1 m- E! L: }
- of x and are floats.% x, J0 N& M/ V7 A
- >>> math.modf(math.pi)
* V) G- G% t8 G3 c+ { - (0.14159265358979312, 3.0)5 `* E. o* ~, I% E* {5 z) A
- >>> math.modf(12.34)
# [: b" I( J% r+ J. x - (0.33999999999999986, 12.0)
复制代码
2 P% D7 C' P* v0 R* smath.sqrt(x) 求x的平方根
* Y8 M- \3 _$ e% r5 v! k* j. G- #求x的平方根
z- C7 ?& z" S. W - sqrt(x)3 T) C z a0 u) H# ]& U" f) X
- Return the square root of x.
! b4 Z- V: U; ~; ~; C - >>> math.sqrt(100)* A9 z8 d4 H1 _' n* t! ?
- 10.0) _( u# V9 T+ K1 [; i9 F% W
- >>> math.sqrt(16)8 e# }$ T6 v6 d% t6 U
- 4.0# `" Y8 E2 L" ? B/ W' R6 [/ ~$ ^
- >>> math.sqrt(20)
( ` u/ [7 ~( e. |9 f0 ~9 c0 v - 4.47213595499958
复制代码
6 D, S# H; D) p0 L# A9 V+ U7 kmath.trunc(x) 返回x的整数部分- #返回x的整数部分
, z, M& z. e( Q. \ - trunc(x:Real) -> Integral
$ _! ^4 W5 a' t - Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
( G8 Y6 e- O/ t! O - >>> math.trunc(6.789)! e% c% y. v5 A! Q# O- j
- 6
0 z/ X! c* F' V& G/ I - >>> math.trunc(math.pi)- |0 y7 ], z' }: V
- 3+ s& m, P e# R: s
- >>> math.trunc(2.567)! u L* W$ {. u; n3 ]2 y
- 2
复制代码 注:其中蓝色字体部分是高中需要掌握的基本语法 |
|