Solved Odd result from Math.round
-
Hi
I have a slider to control gain on a Simple Gain module set with a scale of 0 to -100dB and a 0.1 step size and am using LAF to display the value in the slider handle. The actual numbers it spits out run to many decimal places so to round it to one dp, I'm using
labelText = Math.round(obj.value * 10) / 10 + "dB";
For most values, this works perfectly but between -8dB and -10dB it throws up odd results:
Interface: -10.0dB Interface: -9.9dB Interface: -9.800000000000001dB Interface: -9.699999999999999dB Interface: -9.300000000000001dB Interface: -9.0dB Interface: -8.800000000000001dB Interface: -8.6dB Interface: -8.300000000000001dB Interface: -8.1dB Interface: -8.0dB
When I remove the division by 10, the numbers are all integers so dividing by 10 should just move the decimal place and yet for these values it doesn't.
Any thoughts, as ever, gratefully received.
-
@M_Holloway I think this is normal. Once you round your numbers, use Engine.doubleToString to shorten the result to your desired decimals.
-
If I remember correctly the laf object should contain the value as a text string which you can output directly.
-
@Dan-Korneff Great, thanks for that; replacing the rounding operation with Engine.doubleToString has done the job
-
-
obj.valueAsText
is the thing I was thinking of. -
@M_Holloway Kyle has a fresh one for https://www.youtube.com/watch?v=qTXwRSksJPg
-
@aaronventure Fantastic,, now I get why this happens...
var n; for(i = 0; i < 101; i++) { n = i/10; Console.print("i = " + i); Console.print("n = " + n); }
Interface: n = 8.0 Interface: i = 81 Interface: n = 8.1 Interface: i = 82 Interface: n = 8.199999999999999 Interface: i = 83 Interface: n = 8.300000000000001 Interface: i = 84 Interface: n = 8.4 Interface: i = 85 Interface: n = 8.5 Interface: i = 86 Interface: n = 8.6 Interface: i = 87 Interface: n = 8.699999999999999 Interface: i = 88 Interface: n = 8.800000000000001 Interface: i = 89 Interface: n = 8.9 Interface: i = 90 Interface: n = 9.0 Interface: i = 91 Interface: n = 9.1 Interface: i = 92 Interface: n = 9.199999999999999 Interface: i = 93 Interface: n = 9.300000000000001 Interface: i = 94 Interface: n = 9.4 Interface: i = 95 Interface: n = 9.5 Interface: i = 96 Interface: n = 9.6 Interface: i = 97 Interface: n = 9.699999999999999 Interface: i = 98 Interface: n = 9.800000000000001 Interface: i = 99 Interface: n = 9.9 Interface: i = 100 Interface: n = 10.0
-
@M_Holloway you can always do a custom round float function for your interface needs.