cpython: 5e8b94397f81 (original) (raw)

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,8 +16,6 @@ Core and Builtins

new file mode 100644 --- /dev/null +++ b/Modules/clinic/cmathmodule.c.h @@ -0,0 +1,851 @@ +/[clinic input] +preserve +[clinic start generated code]/ + +PyDoc_STRVAR(cmath_acos__doc__, +"acos($module, z, /)\n" +"--\n" +"\n" +"Return the arc cosine of z."); + +#define CMATH_ACOS_METHODDEF [](#l2.15)

+ +static Py_complex +cmath_acos_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_acos(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_acosh__doc__, +"acosh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arccosine of z."); + +#define CMATH_ACOSH_METHODDEF [](#l2.58)

+ +static Py_complex +cmath_acosh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_acosh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_asin__doc__, +"asin($module, z, /)\n" +"--\n" +"\n" +"Return the arc sine of z."); + +#define CMATH_ASIN_METHODDEF [](#l2.101)

+ +static Py_complex +cmath_asin_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_asin(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_asinh__doc__, +"asinh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arc sine of z."); + +#define CMATH_ASINH_METHODDEF [](#l2.144)

+ +static Py_complex +cmath_asinh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_asinh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_atan__doc__, +"atan($module, z, /)\n" +"--\n" +"\n" +"Return the arc tangent of z."); + +#define CMATH_ATAN_METHODDEF [](#l2.187)

+ +static Py_complex +cmath_atan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_atan(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_atanh__doc__, +"atanh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic arc tangent of z."); + +#define CMATH_ATANH_METHODDEF [](#l2.230)

+ +static Py_complex +cmath_atanh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_atanh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_cos__doc__, +"cos($module, z, /)\n" +"--\n" +"\n" +"Return the cosine of z."); + +#define CMATH_COS_METHODDEF [](#l2.273)

+ +static Py_complex +cmath_cos_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_cos(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_cosh__doc__, +"cosh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic cosine of z."); + +#define CMATH_COSH_METHODDEF [](#l2.316)

+ +static Py_complex +cmath_cosh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_cosh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_exp__doc__, +"exp($module, z, /)\n" +"--\n" +"\n" +"Return the exponential value e**z."); + +#define CMATH_EXP_METHODDEF [](#l2.359)

+ +static Py_complex +cmath_exp_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_exp(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_log10__doc__, +"log10($module, z, /)\n" +"--\n" +"\n" +"Return the base-10 logarithm of z."); + +#define CMATH_LOG10_METHODDEF [](#l2.402)

+ +static Py_complex +cmath_log10_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_log10(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_sin__doc__, +"sin($module, z, /)\n" +"--\n" +"\n" +"Return the sine of z."); + +#define CMATH_SIN_METHODDEF [](#l2.445)

+ +static Py_complex +cmath_sin_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sin(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_sinh__doc__, +"sinh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic sine of z."); + +#define CMATH_SINH_METHODDEF [](#l2.488)

+ +static Py_complex +cmath_sinh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sinh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_sqrt__doc__, +"sqrt($module, z, /)\n" +"--\n" +"\n" +"Return the square root of z."); + +#define CMATH_SQRT_METHODDEF [](#l2.531)

+ +static Py_complex +cmath_sqrt_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_sqrt(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_tan__doc__, +"tan($module, z, /)\n" +"--\n" +"\n" +"Return the tangent of z."); + +#define CMATH_TAN_METHODDEF [](#l2.574)

+ +static Py_complex +cmath_tan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_tan(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_tanh__doc__, +"tanh($module, z, /)\n" +"--\n" +"\n" +"Return the hyperbolic tangent of z."); + +#define CMATH_TANH_METHODDEF [](#l2.617)

+ +static Py_complex +cmath_tanh_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_tanh(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_log__doc__, +"log($module, x, y_obj=None, /)\n" +"--\n" +"\n" +"The logarithm of z to the given base.\n" +"\n" +"If the base not specified, returns the natural logarithm (base e) of z."); + +#define CMATH_LOG_METHODDEF [](#l2.662)

+ +static PyObject * +cmath_log_impl(PyModuleDef *module, Py_complex x, PyObject *y_obj); + +static PyObject * +cmath_log(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_phase__doc__, +"phase($module, z, /)\n" +"--\n" +"\n" +"Return argument, also known as the phase angle, of a complex."); + +#define CMATH_PHASE_METHODDEF [](#l2.691)

+ +static PyObject * +cmath_phase_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_phase(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_polar__doc__, +"polar($module, z, /)\n" +"--\n" +"\n" +"Convert a complex from rectangular coordinates to polar coordinates.\n" +"\n" +"r is the distance from 0 and phi the phase angle."); + +#define CMATH_POLAR_METHODDEF [](#l2.721)

