Skip to content
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

Properly propagate error in bit_truncate_relative #285

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions caput/truncate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ from cython.parallel import prange
import numpy as np
cimport numpy as cnp

from libc.math cimport fabs

cdef extern from "truncate.hpp":
inline int bit_truncate(int val, int err) nogil

Expand Down Expand Up @@ -229,7 +231,7 @@ def bit_truncate_relative_float(float[:] val, float prec):
cdef Py_ssize_t i = 0

for i in prange(n, nogil=True):
val[i] = _bit_truncate_float(val[i], prec * val[i])
val[i] = _bit_truncate_float(val[i], fabs(prec * val[i]))

return np.asarray(val)

Expand Down Expand Up @@ -258,7 +260,7 @@ def bit_truncate_relative_double(cnp.float64_t[:] val, cnp.float64_t prec):
cdef Py_ssize_t i = 0

for i in prange(n, nogil=True):
val[i] = _bit_truncate_double(val[i], prec * val[i])
val[i] = _bit_truncate_double(val[i], fabs(prec * val[i]))

return np.asarray(val, dtype=np.float64)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ def test_truncate_relative():
)
== np.asarray([32, 32], dtype=np.float64)
).all()

# Check the case where values are negative
assert (
truncate.bit_truncate_relative(
np.asarray([-32.121, 32.5], dtype=np.float32),
0.1,
)
== np.asarray([-32, 32], dtype=np.float32)
).all()
assert (
truncate.bit_truncate_relative(
np.asarray([-32.121, 32.5], dtype=np.float64),
0.1,
)
== np.asarray([-32, 32], dtype=np.float64)
).all()
Loading