Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6f2267

Browse files
committed
Added degrees() and radians() to mathmodule. Closes patch 552452 and
feature request 426539.
1 parent 64108af commit d6f2267

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Modules/mathmodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ math_log10(PyObject *self, PyObject *args)
274274
static char math_log10_doc[] =
275275
"log10(x) -> the base 10 logarithm of x.";
276276

277+
static const double degToRad = 3.141592653589793238462643383 / 180.0;
278+
279+
static PyObject *
280+
math_degrees(PyObject *self, PyObject *args)
281+
{
282+
double x;
283+
if (! PyArg_ParseTuple(args, "d:degrees", &x))
284+
return NULL;
285+
return PyFloat_FromDouble(x / degToRad);
286+
}
287+
288+
static char math_degrees_doc[] =
289+
"degrees(x) -> converts angle x from radians to degrees";
290+
291+
static PyObject *
292+
math_radians(PyObject *self, PyObject *args)
293+
{
294+
double x;
295+
if (! PyArg_ParseTuple(args, "d:radians", &x))
296+
return NULL;
297+
return PyFloat_FromDouble(x * degToRad);
298+
}
299+
300+
static char math_radians_doc[] =
301+
"radians(x) -> converts angle x from degrees to radians";
277302

278303
static PyMethodDef math_methods[] = {
279304
{"acos", math_acos, METH_VARARGS, math_acos_doc},
@@ -283,6 +308,7 @@ static PyMethodDef math_methods[] = {
283308
{"ceil", math_ceil, METH_VARARGS, math_ceil_doc},
284309
{"cos", math_cos, METH_VARARGS, math_cos_doc},
285310
{"cosh", math_cosh, METH_VARARGS, math_cosh_doc},
311+
{"degrees", math_degrees, METH_VARARGS, math_degrees_doc},
286312
{"exp", math_exp, METH_VARARGS, math_exp_doc},
287313
{"fabs", math_fabs, METH_VARARGS, math_fabs_doc},
288314
{"floor", math_floor, METH_VARARGS, math_floor_doc},
@@ -294,6 +320,7 @@ static PyMethodDef math_methods[] = {
294320
{"log10", math_log10, METH_VARARGS, math_log10_doc},
295321
{"modf", math_modf, METH_VARARGS, math_modf_doc},
296322
{"pow", math_pow, METH_VARARGS, math_pow_doc},
323+
{"radians", math_radians, METH_VARARGS, math_radians_doc},
297324
{"sin", math_sin, METH_VARARGS, math_sin_doc},
298325
{"sinh", math_sinh, METH_VARARGS, math_sinh_doc},
299326
{"sqrt", math_sqrt, METH_VARARGS, math_sqrt_doc},

0 commit comments

Comments
 (0)