+ +static PyObject * +cmath_polar_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_polar(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_rect__doc__, +"rect($module, r, phi, /)\n" +"--\n" +"\n" +"Convert from polar coordinates to rectangular coordinates."); + +#define CMATH_RECT_METHODDEF [](#l2.749)

+ +static PyObject * +cmath_rect_impl(PyModuleDef *module, double r, double phi); + +static PyObject * +cmath_rect(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_isfinite__doc__, +"isfinite($module, z, /)\n" +"--\n" +"\n" +"Return True if both the real and imaginary parts of z are finite, else False."); + +#define CMATH_ISFINITE_METHODDEF [](#l2.778)

+ +static PyObject * +cmath_isfinite_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isfinite(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_isnan__doc__, +"isnan($module, z, /)\n" +"--\n" +"\n" +"Checks if the real or imaginary part of z not a number (NaN)."); + +#define CMATH_ISNAN_METHODDEF [](#l2.806)

+ +static PyObject * +cmath_isnan_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isnan(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} + +PyDoc_STRVAR(cmath_isinf__doc__, +"isinf($module, z, /)\n" +"--\n" +"\n" +"Checks if the real or imaginary part of z is infinite."); + +#define CMATH_ISINF_METHODDEF [](#l2.834)

+ +static PyObject * +cmath_isinf_impl(PyModuleDef *module, Py_complex z); + +static PyObject * +cmath_isinf(PyModuleDef *module, PyObject *args) +{

+

+ +exit:

+} +/[clinic end generated code: output=4407f898ae07c83d input=a9049054013a1b77]/

--- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -8,6 +8,41 @@ float.h. We assume that FLT_RADIX is either 2 or 16. / #include <float.h> +#include "clinic/cmathmodule.c.h" +/[clinic input] +output preset file +module cmath +[clinic start generated code]/ +/[clinic end generated code: output=da39a3ee5e6b4b0d input=ef7e0fdd8a143c03]/ + +/[python input] +class Py_complex_protected_converter(Py_complex_converter):

+ + +class Py_complex_protected_return_converter(CReturnConverter):

+

