Skip to content

Commit

Permalink
bevy_ui::layout::round_ties_up
Browse files Browse the repository at this point in the history
* Added comments explaining the behaviour of the `round_ties_up` function.
* Changed the first predicate to use `<=` instead of `<`. This doesn't change the function's behaviour (since `(0.).ceil() == 0.`) but it feels like it makes it a little easier to undertand.
  • Loading branch information
ickshonpe committed Jun 24, 2023
1 parent de5653b commit 33f47a0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ pub fn ui_layout_system(
#[inline]
/// Round `value` to the closest whole integer, with ties (values with a fractional part equal to 0.5) rounded towards positive infinity.
fn round_ties_up(value: f32) -> f32 {
if 0. < value || value.fract() != 0.5 {
if 0. <= value || value.fract() != 0.5 {
// The `round` function rounds ties away from zero. For positive numbers "away from zero" is towards positive infinity.
// So for all positive values, and negative values with a fractional part not equal to 0.5, `round` returns the correct result.
value.round()
} else {
// In the remaining cases, where `value` is negative and its fractional part is equal to 0.5, we use `ceil` to round it up towards positive infinity.
value.ceil()
}
}
Expand Down

0 comments on commit 33f47a0

Please sign in to comment.