From 0a891d70ded5fa8b1e6491529a3b531f521b5ae1 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 15 Aug 2016 09:12:52 -0700 Subject: Issue #12345: Add mathemathcal constant tau to math and cmath. Patch by Lisa Roach. See also PEP 628. --- Doc/library/cmath.rst | 6 ++++-- Doc/library/math.rst | 7 +++++++ Include/pymath.h | 6 ++++++ Lib/test/test_math.py | 1 + Misc/NEWS | 3 +++ Modules/cmathmodule.c | 1 + Modules/mathmodule.c | 1 + 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Doc/library/cmath.rst b/Doc/library/cmath.rst index 62ddb6b..f04f8d0 100644 --- a/Doc/library/cmath.rst +++ b/Doc/library/cmath.rst @@ -253,6 +253,10 @@ Constants The mathematical constant *e*, as a float. +.. data:: tau + + The mathematical constant *τ*, as a float. + .. index:: module: math Note that the selection of functions is similar, but not identical, to that in @@ -276,5 +280,3 @@ cuts for numerical purposes, a good reference should be the following: Kahan, W: Branch cuts for complex elementary functions; or, Much ado about nothing's sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art in numerical analysis. Clarendon Press (1987) pp165-211. - - diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 3fdea18..32e1352 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -426,6 +426,13 @@ Constants The mathematical constant e = 2.718281..., to available precision. +.. data:: tau + + The mathematical constant τ = 6.283185..., to available precision. + Tau is a circle constant equal to 2π, the ratio of a circle's circumference to + its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still) + Wrong `_, and start celebrating + `Tau day `_ by eating twice as much pie! .. data:: inf diff --git a/Include/pymath.h b/Include/pymath.h index ed76053..894362e 100644 --- a/Include/pymath.h +++ b/Include/pymath.h @@ -55,6 +55,12 @@ extern double pow(double, double); #define Py_MATH_E 2.7182818284590452354 #endif +/* Tau (2pi) to 40 digits, taken from tauday.com/tau-digits. */ +#ifndef Py_MATH_TAU +#define Py_MATH_TAU 6.2831853071795864769252867665590057683943L +#endif + + /* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU register and into a 64-bit memory location, rounding from extended precision to double precision in the process. On other platforms it does diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 605adb5..48e8007 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -196,6 +196,7 @@ class MathTests(unittest.TestCase): def testConstants(self): self.ftest('pi', math.pi, 3.1415926) self.ftest('e', math.e, 2.7182818) + self.assertEqual(math.tau, 2*math.pi) def testAcos(self): self.assertRaises(TypeError, math.acos) diff --git a/Misc/NEWS b/Misc/NEWS index eabbf8c..ec86a53 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -57,6 +57,9 @@ Core and Builtins Library ------- +- Issue #12345: Add mathemathcal constant tau to math and cmath. See also + PEP 628. + - Issue #26823: traceback.StackSummary.format now abbreviates large sections of repeated lines as "[Previous line repeated {count} more times]" (this change then further affects other traceback display operations in the module). Patch diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index cba42a7..0e7d4db 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -1239,6 +1239,7 @@ PyInit_cmath(void) PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); + PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */ /* initialize special value tables */ diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index cf04901..43aa229 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2144,6 +2144,7 @@ PyInit_math(void) PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); + PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */ PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf())); #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN) PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan())); -- cgit v0.12