Skip to content

bpo-45917: Add math.exp2() method - return 2 raised to the power of x #29829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ Julien Miotte
Andrii V. Mishkovskyi
Dom Mitchell
Dustin J. Mitchell
Gideon Mitchell
Tim Mitchell
Zubin Mithra
Florian Mladitsch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :func:`math.exp2`:, which returns 2 raised to the power of x.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a function that returns 2 raised to a given number :func:`math.exp2`
4 changes: 4 additions & 0 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy