You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a follow-up to #188. On some architectures, romanisim is still crashing when given the following input (with env set up properly for WebbPSF, CRDS, etc):
This is due to differences in how conversion overflow is handled between different architectures. I created 2 docker images with python & numpy, one for ARM architecture and one for AMD architecture:
# Dockerfile:FROM python:3.11
RUN pip install numpy
ENTRYPOINT ["/bin/bash"]
The ARM image (which is the same architecture apple silicon processors use) doesn't have integer overflow, but the AMD image does:
This matches my observation of seeing the error when running in linux containers in AWS (AMD) but not seeing it on my local macbook machine (ARM).
Since integer overflow is Undefined Behavior, different processor types are allowed to implement it differently, but it's certainly surprising!
Solution
We can resolve this by removing the cause of the integer overflow. In this case, it's not sufficient to "clip" the value to a max of 2^32-1, since floating point imprecision makes that value equivalent to 2^31, (which overflows):
As floating point numbers get further from 0, their precision decreases, and in fact any number between 2^31 - 64 and 2^31 + 128 will end up equal to 2^31:
This is because the distance between adjacent 32bit floats is 128 when you're near 2^31 but less than it, and 256 when you're near 2^31 but greater than it. So 2^31 - 65 rounds down to 2^31 - 128, and 2^31 + 128 rounds up to 2^31 + 256.
You can use Numpy's nextafter function to get the next (or in this case, previous) floating point number at the given precision, and this can be used as the limit in the clip call:
Summary
This is a follow-up to #188. On some architectures, romanisim is still crashing when given the following input (with env set up properly for WebbPSF, CRDS, etc):
This is due to differences in how conversion overflow is handled between different architectures. I created 2 docker images with python & numpy, one for ARM architecture and one for AMD architecture:
The ARM image (which is the same architecture apple silicon processors use) doesn't have integer overflow, but the AMD image does:
This matches my observation of seeing the error when running in linux containers in AWS (AMD) but not seeing it on my local macbook machine (ARM).
Since integer overflow is Undefined Behavior, different processor types are allowed to implement it differently, but it's certainly surprising!
Solution
We can resolve this by removing the cause of the integer overflow. In this case, it's not sufficient to "clip" the value to a max of 2^32-1, since floating point imprecision makes that value equivalent to
2^31
, (which overflows):As floating point numbers get further from 0, their precision decreases, and in fact any number between
2^31 - 64
and2^31 + 128
will end up equal to2^31
:This is because the distance between adjacent 32bit floats is 128 when you're near 2^31 but less than it, and 256 when you're near 2^31 but greater than it. So
2^31 - 65
rounds down to2^31 - 128
, and2^31 + 128
rounds up to2^31 + 256
.You can use Numpy's
nextafter
function to get the next (or in this case, previous) floating point number at the given precision, and this can be used as the limit in the clip call:This solution works on both ARM and AMD architecture.
related:
Patch
I don't have permission to create branches in this repo, otherwise I'd make a PR. Here's the patch to fix:Edit: just made a PR: #194
The text was updated successfully, but these errors were encountered: