Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions docs/upcoming_changes/9416.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Support math.log2
=================
Comment thread
guilhermeleobas marked this conversation as resolved.
Resolved

Support for ``math.log2`` is added.
1 change: 1 addition & 0 deletions numba/core/typing/mathdecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@infer_global(math.log)
@infer_global(math.log1p)
@infer_global(math.log10)
@infer_global(math.log2)
@infer_global(math.sin)
@infer_global(math.cos)
@infer_global(math.tan)
Expand Down
1 change: 1 addition & 0 deletions numba/cpython/mathimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def float_impl(context, builder, sig, args):
exp_impl = unary_math_intr(math.exp, 'llvm.exp')
log_impl = unary_math_intr(math.log, 'llvm.log')
log10_impl = unary_math_intr(math.log10, 'llvm.log10')
log2_impl = unary_math_intr(math.log2, 'llvm.log2')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this implementation should raise ValueError('math domain error') for input values that are zero or negative, however, what is present is no different to e.g. log10 above which would suffer from the same issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can tackle this in a follow-up PR if needed.

sin_impl = unary_math_intr(math.sin, 'llvm.sin')
cos_impl = unary_math_intr(math.cos, 'llvm.cos')

Expand Down
12 changes: 12 additions & 0 deletions numba/tests/test_mathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def log10(x):
return math.log10(x)


def log2(x):
return math.log2(x)


def floor(x):
return math.floor(x)

Expand Down Expand Up @@ -285,6 +289,14 @@ def test_log10(self):
x_values = [1, 10, 100, 1000, 100000, 1000000, 0.1, 1.1]
self.run_unary(pyfunc, x_types, x_values)

def test_log2(self):
pyfunc = log2
x_types = [types.int16, types.int32, types.int64,
types.uint16, types.uint32, types.uint64,
types.float32, types.float64]
x_values = [1, 10, 100, 1000, 100000, 1000000, 0.1, 1.1]
self.run_unary(pyfunc, x_types, x_values)

def test_asin(self):
pyfunc = asin
x_types = [types.int16, types.int32, types.int64,
Expand Down