-
-
Notifications
You must be signed in to change notification settings - Fork 1k
weather: Fix incorrect rounding for negative temperatures #2194
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 |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <concepts> | ||
|
|
||
| namespace Pinetime { | ||
| namespace Utility { | ||
| // returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90 | ||
| int16_t Asin(int16_t arg); | ||
|
|
||
| static constexpr auto RoundedDiv(std::integral auto dividend, std::integral auto divisor) -> decltype(dividend / divisor) { | ||
|
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. I think adding a docstring/comment what we expect from this function would be helpful. And as @mark9064 said what are the constraints on the inputs (if any) 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. I'm all for int16_t fixed_rounding(int16_t value, int16_t divisor) {
int16_t signed_value = ((uint16_t)value >> 14u) & 0x2; //if signed 2 otherwise 0
int16_t half_divisor = (divisor / 2);
//add a half, subtract divisor conditionally on sign
return (value + half_divisor - half_divisor*signed_value) / divisor;
}
//same as above except we don't calculate divisor*
int16_t fixed_rounding_b(int16_t value, int16_t divisor) {
int16_t signed_value = (uint16_t)value >> 15u; //if signed 1 otherwise 0
int16_t half_divisor = (divisor / 2);
return (value + half_divisor - divisor * signed_value) / divisor;
}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. Or...if you trust bithacky madness, no int16_t fixed_rounding(int16_t value, int16_t divisor) {
int16_t signed_value = value >> 15u; // true evil: we use >>'s signed behavior to propagate the leading 1 across all bits, eg: we have 0xffff or 0x0000
int16_t half_divisor = (divisor / 2);
return (value + half_divisor - (divisor & signed_value)) / divisor; //we replace the * by "1" with an &
} |
||
| return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor; | ||
| } | ||
| } | ||
| } | ||
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.
I think we should note the overflow properties in a docstring. abs(dividend) + abs(divisor) / 2 must be less than the type maximum I think? Also I think it would be useful to note whether both the dividend and divisor can be negative