-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix negative temperature rounding #2390
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
Conversation
|
Build size and comparison to main:
|
544ffa1 to
b1d1d71
Compare
|
This looks good! Sorry for not being very involved recently. I do suggest using an implementation that rounds away from zero as opposed to one that rounds towards zero, as that is more commonly used and is more intuitive. Also, are the static casts necessary for the integer literals? Can't the compiler figure the type out without it? If they are necessary it might be good to have the function take 2 type parameters to avoid that (that might make the overflow cases harder to reason about though, haven't thought it through much). |
b1d1d71 to
54b3e26
Compare
|
No worries on being busy 😄 really appreciate the review Good point on the rounding mode, I've switched it to round half away from zero 👍 The static casts are unfortunately necessary. If we allow two different types we get the really annoying integer promotion rules we ran into last time, so I think it's best to stick with something stricter where we can be confident the behaviour is correct. I agree that the required casts are annoying though :/ just not sure there's a good alternative |
In theory we should only run into those issues if either of the types is unsigned, which can be documented? Not sure if that's a good solution though haha. |
|
Yeah I'd prefer to avoid any scenario where it's easy to get bad behaviour without realising (i.e. relying on documentation). I think a couple of extra casts are preferable to an incorrect output |
|
We could add something like this: https://stackoverflow.com/questions/36406333/fixed-width-integer-literals-in-c. Then we don't have to use static casts directly, and we can just do |
|
A rust style literal suffix would be really nice actually from a usability standpoint. Why isn't this part of the language itself yet... I'm a bit split if it's a good idea for the project though. Contributors probably won't expect a language extension like that and I don't like having two (well three if you count C style casts) ways to do the same thing. Maybe I'm overthinking this? |
|
|
||
| T m = dividend % divisor; | ||
| T h = divisor / 2 + divisor % 2; | ||
| T res = (dividend / divisor) + (!(dividend < 0) & (m >= h)) - ((dividend < 0) & ((m + h) <= 0)); |
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 may be missing something, but m and h are purely positive or 0. so the m+h should only ve true if both are 0?
so maybe I dont understand the negative divident case 😅
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.
m can be negative. For example RoundedDiv(-23, 17) would make m = -23 % 17 = -6
Suggested implementation from #2194
@JF002
(to whoever merging: please rebase merge to keep authorship)