以下记录和上一篇文章一样,都是根据英文教程的第八章来写的。在Python的C源码中,与内建模块相关的C代码位于Python/bltinmodule.c文件中,该文件里有一个builtin_methods的静态数组,该数组中就定义了内建模块会导入的函数名...
函数名 | 描述 |
---|---|
abs(x) | 计算并返回参数x的绝对值,即x与零点之间的距离值。 |
ceil(x) | 计算出x的上限,即函数会返回不小于x的最小整数值。 |
cmp(x, y) | 比较x与y的值,如果x小于y则返回-1,如果x等于y返回0,如果x大于y则返回1 。 |
exp(x) | 计算出 ex 即e的x次方,e是数学中最重要的常数之一,其概念可以参考 e_数学常数 该链接对应的维基百科文章。 |
fabs(x) |
也是计算出参数x的绝对值,与上面abs函数的区别在于:fabs函数无法计算复数的绝对值,另外,fabs返回的结果始终是个浮点数, 而abs函数会根据参数来决定返回的结果类型,比如当abs函数的参数是个整数时,abs函数就会返回整数类型的结果。在下面会对这两个 函数进行举例说明。 |
floor(x) | 计算出x的下限,即函数会返回不大于x的最大整数值。 |
log(x) |
计算并返回参数x的自然对数,参数x必须大于0,当x小于等于0时,会产生domain error的域错误。 有关自然对数的概念,可以参考 自然对数 该链接对应的维基百科文章。 |
log10(x) |
计算出以10为底的x的对数,参数x同样必须大于0,当x小于等于0时,会产生domain error的域错误。 有关对数的概念,可以参考 对数 该链接对应的维基百科文章。 |
max(x1, x2,...) | 从max函数的参数中找到最大值,并返回该值。 |
min(x1, x2,...) | 从min函数的参数中找到最小值,并返回该值。 |
modf(x) | 返回参数x的小数与整数部分,这两部分会以元组的形式进行返回,下面会进行举例说明。 |
pow(x, y) | 计算并返回x的y次方的值。 |
round(x [,n]) | 对参数x进行四舍五入,第二个参数n是可选的,表示舍入到小数点后的第几位。 |
sqrt(x) | 计算参数x的平方根,参数x的值必须大于等于0,否则会产生domain error的域错误。 |
static PyMethodDef builtin_methods[] = { {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc}, {"abs", builtin_abs, METH_O, abs_doc}, {"all", builtin_all, METH_O, all_doc}, {"any", builtin_any, METH_O, any_doc}, {"apply", builtin_apply, METH_VARARGS, apply_doc}, {"bin", builtin_bin, METH_O, bin_doc}, {"callable", builtin_callable, METH_O, callable_doc}, {"chr", builtin_chr, METH_VARARGS, chr_doc}, {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc}, {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, {"dir", builtin_dir, METH_VARARGS, dir_doc}, {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, {"eval", builtin_eval, METH_VARARGS, eval_doc}, {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, {"filter", builtin_filter, METH_VARARGS, filter_doc}, {"format", builtin_format, METH_VARARGS, format_doc}, {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, {"hash", builtin_hash, METH_O, hash_doc}, {"hex", builtin_hex, METH_O, hex_doc}, {"id", builtin_id, METH_O, id_doc}, {"input", builtin_input, METH_VARARGS, input_doc}, {"intern", builtin_intern, METH_VARARGS, intern_doc}, {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, {"iter", builtin_iter, METH_VARARGS, iter_doc}, {"len", builtin_len, METH_O, len_doc}, {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, {"map", builtin_map, METH_VARARGS, map_doc}, {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, {"next", builtin_next, METH_VARARGS, next_doc}, {"oct", builtin_oct, METH_O, oct_doc}, {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, {"ord", builtin_ord, METH_O, ord_doc}, {"pow", builtin_pow, METH_VARARGS, pow_doc}, {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, {"range", builtin_range, METH_VARARGS, range_doc}, {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, {"reload", builtin_reload, METH_O, reload_doc}, {"repr", builtin_repr, METH_O, repr_doc}, {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, {"sum", builtin_sum, METH_VARARGS, sum_doc}, #ifdef Py_USING_UNICODE {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, #endif {"vars", builtin_vars, METH_VARARGS, vars_doc}, {"zip", builtin_zip, METH_VARARGS, zip_doc}, {NULL, NULL}, }; |
[email protected]:~$ python Python 2.7.8 (default, Feb 20 2015, 12:54:46) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> abs(-3) 3 >>> abs(-3.5) 3.5 >>> abs(3+4j) 5.0 >>> cmp(4,3) 1 >>> max(1,3,5,8,100,200) 200 >>> min(1,3,5,8,100,200) 1 >>> round(3.563) 4.0 >>> round(3.563, 0) 4.0 >>> round(3.563, 1) 3.6 >>> round(3.555, 2) 3.56 >>> round(3.565, 2) 3.56 >>> round(3.563, -1) 0.0 >>> round(3.563, -2) 0.0 >>> quit() [email protected]:~$ |
static PyMethodDef math_methods[] = { {"acos", math_acos, METH_O, math_acos_doc}, {"acosh", math_acosh, METH_O, math_acosh_doc}, {"asin", math_asin, METH_O, math_asin_doc}, {"asinh", math_asinh, METH_O, math_asinh_doc}, {"atan", math_atan, METH_O, math_atan_doc}, {"atan2", math_atan2, METH_VARARGS, math_atan2_doc}, {"atanh", math_atanh, METH_O, math_atanh_doc}, {"ceil", math_ceil, METH_O, math_ceil_doc}, {"copysign", math_copysign, METH_VARARGS, math_copysign_doc}, {"cos", math_cos, METH_O, math_cos_doc}, {"cosh", math_cosh, METH_O, math_cosh_doc}, {"degrees", math_degrees, METH_O, math_degrees_doc}, {"erf", math_erf, METH_O, math_erf_doc}, {"erfc", math_erfc, METH_O, math_erfc_doc}, {"exp", math_exp, METH_O, math_exp_doc}, {"expm1", math_expm1, METH_O, math_expm1_doc}, {"fabs", math_fabs, METH_O, math_fabs_doc}, {"factorial", math_factorial, METH_O, math_factorial_doc}, {"floor", math_floor, METH_O, math_floor_doc}, {"fmod", math_fmod, METH_VARARGS, math_fmod_doc}, {"frexp", math_frexp, METH_O, math_frexp_doc}, {"fsum", math_fsum, METH_O, math_fsum_doc}, {"gamma", math_gamma, METH_O, math_gamma_doc}, {"hypot", math_hypot, METH_VARARGS, math_hypot_doc}, {"isinf", math_isinf, METH_O, math_isinf_doc}, {"isnan", math_isnan, METH_O, math_isnan_doc}, {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc}, {"lgamma", math_lgamma, METH_O, math_lgamma_doc}, {"log", math_log, METH_VARARGS, math_log_doc}, {"log1p", math_log1p, METH_O, math_log1p_doc}, {"log10", math_log10, METH_O, math_log10_doc}, {"modf", math_modf, METH_O, math_modf_doc}, {"pow", math_pow, METH_VARARGS, math_pow_doc}, {"radians", math_radians, METH_O, math_radians_doc}, {"sin", math_sin, METH_O, math_sin_doc}, {"sinh", math_sinh, METH_O, math_sinh_doc}, {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, {"tan", math_tan, METH_O, math_tan_doc}, {"tanh", math_tanh, METH_O, math_tanh_doc}, {"trunc", math_trunc, METH_O, math_trunc_doc}, {NULL, NULL} /* sentinel */ }; |
[email protected]:~$ python Python 2.7.8 (default, Feb 20 2015, 12:54:46) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.ceil(3.3) 4.0 >>> math.floor(3.3) 3.0 >>> math.exp(1) 2.718281828459045 >>> math.fabs(3) 3.0 >>> math.fabs(-3) 3.0 >>> math.log(2) 0.6931471805599453 >>> math.log10(2) 0.3010299956639812 >>> math.modf(3.1415) (0.14150000000000018, 3.0) >>> math.modf(-3.1415) (-0.14150000000000018, -3.0) >>> math.pow(2, 3) 8.0 >>> pow(2, 3) 8 >>> 2**3 8 >>> sqrt(25) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sqrt' is not defined >>> math.sqrt(25) 5.0 >>> quit() [email protected]:~$ |
[email protected]:~$ python Python 2.7.8 (default, Feb 20 2015, 12:54:46) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type(abs(-2)) <type 'int'> >>> type(abs(-2.0)) <type 'float'> >>> type(abs(3+4j)) <type 'float'> >>> import math >>> type(math.fabs(-2)) <type 'float'> >>> type(math.fabs(-2.0)) <type 'float'> >>> type(math.fabs(3+4j)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't convert complex to float >>> quit() [email protected]:~$ |
[email protected]:~$ man ceil CEIL(3) Linux Programmer's Manual CEIL(3) NAME ceil, ceilf, ceill - ceiling function: smallest integral value not less than argument SYNOPSIS #include <math.h> double ceil(double x); float ceilf(float x); long double ceill(long double x); .................................................... [email protected]:~$ man floor FLOOR(3) Linux Programmer's Manual FLOOR(3) NAME floor, floorf, floorl - largest integral value not greater than argu‐ ment SYNOPSIS #include <math.h> double floor(double x); float floorf(float x); long double floorl(long double x); .................................................... [email protected]:~$ man exp EXP(3) Linux Programmer's Manual EXP(3) NAME exp, expf, expl - base-e exponential function SYNOPSIS #include <math.h> double exp(double x); float expf(float x); long double expl(long double x); Link with -lm. .................................................... [email protected]:~$ man fabs FABS(3) Linux Programmer's Manual FABS(3) NAME fabs, fabsf, fabsl - absolute value of floating-point number SYNOPSIS #include <math.h> double fabs(double x); float fabsf(float x); long double fabsl(long double x); Link with -lm. .................................................... [email protected]:~$ man log LOG(3) Linux Programmer's Manual LOG(3) NAME log, logf, logl - natural logarithmic function SYNOPSIS #include <math.h> double log(double x); float logf(float x); long double logl(long double x); .................................................... [email protected]:~$ man log10 LOG10(3) Linux Programmer's Manual LOG10(3) NAME log10, log10f, log10l - base-10 logarithmic function SYNOPSIS #include <math.h> double log10(double x); float log10f(float x); long double log10l(long double x); .................................................... [email protected]:~$ man modf MODF(3) Linux Programmer's Manual MODF(3) NAME modf, modff, modfl - extract signed integral and fractional values from floating-point number SYNOPSIS #include <math.h> double modf(double x, double *iptr); float modff(float x, float *iptr); long double modfl(long double x, long double *iptr); .................................................... [email protected]:~$ man pow POW(3) Linux Programmer's Manual POW(3) NAME pow, powf, powl - power functions SYNOPSIS #include <math.h> double pow(double x, double y); float powf(float x, float y); long double powl(long double x, long double y); .................................................... [email protected]:~$ man sqrt SQRT(3) Linux Programmer's Manual SQRT(3) NAME sqrt, sqrtf, sqrtl - square root function SYNOPSIS #include <math.h> double sqrt(double x); float sqrtf(float x); long double sqrtl(long double x); .................................................... [email protected]:~$ |