Repository navigation
Closed
Labels
bugSomething isn't working
Description
This has been edited to reflect new requirements for this issues the original is below
Right now our mod is incorrect for large scalars uint/float and for edge cases in the modulus (+/-np.inf, '+/-0.0`)
Attempting to update it to fmod correctly handles large scalars but is not incorrect on negative floats and edge cases both being modded and doing the modding.
In this issue we should:
- add
ak.fmodwhich matchesnp.fmodusing the chplfmodfunction - update
ak.modto matchnp.modby doing something similar tofloorDivisionHelper, by cleverly modifying the return of the chplfmod
ORIGINAL:
While working #2508, I found the section of test_divmod that says it's putting the edge cases in the denominator doesn't do that and is identical to the numerator
Lines 235 to 251 in 2034dcb
| # Edge cases in the numerator | |
| edge_case = [-np.inf, -7.0, -0.0, np.nan, 0.0, 7.0, np.inf] | |
| np_edge_case = np.array(edge_case) | |
| ak_edge_case = ak.array(np_edge_case) | |
| ak_div, ak_mod = ak.divmod(ak_edge_case, ak.arange(1, len(edge_case)+1)) | |
| np_div, np_mod = np.divmod(np_edge_case, np.arange(1, len(edge_case)+1)) | |
| self.assertTrue(np.allclose(ak_div.to_ndarray(), np_div, equal_nan=True)) | |
| self.assertTrue(np.allclose(ak_mod.to_ndarray(), np_mod, equal_nan=True)) | |
| # Edge cases in the denominator | |
| edge_case = [-np.inf, -7.0, np.nan, 7.0, np.inf] | |
| np_edge_case = np.array(edge_case) | |
| ak_edge_case = ak.array(np_edge_case) | |
| ak_div, ak_mod = ak.divmod(ak_edge_case, ak.arange(1, len(edge_case)+1)) | |
| np_div, np_mod = np.divmod(np_edge_case, np.arange(1, len(edge_case)+1)) | |
| self.assertTrue(np.allclose(ak_div.to_ndarray(), np_div, equal_nan=True)) | |
| self.assertTrue(np.allclose(ak_mod.to_ndarray(), np_mod, equal_nan=True)) |
I ran some quick tests on master and mod behaves incorrectly with the float edge cases in the denominator
>>> edge_case = [-np.inf, -7.0, np.nan, 7.0, np.inf]
>>> np_edge_case = np.array(edge_case)
>>> ak_edge_case = ak.array(np_edge_case)
>>> ak_ind = ak.arange(1, len(edge_case)+1)
>>> np_ind = np.arange(1, len(edge_case)+1)
# edge cases in numerator (behaves correctly)
>>> ak_edge_case % ak_ind
array([nan 1 nan 3 nan])
>>> np_edge_case % np_ind
array([nan, 1., nan, 3., nan])
# edge cases in denominator (incorrect results)
>>> ak_ind % ak_edge_case
array([nan -5 nan 4 nan])
>>> np_ind % np_edge_case
array([-inf, -5., nan, 4., 5.])
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
stress-tess commentedon Jun 21, 2023
In the issue in which this was discovered, we are switching
modto usefmod. While doing this I encountered different behavior for negative floats.It seems like it's a well documented thing and
%andfmodare meant to behave differently. There's anfmodfunction both in python's math module and in numpy for this reason.I think we should have a
modandfmodas well to match numpy since these do different thingsnp.mod
np.fmod
Ideally these would map one-to-one onto the chapel
modandfmodfunctions. I added a comment expressing this to the issue i posted in their repostress-tess commentedon Jun 21, 2023
More information about the different definitions can be found here
python and numpy's
%/moduses the floored definition and theirfmoduses the truncated definition. I asked the chapel folks what definition theirmodis using[-]incorrect results for `mod` with float edge_cases[/-][+]incorrect results for `mod` with float edge_cases and negatives[/+]stress-tess commentedon Jun 22, 2023
playing around with this a bit with @jaketrookman, it looks like a lot of the time
modisfmod + moduluswith some exceptions (maybe when evenly divides). Theres some other things like how the sign is determined. hopefully we can look at the definitions and try to find an easy way to convert the results fromfmodinto what we expect frommod[-]incorrect results for `mod` with float edge_cases and negatives[/-][+]overhaul our `mod` functionality[/+]Closes Bears-R-Us#2519: Overhaul `mod`
Fixes #2508 and #2519: Fixes `uint` scalar handling and overhauls `mo…