Skip to content

Commit

Permalink
MAINT: pow1pm1: adopt the convention pow1pm1(-1, 0) is 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
WarrenWeckesser committed Jun 18, 2024
1 parent e0812d3 commit af85b85
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires = [

[project]
name = 'ufunclab'
version = '0.0.8.dev8'
version = '0.0.8.dev9'
description = 'NumPy ufuncs and utilities.'
readme = 'README.md'
requires-python = '>=3.9'
Expand Down
3 changes: 3 additions & 0 deletions src/pow1pm1/define_cxxgen_extmods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
`nan` is returned when `x < -1`.
The function follows the widely used convention that `pow(0, 0)`
is 1, so `pow1pm1(-1, 0)` is 0.0.
Parameters
----------
x : array_like
Expand Down
5 changes: 3 additions & 2 deletions src/pow1pm1/pow1pm1.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
template<typename T>
T pow1pm1(T x, T y)
{
// XXX Maybe handle (x, y) == (-1, 0) as a special case, and return 0
// instead of nan?
if (x == -1.0 && y == 0.0) {
return static_cast<T>(0.0);
}
return std::expm1(y * std::log1p(x));
}

Expand Down
5 changes: 5 additions & 0 deletions ufunclab/tests/test_pow1pm1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
def test_logexpint1(x, y, expected):
z = pow1pm1(x, y)
assert_allclose(z, expected, rtol=1e-15)


def test_edge_case():
w = pow1pm1(-1.0, 0.0)
assert w == 0.0

0 comments on commit af85b85

Please sign in to comment.