Skip to content

Commit 6266e4a

Browse files
Turretedmdickinson
andauthored
bpo-45917: Add math.exp2() method - return 2 raised to the power of x (GH-29829)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
1 parent c1f93f0 commit 6266e4a

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
lines changed

Doc/library/math.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ Power and logarithmic functions
356356
or ``pow(math.e, x)``.
357357

358358

359+
.. function:: exp2(x)
360+
361+
Return *2* raised to the power *x*.
362+
363+
.. versionadded:: 3.11
364+
365+
359366
.. function:: expm1(x)
360367

361368
Return *e* raised to the power *x*, minus 1. Here *e* is the base of natural

Doc/whatsnew/3.11.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ fractions
203203

204204
math
205205
----
206+
* Add :func:`math.exp2`: return 2 raised to the power of x.
207+
(Contributed by Gideon Mitchell in :issue:`45917`.)
206208

207209
* Add :func:`math.cbrt`: return the cube root of x.
208210
(Contributed by Ajith Ramachandran in :issue:`44357`.)

Lib/test/test_math.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ def testExp(self):
501501
self.assertTrue(math.isnan(math.exp(NAN)))
502502
self.assertRaises(OverflowError, math.exp, 1000000)
503503

504+
def testExp2(self):
505+
self.assertRaises(TypeError, math.exp2)
506+
self.ftest('exp2(-1)', math.exp2(-1), 0.5)
507+
self.ftest('exp2(0)', math.exp2(0), 1)
508+
self.ftest('exp2(1)', math.exp2(1), 2)
509+
self.ftest('exp2(2.3)', math.exp2(2.3), 4.924577653379665)
510+
self.assertEqual(math.exp2(INF), INF)
511+
self.assertEqual(math.exp2(NINF), 0.)
512+
self.assertTrue(math.isnan(math.exp2(NAN)))
513+
self.assertRaises(OverflowError, math.exp2, 1000000)
514+
504515
def testFabs(self):
505516
self.assertRaises(TypeError, math.fabs)
506517
self.ftest('fabs(-1)', math.fabs(-1), 1)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ Julien Miotte
11911191
Andrii V. Mishkovskyi
11921192
Dom Mitchell
11931193
Dustin J. Mitchell
1194+
Gideon Mitchell
11941195
Tim Mitchell
11951196
Zubin Mithra
11961197
Florian Mladitsch
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added :func:`math.exp2`:, which returns 2 raised to the power of x.

Modules/mathmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,9 @@ FUNC1A(erfc, m_erfc,
12481248
FUNC1(exp, exp, 1,
12491249
"exp($module, x, /)\n--\n\n"
12501250
"Return e raised to the power of x.")
1251+
FUNC1(exp2, exp2, 1,
1252+
"exp2($module, x, /)\n--\n\n"
1253+
"Return 2 raised to the power of x.")
12511254
FUNC1(expm1, expm1, 1,
12521255
"expm1($module, x, /)\n--\n\n"
12531256
"Return exp(x)-1.\n\n"
@@ -3564,6 +3567,7 @@ static PyMethodDef math_methods[] = {
35643567
{"erf", math_erf, METH_O, math_erf_doc},
35653568
{"erfc", math_erfc, METH_O, math_erfc_doc},
35663569
{"exp", math_exp, METH_O, math_exp_doc},
3570+
{"exp2", math_exp2, METH_O, math_exp2_doc},
35673571
{"expm1", math_expm1, METH_O, math_expm1_doc},
35683572
{"fabs", math_fabs, METH_O, math_fabs_doc},
35693573
MATH_FACTORIAL_METHODDEF

0 commit comments

Comments
 (0)