Skip to content

Conversation

@mark9064
Copy link
Member

Suggested implementation from #2194

@JF002

(to whoever merging: please rebase merge to keep authorship)

@mark9064 mark9064 added this to the 1.16.0 milestone Dec 15, 2025
@mark9064 mark9064 added bug Something isn't working weather Bugs and PRs related to Weather labels Dec 15, 2025
@github-actions
Copy link

github-actions bot commented Dec 15, 2025

Build size and comparison to main:

Section Size Difference
text 382876B 96B
data 944B 0B
bss 22632B 0B

Run in InfiniEmu

@mark9064 mark9064 force-pushed the temperature-rounding branch 2 times, most recently from 544ffa1 to b1d1d71 Compare December 15, 2025 00:21
@FintasticMan
Copy link
Member

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).

@mark9064 mark9064 force-pushed the temperature-rounding branch from b1d1d71 to 54b3e26 Compare December 16, 2025 13:43
@mark9064
Copy link
Member Author

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

@FintasticMan
Copy link
Member

If we allow two different types we get the really annoying integer promotion rules we ran into last time

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.

@mark9064
Copy link
Member Author

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

@FintasticMan
Copy link
Member

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 100_i16 for example.

@mark9064
Copy link
Member Author

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));
Copy link
Contributor

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 😅

Copy link
Member Author

@mark9064 mark9064 Dec 18, 2025

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

@mark9064 mark9064 merged commit f2814dd into InfiniTimeOrg:main Dec 21, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working weather Bugs and PRs related to Weather

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants