Hi,
I've been the whole day struggling with some weird numbers in my application, and after checking absolutely everything I finally found that the problem was not in my calculations, but in how the RangeSlider from ControlP5 was actually showing them... the good thing is that I actually cleaned the code.
I'm attaching a sample code, just a simplified version of the library example, adding three range sliders.
The issue shows when trying to set the value in the slider (you can hide decimals, but actually that they explains a lot)
This is the results you get if you run the code below:
Slider from 0-40000 set as 100-35000 shows 0.00-34947.36
Slider from 0-4000 set as 100-3500 shows 94.73-3494.73
Slider from 0-400 set as 100-350 shows 100.00-349.47
It seems clearly a problem in how is internally dealing with the decimals.
The only way you get the precise result you are asking, is if your slider is from 0-100. Anything bigger than that will be adjusted sliding the decimal period (multiplying by 10, 100, 1000… etc) resulting in an every time more inaccurate output.
I tried to send the values as floats, but it actually doesn't change anything.
I can imagine a workaround using sliders under 100 and then scaling them myself to the right value (avoiding the float issue)... but as you can imagine that would be quite nasty in terms of keeping the code as simple as it needs.
Do you have any simpler solution for this? Otherwise I just prefer to remove the GUI.
I'd much appreciate the help.
//// EXAMPLE CODE TO SHOW THE ISSUE
import controlP5.*;
ControlP5 cp5;
Range rangeL;
Range rangeS;
Range range;
void setup() {
size(700, 400);
cp5 = new ControlP5(this);
rangeL = cp5.addRange("Range 10K")
//.setDecimalPrecision(0)
.setPosition(50, 50)
.setSize(400, 10)
.setRange(0, 40000)
.setRangeValues(100, 35000)
;
rangeS = cp5.addRange("Range K")
//.setDecimalPrecision(0)
.setPosition(50, 100)
.setSize(400, 10)
.setRange(0, 4000)
.setRangeValues(100, 3500)
;
range = cp5.addRange("Range")
//.setDecimalPrecision(0)
.setPosition(50, 150)
.setSize(400, 10)
.setRange(0, 400)
.setRangeValues(100, 350)
;
}
void draw() {
background(0);
}