From 31a27c1243bf07922f5afb4ba36c4d9a27694d44 Mon Sep 17 00:00:00 2001 From: Turreted Date: Sun, 28 Nov 2021 20:20:40 -0600 Subject: [PATCH 1/7] add math.exp2 to python's math library --- .../next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst | 1 + Modules/mathmodule.c | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst b/Misc/NEWS.d/next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst new file mode 100644 index 00000000000000..effd8da29a0f20 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst @@ -0,0 +1 @@ +Added :func:`math.exp2`:, which returns 2 raised to the power of x. diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 67669f19bc28cb..64ce4e6a13fd59 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1248,6 +1248,9 @@ FUNC1A(erfc, m_erfc, FUNC1(exp, exp, 1, "exp($module, x, /)\n--\n\n" "Return e raised to the power of x.") +FUNC1(exp2, exp2, 1, + "exp2($module, x, /)\n--\n\n" + "Return 2 raised to the power of x.") FUNC1(expm1, expm1, 1, "expm1($module, x, /)\n--\n\n" "Return exp(x)-1.\n\n" @@ -3564,6 +3567,7 @@ static PyMethodDef math_methods[] = { {"erf", math_erf, METH_O, math_erf_doc}, {"erfc", math_erfc, METH_O, math_erfc_doc}, {"exp", math_exp, METH_O, math_exp_doc}, + {"exp2", math_exp2, METH_O, math_exp2_doc}, {"expm1", math_expm1, METH_O, math_expm1_doc}, {"fabs", math_fabs, METH_O, math_fabs_doc}, MATH_FACTORIAL_METHODDEF From 5ab6380abbff0141b68d926a6b081cca01b75561 Mon Sep 17 00:00:00 2001 From: Turreted Date: Sun, 28 Nov 2021 20:21:49 -0600 Subject: [PATCH 2/7] update documentation to include math.exp2 --- Doc/library/math.rst | 5 +++++ Doc/whatsnew/3.11.rst | 2 ++ Misc/ACKS | 1 + 3 files changed, 8 insertions(+) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 71186788a652af..a9cc7088e2137d 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -355,6 +355,11 @@ Power and logarithmic functions of natural logarithms. This is usually more accurate than ``math.e ** x`` or ``pow(math.e, x)``. +.. function:: exp2(x) + + Return *2* raised to the power *x*. + + .. versionadded:: 3.11 .. function:: expm1(x) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 9751f894f9a9a5..5f4c3723ca2c78 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -203,6 +203,8 @@ fractions math ---- +* Add :func:`math.exp2`: return 2 raised to the power of x. + (Contributed by Gideon Mitchell in :issue:`45917`.) * Add :func:`math.cbrt`: return the cube root of x. (Contributed by Ajith Ramachandran in :issue:`44357`.) diff --git a/Misc/ACKS b/Misc/ACKS index 204293fa50d9c0..94a82a07506220 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1191,6 +1191,7 @@ Julien Miotte Andrii V. Mishkovskyi Dom Mitchell Dustin J. Mitchell +Gideon Mitchell Tim Mitchell Zubin Mithra Florian Mladitsch From 66f67f73db44223c94054e7155c9560122af1317 Mon Sep 17 00:00:00 2001 From: Turreted Date: Sun, 28 Nov 2021 20:22:09 -0600 Subject: [PATCH 3/7] add tests for math.exp2 --- Lib/test/test_math.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index a9f1b1e11bcb39..af5403bcf37c03 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -501,6 +501,16 @@ def testExp(self): self.assertTrue(math.isnan(math.exp(NAN))) self.assertRaises(OverflowError, math.exp, 1000000) + def testExp2(self): + self.assertRaises(TypeError, math.exp2) + self.ftest('exp2(-1)', math.exp2(-1), 0.5) + self.ftest('exp2(0)', math.exp2(0), 1) + self.ftest('exp2(1)', math.exp2(1), 2) + self.assertEqual(math.exp2(INF), INF) + self.assertEqual(math.exp2(NINF), 0.) + self.assertTrue(math.isnan(math.exp2(NAN))) + self.assertRaises(OverflowError, math.exp2, 1000000) + def testFabs(self): self.assertRaises(TypeError, math.fabs) self.ftest('fabs(-1)', math.fabs(-1), 1) From 3c7b741464793e13ec8515c1e2ce6d4e9776dbd1 Mon Sep 17 00:00:00 2001 From: Turreted Date: Sun, 28 Nov 2021 22:46:59 -0600 Subject: [PATCH 4/7] add blurb documentation --- .../NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst b/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst new file mode 100644 index 00000000000000..be3b46a0a78584 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst @@ -0,0 +1 @@ +Added a function that returns 2 raised to a given number :func:`math.exp2` From 54ad30a9b856d0faacb4cd56e2db188f05bb6335 Mon Sep 17 00:00:00 2001 From: Gideon <41593269+Turreted@users.noreply.github.com> Date: Mon, 29 Nov 2021 10:27:52 -0600 Subject: [PATCH 5/7] Resolve Blurb error --- .../NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst diff --git a/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst b/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst deleted file mode 100644 index be3b46a0a78584..00000000000000 --- a/Misc/NEWS.d/next/Library/2021-11-28-22-44-54.bpo-45917.lNn_gz.rst +++ /dev/null @@ -1 +0,0 @@ -Added a function that returns 2 raised to a given number :func:`math.exp2` From b41d5d7e46fcf931a1e8ac31a4d028eb723be280 Mon Sep 17 00:00:00 2001 From: Gideon <41593269+Turreted@users.noreply.github.com> Date: Mon, 29 Nov 2021 10:35:44 -0600 Subject: [PATCH 6/7] Add non-integral testcase to Lib/test/test_math.py Co-authored-by: Mark Dickinson --- Lib/test/test_math.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index af5403bcf37c03..a7df00f0fb1013 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -506,6 +506,7 @@ def testExp2(self): self.ftest('exp2(-1)', math.exp2(-1), 0.5) self.ftest('exp2(0)', math.exp2(0), 1) self.ftest('exp2(1)', math.exp2(1), 2) + self.ftest('exp2(2.3)', math.exp2(2.3), 4.924577653379665) self.assertEqual(math.exp2(INF), INF) self.assertEqual(math.exp2(NINF), 0.) self.assertTrue(math.isnan(math.exp2(NAN))) From 23e5bb86744cd9bf6e0a36d6d346c0b438424205 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Mon, 29 Nov 2021 18:21:56 +0000 Subject: [PATCH 7/7] Inter-function spacing consistency in .rst file --- Doc/library/math.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index a9cc7088e2137d..1ad60459e8d377 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -355,12 +355,14 @@ Power and logarithmic functions of natural logarithms. This is usually more accurate than ``math.e ** x`` or ``pow(math.e, x)``. + .. function:: exp2(x) Return *2* raised to the power *x*. .. versionadded:: 3.11 + .. function:: expm1(x) Return *e* raised to the power *x*, minus 1. Here *e* is the base of natural