+PyFPE_END_PROTECT(_return_value); +if (errno == EDOM) {{

+}} +else if (errno == ERANGE) {{

+}} +else {{

+}} +""".strip()) +[python start generated code]/ +/[python end generated code: output=da39a3ee5e6b4b0d input=231019039a6fbb9a]/ + #if (FLT_RADIX != 2 && FLT_RADIX != 16) #error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16" #endif @@ -48,12 +83,12 @@ #define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2) / forward declarations */ -static Py_complex c_asinh(Py_complex); -static Py_complex c_atanh(Py_complex); -static Py_complex c_cosh(Py_complex); -static Py_complex c_sinh(Py_complex); -static Py_complex c_sqrt(Py_complex); -static Py_complex c_tanh(Py_complex); +static Py_complex cmath_asinh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_atanh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_cosh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_sinh_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_sqrt_impl(PyModuleDef *, Py_complex); +static Py_complex cmath_tanh_impl(PyModuleDef , Py_complex); static PyObject * math_error(void); / Code to deal with special values (infinities, NaNs, etc.). / @@ -123,8 +158,18 @@ special_type(double d) static Py_complex acos_special_values[7][7]; +/[clinic input] +cmath.acos -> Py_complex_protected +

+ +Return the arc cosine of z. +[clinic start generated code]/ + static Py_complex -c_acos(Py_complex z) +cmath_acos_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=7c1dd21ff818db6b input=bd6cbd78ae851927]/ { Py_complex s1, s2, r; @@ -145,10 +190,10 @@ c_acos(Py_complex z) } else { s1.real = 1.-z.real; s1.imag = -z.imag;

-PyDoc_STRVAR(c_asin_doc, -"asin(x)\n" -"\n" -"Return the arc sine of x."); - static Py_complex asinh_special_values[7][7]; +/[clinic input] +cmath.asinh = cmath.acos + +Return the hyperbolic arc sine of z. +[clinic start generated code]/ + static Py_complex -c_asinh(Py_complex z) +cmath_asinh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=0c6664823c7b1b35 input=5a21fa0242928c9b]*/ { Py_complex s1, s2, r; @@ -235,10 +285,10 @@ c_asinh(Py_complex z) } else { s1.real = 1.+z.imag; s1.imag = -z.real;

-PyDoc_STRVAR(c_atan_doc, -"atan(x)\n" -"\n" -"Return the arc tangent of x."); - static Py_complex atanh_special_values[7][7]; +/[clinic input] +cmath.atanh = cmath.acos + +Return the hyperbolic arc tangent of z. +[clinic start generated code]/ + static Py_complex -c_atanh(Py_complex z) +cmath_atanh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=279e0b9fefc8da7c input=df19cdc9f9d431c9]/ { Py_complex r; double ay, h; @@ -313,7 +367,7 @@ c_atanh(Py_complex z) / Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */ if (z.real < 0.) {

-PyDoc_STRVAR(c_cos_doc, -"cos(x)\n" -"\n" -"Return the cosine of x."); - /* cosh(infinity + iy) needs to be dealt with specially / static Py_complex cosh_special_values[7][7]; +/[clinic input] +cmath.cosh = cmath.acos + +Return the hyperbolic cosine of z. +[clinic start generated code]/ + static Py_complex -c_cosh(Py_complex z) +cmath_cosh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=f3b5d3282b3024d3 input=d6b66339e9cc332b]/ { Py_complex r; double x_minus_one; @@ -426,18 +484,20 @@ c_cosh(Py_complex z) return r; } -PyDoc_STRVAR(c_cosh_doc, -"cosh(x)\n" -"\n" -"Return the hyperbolic cosine of x."); - / exp(infinity + iy) and exp(-infinity + iy) need special treatment for finite y / static Py_complex exp_special_values[7][7]; +/[clinic input] +cmath.exp = cmath.acos + +Return the exponential value e*z. +[clinic start generated code]/ + static Py_complex -c_exp(Py_complex z) +cmath_exp_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=6f8825eb2bcad9ba input=8b9e6cf8a92174c3]/ { Py_complex r; double l; @@ -486,12 +546,6 @@ c_exp(Py_complex z) return r; } -PyDoc_STRVAR(c_exp_doc, -"exp(x)\n" -"\n" -"Return the exponential value e**x."); - - static Py_complex log_special_values[7][7]; static Py_complex @@ -564,8 +618,15 @@ c_log(Py_complex z) } +/[clinic input] +cmath.log10 = cmath.acos + +Return the base-10 logarithm of z. +[clinic start generated code]/ + static Py_complex -c_log10(Py_complex z) +cmath_log10_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=c7c426ca0e782341 input=cff5644f73c1519c]/ { Py_complex r; int errno_save; @@ -578,36 +639,40 @@ c_log10(Py_complex z) return r; } -PyDoc_STRVAR(c_log10_doc, -"log10(x)\n" -"\n" -"Return the base-10 logarithm of x."); +/[clinic input] +cmath.sin = cmath.acos + +Return the sine of z. +[clinic start generated code]/ static Py_complex -c_sin(Py_complex z) +cmath_sin_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=e7f5e2b253825ac7 input=2d3519842a8b4b85]/ { / sin(z) = -i sin(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;

-PyDoc_STRVAR(c_sin_doc, -"sin(x)\n" -"\n" -"Return the sine of x."); - /* sinh(infinity + iy) needs to be dealt with specially / static Py_complex sinh_special_values[7][7]; +/[clinic input] +cmath.sinh = cmath.acos + +Return the hyperbolic sine of z. +[clinic start generated code]/ + static Py_complex -c_sinh(Py_complex z) +cmath_sinh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=d71fff8298043a95 input=d2d3fc8c1ddfd2dd]/ { Py_complex r; double x_minus_one; @@ -655,16 +720,18 @@ c_sinh(Py_complex z) return r; } -PyDoc_STRVAR(c_sinh_doc, -"sinh(x)\n" -"\n" -"Return the hyperbolic sine of x."); - static Py_complex sqrt_special_values[7][7]; +/[clinic input] +cmath.sqrt = cmath.acos + +Return the square root of z. +[clinic start generated code]/ + static Py_complex -c_sqrt(Py_complex z) +cmath_sqrt_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=b6bda283d0c5a7b4 input=7088b166fc9a58c7]/ { / Method: use symmetries to reduce to the case when x = z.real and y @@ -730,36 +797,40 @@ c_sqrt(Py_complex z) return r; } -PyDoc_STRVAR(c_sqrt_doc, -"sqrt(x)\n" -"\n" -"Return the square root of x."); +/[clinic input] +cmath.tan = cmath.acos + +Return the tangent of z. +[clinic start generated code]/ static Py_complex -c_tan(Py_complex z) +cmath_tan_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=df374bacf36d99b4 input=fc167e528767888e]/ { /* tan(z) = -i tanh(iz) */ Py_complex s, r; s.real = -z.imag; s.imag = z.real;

-PyDoc_STRVAR(c_tan_doc, -"tan(x)\n" -"\n" -"Return the tangent of x."); - /* tanh(infinity + iy) needs to be dealt with specially / static Py_complex tanh_special_values[7][7]; +/[clinic input] +cmath.tanh = cmath.acos + +Return the hyperbolic tangent of z. +[clinic start generated code]/ + static Py_complex -c_tanh(Py_complex z) +cmath_tanh_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=f578773d27a18e96 input=22f67f9dc6d29685]/ { / Formula: @@ -822,25 +893,33 @@ c_tanh(Py_complex z) return r; } -PyDoc_STRVAR(c_tanh_doc, -"tanh(x)\n" -"\n" -"Return the hyperbolic tangent of x."); + +/*[clinic input] +cmath.log

+ +The logarithm of z to the given base. + +If the base not specified, returns the natural logarithm (base e) of z. +[clinic start generated code]*/ static PyObject * -cmath_log(PyObject *self, PyObject *args) +cmath_log_impl(PyModuleDef module, Py_complex x, PyObject y_obj) +/[clinic end generated code: output=35e2a1e5229b5a46 input=ee0e823a7c6e68ea]/ {

- errno = 0; PyFPE_START_PROTECT("complex function", return 0) x = c_log(x);

-} + +/*[clinic input] +cmath.phase -#define FUNC1(stubname, func) [](#l3.586)

-FUNC1(cmath_acos, c_acos) -FUNC1(cmath_acosh, c_acosh) -FUNC1(cmath_asin, c_asin) -FUNC1(cmath_asinh, c_asinh) -FUNC1(cmath_atan, c_atan) -FUNC1(cmath_atanh, c_atanh) -FUNC1(cmath_cos, c_cos) -FUNC1(cmath_cosh, c_cosh) -FUNC1(cmath_exp, c_exp) -FUNC1(cmath_log10, c_log10) -FUNC1(cmath_sin, c_sin) -FUNC1(cmath_sinh, c_sinh) -FUNC1(cmath_sqrt, c_sqrt) -FUNC1(cmath_tan, c_tan) -FUNC1(cmath_tanh, c_tanh) +Return argument, also known as the phase angle, of a complex. +[clinic start generated code]*/ static PyObject * -cmath_phase(PyObject *self, PyObject args) +cmath_phase_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=e09eaf373cb624c3 input=5cf75228ba94b69d]/ {

+ errno = 0; PyFPE_START_PROTECT("arg function", return 0) phi = c_atan2(z); @@ -930,17 +970,23 @@ cmath_phase(PyObject *self, PyObject ar return PyFloat_FromDouble(phi); } -PyDoc_STRVAR(cmath_phase_doc, -"phase(z) -> float\n\n[](#l3.629) -Return argument, also known as the phase angle, of a complex."); +/[clinic input] +cmath.polar +

+ +Convert a complex from rectangular coordinates to polar coordinates. + +r is the distance from 0 and phi the phase angle. +[clinic start generated code]*/ static PyObject * -cmath_polar(PyObject *self, PyObject args) +cmath_polar_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=07d41b16c877875a input=26c353574fd1a861]/ {

+ PyFPE_START_PROTECT("polar function", return 0) phi = c_atan2(z); /* should not cause any exception / r = _Py_c_abs(z); / sets errno to ERANGE on overflow; otherwise 0 */ @@ -951,11 +997,6 @@ cmath_polar(PyObject self, PyObject ar return Py_BuildValue("dd", r, phi); } -PyDoc_STRVAR(cmath_polar_doc, -"polar(z) -> r: float, phi: float\n\n[](#l3.660) -Convert a complex from rectangular coordinates to polar coordinates. r is\n[](#l3.661) -the distance from 0 and phi the phase angle."); - / rect() isn't covered by the C99 standard, but it's not too hard to figure out 'spirit of C99' rules for special value handing: @@ -969,13 +1010,21 @@ the distance from 0 and phi the phase an static Py_complex rect_special_values[7][7]; +/[clinic input] +cmath.rect +

+ +Convert from polar coordinates to rectangular coordinates. +[clinic start generated code]*/ + static PyObject * -cmath_rect(PyObject *self, PyObject args) +cmath_rect_impl(PyModuleDef module, double r, double phi) +/[clinic end generated code: output=d97a8749bd63e9d5 input=24c5646d147efd69]/ { Py_complex z;

} -PyDoc_STRVAR(cmath_rect_doc, -"rect(r, phi) -> z: complex\n\n[](#l3.698) -Convert from polar coordinates to rectangular coordinates."); +/[clinic input] +cmath.isfinite = cmath.polar + +Return True if both the real and imaginary parts of z are finite, else False. +[clinic start generated code]/ static PyObject * -cmath_isfinite(PyObject *self, PyObject args) +cmath_isfinite_impl(PyModuleDef module, Py_complex z) +/[clinic end generated code: output=8f6682fa93de45d6 input=848e7ee701895815]/ {

} -PyDoc_STRVAR(cmath_isinf_doc, -"isinf(z) -> bool\n[](#l3.759) -Checks if the real or imaginary part of z is infinite."); - PyDoc_STRVAR(module_doc, "This module is always available. It provides access to mathematical\n" "functions for complex numbers."); static PyMethodDef cmath_methods[] = {

};