-
-
Notifications
You must be signed in to change notification settings - Fork 137
Fixing casting issue in fixed-point.hh #1941
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,7 +172,7 @@ class FixedPoint { | |
| return U((value - scale / 2) / scale); | ||
| } | ||
| } | ||
| return U(value + scale / 2) / U(scale); | ||
| return U((value + scale / 2) / scale); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion LGTM! This change correctly fixes the casting precision issue. The modification ensures that integer division occurs before type conversion, which prevents potential precision loss and inconsistent behavior when converting between signed and unsigned types. By performing However, this change creates an inconsistency with other similar methods in the class. Consider applying the same pattern to the Current return U(value - scale + 1) / U(scale); // and
return U(value) / U(scale);Current return U(value) / U(scale); // and
return U(value + scale - 1) / U(scale);Suggested refactor for consistency: # In floor() method:
-return U(value - scale + 1) / U(scale);
+return U((value - scale + 1) / scale);
-return U(value) / U(scale);
+return U(value / scale);
# In ceil() method:
-return U(value) / U(scale);
+return U(value / scale);
-return U(value + scale - 1) / U(scale);
+return U((value + scale - 1) / scale);This would ensure all conversion methods use the same "divide first, then cast" approach for better consistency and to prevent similar casting issues. 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rounding logic is duplicated between signed and unsigned
integer()overloads. Consider extracting a common helper to centralize this behavior and reduce duplication.