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.
Hey there.
TL;DR;
There was an overflow at some edge case when converting from the float [-1, 1]~ish value to the short [-2^15, 2^15 - 1] value for the sticks. This commit fixes it.
Not TL;DR;
When casting from float to short, most casts were "guarded" by min-max correction before casting, however some weren't guarded properly, causing the cast to yield the wrong value.
Below is a code sample showing why it happened. It mostly has to do with how casting works from higher precision types to lower precision ones.
The specific place the code fails is when in the single joy-con mode the resulting float value is multiplied by -1 before being cased to short. In the case the float value was
Int16.MinValue
(being -32768) it will result in a value of 32768, which is equal toInt16.MaxValue
(32767) + 1, so this value is out of the range that a short supports.Here's the code sample.
https://rextester.com/JEUU57427
P.S. 1 - To nitpick a bit, the value should be rounded before casting, as in DoSomething2 in the code sample, to achieve better (meaningless) accuracy. I did not change that logic, feel free to do so yourself if desired.
P.S. 2 - I'm not sure in what IDE the code is edited normally but the indention is really mixed, and VS2017 on my setup does reformat on save. I just kept the old mix formatting to keep the commit clean but I suggest you reformat the code in some commit.
Keep up the good work.