Control P5 Slider rounds incorrectly on negative number

When the Slider reaches -40 it return a -39.99 and rounds down to a -39. If every other value returned in this example is an int and base 10 why is the -40 off? And how do I fix it so my numbers are multiples of 10 from -100 to 0?

import controlP5.*;

ControlP5 cp5;
Slider testSlider;

void setup() {
  size(300,100);
  cp5 = new ControlP5(this);

  testSlider = cp5.addSlider("test_slider")    
     .setSize(200,20)
     .setRange(-100,0)
     .setValue(-40)
     .setNumberOfTickMarks(11)
     .setDecimalPrecision(0)
     ;
}

void draw() {
}
1 Like

The problem is that the number has not been rounded despite having set the decimal precision to 0

Here is the method that returns a slider’s value, note it uses Processing’s map function.

	@Override public float getValue( ) {
		return PApplet.map( _myValue , 0 , 1 , _myMinReal , _myMaxReal );
	}

If we simulate this with

float value = map(0.6, 0, 1, -100, 0);
float rounded = round(value);
println(value, rounded);

we get -39.999996 -40.0 which supports my statement in the first paragraph.

I can think of no way to make the slider display the rounded value without editing the controlP5 library source code and rebuilding it.

1 Like