Fix rounding issue for scalar rounding #49
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found a value that I believe to be rounded incorrectly when using
FormatInfo.BFloat16. This happened when using theTowardsZerorounding mode.Minimal Example:
Output Before Fix:
Output After Fix:
ais the value that gets rounded incorrectly.bis a value that can be encoded by BFloat19.ais larger thanb, but roundingadown yields a value that is less thanb.It appears to have skipped a value when rounding down.
I dug a little and believe that the following is the root cause:
gfloat/src/gfloat/round.py
Line 76 in 9c31e1d
np.log2(vpos)appears to be rounding its result up (at least on my platform). It ends up outputting an integer (but still a float type) despite the input not being a power of 2.Calculation:
Output:
Since it is flooring
log2(vpos), I believe the code relies onvposbeing greater than2**(floor(log2(vpos))).However, since
log2(vpos)rounds up to an integer,floordoes not lower it to the correct value.This causes what would be the lsb of the mantissa to be rounded off.
I have not used the array rounding, but I expect that the following has the same issue:
gfloat/src/gfloat/round_ndarray.py
Line 91 in 9c31e1d