表格中的三角函数,在Python里都是属于math模块的。在之前 Python相关的数学运算函数 的文章里,已经介绍过math模块,该模块对应的C源代码位于Modules/mathmodule.c文件中...
函数名 | 描述 |
---|---|
acos(x) | 返回参数x的反余弦值,返回值以弧度为单位。 |
asin(x) | 返回参数x的反正弦值,返回值以弧度为单位。 |
atan(x) | 返回参数x的反正切值,返回值以弧度为单位。 |
atan2(y, x) | 计算并返回atan(y / x)的值,返回值以弧度为单位。 |
cos(x) | 返回角度x的余弦值(角度x以弧度为单位)。 |
hypot(x, y) |
返回以x,y为两直角边的直接三角形的斜边长, 相当于sqrt(x*x + y*y),即x的平方加上y的平方,相加的 结果再开平方根。 |
sin(x) | 返回角度x的正弦值(角度x以弧度为单位)。 |
tan(x) | 返回角度x的正切值(角度x以弧度为单位)。 |
degrees(x) | 将参数x由弧度转为度。 |
radians(x) | 将参数x由度转为弧度。 |
[email protected]:~/Downloads/Python-2.7.8$ ls Modules/mathmodule.c Modules/mathmodule.c [email protected]:~/Downloads/Python-2.7.8$ |
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]:~$ gdb -q python Reading symbols from /mnt/zenglOX/Python-2.7.8/python...done. (gdb) r Starting program: /mnt/zenglOX/Python-2.7.8/python [Thread debugging using libthread_db enabled] 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 [41074 refs] >>> math.acos(0.6) Program received signal SIGINT, Interrupt. (按ctrl+c组合键来中断Python,并进入gdb调试界面) 0xb7ee09f8 in ___newselect_nocancel () from /lib/libc.so.6 (gdb) b math_acos Breakpoint 1 at 0xb7fd3e35: file /mnt/zenglOX/Python-2.7.8/Modules/mathmodule.c, line 809. (gdb) c Continuing. (此处再按一次回车符,让上面的math.acos(0.6)脚本得以执行, 并触发mathmodule.c里设置的断点) Breakpoint 1, math_acos (self=0x0, args=0x82114a4) at /mnt/zenglOX/Python-2.7.8/Modules/mathmodule.c:809 809 FUNC1(acos, acos, 0, (gdb) s math_1 (arg=0x82114a4, func=0xb7f7a430 <acos>, can_overflow=0) at /mnt/zenglOX/Python-2.7.8/Modules/mathmodule.c:686 686 x = PyFloat_AsDouble(arg); (gdb) n 687 if (x == -1.0 && PyErr_Occurred()) (gdb) p x $1 = 0.59999999999999998 (gdb) n 689 errno = 0; (gdb) n 691 r = (*func)(x); (gdb) p func $2 = (double (*)(double)) 0xb7f7a430 <acos> (gdb) si 0xb7fd3aeb 691 r = (*func)(x); (gdb) si 0xb7fd3aee 691 r = (*func)(x); (gdb) si 0xb7fd3af1 691 r = (*func)(x); (gdb) si 0xb7fd3af4 691 r = (*func)(x); (gdb) si 0xb7f7a430 in acos () from /lib/libm.so.6 (gdb) finish Run till exit from #0 0xb7f7a430 in acos () from /lib/libm.so.6 0xb7fd3af6 in math_1 (arg=0x82114a4, func=0xb7f7a430 <acos>, can_overflow=0) at /mnt/zenglOX/Python-2.7.8/Modules/mathmodule.c:691 691 r = (*func)(x); (gdb) n 693 if (Py_IS_NAN(r)) { (gdb) p r $3 = 0.9272952180016123 (gdb) c Continuing. 0.9272952180016123 [41076 refs] >>> quit() [18354 refs] Program exited normally. (gdb) q [email protected]:~$ |
[email protected]:~$ man acos ACOS(3) Linux Programmer's Manual ACOS(3) NAME acos, acosf, acosl - arc cosine function SYNOPSIS #include <math.h> double acos(double x); float acosf(float x); long double acosl(long double x); .................................................... [email protected]:~$ man asin ASIN(3) Linux Programmer's Manual ASIN(3) NAME asin, asinf, asinl - arc sine function SYNOPSIS #include <math.h> double asin(double x); float asinf(float x); long double asinl(long double x); .................................................... [email protected]:~$ man atan ATAN(3) Linux Programmer's Manual ATAN(3) NAME atan, atanf, atanl - arc tangent function SYNOPSIS #include <math.h> double atan(double x); float atanf(float x); long double atanl( long double x); .................................................... [email protected]:~$ man atan2 ATAN2(3) Linux Programmer's Manual ATAN2(3) NAME atan2, atan2f, atan2l - arc tangent function of two variables SYNOPSIS #include <math.h> double atan2(double y, double x); float atan2f(float y, float x); long double atan2l(long double y, long double x); .................................................... DESCRIPTION The atan2() function calculates the principal value of the arc tangent of y/x, using the signs of the two arguments to determine the quadrant of the result. .................................................... [email protected]:~$ man cos COS(3) Linux Programmer's Manual COS(3) NAME cos, cosf, cosl - cosine function SYNOPSIS #include <math.h> double cos(double x); float cosf(float x); long double cosl(long double x); .................................................... [email protected]:~$ man hypot HYPOT(3) Linux Programmer's Manual HYPOT(3) NAME hypot, hypotf, hypotl - Euclidean distance function SYNOPSIS #include <math.h> double hypot(double x, double y); float hypotf(float x, float y); long double hypotl(long double x, long double y); .................................................... [email protected]:~$ man sin SIN(3) Linux Programmer's Manual SIN(3) NAME sin, sinf, sinl - sine function SYNOPSIS #include <math.h> double sin(double x); float sinf(float x); long double sinl(long double x); .................................................... [email protected]:~$ man tan TAN(3) Linux Programmer's Manual TAN(3) NAME tan, tanf, tanl - tangent function SYNOPSIS #include <math.h> double tan(double x); float tanf(float x); long double tanl(long double x); .................................................... [email protected]:~$ |
static const double degToRad = Py_MATH_PI / 180.0; static const double radToDeg = 180.0 / Py_MATH_PI; static PyObject * math_degrees(PyObject *self, PyObject *arg) { double x = PyFloat_AsDouble(arg); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x * radToDeg); } .................................................... static PyObject * math_radians(PyObject *self, PyObject *arg) { double x = PyFloat_AsDouble(arg); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x * degToRad); } |
[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.acos(0.6) 0.9272952180016123 >>> math.asin(0.6) 0.6435011087932844 >>> math.atan(0.6) 0.5404195002705842 >>> math.atan2(6, 10) 0.5404195002705842 >>> math.degrees(0.8) 45.836623610465864 >>> math.radians(45.8366) 0.7999995879196328 >>> math.cos(0.8) 0.6967067093471654 >>> math.hypot(3, 4) 5.0 >>> math.sin(0.8) 0.7173560908995228 >>> math.tan(0.8) 1.0296385570503641 >>> quit() [email protected]:~$ |
PyMODINIT_FUNC initmath(void) { PyObject *m; m = Py_InitModule3("math", math_methods, module_doc); if (m == NULL) goto finally; PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); finally: return; } |
#ifndef Py_MATH_PI #define Py_MATH_PI 3.14159265358979323846 #endif .................................................... #ifndef Py_MATH_E #define Py_MATH_E 2.7182818284590452354 #endif |
[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.pi 3.141592653589793 >>> math.e 2.718281828459045 >>> math.pi = 10.10 >>> math.e = 10.10 >>> math.pi 10.1 >>> math.e 10.1 >>> quit() [email protected]:~$ |