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

Fix stick casting overflow. #122

Merged
merged 1 commit into from
Mar 13, 2019
Merged

Fix stick casting overflow. #122

merged 1 commit into from
Mar 13, 2019

Conversation

idan-weizman
Copy link
Contributor

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 to Int16.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.

@Davidobot
Copy link
Owner

Thank you very much for picking up on this. I did notice that just sometimes, the values would jump around, but I didn't find the time to look into it.

@Davidobot Davidobot merged commit 1fe6c41 into Davidobot:master Mar 13, 2019
d3xMachina pushed a commit to d3xMachina/BetterJoy that referenced this pull request Nov 19, 2021
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

Successfully merging this pull request may close these issues.

2 participants