Skip to content
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

Simplify FixedDecimal's rounding APIs #5028

Merged
merged 11 commits into from
Jun 26, 2024

Conversation

jedel1043
Copy link
Contributor

Related to #2902

Mainly removes all the _to_increment methods, which are superseeded by the round_with_mode_and_increment method.

Additionally, adds a round function that does half to even rounding.

@jedel1043 jedel1043 changed the title Fd/new rounding ops Simplify FixedDecimal's rounding APIs Jun 10, 2024
@jedel1043 jedel1043 force-pushed the fd/new-rounding-ops branch from 739fee1 to 25e8578 Compare June 10, 2024 16:41
@jedel1043 jedel1043 requested a review from a team as a code owner June 10, 2024 16:41
Manishearth
Manishearth previously approved these changes Jun 10, 2024
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

we should document these more but I guess that can happen separately

self.0.half_expand_to_increment(position, increment.into())
#[diplomat::rust_link(fixed_decimal::FixedDecimal::round, FnInStruct)]
#[diplomat::rust_link(fixed_decimal::FixedDecimal::rounded, FnInStruct, hidden)]
pub fn round(&mut self, position: i16) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: documentation on what round means here

Copy link
Member

Choose a reason for hiding this comment

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

@eggrobin wdyt about this being the default round function? Alternatively we can call this round_half_even or similar.

Copy link
Member

Choose a reason for hiding this comment

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

wdyt about this being the default round function?

Sounds reasonable to me (so long as it is documented of course, which it now is).

@jedel1043 jedel1043 requested a review from Manishearth June 11, 2024 22:50
ffi/capi/src/fixed_decimal.rs Outdated Show resolved Hide resolved
self.0.half_expand_to_increment(position, increment.into())
#[diplomat::rust_link(fixed_decimal::FixedDecimal::round, FnInStruct)]
#[diplomat::rust_link(fixed_decimal::FixedDecimal::rounded, FnInStruct, hidden)]
pub fn round(&mut self, position: i16) {
Copy link
Member

Choose a reason for hiding this comment

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

@eggrobin wdyt about this being the default round function? Alternatively we can call this round_half_even or similar.

@jedel1043 jedel1043 requested a review from sffc June 20, 2024 21:40
@sffc
Copy link
Member

sffc commented Jun 25, 2024

2024-06-20 notes:

  • @jedel1043 - I think people will be looking for a round function and we should have a round function.
  • @robertbastian - I think it's not important for us to match float rounding.
  • @jedel1043 - It's probalby because floats lose precision
  • @sffc - Was there any other reason we were hesitant for half-even being the default rounding mode in the round function?
  • @jedel1043 - Other languages use half expand.
  • @sffc - ICU4C/ICU4J I think use half even as default.
  • @sffc - Ultimately I think half even is a good default and we should do it for developer convenience.

Conclusion: Land a round function using half-even rounding.

LGTM: @sffc @jedel1043 @Manishearth @robertbastian

Would like affirmation from @eggrobin

Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

Nice! Lots of work and things to keep track of.

Comment on lines +1623 to +1633
match mode {
RoundingMode::Ceil => self.ceil_to_increment_internal(position, increment),
RoundingMode::Expand => self.expand_to_increment_internal(position, increment),
RoundingMode::Floor => self.floor_to_increment_internal(position, increment),
RoundingMode::Trunc => self.trunc_to_increment_internal(position, increment),
RoundingMode::HalfCeil => self.half_ceil_to_increment_internal(position, increment),
RoundingMode::HalfExpand => self.half_expand_to_increment_internal(position, increment),
RoundingMode::HalfFloor => self.half_floor_to_increment_internal(position, increment),
RoundingMode::HalfTrunc => self.half_trunc_to_increment_internal(position, increment),
RoundingMode::HalfEven => self.half_even_to_increment_internal(position, increment),
}
Copy link
Member

Choose a reason for hiding this comment

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

Thought: I hope the compiler figures out that it should inline round_with_mode_and_increment and all these helper functions except the 3 that contain meaningful code (expand_to_increment_internal, floor_to_increment_internal, and half_even_to_increment_internal).

Copy link
Contributor Author

@jedel1043 jedel1043 Jun 26, 2024

Choose a reason for hiding this comment

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

I checked the assembly and it inlines everything correctly. half_even_to_increment_internal is also inlined but I think is mostly because it's only used in that method.

@sffc
Copy link
Member

sffc commented Jun 26, 2024

I'm going to merge this and we can revisit the method name if @eggrobin has concerns about it. Thanks @jedel1043!

@sffc sffc merged commit cdd447e into unicode-org:main Jun 26, 2024
28 checks passed
@jedel1043 jedel1043 deleted the fd/new-rounding-ops branch June 26, 2024 07:42
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.

4 participants