Slider::getValue() gives different value that Slider is set to

Hello,
I have problem, probably with some types mismatch (double vs float vs int).
My Slider range is set by AudioParameterFloat from -24.00f to 24.00f, and step is set to 0.01f.
And when I set by text box editor value 0, then in sliderValueChanged() when I use:
float value = mySlider.getValue();
I get value = -0.000000536441803

I also tried: float value = (float)mySlider.getValue();
but I get the same result.

Why is that? How to prevent it? Do I need to write my own method that round float to two decimal places after comma?

For any help great thanks in advance.

I’m noticing similar behavior in my app. Have you found an answer to this?

Hmm… Sorry it was long time ago. And I don’t remember exactly what was my code.

But I suppose in that case I had set step directly on slider and simultanously on AudioProcessorParameter which was attached to slider and there was some missmatch. But I am not sure.

If it’s the case then maybe you should look something like that in your code.

I’ve just checked my current code. And I see I use only parameter to define range and step. And it works for me like that:
std::make_unique<AudioParameterFloat>( PARAM_ID, PARAM_NAME, NormalisableRange<float>( PARAM_MIN, PARAM_MAX, PARAM_STEP, PARAM_SKEW), PARAM_DEFAULT)

Thanks.

I just finished tracing through my code and found a function called “constrainedValue”. In my case I pass 55.00 to “setValue”, but getValue returns 54.99…

Below is JUCE’s “setValue” implementation. “constrainedValue” literally changed the value I sent to it.

    void setValue (double newValue, NotificationType notification)
{
    // for a two-value style slider, you should use the setMinValue() and setMaxValue()
    // methods to set the two values.
    jassert (style != TwoValueHorizontal && style != TwoValueVertical);

    newValue = constrainedValue (newValue);

    if (style == ThreeValueHorizontal || style == ThreeValueVertical)
    {
        jassert (static_cast<double> (valueMin.getValue()) <= static_cast<double> (valueMax.getValue()));

        newValue = jlimit (static_cast<double> (valueMin.getValue()),
                           static_cast<double> (valueMax.getValue()),
                           newValue);
    }

    if (newValue != lastCurrentValue)
    {
        if (valueBox != nullptr)
            valueBox->hideEditor (true);

        lastCurrentValue = newValue;

        // (need to do this comparison because the Value will use equalsWithSameType to compare
        // the new and old values, so will generate unwanted change events if the type changes)
        if (currentValue != newValue)
            currentValue = newValue;

        updateText();
        owner.repaint();
        updatePopupDisplay (newValue);

        triggerChangeMessage (notification);
    }
}