summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGideon <41593269+Turreted@users.noreply.github.com>2021-11-29 18:55:43 (GMT)
committerGitHub <noreply@github.com>2021-11-29 18:55:43 (GMT)
commit6266e4af873a27c9d352115f2f7a1ad0885fc031 (patch)
treef7cccb86aa38df82d29cf17767c8c7f571e5cf00
parentc1f93f0d378958dfae4f24aad0c0088e3e04e403 (diff)
downloadcpython-6266e4af873a27c9d352115f2f7a1ad0885fc031.zip
cpython-6266e4af873a27c9d352115f2f7a1ad0885fc031.tar.gz
cpython-6266e4af873a27c9d352115f2f7a1ad0885fc031.tar.bz2
bpo-45917: Add math.exp2() method - return 2 raised to the power of x (GH-29829)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
-rw-r--r--Doc/library/math.rst7
-rw-r--r--Doc/whatsnew/3.11.rst2
-rw-r--r--Lib/test/test_math.py11
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2021-11-28-17-24-11.bpo-45917.J5TIrd.rst1
-rw-r--r--Modules/mathmodule.c4
6 files changed, 26 insertions, 0 deletions
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 7118678..1ad6045 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -356,6 +356,13 @@ Power and logarithmic functions
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
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 3b65921..8db26cd 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/Lib/test/test_math.py b/Lib/test/test_math.py
index a9f1b1e..a7df00f 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -501,6 +501,17 @@ class MathTests(unittest.TestCase):
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.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)))
+ self.assertRaises(OverflowError, math.exp2, 1000000)
+
def testFabs(self):
self.assertRaises(TypeError, math.fabs)
self.ftest('fabs(-1)', math.fabs(-1), 1)
diff --git a/Misc/ACKS b/Misc/ACKS
index 204293f..94a82a0 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
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 0000000..effd8da
--- /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 67669f1..64ce4e6 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