Skip to content

bevy_core_widgets slider precision rounding issues #20921

@janis-bhm

Description

@janis-bhm

Bevy version and features

eed2e0ebe

[Optional] Relevant system information

rustc 1.91.0-nightly (523d3999d 2025-08-30)
6.12.44 x86_64 12th Gen Intel i5-12600K

What went wrong

negative exponents in SliderPrecision result in inaccurate numbers (e.g. 3999.999998 instead of 4000).
This can be fixed by multiplying by the reciprocal instead of dividing by the factor again, but then the same issue appears for positive exponents (e.g. 5.319999996 instead of 5.32).

    fn round(&self, value: f32) -> f32 {
        let factor = ops::powf(10.0_f32, self.0 as f32);
        let rounded = (value * factor).round();
        if self.0.is_positive() {
            rounded / factor
        } else {
            let recip = ops::powf(10.0_f32, -self.0 as f32);
            rounded * recip
        }
    }

this results in the right apparent numbers for both positive and negative exponents, but I'm not sure if this is in any way dependent on my machine/rustc/libm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MathFundamental domain-agnostic mathematical operationsA-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behaviorD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!X-UncontroversialThis work is generally agreed upon

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions