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

Truncate float NaN behaviour is inconsistent across systems #280

Closed
ljgray opened this issue Sep 27, 2024 · 4 comments
Closed

Truncate float NaN behaviour is inconsistent across systems #280

ljgray opened this issue Sep 27, 2024 · 4 comments
Assignees

Comments

@ljgray
Copy link
Contributor

ljgray commented Sep 27, 2024

The following function calls

assert truncate.bit_truncate_float(32.121, np.nan) == 0
assert truncate.bit_truncate_double(32.121, np.nan) == 0

are true on linux but false on macos. Windows is untested.

With macos, calling bit_truncate_float/bit_truncate_double with err=np.nan evaluates the same as err=0.0 and returns the input argument - in this case, 32.121. This behaviour is different from err=-1 and err=np.inf, which both correctly evaluate to 0.0.

On linux (Ubuntu, at least), all three cases evaluate to 0.0.

Update: it appears that calling

np.asarray(np.nan).astype(int)

on macos evaluates to 0, wheras on linux it evaluates to -9223372036854775808, the smallest int64 value. It seems that numpy's solution is to throw a RuntimeWarning: numpy/numpy#21166

@ketiltrout Do you have any thoughts on how we should handle this in truncate?

@ketiltrout
Copy link
Member

ketiltrout commented Oct 2, 2024

Probably we should check if err is any sort of NAN (i.e. err != err), and just raise ValueError in that case, because what are we supposed to assume the caller meant?

If we don't want to do that, we should explicity set err to np.inf (i.e. act as if there's no limit) in this case.

Does this need to be handled in trunacte.hpp (i.e. are there other places using the C++ functions directly), or can we put it in the boilerplate in truncate.pyx (where' it's easier to raise exceptions)?

@ketiltrout
Copy link
Member

(If in the .hpp, the C++-ish equivalent to raising an exception would probably be to just return NaN in this case).

@ljgray
Copy link
Contributor Author

ljgray commented Oct 2, 2024

@ketiltrout Do you a preference for doing this check in the .pyx vs the .hpp file?

@ketiltrout
Copy link
Member

I would do it in the .pyx. The Python-ier the better.

@ljgray ljgray closed this as completed in 5494000 Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants