Skip to content

Comments

Add int-based rounding functions#637

Merged
chiphogg merged 3 commits intomainfrom
chiphogg/int-round-in#243
Jan 21, 2026
Merged

Add int-based rounding functions#637
chiphogg merged 3 commits intomainfrom
chiphogg/int-round-in#243

Conversation

@chiphogg
Copy link
Member

We introduce functions such as int_round_as as alternatives for every
function in the round_as family. The new functions use bespoke logic,
while the old ones wrap STL functions such as std::round. This gives
the new functions two advantages:

  1. They stay purely in the integer domain.
  2. They can support constexpr.

It feels goofy for an integer input to have to go through floating
point, and all of its weirdness at large values.

I've never seen anything like this before, probably because the notion
of "rounding" an integer type makes little sense in general: it's
already an integer. In the realm of unit conversions, though, we do
have applications for this.

Helps #243. A future PR will add "explicit Rep" versions of these.

We introduce functions such as `int_round_as` as alternatives for every
function in the `round_as` family.  The new functions use bespoke logic,
while the old ones wrap STL functions such as `std::round`.  This gives
the new functions two advantages:

1. They stay purely in the integer domain.
2. They can support `constexpr`.

It feels goofy for an integer input to have to go through floating
point, and all of its weirdness at large values.

I've never seen anything like this before, probably because the notion
of "rounding" an integer type makes little sense in general: it's
_already_ an integer.  In the realm of unit conversions, though, we do
have applications for this.

Helps #243.  A future PR will add "explicit Rep" versions of these.
TEST(IntRoundIn, SupportsInt64) {
constexpr int64_t large_value = 9'000'000'000'000'000'500LL;
EXPECT_THAT(int_round_in(kilo(meters), meters(large_value)),
SameTypeAndValue(int64_t{9'000'000'000'000'001LL}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the int64_t here if we've got LL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think so, until the test failed. Turns out that LL does not necessarily mean "precisely int64_t".

or need `constexpr` compatibility, consider using [`int_floor_in` and
`int_floor_as`](#int_floor) instead.

#### `int_round_in`, `int_round_as` {#int_round}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looking at how the standard library uses prefixes and suffixes, should the API be int_round_as or round_int_as or round_as_int, or int_round_int_as?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot int_round_int_as_int. 😅

I wouldn't say I love the name, but I had a hard time coming up with a better one. Really, it's a shorthand for "integer-domain rounding". I wanted to keep the function name as short as I could while still making the distinction.

Now that you bring it up, I think it makes sense to explain this in the docs, so I added a sentence to each section.

chiphogg added a commit that referenced this pull request Jan 21, 2026
Very surprising that this didn't fail before, as apparently this load
statement was always needed for bzlmod.  It started failing in #637 ---
and, interestingly, only on a _second_ commit.  Still, the fix is
straightforward.
chiphogg added a commit that referenced this pull request Jan 21, 2026
Very surprising that this didn't fail before, as apparently this load
statement was always needed for bzlmod.  It started failing in #637 ---
and, interestingly, only on a _second_ commit.  Still, the fix is
straightforward.
@chiphogg chiphogg merged commit 5aa4a15 into main Jan 21, 2026
27 checks passed
@chiphogg chiphogg deleted the chiphogg/int-round-in#243 branch January 21, 2026 19:31
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